zotregistry.dev/zot@v1.4.4-0.20240314164342-eec277e14d20/pkg/storage/local/driver_test.go (about) 1 package local_test 2 3 import ( 4 "os" 5 "path" 6 "strings" 7 "testing" 8 9 storagedriver "github.com/docker/distribution/registry/storage/driver" 10 . "github.com/smartystreets/goconvey/convey" 11 12 storageConstants "zotregistry.dev/zot/pkg/storage/constants" 13 "zotregistry.dev/zot/pkg/storage/local" 14 ) 15 16 func TestStorageDriver(t *testing.T) { 17 driver := local.New(true) 18 19 Convey("Test DirExists", t, func() { 20 rootDir := t.TempDir() 21 22 // Folder exists 23 result := driver.DirExists(rootDir) 24 So(result, ShouldBeTrue) 25 26 // Folder name triggering ENAMETOOLONG 27 result = driver.DirExists(path.Join(rootDir, strings.Repeat("1234567890", 1000))) 28 So(result, ShouldBeFalse) 29 30 // Folder which does not exist 31 result = driver.DirExists(path.Join(rootDir, "someName")) 32 So(result, ShouldBeFalse) 33 34 // Path is actually a file 35 fileName := "testFile" 36 _, err := os.Create(path.Join(rootDir, fileName)) 37 So(err, ShouldBeNil) 38 result = driver.DirExists(path.Join(rootDir, fileName)) 39 So(result, ShouldBeFalse) 40 41 // Folder name triggering ENOTDIR a one of the parents is not a folder 42 result = driver.DirExists(path.Join(rootDir, fileName, "someName")) 43 So(result, ShouldBeFalse) 44 45 // New folder created by driver 46 repoName := "testRepo" 47 err = driver.EnsureDir(path.Join(rootDir, repoName)) 48 So(err, ShouldBeNil) 49 50 result = driver.DirExists(path.Join(rootDir, repoName)) 51 So(result, ShouldBeTrue) 52 53 // Folder without permissions 54 err = os.Chmod(path.Join(rootDir, repoName), 0o000) 55 So(err, ShouldBeNil) 56 defer os.Chmod(path.Join(rootDir, repoName), storageConstants.DefaultDirPerms) //nolint:errcheck 57 58 result = driver.DirExists(path.Join(rootDir, repoName)) 59 So(result, ShouldBeTrue) 60 }) 61 62 Convey("Test Walk", t, func() { 63 Convey("Test all folders are walked and files are identified correctly", func() { 64 rootDir := t.TempDir() 65 err := driver.EnsureDir(path.Join(rootDir, "d1", "d11")) 66 So(err, ShouldBeNil) 67 err = driver.EnsureDir(path.Join(rootDir, "d1", "d12")) 68 So(err, ShouldBeNil) 69 err = driver.EnsureDir(path.Join(rootDir, "d2")) 70 So(err, ShouldBeNil) 71 _, err = os.Create(path.Join(rootDir, "d1", "d11", "f111")) 72 So(err, ShouldBeNil) 73 _, err = os.Create(path.Join(rootDir, "d2", "f21")) 74 So(err, ShouldBeNil) 75 76 fileList := []string{} 77 folderList := []string{} 78 79 err = driver.Walk(rootDir, func(fileInfo storagedriver.FileInfo) error { 80 if fileInfo.IsDir() { 81 folderList = append(folderList, fileInfo.Path()) 82 } else { 83 fileList = append(fileList, fileInfo.Path()) 84 } 85 86 return nil 87 }) 88 So(err, ShouldBeNil) 89 90 So(len(fileList), ShouldEqual, 2) 91 So(fileList, ShouldContain, path.Join(rootDir, "d1", "d11", "f111")) 92 So(fileList, ShouldContain, path.Join(rootDir, "d2", "f21")) 93 So(len(folderList), ShouldEqual, 4) 94 So(folderList, ShouldContain, path.Join(rootDir, "d1")) 95 So(folderList, ShouldContain, path.Join(rootDir, "d1", "d11")) 96 So(folderList, ShouldContain, path.Join(rootDir, "d1", "d12")) 97 So(folderList, ShouldContain, path.Join(rootDir, "d2")) 98 }) 99 100 Convey("Test deleting folders while walking raises doesn't raise errors", func() { 101 rootDir := t.TempDir() 102 err := driver.EnsureDir(path.Join(rootDir, "d1")) 103 So(err, ShouldBeNil) 104 err = driver.EnsureDir(path.Join(rootDir, "d2")) 105 So(err, ShouldBeNil) 106 107 // List/Sort d1 and d2, delete d2 while d1 is walked 108 // While d2 is walked the PathNotFoundError should be ignored 109 err = driver.Walk(rootDir, func(fileInfo storagedriver.FileInfo) error { 110 return driver.Delete(path.Join(rootDir, "d2")) 111 }) 112 So(err, ShouldBeNil) 113 }) 114 }) 115 }