export_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package container // import "github.com/docker/docker/integration/container"
  2. import (
  3. "context"
  4. "encoding/json"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/docker/docker/api/types"
  9. "github.com/docker/docker/api/types/filters"
  10. "github.com/docker/docker/integration/internal/container"
  11. "github.com/docker/docker/pkg/jsonmessage"
  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. defer setupTest(t)()
  22. client := testEnv.APIClient()
  23. ctx := context.Background()
  24. cID := container.Run(ctx, t, client, container.WithCmd("true"))
  25. poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
  26. reference := "repo/" + strings.ToLower(t.Name()) + ":v1"
  27. exportResp, err := client.ContainerExport(ctx, cID)
  28. assert.NilError(t, err)
  29. importResp, err := client.ImageImport(ctx, types.ImageImportSource{
  30. Source: exportResp,
  31. SourceName: "-",
  32. }, reference, types.ImageImportOptions{})
  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 := client.ImageList(ctx, types.ImageListOptions{
  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. d := daemon.New(t)
  54. c := d.NewClientT(t)
  55. d.StartWithBusybox(t)
  56. defer d.Stop(t)
  57. ctx := context.Background()
  58. ctrID := container.Create(ctx, t, c)
  59. d.Restart(t)
  60. _, err := c.ContainerExport(ctx, ctrID)
  61. assert.NilError(t, err)
  62. }