3
# Tests based on 'podman help'
5
# Find all commands listed by 'podman --help'. Run each one, make sure it
6
# provides its own --help output. If the usage message ends in '[command]',
7
# treat it as a subcommand, and recurse into its own list of sub-subcommands.
9
# Any usage message that ends in '[options]' is interpreted as a command
10
# that takes no further arguments; we confirm by running with 'invalid-arg'
11
# and confirming that it exits with error status and message.
15
function check_help() {
19
for cmd in $(_podman_commands "$@"); do
20
# Skip the compose command which is calling `docker-compose --help`
21
# and hence won't match the assumptions made below.
22
if [[ "$cmd" == "compose" ]]; then
25
# Human-readable podman command string, with multiple spaces collapsed
26
command_string="podman $* $cmd"
27
command_string=${command_string// / } # 'podman x' -> 'podman x'
29
dprint "$command_string --help"
30
run_podman "$@" $cmd --help
31
local full_help="$output"
33
# The line immediately after 'Usage:' gives us a 1-line synopsis
34
usage=$(echo "$full_help" | grep -A1 '^Usage:' | tail -1)
35
assert "$usage" != "" "podman $cmd: no Usage message found"
37
# e.g. 'podman ps' should not show 'podman container ps' in usage
38
# Trailing space in usage handles 'podman system renumber' which
40
is "$usage " " $command_string .*" "Usage string matches command"
42
# Strip off the leading command string; we no longer need it
43
usage=$(sed -e "s/^ $command_string \?//" <<<"$usage")
45
# If usage ends in '[command]', recurse into subcommands
46
if expr "$usage" : '\[command\]' >/dev/null; then
48
# (except for 'podman help', which is a special case)
49
if [[ $cmd != "help" ]]; then
55
# We had someone write upper-case '[OPTIONS]' once. Prevent it.
56
assert "$usage" !~ '\[OPTION' \
57
"'options' string must be lower-case in usage"
59
# We had someone do 'podman foo ARG [options]' one time. Yeah, no.
60
assert "$usage" !~ '[A-Z].*\[option' \
61
"'options' must precede arguments in usage"
63
# Strip off '[options]' but remember if we've seen it.
65
if [[ $usage =~ \[options\] ]]; then
67
usage=$(sed -e 's/^\[options\] \?//' <<<"$usage")
70
# From this point on, remaining argument descriptions must be UPPER CASE
71
# e.g., 'podman cmd [options] arg' or 'podman cmd [arg]' are invalid.
72
assert "$usage" !~ '[a-z]' \
73
"$command_string: argument names must be UPPER CASE"
75
# It makes no sense to have an optional arg followed by a mandatory one
76
assert "$usage" !~ '\[.*\] [A-Z]' \
77
"$command_string: optional args must be _after_ required ones"
79
# Cross-check: if usage includes '[options]', there must be a
80
# longer 'Options:' section in the full --help output; vice-versa,
81
# if 'Options:' is in full output, usage line must have '[options]'.
82
if [[ $has_options ]]; then
83
if ! expr "$full_help" : ".*Options:" >/dev/null; then
84
die "$command_string: Usage includes '[options]' but has no 'Options:' subsection"
86
elif expr "$full_help" : ".*Options:" >/dev/null; then
87
die "$command_string: --help has 'Options:' section but no '[options]' in synopsis"
90
# If usage lists no arguments (strings in ALL CAPS), confirm
91
# by running with 'invalid-arg' and expecting failure.
92
if ! expr "$usage" : '.*[A-Z]' >/dev/null; then
93
if [ "$cmd" != "help" ]; then
94
dprint "$command_string invalid-arg"
95
run_podman '?' "$@" $cmd invalid-arg
97
"'$usage' indicates that the command takes no arguments. I invoked it with 'invalid-arg' and expected an error status"
98
is "$output" "Error: .* takes no arguments" \
99
"'$usage' indicates that the command takes no arguments. I invoked it with 'invalid-arg' and expected the following error message"
101
found[takes_no_args]=1
104
# If command lists "-l, --latest" in help output, combine -l with arg.
105
# This should be disallowed with a clear message.
106
if expr "$full_help" : ".*-l, --latest" >/dev/null; then
107
local nope="exec list port ps top" # these can't be tested
109
nope="$nope mount restore" # these don't work rootless
111
if ! grep -wq "$cmd" <<<$nope; then
112
run_podman 125 "$@" $cmd -l nonexistent-container
113
is "$output" "Error: .*--latest and \(containers\|pods\|arguments\) cannot be used together" \
114
"'$command_string' with both -l and container"
116
# Combine -l and -a, too (but spell it as --all, because "-a"
117
# means "attach" in podman container start)
118
run_podman 125 "$@" $cmd --all --latest
119
is "$output" "Error: \(--all and --latest cannot be used together\|--all, --latest and containers cannot be used together\|--all, --latest and arguments cannot be used together\|unknown flag\)" \
120
"'$command_string' with both --all and --latest"
124
# If usage has required arguments, try running without them.
125
if expr "$usage" : '[A-Z]' >/dev/null; then
126
# Exceptions: these commands don't work rootless
128
# "pause is not supported for rootless containers"
129
if [[ "$cmd" = "pause" ]] || [[ "$cmd" = "unpause" ]]; then
133
if [ "$@" = "network" -a "$cmd" = "rm" ]; then
138
# The </dev/null protects us from 'podman login' which will
139
# try to read username/password from stdin.
140
dprint "$command_string (without required args)"
141
run_podman '?' "$@" $cmd </dev/null
143
"'$usage' indicates at least one required arg. I invoked it with no args and expected an error exit code"
144
is "$output" "Error:.* \(require\|specif\|must\|provide\|need\|choose\|accepts\)" \
145
"'$usage' indicates at least one required arg. I invoked it with no args and expected one of these error messages"
147
found[required_args]=1
150
# Commands with fixed number of arguments (i.e. no ellipsis): count
151
# the required args, then invoke with one extra. We should get a
153
if ! expr "$usage" : ".*\.\.\."; then
154
local n_args=$(wc -w <<<"$usage")
156
run_podman '?' "$@" $cmd $(seq --format='x%g' 0 $n_args)
158
"'$usage' indicates a maximum of $n_args args. I invoked it with more, and expected this exit status"
159
is "$output" "Error:.* \(takes no arguments\|requires exactly $n_args arg\|accepts at most\|too many arguments\|accepts $n_args arg(s), received\|accepts between .* and .* arg(s), received \)" \
160
"'$usage' indicates a maximum of $n_args args. I invoked it with more, and expected one of these error messages"
165
count=$(expr $count + 1)
168
# Any command that takes subcommands, prints its help and errors if called
172
is "$status" 125 "'podman $*' without any subcommand - exit status"
173
is "$output" ".*Usage:.*Error: missing command '.*$* COMMAND'" \
174
"'podman $*' without any subcommand - expected error message"
176
# Assume that 'NoSuchCommand' is not a command
177
dprint "podman $* NoSuchCommand"
178
run_podman '?' "$@" NoSuchCommand
179
is "$status" 125 "'podman $* NoSuchCommand' - exit status"
180
is "$output" "Error: unrecognized command .*$* NoSuchCommand" \
181
"'podman $* NoSuchCommand' - expected error message"
183
# This can happen if the output of --help changes, such as between
184
# the old command parser and cobra.
185
assert "$count" -gt 0 \
186
"Internal error: no commands found in 'podman help $*' list"
188
# Sanity check: make sure the special loops above triggered at least once.
189
# (We've had situations where a typo makes the conditional never run)
191
for i in subcommands required_args takes_no_args fixed_args; do
192
assert "${found[$i]}" != "" \
193
"Internal error: '$i' subtest did not trigger"
199
@test "podman help - basic tests" {
202
# Called with no args -- start with 'podman --help'. check_help() will
203
# recurse for any subcommands.
206
# Test for regression of #7273 (spurious "--remote" help on output)
207
for helpopt in help --help -h; do
209
is "${lines[0]}" "Manage pods, containers and images" \
210
"podman $helpopt: first line of output"