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()

	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,
		},

Review comment (suggestion):
One more comment, then I think we're good to merge ...

Speaking with @thaJeztah last week - he asks that we try to use a common prefix for tests within a file. I'm not sure how consistent we've been with that in the past, but it'll be `TestImport...` in this case.

```suggestion
func TestImageImportBadSrc(t *testing.T) {
```

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,
		},