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

Code before:
	assert.Check(t, is.Equal(resp.Items[1].Deleted, imageIdx.Manifests[0].Digest.String()))
	// TODO(vvoland): Should it also include platform-specific manifests? https://github.com/moby/moby/pull/49982
}

func checkPlatformDeleted(t *testing.T, imageIdx *ocispec.Index, resp []image.DeleteResponse, mfstDesc ocispec.Descriptor) {
	for _, r := range resp {
		if r.Deleted != "" {
			if assert.Check(t, is.Equal(r.Deleted, mfstDesc.Digest.String())) {
				continue
			}
			if r.Deleted == imageIdx.Manifests[0].Digest.String() {
				t.Log("Root image was deleted, expected only platform:", platforms.FormatAll(*mfstDesc.Platform))
			}
		}
	}
}

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

	const name = "test-api-images-delete"

	buildCtx := fakecontext.New(t, t.TempDir(),
		fakecontext.WithDockerfile(`FROM busybox
    ENV FOO bar`))
	defer buildCtx.Close()

	imgID := build.Do(ctx, t, apiClient, buildCtx)

	// Cleanup always runs
	defer func() {
		_, _ = apiClient.ImageRemove(ctx, imgID, client.ImageRemoveOptions{Force: true})
	}()

	_, err := apiClient.ImageTag(ctx, client.ImageTagOptions{Source: imgID, Target: name})
	assert.NilError(t, err)

	_, err = apiClient.ImageTag(ctx, client.ImageTagOptions{Source: imgID, Target: "test:tag1"})
	assert.NilError(t, err)

	_, err = apiClient.ImageRemove(ctx, imgID, client.ImageRemoveOptions{})
	assert.Check(t, is.ErrorContains(err, "unable to delete"))

	_, err = apiClient.ImageRemove(ctx, "test:noexist", client.ImageRemoveOptions{})
	assert.Check(t, is.ErrorContains(err, "No such image"))

	_, err = apiClient.ImageRemove(ctx, "test:tag1", client.ImageRemoveOptions{})
	assert.NilError(t, err)
}


Review comment (bug):
I wasn't sure if whitespace at the start of the line in the Dockerfile would work - so tried it, and it was ok. But I got this warning, also worth fixing ..

```
 1 warning found (use docker --debug to expand):
 - LegacyKeyValueFormat: "ENV key=value" should be used instead of legacy "ENV key value" format (line 2)
```

Code after:
	assert.Check(t, is.Equal(resp.Items[0].Untagged, imgName))
	assert.Check(t, is.Equal(resp.Items[1].Deleted, imageIdx.Manifests[0].Digest.String()))
	// TODO(vvoland): Should it also include platform-specific manifests? https://github.com/moby/moby/pull/49982
}

func checkPlatformDeleted(t *testing.T, imageIdx *ocispec.Index, resp []image.DeleteResponse, mfstDesc ocispec.Descriptor) {
	for _, r := range resp {
		if r.Deleted != "" {
			if assert.Check(t, is.Equal(r.Deleted, mfstDesc.Digest.String())) {
				continue
			}
			if r.Deleted == imageIdx.Manifests[0].Digest.String() {
				t.Log("Root image was deleted, expected only platform:", platforms.FormatAll(*mfstDesc.Platform))
			}
		}
	}
}

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

	const name = "test-api-images-delete"

	buildCtx := fakecontext.New(t, t.TempDir(),
		fakecontext.WithDockerfile(`FROM busybox
ENV FOO=bar`))
	defer buildCtx.Close()

	imgID := build.Do(ctx, t, apiClient, buildCtx)

	// Cleanup always runs
	defer func() {
		_, _ = apiClient.ImageRemove(ctx, imgID, client.ImageRemoveOptions{Force: true})
	}()

	_, err := apiClient.ImageTag(ctx, client.ImageTagOptions{Source: imgID, Target: name})
	assert.NilError(t, err)

	_, err = apiClient.ImageTag(ctx, client.ImageTagOptions{Source: imgID, Target: "test:tag1"})
	assert.NilError(t, err)

	_, err = apiClient.ImageRemove(ctx, imgID, client.ImageRemoveOptions{})
	assert.Check(t, is.ErrorType(err, cerrdefs.IsConflict))
	assert.Check(t, is.ErrorContains(err, "unable to delete "+stringid.TruncateID(imgID)))

	_, err = apiClient.ImageRemove(ctx, "test:noexist", client.ImageRemoveOptions{})
	assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
	assert.Check(t, is.ErrorContains(err, "No such image: test:noexist"))

	_, err = apiClient.ImageRemove(ctx, "test:tag1", client.ImageRemoveOptions{})