checkpoint_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "fmt"
  5. "os/exec"
  6. "regexp"
  7. "sort"
  8. "testing"
  9. "time"
  10. "github.com/docker/docker/api/types"
  11. mounttypes "github.com/docker/docker/api/types/mount"
  12. "github.com/docker/docker/client"
  13. "github.com/docker/docker/integration/internal/container"
  14. "github.com/docker/docker/internal/test/request"
  15. "gotest.tools/assert"
  16. is "gotest.tools/assert/cmp"
  17. "gotest.tools/poll"
  18. "gotest.tools/skip"
  19. )
  20. func containerExec(t *testing.T, client client.APIClient, cID string, cmd []string) {
  21. t.Logf("Exec: %s", cmd)
  22. ctx := context.Background()
  23. r, err := container.Exec(ctx, client, cID, cmd)
  24. assert.NilError(t, err)
  25. t.Log(r.Combined())
  26. assert.Equal(t, r.ExitCode, 0)
  27. }
  28. func TestCheckpoint(t *testing.T) {
  29. t.Skip("TestCheckpoint is broken; see https://github.com/moby/moby/issues/38963")
  30. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  31. skip.If(t, !testEnv.DaemonInfo.ExperimentalBuild)
  32. defer setupTest(t)()
  33. cmd := exec.Command("criu", "check")
  34. stdoutStderr, err := cmd.CombinedOutput()
  35. t.Logf("%s", stdoutStderr)
  36. assert.NilError(t, err)
  37. ctx := context.Background()
  38. client := request.NewAPIClient(t)
  39. mnt := mounttypes.Mount{
  40. Type: mounttypes.TypeTmpfs,
  41. Target: "/tmp",
  42. }
  43. t.Log("Start a container")
  44. cID := container.Run(t, ctx, client, container.WithMount(mnt))
  45. poll.WaitOn(t,
  46. container.IsInState(ctx, client, cID, "running"),
  47. poll.WithDelay(100*time.Millisecond),
  48. )
  49. cptOpt := types.CheckpointCreateOptions{
  50. Exit: false,
  51. CheckpointID: "test",
  52. }
  53. {
  54. // FIXME: ipv6 iptables modules are not uploaded in the test environment
  55. cmd := exec.Command("bash", "-c", "set -x; "+
  56. "mount --bind $(type -P true) $(type -P ip6tables-restore) && "+
  57. "mount --bind $(type -P true) $(type -P ip6tables-save)")
  58. stdoutStderr, err = cmd.CombinedOutput()
  59. t.Logf("%s", stdoutStderr)
  60. assert.NilError(t, err)
  61. defer func() {
  62. cmd := exec.Command("bash", "-c", "set -x; "+
  63. "umount -c -i -l $(type -P ip6tables-restore); "+
  64. "umount -c -i -l $(type -P ip6tables-save)")
  65. stdoutStderr, err = cmd.CombinedOutput()
  66. t.Logf("%s", stdoutStderr)
  67. assert.NilError(t, err)
  68. }()
  69. }
  70. t.Log("Do a checkpoint and leave the container running")
  71. err = client.CheckpointCreate(ctx, cID, cptOpt)
  72. if err != nil {
  73. // An error can contain a path to a dump file
  74. t.Logf("%s", err)
  75. re := regexp.MustCompile("path= (.*): ")
  76. m := re.FindStringSubmatch(fmt.Sprintf("%s", err))
  77. if len(m) >= 2 {
  78. dumpLog := m[1]
  79. t.Logf("%s", dumpLog)
  80. cmd := exec.Command("cat", dumpLog)
  81. stdoutStderr, err = cmd.CombinedOutput()
  82. t.Logf("%s", stdoutStderr)
  83. }
  84. }
  85. assert.NilError(t, err)
  86. inspect, err := client.ContainerInspect(ctx, cID)
  87. assert.NilError(t, err)
  88. assert.Check(t, is.Equal(true, inspect.State.Running))
  89. checkpoints, err := client.CheckpointList(ctx, cID, types.CheckpointListOptions{})
  90. assert.NilError(t, err)
  91. assert.Equal(t, len(checkpoints), 1)
  92. assert.Equal(t, checkpoints[0].Name, "test")
  93. // Create a test file on a tmpfs mount.
  94. containerExec(t, client, cID, []string{"touch", "/tmp/test-file"})
  95. // Do a second checkpoint
  96. cptOpt = types.CheckpointCreateOptions{
  97. Exit: true,
  98. CheckpointID: "test2",
  99. }
  100. t.Log("Do a checkpoint and stop the container")
  101. err = client.CheckpointCreate(ctx, cID, cptOpt)
  102. assert.NilError(t, err)
  103. poll.WaitOn(t,
  104. container.IsInState(ctx, client, cID, "exited"),
  105. poll.WithDelay(100*time.Millisecond),
  106. )
  107. inspect, err = client.ContainerInspect(ctx, cID)
  108. assert.NilError(t, err)
  109. assert.Check(t, is.Equal(false, inspect.State.Running))
  110. // Check that both checkpoints are listed.
  111. checkpoints, err = client.CheckpointList(ctx, cID, types.CheckpointListOptions{})
  112. assert.NilError(t, err)
  113. assert.Equal(t, len(checkpoints), 2)
  114. cptNames := make([]string, 2)
  115. for i, c := range checkpoints {
  116. cptNames[i] = c.Name
  117. }
  118. sort.Strings(cptNames)
  119. assert.Equal(t, cptNames[0], "test")
  120. assert.Equal(t, cptNames[1], "test2")
  121. // Restore the container from a second checkpoint.
  122. startOpt := types.ContainerStartOptions{
  123. CheckpointID: "test2",
  124. }
  125. t.Log("Restore the container")
  126. err = client.ContainerStart(ctx, cID, startOpt)
  127. assert.NilError(t, err)
  128. inspect, err = client.ContainerInspect(ctx, cID)
  129. assert.NilError(t, err)
  130. assert.Check(t, is.Equal(true, inspect.State.Running))
  131. // Check that the test file has been restored.
  132. containerExec(t, client, cID, []string{"test", "-f", "/tmp/test-file"})
  133. for _, id := range []string{"test", "test2"} {
  134. cptDelOpt := types.CheckpointDeleteOptions{
  135. CheckpointID: id,
  136. }
  137. err = client.CheckpointDelete(ctx, cID, cptDelOpt)
  138. assert.NilError(t, err)
  139. }
  140. }