export_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. containerTypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/api/types/filters"
  10. "github.com/docker/docker/integration/internal/container"
  11. "github.com/docker/docker/integration/internal/request"
  12. "github.com/docker/docker/internal/test/daemon"
  13. "github.com/docker/docker/pkg/jsonmessage"
  14. "github.com/gotestyourself/gotestyourself/assert"
  15. is "github.com/gotestyourself/gotestyourself/assert/cmp"
  16. "github.com/gotestyourself/gotestyourself/poll"
  17. "github.com/gotestyourself/gotestyourself/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 != "linux")
  22. defer setupTest(t)()
  23. client := request.NewAPIClient(t)
  24. ctx := context.Background()
  25. cID := container.Run(t, ctx, client, container.WithCmd("true"))
  26. poll.WaitOn(t, container.IsStopped(ctx, client, cID), poll.WithDelay(100*time.Millisecond))
  27. reference := "repo/testexp:v1"
  28. exportResp, err := client.ContainerExport(ctx, cID)
  29. assert.NilError(t, err)
  30. importResp, err := client.ImageImport(ctx, types.ImageImportSource{
  31. Source: exportResp,
  32. SourceName: "-",
  33. }, reference, types.ImageImportOptions{})
  34. assert.NilError(t, err)
  35. // If the import is successfully, then the message output should contain
  36. // the image ID and match with the output from `docker images`.
  37. dec := json.NewDecoder(importResp)
  38. var jm jsonmessage.JSONMessage
  39. err = dec.Decode(&jm)
  40. assert.NilError(t, err)
  41. images, err := client.ImageList(ctx, types.ImageListOptions{
  42. Filters: filters.NewArgs(filters.Arg("reference", reference)),
  43. })
  44. assert.NilError(t, err)
  45. assert.Check(t, is.Equal(jm.Status, images[0].ID))
  46. }
  47. // TestExportContainerAfterDaemonRestart checks that a container
  48. // created before start of the currently running dockerd
  49. // can be exported (as reported in #36561). To satisfy this
  50. // condition, daemon restart is needed after container creation.
  51. func TestExportContainerAfterDaemonRestart(t *testing.T) {
  52. skip.If(t, testEnv.DaemonInfo.OSType != "linux")
  53. skip.If(t, testEnv.IsRemoteDaemon())
  54. d := daemon.New(t)
  55. client, err := d.NewClient()
  56. assert.NilError(t, err)
  57. d.StartWithBusybox(t)
  58. defer d.Stop(t)
  59. ctx := context.Background()
  60. cfg := containerTypes.Config{
  61. Image: "busybox",
  62. Cmd: []string{"top"},
  63. }
  64. ctr, err := client.ContainerCreate(ctx, &cfg, nil, nil, "")
  65. assert.NilError(t, err)
  66. d.Restart(t)
  67. _, err = client.ContainerExport(ctx, ctr.ID)
  68. assert.NilError(t, err)
  69. }