create_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. if versions.LessThan(testEnv.DaemonAPIVersion(), "1.32") {
  377. assert.Check(t, errdefs.IsSystem(err))
  378. } else {
  379. assert.Check(t, errdefs.IsInvalidParameter(err))
  380. }
  381. assert.ErrorContains(t, err, tc.expectedErr)
  382. })
  383. }
  384. }
  385. // Make sure that anonymous volumes can be overritten by tmpfs
  386. // https://github.com/moby/moby/issues/40446
  387. func TestCreateTmpfsOverrideAnonymousVolume(t *testing.T) {
  388. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "windows does not support tmpfs")
  389. ctx := setupTest(t)
  390. apiClient := testEnv.APIClient()
  391. id := ctr.Create(ctx, t, apiClient,
  392. ctr.WithVolume("/foo"),
  393. ctr.WithTmpfs("/foo"),
  394. ctr.WithVolume("/bar"),
  395. ctr.WithTmpfs("/bar:size=999"),
  396. ctr.WithCmd("/bin/sh", "-c", "mount | grep '/foo' | grep tmpfs && mount | grep '/bar' | grep tmpfs"),
  397. )
  398. defer func() {
  399. err := apiClient.ContainerRemove(ctx, id, container.RemoveOptions{Force: true})
  400. assert.NilError(t, err)
  401. }()
  402. inspect, err := apiClient.ContainerInspect(ctx, id)
  403. assert.NilError(t, err)
  404. // tmpfs do not currently get added to inspect.Mounts
  405. // Normally an anonymous volume would, except now tmpfs should prevent that.
  406. assert.Assert(t, is.Len(inspect.Mounts, 0))
  407. chWait, chErr := apiClient.ContainerWait(ctx, id, container.WaitConditionNextExit)
  408. assert.NilError(t, apiClient.ContainerStart(ctx, id, container.StartOptions{}))
  409. timeout := time.NewTimer(30 * time.Second)
  410. defer timeout.Stop()
  411. select {
  412. case <-timeout.C:
  413. t.Fatal("timeout waiting for container to exit")
  414. case status := <-chWait:
  415. var errMsg string
  416. if status.Error != nil {
  417. errMsg = status.Error.Message
  418. }
  419. assert.Equal(t, int(status.StatusCode), 0, errMsg)
  420. case err := <-chErr:
  421. assert.NilError(t, err)
  422. }
  423. }
  424. // Test that if the referenced image platform does not match the requested platform on container create that we get an
  425. // error.
  426. func TestCreateDifferentPlatform(t *testing.T) {
  427. ctx := setupTest(t)
  428. apiClient := testEnv.APIClient()
  429. img, _, err := apiClient.ImageInspectWithRaw(ctx, "busybox:latest")
  430. assert.NilError(t, err)
  431. assert.Assert(t, img.Architecture != "")
  432. t.Run("different os", func(t *testing.T) {
  433. ctx := testutil.StartSpan(ctx, t)
  434. p := ocispec.Platform{
  435. OS: img.Os + "DifferentOS",
  436. Architecture: img.Architecture,
  437. Variant: img.Variant,
  438. }
  439. _, err := apiClient.ContainerCreate(ctx, &container.Config{Image: "busybox:latest"}, &container.HostConfig{}, nil, &p, "")
  440. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  441. })
  442. t.Run("different cpu arch", func(t *testing.T) {
  443. ctx := testutil.StartSpan(ctx, t)
  444. p := ocispec.Platform{
  445. OS: img.Os,
  446. Architecture: img.Architecture + "DifferentArch",
  447. Variant: img.Variant,
  448. }
  449. _, err := apiClient.ContainerCreate(ctx, &container.Config{Image: "busybox:latest"}, &container.HostConfig{}, nil, &p, "")
  450. assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
  451. })
  452. }
  453. func TestCreateVolumesFromNonExistingContainer(t *testing.T) {
  454. ctx := setupTest(t)
  455. cli := testEnv.APIClient()
  456. _, err := cli.ContainerCreate(
  457. ctx,
  458. &container.Config{Image: "busybox"},
  459. &container.HostConfig{VolumesFrom: []string{"nosuchcontainer"}},
  460. nil,
  461. nil,
  462. "",
  463. )
  464. assert.Check(t, errdefs.IsInvalidParameter(err))
  465. }
  466. // Test that we can create a container from an image that is for a different platform even if a platform was not specified
  467. // This is for the regression detailed here: https://github.com/moby/moby/issues/41552
  468. func TestCreatePlatformSpecificImageNoPlatform(t *testing.T) {
  469. ctx := setupTest(t)
  470. skip.If(t, testEnv.DaemonInfo.Architecture == "arm", "test only makes sense to run on non-arm systems")
  471. skip.If(t, testEnv.DaemonInfo.OSType != "linux", "test image is only available on linux")
  472. cli := testEnv.APIClient()
  473. _, err := cli.ContainerCreate(
  474. ctx,
  475. &container.Config{Image: "arm32v7/hello-world"},
  476. &container.HostConfig{},
  477. nil,
  478. nil,
  479. "",
  480. )
  481. assert.NilError(t, err)
  482. }
  483. func TestCreateInvalidHostConfig(t *testing.T) {
  484. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  485. ctx := setupTest(t)
  486. apiClient := testEnv.APIClient()
  487. testCases := []struct {
  488. doc string
  489. hc container.HostConfig
  490. expectedErr string
  491. }{
  492. {
  493. doc: "invalid IpcMode",
  494. hc: container.HostConfig{IpcMode: "invalid"},
  495. expectedErr: "Error response from daemon: invalid IPC mode: invalid",
  496. },
  497. {
  498. doc: "invalid PidMode",
  499. hc: container.HostConfig{PidMode: "invalid"},
  500. expectedErr: "Error response from daemon: invalid PID mode: invalid",
  501. },
  502. {
  503. doc: "invalid PidMode without container ID",
  504. hc: container.HostConfig{PidMode: "container"},
  505. expectedErr: "Error response from daemon: invalid PID mode: container",
  506. },
  507. {
  508. doc: "invalid UTSMode",
  509. hc: container.HostConfig{UTSMode: "invalid"},
  510. expectedErr: "Error response from daemon: invalid UTS mode: invalid",
  511. },
  512. {
  513. doc: "invalid Annotations",
  514. hc: container.HostConfig{Annotations: map[string]string{"": "a"}},
  515. expectedErr: "Error response from daemon: invalid Annotations: the empty string is not permitted as an annotation key",
  516. },
  517. }
  518. for _, tc := range testCases {
  519. tc := tc
  520. t.Run(tc.doc, func(t *testing.T) {
  521. t.Parallel()
  522. ctx := testutil.StartSpan(ctx, t)
  523. cfg := container.Config{
  524. Image: "busybox",
  525. }
  526. resp, err := apiClient.ContainerCreate(ctx, &cfg, &tc.hc, nil, nil, "")
  527. assert.Check(t, is.Equal(len(resp.Warnings), 0))
  528. assert.Check(t, errdefs.IsInvalidParameter(err), "got: %T", err)
  529. assert.Error(t, err, tc.expectedErr)
  530. })
  531. }
  532. }
  533. func TestCreateWithMultipleEndpointSettings(t *testing.T) {
  534. ctx := setupTest(t)
  535. testcases := []struct {
  536. apiVersion string
  537. expectedErr string
  538. }{
  539. {apiVersion: "1.44"},
  540. {apiVersion: "1.43", expectedErr: "Container cannot be created with multiple network endpoints"},
  541. }
  542. for _, tc := range testcases {
  543. t.Run("with API v"+tc.apiVersion, func(t *testing.T) {
  544. apiClient, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion(tc.apiVersion))
  545. assert.NilError(t, err)
  546. config := container.Config{
  547. Image: "busybox",
  548. }
  549. networkingConfig := network.NetworkingConfig{
  550. EndpointsConfig: map[string]*network.EndpointSettings{
  551. "net1": {},
  552. "net2": {},
  553. "net3": {},
  554. },
  555. }
  556. _, err = apiClient.ContainerCreate(ctx, &config, &container.HostConfig{}, &networkingConfig, nil, "")
  557. if tc.expectedErr == "" {
  558. assert.NilError(t, err)
  559. } else {
  560. assert.ErrorContains(t, err, tc.expectedErr)
  561. }
  562. })
  563. }
  564. }
  565. func TestCreateWithCustomMACs(t *testing.T) {
  566. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  567. skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.44"), "requires API v1.44")
  568. ctx := setupTest(t)
  569. apiClient := testEnv.APIClient()
  570. net.CreateNoError(ctx, t, apiClient, "testnet")
  571. attachCtx, cancel := context.WithTimeout(ctx, 1*time.Second)
  572. defer cancel()
  573. res := ctr.RunAttach(attachCtx, t, apiClient,
  574. ctr.WithCmd("ip", "-o", "link", "show"),
  575. ctr.WithNetworkMode("bridge"),
  576. ctr.WithMacAddress("bridge", "02:32:1c:23:00:04"))
  577. assert.Equal(t, res.ExitCode, 0)
  578. assert.Equal(t, res.Stderr.String(), "")
  579. scanner := bufio.NewScanner(res.Stdout)
  580. for scanner.Scan() {
  581. fields := strings.Fields(scanner.Text())
  582. // The expected output is:
  583. // 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
  584. // 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
  585. if len(fields) < 11 {
  586. continue
  587. }
  588. ifaceName := fields[1]
  589. if ifaceName[:3] != "eth" {
  590. continue
  591. }
  592. mac := fields[len(fields)-3]
  593. assert.Equal(t, mac, "02:32:1c:23:00:04")
  594. }
  595. }