ovs-command-completion: Autotest integration.
[cascardo/ovs.git] / tests / completion.at
1 AT_BANNER([command completion unit tests - bash])
2
3 m4_define([GET_FORMAT], [
4 echo "$@" | grep -A 1 -- "Command format" | tail -n+2
5 ])
6
7 m4_define([GET_EXPAN], [
8 echo "$@" | grep -- "available completions for keyword" \
9      | sed -e 's/^[ ]*//g;s/[ ]*$//g'
10 ])
11
12 m4_define([GET_AVAIL], [
13 echo "$@" | sed -e '1,/Available/d' | tail -n+2
14 ])
15
16 m4_define([GET_COMP_STR], [
17 echo "available completions for keyword \"$1\": $2" \
18      | sed -e 's/[ ]*$//g'
19 ])
20
21 AT_SETUP([bash completion - basic verification])
22 AT_SKIP_IF([test -z ${BASH_VERSION+x}])
23 OVS_VSWITCHD_START
24
25 # complete ovs-appctl [TAB]
26 # complete ovs-dpctl  [TAB]
27 # complete ovs-ofctl  [TAB]
28 # complete ovsdb-tool [TAB]
29 m4_foreach(
30 [test_command],
31 [[ovs-appctl],
32 [ovs-dpctl],
33 [ovs-ofctl],
34 [ovsdb-tool]],
35 [
36 INPUT="$(bash ovs-command-compgen.bash debug test_command TAB 2>&1)"
37 MATCH="$(test_command --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)
38 $(test_command list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
39 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
40 [0], [dnl
41 ${MATCH}
42 ])])
43
44
45 # complete ovs-appctl --tar[TAB]
46 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --tar 2>&1)"
47 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
48 [0], [dnl
49 --target
50 ])
51
52
53 # complete ovs-appctl --target [TAB]
54 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target TAB 2>&1)"
55 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
56 [0], [dnl
57 ovs-ofctl
58 ovs-vswitchd
59 ovsdb-server
60 ])
61
62
63 # complete ovs-appctl --target ovs-vswitchd [TAB]
64 # complete ovs-appctl --target ovsdb-server [TAB]
65 # complete ovs-appctl --target ovs-ofctl    [TAB]
66 AT_CHECK([ovs-ofctl monitor br0 --detach --no-chdir --pidfile])
67 m4_foreach(
68 [target_daemon],
69 [[ovs-vswitchd],
70 [ovsdb-server],
71 [ovs-ofctl]],
72 [
73 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target target_daemon TAB 2>&1)"
74 MATCH="$(ovs-appctl --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)
75 $(ovs-appctl --target target_daemon list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
76 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
77 [0], [dnl
78 ${MATCH}
79 ])])
80 AT_CHECK([ovs-appctl --target ovs-ofctl exit])
81
82
83 # check all ovs-appctl subcommand formats
84 LIST=$(ovs-appctl list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)
85 for subcommand in $LIST; do
86     INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl $subcommand TAB 2>&1)"
87     MATCH=$(ovs-appctl list-commands | tail -n+2 | cut -c3- | grep -- "^$subcommand " | tr -s ' ')
88     AT_CHECK_UNQUOTED([GET_FORMAT(${INPUT})],
89 [0], [dnl
90 ${MATCH}
91 ])
92 done
93
94 OVS_VSWITCHD_STOP
95 AT_CLEANUP
96
97
98 # complex completion check - bfd/set-forwarding
99 # bfd/set-forwarding [interface] normal|false|true
100 # test expansion of 'interface'
101 AT_SETUP([bash completion - complex completion check 1])
102 AT_SKIP_IF([test -z ${BASH_VERSION+x}])
103 OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy)
104
105 # check the top level completion.
106 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding TAB 2>&1)"
107 MATCH="$(GET_COMP_STR([normal], [])
108 GET_COMP_STR([false], [])
109 GET_COMP_STR([true], [])
110 GET_COMP_STR([interface], [p0]))"
111 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
112 [0], [dnl
113 ${MATCH}
114 ])
115 # check the available completions.
116 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
117 [0], [dnl
118 p0
119 ])
120
121
122 # set argument to 'true', there should be no more completions.
123 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding true TAB 2>&1)"
124 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
125 [0], [dnl
126 Command format:
127 bfd/set-forwarding [[interface]] normal|false|true
128 ])
129
130
131 # set argument to 'p1', there should still be the completion for booleans.
132 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding p1 TAB 2>&1)"
133 MATCH="$(GET_COMP_STR([normal], [])
134 GET_COMP_STR([false], [])
135 GET_COMP_STR([true], []))"
136 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
137 [0], [dnl
138 ${MATCH}
139 ])
140 # check the available completions.
141 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])
142
143
144 # set argument to 'p1 false', there should still no more completions.
145 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding p1 false TAB 2>&1)"
146 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
147 [0], [dnl
148 Command format:
149 bfd/set-forwarding [[interface]] normal|false|true
150 ])
151
152 OVS_VSWITCHD_STOP
153 AT_CLEANUP
154
155
156 # complex completion check - lacp/show
157 # lacp/show [port]
158 # test expansion on 'port'
159 AT_SETUP([bash completion - complex completion check 2])
160 AT_SKIP_IF([test -z ${BASH_VERSION+x}])
161 OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy \
162                    -- add-port br0 p1 -- set Interface p1 type=dummy)
163
164 # check the top level completion.
165 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show TAB 2>&1)"
166 MATCH="$(GET_COMP_STR([port], [br0 p0 p1]))"
167 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
168 [0], [dnl
169 ${MATCH}
170 ])
171 # check the available completions.
172 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
173 [0], [dnl
174 br0
175 p0
176 p1
177 ])
178
179
180 # set argument to 'p1', there should be no more completions.
181 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show p1 TAB 2>&1)"
182 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
183 [0], [dnl
184 Command format:
185 lacp/show [[port]]
186 ])
187
188 OVS_VSWITCHD_STOP
189 AT_CLEANUP
190
191
192 # complex completion check - ofproto/trace
193 # ofproto/trace {[dp_name] odp_flow | bridge br_flow} [-generate|packet]
194 # test expansion on 'dp|dp_name' and 'bridge'
195 AT_SETUP([bash completion - complex completion check 3])
196 AT_SKIP_IF([test -z ${BASH_VERSION+x}])
197 OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy \
198                    -- add-port br0 p1 -- set Interface p1 type=dummy)
199
200 # check the top level completion.
201 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace TAB 2>&1)"
202 MATCH="$(GET_COMP_STR([bridge], [br0])
203 GET_COMP_STR([odp_flow], [])
204 GET_COMP_STR([dp_name], [ovs-dummy]))"
205 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
206 [0], [dnl
207 ${MATCH}
208 ])
209 # check the available completions.
210 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
211 [0], [dnl
212 br0
213 ovs-dummy
214 ])
215
216
217 # set argument to 'ovs-dummy', should go to the dp-name path.
218 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-dummy TAB 2>&1)"
219 MATCH="$(GET_COMP_STR([odp_flow], []))"
220 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
221 [0], [dnl
222 ${MATCH}
223 ])
224 # check the available completions.
225 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])
226
227
228 # set odp_flow to some random string, should go to the next level.
229 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-dummy "in_port(123),mac(),ip,tcp" TAB 2>&1)"
230 MATCH="$(GET_COMP_STR([-generate], [-generate])
231 GET_COMP_STR([packet], []))"
232 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
233 [0], [dnl
234 ${MATCH}
235 ])
236 # check the available completions.
237 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
238 [0], [dnl
239 -generate
240 ])
241
242
243 # set packet to some random string, there should be no more completions.
244 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-dummy "in_port(123),mac(),ip,tcp" "ABSJDFLSDJFOIWEQR" TAB 2>&1)"
245 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
246 [0], [dnl
247 Command format:
248 ofproto/trace {[[dp_name]] odp_flow | bridge br_flow} [[-generate|packet]]
249 ])
250
251
252 # set argument to 'br0', should go to the bridge path.
253 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace br0 TAB 2>&1)"
254 MATCH="$(GET_COMP_STR([br_flow], []))"
255 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
256 [0], [dnl
257 ${MATCH}
258 ])
259 # check the available completions.
260 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])
261
262
263 # set argument to some random string, should go to the odp_flow path.
264 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace "in_port(123),mac(),ip,tcp" TAB 2>&1)"
265 MATCH="$(GET_COMP_STR([-generate], [-generate])
266 GET_COMP_STR([packet], []))"
267 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
268 [0], [dnl
269 ${MATCH}
270 ])
271 # check the available completions.
272 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
273 [0], [dnl
274 -generate
275 ])
276
277 OVS_VSWITCHD_STOP
278 AT_CLEANUP
279
280
281 # complex completion check - vlog/set
282 # vlog/set {spec | PATTERN:destination:pattern}
283 # test non expandable arguments
284 AT_SETUP([bash completion - complex completion check 4])
285 AT_SKIP_IF([test -z ${BASH_VERSION+x}])
286 OVS_VSWITCHD_START
287
288 # check the top level completion.
289 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl vlog/set TAB 2>&1)"
290 MATCH="$(GET_COMP_STR([PATTERN:destination:pattern], [])
291 GET_COMP_STR([spec], []))"
292 AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
293 [0], [dnl
294 ${MATCH}
295 ])
296 # check the available completions.
297 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])
298
299
300 # set argument to random 'abcd', there should be no more completions.
301 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl vlog/set abcd TAB 2>&1)"
302 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
303 [0], [dnl
304 Command format:
305 vlog/set {spec | PATTERN:destination:pattern}
306 ])
307
308 OVS_VSWITCHD_STOP
309 AT_CLEANUP
310
311
312 AT_SETUP([bash completion - negative test])
313 AT_SKIP_IF([test -z ${BASH_VERSION+x}])
314 OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy)
315
316 # negative test - incorrect subcommand
317 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ERROR 2>&1)"
318 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e 's/[ \t]*$//' | sed -e '/./,$!d'], [0])
319 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ERROR TAB 2>&1)"
320 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e 's/[ \t]*$//' | sed -e '/./!d'],
321 [0], [dnl
322 Command format:
323 ])
324
325
326 # negative test - no ovs-vswitchd
327 # negative test - no ovsdb-server
328 # negative test - no ovs-ofctl
329 # should not see any error.
330 OVS_VSWITCHD_STOP
331 m4_foreach(
332 [target_daemon],
333 [[ovs-vswitchd],
334 [ovsdb-server],
335 [ovs-ofctl]],
336 [
337 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target target_daemon TAB 2>&1)"
338 MATCH="$(ovs-appctl --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)"
339 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
340 [0], [dnl
341 ${MATCH}
342 ])
343 INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target target_daemon ERROR SUBCMD TAB 2>&1)"
344 AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e 's/[ \t]*$//' | sed -e '/./!d'],
345 [0], [dnl
346 Command format:
347 ])])
348
349
350 # negative test - do not match on nested option
351 INPUT="$(bash ovs-command-compgen.bash debug ovsdb-tool create TAB 2>&1)"
352 AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])
353
354 AT_CLEANUP