|
| 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 | + t.Helper() |
| 60 | + return func(args ...string) client.Program { |
| 61 | + p := &mockProgram{ |
| 62 | + mode: mode, |
| 63 | + } |
| 64 | + if len(args) > 0 { |
| 65 | + p.action = args[0] |
| 66 | + } |
| 67 | + return p |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestGetAll(t *testing.T) { |
| 72 | + dbProgram := newMockProgram(t, "db") |
| 73 | + normalProgram := newMockProgram(t, "normal") |
| 74 | + |
| 75 | + tests := []struct { |
| 76 | + name string |
| 77 | + program client.ProgramFunc |
| 78 | + wantErr bool |
| 79 | + contexts []string |
| 80 | + expected map[string]types.AuthConfig |
| 81 | + }{ |
| 82 | + {name: "db", program: dbProgram, wantErr: false, contexts: []string{"credctx"}, expected: map[string]types.AuthConfig{ |
| 83 | + "https://example///credctx": { |
| 84 | + Username: "username", |
| 85 | + ServerAddress: "https://example///credctx", |
| 86 | + }, |
| 87 | + }}, |
| 88 | + {name: "normal", program: normalProgram, wantErr: false, contexts: []string{"credctx"}, expected: map[string]types.AuthConfig{ |
| 89 | + "https://example///credctx": { |
| 90 | + Username: "username", |
| 91 | + ServerAddress: "https://example///credctx", |
| 92 | + }, |
| 93 | + "https://example///otherContext1": { |
| 94 | + Username: "username", |
| 95 | + ServerAddress: "https://example///otherContext1", |
| 96 | + }, |
| 97 | + "https://example///otherContext2": { |
| 98 | + Username: "username", |
| 99 | + ServerAddress: "https://example///otherContext2", |
| 100 | + }, |
| 101 | + }}, |
| 102 | + } |
| 103 | + |
| 104 | + for _, test := range tests { |
| 105 | + t.Run(test.name, func(t *testing.T) { |
| 106 | + store := &toolCredentialStore{ |
| 107 | + program: test.program, |
| 108 | + contexts: test.contexts, |
| 109 | + } |
| 110 | + got, err := store.GetAll() |
| 111 | + if (err != nil) != test.wantErr { |
| 112 | + t.Errorf("GetAll() error = %v, wantErr %v", err, test.wantErr) |
| 113 | + } |
| 114 | + if len(got) != len(test.expected) { |
| 115 | + t.Errorf("GetAll() got %d credentials, want %d", len(got), len(test.expected)) |
| 116 | + } |
| 117 | + for name, cred := range got { |
| 118 | + if _, ok := test.expected[name]; !ok { |
| 119 | + t.Errorf("GetAll() got unexpected credential: %s", name) |
| 120 | + } |
| 121 | + if got[name].Username != test.expected[name].Username { |
| 122 | + t.Errorf("GetAll() got unexpected username for %s", cred.ServerAddress) |
| 123 | + } |
| 124 | + if got[name].Username != test.expected[name].Username { |
| 125 | + t.Errorf("GetAll() got unexpected username for %s", name) |
| 126 | + } |
| 127 | + } |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
0 commit comments