3 # Tests for the ovs-command-compgen.bash
5 # Please run this with ovs-command-compgen.bash script inside
6 # ovs-sandbox, under the same directory.
8 # For information about running the ovs-sandbox, please refer to
9 # the tutorial directory.
19 TEST_COMMANDS=(ovs-appctl ovs-ofctl ovs-dpctl ovsdb-tool)
20 TEST_APPCTL_TARGETS=(ovs-vswitchd ovsdb-server ovs-ofctl)
25 get_command_format() {
28 echo "$(grep -A 1 "Command format" <<< "$input" | tail -n+2)"
31 get_argument_expansion() {
34 echo "$(grep -- "available completions for keyword" <<< "$input" | sed -e 's/^[ \t]*//')"
37 get_available_completions() {
40 echo "$(sed -e '1,/Available/d' <<< "$input" | tail -n+2)"
43 generate_expect_completions() {
45 local completions="$2"
47 echo "available completions for keyword \"$keyword\": $completions" \
48 | sed -e 's/[ \t]*$//'
64 printf "%2d: %-70s %s\n" "$TEST_COUNTER" "$1" "$2"
77 printf "failed at stage_%s:\n" "$stage"
78 printf "actual output: %s\n" "$actual"
79 printf "expect output: %s\n" "$expect"
88 local comp_output tmp expect
90 if [ -n "$target" ]; then
91 target_line="--target $target"
93 comp_output="$(bash ovs-command-compgen.bash debug ovs-appctl $target_line TAB 2>&1)"
94 tmp="$(get_available_completions "$comp_output")"
95 expect="$(ovs-appctl --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)
96 $(ovs-appctl $target_line list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
97 if [ "$tmp" = "$expect" ]; then
108 ovs-vsctl add-port br0 p1
116 ## ------------------------------- ##
117 ## ovs-command-compgen unit tests. ##
118 ## ------------------------------- ##
123 # complete ovs-appctl [TAB]
124 # complete ovs-dpctl [TAB]
125 # complete ovs-ofctl [TAB]
126 # complete ovsdb-tool [TAB]
128 for test_command in ${TEST_COMMANDS[@]}; do
131 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ${test_command} TAB 2>&1)"
132 TMP="$(get_available_completions "$COMP_OUTPUT")"
133 EXPECT="$(${test_command} --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)
134 $(${test_command} list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
135 if [ "$TMP" = "$EXPECT" ]; then
141 print_result "complete ${test_command} [TAB]" "$TEST_RESULT"
145 # complete ovs-appctl --tar[TAB]
149 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --tar 2>&1)"
150 TMP="$(get_available_completions "$COMP_OUTPUT")"
152 if [ "$TMP" = "$EXPECT" ]; then
158 print_result "complete ovs-appctl --targ[TAB]" "$TEST_RESULT"
161 # complete ovs-appctl --target [TAB]
165 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target TAB 2>&1)"
166 TMP="$(get_available_completions "$COMP_OUTPUT")"
167 EXPECT="$(echo ${TEST_APPCTL_TARGETS[@]} | tr ' ' '\n' | sort)"
168 if [ "$TMP" = "$EXPECT" ]; then
174 print_result "complete ovs-appctl --target [TAB]" "$TEST_RESULT"
177 # complete ovs-appctl --target ovs-vswitchd [TAB]
178 # complete ovs-appctl --target ovsdb-server [TAB]
179 # complete ovs-appctl --target ovs-ofctl [TAB]
183 for target in ${TEST_APPCTL_TARGETS[@]}; do
184 target_field="--target $i "
186 if [ "$target" = "ovs-ofctl" ]; then
187 ovs-ofctl monitor br0 --detach --no-chdir --pidfile
190 TEST_RESULT="$(ovs_apptcl_TAB $target)"
192 print_result "complete ovs-appctl ${target_field}[TAB]" "$TEST_RESULT"
194 if [ "$target" = "ovs-ofctl" ]; then
195 ovs-appctl --target ovs-ofctl exit
200 # check all subcommand formats
204 TMP="$(ovs-appctl list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
206 # for each subcmd, check the print of subcmd format
208 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl $i TAB 2>&1)"
209 tmp="$(get_command_format "$COMP_OUTPUT")"
210 EXPECT="$(ovs-appctl list-commands | tail -n+2 | cut -c3- | grep -- "^$i " | tr -s ' ' | sort)"
211 if [ "$tmp" = "$EXPECT" ]; then
219 print_result "check all subcommand format" "$TEST_RESULT"
222 # complex completion check - bfd/set-forwarding
223 # bfd/set-forwarding [interface] normal|false|true
224 # test expansion of 'interface'
228 for i in loop_once; do
229 # check the top level completion.
230 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding TAB 2>&1)"
231 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
232 EXPECT="$(generate_expect_completions "normal" "")
233 $(generate_expect_completions "false" "")
234 $(generate_expect_completions "true" "")
235 $(generate_expect_completions "interface" "p1")"
236 if [ "$TMP" != "$EXPECT" ]; then
237 print_error "1" "$TMP" "$EXPECT"
242 # check the available completions.
243 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
245 if [ "$TMP" != "$EXPECT" ]; then
246 print_error "2" "$TMP" "$EXPECT"
251 # set argument to 'true', there should be no more completions.
252 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding true TAB 2>&1)"
253 TMP="$(sed -e '/./,$!d' <<< "$COMP_OUTPUT")"
254 EXPECT="Command format:
255 bfd/set-forwarding [interface] normal|false|true"
256 if [ "$TMP" != "$EXPECT" ]; then
257 print_error "3" "$TMP" "$EXPECT"
262 # set argument to 'p1', there should still be the completion for booleans.
263 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding p1 TAB 2>&1)"
264 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
265 EXPECT="$(generate_expect_completions "normal" "")
266 $(generate_expect_completions "false" "")
267 $(generate_expect_completions "true" "")"
268 if [ "$TMP" != "$EXPECT" ]; then
269 print_error "4" "$TMP" "$EXPECT"
274 # check the available completions.
275 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
277 if [ "$TMP" != "$EXPECT" ]; then
278 print_error "5" "$TMP" "$EXPECT"
283 # set argument to 'p1 false', there should still no more completions.
284 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding p1 false TAB 2>&1)"
285 TMP="$(sed -e '/./,$!d' <<< "$COMP_OUTPUT")"
286 EXPECT="Command format:
287 bfd/set-forwarding [interface] normal|false|true"
288 if [ "$TMP" != "$EXPECT" ]; then
289 print_error "6" "$TMP" "$EXPECT"
297 print_result "complex completion check - bfd/set-forwarding" "$TEST_RESULT"
300 # complex completion check - lacp/show
302 # test expansion on 'port'
306 for i in loop_once; do
307 # check the top level completion.
308 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show TAB 2>&1)"
309 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
310 EXPECT="$(generate_expect_completions "port" "br0 p1")"
311 if [ "$TMP" != "$EXPECT" ]; then
312 print_error "1" "$TMP" "$EXPECT"
317 # check the available completions.
318 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
320 if [ "$TMP" != "$EXPECT" ]; then
321 print_error "2" "$TMP" "$EXPECT"
326 # set argument to 'p1', there should be no more completions.
327 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show p1 TAB 2>&1)"
328 TMP="$(sed -e '/./,$!d' <<< "$COMP_OUTPUT")"
329 EXPECT="Command format:
331 if [ "$TMP" != "$EXPECT" ]; then
332 print_error "3" "$TMP" "$EXPECT"
340 print_result "complex completion check - lacp/show" "$TEST_RESULT"
343 # complex completion check - ofproto/trace
344 # ofproto/trace {[dp_name] odp_flow | bridge br_flow} [-generate|packet]
345 # test expansion on 'dp|dp_name' and 'bridge'
349 for i in loop_once; do
350 # check the top level completion.
351 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace TAB 2>&1)"
352 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
353 EXPECT="$(generate_expect_completions "bridge" "br0")
354 $(generate_expect_completions "odp_flow" "")
355 $(generate_expect_completions "dp_name" "ovs-system")"
356 if [ "$TMP" != "$EXPECT" ]; then
357 print_error "1" "$TMP" "$EXPECT"
362 # check the available completions.
363 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
364 EXPECT="br0 ovs-system"
365 if [ "$TMP" != "$EXPECT" ]; then
366 print_error "2" "$TMP" "$EXPECT"
371 # set argument to 'ovs-system', should go to the dp-name path.
372 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-system TAB 2>&1)"
373 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
374 EXPECT="$(generate_expect_completions "odp_flow" "")"
375 if [ "$TMP" != "$EXPECT" ]; then
376 print_error "3" "$TMP" "$EXPECT"
381 # check the available completions.
382 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
384 if [ "$TMP" != "$EXPECT" ]; then
385 print_error "4" "$TMP" "$EXPECT"
390 # set odp_flow to some random string, should go to the next level.
391 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-system "in_port(123),mac(),ip,tcp" TAB 2>&1)"
392 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
393 EXPECT="$(generate_expect_completions "-generate" "-generate")
394 $(generate_expect_completions "packet" "")"
395 if [ "$TMP" != "$EXPECT" ]; then
396 print_error "5" "$TMP" "$EXPECT"
401 # check the available completions.
402 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
404 if [ "$TMP" != "$EXPECT" ]; then
405 print_error "6" "$TMP" "$EXPECT"
410 # set packet to some random string, there should be no more completions.
411 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-system "in_port(123),mac(),ip,tcp" "ABSJDFLSDJFOIWEQR" TAB 2>&1)"
412 TMP="$(sed -e '/./,$!d' <<< "$COMP_OUTPUT")"
413 EXPECT="Command format:
414 ofproto/trace {[dp_name] odp_flow | bridge br_flow} [-generate|packet]"
415 if [ "$TMP" != "$EXPECT" ]; then
416 print_error "7" "$TMP" "$EXPECT"
421 # set argument to 'br0', should go to the bridge path.
422 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace br0 TAB 2>&1)"
423 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
424 EXPECT="$(generate_expect_completions "br_flow" "")"
425 if [ "$TMP" != "$EXPECT" ]; then
426 print_error "8" "$TMP" "$EXPECT"
431 # check the available completions.
432 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
434 if [ "$TMP" != "$EXPECT" ]; then
435 print_error "9" "$TMP" "$EXPECT"
440 # set argument to some random string, should go to the odp_flow path.
441 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace "in_port(123),mac(),ip,tcp" TAB 2>&1)"
442 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
443 EXPECT="$(generate_expect_completions "-generate" "-generate")
444 $(generate_expect_completions "packet" "")"
445 if [ "$TMP" != "$EXPECT" ]; then
446 print_error "10" "$TMP" "$EXPEC"T
451 # check the available completions.
452 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
454 if [ "$TMP" != "$EXPECT" ]; then
455 print_error "11" "$TMP" "$EXPECT"
463 print_result "complex completion check - ofproto/trace" "$TEST_RESULT"
466 # complex completion check - vlog/set
467 # vlog/set {spec | PATTERN:destination:pattern}
468 # test non expandable arguments
472 for i in loop_once; do
473 # check the top level completion.
474 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl vlog/set TAB 2>&1)"
475 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
476 EXPECT="$(generate_expect_completions "PATTERN:destination:pattern" "")
477 $(generate_expect_completions "spec" "")"
478 if [ "$TMP" != "$EXPECT" ]; then
479 print_error "1" "$TMP" "$EXPECT"
484 # check the available completions.
485 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
487 if [ "$TMP" != "$EXPECT" ]; then
488 print_error "2" "$TMP" "$EXPECT"
493 # set argument to random 'abcd', there should be no more completions.
494 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl vlog/set abcd TAB 2>&1)"
495 TMP="$(sed -e '/./,$!d' <<< "$COMP_OUTPUT")"
496 EXPECT="Command format:
497 vlog/set {spec | PATTERN:destination:pattern}"
498 if [ "$TMP" != "$EXPECT" ]; then
499 print_error "3" "$TMP" "$EXPECT"
507 print_result "complex completion check - vlog/set" "$TEST_RESULT"
510 # complete after delete port
513 ovs-vsctl del-port p1
515 for i in loop_once; do
516 # check match on interface, there should be no available interface expansion.
517 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding TAB 2>&1)"
518 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
519 EXPECT="$(generate_expect_completions "normal" "")
520 $(generate_expect_completions "false" "")
521 $(generate_expect_completions "true" "")
522 $(generate_expect_completions "interface" "")"
523 if [ "$TMP" != "$EXPECT" ]; then
524 print_error "1" "$TMP" "$EXPECT"
529 # check the available completions.
530 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
532 if [ "$TMP" != "$EXPECT" ]; then
533 print_error "2" "$TMP" "$EXPECT"
538 # check match on port, there should be no p1 as port.
539 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show TAB 2>&1)"
540 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
541 EXPECT="$(generate_expect_completions "port" "br0")"
542 if [ "$TMP" != "$EXPECT" ]; then
543 print_error "3" "$TMP" "$EXPECT"
548 # check the available completions.
549 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
551 if [ "$TMP" != "$EXPECT" ]; then
552 print_error "4" "$TMP" "$EXPECT"
560 print_result "complete after delete port" "$TEST_RESULT"
563 # complete after delete bridge
567 for i in loop_once; do
568 # check match on port, there should be no p1 as port.
569 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bridge/dump-flows TAB 2>&1)"
570 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
571 EXPECT="$(generate_expect_completions "bridge" "")"
572 if [ "$TMP" != "$EXPECT" ]; then
573 print_error "1" "$TMP" "$EXPECT"
578 # check the available completions.
579 TMP="$(get_available_completions "$COMP_OUTPUT" | tr '\n' ' ' | sed -e 's/[ \t]*$//')"
581 if [ "$TMP" != "$EXPECT" ]; then
582 print_error "2" "$TMP" "$EXPECT"
587 # check 'ovs-ofctl monitor [misslen] [invalid_ttl] [watch:[...]]', should
588 # not show any available completion.
589 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-ofctl monitor non_exist_br TAB 2>&1)"
590 TMP="$(get_argument_expansion "$COMP_OUTPUT" | sed -e 's/[ \t]*$//')"
592 if [ "$TMP" != "$EXPECT" ]; then
593 print_error "3" "$TMP" "$EXPECT"
601 print_result "complete after delete bridge" "$TEST_RESULT"
604 # negative test - incorrect subcommand
608 for i in loop_once; do
609 # incorrect subcommand
610 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ERROR 2>&1)"
611 TMP="$(echo "$COMP_OUTPUT" | sed -e 's/[ \t]*$//' | sed -e '/./,$!d')"
613 if [ "$TMP" != "$EXPECT" ]; then
614 print_error "1" "$TMP" "$EXPECT"
619 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ERROR TAB 2>&1)"
620 TMP="$(echo "$COMP_OUTPUT" | sed -e 's/[ \t]*$//' | sed -e '/./!d')"
621 EXPECT="Command format:"
622 if [ "$TMP" != "$EXPECT" ]; then
623 print_error "2" "$TMP" "$EXPECT"
631 print_result "negative test - incorrect subcommand" "$TEST_RESULT"
634 # negative test - no ovs-vswitchd
635 # negative test - no ovsdb-server
636 # negative test - no ovs-ofctl
637 # should not see any error.
640 killall ovs-vswitchd ovsdb-server
642 for i in ${TEST_APPCTL_TARGETS[@]}; do
643 for j in loop_once; do
648 # should show no avaiable subcommands.
649 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target $daemon TAB 2>&1)"
650 TMP="$(get_available_completions "$COMP_OUTPUT")"
651 EXPECT="$(ovs-appctl --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)"
652 if [ "$TMP" != "$EXPECT" ]; then
657 # should not match any input.
658 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target $daemon ERROR SUBCMD TAB 2>&1)"
659 TMP="$(echo "$COMP_OUTPUT" | sed -e 's/[ \t]*$//' | sed -e '/./!d')"
660 EXPECT="Command format:"
661 if [ "$TMP" != "$EXPECT" ]; then
668 print_result "negative test - no $daemon" "$TEST_RESULT"
672 # negative test - do not match on nested option
676 for i in loop_once; do
677 COMP_OUTPUT="$(bash ovs-command-compgen.bash debug ovsdb-tool create TAB 2>&1)"
678 TMP="$(get_available_completions "$COMP_OUTPUT")"
680 if [ "$TMP" != "$EXPECT" ]; then
688 print_result "negative test - do not match on nested option" "$TEST_RESULT"