|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/docker/distribution/context" |
| 9 | + |
| 10 | + registryclient "github.com/openshift/image-registry/pkg/dockerregistry/server/client" |
| 11 | + "github.com/openshift/image-registry/pkg/testutil" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCatalog(t *testing.T) { |
| 15 | + const blobRepoCacheTTL = time.Millisecond * 500 |
| 16 | + |
| 17 | + type isMeta struct{ namespace, name string } |
| 18 | + |
| 19 | + for _, tc := range []struct { |
| 20 | + name string |
| 21 | + isObjectMeta []isMeta |
| 22 | + buffer []string |
| 23 | + last string |
| 24 | + expectedRepos []string |
| 25 | + expectedError error |
| 26 | + }{ |
| 27 | + { |
| 28 | + name: "no image streams", |
| 29 | + buffer: make([]string, 2), |
| 30 | + expectedError: io.EOF, |
| 31 | + }, |
| 32 | + |
| 33 | + { |
| 34 | + name: "one image stream", |
| 35 | + isObjectMeta: []isMeta{{"nm", "foo"}}, |
| 36 | + buffer: make([]string, 2), |
| 37 | + expectedRepos: []string{"nm/foo"}, |
| 38 | + expectedError: io.EOF, |
| 39 | + }, |
| 40 | + |
| 41 | + { |
| 42 | + name: "2 image streams in the same namespace", |
| 43 | + isObjectMeta: []isMeta{{"nm", "foo"}, {"nm", "bar"}}, |
| 44 | + buffer: make([]string, 2), |
| 45 | + expectedRepos: []string{"nm/bar", "nm/foo"}, |
| 46 | + expectedError: nil, |
| 47 | + }, |
| 48 | + |
| 49 | + { |
| 50 | + name: "3 image streams in different namespaces", |
| 51 | + isObjectMeta: []isMeta{{"fst", "is"}, {"snd", "is"}, {"trd", "name"}}, |
| 52 | + buffer: make([]string, 4), |
| 53 | + expectedRepos: []string{"fst/is", "snd/is", "trd/name"}, |
| 54 | + expectedError: io.EOF, |
| 55 | + }, |
| 56 | + |
| 57 | + { |
| 58 | + name: "repositories get sorted", |
| 59 | + isObjectMeta: []isMeta{ |
| 60 | + {"nm", "ab"}, {"nmc", "aa"}, {"ab", "cd"}, {"ss", "is"}, {"ab", "aa"}, {"a", "nn"}, |
| 61 | + }, |
| 62 | + buffer: make([]string, 7), |
| 63 | + expectedRepos: []string{"a/nn", "ab/aa", "ab/cd", "nm/ab", "nmc/aa", "ss/is"}, |
| 64 | + expectedError: io.EOF, |
| 65 | + }, |
| 66 | + |
| 67 | + { |
| 68 | + name: "short buffer", |
| 69 | + isObjectMeta: []isMeta{{"nm", "foo"}, {"nm", "bar"}}, |
| 70 | + buffer: make([]string, 1), |
| 71 | + expectedRepos: []string{"nm/bar"}, |
| 72 | + expectedError: nil, |
| 73 | + }, |
| 74 | + |
| 75 | + { |
| 76 | + name: "skip the first", |
| 77 | + isObjectMeta: []isMeta{{"nm", "foo"}, {"nm", "bar"}}, |
| 78 | + buffer: make([]string, 2), |
| 79 | + last: "nm/bar", |
| 80 | + expectedRepos: []string{"nm/foo"}, |
| 81 | + expectedError: io.EOF, |
| 82 | + }, |
| 83 | + |
| 84 | + { |
| 85 | + name: "skip the last", |
| 86 | + isObjectMeta: []isMeta{{"nm", "foo"}, {"nm", "bar"}}, |
| 87 | + buffer: make([]string, 2), |
| 88 | + last: "nm/foo", |
| 89 | + expectedRepos: []string{}, |
| 90 | + expectedError: io.EOF, |
| 91 | + }, |
| 92 | + |
| 93 | + { |
| 94 | + name: "bigger buffer capacity does not matter", |
| 95 | + isObjectMeta: []isMeta{{"nm", "foo"}, {"nm", "bar"}}, |
| 96 | + buffer: make([]string, 1, 2), |
| 97 | + expectedRepos: []string{"nm/bar"}, |
| 98 | + expectedError: nil, |
| 99 | + }, |
| 100 | + |
| 101 | + { |
| 102 | + name: "pick a repository in the middle", |
| 103 | + isObjectMeta: []isMeta{{"bar", "is"}, {"baz", "is"}, {"foo", "is"}}, |
| 104 | + buffer: make([]string, 1), |
| 105 | + last: "bar/is", |
| 106 | + expectedRepos: []string{"baz/is"}, |
| 107 | + expectedError: nil, |
| 108 | + }, |
| 109 | + } { |
| 110 | + t.Run(tc.name, func(t *testing.T) { |
| 111 | + ctx := context.Background() |
| 112 | + ctx = testutil.WithTestLogger(ctx, t) |
| 113 | + ctx = withAuthPerformed(ctx) |
| 114 | + |
| 115 | + fos, imageClient := testutil.NewFakeOpenShiftWithClient(ctx) |
| 116 | + for _, is := range tc.isObjectMeta { |
| 117 | + testutil.AddImageStream(t, fos, is.namespace, is.name, nil) |
| 118 | + } |
| 119 | + |
| 120 | + reg, err := newTestRegistry( |
| 121 | + ctx, |
| 122 | + registryclient.NewFakeRegistryAPIClient(nil, imageClient), |
| 123 | + nil, |
| 124 | + blobRepoCacheTTL, |
| 125 | + false, |
| 126 | + false) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("unexpected error: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + numFilled, err := reg.Repositories(ctx, tc.buffer, tc.last) |
| 132 | + if a, e := err, tc.expectedError; a != e { |
| 133 | + t.Errorf("got unexpected error: %q != %q", a, e) |
| 134 | + } |
| 135 | + |
| 136 | + if a, e := numFilled, len(tc.expectedRepos); a != e { |
| 137 | + t.Errorf("got unexpected number of repos: %d != %d", numFilled, e) |
| 138 | + } |
| 139 | + |
| 140 | + for i := 0; i < numFilled || i < len(tc.expectedRepos); i++ { |
| 141 | + if i < numFilled && i >= len(tc.expectedRepos) { |
| 142 | + t.Errorf("got unexpected repository at position #%d: %q", i, tc.buffer[i]) |
| 143 | + } else if i < len(tc.expectedRepos) && i >= numFilled { |
| 144 | + t.Errorf("expected repository %q not returned", tc.expectedRepos[i]) |
| 145 | + } else if a, e := tc.buffer[i], tc.expectedRepos[i]; a != e { |
| 146 | + t.Errorf("got unexpected repository at position #%d: %q != %q", i, a, e) |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
0 commit comments