Skip to content

Commit

Permalink
ovs-command-completion: Autotest integration.
Browse files Browse the repository at this point in the history
This commit integrates the unit tests defined in
utilities/ovs-command-compgen-test.bash into 'make check'.
The tests will be skipped if the current shell is not bash.

Signed-off-by: Alex Wang <[email protected]>
Acked-by: Ben Pfaff <[email protected]>
  • Loading branch information
yew011 committed Jan 30, 2015
1 parent 9530457 commit 08d4254
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 689 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ scripts_SCRIPTS =
scripts_DATA =
SUFFIXES =
check_DATA =
check_SCRIPTS =
pkgconfig_DATA =

scriptsdir = $(pkgdatadir)/scripts
Expand Down
1 change: 1 addition & 0 deletions tests/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ COMMON_MACROS_AT = \

TESTSUITE_AT = \
tests/testsuite.at \
tests/completion.at \
tests/library.at \
tests/heap.at \
tests/bundle.at \
Expand Down
354 changes: 354 additions & 0 deletions tests/completion.at
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
AT_BANNER([command completion unit tests - bash])

m4_define([GET_FORMAT], [
echo "$@" | grep -A 1 -- "Command format" | tail -n+2
])

m4_define([GET_EXPAN], [
echo "$@" | grep -- "available completions for keyword" \
| sed -e 's/^[ ]*//g;s/[ ]*$//g'
])

m4_define([GET_AVAIL], [
echo "$@" | sed -e '1,/Available/d' | tail -n+2
])

m4_define([GET_COMP_STR], [
echo "available completions for keyword \"$1\": $2" \
| sed -e 's/[ ]*$//g'
])

AT_SETUP([bash completion - basic verification])
AT_SKIP_IF([test -z ${BASH_VERSION+x}])
OVS_VSWITCHD_START

# complete ovs-appctl [TAB]
# complete ovs-dpctl [TAB]
# complete ovs-ofctl [TAB]
# complete ovsdb-tool [TAB]
m4_foreach(
[test_command],
[[ovs-appctl],
[ovs-dpctl],
[ovs-ofctl],
[ovsdb-tool]],
[
INPUT="$(bash ovs-command-compgen.bash debug test_command TAB 2>&1)"
MATCH="$(test_command --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)
$(test_command list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
${MATCH}
])])


# complete ovs-appctl --tar[TAB]
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --tar 2>&1)"
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
--target
])


# complete ovs-appctl --target [TAB]
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target TAB 2>&1)"
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
ovs-ofctl
ovs-vswitchd
ovsdb-server
])


# complete ovs-appctl --target ovs-vswitchd [TAB]
# complete ovs-appctl --target ovsdb-server [TAB]
# complete ovs-appctl --target ovs-ofctl [TAB]
AT_CHECK([ovs-ofctl monitor br0 --detach --no-chdir --pidfile])
m4_foreach(
[target_daemon],
[[ovs-vswitchd],
[ovsdb-server],
[ovs-ofctl]],
[
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target target_daemon TAB 2>&1)"
MATCH="$(ovs-appctl --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)
$(ovs-appctl --target target_daemon list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)"
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
${MATCH}
])])
AT_CHECK([ovs-appctl --target ovs-ofctl exit])


# check all ovs-appctl subcommand formats
LIST=$(ovs-appctl list-commands | tail -n +2 | cut -c3- | cut -d ' ' -f1 | sort)
for subcommand in $LIST; do
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl $subcommand TAB 2>&1)"
MATCH=$(ovs-appctl list-commands | tail -n+2 | cut -c3- | grep -- "^$subcommand " | tr -s ' ')
AT_CHECK_UNQUOTED([GET_FORMAT(${INPUT})],
[0], [dnl
${MATCH}
])
done

OVS_VSWITCHD_STOP
AT_CLEANUP


# complex completion check - bfd/set-forwarding
# bfd/set-forwarding [interface] normal|false|true
# test expansion of 'interface'
AT_SETUP([bash completion - complex completion check 1])
AT_SKIP_IF([test -z ${BASH_VERSION+x}])
OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy)

# check the top level completion.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding TAB 2>&1)"
MATCH="$(GET_COMP_STR([normal], [])
GET_COMP_STR([false], [])
GET_COMP_STR([true], [])
GET_COMP_STR([interface], [p0]))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
p0
])


# set argument to 'true', there should be no more completions.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding true TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
[0], [dnl
Command format:
bfd/set-forwarding [[interface]] normal|false|true
])


# set argument to 'p1', there should still be the completion for booleans.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding p1 TAB 2>&1)"
MATCH="$(GET_COMP_STR([normal], [])
GET_COMP_STR([false], [])
GET_COMP_STR([true], []))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])


# set argument to 'p1 false', there should still no more completions.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl bfd/set-forwarding p1 false TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
[0], [dnl
Command format:
bfd/set-forwarding [[interface]] normal|false|true
])

OVS_VSWITCHD_STOP
AT_CLEANUP


# complex completion check - lacp/show
# lacp/show [port]
# test expansion on 'port'
AT_SETUP([bash completion - complex completion check 2])
AT_SKIP_IF([test -z ${BASH_VERSION+x}])
OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy \
-- add-port br0 p1 -- set Interface p1 type=dummy)

# check the top level completion.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show TAB 2>&1)"
MATCH="$(GET_COMP_STR([port], [br0 p0 p1]))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
br0
p0
p1
])


# set argument to 'p1', there should be no more completions.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl lacp/show p1 TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
[0], [dnl
Command format:
lacp/show [[port]]
])

OVS_VSWITCHD_STOP
AT_CLEANUP


# complex completion check - ofproto/trace
# ofproto/trace {[dp_name] odp_flow | bridge br_flow} [-generate|packet]
# test expansion on 'dp|dp_name' and 'bridge'
AT_SETUP([bash completion - complex completion check 3])
AT_SKIP_IF([test -z ${BASH_VERSION+x}])
OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy \
-- add-port br0 p1 -- set Interface p1 type=dummy)

# check the top level completion.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace TAB 2>&1)"
MATCH="$(GET_COMP_STR([bridge], [br0])
GET_COMP_STR([odp_flow], [])
GET_COMP_STR([dp_name], [ovs-dummy]))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
br0
ovs-dummy
])


# set argument to 'ovs-dummy', should go to the dp-name path.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-dummy TAB 2>&1)"
MATCH="$(GET_COMP_STR([odp_flow], []))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])


# set odp_flow to some random string, should go to the next level.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-dummy "in_port(123),mac(),ip,tcp" TAB 2>&1)"
MATCH="$(GET_COMP_STR([-generate], [-generate])
GET_COMP_STR([packet], []))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
-generate
])


# set packet to some random string, there should be no more completions.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace ovs-dummy "in_port(123),mac(),ip,tcp" "ABSJDFLSDJFOIWEQR" TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
[0], [dnl
Command format:
ofproto/trace {[[dp_name]] odp_flow | bridge br_flow} [[-generate|packet]]
])


# set argument to 'br0', should go to the bridge path.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace br0 TAB 2>&1)"
MATCH="$(GET_COMP_STR([br_flow], []))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])


# set argument to some random string, should go to the odp_flow path.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ofproto/trace "in_port(123),mac(),ip,tcp" TAB 2>&1)"
MATCH="$(GET_COMP_STR([-generate], [-generate])
GET_COMP_STR([packet], []))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
-generate
])

OVS_VSWITCHD_STOP
AT_CLEANUP


# complex completion check - vlog/set
# vlog/set {spec | PATTERN:destination:pattern}
# test non expandable arguments
AT_SETUP([bash completion - complex completion check 4])
AT_SKIP_IF([test -z ${BASH_VERSION+x}])
OVS_VSWITCHD_START

# check the top level completion.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl vlog/set TAB 2>&1)"
MATCH="$(GET_COMP_STR([PATTERN:destination:pattern], [])
GET_COMP_STR([spec], []))"
AT_CHECK_UNQUOTED([GET_EXPAN(${INPUT})],
[0], [dnl
${MATCH}
])
# check the available completions.
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])


# set argument to random 'abcd', there should be no more completions.
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl vlog/set abcd TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e '/./,$!d'],
[0], [dnl
Command format:
vlog/set {spec | PATTERN:destination:pattern}
])

OVS_VSWITCHD_STOP
AT_CLEANUP


AT_SETUP([bash completion - negative test])
AT_SKIP_IF([test -z ${BASH_VERSION+x}])
OVS_VSWITCHD_START(add-port br0 p0 -- set Interface p0 type=dummy)

# negative test - incorrect subcommand
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ERROR 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e 's/[ \t]*$//' | sed -e '/./,$!d'], [0])
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl ERROR TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e 's/[ \t]*$//' | sed -e '/./!d'],
[0], [dnl
Command format:
])


# negative test - no ovs-vswitchd
# negative test - no ovsdb-server
# negative test - no ovs-ofctl
# should not see any error.
OVS_VSWITCHD_STOP
m4_foreach(
[target_daemon],
[[ovs-vswitchd],
[ovsdb-server],
[ovs-ofctl]],
[
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target target_daemon TAB 2>&1)"
MATCH="$(ovs-appctl --option | sort | sed -n '/^--.*/p' | cut -d '=' -f1)"
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})],
[0], [dnl
${MATCH}
])
INPUT="$(bash ovs-command-compgen.bash debug ovs-appctl --target target_daemon ERROR SUBCMD TAB 2>&1)"
AT_CHECK_UNQUOTED([echo "$INPUT" | sed -e 's/[ \t]*$//' | sed -e '/./!d'],
[0], [dnl
Command format:
])])


# negative test - do not match on nested option
INPUT="$(bash ovs-command-compgen.bash debug ovsdb-tool create TAB 2>&1)"
AT_CHECK_UNQUOTED([GET_AVAIL(${INPUT})], [0])

AT_CLEANUP
1 change: 1 addition & 0 deletions tests/testsuite.at
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ m4_include([tests/ovs-macros.at])
m4_include([tests/ovsdb-macros.at])
m4_include([tests/ofproto-macros.at])

m4_include([tests/completion.at])
m4_include([tests/bfd.at])
m4_include([tests/cfm.at])
m4_include([tests/lacp.at])
Expand Down
4 changes: 3 additions & 1 deletion utilities/automake.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ scripts_SCRIPTS += \
utilities/ovs-save
scripts_DATA += utilities/ovs-lib

check_SCRIPTS += utilities/ovs-command-compgen.bash

utilities/ovs-lib: $(top_builddir)/config.status

docs += utilities/ovs-command-compgen.INSTALL.md
EXTRA_DIST += \
utilities/ovs-check-dead-ifs.in \
utilities/ovs-command-compgen.bash \
utilities/ovs-command-compgen-test.bash \
utilities/ovs-command-compgen.INSTALL.md \
utilities/ovs-ctl.in \
utilities/ovs-dev.py \
utilities/ovs-docker \
Expand Down
Loading

0 comments on commit 08d4254

Please sign in to comment.