From 33cf7aec51eec726544499189b20ee1395b9f513 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Mon, 3 Aug 2020 15:41:25 -0700 Subject: Refactor parsing to not require --remote to be first Use cobra.Command.FParseErrWhitelist to no longer require --remote to be the first argument in flags when using CLI Signed-off-by: Jhon Honce --- cmd/podman/registry/remote.go | 26 ++++++++++++++------------ test/utils/utils.go | 4 ++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cmd/podman/registry/remote.go b/cmd/podman/registry/remote.go index 006a1b900..57427f372 100644 --- a/cmd/podman/registry/remote.go +++ b/cmd/podman/registry/remote.go @@ -5,22 +5,24 @@ import ( "sync" "github.com/containers/libpod/v2/pkg/domain/entities" - "github.com/spf13/cobra" + "github.com/spf13/pflag" ) -var ( - // Was --remote given on command line - remoteOverride bool - remoteSync sync.Once -) +// Value for --remote given on command line +var remoteFromCLI = struct { + Value bool + sync sync.Once +}{} -// IsRemote returns true if podman was built to run remote +// IsRemote returns true if podman was built to run remote or --remote flag given on CLI // Use in init() functions as a initialization check func IsRemote() bool { - remoteSync.Do(func() { - remote := &cobra.Command{} - remote.Flags().BoolVarP(&remoteOverride, "remote", "r", false, "") - _ = remote.ParseFlags(os.Args) + remoteFromCLI.sync.Do(func() { + fs := pflag.NewFlagSet("remote", pflag.ContinueOnError) + fs.BoolVarP(&remoteFromCLI.Value, "remote", "r", false, "") + fs.ParseErrorsWhitelist.UnknownFlags = true + fs.SetInterspersed(false) + _ = fs.Parse(os.Args[1:]) }) - return podmanOptions.EngineMode == entities.TunnelMode || remoteOverride + return podmanOptions.EngineMode == entities.TunnelMode || remoteFromCLI.Value } diff --git a/test/utils/utils.go b/test/utils/utils.go index 0597cd292..20e185fc1 100644 --- a/test/utils/utils.go +++ b/test/utils/utils.go @@ -207,6 +207,10 @@ func WaitContainerReady(p PodmanTestCommon, id string, expStr string, timeout in // OutputToString formats session output to string func (s *PodmanSession) OutputToString() string { + if s == nil || s.Out == nil || s.Out.Contents() == nil { + return "" + } + fields := strings.Fields(string(s.Out.Contents())) return strings.Join(fields, " ") } -- cgit v1.2.3-54-g00ecf From 8b38d27ebdc825c1336d898d1a21fd8f301b18b0 Mon Sep 17 00:00:00 2001 From: Ed Santiago Date: Wed, 5 Aug 2020 08:37:09 -0600 Subject: system tests: podman-remote, image tree - new sanity checks for podman-remote: - first, confirm that when PODMAN is "-remote", we actually talk to a server (validated by presence of "Server:" string in "podman version"). - second, add test for #7212, in which we run "podman --remote" (podman with --remote flag, not podman-remote command) and make sure --remote is allowed both as the first option and also with other flag options preceding. - new test for "podman image tree" (piggybacking on top of a "podman build" test, because that gives us lots of layers). - skip "podman exec - basic test" when remote. It is consistently causing CI failures, breaking all of CI, due to #7241. Signed-off-by: Ed Santiago --- test/system/001-basic.bats | 31 +++++++++++++++++++++++++++++++ test/system/070-build.bats | 17 +++++++++++++++++ test/system/075-exec.bats | 2 ++ 3 files changed, 50 insertions(+) diff --git a/test/system/001-basic.bats b/test/system/001-basic.bats index b12836b9e..a5a3324fb 100644 --- a/test/system/001-basic.bats +++ b/test/system/001-basic.bats @@ -38,6 +38,37 @@ function setup() { run_podman pull $IMAGE } +# PR #7212: allow --remote anywhere before subcommand, not just as 1st flag +@test "podman-remote : really is remote, works as --remote option" { + if ! is_remote; then + skip "only applicable on podman-remote" + fi + + # First things first: make sure our podman-remote actually is remote! + run_podman version + is "$output" ".*Server:" "the given podman path really contacts a server" + + # $PODMAN may be a space-separated string, e.g. if we include a --url. + # Split it into its components; remove "-remote" from the command path; + # and preserve any other args if present. + local -a podman_as_array=($PODMAN) + local podman_path=${podman_as_array[0]} + local podman_non_remote=${podman_path%%-remote} + local -a podman_args=("${podman_as_array[@]:1}") + + # This always worked: running "podman --remote ..." + PODMAN="${podman_non_remote} --remote ${podman_args[@]}" run_podman version + is "$output" ".*Server:" "podman --remote: contacts server" + + # This was failing: "podman --foo --bar --remote". + PODMAN="${podman_non_remote} --tmpdir /var/tmp --log-level=error ${podman_args[@]} --remote" run_podman version + is "$output" ".*Server:" "podman [flags] --remote: contacts server" + + # ...but no matter what, --remote is never allowed after subcommand + PODMAN="${podman_non_remote} ${podman_args[@]}" run_podman 125 version --remote + is "$output" "Error: unknown flag: --remote" "podman version --remote" +} + # This is for development only; it's intended to make sure our timeout # in run_podman continues to work. This test should never run in production # because it will, by definition, fail. diff --git a/test/system/070-build.bats b/test/system/070-build.bats index bdc05a172..7dd1077ae 100644 --- a/test/system/070-build.bats +++ b/test/system/070-build.bats @@ -164,6 +164,7 @@ EOF # cd to the dir, so we test relative paths (important for podman-remote) cd $PODMAN_TMPDIR run_podman build -t build_test -f build-test/Containerfile build-test + local iid="${lines[-1]}" # Run without args - should run the above script. Verify its output. export MYENV2="$s_env2" @@ -231,6 +232,22 @@ Labels.$label_name | $label_value run_podman run --rm build_test stat -c'%u:%g:%N' /a/b/c/myfile is "$output" "4:5:/a/b/c/myfile" "file in volume is chowned" + # Hey, as long as we have an image with lots of layers, let's + # confirm that 'image tree' works as expected + run_podman image tree build_test + is "${lines[0]}" "Image ID: ${iid:0:12}" \ + "image tree: first line" + is "${lines[1]}" "Tags: \[localhost/build_test:latest]" \ + "image tree: second line" + is "${lines[2]}" "Size: [0-9.]\+[kM]B" \ + "image tree: third line" + is "${lines[3]}" "Image Layers" \ + "image tree: fourth line" + is "${lines[4]}" "... ID: [0-9a-f]\{12\} Size: .* Top Layer of: \[$IMAGE]" \ + "image tree: first layer line" + is "${lines[-1]}" "... ID: [0-9a-f]\{12\} Size: .* Top Layer of: \[localhost/build_test:latest]" \ + "image tree: last layer line" + # Clean up run_podman rmi -f build_test } diff --git a/test/system/075-exec.bats b/test/system/075-exec.bats index f53a22a3f..6c72a0eab 100644 --- a/test/system/075-exec.bats +++ b/test/system/075-exec.bats @@ -6,6 +6,8 @@ load helpers @test "podman exec - basic test" { + skip_if_remote "FIXME: pending #7241" + rand_filename=$(random_string 20) rand_content=$(random_string 50) -- cgit v1.2.3-54-g00ecf From b64d29bdf2d6aa9fe402ab2b54cc888fe1e4eb62 Mon Sep 17 00:00:00 2001 From: Jhon Honce Date: Tue, 18 Aug 2020 08:57:00 -0700 Subject: Remove help/usage from --remote pre-check --remote pre-check was providing usage context, which was also being provided by the root podman command. Fixes #7273 Signed-off-by: Jhon Honce --- cmd/podman/registry/remote.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cmd/podman/registry/remote.go b/cmd/podman/registry/remote.go index 57427f372..217652ec0 100644 --- a/cmd/podman/registry/remote.go +++ b/cmd/podman/registry/remote.go @@ -15,13 +15,14 @@ var remoteFromCLI = struct { }{} // IsRemote returns true if podman was built to run remote or --remote flag given on CLI -// Use in init() functions as a initialization check +// Use in init() functions as an initialization check func IsRemote() bool { remoteFromCLI.sync.Do(func() { fs := pflag.NewFlagSet("remote", pflag.ContinueOnError) - fs.BoolVarP(&remoteFromCLI.Value, "remote", "r", false, "") fs.ParseErrorsWhitelist.UnknownFlags = true + fs.Usage = func() {} fs.SetInterspersed(false) + fs.BoolVarP(&remoteFromCLI.Value, "remote", "r", false, "") _ = fs.Parse(os.Args[1:]) }) return podmanOptions.EngineMode == entities.TunnelMode || remoteFromCLI.Value -- cgit v1.2.3-54-g00ecf