@@ -10,9 +10,15 @@ import (
1010
1111type fakeStore struct {
1212 configs map [string ]types.AuthConfig
13+ saveFn func (* fakeStore ) error
1314}
1415
1516func (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,15 +27,82 @@ func (f *fakeStore) GetAuthConfigs() map[string]types.AuthConfig {
2127}
2228
2329func (f * fakeStore ) GetFilename () string {
24- return "/tmp/docker-fakestore "
30+ return "no-config.json "
2531}
2632
27- func newStore (auths map [string ]types.AuthConfig ) store {
28- return & fakeStore {configs : auths }
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 .Equal (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 .Equal (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 .Equal (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 .Equal (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 .Equal (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 .Equal (retrievedAuth , types.AuthConfig {}))
100+ assert .Check (t , is .Equal (saveCount , expectedSaveCount ))
101+ })
29102}
30103
31104func TestFileStoreAddCredentials (t * testing.T ) {
32- f := newStore ( make ( map [string ]types.AuthConfig ))
105+ f := & fakeStore { configs : map [string ]types.AuthConfig {}}
33106
34107 s := NewFileStore (f )
35108 auth := types.AuthConfig {
@@ -47,13 +120,13 @@ func TestFileStoreAddCredentials(t *testing.T) {
47120}
48121
49122func TestFileStoreGet (t * testing.T ) {
50- f := newStore ( map [string ]types.AuthConfig {
123+ f := & fakeStore { configs : map [string ]types.AuthConfig {
51124 "https://example.com" : {
52125 Auth : "super_secret_token" ,
53126 Email : "foo@example.com" ,
54127 ServerAddress : "https://example.com" ,
55128 },
56- })
129+ }}
57130
58131 s := NewFileStore (f )
59132 a , err := s .Get ("https://example.com" )
@@ -71,7 +144,7 @@ func TestFileStoreGet(t *testing.T) {
71144func TestFileStoreGetAll (t * testing.T ) {
72145 s1 := "https://example.com"
73146 s2 := "https://example2.example.com"
74- f := newStore ( map [string ]types.AuthConfig {
147+ f := & fakeStore { configs : map [string ]types.AuthConfig {
75148 s1 : {
76149 Auth : "super_secret_token" ,
77150 Email : "foo@example.com" ,
@@ -82,7 +155,7 @@ func TestFileStoreGetAll(t *testing.T) {
82155 Email : "foo@example2.com" ,
83156 ServerAddress : "https://example2.example.com" ,
84157 },
85- })
158+ }}
86159
87160 s := NewFileStore (f )
88161 as , err := s .GetAll ()
@@ -107,13 +180,13 @@ func TestFileStoreGetAll(t *testing.T) {
107180}
108181
109182func TestFileStoreErase (t * testing.T ) {
110- f := newStore ( map [string ]types.AuthConfig {
183+ f := & fakeStore { configs : map [string ]types.AuthConfig {
111184 "https://example.com" : {
112185 Auth : "super_secret_token" ,
113186 Email : "foo@example.com" ,
114187 ServerAddress : "https://example.com" ,
115188 },
116- })
189+ }}
117190
118191 s := NewFileStore (f )
119192 err := s .Erase ("https://example.com" )
0 commit comments