create_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "bufio"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/docker/docker/api/types/container"
  12. "github.com/docker/docker/api/types/network"
  13. "github.com/docker/docker/api/types/versions"
  14. "github.com/docker/docker/client"
  15. "github.com/docker/docker/errdefs"
  16. ctr "github.com/docker/docker/integration/internal/container"
  17. net "github.com/docker/docker/integration/internal/network"
  18. "github.com/docker/docker/oci"
  19. "github.com/docker/docker/testutil"
  20. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  21. "gotest.tools/v3/assert"
  22. is "gotest.tools/v3/assert/cmp"
  23. "gotest.tools/v3/poll"
  24. "gotest.tools/v3/skip"
  25. )
  26. func TestCreateFailsWhenIdentifierDoesNotExist(t *testing.T) {
  27. ctx := setupTest(t)
  28. client := testEnv.APIClient()
  29. testCases := []struct {
  30. doc string
  31. image string
  32. expectedError string
  33. }{
  34. {
  35. doc: "image and tag",
  36. image: "test456:v1",
  37. expectedError: "No such image: test456:v1",
  38. },
  39. {
  40. doc: "image no tag",
  41. image: "test456",
  42. expectedError: "No such image: test456",
  43. },
  44. {
  45. doc: "digest",
  46. image: "sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
  47. expectedError: "No such image: sha256:0cb40641836c461bc97c793971d84d758371ed682042457523e4ae701efeaaaa",
  48. },
  49. }
  50. for _, tc := range testCases {
  51. tc := tc
  52. t.Run(tc.doc, func(t *testing.T) {
  53. t.Parallel()
  54. ctx := testutil.StartSpan(ctx, t)
  55. _, err := client.ContainerCreate(ctx,
  56. &container.Config{Image: tc.image},
  57. &container.HostConfig{},
  58. &network.NetworkingConfig{},
  59. nil,
  60. "",
  61. )
  62. assert.Check(t, is.ErrorContains(err, tc.expectedError))
  63. assert.Check(t, errdefs.IsNotFound(err))
  64. })
  65. }
  66. }
  67. // TestCreateLinkToNonExistingContainer verifies that linking to a non-existing
  68. // container returns an "invalid parameter" (400) status, and not the underlying
  69. // "non exists" (404).
  70. func TestCreateLinkToNonExistingContainer(t *testing.T) {
  71. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "legacy links are not supported on windows")
  72. ctx := setupTest(t)
  73. c := testEnv.APIClient()
  74. _, err := c.ContainerCreate(ctx,
  75. &container.Config{
  76. Image: "busybox",
  77. },
  78. &container.HostConfig{
  79. Links: []string{"no-such-container"},
  80. },
  81. &network.NetworkingConfig{},
  82. nil,
  83. "",
  84. )
  85. assert.Check(t, is.ErrorContains(err, "could not get container for no-such-container"))
  86. assert.Check(t, errdefs.IsInvalidParameter(err))
  87. }
  88. func TestCreateWithInvalidEnv(t *testing.T) {
  89. ctx := setupTest(t)
  90. client := testEnv.APIClient()
  91. testCases := []struct {
  92. env string
  93. expectedError string
  94. }{
  95. {
  96. env: "",
  97. expectedError: "invalid environment variable:",
  98. },
  99. {
  100. env: "=",
  101. expectedError: "invalid environment variable: =",
  102. },
  103. {
  104. env: "=foo",
  105. expectedError: "invalid environment variable: =foo",
  106. },
  107. }
  108. for index, tc := range testCases {
  109. tc := tc
  110. t.Run(strconv.Itoa(index), func(t *testing.T) {
  111. t.Parallel()
  112. ctx := testutil.StartSpan(ctx, t)
  113. _, err := client.ContainerCreate(ctx,
  114. &container.Config{
  115. Image: "busybox",
  116. Env: []string{tc.env},
  117. },
  118. &container.HostConfig{},
  119. &network.NetworkingConfig{},
  120. nil,
  121. "",
  122. )
  123. assert.Check(t, is.ErrorContains(err, tc.expectedError))
  124. assert.Check(t, errdefs.IsInvalidParameter(err))
  125. })
  126. }
  127. }
  128. // Test case for #30166 (target was not validated)
  129. func TestCreateTmpfsMountsTarget(t *testing.T) {
  130. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  131. ctx := setupTest(t)
  132. client := testEnv.APIClient()
  133. testCases := []struct {
  134. target string
  135. expectedError string
  136. }{
  137. {
  138. target: ".",
  139. expectedError: "mount path must be absolute",
  140. },
  141. {
  142. target: "foo",
  143. expectedError: "mount path must be absolute",
  144. },
  145. {
  146. target: "/",
  147. expectedError: "destination can't be '/'",
  148. },
  149. {
  150. target: "//",
  151. expectedError: "destination can't be '/'",
  152. },
  153. }
  154. for _, tc := range testCases {
  155. _, err := client.ContainerCreate(ctx,
  156. &container.Config{
  157. Image: "busybox",
  158. },
  159. &container.HostConfig{
  160. Tmpfs: map[string]string{tc.target: ""},
  161. },
  162. &network.NetworkingConfig{},
  163. nil,
  164. "",
  165. )
  166. assert.Check(t, is.ErrorContains(err, tc.expectedError))
  167. assert.Check(t, errdefs.IsInvalidParameter(err))
  168. }
  169. }
  170. func TestCreateWithCustomMaskedPaths(t *testing.T) {
  171. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  172. ctx := setupTest(t)
  173. apiClient := testEnv.APIClient()
  174. testCases := []struct {
  175. maskedPaths []string
  176. expected []string
  177. }{
  178. {
  179. maskedPaths: []string{},
  180. expected: []string{},
  181. },
  182. {
  183. maskedPaths: nil,
  184. expected: oci.DefaultSpec().Linux.MaskedPaths,
  185. },
  186. {
  187. maskedPaths: []string{"/proc/kcore", "/proc/keys"},
  188. expected: []string{"/proc/kcore", "/proc/keys"},
  189. },
  190. }
  191. checkInspect := func(t *testing.T, ctx context.Context, name string, expected []string) {
  192. _, b, err := apiClient.ContainerInspectWithRaw(ctx, name, false)
  193. assert.NilError(t, err)
  194. var inspectJSON map[string]interface{}
  195. err = json.Unmarshal(b, &inspectJSON)
  196. assert.NilError(t, err)
  197. cfg, ok := inspectJSON["HostConfig"].(map[string]interface{})
  198. assert.Check(t, is.Equal(true, ok), name)
  199. maskedPaths, ok := cfg["MaskedPaths"].([]interface{})
  200. assert.Check(t, is.Equal(true, ok), name)
  201. mps := []string{}
  202. for _, mp := range maskedPaths {
  203. mps = append(mps, mp.(string))
  204. }
  205. assert.DeepEqual(t, expected, mps)
  206. }
  207. // TODO: This should be using subtests
  208. for i, tc := range testCases {
  209. name := fmt.Sprintf("create-masked-paths-%d", i)
  210. config := container.Config{
  211. Image: "busybox",
  212. Cmd: []string{"true"},
  213. }
  214. hc := container.HostConfig{}
  215. if tc.maskedPaths != nil {
  216. hc.MaskedPaths = tc.maskedPaths
  217. }
  218. // Create the container.
  219. c, err := apiClient.ContainerCreate(ctx,
  220. &config,
  221. &hc,
  222. &network.NetworkingConfig{},
  223. nil,
  224. name,
  225. )
  226. assert.NilError(t, err)
  227. checkInspect(t, ctx, name, tc.expected)
  228. // Start the container.
  229. err = apiClient.ContainerStart(ctx, c.ID, container.StartOptions{})
  230. assert.NilError(t, err)
  231. poll.WaitOn(t, ctr.IsInState(ctx, apiClient, c.ID, "exited"), poll.WithDelay(100*time.Millisecond))
  232. checkInspect(t, ctx, name, tc.expected)
  233. }
  234. }
  235. func TestCreateWithCustomReadonlyPaths(t *testing.T) {
  236. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  237. ctx := setupTest(t)
  238. apiClient := testEnv.APIClient()
  239. testCases := []struct {
  240. readonlyPaths []string
  241. expected []string
  242. }{
  243. {
  244. readonlyPaths: []string{},
  245. expected: []string{},
  246. },
  247. {
  248. readonlyPaths: nil,
  249. expected: oci.DefaultSpec().Linux.ReadonlyPaths,
  250. },
  251. {
  252. readonlyPaths: []string{"/proc/asound", "/proc/bus"},
  253. expected: []string{"/proc/asound", "/proc/bus"},
  254. },
  255. }
  256. checkInspect := func(t *testing.T, ctx context.Context, name string, expected []string) {
  257. _, b, err := apiClient.ContainerInspectWithRaw(ctx, name, false)
  258. assert.NilError(t, err)
  259. var inspectJSON map[string]interface{}
  260. err = json.Unmarshal(b, &inspectJSON)
  261. assert.NilError(t, err)
  262. cfg, ok := inspectJSON["HostConfig"].(map[string]interface{})
  263. assert.Check(t, is.Equal(true, ok), name)
  264. readonlyPaths, ok := cfg["ReadonlyPaths"].([]interface{})
  265. assert.Check(t, is.Equal(true, ok), name)
  266. rops := []string{}
  267. for _, rop := range readonlyPaths {
  268. rops = append(rops, rop.(string))
  269. }
  270. assert.DeepEqual(t, expected, rops)
  271. }
  272. for i, tc := range testCases {
  273. name := fmt.Sprintf("create-readonly-paths-%d", i)
  274. config := container.Config{
  275. Image: "busybox",
  276. Cmd: []string{"true"},
  277. }
  278. hc := container.HostConfig{}
  279. if tc.readonlyPaths != nil {
  280. hc.ReadonlyPaths = tc.readonlyPaths
  281. }
  282. // Create the container.
  283. c, err := apiClient.ContainerCreate(ctx,
  284. &config,
  285. &hc,
  286. &network.NetworkingConfig{},
  287. nil,
  288. name,
  289. )
  290. assert.NilError(t, err)
  291. checkInspect(t, ctx, name, tc.expected)
  292. // Start the container.
  293. err = apiClient.ContainerStart(ctx, c.ID, container.StartOptions{})
  294. assert.NilError(t, err)
  295. poll.WaitOn(t, ctr.IsInState(ctx, apiClient, c.ID, "exited"), poll.WithDelay(100*time.Millisecond))
  296. checkInspect(t, ctx, name, tc.expected)
  297. }
  298. }
  299. func TestCreateWithInvalidHealthcheckParams(t *testing.T) {
  300. ctx := setupTest(t)
  301. apiClient := testEnv.APIClient()
  302. testCases := []struct {
  303. doc string
  304. interval time.Duration
  305. timeout time.Duration
  306. retries int
  307. startPeriod time.Duration
  308. startInterval time.Duration
  309. expectedErr string
  310. }{
  311. {
  312. doc: "test invalid Interval in Healthcheck: less than 0s",
  313. interval: -10 * time.Millisecond,
  314. timeout: time.Second,
  315. retries: 1000,
  316. expectedErr: fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration),
  317. },
  318. {
  319. doc: "test invalid Interval in Healthcheck: larger than 0s but less than 1ms",
  320. interval: 500 * time.Microsecond,
  321. timeout: time.Second,
  322. retries: 1000,
  323. expectedErr: fmt.Sprintf("Interval in Healthcheck cannot be less than %s", container.MinimumDuration),
  324. },
  325. {
  326. doc: "test invalid Timeout in Healthcheck: less than 1ms",
  327. interval: time.Second,
  328. timeout: -100 * time.Millisecond,
  329. retries: 1000,
  330. expectedErr: fmt.Sprintf("Timeout in Healthcheck cannot be less than %s", container.MinimumDuration),
  331. },
  332. {
  333. doc: "test invalid Retries in Healthcheck: less than 0",
  334. interval: time.Second,
  335. timeout: time.Second,
  336. retries: -10,
  337. expectedErr: "Retries in Healthcheck cannot be negative",
  338. },
  339. {
  340. doc: "test invalid StartPeriod in Healthcheck: not 0 and less than 1ms",
  341. interval: time.Second,
  342. timeout: time.Second,
  343. retries: 1000,
  344. startPeriod: 100 * time.Microsecond,
  345. expectedErr: fmt.Sprintf("StartPeriod in Healthcheck cannot be less than %s", container.MinimumDuration),
  346. },
  347. {
  348. doc: "test invalid StartInterval in Healthcheck: not 0 and less than 1ms",
  349. interval: time.Second,
  350. timeout: time.Second,
  351. retries: 1000,
  352. startPeriod: time.Second,
  353. startInterval: 100 * time.Microsecond,
  354. expectedErr: fmt.Sprintf("StartInterval in Healthcheck cannot be less than %s", container.MinimumDuration),
  355. },
  356. }
  357. for _, tc := range testCases {
  358. tc := tc
  359. t.Run(tc.doc, func(t *testing.T) {
  360. t.Parallel()
  361. ctx := testutil.StartSpan(ctx, t)
  362. cfg := container.Config{
  363. Image: "busybox",
  364. Healthcheck: &container.HealthConfig{
  365. Interval: tc.interval,
  366. Timeout: tc.timeout,
  367. Retries: tc.retries,
  368. StartInterval: tc.startInterval,
  369. },
  370. }
  371. if tc.startPeriod != 0 {
  372. cfg.Healthcheck.StartPeriod = tc.startPeriod
  373. }
  374. resp, err := apiClient.ContainerCreate(ctx, &cfg, &container.HostConfig{}, nil, nil, "")
  375. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  376. assert.Check(t, errdefs.IsInvalidParameter(err))
  377. assert.ErrorContains(t, err, tc.expectedErr)
  378. })
  379. }
  380. }
  381. // Make sure that anonymous volumes can be overritten by tmpfs
  382. // https://github.com/moby/moby/issues/40446
  383. func TestCreateTmpfsOverrideAnonymousVolume(t *testing.T) {
  384. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "windows does not support tmpfs")
  385. ctx := setupTest(t)
  386. apiClient := testEnv.APIClient()
  387. id := ctr.Create(ctx, t, apiClient,
  388. ctr.WithVolume("/foo"),
  389. ctr.WithTmpfs("/foo"),
  390. ctr.WithVolume("/bar"),
  391. ctr.WithTmpfs("/bar:size=999"),
  392. ctr.WithCmd("/bin/sh", "-c", "mount | grep '/foo' | grep tmpfs && mount | grep '/bar' | grep tmpfs"),
  393. )
  394. defer func() {
  395. err := apiClient.ContainerRemove(ctx, id, container.RemoveOptions{Force: true})
  396. assert.NilError(t, err)
  397. }()
  398. inspect, err := apiClient.ContainerInspect(ctx, id)
  399. assert.NilError(t, err)
  400. // tmpfs do not currently get added to inspect.Mounts
  401. // Normally an anonymous volume would, except now tmpfs should prevent that.
  402. assert.Assert(t, is.Len(inspect.Mounts, 0))
  403. chWait, chErr := apiClient.ContainerWait(ctx, id, container.WaitConditionNextExit)
  404. assert.NilError(t, apiClient.ContainerStart(ctx, id, container.StartOptions{}))
  405. timeout := time.NewTimer(30 * time.Second)
  406. defer timeout.Stop()
  407. select {
  408. case <-timeout.C:
  409. t.Fatal("timeout waiting for container to exit")
  410. case status := <-chWait:
  411. var errMsg string
  412. if status.Error != nil {
  413. errMsg = status.Error.Message
  414. }
  415. assert.Equal(t, int(status.StatusCode), 0, errMsg)
  416. case err := <-chErr:
  417. assert.NilError(t, err)
  418. }
  419. }
  420. // Test that if the referenced image platform does not match the requested platform on container create that we get an
  421. // error.
  422. func TestCreateDifferentPlatform(t *testing.T) {
  423. ctx := setupTest(t)
  424. apiClient := testEnv.APIClient()
  425. img, _, err := apiClient.ImageInspectWithRaw(ctx, "busybox:latest")
  426. assert.NilError(t, err)
  427. assert.Assert(t, img.Architecture != "")
  428. t.Run("different os", func(t *testing.T) {
  429. ctx := testutil.StartSpan(ctx, t)
  430. p := ocispec.Platform{
  431. OS: img.Os + "DifferentOS",
  432. Architecture: img.Architecture,
  433. Variant: img.Variant,
  434. }
  435. _, err := apiClient.ContainerCreate(ctx, &container.Config{Image: "busybox:latest"}, &container.HostConfig{}, nil, &p, "")
  436. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  437. })
  438. t.Run("different cpu arch", func(t *testing.T) {
  439. ctx := testutil.StartSpan(ctx, t)
  440. p := ocispec.Platform{
  441. OS: img.Os,
  442. Architecture: img.Architecture + "DifferentArch",
  443. Variant: img.Variant,
  444. }
  445. _, err := apiClient.ContainerCreate(ctx, &container.Config{Image: "busybox:latest"}, &container.HostConfig{}, nil, &p, "")
  446. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  447. })
  448. }
  449. func TestCreateVolumesFromNonExistingContainer(t *testing.T) {
  450. ctx := setupTest(t)
  451. cli := testEnv.APIClient()
  452. _, err := cli.ContainerCreate(
  453. ctx,
  454. &container.Config{Image: "busybox"},
  455. &container.HostConfig{VolumesFrom: []string{"nosuchcontainer"}},
  456. nil,
  457. nil,
  458. "",
  459. )
  460. assert.Check(t, errdefs.IsInvalidParameter(err))
  461. }
  462. // Test that we can create a container from an image that is for a different platform even if a platform was not specified
  463. // This is for the regression detailed here: https://github.com/moby/moby/issues/41552
  464. func TestCreatePlatformSpecificImageNoPlatform(t *testing.T) {
  465. ctx := setupTest(t)
  466. skip.If(t, testEnv.DaemonInfo.Architecture == "arm", "test only makes sense to run on non-arm systems")
  467. skip.If(t, testEnv.DaemonInfo.OSType != "linux", "test image is only available on linux")
  468. cli := testEnv.APIClient()
  469. _, err := cli.ContainerCreate(
  470. ctx,
  471. &container.Config{Image: "arm32v7/hello-world"},
  472. &container.HostConfig{},
  473. nil,
  474. nil,
  475. "",
  476. )
  477. assert.NilError(t, err)
  478. }
  479. func TestCreateInvalidHostConfig(t *testing.T) {
  480. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  481. ctx := setupTest(t)
  482. apiClient := testEnv.APIClient()
  483. testCases := []struct {
  484. doc string
  485. hc container.HostConfig
  486. expectedErr string
  487. }{
  488. {
  489. doc: "invalid IpcMode",
  490. hc: container.HostConfig{IpcMode: "invalid"},
  491. expectedErr: "Error response from daemon: invalid IPC mode: invalid",
  492. },
  493. {
  494. doc: "invalid PidMode",
  495. hc: container.HostConfig{PidMode: "invalid"},
  496. expectedErr: "Error response from daemon: invalid PID mode: invalid",
  497. },
  498. {
  499. doc: "invalid PidMode without container ID",
  500. hc: container.HostConfig{PidMode: "container"},
  501. expectedErr: "Error response from daemon: invalid PID mode: container",
  502. },
  503. {
  504. doc: "invalid UTSMode",
  505. hc: container.HostConfig{UTSMode: "invalid"},
  506. expectedErr: "Error response from daemon: invalid UTS mode: invalid",
  507. },
  508. {
  509. doc: "invalid Annotations",
  510. hc: container.HostConfig{Annotations: map[string]string{"": "a"}},
  511. expectedErr: "Error response from daemon: invalid Annotations: the empty string is not permitted as an annotation key",
  512. },
  513. }
  514. for _, tc := range testCases {
  515. tc := tc
  516. t.Run(tc.doc, func(t *testing.T) {
  517. t.Parallel()
  518. ctx := testutil.StartSpan(ctx, t)
  519. cfg := container.Config{
  520. Image: "busybox",
  521. }
  522. resp, err := apiClient.ContainerCreate(ctx, &cfg, &tc.hc, nil, nil, "")
  523. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  524. assert.Check(t, errdefs.IsInvalidParameter(err), "got: %T", err)
  525. assert.Error(t, err, tc.expectedErr)
  526. })
  527. }
  528. }
  529. func TestCreateWithMultipleEndpointSettings(t *testing.T) {
  530. ctx := setupTest(t)
  531. testcases := []struct {
  532. apiVersion string
  533. expectedErr string
  534. }{
  535. {apiVersion: "1.44"},
  536. {apiVersion: "1.43", expectedErr: "Container cannot be created with multiple network endpoints"},
  537. }
  538. for _, tc := range testcases {
  539. t.Run("with API v"+tc.apiVersion, func(t *testing.T) {
  540. apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(tc.apiVersion))
  541. assert.NilError(t, err)
  542. config := container.Config{
  543. Image: "busybox",
  544. }
  545. networkingConfig := network.NetworkingConfig{
  546. EndpointsConfig: map[string]*network.EndpointSettings{
  547. "net1": {},
  548. "net2": {},
  549. "net3": {},
  550. },
  551. }
  552. _, err = apiClient.ContainerCreate(ctx, &config, &container.HostConfig{}, &networkingConfig, nil, "")
  553. if tc.expectedErr == "" {
  554. assert.NilError(t, err)
  555. } else {
  556. assert.ErrorContains(t, err, tc.expectedErr)
  557. }
  558. })
  559. }
  560. }
  561. func TestCreateWithCustomMACs(t *testing.T) {
  562. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  563. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.44"), "requires API v1.44")
  564. ctx := setupTest(t)
  565. apiClient := testEnv.APIClient()
  566. net.CreateNoError(ctx, t, apiClient, "testnet")
  567. attachCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
  568. defer cancel()
  569. res := ctr.RunAttach(attachCtx, t, apiClient,
  570. ctr.WithCmd("ip", "-o", "link", "show"),
  571. ctr.WithNetworkMode("bridge"),
  572. ctr.WithMacAddress("bridge", "02:32:1c:23:00:04"))
  573. assert.Equal(t, res.ExitCode, 0)
  574. assert.Equal(t, res.Stderr.String(), "")
  575. scanner := bufio.NewScanner(res.Stdout)
  576. for scanner.Scan() {
  577. fields := strings.Fields(scanner.Text())
  578. // The expected output is:
  579. // 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1000\ link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  580. // 134: eth0@if135: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1400 qdisc noqueue \ link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
  581. if len(fields) < 11 {
  582. continue
  583. }
  584. ifaceName := fields[1]
  585. if ifaceName[:3] != "eth" {
  586. continue
  587. }
  588. mac := fields[len(fields)-3]
  589. assert.Equal(t, mac, "02:32:1c:23:00:04")
  590. }
  591. }