2015-04-07 22:52:48 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2017-08-11 22:31:16 +00:00
|
|
|
"context"
|
2015-04-07 22:52:48 +00:00
|
|
|
"fmt"
|
2015-12-02 00:24:31 +00:00
|
|
|
"sort"
|
2015-04-07 22:52:48 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
|
|
|
)
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
func testFS(t *testing.T) (driver.StorageDriver, map[string]string, context.Context) {
|
2015-04-07 22:52:48 +00:00
|
|
|
d := inmemory.New()
|
2015-04-27 22:58:58 +00:00
|
|
|
ctx := context.Background()
|
2015-04-07 22:52:48 +00:00
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"/a": "dir",
|
|
|
|
"/a/b": "dir",
|
|
|
|
"/a/b/c": "dir",
|
|
|
|
"/a/b/c/d": "file",
|
|
|
|
"/a/b/c/e": "file",
|
2015-12-02 00:55:10 +00:00
|
|
|
"/a/b/f": "dir",
|
|
|
|
"/a/b/f/g": "file",
|
|
|
|
"/a/b/f/h": "file",
|
|
|
|
"/a/b/f/i": "file",
|
|
|
|
"/z": "dir",
|
|
|
|
"/z/y": "file",
|
|
|
|
}
|
|
|
|
|
|
|
|
for p, typ := range expected {
|
|
|
|
if typ != "file" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := d.PutContent(ctx, p, []byte(p)); err != nil {
|
|
|
|
t.Fatalf("unable to put content into fixture: %v", err)
|
|
|
|
}
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
return d, expected, ctx
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalkErrors(t *testing.T) {
|
2015-04-27 22:58:58 +00:00
|
|
|
d, expected, ctx := testFS(t)
|
2015-04-07 22:52:48 +00:00
|
|
|
fileCount := len(expected)
|
2015-04-27 22:58:58 +00:00
|
|
|
err := Walk(ctx, d, "", func(fileInfo driver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Expected invalid root err")
|
|
|
|
}
|
|
|
|
|
2015-12-02 00:24:07 +00:00
|
|
|
errEarlyExpected := fmt.Errorf("Early termination")
|
|
|
|
|
2015-04-27 22:58:58 +00:00
|
|
|
err = Walk(ctx, d, "/", func(fileInfo driver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
// error on the 2nd file
|
|
|
|
if fileInfo.Path() == "/a/b" {
|
2015-12-02 00:24:07 +00:00
|
|
|
return errEarlyExpected
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
2015-12-02 00:55:10 +00:00
|
|
|
|
2015-04-07 22:52:48 +00:00
|
|
|
delete(expected, fileInfo.Path())
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if len(expected) != fileCount-1 {
|
|
|
|
t.Error("Walk failed to terminate with error")
|
|
|
|
}
|
2015-12-02 00:24:07 +00:00
|
|
|
if err != errEarlyExpected {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected an error due to early termination")
|
|
|
|
} else {
|
|
|
|
t.Error(err.Error())
|
|
|
|
}
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
|
2016-02-23 21:33:38 +00:00
|
|
|
err = Walk(ctx, d, "/nonexistent", func(fileInfo driver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expected missing file err")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalk(t *testing.T) {
|
2015-04-27 22:58:58 +00:00
|
|
|
d, expected, ctx := testFS(t)
|
2015-12-02 00:24:31 +00:00
|
|
|
var traversed []string
|
2015-04-27 22:58:58 +00:00
|
|
|
err := Walk(ctx, d, "/", func(fileInfo driver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
filePath := fileInfo.Path()
|
|
|
|
filetype, ok := expected[filePath]
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("Unexpected file in walk: %q", filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
if filetype != "dir" {
|
|
|
|
t.Errorf("Unexpected file type: %q", filePath)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if filetype != "file" {
|
|
|
|
t.Errorf("Unexpected file type: %q", filePath)
|
|
|
|
}
|
2015-12-02 00:55:10 +00:00
|
|
|
|
|
|
|
// each file has its own path as the contents. If the length
|
|
|
|
// doesn't match the path length, fail.
|
|
|
|
if fileInfo.Size() != int64(len(fileInfo.Path())) {
|
|
|
|
t.Fatalf("unexpected size for %q: %v != %v",
|
|
|
|
fileInfo.Path(), fileInfo.Size(), len(fileInfo.Path()))
|
|
|
|
}
|
2015-04-07 22:52:48 +00:00
|
|
|
}
|
|
|
|
delete(expected, filePath)
|
2015-12-02 00:24:31 +00:00
|
|
|
traversed = append(traversed, filePath)
|
2015-04-07 22:52:48 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if len(expected) > 0 {
|
|
|
|
t.Errorf("Missed files in walk: %q", expected)
|
|
|
|
}
|
2015-12-02 00:24:31 +00:00
|
|
|
|
|
|
|
if !sort.StringsAreSorted(traversed) {
|
|
|
|
t.Errorf("result should be sorted: %v", traversed)
|
|
|
|
}
|
|
|
|
|
2015-04-07 22:52:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWalkSkipDir(t *testing.T) {
|
2015-04-27 22:58:58 +00:00
|
|
|
d, expected, ctx := testFS(t)
|
|
|
|
err := Walk(ctx, d, "/", func(fileInfo driver.FileInfo) error {
|
2015-04-07 22:52:48 +00:00
|
|
|
filePath := fileInfo.Path()
|
|
|
|
if filePath == "/a/b" {
|
|
|
|
// skip processing /a/b/c and /a/b/c/d
|
|
|
|
return ErrSkipDir
|
|
|
|
}
|
|
|
|
delete(expected, filePath)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf(err.Error())
|
|
|
|
}
|
|
|
|
if _, ok := expected["/a/b/c"]; !ok {
|
|
|
|
t.Errorf("/a/b/c not skipped")
|
|
|
|
}
|
|
|
|
if _, ok := expected["/a/b/c/d"]; !ok {
|
|
|
|
t.Errorf("/a/b/c/d not skipped")
|
|
|
|
}
|
|
|
|
if _, ok := expected["/a/b/c/e"]; !ok {
|
|
|
|
t.Errorf("/a/b/c/e not skipped")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|