Repository: moby/moby
File: integration/image/import_test.go (Go)

Code before:
			// TODO: platforms.Normalize() only validates os or arch if a single component is passed,
			//       but ignores unknown os/arch in other cases. See:
			//       https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
			platform: ocispec.Platform{
				OS: "nintendo64",
			},
			expectedErr: "unknown operating system or architecture",
		},
	}

	for i, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ctx := testutil.StartSpan(ctx, t)
			reference := "import-with-platform:tc-" + strconv.Itoa(i)
			_, err = apiClient.ImageImport(ctx,
				client.ImageImportSource{Source: imageRdr, SourceName: "-"},
				reference,
				client.ImageImportOptions{Platform: tc.platform})

			assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
			assert.Check(t, is.ErrorContains(err, tc.expectedErr))
		})
	}
}

func TestAPIImageImportBadSrc(t *testing.T) {
	ctx := setupTest(t)
	apiClient := testEnv.APIClient()

	server := httptest.NewServer(nil)
	defer server.Close()

	trimmedHTTP := strings.TrimPrefix(server.URL, "http://")

	tests := []struct {
		name      string
		fromSrc   string
		expectErr func(error) bool
	}{
		{
			name:      "missing file via full URL",
			fromSrc:   server.URL + "/nofile.tar",
			expectErr: cerrdefs.IsNotFound,
		},
		{
			name:      "missing file via trimmed URL",
			fromSrc:   trimmedHTTP + "/nofile.tar",
			expectErr: cerrdefs.IsNotFound,
		},
		{
			name:      "encoded path via trimmed URL",

Review comment (bug):
The first three tests are failing in the rootless runs, it'll need a Skip ...

```
	skip.If(t, testEnv.IsRootless, "rootless daemon cannot access the test's HTTP server in the host's netns")
```

```
=== FAIL: amd64.docker.docker.integration.image TestAPIImageImportBadSrc/missing_file_via_full_URL (0.00s)
    import_test.go:240: err = &client.httpError{err:(*fmt.wrapError)(0xc000412fa0), errdef:errdefs.errInternal{}}
    import_test.go:242: assertion failed: expression is false: tc.expectErr(err)
    --- FAIL: TestAPIImageImportBadSrc/missing_file_via_full_URL (0.00s)

=== FAIL: amd64.docker.docker.integration.image TestAPIImageImportBadSrc/missing_file_via_trimmed_URL (0.00s)
    import_test.go:240: err = &client.httpError{err:(*fmt.wrapError)(0xc00011ac40), errdef:errdefs.errInternal{}}
    import_test.go:242: assertion failed: expression is false: tc.expectErr(err)
    --- FAIL: TestAPIImageImportBadSrc/missing_file_via_trimmed_URL (0.00s)

=== FAIL: amd64.docker.docker.integration.image TestAPIImageImportBadSrc/encoded_path_via_trimmed_URL (0.00s)
    import_test.go:240: err = &client.httpError{err:(*fmt.wrapError)(0xc00005caa0), errdef:errdefs.errInternal{}}
    import_test.go:242: assertion failed: expression is false: tc.expectErr(err)
    --- FAIL: TestAPIImageImportBadSrc/encoded_path_via_trimmed_URL (0.00s)
```

Code after:
			// TODO: platforms.Normalize() only validates os or arch if a single component is passed,
			//       but ignores unknown os/arch in other cases. See:
			//       https://github.com/containerd/containerd/blob/7d4891783aac5adf6cd83f657852574a71875631/platforms/platforms.go#L183-L209
			platform: ocispec.Platform{
				OS: "nintendo64",
			},
			expectedErr: "unknown operating system or architecture",
		},
	}

	for i, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			ctx := testutil.StartSpan(ctx, t)
			reference := "import-with-platform:tc-" + strconv.Itoa(i)
			_, err = apiClient.ImageImport(ctx,
				client.ImageImportSource{Source: imageRdr, SourceName: "-"},
				reference,
				client.ImageImportOptions{Platform: tc.platform})

			assert.Check(t, is.ErrorType(err, cerrdefs.IsInvalidArgument))
			assert.Check(t, is.ErrorContains(err, tc.expectedErr))
		})
	}
}

func TestImageImportBadSrc(t *testing.T) {
	ctx := setupTest(t)
	apiClient := testEnv.APIClient()

	skip.If(t, testEnv.IsRootless, "rootless daemon cannot access the test's HTTP server in the host's netns")

	server := httptest.NewServer(nil)
	defer server.Close()

	trimmedHTTP := strings.TrimPrefix(server.URL, "http://")

	tests := []struct {
		name      string
		fromSrc   string
		expectErr func(error) bool
	}{
		{
			name:      "missing file via full URL",
			fromSrc:   server.URL + "/nofile.tar",
			expectErr: cerrdefs.IsNotFound,
		},
		{
			name:      "missing file via trimmed URL",
			fromSrc:   trimmedHTTP + "/nofile.tar",
			expectErr: cerrdefs.IsNotFound,
		},