podman

Форк
0
/
015-help.bats 
215 строк · 9.4 Кб
1
#!/usr/bin/env bats
2
#
3
# Tests based on 'podman help'
4
#
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.
8
#
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.
12
#
13
load helpers
14

15
function check_help() {
16
    local count=0
17
    local -A found
18

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
23
            continue
24
        fi
25
        # Human-readable podman command string, with multiple spaces collapsed
26
        command_string="podman $* $cmd"
27
        command_string=${command_string//  / }  # 'podman  x' -> 'podman x'
28

29
        dprint "$command_string --help"
30
        run_podman "$@" $cmd --help
31
        local full_help="$output"
32

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"
36

37
        # e.g. 'podman ps' should not show 'podman container ps' in usage
38
        # Trailing space in usage handles 'podman system renumber' which
39
        # has no ' [options]'
40
        is "$usage " "  $command_string .*" "Usage string matches command"
41

42
        # Strip off the leading command string; we no longer need it
43
        usage=$(sed -e "s/^  $command_string \?//" <<<"$usage")
44

45
        # If usage ends in '[command]', recurse into subcommands
46
        if expr "$usage" : '\[command\]' >/dev/null; then
47
            found[subcommands]=1
48
            # (except for 'podman help', which is a special case)
49
            if [[ $cmd != "help" ]]; then
50
                check_help "$@" $cmd
51
            fi
52
            continue
53
        fi
54

55
        # We had someone write upper-case '[OPTIONS]' once. Prevent it.
56
        assert "$usage" !~ '\[OPTION' \
57
               "'options' string must be lower-case in usage"
58

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"
62

63
        # Strip off '[options]' but remember if we've seen it.
64
        local has_options=
65
        if [[ $usage =~ \[options\] ]]; then
66
            has_options=1
67
            usage=$(sed -e 's/^\[options\] \?//' <<<"$usage")
68
        fi
69

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"
74

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"
78

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"
85
            fi
86
        elif expr "$full_help" : ".*Options:" >/dev/null; then
87
            die "$command_string: --help has 'Options:' section but no '[options]' in synopsis"
88
        fi
89

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
96
                is "$status" 125 \
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"
100
            fi
101
            found[takes_no_args]=1
102
        fi
103

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
108
            if is_rootless; then
109
                nope="$nope mount restore"       # these don't work rootless
110
            fi
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"
115

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"
121
            fi
122
        fi
123

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
127
            if is_rootless; then
128
                # "pause is not supported for rootless containers"
129
                if [[ "$cmd" = "pause" ]] || [[ "$cmd" = "unpause" ]]; then
130
                    continue
131
                fi
132
                # "network rm" too
133
                if [ "$@" = "network" -a "$cmd" = "rm" ]; then
134
                    continue
135
                fi
136
            fi
137

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
142
            is "$status" 125 \
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"
146

147
            found[required_args]=1
148
        fi
149

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
152
        # usage error.
153
        if ! expr "$usage" : ".*\.\.\."; then
154
            local n_args=$(wc -w <<<"$usage")
155

156
            run_podman '?' "$@" $cmd $(seq --format='x%g' 0 $n_args)
157
            is "$status" 125 \
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"
161

162
            found[fixed_args]=1
163
        fi
164

165
        count=$(expr $count + 1)
166
    done
167

168
    # Any command that takes subcommands, prints its help and errors if called
169
    # without one.
170
    dprint "podman $*"
171
    run_podman '?' "$@"
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"
175

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"
182

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"
187

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)
190
    if [ -z "$*" ]; then
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"
194
        done
195
    fi
196
}
197

198

199
@test "podman help - basic tests" {
200
    skip_if_remote
201

202
    # Called with no args -- start with 'podman --help'. check_help() will
203
    # recurse for any subcommands.
204
    check_help
205

206
    # Test for regression of #7273 (spurious "--remote" help on output)
207
    for helpopt in help --help -h; do
208
        run_podman $helpopt
209
        is "${lines[0]}" "Manage pods, containers and images" \
210
           "podman $helpopt: first line of output"
211
    done
212

213
}
214

215
# vim: filetype=sh
216

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.