Browse code

Merge pull request #51498 from 2003Aditya/TestAPIImagesDelete

Test api images delete

Sebastiaan van Stijn authored on 2025/11/17 07:04:06
Showing 2 changed files
... ...
@@ -36,30 +36,6 @@ func (s *DockerAPISuite) TestAPIImagesSaveAndLoad(c *testing.T) {
36 36
 	assert.Equal(c, strings.TrimSpace(inspectOut), id, "load did not work properly")
37 37
 }
38 38
 
39
-func (s *DockerAPISuite) TestAPIImagesDelete(c *testing.T) {
40
-	apiClient, err := client.New(client.FromEnv)
41
-	assert.NilError(c, err)
42
-	defer apiClient.Close()
43
-
44
-	if testEnv.DaemonInfo.OSType != "windows" {
45
-		testRequires(c, Network)
46
-	}
47
-	name := "test-api-images-delete"
48
-	cli.BuildCmd(c, name, build.WithDockerfile("FROM busybox\nENV FOO bar"))
49
-	id := getIDByName(c, name)
50
-
51
-	cli.DockerCmd(c, "tag", name, "test:tag1")
52
-
53
-	_, err = apiClient.ImageRemove(testutil.GetContext(c), id, client.ImageRemoveOptions{})
54
-	assert.ErrorContains(c, err, "unable to delete")
55
-
56
-	_, err = apiClient.ImageRemove(testutil.GetContext(c), "test:noexist", client.ImageRemoveOptions{})
57
-	assert.ErrorContains(c, err, "No such image")
58
-
59
-	_, err = apiClient.ImageRemove(testutil.GetContext(c), "test:tag1", client.ImageRemoveOptions{})
60
-	assert.NilError(c, err)
61
-}
62
-
63 39
 func (s *DockerAPISuite) TestAPIImagesImportBadSrc(c *testing.T) {
64 40
 	testRequires(c, Network, testEnv.IsLocalDaemon)
65 41
 
... ...
@@ -9,8 +9,11 @@ import (
9 9
 
10 10
 	"github.com/moby/moby/api/types/image"
11 11
 	"github.com/moby/moby/client"
12
+	"github.com/moby/moby/client/pkg/stringid"
13
+	build "github.com/moby/moby/v2/integration/internal/build"
12 14
 	"github.com/moby/moby/v2/integration/internal/container"
13 15
 	iimage "github.com/moby/moby/v2/integration/internal/image"
16
+	"github.com/moby/moby/v2/internal/testutil/fakecontext"
14 17
 	"github.com/moby/moby/v2/internal/testutil/specialimage"
15 18
 	ocispec "github.com/opencontainers/image-spec/specs-go/v1"
16 19
 	"gotest.tools/v3/assert"
... ...
@@ -176,3 +179,39 @@ func checkPlatformDeleted(t *testing.T, imageIdx *ocispec.Index, resp []image.De
176 176
 		}
177 177
 	}
178 178
 }
179
+
180
+func TestAPIImagesDelete(t *testing.T) {
181
+	ctx := setupTest(t)
182
+	apiClient := testEnv.APIClient()
183
+
184
+	const name = "test-api-images-delete"
185
+
186
+	buildCtx := fakecontext.New(t, t.TempDir(),
187
+		fakecontext.WithDockerfile(`FROM busybox
188
+ENV FOO=bar`))
189
+	defer buildCtx.Close()
190
+
191
+	imgID := build.Do(ctx, t, apiClient, buildCtx)
192
+
193
+	// Cleanup always runs
194
+	defer func() {
195
+		_, _ = apiClient.ImageRemove(ctx, imgID, client.ImageRemoveOptions{Force: true})
196
+	}()
197
+
198
+	_, err := apiClient.ImageTag(ctx, client.ImageTagOptions{Source: imgID, Target: name})
199
+	assert.NilError(t, err)
200
+
201
+	_, err = apiClient.ImageTag(ctx, client.ImageTagOptions{Source: imgID, Target: "test:tag1"})
202
+	assert.NilError(t, err)
203
+
204
+	_, err = apiClient.ImageRemove(ctx, imgID, client.ImageRemoveOptions{})
205
+	assert.Check(t, is.ErrorType(err, cerrdefs.IsConflict))
206
+	assert.Check(t, is.ErrorContains(err, "unable to delete "+stringid.TruncateID(imgID)))
207
+
208
+	_, err = apiClient.ImageRemove(ctx, "test:noexist", client.ImageRemoveOptions{})
209
+	assert.Check(t, is.ErrorType(err, cerrdefs.IsNotFound))
210
+	assert.Check(t, is.ErrorContains(err, "No such image: test:noexist"))
211
+
212
+	_, err = apiClient.ImageRemove(ctx, "test:tag1", client.ImageRemoveOptions{})
213
+	assert.NilError(t, err)
214
+}