import_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package image // import "github.com/docker/docker/integration/image"
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "context"
  6. "io"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/image"
  13. "github.com/docker/docker/testutil"
  14. "github.com/docker/docker/testutil/daemon"
  15. "gotest.tools/v3/assert"
  16. "gotest.tools/v3/skip"
  17. )
  18. // Ensure we don't regress on CVE-2017-14992.
  19. func TestImportExtremelyLargeImageWorks(t *testing.T) {
  20. skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
  21. skip.If(t, runtime.GOARCH == "arm64", "effective test will be time out")
  22. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "TODO enable on windows")
  23. t.Parallel()
  24. // Spin up a new daemon, so that we can run this test in parallel (it's a slow test)
  25. d := daemon.New(t)
  26. d.Start(t, "--iptables=false")
  27. defer d.Stop(t)
  28. client := d.NewClientT(t)
  29. // Construct an empty tar archive with about 8GB of junk padding at the
  30. // end. This should not cause any crashes (the padding should be mostly
  31. // ignored).
  32. var tarBuffer bytes.Buffer
  33. tw := tar.NewWriter(&tarBuffer)
  34. err := tw.Close()
  35. assert.NilError(t, err)
  36. imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 8*1024*1024*1024))
  37. reference := strings.ToLower(t.Name()) + ":v42"
  38. _, err = client.ImageImport(context.Background(),
  39. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  40. reference,
  41. types.ImageImportOptions{})
  42. assert.NilError(t, err)
  43. }
  44. func TestImportWithCustomPlatform(t *testing.T) {
  45. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "TODO enable on windows")
  46. defer setupTest(t)()
  47. client := testEnv.APIClient()
  48. ctx := context.Background()
  49. // Construct an empty tar archive.
  50. var tarBuffer bytes.Buffer
  51. tw := tar.NewWriter(&tarBuffer)
  52. err := tw.Close()
  53. assert.NilError(t, err)
  54. imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 0))
  55. tests := []struct {
  56. name string
  57. platform string
  58. expected image.V1Image
  59. expectedErr string
  60. }{
  61. {
  62. platform: "",
  63. expected: image.V1Image{
  64. OS: runtime.GOOS,
  65. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  66. },
  67. },
  68. {
  69. platform: " ",
  70. expectedErr: "is an invalid component",
  71. },
  72. {
  73. platform: "/",
  74. expectedErr: "is an invalid component",
  75. },
  76. {
  77. platform: runtime.GOOS,
  78. expected: image.V1Image{
  79. OS: runtime.GOOS,
  80. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  81. },
  82. },
  83. {
  84. platform: strings.ToUpper(runtime.GOOS),
  85. expected: image.V1Image{
  86. OS: runtime.GOOS,
  87. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  88. },
  89. },
  90. {
  91. platform: runtime.GOOS + "/sparc64",
  92. expected: image.V1Image{
  93. OS: runtime.GOOS,
  94. Architecture: "sparc64",
  95. },
  96. },
  97. {
  98. platform: "macos",
  99. expectedErr: "operating system is not supported",
  100. },
  101. {
  102. platform: "macos/arm64",
  103. expectedErr: "operating system is not supported",
  104. },
  105. {
  106. // TODO: platforms.Normalize() only validates os or arch if a single component is passed,
  107. // but ignores unknown os/arch in other cases. See:
  108. // https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
  109. platform: "nintendo64",
  110. expectedErr: "unknown operating system or architecture",
  111. },
  112. }
  113. for i, tc := range tests {
  114. tc := tc
  115. t.Run(tc.platform, func(t *testing.T) {
  116. reference := "import-with-platform:tc-" + strconv.Itoa(i)
  117. _, err = client.ImageImport(context.Background(),
  118. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  119. reference,
  120. types.ImageImportOptions{Platform: tc.platform})
  121. if tc.expectedErr != "" {
  122. assert.ErrorContains(t, err, tc.expectedErr)
  123. } else {
  124. assert.NilError(t, err)
  125. inspect, _, err := client.ImageInspectWithRaw(ctx, reference)
  126. assert.NilError(t, err)
  127. assert.Equal(t, inspect.Os, tc.expected.OS)
  128. assert.Equal(t, inspect.Architecture, tc.expected.Architecture)
  129. }
  130. })
  131. }
  132. }