11package image
22
33import (
4+ "fmt"
45 "os"
56 "path"
7+ "strings"
68 "testing"
79
810 "github.com/argoproj-labs/argocd-image-updater/pkg/kube"
9-
1011 "github.com/argoproj-labs/argocd-image-updater/test/fake"
1112 "github.com/argoproj-labs/argocd-image-updater/test/fixture"
12-
1313 "github.com/stretchr/testify/assert"
1414 "github.com/stretchr/testify/require"
1515)
@@ -101,6 +101,12 @@ func Test_ParseCredentialAnnotation(t *testing.T) {
101101 assert .Equal (t , "DUMMY_SECRET" , src .EnvName )
102102 })
103103
104+ t .Run ("Parse external script credentials" , func (t * testing.T ) {
105+ src , err := ParseCredentialSource ("ext:/tmp/a.sh" , false )
106+ require .NoError (t , err )
107+ assert .Equal (t , CredentialSourceExt , src .Type )
108+ assert .Equal (t , "/tmp/a.sh" , src .ScriptPath )
109+ })
104110}
105111
106112func Test_ParseCredentialReference (t * testing.T ) {
@@ -130,6 +136,53 @@ func Test_ParseCredentialReference(t *testing.T) {
130136
131137}
132138
139+ func Test_FetchCredentialsFromSecret (t * testing.T ) {
140+ t .Run ("Fetch credentials from secret" , func (t * testing.T ) {
141+ secretData := make (map [string ][]byte )
142+ secretData ["username_password" ] = []byte (fmt .Sprintf ("%s:%s" , "foo" , "bar" ))
143+ secret := fixture .NewSecret ("test" , "test" , secretData )
144+ clientset := fake .NewFakeClientsetWithResources (secret )
145+ credSrc := & CredentialSource {
146+ Type : CredentialSourceSecret ,
147+ SecretNamespace : "test" ,
148+ SecretName : "test" ,
149+ SecretField : "username_password" ,
150+ }
151+ creds , err := credSrc .FetchCredentials ("NA" , & kube.KubernetesClient {Clientset : clientset })
152+ require .NoError (t , err )
153+ require .NotNil (t , creds )
154+ assert .Equal (t , "foo" , creds .Username )
155+ assert .Equal (t , "bar" , creds .Password )
156+
157+ credSrc .SecretNamespace = "test1" // test with a wrong SecretNamespace
158+ creds , err = credSrc .FetchCredentials ("NA" , & kube.KubernetesClient {Clientset : clientset })
159+ require .Error (t , err )
160+ require .Nil (t , creds )
161+ })
162+
163+ t .Run ("Fetch credentials from secret with invalid config" , func (t * testing.T ) {
164+ secretData := make (map [string ][]byte )
165+ secretData ["username_password" ] = []byte (fmt .Sprintf ("%s:%s" , "foo" , "bar" ))
166+ secret := fixture .NewSecret ("test" , "test" , secretData )
167+ clientset := fake .NewFakeClientsetWithResources (secret )
168+ credSrc := & CredentialSource {
169+ Type : CredentialSourceSecret ,
170+ SecretNamespace : "test" ,
171+ SecretName : "test" ,
172+ SecretField : "username_password" ,
173+ }
174+ creds , err := credSrc .FetchCredentials ("NA" , nil )
175+ require .Error (t , err ) // should fail with "could not fetch credentials: no Kubernetes client given"
176+ require .Nil (t , creds )
177+
178+ credSrc .SecretField = "BAD" // test with a wrong SecretField
179+ creds , err = credSrc .FetchCredentials ("NA" , & kube.KubernetesClient {Clientset : clientset })
180+ require .Error (t , err )
181+ require .Nil (t , creds )
182+
183+ })
184+ }
185+
133186func Test_FetchCredentialsFromPullSecret (t * testing.T ) {
134187 t .Run ("Fetch credentials from pull secret" , func (t * testing.T ) {
135188 dockerJson := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
@@ -148,6 +201,33 @@ func Test_FetchCredentialsFromPullSecret(t *testing.T) {
148201 require .NotNil (t , creds )
149202 assert .Equal (t , "foo" , creds .Username )
150203 assert .Equal (t , "bar" , creds .Password )
204+
205+ credSrc .SecretNamespace = "test1" // test with a wrong SecretNamespace
206+ creds , err = credSrc .FetchCredentials ("https://registry-1.docker.io" , & kube.KubernetesClient {Clientset : clientset })
207+ require .Error (t , err )
208+ require .Nil (t , creds )
209+ })
210+
211+ t .Run ("Fetch credentials from pull secret with invalid config" , func (t * testing.T ) {
212+ dockerJson := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
213+ dockerJson = strings .ReplaceAll (dockerJson , "auths" , "BAD-KEY" )
214+ secretData := make (map [string ][]byte )
215+ secretData [pullSecretField ] = []byte (dockerJson )
216+ pullSecret := fixture .NewSecret ("test" , "test" , secretData )
217+ clientset := fake .NewFakeClientsetWithResources (pullSecret )
218+ credSrc := & CredentialSource {
219+ Type : CredentialSourcePullSecret ,
220+ Registry : "https://registry-1.docker.io/v2" ,
221+ SecretNamespace : "test" ,
222+ SecretName : "test" ,
223+ }
224+ creds , err := credSrc .FetchCredentials ("https://registry-1.docker.io" , & kube.KubernetesClient {Clientset : clientset })
225+ require .Error (t , err ) // should fail with "no credentials in image pull secret"
226+ require .Nil (t , creds )
227+
228+ creds , err = credSrc .FetchCredentials ("https://registry-1.docker.io" , nil )
229+ require .Error (t , err ) // should fail with "could not fetch credentials: no Kubernetes client given"
230+ require .Nil (t , creds )
151231 })
152232
153233 t .Run ("Fetch credentials from pull secret with protocol stripped" , func (t * testing.T ) {
@@ -266,6 +346,18 @@ func Test_FetchCredentialsFromExt(t *testing.T) {
266346 })
267347}
268348
349+ func Test_FetchCredentialsFromUnknown (t * testing.T ) {
350+ t .Run ("Fetch credentials from unknown type" , func (t * testing.T ) {
351+ credSrc := & CredentialSource {
352+ Type : CredentialSourceType (- 1 ),
353+ Registry : "https://registry-1.docker.io/v2" ,
354+ }
355+ creds , err := credSrc .FetchCredentials ("https://registry-1.docker.io" , nil )
356+ require .Error (t , err ) // should fail with "unknown credential type"
357+ require .Nil (t , creds )
358+ })
359+ }
360+
269361func Test_ParseDockerConfig (t * testing.T ) {
270362 t .Run ("Parse valid Docker configuration with matching registry" , func (t * testing.T ) {
271363 config := fixture .MustReadFile ("../../test/testdata/docker/valid-config.json" )
@@ -283,6 +375,22 @@ func Test_ParseDockerConfig(t *testing.T) {
283375 assert .Equal (t , "bar" , password )
284376 })
285377
378+ t .Run ("Parse valid Docker configuration with matching http registry as prefix" , func (t * testing.T ) {
379+ config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
380+ username , password , err := parseDockerConfigJson ("http://registry-1.docker.io" , config )
381+ require .NoError (t , err )
382+ assert .Equal (t , "foo" , username )
383+ assert .Equal (t , "bar" , password )
384+ })
385+
386+ t .Run ("Parse valid Docker configuration with matching no-protocol registry as prefix" , func (t * testing.T ) {
387+ config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
388+ username , password , err := parseDockerConfigJson ("registry-1.docker.io" , config )
389+ require .NoError (t , err )
390+ assert .Equal (t , "foo" , username )
391+ assert .Equal (t , "bar" , password )
392+ })
393+
286394 t .Run ("Parse valid Docker configuration with matching registry as prefix with / in the end" , func (t * testing.T ) {
287395 config := fixture .MustReadFile ("../../test/testdata/docker/valid-config-noproto.json" )
288396 username , password , err := parseDockerConfigJson ("https://registry-1.docker.io/" , config )
0 commit comments