From 6864a5547a774d19a7ccb9d50a7799b721fb66ef Mon Sep 17 00:00:00 2001 From: Ed Santiago Date: Tue, 23 Jun 2020 08:10:08 -0600 Subject: BATS tests: new too-many-arguments test ...plus a few others. And fixes to actual parsing. If a command's usage message includes '...' in the argument list, assume it can take unlimited arguments. Nothing we can check. For all others, though, the ALL-CAPS part on the right-hand side of the usage message will define an upper bound on the number of arguments accepted by the command. So in our 'podman --help' test, generate N+1 args and run that command. We expect a 125 exit status and a suitably helpful error message. Not all podman commands or subcommands were checking, so I fixed that. And, fixed some broken usage messages (all-caps FLAGS, and '[flags]' at the end of 'ARGS'). Add new checks to the help test to prevent those in the future. Plus a little refactoring/cleanup where necessary. Signed-off-by: Ed Santiago --- test/system/015-help.bats | 63 +++++++++++++++++++++++++++++++++++++++-------- test/system/120-load.bats | 6 ----- 2 files changed, 53 insertions(+), 16 deletions(-) (limited to 'test/system') diff --git a/test/system/015-help.bats b/test/system/015-help.bats index 42d3bd3ec..14af8e1a4 100644 --- a/test/system/015-help.bats +++ b/test/system/015-help.bats @@ -25,7 +25,7 @@ function podman_commands() { function check_help() { local count=0 - local subcommands_found=0 + local -A found for cmd in $(podman_commands "$@"); do # Human-readable podman command string, with multiple spaces collapsed @@ -44,24 +44,37 @@ function check_help() { # If usage ends in '[command]', recurse into subcommands if expr "$usage" : '.*\[command\]$' >/dev/null; then - subcommands_found=$(expr $subcommands_found + 1) + found[subcommands]=1 check_help "$@" $cmd continue fi - # If usage ends in '[flag]', command takes no more arguments. - # Confirm that by running with 'invalid-arg' and expecting failure. - if expr "$usage" : '.*\[flags\]$' >/dev/null; then + # We had someone write upper-case '[FLAGS]' once. Prevent it. + if expr "$usage" : '.*\[FLAG' >/dev/null; then + die "'flags' string must be lower-case in usage: $usage" + fi + + # We had someone do 'podman foo ARG [flags]' one time. Yeah, no. + if expr "$usage" : '.*[A-Z].*\[flag' >/dev/null; then + die "'flags' must precede arguments in usage: $usage" + fi + + # If usage lists no arguments (strings in ALL CAPS), confirm + # by running with 'invalid-arg' and expecting failure. + if ! expr "$usage" : '.*[A-Z]' >/dev/null; then if [ "$cmd" != "help" ]; then dprint "$command_string invalid-arg" run_podman 125 "$@" $cmd invalid-arg is "$output" "Error: .* takes no arguments" \ "'$command_string' with extra (invalid) arguments" fi + found[takes_no_args]=1 fi - # If usage has required arguments, try running without them - if expr "$usage" : '.*\[flags\] [A-Z]' >/dev/null; then + # If usage has required arguments, try running without them. + # The expression here is 'first capital letter is not in [BRACKETS]'. + # It is intended to handle 'podman foo [flags] ARG' but not ' [ARG]'. + if expr "$usage" : '[^A-Z]\+ [A-Z]' >/dev/null; then # Exceptions: these commands don't work rootless if is_rootless; then # "pause is not supported for rootless containers" @@ -80,6 +93,31 @@ function check_help() { run_podman 125 "$@" $cmd $n_args arguments" + + found[fixed_args]=1 + fi fi count=$(expr $count + 1) @@ -101,9 +139,14 @@ function check_help() { [ $count -gt 0 ] || \ die "Internal error: no commands found in 'podman help $@' list" - # At least the top level must have some subcommands - if [ -z "$*" -a $subcommands_found -eq 0 ]; then - die "Internal error: did not find any podman subcommands" + # Sanity check: make sure the special loops above triggered at least once. + # (We've had situations where a typo makes the conditional never run) + if [ -z "$*" ]; then + for i in subcommands required_args takes_no_args fixed_args; do + if [[ -z ${found[$i]} ]]; then + die "Internal error: '$i' subtest did not trigger" + fi + done fi } diff --git a/test/system/120-load.bats b/test/system/120-load.bats index f290c1888..afa5ab473 100644 --- a/test/system/120-load.bats +++ b/test/system/120-load.bats @@ -107,10 +107,4 @@ verify_iid_and_name() { "Diagnostic from 'podman load' without redirection or -i" } -@test "podman load - at most 1 arg(s)" { - run_podman 125 load 1 2 3 - is "$output" \ - "Error: accepts at most 1 arg(s), received 3" -} - # vim: filetype=sh -- cgit v1.2.3-54-g00ecf