import_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. "github.com/docker/docker/image"
  12. "github.com/docker/docker/testutil"
  13. "github.com/docker/docker/testutil/daemon"
  14. "gotest.tools/v3/assert"
  15. "gotest.tools/v3/skip"
  16. )
  17. // Ensure we don't regress on CVE-2017-14992.
  18. func TestImportExtremelyLargeImageWorks(t *testing.T) {
  19. skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
  20. skip.If(t, runtime.GOARCH == "arm64", "effective test will be time out")
  21. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "TODO enable on windows")
  22. t.Parallel()
  23. ctx := testutil.StartSpan(baseContext, t)
  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(ctx,
  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. ctx := setupTest(t)
  47. client := testEnv.APIClient()
  48. // Construct an empty tar archive.
  49. var tarBuffer bytes.Buffer
  50. tw := tar.NewWriter(&tarBuffer)
  51. err := tw.Close()
  52. assert.NilError(t, err)
  53. imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 0))
  54. tests := []struct {
  55. name string
  56. platform string
  57. expected image.V1Image
  58. }{
  59. {
  60. platform: "",
  61. expected: image.V1Image{
  62. OS: runtime.GOOS,
  63. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  64. },
  65. },
  66. {
  67. platform: runtime.GOOS,
  68. expected: image.V1Image{
  69. OS: runtime.GOOS,
  70. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  71. },
  72. },
  73. {
  74. platform: strings.ToUpper(runtime.GOOS),
  75. expected: image.V1Image{
  76. OS: runtime.GOOS,
  77. Architecture: runtime.GOARCH, // this may fail on armhf due to normalization?
  78. },
  79. },
  80. {
  81. platform: runtime.GOOS + "/sparc64",
  82. expected: image.V1Image{
  83. OS: runtime.GOOS,
  84. Architecture: "sparc64",
  85. },
  86. },
  87. }
  88. for i, tc := range tests {
  89. tc := tc
  90. t.Run(tc.platform, func(t *testing.T) {
  91. ctx := testutil.StartSpan(ctx, t)
  92. reference := "import-with-platform:tc-" + strconv.Itoa(i)
  93. _, err = client.ImageImport(ctx,
  94. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  95. reference,
  96. types.ImageImportOptions{Platform: tc.platform})
  97. assert.NilError(t, err)
  98. inspect, _, err := client.ImageInspectWithRaw(ctx, reference)
  99. assert.NilError(t, err)
  100. assert.Equal(t, inspect.Os, tc.expected.OS)
  101. assert.Equal(t, inspect.Architecture, tc.expected.Architecture)
  102. })
  103. }
  104. }
  105. func TestImportWithCustomPlatformReject(t *testing.T) {
  106. skip.If(t, testEnv.DaemonInfo.OSType == "windows", "TODO enable on windows")
  107. skip.If(t, testEnv.UsingSnapshotter(), "we support importing images/other platforms w/ containerd image store")
  108. ctx := setupTest(t)
  109. client := testEnv.APIClient()
  110. // Construct an empty tar archive.
  111. var tarBuffer bytes.Buffer
  112. tw := tar.NewWriter(&tarBuffer)
  113. err := tw.Close()
  114. assert.NilError(t, err)
  115. imageRdr := io.MultiReader(&tarBuffer, io.LimitReader(testutil.DevZero, 0))
  116. tests := []struct {
  117. name string
  118. platform string
  119. expected image.V1Image
  120. expectedErr string
  121. }{
  122. {
  123. platform: " ",
  124. expectedErr: "is an invalid component",
  125. },
  126. {
  127. platform: "/",
  128. expectedErr: "is an invalid component",
  129. },
  130. {
  131. platform: "macos",
  132. expectedErr: "operating system is not supported",
  133. },
  134. {
  135. platform: "macos/arm64",
  136. expectedErr: "operating system is not supported",
  137. },
  138. {
  139. // TODO: platforms.Normalize() only validates os or arch if a single component is passed,
  140. // but ignores unknown os/arch in other cases. See:
  141. // https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
  142. platform: "nintendo64",
  143. expectedErr: "unknown operating system or architecture",
  144. },
  145. }
  146. for i, tc := range tests {
  147. tc := tc
  148. t.Run(tc.platform, func(t *testing.T) {
  149. ctx := testutil.StartSpan(ctx, t)
  150. reference := "import-with-platform:tc-" + strconv.Itoa(i)
  151. _, err = client.ImageImport(ctx,
  152. types.ImageImportSource{Source: imageRdr, SourceName: "-"},
  153. reference,
  154. types.ImageImportOptions{Platform: tc.platform})
  155. assert.ErrorContains(t, err, tc.expectedErr)
  156. })
  157. }
  158. }