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