export_test.go 2.3 KB

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