export_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "encoding/json"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/docker/docker/api/types"
  8. "github.com/docker/docker/api/types/filters"
  9. "github.com/docker/docker/api/types/image"
  10. "github.com/docker/docker/integration/internal/container"
  11. "github.com/docker/docker/pkg/jsonmessage"
  12. "github.com/docker/docker/testutil"
  13. "github.com/docker/docker/testutil/daemon"
  14. "gotest.tools/v3/assert"
  15. is "gotest.tools/v3/assert/cmp"
  16. "gotest.tools/v3/poll"
  17. "gotest.tools/v3/skip"
  18. )
  19. // export an image and try to import it into a new one
  20. func TestExportContainerAndImportImage(t *testing.T) {
  21. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  22. ctx := setupTest(t)
  23. apiClient := testEnv.APIClient()
  24. cID := container.Run(ctx, t, apiClient, container.WithCmd("true"))
  25. poll.WaitOn(t, container.IsStopped(ctx, apiClient, cID), poll.WithDelay(100*time.Millisecond))
  26. reference := "repo/" + strings.ToLower(t.Name()) + ":v1"
  27. exportResp, err := apiClient.ContainerExport(ctx, cID)
  28. assert.NilError(t, err)
  29. importResp, err := apiClient.ImageImport(ctx, types.ImageImportSource{
  30. Source: exportResp,
  31. SourceName: "-",
  32. }, reference, image.ImportOptions{})
  33. assert.NilError(t, err)
  34. // If the import is successfully, then the message output should contain
  35. // the image ID and match with the output from `docker images`.
  36. dec := json.NewDecoder(importResp)
  37. var jm jsonmessage.JSONMessage
  38. err = dec.Decode(&jm)
  39. assert.NilError(t, err)
  40. images, err := apiClient.ImageList(ctx, image.ListOptions{
  41. Filters: filters.NewArgs(filters.Arg("reference", reference)),
  42. })
  43. assert.NilError(t, err)
  44. assert.Check(t, is.Equal(jm.Status, images[0].ID))
  45. }
  46. // TestExportContainerAfterDaemonRestart checks that a container
  47. // created before start of the currently running dockerd
  48. // can be exported (as reported in #36561). To satisfy this
  49. // condition, daemon restart is needed after container creation.
  50. func TestExportContainerAfterDaemonRestart(t *testing.T) {
  51. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  52. skip.If(t, testEnv.IsRemoteDaemon)
  53. ctx := testutil.StartSpan(baseContext, t)
  54. d := daemon.New(t)
  55. c := d.NewClientT(t)
  56. d.StartWithBusybox(ctx, t)
  57. defer d.Stop(t)
  58. ctrID := container.Create(ctx, t, c)
  59. d.Restart(t)
  60. _, err := c.ContainerExport(ctx, ctrID)
  61. assert.NilError(t, err)
  62. }