8000 cli/config/credentials: add test for save being idempotent · docker/cli@3f64900 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3f64900

Browse files
committed
cli/config/credentials: add test for save being idempotent
Test case for d3f6867 Signed-off-by: Sebastiaan van Stijn <github@gone.nl> (cherry picked from commit 47a5066) Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 720da3c commit 3f64900

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

cli/config/credentials/file_store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func NewFileStore(file store) Store {
2525
return &fileStore{file: file}
2626
}
2727

28-
// Erase removes the given credentials from the file store.
28+
// Erase removes the given credentials from the file store.This function is
29+
// idempotent and does not update the file if credentials did not change.
2930
func (c *fileStore) Erase(serverAddress string) error {
3031
if _, exists := c.file.GetAuthConfigs()[serverAddress]; !exists {
3132
// nothing to do; no credentials found for the given serverAddress

cli/config/credentials/file_store_test.go

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ import (
1010

1111
type fakeStore struct {
1212
configs map[string]types.AuthConfig
13+
saveFn func(*fakeStore) error
1314
}
1415

1516
func (f *fakeStore) Save() error {
17+
if f.saveFn != nil {
18+
// Pass a reference to the fakeStore itself in case saveFn
19+
// wants to access it.
20+
return f.saveFn(f)
21+
}
1622
return nil
1723
}
1824

@@ -21,7 +27,78 @@ func (f *fakeStore) GetAuthConfigs() map[string]types.AuthConfig {
2127
}
2228

2329
func (f *fakeStore) GetFilename() string {
24-
return "/tmp/docker-fakestore"
30+
return "no-config.json"
31+
}
32+
33+
// TestFileStoreIdempotent verifies that the config-file isn't updated
34+
// if nothing changed.
35+
func TestFileStoreIdempotent(t *testing.T) {
36+
var saveCount, expectedSaveCount int
37+
38+
s := NewFileStore(&fakeStore{
39+
configs: map[string]types.AuthConfig{},
40+
saveFn: func(*fakeStore) error {
41+
saveCount++
42+
return nil
43+
},
44+
})
45+
authOne := types.AuthConfig{
46+
Auth: "super_secret_token",
47+
Email: "foo@example.com",
48+
ServerAddress: "https://example.com",
49+
}
50+
authTwo := types.AuthConfig{
51+
Auth: "also_super_secret_token",
52+
Email: "bar@example.com",
53+
ServerAddress: "https://other.example.com",
54+
}
55+
56+
expectedSaveCount = 1
57+
t.Run("store new credentials", func(t *testing.T) {
58+
assert.NilError(t, s.Store(authOne))
59+
retrievedAuth, err := s.Get(authOne.ServerAddress)
60+
assert.NilError(t, err)
61+
assert.Check(t, is.DeepEqual(retrievedAuth, authOne))
62+
assert.Check(t, is.Equal(saveCount, expectedSaveCount))
63+
})
64+
t.Run("store same credentials is a no-op", func(t *testing.T) {
65+
assert.NilError(t, s.Store(authOne))
66+
retrievedAuth, err := s.Get(authOne.ServerAddress)
67+
assert.NilError(t, err)
68+
assert.Check(t, is.DeepEqual(retrievedAuth, authOne))
69+
assert.Check(t, is.Equal(saveCount, expectedSaveCount), "should not have saved if nothing changed")
70+
})
71+
t.Run("store other credentials", func(t *testing.T) {
72+
expectedSaveCount++
73+
assert.NilError(t, s.Store(authTwo))
74+
retrievedAuth, err := s.Get(authTwo.ServerAddress)
75+
assert.NilError(t, err)
76+
assert.Check(t, is.DeepEqual(retrievedAuth, authTwo))
77+
assert.Check(t, is.Equal(saveCount, expectedSaveCount))
78+
})
79+
t.Run("erase credentials", func(t *testing.T) {
80+
expectedSaveCount++
81+
assert.NilError(t, s.Erase(authOne.ServerAddress))
82+
retrievedAuth, err := s.Get(authOne.ServerAddress)
83+
assert.NilError(t, err)
84+
assert.Check(t, is.DeepEqual(retrievedAuth, types.AuthConfig{}))
85+
assert.Check(t, is.Equal(saveCount, expectedSaveCount))
86+
})
87+
t.Run("erase non-existing credentials is a no-op", func(t *testing.T) {
88+
assert.NilError(t, s.Erase(authOne.ServerAddress))
89+
retrievedAuth, err := s.Get(authOne.ServerAddress)
90+
assert.NilError(t, err)
91+
assert.Check(t, is.DeepEqual(retrievedAuth, types.AuthConfig{}))
92+
assert.Check(t, is.Equal(saveCount, expectedSaveCount), "should not have saved if nothing changed")
93+
})
94+
t.Run("erase other credentials", func(t *testing.T) {
95+
expectedSaveCount++
96+
assert.NilError(t, s.Erase(authTwo.ServerAddress))
97+
retrievedAuth, err := s.Get(authTwo.ServerAddress)
98+
assert.NilError(t, err)
99+
assert.Check(t, is.DeepEqual(retrievedAuth, types.AuthConfig{}))
100+
assert.Check(t, is.Equal(saveCount, expectedSaveCount))
101+
})
25102
}
26103

27104
func TestFileStoreAddCredentials(t *testing.T) {

0 commit comments

Comments
 (0)
0