github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/storage/catalog_test.go (about)

     1  package storage
     2  
     3  import (
     4  	"io"
     5  	"testing"
     6  
     7  	"github.com/docker/distribution"
     8  	"github.com/docker/distribution/context"
     9  	"github.com/docker/distribution/registry/storage/cache/memory"
    10  	"github.com/docker/distribution/registry/storage/driver"
    11  	"github.com/docker/distribution/registry/storage/driver/inmemory"
    12  )
    13  
    14  type setupEnv struct {
    15  	ctx      context.Context
    16  	driver   driver.StorageDriver
    17  	expected []string
    18  	registry distribution.Namespace
    19  }
    20  
    21  func setupFS(t *testing.T) *setupEnv {
    22  	d := inmemory.New()
    23  	c := []byte("")
    24  	ctx := context.Background()
    25  	registry, err := NewRegistry(ctx, d, BlobDescriptorCacheProvider(memory.NewInMemoryBlobDescriptorCacheProvider()), EnableRedirect)
    26  	if err != nil {
    27  		t.Fatalf("error creating registry: %v", err)
    28  	}
    29  	rootpath, _ := pathFor(repositoriesRootPathSpec{})
    30  
    31  	repos := []string{
    32  		"/foo/a/_layers/1",
    33  		"/foo/b/_layers/2",
    34  		"/bar/c/_layers/3",
    35  		"/bar/d/_layers/4",
    36  		"/foo/d/in/_layers/5",
    37  		"/an/invalid/repo",
    38  		"/bar/d/_layers/ignored/dir/6",
    39  	}
    40  
    41  	for _, repo := range repos {
    42  		if err := d.PutContent(ctx, rootpath+repo, c); err != nil {
    43  			t.Fatalf("Unable to put to inmemory fs")
    44  		}
    45  	}
    46  
    47  	expected := []string{
    48  		"bar/c",
    49  		"bar/d",
    50  		"foo/a",
    51  		"foo/b",
    52  		"foo/d/in",
    53  	}
    54  
    55  	return &setupEnv{
    56  		ctx:      ctx,
    57  		driver:   d,
    58  		expected: expected,
    59  		registry: registry,
    60  	}
    61  }
    62  
    63  func TestCatalog(t *testing.T) {
    64  	env := setupFS(t)
    65  
    66  	p := make([]string, 50)
    67  
    68  	numFilled, err := env.registry.Repositories(env.ctx, p, "")
    69  
    70  	if !testEq(p, env.expected, numFilled) {
    71  		t.Errorf("Expected catalog repos err")
    72  	}
    73  
    74  	if err != io.EOF {
    75  		t.Errorf("Catalog has more values which we aren't expecting")
    76  	}
    77  }
    78  
    79  func TestCatalogInParts(t *testing.T) {
    80  	env := setupFS(t)
    81  
    82  	chunkLen := 2
    83  	p := make([]string, chunkLen)
    84  
    85  	numFilled, err := env.registry.Repositories(env.ctx, p, "")
    86  	if err == io.EOF || numFilled != len(p) {
    87  		t.Errorf("Expected more values in catalog")
    88  	}
    89  
    90  	if !testEq(p, env.expected[0:chunkLen], numFilled) {
    91  		t.Errorf("Expected catalog first chunk err")
    92  	}
    93  
    94  	lastRepo := p[len(p)-1]
    95  	numFilled, err = env.registry.Repositories(env.ctx, p, lastRepo)
    96  
    97  	if err == io.EOF || numFilled != len(p) {
    98  		t.Errorf("Expected more values in catalog")
    99  	}
   100  
   101  	if !testEq(p, env.expected[chunkLen:chunkLen*2], numFilled) {
   102  		t.Errorf("Expected catalog second chunk err")
   103  	}
   104  
   105  	lastRepo = p[len(p)-1]
   106  	numFilled, err = env.registry.Repositories(env.ctx, p, lastRepo)
   107  
   108  	if err != io.EOF {
   109  		t.Errorf("Catalog has more values which we aren't expecting")
   110  	}
   111  
   112  	if !testEq(p, env.expected[chunkLen*2:chunkLen*3-1], numFilled) {
   113  		t.Errorf("Expected catalog third chunk err")
   114  	}
   115  
   116  }
   117  
   118  func testEq(a, b []string, size int) bool {
   119  	for cnt := 0; cnt < size-1; cnt++ {
   120  		if a[cnt] != b[cnt] {
   121  			return false
   122  		}
   123  	}
   124  	return true
   125  }