|
1 | | -package credentials |
| 1 | +package memory |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
5 | 4 | "testing" |
6 | 5 |
|
7 | 6 | "github.com/docker/cli/cli/config/types" |
8 | 7 | "gotest.tools/v3/assert" |
9 | 8 | ) |
10 | 9 |
|
11 | 10 | func TestEnvStore(t *testing.T) { |
12 | | - envConfig := map[string]types.AuthConfig{ |
| 11 | + config := map[string]types.AuthConfig{ |
13 | 12 | "https://example.com": { |
14 | 13 | Email: "something-something", |
15 | 14 | ServerAddress: "https://example.com", |
16 | 15 | Auth: "super_secret_token", |
17 | 16 | }, |
18 | 17 | } |
19 | | - d, err := json.Marshal(map[string]map[string]types.AuthConfig{ |
20 | | - "auth": envConfig, |
21 | | - }) |
22 | | - assert.NilError(t, err) |
23 | | - |
24 | | - t.Setenv("DOCKER_AUTH_CONFIG", string(d)) |
25 | 18 |
|
26 | | - fileConfig := map[string]types.AuthConfig{ |
| 19 | + fallbackConfig := map[string]types.AuthConfig{ |
27 | 20 | "https://only-in-file.com": { |
28 | 21 | Email: "something-something", |
29 | 22 | ServerAddress: "https://only-in-file.com", |
30 | 23 | Auth: "super_secret_token", |
31 | 24 | }, |
32 | 25 | } |
33 | 26 |
|
34 | | - var saveCount int |
35 | | - fileStore := NewFileStore(&fakeStore{ |
36 | | - configs: fileConfig, |
37 | | - saveFn: func(*fakeStore) error { |
38 | | - saveCount++ |
39 | | - return nil |
40 | | - }, |
41 | | - }) |
| 27 | + fallbackStore := NewInMemoryStore(fallbackConfig) |
42 | 28 |
|
43 | | - envStore := NewEnvStore(fileStore) |
| 29 | + memoryStore := NewInMemoryStore(config, WithFallbackStore(fallbackStore)) |
44 | 30 |
|
45 | 31 | t.Run("case=get credentials from env", func(t *testing.T) { |
46 | | - c, err := envStore.Get("https://example.com") |
| 32 | + c, err := memoryStore.Get("https://example.com") |
47 | 33 | assert.NilError(t, err) |
48 | | - assert.Equal(t, c, envConfig["https://example.com"]) |
| 34 | + assert.Equal(t, c, config["https://example.com"]) |
49 | 35 | }) |
50 | 36 |
|
51 | 37 | t.Run("case=get credentials from file", func(t *testing.T) { |
52 | | - c, err := envStore.Get("https://only-in-file.com") |
| 38 | + c, err := memoryStore.Get("https://only-in-file.com") |
53 | 39 | assert.NilError(t, err) |
54 | | - assert.Equal(t, c, fileConfig["https://only-in-file.com"]) |
| 40 | + assert.Equal(t, c, fallbackConfig["https://only-in-file.com"]) |
55 | 41 | }) |
56 | 42 |
|
57 | 43 | t.Run("case=storing credentials should not update env", func(t *testing.T) { |
58 | | - err := envStore.Store(types.AuthConfig{ |
| 44 | + err := memoryStore.Store(types.AuthConfig{ |
59 | 45 | Email: "not-in-env", |
60 | 46 | ServerAddress: "https://not-in-env", |
61 | 47 | Auth: "not-in-env", |
62 | 48 | }) |
63 | 49 | assert.NilError(t, err) |
64 | | - assert.Equal(t, saveCount, 1) |
65 | 50 | }) |
66 | 51 |
|
67 | 52 | t.Run("case=delete credentials should not update env", func(t *testing.T) { |
68 | | - err := envStore.Erase("https://example.com") |
69 | | - assert.NilError(t, err) |
70 | | - c, err := envStore.Get("https://example.com") |
| 53 | + err := memoryStore.Erase("https://example.com") |
71 | 54 | assert.NilError(t, err) |
72 | | - assert.Equal(t, c, envConfig["https://example.com"]) |
| 55 | + _, err = memoryStore.Get("https://example.com") |
| 56 | + assert.Equal(t, IsErrValueNotFound(err), true) |
73 | 57 | }) |
74 | 58 | } |
0 commit comments