1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
package image
import (
"strings"
"testing"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/docker/docker/integration/internal/container"
"github.com/docker/docker/internal/testutils/specialimage"
"github.com/docker/docker/testutil"
"github.com/docker/docker/testutil/daemon"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
)
// Regression test for: https://github.com/moby/moby/issues/45732
func TestPruneDontDeleteUsedDangling(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot start multiple daemons on windows")
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
ctx := setupTest(t)
d := daemon.New(t)
d.Start(t)
defer d.Stop(t)
client := d.NewClientT(t)
defer client.Close()
danglingID := specialimage.Load(ctx, t, client, specialimage.Dangling)
_, _, err := client.ImageInspectWithRaw(ctx, danglingID)
assert.NilError(t, err, "Test dangling image doesn't exist")
container.Create(ctx, t, client,
container.WithImage(danglingID),
container.WithCmd("sleep", "60"))
pruned, err := client.ImagesPrune(ctx, filters.NewArgs(filters.Arg("dangling", "true")))
assert.NilError(t, err)
for _, deleted := range pruned.ImagesDeleted {
if strings.Contains(deleted.Deleted, danglingID) || strings.Contains(deleted.Untagged, danglingID) {
t.Errorf("used dangling image %s shouldn't be deleted", danglingID)
}
}
_, _, err = client.ImageInspectWithRaw(ctx, danglingID)
assert.NilError(t, err, "Test dangling image should still exist")
}
func TestPruneLexographicalOrder(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot start multiple daemons on windows")
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
ctx := setupTest(t)
d := daemon.New(t)
d.Start(t)
defer d.Stop(t)
apiClient := d.NewClientT(t)
defer apiClient.Close()
d.LoadBusybox(ctx, t)
inspect, _, err := apiClient.ImageInspectWithRaw(ctx, "busybox:latest")
assert.NilError(t, err)
id := inspect.ID
var tags = []string{"h", "a", "j", "o", "s", "q", "w", "e", "r", "t"}
for _, tag := range tags {
err = apiClient.ImageTag(ctx, id, "busybox:"+tag)
assert.NilError(t, err)
}
err = apiClient.ImageTag(ctx, id, "busybox:z")
assert.NilError(t, err)
_, err = apiClient.ImageRemove(ctx, "busybox:latest", image.RemoveOptions{Force: true})
assert.NilError(t, err)
// run container
cid := container.Create(ctx, t, apiClient, container.WithImage(id))
defer container.Remove(ctx, t, apiClient, cid, containertypes.RemoveOptions{Force: true})
pruned, err := apiClient.ImagesPrune(ctx, filters.NewArgs(filters.Arg("dangling", "false")))
assert.NilError(t, err)
assert.Check(t, is.Len(pruned.ImagesDeleted, len(tags)))
for _, p := range pruned.ImagesDeleted {
assert.Check(t, is.Equal(p.Deleted, ""))
assert.Check(t, p.Untagged != "busybox:z")
}
}
// Regression test for https://github.com/moby/moby/issues/48063
func TestPruneDontDeleteUsedImage(t *testing.T) {
skip.If(t, testEnv.DaemonInfo.OSType == "windows", "cannot start multiple daemons on windows")
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
ctx := setupTest(t)
for _, env := range []struct {
name string
prepare func(t *testing.T, client *daemon.Daemon, apiClient *client.Client) error
check func(t *testing.T, apiClient *client.Client, pruned image.PruneReport)
}{
{
// Container uses the busybox:latest image and it's the only image
// tag with the same target.
name: "single tag",
check: func(t *testing.T, apiClient *client.Client, pruned image.PruneReport) {
assert.Check(t, is.Len(pruned.ImagesDeleted, 0))
_, _, err := apiClient.ImageInspectWithRaw(ctx, "busybox:latest")
assert.NilError(t, err, "Busybox image should still exist")
},
},
{
// Container uses the busybox:latest image and there's also a second
// busybox:other tag pointing to the same image.
name: "two tags",
prepare: func(t *testing.T, d *daemon.Daemon, apiClient *client.Client) error {
return apiClient.ImageTag(ctx, "busybox:latest", "busybox:a")
},
check: func(t *testing.T, apiClient *client.Client, pruned image.PruneReport) {
if assert.Check(t, is.Len(pruned.ImagesDeleted, 1)) {
assert.Check(t, is.Equal(pruned.ImagesDeleted[0].Deleted, ""))
assert.Check(t, is.Equal(pruned.ImagesDeleted[0].Untagged, "busybox:a"))
}
_, _, err := apiClient.ImageInspectWithRaw(ctx, "busybox:a")
assert.Check(t, err != nil, "Busybox:a image should be deleted")
_, _, err = apiClient.ImageInspectWithRaw(ctx, "busybox:latest")
assert.Check(t, err == nil, "Busybox:latest image should still exist")
},
},
} {
for _, tc := range []struct {
name string
imageID func(t *testing.T, inspect types.ImageInspect) string
}{
{
name: "full id",
imageID: func(t *testing.T, inspect types.ImageInspect) string {
return inspect.ID
},
},
{
name: "full id without sha256 prefix",
imageID: func(t *testing.T, inspect types.ImageInspect) string {
return strings.TrimPrefix(inspect.ID, "sha256:")
},
},
{
name: "truncated id (without sha256 prefix)",
imageID: func(t *testing.T, inspect types.ImageInspect) string {
return strings.TrimPrefix(inspect.ID, "sha256:")[:8]
},
},
{
name: "repo and digest without tag",
imageID: func(t *testing.T, inspect types.ImageInspect) string {
skip.If(t, !testEnv.UsingSnapshotter())
return "busybox@" + inspect.ID
},
},
{
name: "tagged and digested",
imageID: func(t *testing.T, inspect types.ImageInspect) string {
skip.If(t, !testEnv.UsingSnapshotter())
return "busybox:latest@" + inspect.ID
},
},
{
name: "repo digest",
imageID: func(t *testing.T, inspect types.ImageInspect) string {
// graphdriver won't have a repo digest
skip.If(t, len(inspect.RepoDigests) == 0, "no repo digest")
return inspect.RepoDigests[0]
},
},
} {
tc := tc
t.Run(env.name+"/"+tc.name, func(t *testing.T) {
ctx := testutil.StartSpan(ctx, t)
d := daemon.New(t)
d.Start(t)
defer d.Stop(t)
apiClient := d.NewClientT(t)
defer apiClient.Close()
d.LoadBusybox(ctx, t)
if env.prepare != nil {
err := env.prepare(t, d, apiClient)
assert.NilError(t, err, "prepare failed")
}
inspect, _, err := apiClient.ImageInspectWithRaw(ctx, "busybox:latest")
assert.NilError(t, err)
image := tc.imageID(t, inspect)
t.Log(image)
cid := container.Run(ctx, t, apiClient,
container.WithImage(image),
container.WithCmd("sleep", "60"))
defer container.Remove(ctx, t, apiClient, cid, containertypes.RemoveOptions{Force: true})
// dangling=false also prunes unused images
pruned, err := apiClient.ImagesPrune(ctx, filters.NewArgs(filters.Arg("dangling", "false")))
assert.NilError(t, err)
env.check(t, apiClient, pruned)
})
}
}
}
|