blob: 214512d269e8da6fc1265503c9901a97fb3c82f0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#!/bin/bash
cd $(dirname $0)
nft=${NFT:-../../src/nft}
debug=false
test_json=false
mydiff() {
diff -w -I '^# ' "$@"
}
err() {
echo "$*" >&2
}
die() {
err "$*"
exit 1
}
if [ "$(id -u)" != "0" ] ; then
die "this requires root!"
fi
testdir=$(mktemp -d)
if [ ! -d $testdir ]; then
die "Failed to create test directory"
fi
trap 'rm -rf $testdir; $nft flush ruleset' EXIT
command_file=$(mktemp -p $testdir)
output_file=$(mktemp -p $testdir)
cmd_append() {
echo "$*" >>$command_file
}
monitor_output_append() {
[[ "$*" == '-' ]] && {
cat $command_file >>$output_file
return
}
echo "$*" >>$output_file
}
echo_output_append() {
# this is a bit tricky: for replace commands, nft prints a delete
# command - so in case there is a replace command in $command_file,
# just assume any other commands in the same file are sane
grep -q '^replace' $command_file >/dev/null 2>&1 && {
monitor_output_append "$*"
return
}
[[ "$*" == '-' ]] && {
grep '^\(add\|replace\|insert\)' $command_file >>$output_file
return
}
[[ "$*" =~ ^add|replace|insert ]] && echo "$*" >>$output_file
}
json_output_filter() { # (filename)
# unify handle values
sed -i -e 's/\("handle":\) [0-9][0-9]*/\1 0/g' "$1"
}
monitor_run_test() {
monitor_output=$(mktemp -p $testdir)
monitor_args=""
$test_json && monitor_args="vm json"
local rc=0
$nft -nn monitor $monitor_args >$monitor_output &
monitor_pid=$!
sleep 0.5
$debug && {
echo "command file:"
cat $command_file
}
$nft -f - <$command_file || {
err "nft command failed!"
rc=1
}
sleep 0.5
kill $monitor_pid
wait >/dev/null 2>&1
$test_json && json_output_filter $monitor_output
mydiff -q $monitor_output $output_file >/dev/null 2>&1
if [[ $rc == 0 && $? != 0 ]]; then
err "monitor output differs!"
mydiff -u $output_file $monitor_output >&2
rc=1
fi
rm $command_file
rm $output_file
touch $command_file
touch $output_file
return $rc
}
echo_run_test() {
echo_output=$(mktemp -p $testdir)
local rc=0
$debug && {
echo "command file:"
cat $command_file
}
$nft -nn -e -f - <$command_file >$echo_output || {
err "nft command failed!"
rc=1
}
mydiff -q $echo_output $output_file >/dev/null 2>&1
if [[ $rc == 0 && $? != 0 ]]; then
err "echo output differs!"
mydiff -u $output_file $echo_output >&2
rc=1
fi
rm $command_file
rm $output_file
touch $command_file
touch $output_file
return $rc
}
netns=true
for arg in "$@"; do
[[ "$arg" == "--no-netns" ]] && netns=false
done
if $netns; then
exec unshare -n $0 --no-netns "$@"
fi
testcases=""
while [ -n "$1" ]; do
case "$1" in
-d|--debug)
debug=true
shift
;;
-j|--json)
test_json=true
shift
;;
--no-netns)
shift
;;
-H|--host)
nft=nft
shift
;;
testcases/*.t)
testcases+=" $1"
shift
;;
*)
echo "unknown option '$1'"
;&
-h|--help)
echo "Usage: $(basename $0) [-j|--json] [-d|--debug] [testcase ...]"
exit 1
;;
esac
done
if $test_json; then
variants="monitor"
else
variants="monitor echo"
fi
rc=0
for variant in $variants; do
run_test=${variant}_run_test
output_append=${variant}_output_append
for testcase in ${testcases:-testcases/*.t}; do
filename=$(basename $testcase)
echo "$variant: running tests from file $filename"
rc_start=$rc
# files are like this:
#
# I add table ip t
# O add table ip t
# I add chain ip t c
# O add chain ip t c
$nft flush ruleset
input_complete=false
while read dir line; do
case $dir in
I)
$input_complete && {
$run_test
let "rc += $?"
}
input_complete=false
cmd_append "$line"
;;
O)
input_complete=true
$test_json || $output_append "$line"
;;
J)
input_complete=true
$test_json && $output_append "$line"
;;
'#'|'')
# ignore comments and empty lines
;;
esac
done <$testcase
$input_complete && {
$run_test
let "rc += $?"
}
let "rc_diff = rc - rc_start"
[[ $rc_diff -ne 0 ]] && \
echo "$variant: $rc_diff tests from file $filename failed"
done
done
exit $rc
|