github.com/lusis/distribution@v2.0.1+incompatible/registry/storage/walk_test.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/docker/distribution/registry/storage/driver" 8 "github.com/docker/distribution/registry/storage/driver/inmemory" 9 ) 10 11 func testFS(t *testing.T) (driver.StorageDriver, map[string]string) { 12 d := inmemory.New() 13 c := []byte("") 14 if err := d.PutContent("/a/b/c/d", c); err != nil { 15 t.Fatalf("Unable to put to inmemory fs") 16 } 17 if err := d.PutContent("/a/b/c/e", c); err != nil { 18 t.Fatalf("Unable to put to inmemory fs") 19 } 20 21 expected := map[string]string{ 22 "/a": "dir", 23 "/a/b": "dir", 24 "/a/b/c": "dir", 25 "/a/b/c/d": "file", 26 "/a/b/c/e": "file", 27 } 28 29 return d, expected 30 } 31 32 func TestWalkErrors(t *testing.T) { 33 d, expected := testFS(t) 34 fileCount := len(expected) 35 err := Walk(d, "", func(fileInfo driver.FileInfo) error { 36 return nil 37 }) 38 if err == nil { 39 t.Error("Expected invalid root err") 40 } 41 42 err = Walk(d, "/", func(fileInfo driver.FileInfo) error { 43 // error on the 2nd file 44 if fileInfo.Path() == "/a/b" { 45 return fmt.Errorf("Early termination") 46 } 47 delete(expected, fileInfo.Path()) 48 return nil 49 }) 50 if len(expected) != fileCount-1 { 51 t.Error("Walk failed to terminate with error") 52 } 53 if err != nil { 54 t.Error(err.Error()) 55 } 56 57 err = Walk(d, "/nonexistant", func(fileInfo driver.FileInfo) error { 58 return nil 59 }) 60 if err == nil { 61 t.Errorf("Expected missing file err") 62 } 63 64 } 65 66 func TestWalk(t *testing.T) { 67 d, expected := testFS(t) 68 err := Walk(d, "/", func(fileInfo driver.FileInfo) error { 69 filePath := fileInfo.Path() 70 filetype, ok := expected[filePath] 71 if !ok { 72 t.Fatalf("Unexpected file in walk: %q", filePath) 73 } 74 75 if fileInfo.IsDir() { 76 if filetype != "dir" { 77 t.Errorf("Unexpected file type: %q", filePath) 78 } 79 } else { 80 if filetype != "file" { 81 t.Errorf("Unexpected file type: %q", filePath) 82 } 83 } 84 delete(expected, filePath) 85 return nil 86 }) 87 if len(expected) > 0 { 88 t.Errorf("Missed files in walk: %q", expected) 89 } 90 if err != nil { 91 t.Fatalf(err.Error()) 92 } 93 } 94 95 func TestWalkSkipDir(t *testing.T) { 96 d, expected := testFS(t) 97 err := Walk(d, "/", func(fileInfo driver.FileInfo) error { 98 filePath := fileInfo.Path() 99 if filePath == "/a/b" { 100 // skip processing /a/b/c and /a/b/c/d 101 return ErrSkipDir 102 } 103 delete(expected, filePath) 104 return nil 105 }) 106 if err != nil { 107 t.Fatalf(err.Error()) 108 } 109 if _, ok := expected["/a/b/c"]; !ok { 110 t.Errorf("/a/b/c not skipped") 111 } 112 if _, ok := expected["/a/b/c/d"]; !ok { 113 t.Errorf("/a/b/c/d not skipped") 114 } 115 if _, ok := expected["/a/b/c/e"]; !ok { 116 t.Errorf("/a/b/c/e not skipped") 117 } 118 119 }