1
1
package image
2
2
3
3
import (
4
+ "fmt"
4
5
"os"
5
6
"path"
7
+ "strings"
6
8
"testing"
7
9
8
- "github.com/argoproj-labs/argocd-image-updater/pkg/kube"
10
+ "github.com/stretchr/testify/assert"
11
+ "github.com/stretchr/testify/require"
9
12
13
+ "github.com/argoproj-labs/argocd-image-updater/pkg/kube"
10
14
"github.com/argoproj-labs/argocd-image-updater/test/fake"
11
15
"github.com/argoproj-labs/argocd-image-updater/test/fixture"
12
-
13
- "github.com/stretchr/testify/assert"
14
- "github.com/stretchr/testify/require"
15
16
)
16
17
17
18
func Test_ParseCredentialAnnotation (t * testing.T ) {
@@ -101,6 +102,12 @@ func Test_ParseCredentialAnnotation(t *testing.T) {
101
102
assert .Equal (t , "DUMMY_SECRET" , src .EnvName )
102
103
})
103
104
105
+ t .Run ("Parse external script credentials" , func (t * testing.T ) {
106
+ src , err := ParseCredentialSource ("ext:/tmp/a.sh" , false )
107
+ require .NoError (t , err )
108
+ assert .Equal (t , CredentialSourceExt , src .Type )
109
+ assert .Equal (t , "/tmp/a.sh" , src .ScriptPath )
110
+ })
104
111
}
105
112
106
113
func Test_ParseCredentialReference (t * testing.T ) {
@@ -130,6 +137,53 @@ func Test_ParseCredentialReference(t *testing.T) {
130
137
131
138
}
132
139
140
+ func Test_FetchCredentialsFromSecret (t * testing.T ) {
141
+ t .Run ("Fetch credentials from secret" , func (t * testing.T ) {
142
+ secretData := make (map [string ][]byte )
143
+ secretData ["username_password" ] = []byte (fmt .Sprintf ("%s:%s" , "foo" , "bar" ))
144
+ secret := fixture .NewSecret ("test" , "test" , secretData )
145
+ clientset := fake .NewFakeClientsetWithResources (secret )
146
+ credSrc := & CredentialSource {
147
+ Type : CredentialSourceSecret ,
148
+ SecretNamespace : "test" ,
149
+ SecretName : "test" ,
150
+ SecretField : "username_password" ,
151
+ }
152
+ creds , err := credSrc .FetchCredentials ("NA" , & kube.KubernetesClient {Clientset : clientset })
153
+ require .NoError (t , err )
154
+ require .NotNil (t , creds )
155
+ assert .Equal (t , "foo" , creds .Username )
156
+ assert .Equal (t , "bar" , creds .Password )
157
+
158
+ credSrc .SecretNamespace = "test1" // test with a wrong SecretNamespace
159
+ creds , err = credSrc .FetchCredentials ("NA" , & kube.KubernetesClient {Clientset : clientset })
160
+ require .Error (t , err )
161
+ require .Nil (t , creds )
162
+ })
163
+
164
+ t .Run ("Fetch credentials from secret with invalid config" , func (t * testing.T ) {
165
+ secretData := make (map [string ][]byte )
166
+ secretData ["username_password" ] = []byte (fmt .Sprintf ("%s:%s" , "foo" , "bar" ))
167
+ secret := fixture .NewSecret ("test" , "test" , secretData )
168
+ clientset := fake .NewFakeClientsetWithResources (secret )
169
+ credSrc := & CredentialSource {
170
+ Type : CredentialSourceSecret ,
171
+ SecretNamespace : "test" ,
172
+ SecretName : "test" ,
173
+ SecretField : "username_password" ,
174
+ }
175
+ creds , err := credSrc .FetchCredentials ("NA" , nil )
176
+ require .Error (t , err ) // should fail with "could not fetch credentials: no Kubernetes client given"
177
+ require .Nil (t , creds )
178
+
179
+ credSrc .SecretField = "BAD" // test with a wrong SecretField
180
+ creds , err = credSrc .FetchCredentials ("NA" , & kube.KubernetesClient {Clientset : clientset })
181
+ require .Error (t , err )
182
+ require .Nil (t , creds )
183
+
184
+ })
185
+ }
186
+
133
187
func Test_FetchCredentialsFromPullSecret (t * testing.T ) {
134
188
t .Run ("Fetch credentials from pull secret" , func (t * testing.T ) {
135
189
dockerJson := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
@@ -148,6 +202,33 @@ func Test_FetchCredentialsFromPullSecret(t *testing.T) {
148
202
require .NotNil (t , creds )
149
203
assert .Equal (t , "foo" , creds .Username )
150
204
assert .Equal (t , "bar" , creds .Password )
205
+
206
+ credSrc .SecretNamespace = "test1" // test with a wrong SecretNamespace
207
+ creds , err = credSrc .FetchCredentials ("https://registry-1.docker.io" , & kube.KubernetesClient {Clientset : clientset })
208
+ require .Error (t , err )
209
+ require .Nil (t , creds )
210
+ })
211
+
212
+ t .Run ("Fetch credentials from pull secret with invalid config" , func (t * testing.T ) {
213
+ dockerJson := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
214
+ dockerJson = strings .ReplaceAll (dockerJson , "auths" , "BAD-KEY" )
215
+ secretData := make (map [string ][]byte )
216
+ secretData [pullSecretField ] = []byte (dockerJson )
217
+ pullSecret := fixture .NewSecret ("test" , "test" , secretData )
218
+ clientset := fake .NewFakeClientsetWithResources (pullSecret )
219
+ credSrc := & CredentialSource {
220
+ Type : CredentialSourcePullSecret ,
221
+ Registry : "https://registry-1.docker.io/v2" ,
222
+ SecretNamespace : "test" ,
223
+ SecretName : "test" ,
224
+ }
225
+ creds , err := credSrc .FetchCredentials ("https://registry-1.docker.io" , & kube.KubernetesClient {Clientset : clientset })
226
+ require .Error (t , err ) // should fail with "no credentials in image pull secret"
227
+ require .Nil (t , creds )
228
+
229
+ creds , err = credSrc .FetchCredentials ("https://registry-1.docker.io" , nil )
230
+ require .Error (t , err ) // should fail with "could not fetch credentials: no Kubernetes client given"
231
+ require .Nil (t , creds )
151
232
})
152
233
153
234
t .Run ("Fetch credentials from pull secret with protocol stripped" , func (t * testing.T ) {
@@ -266,6 +347,18 @@ func Test_FetchCredentialsFromExt(t *testing.T) {
266
347
})
267
348
}
268
349
350
+ func Test_FetchCredentialsFromUnknown (t * testing.T ) {
351
+ t .Run ("Fetch credentials from unknown type" , func (t * testing.T ) {
352
+ credSrc := & CredentialSource {
353
+ Type : CredentialSourceType (- 1 ),
354
+ Registry : "https://registry-1.docker.io/v2" ,
355
+ }
356
+ creds , err := credSrc .FetchCredentials ("https://registry-1.docker.io" , nil )
357
+ require .Error (t , err ) // should fail with "unknown credential type"
358
+ require .Nil (t , creds )
359
+ })
360
+ }
361
+
269
362
func Test_ParseDockerConfig (t * testing.T ) {
270
363
t .Run ("Parse valid Docker configuration with matching registry" , func (t * testing.T ) {
271
364
config := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
@@ -283,6 +376,22 @@ func Test_ParseDockerConfig(t *testing.T) {
283
376
assert .Equal (t , "bar" , password )
284
377
})
285
378
379
+ t .Run ("Parse valid Docker configuration with matching http registry as prefix" , func (t * testing.T ) {
380
+ config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
381
+ username , password , err := parseDockerConfigJson ("http://registry-1.docker.io" , config )
382
+ require .NoError (t , err )
383
+ assert .Equal (t , "foo" , username )
384
+ assert .Equal (t , "bar" , password )
385
+ })
386
+
387
+ t .Run ("Parse valid Docker configuration with matching no-protocol registry as prefix" , func (t * testing.T ) {
388
+ config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
389
+ username , password , err := parseDockerConfigJson ("registry-1.docker.io" , config )
390
+ require .NoError (t , err )
391
+ assert .Equal (t , "foo" , username )
392
+ assert .Equal (t , "bar" , password )
393
+ })
394
+
286
395
t .Run ("Parse valid Docker configuration with matching registry as prefix with / in the end" , func (t * testing.T ) {
287
396
config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
288
397
username , password , err := parseDockerConfigJson ("https://registry-1.docker.io/" , config )
0 commit comments