Skip to content

Commit dd18d3c

Browse files
committed
add unit test
Signed-off-by: Grant Linville <[email protected]>
1 parent 95945ea commit dd18d3c

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

pkg/credentials/toolstore_test.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package credentials
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"testing"
8+
9+
"github.com/docker/cli/cli/config/types"
10+
"github.com/docker/docker-credential-helpers/client"
11+
"github.com/docker/docker-credential-helpers/credentials"
12+
)
13+
14+
type mockProgram struct {
15+
// mode is either "db" or "normal"
16+
// db mode will honor contexts, normal mode will not
17+
mode string
18+
action string
19+
contexts []string
20+
}
21+
22+
func (m *mockProgram) Input(in io.Reader) {
23+
switch m.action {
24+
case credentials.ActionList:
25+
var contexts []string
26+
if err := json.NewDecoder(in).Decode(&contexts); err == nil && len(contexts) > 0 {
27+
m.contexts = contexts
28+
}
29+
}
30+
// TODO: add other cases here as needed
31+
}
32+
33+
func (m *mockProgram) Output() ([]byte, error) {
34+
switch m.action {
35+
case credentials.ActionList:
36+
switch m.mode {
37+
case "db":
38+
// Return only credentials that are in the list of contexts.
39+
creds := make(map[string]string)
40+
for _, context := range m.contexts {
41+
creds[fmt.Sprintf("https://example///%s", context)] = "username"
42+
}
43+
return json.Marshal(creds)
44+
case "normal":
45+
// Return credentials in the list of contexts, plus some made up extras.
46+
creds := make(map[string]string)
47+
for _, context := range m.contexts {
48+
creds[fmt.Sprintf("https://example///%s", context)] = "username"
49+
}
50+
creds[fmt.Sprintf("https://example///%s", "otherContext1")] = "username"
51+
creds[fmt.Sprintf("https://example///%s", "otherContext2")] = "username"
52+
return json.Marshal(creds)
53+
}
54+
}
55+
return nil, nil
56+
}
57+
58+
func NewMockProgram(t *testing.T, mode string) client.ProgramFunc {
59+
return func(args ...string) client.Program {
60+
p := &mockProgram{
61+
mode: mode,
62+
}
63+
if len(args) > 0 {
64+
p.action = args[0]
65+
}
66+
return p
67+
}
68+
}
69+
70+
func TestGetAll(t *testing.T) {
71+
dbProgram := NewMockProgram(t, "db")
72+
normalProgram := NewMockProgram(t, "normal")
73+
74+
tests := []struct {
75+
name string
76+
program client.ProgramFunc
77+
wantErr bool
78+
contexts []string
79+
expected map[string]types.AuthConfig
80+
}{
81+
{name: "db", program: dbProgram, wantErr: false, contexts: []string{"credctx"}, expected: map[string]types.AuthConfig{
82+
"https://example///credctx": {
83+
Username: "username",
84+
ServerAddress: "https://example///credctx",
85+
},
86+
}},
87+
{name: "normal", program: normalProgram, wantErr: false, contexts: []string{"credctx"}, expected: map[string]types.AuthConfig{
88+
"https://example///credctx": {
89+
Username: "username",
90+
ServerAddress: "https://example///credctx",
91+
},
92+
"https://example///otherContext1": {
93+
Username: "username",
94+
ServerAddress: "https://example///otherContext1",
95+
},
96+
"https://example///otherContext2": {
97+
Username: "username",
98+
ServerAddress: "https://example///otherContext2",
99+
},
100+
}},
101+
}
102+
103+
for _, test := range tests {
104+
t.Run(test.name, func(t *testing.T) {
105+
store := &toolCredentialStore{
106+
program: test.program,
107+
contexts: test.contexts,
108+
}
109+
got, err := store.GetAll()
110+
if (err != nil) != test.wantErr {
111+
t.Errorf("GetAll() error = %v, wantErr %v", err, test.wantErr)
112+
}
113+
if len(got) != len(test.expected) {
114+
t.Errorf("GetAll() got %d credentials, want %d", len(got), len(test.expected))
115+
}
116+
for name, cred := range got {
117+
if _, ok := test.expected[name]; !ok {
118+
t.Errorf("GetAll() got unexpected credential: %s", name)
119+
}
120+
if got[name].Username != test.expected[name].Username {
121+
t.Errorf("GetAll() got unexpected username for %s", cred.ServerAddress)
122+
}
123+
if got[name].Username != test.expected[name].Username {
124+
t.Errorf("GetAll() got unexpected username for %s", name)
125+
}
126+
}
127+
})
128+
}
129+
}

0 commit comments

Comments
 (0)