2021-08-23 09:14:53 -04:00
|
|
|
//go:build !windows
|
2019-05-07 21:27:15 -04:00
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package image // import "github.com/docker/docker/integration/image"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2019-09-24 14:33:03 -04:00
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-05-07 21:27:15 -04:00
|
|
|
"strconv"
|
2019-07-22 16:39:22 -04:00
|
|
|
"strings"
|
2019-05-07 21:27:15 -04:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2019-09-24 14:33:03 -04:00
|
|
|
_ "github.com/docker/docker/daemon/graphdriver/register" // register graph drivers
|
|
|
|
"github.com/docker/docker/daemon/images"
|
|
|
|
"github.com/docker/docker/layer"
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/daemon"
|
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/skip"
|
2019-05-07 21:27:15 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This is a regression test for #38488
|
|
|
|
// It ensures that orphan layers can be found and cleaned up
|
|
|
|
// after unsuccessful image removal
|
|
|
|
func TestRemoveImageGarbageCollector(t *testing.T) {
|
|
|
|
// This test uses very platform specific way to prevent
|
|
|
|
// daemon for remove image layer.
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType != "linux")
|
|
|
|
skip.If(t, os.Getenv("DOCKER_ENGINE_GOARCH") != "amd64")
|
2020-03-13 09:37:09 -04:00
|
|
|
skip.If(t, testEnv.IsRootless, "rootless mode doesn't support overlay2 on most distros")
|
2019-05-07 21:27:15 -04:00
|
|
|
|
|
|
|
// Create daemon with overlay2 graphdriver because vfs uses disk differently
|
|
|
|
// and this test case would not work with it.
|
2019-09-24 14:33:03 -04:00
|
|
|
d := daemon.New(t, daemon.WithStorageDriver("overlay2"))
|
2019-05-07 21:27:15 -04:00
|
|
|
d.Start(t)
|
|
|
|
defer d.Stop(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := d.NewClientT(t)
|
2019-09-24 14:33:03 -04:00
|
|
|
|
|
|
|
layerStores := make(map[string]layer.Store)
|
|
|
|
layerStores[runtime.GOOS], _ = layer.NewStoreFromOptions(layer.StoreOptions{
|
2020-02-11 12:53:01 -05:00
|
|
|
Root: d.Root,
|
2019-09-24 14:33:03 -04:00
|
|
|
MetadataStorePathTemplate: filepath.Join(d.RootDir(), "image", "%s", "layerdb"),
|
|
|
|
GraphDriver: d.StorageDriver(),
|
|
|
|
GraphDriverOptions: nil,
|
|
|
|
IDMapping: &idtools.IdentityMapping{},
|
|
|
|
PluginGetter: nil,
|
|
|
|
ExperimentalEnabled: false,
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
})
|
|
|
|
i := images.NewImageService(images.ImageServiceConfig{
|
|
|
|
LayerStores: layerStores,
|
|
|
|
})
|
2019-05-07 21:27:15 -04:00
|
|
|
|
|
|
|
img := "test-garbage-collector"
|
|
|
|
|
|
|
|
// Build a image with multiple layers
|
|
|
|
dockerfile := `FROM busybox
|
|
|
|
RUN echo echo Running... > /run.sh`
|
|
|
|
source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile))
|
|
|
|
defer source.Close()
|
|
|
|
resp, err := client.ImageBuild(ctx,
|
|
|
|
source.AsTarReader(t),
|
|
|
|
types.ImageBuildOptions{
|
|
|
|
Remove: true,
|
|
|
|
ForceRemove: true,
|
|
|
|
Tags: []string{img},
|
|
|
|
})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
_, err = io.Copy(ioutil.Discard, resp.Body)
|
|
|
|
resp.Body.Close()
|
|
|
|
assert.NilError(t, err)
|
|
|
|
image, _, err := client.ImageInspectWithRaw(ctx, img)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
// Mark latest image layer to immutable
|
|
|
|
data := image.GraphDriver.Data
|
|
|
|
file, _ := os.Open(data["UpperDir"])
|
|
|
|
attr := 0x00000010
|
|
|
|
fsflags := uintptr(0x40086602)
|
|
|
|
argp := uintptr(unsafe.Pointer(&attr))
|
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), fsflags, argp)
|
|
|
|
assert.Equal(t, "errno 0", errno.Error())
|
|
|
|
|
|
|
|
// Try to remove the image, it should generate error
|
|
|
|
// but marking layer back to mutable before checking errors (so we don't break CI server)
|
|
|
|
_, err = client.ImageRemove(ctx, img, types.ImageRemoveOptions{})
|
|
|
|
attr = 0x00000000
|
|
|
|
argp = uintptr(unsafe.Pointer(&attr))
|
|
|
|
_, _, errno = syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), fsflags, argp)
|
|
|
|
assert.Equal(t, "errno 0", errno.Error())
|
2019-07-22 16:39:22 -04:00
|
|
|
assert.Assert(t, err != nil)
|
|
|
|
errStr := err.Error()
|
|
|
|
if !(strings.Contains(errStr, "permission denied") || strings.Contains(errStr, "operation not permitted")) {
|
|
|
|
t.Errorf("ImageRemove error not an permission error %s", errStr)
|
|
|
|
}
|
2019-05-07 21:27:15 -04:00
|
|
|
|
|
|
|
// Verify that layer remaining on disk
|
|
|
|
dir, _ := os.Stat(data["UpperDir"])
|
|
|
|
assert.Equal(t, "true", strconv.FormatBool(dir.IsDir()))
|
|
|
|
|
|
|
|
// Run imageService.Cleanup() and make sure that layer was removed from disk
|
|
|
|
i.Cleanup()
|
2019-09-12 17:47:49 -04:00
|
|
|
_, err = os.Stat(data["UpperDir"])
|
|
|
|
assert.Assert(t, os.IsNotExist(err))
|
2019-11-28 08:13:04 -05:00
|
|
|
|
|
|
|
// Make sure that removal pending layers does not exist on layerdb either
|
|
|
|
layerdbItems, _ := ioutil.ReadDir(filepath.Join(d.RootDir(), "/image/overlay2/layerdb/sha256"))
|
|
|
|
for _, folder := range layerdbItems {
|
|
|
|
assert.Equal(t, false, strings.HasSuffix(folder.Name(), "-removing"))
|
|
|
|
}
|
2019-05-07 21:27:15 -04:00
|
|
|
}
|