@@ -10,9 +10,15 @@ import (
10
10
11
11
type fakeStore struct {
12
12
configs map [string ]types.AuthConfig
13
+ saveFn func (* fakeStore ) error
13
14
}
14
15
15
16
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
+ }
16
22
return nil
17
23
}
18
24
@@ -21,7 +27,78 @@ func (f *fakeStore) GetAuthConfigs() map[string]types.AuthConfig {
21
27
}
22
28
23
29
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
+ })
25
102
}
26
103
27
104
func TestFileStoreAddCredentials (t * testing.T ) {
0 commit comments