import_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package image // import "github.com/docker/docker/integration/image"
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "io"
  6. "runtime"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "github.com/docker/docker/api/types"
  11. imagetypes "github.com/docker/docker/api/types/image"
  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. ctx := testutil.StartSpan(baseContext, t)
  25. // Spin up a new daemon, so that we can run this test in parallel (it's a slow test)
  26. d := daemon.New(t)
  27. d.Start(t, "--iptables=false")
  28. defer d.Stop(t)
  29. client := d.NewClientT(t)
  30. // Construct an empty tar archive with about 8GB of junk padding at the
  31. // end. This should not cause any crashes (the padding should be mostly
  32. // ignored).
  33. var tarBuffer bytes.Buffer
  34. tw := tar.NewWriter(&tarBuffer)
  35. err := tw.Close()
  36. assert.NilError(t, err)
  37. imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 8*1024*1024*1024))
  38. reference := strings.ToLower(t.Name()) + ":v42"
  39. _, err = client.ImageImport(ctx,
  40. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  41. reference,
  42. imagetypes.ImportOptions{})
  43. assert.NilError(t, err)
  44. }
  45. func TestImportWithCustomPlatform(t *testing.T) {
  46. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "TODO enable on windows")
  47. ctx := setupTest(t)
  48. client := testEnv.APIClient()
  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. }{
  60. {
  61. platform: "",
  62. expected: image.V1Image{
  63. OS: runtime.GOOS,
  64. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  65. },
  66. },
  67. {
  68. platform: runtime.GOOS,
  69. expected: image.V1Image{
  70. OS: runtime.GOOS,
  71. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  72. },
  73. },
  74. {
  75. platform: strings.ToUpper(runtime.GOOS),
  76. expected: image.V1Image{
  77. OS: runtime.GOOS,
  78. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  79. },
  80. },
  81. {
  82. platform: runtime.GOOS + "/sparc64",
  83. expected: image.V1Image{
  84. OS: runtime.GOOS,
  85. Architecture: "sparc64",
  86. },
  87. },
  88. }
  89. for i, tc := range tests {
  90. tc := tc
  91. t.Run(tc.platform, func(t *testing.T) {
  92. ctx := testutil.StartSpan(ctx, t)
  93. reference := "import-with-platform:tc-" + strconv.Itoa(i)
  94. _, err = client.ImageImport(ctx,
  95. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  96. reference,
  97. imagetypes.ImportOptions{Platform: tc.platform})
  98. assert.NilError(t, err)
  99. inspect, _, err := client.ImageInspectWithRaw(ctx, reference)
  100. assert.NilError(t, err)
  101. assert.Equal(t, inspect.Os, tc.expected.OS)
  102. assert.Equal(t, inspect.Architecture, tc.expected.Architecture)
  103. })
  104. }
  105. }
  106. func TestImportWithCustomPlatformReject(t *testing.T) {
  107. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "TODO enable on windows")
  108. skip.If(t, testEnv.UsingSnapshotter(), "we support importing images/other platforms w/ containerd image store")
  109. ctx := setupTest(t)
  110. client := testEnv.APIClient()
  111. // Construct an empty tar archive.
  112. var tarBuffer bytes.Buffer
  113. tw := tar.NewWriter(&tarBuffer)
  114. err := tw.Close()
  115. assert.NilError(t, err)
  116. imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 0))
  117. tests := []struct {
  118. name string
  119. platform string
  120. expected image.V1Image
  121. expectedErr string
  122. }{
  123. {
  124. platform: " ",
  125. expectedErr: "is an invalid component",
  126. },
  127. {
  128. platform: "/",
  129. expectedErr: "is an invalid component",
  130. },
  131. {
  132. platform: "macos",
  133. expectedErr: "operating system is not supported",
  134. },
  135. {
  136. platform: "macos/arm64",
  137. expectedErr: "operating system is not supported",
  138. },
  139. {
  140. // TODO: platforms.Normalize() only validates os or arch if a single component is passed,
  141. // but ignores unknown os/arch in other cases. See:
  142. // https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
  143. platform: "nintendo64",
  144. expectedErr: "unknown operating system or architecture",
  145. },
  146. }
  147. for i, tc := range tests {
  148. tc := tc
  149. t.Run(tc.platform, func(t *testing.T) {
  150. ctx := testutil.StartSpan(ctx, t)
  151. reference := "import-with-platform:tc-" + strconv.Itoa(i)
  152. _, err = client.ImageImport(ctx,
  153. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  154. reference,
  155. imagetypes.ImportOptions{Platform: tc.platform})
  156. assert.ErrorContains(t, err, tc.expectedErr)
  157. })
  158. }
  159. }