github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/image/remove_unix_test.go (about) 1 // +build !windows 2 3 package image // import "github.com/demonoid81/moby/integration/image" 4 5 import ( 6 "context" 7 "io" 8 "io/ioutil" 9 "os" 10 "path/filepath" 11 "runtime" 12 "strconv" 13 "strings" 14 "syscall" 15 "testing" 16 "unsafe" 17 18 "github.com/demonoid81/moby/api/types" 19 _ "github.com/demonoid81/moby/daemon/graphdriver/register" // register graph drivers 20 "github.com/demonoid81/moby/daemon/images" 21 "github.com/demonoid81/moby/layer" 22 "github.com/demonoid81/moby/pkg/idtools" 23 "github.com/demonoid81/moby/testutil/daemon" 24 "github.com/demonoid81/moby/testutil/fakecontext" 25 "gotest.tools/v3/assert" 26 "gotest.tools/v3/skip" 27 ) 28 29 // This is a regression test for #38488 30 // It ensures that orphan layers can be found and cleaned up 31 // after unsuccessful image removal 32 func TestRemoveImageGarbageCollector(t *testing.T) { 33 // This test uses very platform specific way to prevent 34 // daemon for remove image layer. 35 skip.If(t, testEnv.DaemonInfo.OSType != "linux") 36 skip.If(t, os.Getenv("DOCKER_ENGINE_GOARCH") != "amd64") 37 skip.If(t, testEnv.IsRootless, "rootless mode doesn't support overlay2 on most distros") 38 39 // Create daemon with overlay2 graphdriver because vfs uses disk differently 40 // and this test case would not work with it. 41 d := daemon.New(t, daemon.WithStorageDriver("overlay2")) 42 d.Start(t) 43 defer d.Stop(t) 44 45 ctx := context.Background() 46 client := d.NewClientT(t) 47 48 layerStores := make(map[string]layer.Store) 49 layerStores[runtime.GOOS], _ = layer.NewStoreFromOptions(layer.StoreOptions{ 50 Root: d.Root, 51 MetadataStorePathTemplate: filepath.Join(d.RootDir(), "image", "%s", "layerdb"), 52 GraphDriver: d.StorageDriver(), 53 GraphDriverOptions: nil, 54 IDMapping: &idtools.IdentityMapping{}, 55 PluginGetter: nil, 56 ExperimentalEnabled: false, 57 OS: runtime.GOOS, 58 }) 59 i := images.NewImageService(images.ImageServiceConfig{ 60 LayerStores: layerStores, 61 }) 62 63 img := "test-garbage-collector" 64 65 // Build a image with multiple layers 66 dockerfile := `FROM busybox 67 RUN echo echo Running... > /run.sh` 68 source := fakecontext.New(t, "", fakecontext.WithDockerfile(dockerfile)) 69 defer source.Close() 70 resp, err := client.ImageBuild(ctx, 71 source.AsTarReader(t), 72 types.ImageBuildOptions{ 73 Remove: true, 74 ForceRemove: true, 75 Tags: []string{img}, 76 }) 77 assert.NilError(t, err) 78 _, err = io.Copy(ioutil.Discard, resp.Body) 79 resp.Body.Close() 80 assert.NilError(t, err) 81 image, _, err := client.ImageInspectWithRaw(ctx, img) 82 assert.NilError(t, err) 83 84 // Mark latest image layer to immutable 85 data := image.GraphDriver.Data 86 file, _ := os.Open(data["UpperDir"]) 87 attr := 0x00000010 88 fsflags := uintptr(0x40086602) 89 argp := uintptr(unsafe.Pointer(&attr)) 90 _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), fsflags, argp) 91 assert.Equal(t, "errno 0", errno.Error()) 92 93 // Try to remove the image, it should generate error 94 // but marking layer back to mutable before checking errors (so we don't break CI server) 95 _, err = client.ImageRemove(ctx, img, types.ImageRemoveOptions{}) 96 attr = 0x00000000 97 argp = uintptr(unsafe.Pointer(&attr)) 98 _, _, errno = syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), fsflags, argp) 99 assert.Equal(t, "errno 0", errno.Error()) 100 assert.Assert(t, err != nil) 101 errStr := err.Error() 102 if !(strings.Contains(errStr, "permission denied") || strings.Contains(errStr, "operation not permitted")) { 103 t.Errorf("ImageRemove error not an permission error %s", errStr) 104 } 105 106 // Verify that layer remaining on disk 107 dir, _ := os.Stat(data["UpperDir"]) 108 assert.Equal(t, "true", strconv.FormatBool(dir.IsDir())) 109 110 // Run imageService.Cleanup() and make sure that layer was removed from disk 111 i.Cleanup() 112 _, err = os.Stat(data["UpperDir"]) 113 assert.Assert(t, os.IsNotExist(err)) 114 115 // Make sure that removal pending layers does not exist on layerdb either 116 layerdbItems, _ := ioutil.ReadDir(filepath.Join(d.RootDir(), "/image/overlay2/layerdb/sha256")) 117 for _, folder := range layerdbItems { 118 assert.Equal(t, false, strings.HasSuffix(folder.Name(), "-removing")) 119 } 120 }