github.com/containers/podman/v5@v5.1.0-rc1/test/system/610-format.bats (about) 1 #!/usr/bin/env bats -*- bats -*- 2 # 3 # PR #15673: For all commands that accept --format '{{.GoTemplate}}', 4 # invoke with --format '{{"\n"}}' and make sure they don't choke. 5 # 6 7 load helpers 8 9 function teardown() { 10 # In case test fails: standard teardown does not wipe machines or secrets 11 run_podman '?' machine rm -f mymachine 12 run_podman '?' secret rm mysecret 13 14 basic_teardown 15 } 16 17 # Most commands can't just be run with --format; they need an argument or 18 # option. This table defines what those are. 19 # 20 # FIXME: once you've finished fixing them all, remove the SKIPs (just 21 # remove the entire lines, except for pod-inspect, just remove the SKIP 22 # but leave "mypod") 23 extra_args_table=" 24 history | $IMAGE 25 image history | $IMAGE 26 image inspect | $IMAGE 27 container inspect | mycontainer 28 inspect | mycontainer 29 30 31 volume inspect | -a 32 secret inspect | mysecret 33 network inspect | podman 34 ps | -a 35 36 image search | $IMAGE 37 search | $IMAGE 38 39 pod inspect | mypod 40 41 events | --stream=false --events-backend=file 42 system events | --stream=false --events-backend=file 43 " 44 45 # podman machine is finicky. Assume we can't run it, but see below for more. 46 can_run_podman_machine= 47 48 # podman stats, too 49 can_run_stats= 50 51 # Main test loop. Recursively runs 'podman [subcommand] help', looks for: 52 # > '[command]', which indicates, recurse; or 53 # > '--format', in which case we 54 # > check autocompletion, look for Go templates, in which case we 55 # > run the command with --format '{{"\n"}}' and make sure it passes 56 function check_subcommand() { 57 for cmd in $(_podman_commands "$@"); do 58 # Skip the compose command which is calling `docker-compose --help` 59 # and hence won't match the assumptions made below. 60 if [[ "$cmd" == "compose" ]]; then 61 continue 62 fi 63 # Human-readable podman command string, with multiple spaces collapsed 64 # Special case: 'podman machine' can only be run under ideal conditions 65 if [[ "$cmd" = "machine" ]] && [[ -z "$can_run_podman_machine" ]]; then 66 continue 67 fi 68 if [[ "$cmd" = "stats" ]] && [[ -z "$can_run_stats" ]]; then 69 continue 70 fi 71 72 # Human-readable podman command string, with multiple spaces collapsed 73 command_string="podman $* $cmd" 74 command_string=${command_string// / } # 'podman x' -> 'podman x' 75 76 # Run --help, decide if this is a subcommand with subcommands 77 run_podman "$@" $cmd --help 78 local full_help="$output" 79 80 # The line immediately after 'Usage:' gives us a 1-line synopsis 81 usage=$(echo "$full_help" | grep -A1 '^Usage:' | tail -1) 82 assert "$usage" != "" "podman $cmd: no Usage message found" 83 84 # Strip off the leading command string; we no longer need it 85 usage=$(sed -e "s/^ $command_string \?//" <<<"$usage") 86 87 # If usage ends in '[command]', recurse into subcommands 88 if expr "$usage" : '\[command\]' >/dev/null; then 89 # (except for 'podman help', which is a special case) 90 if [[ $cmd != "help" ]]; then 91 check_subcommand "$@" $cmd 92 fi 93 continue 94 fi 95 96 # Not a subcommand-subcommand. Look for --format option 97 if [[ ! "$output" =~ "--format" ]]; then 98 continue 99 fi 100 101 # Have --format. Make sure it's a Go-template option, not like --push 102 run_podman __completeNoDesc "$@" "$cmd" --format '{{.' 103 if [[ ! "$output" =~ \{\{\.[A-Z] ]]; then 104 continue 105 fi 106 107 # Got one. 108 dprint "$command_string has --format" 109 110 # Whatever is needed to make a runnable command 111 local extra=${extra_args[$command_string]} 112 if [[ -n "$extra" ]]; then 113 # Cross off our list 114 unset extra_args["$command_string"] 115 fi 116 117 # This is what does the work. We run with '?' so we can offer 118 # better error messages than just "exited with error status". 119 run_podman '?' "$@" "$cmd" $extra --format '{{"\n"}}' 120 121 # Output must always be empty. 122 # 123 # - If you see "unterminated quoted string" here, there's a 124 # regression, and you need to fix --format (see PR #15673) 125 # 126 # - If you see any other error, it probably means that someone 127 # added a new podman subcommand that supports --format but 128 # needs some sort of option or argument to actually run. 129 # See 'extra_args_table' at the top of this script. 130 # 131 assert "$output" = "" "$command_string --format '{{\"\n\"}}'" 132 133 # *Now* check exit status. This should never, ever, ever trigger! 134 # If it does, it means the podman command failed without an err msg! 135 assert "$status" = "0" \ 136 "$command_string --format '{{\"\n\"}}' failed with no output!" 137 done 138 } 139 140 # Test entry point 141 @test "check Go template formatting" { 142 skip_if_remote 143 144 # Setup: some commands need a container, pod, secret, ... 145 run_podman run -d --name mycontainer $IMAGE top 146 run_podman pod create mypod 147 run_podman secret create mysecret /etc/hosts 148 149 # ...or machine. But podman machine is ultra-finicky, it fails as root 150 # or if qemu is missing. Instead of checking for all the possible ways 151 # to skip it, just try running init. If it works, we can test it. 152 run_podman '?' machine init --image-path=/dev/null mymachine 153 if [[ $status -eq 0 ]]; then 154 can_run_podman_machine=true 155 extra_args_table+=" 156 machine inspect | mymachine 157 " 158 fi 159 160 # Similarly, 'stats' cannot run rootless under cgroups v1 161 if ! is_rootless || is_cgroupsv2; then 162 can_run_stats=true 163 extra_args_table+=" 164 container stats | --no-stream 165 pod stats | --no-stream 166 stats | --no-stream 167 " 168 fi 169 170 # Convert the table at top to an associative array, keyed on subcommand 171 declare -A extra_args 172 while read subcommand extra; do 173 extra_args["podman $subcommand"]=$extra 174 done < <(parse_table "$extra_args_table") 175 176 # Run the test 177 check_subcommand 178 179 # Clean up 180 run_podman pod rm mypod 181 run_podman rmi $(pause_image) 182 run_podman rm -f -t0 mycontainer 183 run_podman secret rm mysecret 184 run_podman '?' machine rm -f mymachine 185 186 # Make sure there are no leftover commands in our table - this would 187 # indicate a typo in the table, or a flaw in our logic such that 188 # we're not actually recursing. 189 local leftovers="${!extra_args[@]}" 190 assert "$leftovers" = "" "Did not find (or test) subcommands:" 191 } 192 193 # vim: filetype=sh