export_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/integration/internal/request"
  11. "github.com/docker/docker/internal/test/daemon"
  12. "github.com/docker/docker/pkg/jsonmessage"
  13. "github.com/gotestyourself/gotestyourself/assert"
  14. is "github.com/gotestyourself/gotestyourself/assert/cmp"
  15. "github.com/gotestyourself/gotestyourself/poll"
  16. "github.com/gotestyourself/gotestyourself/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 != "linux")
  21. defer setupTest(t)()
  22. client := request.NewAPIClient(t)
  23. ctx := context.Background()
  24. cID := container.Run(t, ctx, client, container.WithCmd("true"))
  25. poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
  26. reference := "repo/testexp: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 != "linux")
  52. skip.If(t, testEnv.IsRemoteDaemon())
  53. d := daemon.New(t)
  54. client, err := d.NewClient()
  55. assert.NilError(t, err)
  56. d.StartWithBusybox(t)
  57. defer d.Stop(t)
  58. ctx := context.Background()
  59. ctrID := container.Create(t, ctx, client)
  60. d.Restart(t)
  61. _, err = client.ContainerExport(ctx, ctrID)
  62. assert.NilError(t, err)
  63. }