11package image
22
33import (
4+ "fmt"
45 "os"
56 "path"
7+ "strings"
68 "testing"
79
8- "github.com/argoproj-labs/argocd-image-updater/pkg/kube"
10+ "github.com/stretchr/testify/assert"
11+ "github.com/stretchr/testify/require"
912
13+ "github.com/argoproj-labs/argocd-image-updater/pkg/kube"
1014 "github.com/argoproj-labs/argocd-image-updater/test/fake"
1115 "github.com/argoproj-labs/argocd-image-updater/test/fixture"
12-
13- "github.com/stretchr/testify/assert"
14- "github.com/stretchr/testify/require"
1516)
1617
1718func Test_ParseCredentialAnnotation (t * testing.T ) {
@@ -101,6 +102,12 @@ func Test_ParseCredentialAnnotation(t *testing.T) {
101102 assert .Equal (t , "DUMMY_SECRET" , src .EnvName )
102103 })
103104
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+ })
104111}
105112
106113func Test_ParseCredentialReference (t * testing.T ) {
@@ -130,6 +137,53 @@ func Test_ParseCredentialReference(t *testing.T) {
130137
131138}
132139
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+
133187func Test_FetchCredentialsFromPullSecret (t * testing.T ) {
134188 t .Run ("Fetch credentials from pull secret" , func (t * testing.T ) {
135189 dockerJson := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
@@ -148,6 +202,33 @@ func Test_FetchCredentialsFromPullSecret(t *testing.T) {
148202 require .NotNil (t , creds )
149203 assert .Equal (t , "foo" , creds .Username )
150204 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 )
151232 })
152233
153234 t .Run ("Fetch credentials from pull secret with protocol stripped" , func (t * testing.T ) {
@@ -266,6 +347,18 @@ func Test_FetchCredentialsFromExt(t *testing.T) {
266347 })
267348}
268349
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+
269362func Test_ParseDockerConfig (t * testing.T ) {
270363 t .Run ("Parse valid Docker configuration with matching registry" , func (t * testing.T ) {
271364 config := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
@@ -283,6 +376,22 @@ func Test_ParseDockerConfig(t *testing.T) {
283376 assert .Equal (t , "bar" , password )
284377 })
285378
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+
286395 t .Run ("Parse valid Docker configuration with matching registry as prefix with / in the end" , func (t * testing.T ) {
287396 config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
288397 username , password , err := parseDockerConfigJson ("https://registry-1.docker.io/" , config )
0 commit comments