export_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "encoding/json"
  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/internal/test/daemon"
  11. "github.com/docker/docker/pkg/jsonmessage"
  12. "gotest.tools/assert"
  13. is "gotest.tools/assert/cmp"
  14. "gotest.tools/poll"
  15. "gotest.tools/skip"
  16. )
  17. // export an image and try to import it into a new one
  18. func TestExportContainerAndImportImage(t *testing.T) {
  19. skip.If(t, testEnv.DaemonInfo.OSType == "windows")
  20. defer setupTest(t)()
  21. client := testEnv.APIClient()
  22. ctx := context.Background()
  23. cID := container.Run(t, ctx, client, container.WithCmd("true"))
  24. poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
  25. reference := "repo/testexp:v1"
  26. exportResp, err := client.ContainerExport(ctx, cID)
  27. assert.NilError(t, err)
  28. importResp, err := client.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 := client.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. d := daemon.New(t)
  53. c := d.NewClientT(t)
  54. d.StartWithBusybox(t)
  55. defer d.Stop(t)
  56. ctx := context.Background()
  57. ctrID := container.Create(t, ctx, c)
  58. d.Restart(t)
  59. _, err := c.ContainerExport(ctx, ctrID)
  60. assert.NilError(t, err)
  61. }