@@ -366,10 +366,11 @@ func TestKustomizationReconciler_VarsubNumberBool(t *testing.T) {
366366 manifests := func (name string ) []testserver.File {
367367 return []testserver.File {
368368 {
369- Name : "service-account .yaml" ,
369+ Name : "templates .yaml" ,
370370 Body : fmt .Sprintf (`
371- apiVersion: v1
372- kind: ServiceAccount
371+ ---
372+ apiVersion: source.toolkit.fluxcd.io/v1
373+ kind: GitRepository
373374metadata:
374375 name: %[1]s
375376 namespace: %[1]s
@@ -379,6 +380,29 @@ metadata:
379380 annotations:
380381 id: ${q}${number}${q}
381382 enabled: ${q}${boolean}${q}
383+ spec:
384+ interval: ${number}m
385+ url: https://host/repo
386+ ---
387+ apiVersion: v1
388+ kind: ConfigMap
389+ metadata:
390+ name: %[1]s
391+ namespace: %[1]s
392+ data:
393+ id: ${q}${number}${q}
394+ text: |
395+ This variable is escaped $${var}
396+
397+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus at
398+ nisl sem. Nullam nec dui ipsum. Nam vehicula volutpat ipsum, ac fringilla
399+ nisl convallis sed. Aliquam porttitor turpis finibus, finibus velit ut,
400+ imperdiet mauris. Cras nec neque nulla. Maecenas semper nulla et elit
401+ dictum sagittis. Quisque tincidunt non diam non ullamcorper. Curabitur
402+ pretium urna odio, vitae ullamcorper purus mollis sit amet. Nam ac lectus
403+ ac arcu varius feugiat id fringilla massa.
404+
405+ \?
382406` , name ),
383407 },
384408 }
@@ -423,35 +447,126 @@ metadata:
423447 "boolean" : "true" ,
424448 },
425449 },
426- Wait : true ,
450+ Wait : false ,
427451 },
428452 }
429453 g .Expect (k8sClient .Create (ctx , inputK )).Should (Succeed ())
430454
431- resultSA := & corev1.ServiceAccount {}
455+ g .Eventually (func () bool {
456+ resultK := & kustomizev1.Kustomization {}
457+ _ = k8sClient .Get (ctx , client .ObjectKeyFromObject (inputK ), resultK )
458+ for _ , c := range resultK .Status .Conditions {
459+ if c .Reason == kustomizev1 .ReconciliationSucceededReason {
460+ return true
461+ }
462+ }
463+ return false
464+ }, timeout , interval ).Should (BeTrue ())
465+
466+ resultRepo := & sourcev1.GitRepository {}
467+ g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : id , Namespace : id }, resultRepo )).Should (Succeed ())
468+ g .Expect (resultRepo .Labels ["id" ]).To (Equal ("123" ))
469+ g .Expect (resultRepo .Annotations ["id" ]).To (Equal ("123" ))
470+ g .Expect (resultRepo .Labels ["enabled" ]).To (Equal ("true" ))
471+ g .Expect (resultRepo .Annotations ["enabled" ]).To (Equal ("true" ))
472+
473+ resultCM := & corev1.ConfigMap {}
474+ g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : id , Namespace : id }, resultCM )).Should (Succeed ())
475+ g .Expect (resultCM .Data ["id" ]).To (Equal ("123" ))
476+ g .Expect (resultCM .Data ["text" ]).To (ContainSubstring (`${var}` ))
477+ g .Expect (resultCM .Data ["text" ]).ToNot (ContainSubstring (`$${var}` ))
478+ g .Expect (resultCM .Data ["text" ]).To (ContainSubstring (`\?` ))
479+ }
432480
433- ensureReconciles := func (nameSuffix string ) {
434- t .Run ("reconciles successfully" + nameSuffix , func (t * testing.T ) {
435- g .Eventually (func () bool {
436- resultK := & kustomizev1.Kustomization {}
437- _ = k8sClient .Get (ctx , client .ObjectKeyFromObject (inputK ), resultK )
438- for _ , c := range resultK .Status .Conditions {
439- if c .Reason == kustomizev1 .ReconciliationSucceededReason {
440- return true
441- }
442- }
443- return false
444- }, timeout , interval ).Should (BeTrue ())
481+ func TestKustomizationReconciler_VarsubStrict (t * testing.T ) {
482+ reconciler .StrictSubstitutions = true
483+ defer func () {
484+ reconciler .StrictSubstitutions = false
485+ }()
445486
446- g .Expect (k8sClient .Get (ctx , types.NamespacedName {Name : id , Namespace : id }, resultSA )).Should (Succeed ())
447- })
487+ ctx := context .Background ()
488+
489+ g := NewWithT (t )
490+ id := "vars-" + randStringRunes (5 )
491+ revision := "v1.0.0/" + randStringRunes (7 )
492+
493+ err := createNamespace (id )
494+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create test namespace" )
495+
496+ err = createKubeConfigSecret (id )
497+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create kubeconfig secret" )
498+
499+ manifests := func (name string ) []testserver.File {
500+ return []testserver.File {
501+ {
502+ Name : "service-account.yaml" ,
503+ Body : fmt .Sprintf (`
504+ apiVersion: v1
505+ kind: ServiceAccount
506+ metadata:
507+ name: %[1]s
508+ namespace: %[1]s
509+ labels:
510+ default: ${default:=test}
511+ missing: ${missing}
512+ ` , name ),
513+ },
514+ }
448515 }
449516
450- ensureReconciles (" with optional ConfigMap" )
451- t .Run ("replaces vars from optional ConfigMap" , func (t * testing.T ) {
452- g .Expect (resultSA .Labels ["id" ]).To (Equal ("123" ))
453- g .Expect (resultSA .Annotations ["id" ]).To (Equal ("123" ))
454- g .Expect (resultSA .Labels ["enabled" ]).To (Equal ("true" ))
455- g .Expect (resultSA .Annotations ["enabled" ]).To (Equal ("true" ))
517+ artifact , err := testServer .ArtifactFromFiles (manifests (id ))
518+ g .Expect (err ).NotTo (HaveOccurred ())
519+
520+ repositoryName := types.NamespacedName {
521+ Name : randStringRunes (5 ),
522+ Namespace : id ,
523+ }
524+
525+ err = applyGitRepository (repositoryName , artifact , revision )
526+ g .Expect (err ).NotTo (HaveOccurred ())
527+
528+ inputK := & kustomizev1.Kustomization {
529+ ObjectMeta : metav1.ObjectMeta {
530+ Name : id ,
531+ Namespace : id ,
532+ },
533+ Spec : kustomizev1.KustomizationSpec {
534+ KubeConfig : & meta.KubeConfigReference {
535+ SecretRef : meta.SecretKeyReference {
536+ Name : "kubeconfig" ,
537+ },
538+ },
539+ Interval : metav1.Duration {Duration : reconciliationInterval },
540+ Path : "./" ,
541+ Prune : true ,
542+ SourceRef : kustomizev1.CrossNamespaceSourceReference {
543+ Kind : sourcev1 .GitRepositoryKind ,
544+ Name : repositoryName .Name ,
545+ },
546+ PostBuild : & kustomizev1.PostBuild {
547+ Substitute : map [string ]string {
548+ "test" : "test" ,
549+ },
550+ },
551+ Wait : true ,
552+ },
553+ }
554+ g .Expect (k8sClient .Create (ctx , inputK )).Should (Succeed ())
555+
556+ var resultK kustomizev1.Kustomization
557+ t .Run ("fails to reconcile" , func (t * testing.T ) {
558+ g .Eventually (func () bool {
559+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (inputK ), & resultK )
560+ for _ , c := range resultK .Status .Conditions {
561+ if c .Reason == kustomizev1 .BuildFailedReason {
562+ return true
563+ }
564+ }
565+ return false
566+ }, timeout , interval ).Should (BeTrue ())
456567 })
568+
569+ ready := apimeta .FindStatusCondition (resultK .Status .Conditions , meta .ReadyCondition )
570+ g .Expect (ready .Message ).To (ContainSubstring ("variable not set" ))
571+ g .Expect (k8sClient .Delete (context .Background (), & resultK )).To (Succeed ())
457572}
0 commit comments