File: flags_test.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (258 lines) | stat: -rw-r--r-- 6,742 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package cliplugins

import (
	"os"
	"testing"

	"gotest.tools/v3/icmd"
)

// TestRunGoodArgument ensures correct behaviour when running a valid plugin with an `--argument`.
func TestRunGoodArgument(t *testing.T) {
	run, _, cleanup := prepare(t)
	defer cleanup()

	res := icmd.RunCmd(run("helloworld", "--who", "Cleveland"))
	res.Assert(t, icmd.Expected{
		ExitCode: 0,
		Out:      "Hello Cleveland!",
	})
}

// TestClashWithGlobalArgs ensures correct behaviour when a plugin
// has an argument with the same name as one of the globals.
func TestClashWithGlobalArgs(t *testing.T) {
	run, _, cleanup := prepare(t)
	defer cleanup()

	for _, tc := range []struct {
		name                     string
		args                     []string
		expectedOut, expectedErr string
	}{
		{
			name:        "short-without-val",
			args:        []string{"-D"},
			expectedOut: "Hello World!",
			expectedErr: "Plugin debug mode enabled",
		},
		{
			name:        "long-without-val",
			args:        []string{"--debug"},
			expectedOut: "Hello World!",
			expectedErr: "Plugin debug mode enabled",
		},
		{
			name:        "short-with-val",
			args:        []string{"-c", "Christmas"},
			expectedOut: "Merry Christmas!",
			expectedErr: icmd.None,
		},
		{
			name:        "short-with-val",
			args:        []string{"--context", "Christmas"},
			expectedOut: "Merry Christmas!",
			expectedErr: icmd.None,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			args := append([]string{"helloworld"}, tc.args...)
			res := icmd.RunCmd(run(args...))
			res.Assert(t, icmd.Expected{
				ExitCode: 0,
				Out:      tc.expectedOut,
				Err:      tc.expectedErr,
			})
		})
	}
}

// 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) in order to detect this
// scenario.
func TestGlobalArgsOnlyParsedOnce(t *testing.T) {
	run, _, cleanup := prepare(t)
	defer cleanup()

	// We can rely on `$DOCKER_HOST` being set due to the call to
	// `environment.Setup` in our `TestMain`.
	dh := os.Getenv("DOCKER_HOST")

	for _, tc := range []struct {
		name                     string
		args                     []string
		expectedExitCode         int
		expectedOut, expectedErr string
	}{
		{
			// This is checking the precondition wrt -H mentioned in the function comment
			name:             "fails-if-H-used-twice",
			args:             []string{"-H", dh, "-H", dh, "version", "-f", "{{.Client.Version}}"},
			expectedExitCode: 1,
			expectedOut:      icmd.None,
			expectedErr:      "Specify only one -H",
		},
		{
			name:             "builtin",
			args:             []string{"-H", dh, "version", "-f", "{{.Client.Version}}"},
			expectedExitCode: 0,
			expectedOut:      "", // Will be the client version, but the specifics aren't important so long as stderr is empty.
			expectedErr:      icmd.None,
		},
		{
			name:             "plugin",
			args:             []string{"-H", dh, "helloworld", "apiversion"},
			expectedExitCode: 0,
			expectedOut:      "", // Will be the client version, but the specifics aren't important so long as stderr is empty.
			expectedErr:      icmd.None,
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			res := icmd.RunCmd(run(tc.args...))
			res.Assert(t, icmd.Expected{
				ExitCode: tc.expectedExitCode,
				Out:      tc.expectedOut,
				Err:      tc.expectedErr,
			})
		})
	}
}

// TestUnknownGlobal checks that unknown globals report errors
func TestUnknownGlobal(t *testing.T) {
	run, _, cleanup := prepare(t)
	defer cleanup()

	for name, args := range map[string][]string{
		"no-val":       {"--unknown", "helloworld"},
		"separate-val": {"--unknown", "foo", "helloworld"},
		"joined-val":   {"--unknown=foo", "helloworld"},
	} {
		args := args
		t.Run(name, func(t *testing.T) {
			res := icmd.RunCmd(run(args...))
			res.Assert(t, icmd.Expected{
				ExitCode: 125,
				Out:      icmd.None,
				Err:      "unknown flag: --unknown",
			})
		})
	}
}

// TestCliPluginsVersion checks that `-v` and friends DTRT
func TestCliPluginsVersion(t *testing.T) {
	run, _, cleanup := prepare(t)
	defer cleanup()

	for _, tc := range []struct {
		name           string
		args           []string
		expCode        int
		expOut, expErr string
	}{
		{
			name:    "global-version",
			args:    []string{"version"},
			expCode: 0,
			expOut:  "Client:\n Version:",
			expErr:  icmd.None,
		},
		{
			name:    "global-version-flag",
			args:    []string{"--version"},
			expCode: 0,
			expOut:  "Docker version",
			expErr:  icmd.None,
		},
		{
			name:    "global-short-version-flag",
			args:    []string{"-v"},
			expCode: 0,
			expOut:  "Docker version",
			expErr:  icmd.None,
		},
		{
			name:    "global-with-unknown-arg",
			args:    []string{"version", "foo"},
			expCode: 1,
			expOut:  icmd.None,
			expErr:  `"docker version" accepts no arguments.`,
		},
		{
			name:    "global-with-plugin-arg",
			args:    []string{"version", "helloworld"},
			expCode: 1,
			expOut:  icmd.None,
			expErr:  `"docker version" accepts no arguments.`,
		},
		{
			name:    "global-version-flag-with-unknown-arg",
			args:    []string{"--version", "foo"},
			expCode: 0,
			expOut:  "Docker version",
			expErr:  icmd.None,
		},
		{
			name:    "global-short-version-flag-with-unknown-arg",
			args:    []string{"-v", "foo"},
			expCode: 0,
			expOut:  "Docker version",
			expErr:  icmd.None,
		},
		{
			name:    "global-version-flag-with-plugin",
			args:    []string{"--version", "helloworld"},
			expCode: 125,
			expOut:  icmd.None,
			expErr:  "unknown flag: --version",
		},
		{
			name:    "global-short-version-flag-with-plugin",
			args:    []string{"-v", "helloworld"},
			expCode: 125,
			expOut:  icmd.None,
			expErr:  "unknown shorthand flag: 'v' in -v",
		},
		{
			name:    "plugin-with-version",
			args:    []string{"helloworld", "version"},
			expCode: 0,
			expOut:  "Hello World!",
			expErr:  icmd.None,
		},
		{
			name:    "plugin-with-version-flag",
			args:    []string{"helloworld", "--version"},
			expCode: 125,
			expOut:  icmd.None,
			expErr:  "unknown flag: --version",
		},
		{
			name:    "plugin-with-short-version-flag",
			args:    []string{"helloworld", "-v"},
			expCode: 125,
			expOut:  icmd.None,
			expErr:  "unknown shorthand flag: 'v' in -v",
		},
		{
			name:    "",
			args:    []string{},
			expCode: 0,
			expOut:  "",
			expErr:  "",
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			res := icmd.RunCmd(run(tc.args...))
			res.Assert(t, icmd.Expected{
				ExitCode: tc.expCode,
				Out:      tc.expOut,
				Err:      tc.expErr,
			})
		})
	}
}