github.com/containerd/containerd@v22.0.0-20200918172823-438c87b8e050+incompatible/remotes/docker/scope_test.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package docker 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/containerd/containerd/reference" 24 "gotest.tools/v3/assert" 25 "gotest.tools/v3/assert/cmp" 26 ) 27 28 func TestRepositoryScope(t *testing.T) { 29 testCases := []struct { 30 refspec reference.Spec 31 push bool 32 expected string 33 }{ 34 { 35 refspec: reference.Spec{ 36 Locator: "host/foo/bar", 37 Object: "ignored", 38 }, 39 push: false, 40 expected: "repository:foo/bar:pull", 41 }, 42 { 43 refspec: reference.Spec{ 44 Locator: "host:4242/foo/bar", 45 Object: "ignored", 46 }, 47 push: true, 48 expected: "repository:foo/bar:pull,push", 49 }, 50 } 51 for _, x := range testCases { 52 t.Run(x.refspec.String(), func(t *testing.T) { 53 actual, err := repositoryScope(x.refspec, x.push) 54 assert.NilError(t, err) 55 assert.Equal(t, x.expected, actual) 56 }) 57 } 58 } 59 60 func TestGetTokenScopes(t *testing.T) { 61 testCases := []struct { 62 scopesInCtx []string 63 commonScopes []string 64 expected []string 65 }{ 66 { 67 scopesInCtx: []string{}, 68 commonScopes: []string{"repository:foo/bar:pull"}, 69 expected: []string{"repository:foo/bar:pull"}, 70 }, 71 { 72 scopesInCtx: []string{"repository:foo/bar:pull,push"}, 73 commonScopes: []string{}, 74 expected: []string{"repository:foo/bar:pull,push"}, 75 }, 76 { 77 scopesInCtx: []string{"repository:foo/bar:pull"}, 78 commonScopes: []string{"repository:foo/bar:pull"}, 79 expected: []string{"repository:foo/bar:pull"}, 80 }, 81 { 82 scopesInCtx: []string{"repository:foo/bar:pull"}, 83 commonScopes: []string{"repository:foo/bar:pull,push"}, 84 expected: []string{"repository:foo/bar:pull", "repository:foo/bar:pull,push"}, 85 }, 86 { 87 scopesInCtx: []string{"repository:foo/bar:pull"}, 88 commonScopes: []string{"repository:foo/bar:pull,push", "repository:foo/bar:pull"}, 89 expected: []string{"repository:foo/bar:pull", "repository:foo/bar:pull,push"}, 90 }, 91 } 92 for _, tc := range testCases { 93 ctx := context.WithValue(context.TODO(), tokenScopesKey{}, tc.scopesInCtx) 94 actual := GetTokenScopes(ctx, tc.commonScopes) 95 assert.DeepEqual(t, tc.expected, actual) 96 } 97 } 98 99 func TestCustomScope(t *testing.T) { 100 scope := "whatever:foo/bar:pull" 101 ctx := WithScope(context.Background(), scope) 102 ctx = contextWithAppendPullRepositoryScope(ctx, "foo/bar") 103 104 scopes := GetTokenScopes(ctx, []string{}) 105 assert.Assert(t, cmp.Len(scopes, 2)) 106 assert.Check(t, cmp.Equal(scopes[0], "repository:foo/bar:pull")) 107 assert.Check(t, cmp.Equal(scopes[1], scope)) 108 }