daemon_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. package daemon // import "github.com/docker/docker/integration/daemon"
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "runtime"
  13. "strings"
  14. "syscall"
  15. "testing"
  16. "github.com/docker/docker/api/types"
  17. "github.com/docker/docker/api/types/mount"
  18. "github.com/docker/docker/api/types/volume"
  19. "github.com/docker/docker/daemon/config"
  20. "github.com/docker/docker/errdefs"
  21. "github.com/docker/docker/integration/internal/container"
  22. "github.com/docker/docker/integration/internal/process"
  23. "github.com/docker/docker/pkg/stdcopy"
  24. "github.com/docker/docker/testutil/daemon"
  25. "gotest.tools/v3/assert"
  26. is "gotest.tools/v3/assert/cmp"
  27. "gotest.tools/v3/icmd"
  28. "gotest.tools/v3/poll"
  29. "gotest.tools/v3/skip"
  30. )
  31. func TestConfigDaemonID(t *testing.T) {
  32. skip.If(t, runtime.GOOS == "windows")
  33. d := daemon.New(t)
  34. defer d.Stop(t)
  35. d.Start(t, "--iptables=false")
  36. info := d.Info(t)
  37. assert.Check(t, info.ID != "")
  38. d.Stop(t)
  39. // Verify that (if present) the engine-id file takes precedence
  40. const engineID = "this-is-the-engine-id"
  41. idFile := filepath.Join(d.RootDir(), "engine-id")
  42. assert.Check(t, os.Remove(idFile))
  43. // Using 0644 to allow rootless daemons to read the file (ideally
  44. // we'd chown the file to have the remapped user as owner).
  45. err := os.WriteFile(idFile, []byte(engineID), 0o644)
  46. assert.NilError(t, err)
  47. d.Start(t, "--iptables=false")
  48. info = d.Info(t)
  49. assert.Equal(t, info.ID, engineID)
  50. d.Stop(t)
  51. }
  52. func TestDaemonConfigValidation(t *testing.T) {
  53. skip.If(t, runtime.GOOS == "windows")
  54. d := daemon.New(t)
  55. dockerBinary, err := d.BinaryPath()
  56. assert.NilError(t, err)
  57. params := []string{"--validate", "--config-file"}
  58. dest := os.Getenv("DOCKER_INTEGRATION_DAEMON_DEST")
  59. if dest == "" {
  60. dest = os.Getenv("DEST")
  61. }
  62. testdata := filepath.Join(dest, "..", "..", "integration", "daemon", "testdata")
  63. const (
  64. validOut = "configuration OK"
  65. failedOut = "unable to configure the Docker daemon with file"
  66. )
  67. tests := []struct {
  68. name string
  69. args []string
  70. expectedOut string
  71. }{
  72. {
  73. name: "config with no content",
  74. args: append(params, filepath.Join(testdata, "empty-config-1.json")),
  75. expectedOut: validOut,
  76. },
  77. {
  78. name: "config with {}",
  79. args: append(params, filepath.Join(testdata, "empty-config-2.json")),
  80. expectedOut: validOut,
  81. },
  82. {
  83. name: "invalid config",
  84. args: append(params, filepath.Join(testdata, "invalid-config-1.json")),
  85. expectedOut: failedOut,
  86. },
  87. {
  88. name: "malformed config",
  89. args: append(params, filepath.Join(testdata, "malformed-config.json")),
  90. expectedOut: failedOut,
  91. },
  92. {
  93. name: "valid config",
  94. args: append(params, filepath.Join(testdata, "valid-config-1.json")),
  95. expectedOut: validOut,
  96. },
  97. }
  98. for _, tc := range tests {
  99. tc := tc
  100. t.Run(tc.name, func(t *testing.T) {
  101. t.Parallel()
  102. cmd := exec.Command(dockerBinary, tc.args...)
  103. out, err := cmd.CombinedOutput()
  104. assert.Check(t, is.Contains(string(out), tc.expectedOut))
  105. if tc.expectedOut == failedOut {
  106. assert.ErrorContains(t, err, "", "expected an error, but got none")
  107. } else {
  108. assert.NilError(t, err)
  109. }
  110. })
  111. }
  112. }
  113. func TestConfigDaemonSeccompProfiles(t *testing.T) {
  114. skip.If(t, runtime.GOOS == "windows")
  115. d := daemon.New(t)
  116. defer d.Stop(t)
  117. tests := []struct {
  118. doc string
  119. profile string
  120. expectedProfile string
  121. }{
  122. {
  123. doc: "empty profile set",
  124. profile: "",
  125. expectedProfile: config.SeccompProfileDefault,
  126. },
  127. {
  128. doc: "default profile",
  129. profile: config.SeccompProfileDefault,
  130. expectedProfile: config.SeccompProfileDefault,
  131. },
  132. {
  133. doc: "unconfined profile",
  134. profile: config.SeccompProfileUnconfined,
  135. expectedProfile: config.SeccompProfileUnconfined,
  136. },
  137. }
  138. for _, tc := range tests {
  139. tc := tc
  140. t.Run(tc.doc, func(t *testing.T) {
  141. d.Start(t, "--seccomp-profile="+tc.profile)
  142. info := d.Info(t)
  143. assert.Assert(t, is.Contains(info.SecurityOptions, "name=seccomp,profile="+tc.expectedProfile))
  144. d.Stop(t)
  145. cfg := filepath.Join(d.RootDir(), "daemon.json")
  146. err := os.WriteFile(cfg, []byte(`{"seccomp-profile": "`+tc.profile+`"}`), 0644)
  147. assert.NilError(t, err)
  148. d.Start(t, "--config-file", cfg)
  149. info = d.Info(t)
  150. assert.Assert(t, is.Contains(info.SecurityOptions, "name=seccomp,profile="+tc.expectedProfile))
  151. d.Stop(t)
  152. })
  153. }
  154. }
  155. func TestDaemonProxy(t *testing.T) {
  156. skip.If(t, runtime.GOOS == "windows", "cannot start multiple daemons on windows")
  157. skip.If(t, os.Getenv("DOCKER_ROOTLESS") != "", "cannot connect to localhost proxy in rootless environment")
  158. newProxy := func(rcvd *string, t *testing.T) *httptest.Server {
  159. s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  160. *rcvd = r.Host
  161. w.Header().Set("Content-Type", "application/json")
  162. _, _ = w.Write([]byte("OK"))
  163. }))
  164. t.Cleanup(s.Close)
  165. return s
  166. }
  167. const userPass = "myuser:mypassword@"
  168. // Configure proxy through env-vars
  169. t.Run("environment variables", func(t *testing.T) {
  170. t.Parallel()
  171. ctx := context.Background()
  172. var received string
  173. proxyServer := newProxy(&received, t)
  174. d := daemon.New(t, daemon.WithEnvVars(
  175. "HTTP_PROXY="+proxyServer.URL,
  176. "HTTPS_PROXY="+proxyServer.URL,
  177. "NO_PROXY=example.com",
  178. ))
  179. c := d.NewClientT(t)
  180. d.Start(t, "--iptables=false")
  181. defer d.Stop(t)
  182. info := d.Info(t)
  183. assert.Check(t, is.Equal(info.HTTPProxy, proxyServer.URL))
  184. assert.Check(t, is.Equal(info.HTTPSProxy, proxyServer.URL))
  185. assert.Check(t, is.Equal(info.NoProxy, "example.com"))
  186. _, err := c.ImagePull(ctx, "example.org:5000/some/image:latest", types.ImagePullOptions{})
  187. assert.ErrorContains(t, err, "", "pulling should have failed")
  188. assert.Equal(t, received, "example.org:5000")
  189. // Test NoProxy: example.com should not hit the proxy, and "received" variable should not be changed.
  190. _, err = c.ImagePull(ctx, "example.com/some/image:latest", types.ImagePullOptions{})
  191. assert.ErrorContains(t, err, "", "pulling should have failed")
  192. assert.Equal(t, received, "example.org:5000", "should not have used proxy")
  193. })
  194. // Configure proxy through command-line flags
  195. t.Run("command-line options", func(t *testing.T) {
  196. t.Parallel()
  197. ctx := context.Background()
  198. var received string
  199. proxyServer := newProxy(&received, t)
  200. d := daemon.New(t, daemon.WithEnvVars(
  201. "HTTP_PROXY="+"http://"+userPass+"from-env-http.invalid",
  202. "http_proxy="+"http://"+userPass+"from-env-http.invalid",
  203. "HTTPS_PROXY="+"https://"+userPass+"myuser:mypassword@from-env-https-invalid",
  204. "https_proxy="+"https://"+userPass+"myuser:mypassword@from-env-https-invalid",
  205. "NO_PROXY=ignore.invalid",
  206. "no_proxy=ignore.invalid",
  207. ))
  208. d.Start(t, "--iptables=false", "--http-proxy", proxyServer.URL, "--https-proxy", proxyServer.URL, "--no-proxy", "example.com")
  209. defer d.Stop(t)
  210. c := d.NewClientT(t)
  211. info := d.Info(t)
  212. assert.Check(t, is.Equal(info.HTTPProxy, proxyServer.URL))
  213. assert.Check(t, is.Equal(info.HTTPSProxy, proxyServer.URL))
  214. assert.Check(t, is.Equal(info.NoProxy, "example.com"))
  215. ok, _ := d.ScanLogsT(ctx, t, daemon.ScanLogsMatchAll(
  216. "overriding existing proxy variable with value from configuration",
  217. "http_proxy",
  218. "HTTP_PROXY",
  219. "https_proxy",
  220. "HTTPS_PROXY",
  221. "no_proxy",
  222. "NO_PROXY",
  223. ))
  224. assert.Assert(t, ok)
  225. ok, logs := d.ScanLogsT(ctx, t, daemon.ScanLogsMatchString(userPass))
  226. assert.Assert(t, !ok, "logs should not contain the non-sanitized proxy URL: %s", logs)
  227. _, err := c.ImagePull(ctx, "example.org:5001/some/image:latest", types.ImagePullOptions{})
  228. assert.ErrorContains(t, err, "", "pulling should have failed")
  229. assert.Equal(t, received, "example.org:5001")
  230. // Test NoProxy: example.com should not hit the proxy, and "received" variable should not be changed.
  231. _, err = c.ImagePull(ctx, "example.com/some/image:latest", types.ImagePullOptions{})
  232. assert.ErrorContains(t, err, "", "pulling should have failed")
  233. assert.Equal(t, received, "example.org:5001", "should not have used proxy")
  234. })
  235. // Configure proxy through configuration file
  236. t.Run("configuration file", func(t *testing.T) {
  237. t.Parallel()
  238. ctx := context.Background()
  239. var received string
  240. proxyServer := newProxy(&received, t)
  241. d := daemon.New(t, daemon.WithEnvVars(
  242. "HTTP_PROXY="+"http://"+userPass+"from-env-http.invalid",
  243. "http_proxy="+"http://"+userPass+"from-env-http.invalid",
  244. "HTTPS_PROXY="+"https://"+userPass+"myuser:mypassword@from-env-https-invalid",
  245. "https_proxy="+"https://"+userPass+"myuser:mypassword@from-env-https-invalid",
  246. "NO_PROXY=ignore.invalid",
  247. "no_proxy=ignore.invalid",
  248. ))
  249. c := d.NewClientT(t)
  250. configFile := filepath.Join(d.RootDir(), "daemon.json")
  251. configJSON := fmt.Sprintf(`{"proxies":{"http-proxy":%[1]q, "https-proxy": %[1]q, "no-proxy": "example.com"}}`, proxyServer.URL)
  252. assert.NilError(t, os.WriteFile(configFile, []byte(configJSON), 0644))
  253. d.Start(t, "--iptables=false", "--config-file", configFile)
  254. defer d.Stop(t)
  255. info := d.Info(t)
  256. assert.Check(t, is.Equal(info.HTTPProxy, proxyServer.URL))
  257. assert.Check(t, is.Equal(info.HTTPSProxy, proxyServer.URL))
  258. assert.Check(t, is.Equal(info.NoProxy, "example.com"))
  259. d.ScanLogsT(ctx, t, daemon.ScanLogsMatchAll(
  260. "overriding existing proxy variable with value from configuration",
  261. "http_proxy",
  262. "HTTP_PROXY",
  263. "https_proxy",
  264. "HTTPS_PROXY",
  265. "no_proxy",
  266. "NO_PROXY",
  267. ))
  268. _, err := c.ImagePull(ctx, "example.org:5002/some/image:latest", types.ImagePullOptions{})
  269. assert.ErrorContains(t, err, "", "pulling should have failed")
  270. assert.Equal(t, received, "example.org:5002")
  271. // Test NoProxy: example.com should not hit the proxy, and "received" variable should not be changed.
  272. _, err = c.ImagePull(ctx, "example.com/some/image:latest", types.ImagePullOptions{})
  273. assert.ErrorContains(t, err, "", "pulling should have failed")
  274. assert.Equal(t, received, "example.org:5002", "should not have used proxy")
  275. })
  276. // Conflicting options (passed both through command-line options and config file)
  277. t.Run("conflicting options", func(t *testing.T) {
  278. ctx := context.Background()
  279. const (
  280. proxyRawURL = "https://" + userPass + "example.org"
  281. proxyURL = "https://xxxxx:xxxxx@example.org"
  282. )
  283. d := daemon.New(t)
  284. configFile := filepath.Join(d.RootDir(), "daemon.json")
  285. configJSON := fmt.Sprintf(`{"proxies":{"http-proxy":%[1]q, "https-proxy": %[1]q, "no-proxy": "example.com"}}`, proxyRawURL)
  286. assert.NilError(t, os.WriteFile(configFile, []byte(configJSON), 0644))
  287. err := d.StartWithError("--http-proxy", proxyRawURL, "--https-proxy", proxyRawURL, "--no-proxy", "example.com", "--config-file", configFile, "--validate")
  288. assert.ErrorContains(t, err, "daemon exited during startup")
  289. expected := fmt.Sprintf(
  290. `the following directives are specified both as a flag and in the configuration file: http-proxy: (from flag: %[1]s, from file: %[1]s), https-proxy: (from flag: %[1]s, from file: %[1]s), no-proxy: (from flag: example.com, from file: example.com)`,
  291. proxyURL,
  292. )
  293. poll.WaitOn(t, d.PollCheckLogs(ctx, daemon.ScanLogsMatchString(expected)))
  294. })
  295. // Make sure values are sanitized when reloading the daemon-config
  296. t.Run("reload sanitized", func(t *testing.T) {
  297. t.Parallel()
  298. ctx := context.Background()
  299. const (
  300. proxyRawURL = "https://" + userPass + "example.org"
  301. proxyURL = "https://xxxxx:xxxxx@example.org"
  302. )
  303. d := daemon.New(t)
  304. d.Start(t, "--iptables=false", "--http-proxy", proxyRawURL, "--https-proxy", proxyRawURL, "--no-proxy", "example.com")
  305. defer d.Stop(t)
  306. err := d.Signal(syscall.SIGHUP)
  307. assert.NilError(t, err)
  308. poll.WaitOn(t, d.PollCheckLogs(ctx, daemon.ScanLogsMatchAll("Reloaded configuration:", proxyURL)))
  309. ok, logs := d.ScanLogsT(ctx, t, daemon.ScanLogsMatchString(userPass))
  310. assert.Assert(t, !ok, "logs should not contain the non-sanitized proxy URL: %s", logs)
  311. })
  312. }
  313. func TestLiveRestore(t *testing.T) {
  314. skip.If(t, runtime.GOOS == "windows", "cannot start multiple daemons on windows")
  315. t.Run("volume references", testLiveRestoreVolumeReferences)
  316. t.Run("autoremove", testLiveRestoreAutoRemove)
  317. }
  318. func testLiveRestoreAutoRemove(t *testing.T) {
  319. skip.If(t, testEnv.IsRootless(), "restarted rootless daemon will have a new process namespace")
  320. t.Parallel()
  321. ctx := context.Background()
  322. run := func(t *testing.T) (*daemon.Daemon, func(), string) {
  323. d := daemon.New(t)
  324. d.StartWithBusybox(t, "--live-restore", "--iptables=false")
  325. t.Cleanup(func() {
  326. d.Stop(t)
  327. d.Cleanup(t)
  328. })
  329. tmpDir := t.TempDir()
  330. apiClient := d.NewClientT(t)
  331. cID := container.Run(ctx, t, apiClient,
  332. container.WithBind(tmpDir, "/v"),
  333. // Run until a 'stop' file is created.
  334. container.WithCmd("sh", "-c", "while [ ! -f /v/stop ]; do sleep 0.1; done"),
  335. container.WithAutoRemove)
  336. t.Cleanup(func() { apiClient.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true}) })
  337. finishContainer := func() {
  338. file, err := os.Create(filepath.Join(tmpDir, "stop"))
  339. assert.NilError(t, err, "Failed to create 'stop' file")
  340. file.Close()
  341. }
  342. return d, finishContainer, cID
  343. }
  344. t.Run("engine restart shouldnt kill alive containers", func(t *testing.T) {
  345. d, finishContainer, cID := run(t)
  346. d.Restart(t, "--live-restore", "--iptables=false")
  347. apiClient := d.NewClientT(t)
  348. _, err := apiClient.ContainerInspect(ctx, cID)
  349. assert.NilError(t, err, "Container shouldn't be removed after engine restart")
  350. finishContainer()
  351. poll.WaitOn(t, container.IsRemoved(ctx, apiClient, cID))
  352. })
  353. t.Run("engine restart should remove containers that exited", func(t *testing.T) {
  354. d, finishContainer, cID := run(t)
  355. apiClient := d.NewClientT(t)
  356. // Get PID of the container process.
  357. inspect, err := apiClient.ContainerInspect(ctx, cID)
  358. assert.NilError(t, err)
  359. pid := inspect.State.Pid
  360. d.Stop(t)
  361. finishContainer()
  362. poll.WaitOn(t, process.NotAlive(pid))
  363. d.Start(t, "--live-restore", "--iptables=false")
  364. poll.WaitOn(t, container.IsRemoved(ctx, apiClient, cID))
  365. })
  366. }
  367. func testLiveRestoreVolumeReferences(t *testing.T) {
  368. t.Parallel()
  369. d := daemon.New(t)
  370. d.StartWithBusybox(t, "--live-restore", "--iptables=false")
  371. defer func() {
  372. d.Stop(t)
  373. d.Cleanup(t)
  374. }()
  375. c := d.NewClientT(t)
  376. ctx := context.Background()
  377. runTest := func(t *testing.T, policy string) {
  378. t.Run(policy, func(t *testing.T) {
  379. volName := "test-live-restore-volume-references-" + policy
  380. _, err := c.VolumeCreate(ctx, volume.CreateOptions{Name: volName})
  381. assert.NilError(t, err)
  382. // Create a container that uses the volume
  383. m := mount.Mount{
  384. Type: mount.TypeVolume,
  385. Source: volName,
  386. Target: "/foo",
  387. }
  388. cID := container.Run(ctx, t, c, container.WithMount(m), container.WithCmd("top"), container.WithRestartPolicy(policy))
  389. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  390. // Stop the daemon
  391. d.Restart(t, "--live-restore", "--iptables=false")
  392. // Try to remove the volume
  393. err = c.VolumeRemove(ctx, volName, false)
  394. assert.ErrorContains(t, err, "volume is in use")
  395. _, err = c.VolumeInspect(ctx, volName)
  396. assert.NilError(t, err)
  397. })
  398. }
  399. t.Run("restartPolicy", func(t *testing.T) {
  400. runTest(t, "always")
  401. runTest(t, "unless-stopped")
  402. runTest(t, "on-failure")
  403. runTest(t, "no")
  404. })
  405. // Make sure that the local volume driver's mount ref count is restored
  406. // Addresses https://github.com/moby/moby/issues/44422
  407. t.Run("local volume with mount options", func(t *testing.T) {
  408. v, err := c.VolumeCreate(ctx, volume.CreateOptions{
  409. Driver: "local",
  410. Name: "test-live-restore-volume-references-local",
  411. DriverOpts: map[string]string{
  412. "type": "tmpfs",
  413. "device": "tmpfs",
  414. },
  415. })
  416. assert.NilError(t, err)
  417. m := mount.Mount{
  418. Type: mount.TypeVolume,
  419. Source: v.Name,
  420. Target: "/foo",
  421. }
  422. const testContent = "hello"
  423. cID := container.Run(ctx, t, c, container.WithMount(m), container.WithCmd("sh", "-c", "echo "+testContent+">>/foo/test.txt; sleep infinity"))
  424. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  425. // Wait until container creates a file in the volume.
  426. poll.WaitOn(t, func(t poll.LogT) poll.Result {
  427. stat, err := c.ContainerStatPath(ctx, cID, "/foo/test.txt")
  428. if err != nil {
  429. if errdefs.IsNotFound(err) {
  430. return poll.Continue("file doesn't yet exist")
  431. }
  432. return poll.Error(err)
  433. }
  434. if int(stat.Size) != len(testContent)+1 {
  435. return poll.Error(fmt.Errorf("unexpected test file size: %d", stat.Size))
  436. }
  437. return poll.Success()
  438. })
  439. d.Restart(t, "--live-restore", "--iptables=false")
  440. // Try to remove the volume
  441. // This should fail since its used by a container
  442. err = c.VolumeRemove(ctx, v.Name, false)
  443. assert.ErrorContains(t, err, "volume is in use")
  444. t.Run("volume still mounted", func(t *testing.T) {
  445. skip.If(t, testEnv.IsRootless(), "restarted rootless daemon has a new mount namespace and it won't have the previous mounts")
  446. // Check if a new container with the same volume has access to the previous content.
  447. // This fails if the volume gets unmounted at startup.
  448. cID2 := container.Run(ctx, t, c, container.WithMount(m), container.WithCmd("cat", "/foo/test.txt"))
  449. defer c.ContainerRemove(ctx, cID2, types.ContainerRemoveOptions{Force: true})
  450. poll.WaitOn(t, container.IsStopped(ctx, c, cID2))
  451. inspect, err := c.ContainerInspect(ctx, cID2)
  452. if assert.Check(t, err) {
  453. assert.Check(t, is.Equal(inspect.State.ExitCode, 0), "volume doesn't have the same file")
  454. }
  455. logs, err := c.ContainerLogs(ctx, cID2, types.ContainerLogsOptions{ShowStdout: true})
  456. assert.NilError(t, err)
  457. defer logs.Close()
  458. var stdoutBuf bytes.Buffer
  459. _, err = stdcopy.StdCopy(&stdoutBuf, io.Discard, logs)
  460. assert.NilError(t, err)
  461. assert.Check(t, is.Equal(strings.TrimSpace(stdoutBuf.String()), testContent))
  462. })
  463. // Remove that container which should free the references in the volume
  464. err = c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  465. assert.NilError(t, err)
  466. // Now we should be able to remove the volume
  467. err = c.VolumeRemove(ctx, v.Name, false)
  468. assert.NilError(t, err)
  469. })
  470. // Make sure that we don't panic if the container has bind-mounts
  471. // (which should not be "restored")
  472. // Regression test for https://github.com/moby/moby/issues/45898
  473. t.Run("container with bind-mounts", func(t *testing.T) {
  474. m := mount.Mount{
  475. Type: mount.TypeBind,
  476. Source: os.TempDir(),
  477. Target: "/foo",
  478. }
  479. cID := container.Run(ctx, t, c, container.WithMount(m), container.WithCmd("top"))
  480. defer c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  481. d.Restart(t, "--live-restore", "--iptables=false")
  482. err := c.ContainerRemove(ctx, cID, types.ContainerRemoveOptions{Force: true})
  483. assert.NilError(t, err)
  484. })
  485. }
  486. func TestDaemonDefaultBridgeWithFixedCidrButNoBip(t *testing.T) {
  487. skip.If(t, runtime.GOOS == "windows")
  488. bridgeName := "ext-bridge1"
  489. d := daemon.New(t, daemon.WithEnvVars("DOCKER_TEST_CREATE_DEFAULT_BRIDGE="+bridgeName))
  490. defer func() {
  491. d.Stop(t)
  492. d.Cleanup(t)
  493. }()
  494. defer func() {
  495. // No need to clean up when running this test in rootless mode, as the
  496. // interface is deleted when the daemon is stopped and the netns
  497. // reclaimed by the kernel.
  498. if !testEnv.IsRootless() {
  499. deleteInterface(t, bridgeName)
  500. }
  501. }()
  502. d.StartWithBusybox(t, "--bridge", bridgeName, "--fixed-cidr", "192.168.130.0/24")
  503. }
  504. func deleteInterface(t *testing.T, ifName string) {
  505. icmd.RunCommand("ip", "link", "delete", ifName).Assert(t, icmd.Success)
  506. icmd.RunCommand("iptables", "-t", "nat", "--flush").Assert(t, icmd.Success)
  507. icmd.RunCommand("iptables", "--flush").Assert(t, icmd.Success)
  508. }