@@ -10,6 +10,8 @@ import (
10
10
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/image"
11
11
"github.com/argoproj-labs/argocd-image-updater/registry-scanner/pkg/tag"
12
12
13
+ v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
+
13
15
"sigs.k8s.io/kustomize/api/types"
14
16
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
15
17
@@ -329,3 +331,132 @@ func Test_updateKustomizeFile(t *testing.T) {
329
331
})
330
332
}
331
333
}
334
+
335
+ func Test_getApplicationSource (t * testing.T ) {
336
+ t .Run ("multi-source without git repo annotation" , func (t * testing.T ) {
337
+ app := & v1alpha1.Application {
338
+ ObjectMeta : v1.ObjectMeta {
339
+ Name : "test-app" ,
340
+ },
341
+ Spec : v1alpha1.ApplicationSpec {
342
+ Sources : v1alpha1.ApplicationSources {
343
+ {
344
+ RepoURL : "https://charts.bitnami.com/bitnami" ,
345
+ TargetRevision : "18.2.3" ,
346
+ Chart : "nginx" ,
347
+ Helm : & v1alpha1.ApplicationSourceHelm {},
348
+ },
349
+ {
350
+ RepoURL : "https://github.com/chengfang/image-updater-examples.git" ,
351
+ TargetRevision : "main" ,
352
+ },
353
+ },
354
+ },
355
+ }
356
+
357
+ source := getApplicationSource (app )
358
+ assert .Equal (t , "18.2.3" , source .TargetRevision )
359
+ assert .Equal (t , "https://charts.bitnami.com/bitnami" , source .RepoURL )
360
+ })
361
+
362
+ t .Run ("single source application" , func (t * testing.T ) {
363
+ app := & v1alpha1.Application {
364
+ ObjectMeta : v1.ObjectMeta {
365
+ Name : "test-app" ,
366
+ },
367
+ Spec : v1alpha1.ApplicationSpec {
368
+ Source : & v1alpha1.ApplicationSource {
369
+ RepoURL : "https://github.com/example/repo.git" ,
370
+ TargetRevision : "main" ,
371
+ },
372
+ },
373
+ }
374
+
375
+ source := getApplicationSource (app )
376
+ assert .Equal (t , "main" , source .TargetRevision )
377
+ assert .Equal (t , "https://github.com/example/repo.git" , source .RepoURL )
378
+ })
379
+ }
380
+
381
+ func Test_getWriteBackBranch (t * testing.T ) {
382
+ t .Run ("nil application" , func (t * testing.T ) {
383
+ branch := getWriteBackBranch (nil )
384
+ assert .Equal (t , "" , branch )
385
+ })
386
+
387
+ t .Run ("matching git-repository annotation" , func (t * testing.T ) {
388
+ app := & v1alpha1.Application {
389
+ ObjectMeta : v1.ObjectMeta {
390
+ Name : "test-app" ,
391
+ Annotations : map [string ]string {
392
+ "argocd-image-updater.argoproj.io/git-repository" : "https://github.com/chengfang/image-updater-examples.git" ,
393
+ },
394
+ },
395
+ Spec : v1alpha1.ApplicationSpec {
396
+ Sources : v1alpha1.ApplicationSources {
397
+ {
398
+ RepoURL : "https://charts.bitnami.com/bitnami" ,
399
+ TargetRevision : "18.2.3" ,
400
+ Chart : "nginx" ,
401
+ },
402
+ {
403
+ RepoURL : "https://github.com/chengfang/image-updater-examples.git" ,
404
+ TargetRevision : "main" ,
405
+ },
406
+ },
407
+ },
408
+ }
409
+
410
+ branch := getWriteBackBranch (app )
411
+ assert .Equal (t , "main" , branch )
412
+ })
413
+
414
+ t .Run ("fallback to primary source when no match" , func (t * testing.T ) {
415
+ app := & v1alpha1.Application {
416
+ ObjectMeta : v1.ObjectMeta {
417
+ Name : "test-app" ,
418
+ },
419
+ Spec : v1alpha1.ApplicationSpec {
420
+ Sources : v1alpha1.ApplicationSources {
421
+ {
422
+ RepoURL : "https://charts.bitnami.com/bitnami" ,
423
+ TargetRevision : "18.2.3" ,
424
+ Chart : "nginx" ,
425
+ Helm : & v1alpha1.ApplicationSourceHelm {},
426
+ },
427
+ {
428
+ RepoURL : "https://github.com/chengfang/image-updater-examples.git" ,
429
+ TargetRevision : "main" ,
430
+ },
431
+ },
432
+ },
433
+ }
434
+
435
+ branch := getWriteBackBranch (app )
436
+ assert .Equal (t , "18.2.3" , branch )
437
+ })
438
+
439
+ t .Run ("git-repository annotation with non-matching URL" , func (t * testing.T ) {
440
+ app := & v1alpha1.Application {
441
+ ObjectMeta : v1.ObjectMeta {
442
+ Name : "test-app" ,
443
+ Annotations : map [string ]string {
444
+ "argocd-image-updater.argoproj.io/git-repository" : "https://github.com/different/repo.git" ,
445
+ },
446
+ },
447
+ Spec : v1alpha1.ApplicationSpec {
448
+ Sources : v1alpha1.ApplicationSources {
449
+ {
450
+ RepoURL : "https://charts.bitnami.com/bitnami" ,
451
+ TargetRevision : "18.2.3" ,
452
+ Chart : "nginx" ,
453
+ Helm : & v1alpha1.ApplicationSourceHelm {},
454
+ },
455
+ },
456
+ },
457
+ }
458
+
459
+ branch := getWriteBackBranch (app )
460
+ assert .Equal (t , "18.2.3" , branch )
461
+ })
462
+ }
0 commit comments