TestGlobalArgsOnlyParsedOnce checks that global args are only parsed once (cf https://github.com/docker/cli/issues/1801). These tests rely on `-H` being a list type (i.e. NewNamedListOptsRef) which reject multiple uses dynamically (see `getServerHost()` in github.com/docker/cli/cli/command/cli.go) i
(t *testing.T)
| 75 | // github.com/docker/cli/cli/command/cli.go) in order to detect this |
| 76 | // scenario. |
| 77 | func TestGlobalArgsOnlyParsedOnce(t *testing.T) { |
| 78 | run, _, cleanup := prepare(t) |
| 79 | defer cleanup() |
| 80 | |
| 81 | // We can rely on `$DOCKER_HOST` being set due to the call to |
| 82 | // `environment.Setup` in our `TestMain`. |
| 83 | dh := os.Getenv("DOCKER_HOST") |
| 84 | |
| 85 | for _, tc := range []struct { |
| 86 | name string |
| 87 | args []string |
| 88 | expectedExitCode int |
| 89 | expectedOut, expectedErr string |
| 90 | }{ |
| 91 | { |
| 92 | // This is checking the precondition wrt -H mentioned in the function comment |
| 93 | name: "fails-if-H-used-twice", |
| 94 | args: []string{"-H", dh, "-H", dh, "version", "-f", "{{.Client.Version}}"}, |
| 95 | expectedExitCode: 125, |
| 96 | expectedOut: icmd.None, |
| 97 | expectedErr: fmt.Sprintf(`invalid argument %q for "-H, --host" flag: specify only one -H`, dh), |
| 98 | }, |
| 99 | { |
| 100 | name: "builtin", |
| 101 | args: []string{"-H", dh, "version", "-f", "{{.Client.Version}}"}, |
| 102 | expectedExitCode: 0, |
| 103 | expectedOut: "", // Will be the client version, but the specifics aren't important so long as stderr is empty. |
| 104 | expectedErr: icmd.None, |
| 105 | }, |
| 106 | { |
| 107 | name: "plugin", |
| 108 | args: []string{"-H", dh, "helloworld", "apiversion"}, |
| 109 | expectedExitCode: 0, |
| 110 | expectedOut: "", // Will be the client version, but the specifics aren't important so long as stderr is empty. |
| 111 | expectedErr: icmd.None, |
| 112 | }, |
| 113 | } { |
| 114 | t.Run(tc.name, func(t *testing.T) { |
| 115 | res := icmd.RunCmd(run(tc.args...)) |
| 116 | res.Assert(t, icmd.Expected{ |
| 117 | ExitCode: tc.expectedExitCode, |
| 118 | Out: tc.expectedOut, |
| 119 | Err: tc.expectedErr, |
| 120 | }) |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // TestUnknownGlobal checks that unknown globals report errors |
| 126 | func TestUnknownGlobal(t *testing.T) { |