1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package authn
16
17 import (
18 "testing"
19
20 "github.com/google/go-containerregistry/pkg/name"
21 )
22
23 func TestMultiKeychain(t *testing.T) {
24 one := &Basic{Username: "one", Password: "secret"}
25 two := &Basic{Username: "two", Password: "secret"}
26 three := &Basic{Username: "three", Password: "secret"}
27
28 regOne, _ := name.NewRegistry("one.gcr.io", name.StrictValidation)
29 regTwo, _ := name.NewRegistry("two.gcr.io", name.StrictValidation)
30 regThree, _ := name.NewRegistry("three.gcr.io", name.StrictValidation)
31
32 tests := []struct {
33 name string
34 reg name.Registry
35 kc Keychain
36 want Authenticator
37 }{{
38
39 name: "simple fixed test (match)",
40 reg: regOne,
41 kc: fixedKeychain{regOne: one},
42 want: one,
43 }, {
44
45 name: "simple fixed test (no match)",
46 reg: regTwo,
47 kc: fixedKeychain{regOne: one},
48 want: Anonymous,
49 }, {
50 name: "match first keychain",
51 reg: regOne,
52 kc: NewMultiKeychain(
53 fixedKeychain{regOne: one},
54 fixedKeychain{regOne: three, regTwo: two},
55 ),
56 want: one,
57 }, {
58 name: "match second keychain",
59 reg: regTwo,
60 kc: NewMultiKeychain(
61 fixedKeychain{regOne: one},
62 fixedKeychain{regOne: three, regTwo: two},
63 ),
64 want: two,
65 }, {
66 name: "match no keychain",
67 reg: regThree,
68 kc: NewMultiKeychain(
69 fixedKeychain{regOne: one},
70 fixedKeychain{regOne: three, regTwo: two},
71 ),
72 want: Anonymous,
73 }}
74
75 for _, test := range tests {
76 t.Run(test.name, func(t *testing.T) {
77 got, err := test.kc.Resolve(test.reg)
78 if err != nil {
79 t.Errorf("Resolve() = %v", err)
80 }
81 if got != test.want {
82 t.Errorf("Resolve() = %v, wanted %v", got, test.want)
83 }
84 })
85 }
86 }
87
88 type fixedKeychain map[Resource]Authenticator
89
90 var _ Keychain = (fixedKeychain)(nil)
91
92
93 func (fk fixedKeychain) Resolve(target Resource) (Authenticator, error) {
94 if auth, ok := fk[target]; ok {
95 return auth, nil
96 }
97 return Anonymous, nil
98 }
99
View as plain text