2015-07-13 22:08:13 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2015-07-17 20:42:47 +02:00
|
|
|
"io"
|
2015-07-13 22:08:13 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/context"
|
|
|
|
"github.com/docker/distribution/registry/storage/cache/memory"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver"
|
|
|
|
"github.com/docker/distribution/registry/storage/driver/inmemory"
|
|
|
|
)
|
|
|
|
|
|
|
|
type setupEnv struct {
|
|
|
|
ctx context.Context
|
|
|
|
driver driver.StorageDriver
|
|
|
|
expected []string
|
|
|
|
registry distribution.Namespace
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupFS(t *testing.T) *setupEnv {
|
|
|
|
d := inmemory.New()
|
|
|
|
c := []byte("")
|
|
|
|
ctx := context.Background()
|
2015-07-29 20:12:01 +02:00
|
|
|
registry := NewRegistryWithDriver(ctx, d, memory.NewInMemoryBlobDescriptorCacheProvider(), false, true, false)
|
2015-08-18 03:51:05 +02:00
|
|
|
rootpath, _ := pathFor(repositoriesRootPathSpec{})
|
2015-07-13 22:08:13 +02:00
|
|
|
|
|
|
|
repos := []string{
|
|
|
|
"/foo/a/_layers/1",
|
|
|
|
"/foo/b/_layers/2",
|
|
|
|
"/bar/c/_layers/3",
|
|
|
|
"/bar/d/_layers/4",
|
|
|
|
"/foo/d/in/_layers/5",
|
|
|
|
"/an/invalid/repo",
|
|
|
|
"/bar/d/_layers/ignored/dir/6",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range repos {
|
|
|
|
if err := d.PutContent(ctx, rootpath+repo, c); err != nil {
|
|
|
|
t.Fatalf("Unable to put to inmemory fs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{
|
|
|
|
"bar/c",
|
|
|
|
"bar/d",
|
|
|
|
"foo/a",
|
|
|
|
"foo/b",
|
|
|
|
"foo/d/in",
|
|
|
|
}
|
|
|
|
|
|
|
|
return &setupEnv{
|
|
|
|
ctx: ctx,
|
|
|
|
driver: d,
|
|
|
|
expected: expected,
|
|
|
|
registry: registry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalog(t *testing.T) {
|
|
|
|
env := setupFS(t)
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
p := make([]string, 50)
|
|
|
|
|
|
|
|
numFilled, err := env.registry.Repositories(env.ctx, p, "")
|
2015-07-13 22:08:13 +02:00
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if !testEq(p, env.expected, numFilled) {
|
2015-07-13 22:08:13 +02:00
|
|
|
t.Errorf("Expected catalog repos err")
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if err != io.EOF {
|
2015-07-13 22:08:13 +02:00
|
|
|
t.Errorf("Catalog has more values which we aren't expecting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCatalogInParts(t *testing.T) {
|
|
|
|
env := setupFS(t)
|
|
|
|
|
|
|
|
chunkLen := 2
|
2015-07-17 20:42:47 +02:00
|
|
|
p := make([]string, chunkLen)
|
2015-07-13 22:08:13 +02:00
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
numFilled, err := env.registry.Repositories(env.ctx, p, "")
|
|
|
|
if err == io.EOF || numFilled != len(p) {
|
|
|
|
t.Errorf("Expected more values in catalog")
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if !testEq(p, env.expected[0:chunkLen], numFilled) {
|
|
|
|
t.Errorf("Expected catalog first chunk err")
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
lastRepo := p[len(p)-1]
|
|
|
|
numFilled, err = env.registry.Repositories(env.ctx, p, lastRepo)
|
2015-07-13 22:08:13 +02:00
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if err == io.EOF || numFilled != len(p) {
|
|
|
|
t.Errorf("Expected more values in catalog")
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if !testEq(p, env.expected[chunkLen:chunkLen*2], numFilled) {
|
|
|
|
t.Errorf("Expected catalog second chunk err")
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
lastRepo = p[len(p)-1]
|
|
|
|
numFilled, err = env.registry.Repositories(env.ctx, p, lastRepo)
|
2015-07-13 22:08:13 +02:00
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if err != io.EOF {
|
|
|
|
t.Errorf("Catalog has more values which we aren't expecting")
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
if !testEq(p, env.expected[chunkLen*2:chunkLen*3-1], numFilled) {
|
|
|
|
t.Errorf("Expected catalog third chunk err")
|
2015-07-13 22:08:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-17 20:42:47 +02:00
|
|
|
func testEq(a, b []string, size int) bool {
|
|
|
|
for cnt := 0; cnt < size-1; cnt++ {
|
|
|
|
if a[cnt] != b[cnt] {
|
2015-07-13 22:08:13 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|