Skip to content

Commit 1598586

Browse files
authored
Merge pull request #4646 from akerouanton/followup-macaddress
TestParseWithMacAddress: check endpoint-scoped MacAddress field
2 parents 00532f9 + 2b1f5a2 commit 1598586

1 file changed

Lines changed: 31 additions & 26 deletions

File tree

cli/command/container/opts_test.go

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ func setupRunFlags() (*pflag.FlagSet, *containerOptions) {
6464
return flags, copts
6565
}
6666

67-
func mustParse(t *testing.T, args string) (*container.Config, *container.HostConfig) {
67+
func mustParse(t *testing.T, args string) (*container.Config, *container.HostConfig, *networktypes.NetworkingConfig) {
6868
t.Helper()
69-
config, hostConfig, _, err := parseRun(append(strings.Split(args, " "), "ubuntu", "bash"))
69+
config, hostConfig, nwConfig, err := parseRun(append(strings.Split(args, " "), "ubuntu", "bash"))
7070
assert.NilError(t, err)
71-
return config, hostConfig
71+
return config, hostConfig, nwConfig
7272
}
7373

7474
func TestParseRunLinks(t *testing.T) {
75-
if _, hostConfig := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
75+
if _, hostConfig, _ := mustParse(t, "--link a:b"); len(hostConfig.Links) == 0 || hostConfig.Links[0] != "a:b" {
7676
t.Fatalf("Error parsing links. Expected []string{\"a:b\"}, received: %v", hostConfig.Links)
7777
}
78-
if _, hostConfig := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
78+
if _, hostConfig, _ := mustParse(t, "--link a:b --link c:d"); len(hostConfig.Links) < 2 || hostConfig.Links[0] != "a:b" || hostConfig.Links[1] != "c:d" {
7979
t.Fatalf("Error parsing links. Expected []string{\"a:b\", \"c:d\"}, received: %v", hostConfig.Links)
8080
}
81-
if _, hostConfig := mustParse(t, ""); len(hostConfig.Links) != 0 {
81+
if _, hostConfig, _ := mustParse(t, ""); len(hostConfig.Links) != 0 {
8282
t.Fatalf("Error parsing links. No link expected, received: %v", hostConfig.Links)
8383
}
8484
}
@@ -128,7 +128,7 @@ func TestParseRunAttach(t *testing.T) {
128128
for _, tc := range tests {
129129
tc := tc
130130
t.Run(tc.input, func(t *testing.T) {
131-
config, _ := mustParse(t, tc.input)
131+
config, _, _ := mustParse(t, tc.input)
132132
assert.Equal(t, config.AttachStdin, tc.expected.AttachStdin)
133133
assert.Equal(t, config.AttachStdout, tc.expected.AttachStdout)
134134
assert.Equal(t, config.AttachStderr, tc.expected.AttachStderr)
@@ -186,15 +186,15 @@ func TestParseRunWithInvalidArgs(t *testing.T) {
186186
func TestParseWithVolumes(t *testing.T) {
187187
// A single volume
188188
arr, tryit := setupPlatformVolume([]string{`/tmp`}, []string{`c:\tmp`})
189-
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil {
189+
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds != nil {
190190
t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
191191
} else if _, exists := config.Volumes[arr[0]]; !exists {
192192
t.Fatalf("Error parsing volume flags, %q is missing from volumes. Received %v", tryit, config.Volumes)
193193
}
194194

195195
// Two volumes
196196
arr, tryit = setupPlatformVolume([]string{`/tmp`, `/var`}, []string{`c:\tmp`, `c:\var`})
197-
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds != nil {
197+
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds != nil {
198198
t.Fatalf("Error parsing volume flags, %q should not mount-bind anything. Received %v", tryit, hostConfig.Binds)
199199
} else if _, exists := config.Volumes[arr[0]]; !exists {
200200
t.Fatalf("Error parsing volume flags, %s is missing from volumes. Received %v", arr[0], config.Volumes)
@@ -204,13 +204,13 @@ func TestParseWithVolumes(t *testing.T) {
204204

205205
// A single bind mount
206206
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`})
207-
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || hostConfig.Binds[0] != arr[0] {
207+
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || hostConfig.Binds[0] != arr[0] {
208208
t.Fatalf("Error parsing volume flags, %q should mount-bind the path before the colon into the path after the colon. Received %v %v", arr[0], hostConfig.Binds, config.Volumes)
209209
}
210210

211211
// Two bind mounts.
212212
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/hostVar:/containerVar`}, []string{os.Getenv("ProgramData") + `:c:\ContainerPD`, os.Getenv("TEMP") + `:c:\containerTmp`})
213-
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
213+
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
214214
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
215215
}
216216

@@ -219,26 +219,26 @@ func TestParseWithVolumes(t *testing.T) {
219219
arr, tryit = setupPlatformVolume(
220220
[]string{`/hostTmp:/containerTmp:ro`, `/hostVar:/containerVar:rw`},
221221
[]string{os.Getenv("TEMP") + `:c:\containerTmp:rw`, os.Getenv("ProgramData") + `:c:\ContainerPD:rw`})
222-
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
222+
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
223223
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
224224
}
225225

226226
// Similar to previous test but with alternate modes which are only supported by Linux
227227
if runtime.GOOS != "windows" {
228228
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:ro,Z`, `/hostVar:/containerVar:rw,Z`}, []string{})
229-
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
229+
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
230230
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
231231
}
232232

233233
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp:Z`, `/hostVar:/containerVar:z`}, []string{})
234-
if _, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
234+
if _, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || compareRandomizedStrings(hostConfig.Binds[0], hostConfig.Binds[1], arr[0], arr[1]) != nil {
235235
t.Fatalf("Error parsing volume flags, `%s and %s` did not mount-bind correctly. Received %v", arr[0], arr[1], hostConfig.Binds)
236236
}
237237
}
238238

239239
// One bind mount and one volume
240240
arr, tryit = setupPlatformVolume([]string{`/hostTmp:/containerTmp`, `/containerVar`}, []string{os.Getenv("TEMP") + `:c:\containerTmp`, `c:\containerTmp`})
241-
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] {
241+
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] {
242242
t.Fatalf("Error parsing volume flags, %s and %s should only one and only one bind mount %s. Received %s", arr[0], arr[1], arr[0], hostConfig.Binds)
243243
} else if _, exists := config.Volumes[arr[1]]; !exists {
244244
t.Fatalf("Error parsing volume flags %s and %s. %s is missing from volumes. Received %v", arr[0], arr[1], arr[1], config.Volumes)
@@ -247,7 +247,7 @@ func TestParseWithVolumes(t *testing.T) {
247247
// Root to non-c: drive letter (Windows specific)
248248
if runtime.GOOS == "windows" {
249249
arr, tryit = setupPlatformVolume([]string{}, []string{os.Getenv("SystemDrive") + `\:d:`})
250-
if config, hostConfig := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] || len(config.Volumes) != 0 {
250+
if config, hostConfig, _ := mustParse(t, tryit); hostConfig.Binds == nil || len(hostConfig.Binds) > 1 || hostConfig.Binds[0] != arr[0] || len(config.Volumes) != 0 {
251251
t.Fatalf("Error parsing %s. Should have a single bind mount and no volumes", arr[0])
252252
}
253253
}
@@ -290,10 +290,15 @@ func TestParseWithMacAddress(t *testing.T) {
290290
if _, _, _, err := parseRun([]string{invalidMacAddress, "img", "cmd"}); err != nil && err.Error() != "invalidMacAddress is not a valid mac address" {
291291
t.Fatalf("Expected an error with %v mac-address, got %v", invalidMacAddress, err)
292292
}
293-
if config, _ := mustParse(t, validMacAddress); config.MacAddress != "92:d0:c6:0a:29:33" { //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
293+
config, hostConfig, nwConfig := mustParse(t, validMacAddress)
294+
if config.MacAddress != "92:d0:c6:0a:29:33" { //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
294295
t.Fatalf("Expected the config to have '92:d0:c6:0a:29:33' as container-wide MacAddress, got '%v'",
295296
config.MacAddress) //nolint:staticcheck // ignore SA1019: field is deprecated, but still used on API < v1.44.
296297
}
298+
defaultNw := hostConfig.NetworkMode.NetworkName()
299+
if nwConfig.EndpointsConfig[defaultNw].MacAddress != "92:d0:c6:0a:29:33" {
300+
t.Fatalf("Expected the default endpoint to have the MacAddress '92:d0:c6:0a:29:33' set, got '%v'", nwConfig.EndpointsConfig[defaultNw].MacAddress)
301+
}
297302
}
298303

299304
func TestRunFlagsParseWithMemory(t *testing.T) {
@@ -302,7 +307,7 @@ func TestRunFlagsParseWithMemory(t *testing.T) {
302307
err := flags.Parse(args)
303308
assert.ErrorContains(t, err, `invalid argument "invalid" for "-m, --memory" flag`)
304309

305-
_, hostconfig := mustParse(t, "--memory=1G")
310+
_, hostconfig, _ := mustParse(t, "--memory=1G")
306311
assert.Check(t, is.Equal(int64(1073741824), hostconfig.Memory))
307312
}
308313

@@ -312,10 +317,10 @@ func TestParseWithMemorySwap(t *testing.T) {
312317
err := flags.Parse(args)
313318
assert.ErrorContains(t, err, `invalid argument "invalid" for "--memory-swap" flag`)
314319

315-
_, hostconfig := mustParse(t, "--memory-swap=1G")
320+
_, hostconfig, _ := mustParse(t, "--memory-swap=1G")
316321
assert.Check(t, is.Equal(int64(1073741824), hostconfig.MemorySwap))
317322

318-
_, hostconfig = mustParse(t, "--memory-swap=-1")
323+
_, hostconfig, _ = mustParse(t, "--memory-swap=-1")
319324
assert.Check(t, is.Equal(int64(-1), hostconfig.MemorySwap))
320325
}
321326

@@ -330,14 +335,14 @@ func TestParseHostname(t *testing.T) {
330335
hostnameWithDomain := "--hostname=hostname.domainname"
331336
hostnameWithDomainTld := "--hostname=hostname.domainname.tld"
332337
for hostname, expectedHostname := range validHostnames {
333-
if config, _ := mustParse(t, fmt.Sprintf("--hostname=%s", hostname)); config.Hostname != expectedHostname {
338+
if config, _, _ := mustParse(t, fmt.Sprintf("--hostname=%s", hostname)); config.Hostname != expectedHostname {
334339
t.Fatalf("Expected the config to have 'hostname' as %q, got %q", expectedHostname, config.Hostname)
335340
}
336341
}
337-
if config, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname.domainname" || config.Domainname != "" {
342+
if config, _, _ := mustParse(t, hostnameWithDomain); config.Hostname != "hostname.domainname" || config.Domainname != "" {
338343
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname, got %q", config.Hostname)
339344
}
340-
if config, _ := mustParse(t, hostnameWithDomainTld); config.Hostname != "hostname.domainname.tld" || config.Domainname != "" {
345+
if config, _, _ := mustParse(t, hostnameWithDomainTld); config.Hostname != "hostname.domainname.tld" || config.Domainname != "" {
341346
t.Fatalf("Expected the config to have 'hostname' as hostname.domainname.tld, got %q", config.Hostname)
342347
}
343348
}
@@ -351,14 +356,14 @@ func TestParseHostnameDomainname(t *testing.T) {
351356
"domainname-63-bytes-long-should-be-valid-and-without-any-errors": "domainname-63-bytes-long-should-be-valid-and-without-any-errors",
352357
}
353358
for domainname, expectedDomainname := range validDomainnames {
354-
if config, _ := mustParse(t, "--domainname="+domainname); config.Domainname != expectedDomainname {
359+
if config, _, _ := mustParse(t, "--domainname="+domainname); config.Domainname != expectedDomainname {
355360
t.Fatalf("Expected the config to have 'domainname' as %q, got %q", expectedDomainname, config.Domainname)
356361
}
357362
}
358-
if config, _ := mustParse(t, "--hostname=some.prefix --domainname=domainname"); config.Hostname != "some.prefix" || config.Domainname != "domainname" {
363+
if config, _, _ := mustParse(t, "--hostname=some.prefix --domainname=domainname"); config.Hostname != "some.prefix" || config.Domainname != "domainname" {
359364
t.Fatalf("Expected the config to have 'hostname' as 'some.prefix' and 'domainname' as 'domainname', got %q and %q", config.Hostname, config.Domainname)
360365
}
361-
if config, _ := mustParse(t, "--hostname=another-prefix --domainname=domainname.tld"); config.Hostname != "another-prefix" || config.Domainname != "domainname.tld" {
366+
if config, _, _ := mustParse(t, "--hostname=another-prefix --domainname=domainname.tld"); config.Hostname != "another-prefix" || config.Domainname != "domainname.tld" {
362367
t.Fatalf("Expected the config to have 'hostname' as 'another-prefix' and 'domainname' as 'domainname.tld', got %q and %q", config.Hostname, config.Domainname)
363368
}
364369
}

0 commit comments

Comments
 (0)