@@ -17,18 +17,88 @@ limitations under the License.
1717package controller
1818
1919import (
20+ "context"
21+ "fmt"
2022 "testing"
23+ "time"
2124
25+ "github.com/fluxcd/pkg/apis/meta"
26+ sourcev1 "github.com/fluxcd/source-controller/api/v1"
2227 . "github.com/onsi/gomega"
2328 corev1 "k8s.io/api/core/v1"
29+ "k8s.io/apimachinery/pkg/api/errors"
2430 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+ "k8s.io/apimachinery/pkg/types"
2532 "k8s.io/client-go/tools/record"
2633 ctrl "sigs.k8s.io/controller-runtime"
2734 "sigs.k8s.io/controller-runtime/pkg/client"
2835
2936 kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
3037)
3138
39+ func TestKustomizationReconciler_StagedApply (t * testing.T ) {
40+ g := NewWithT (t )
41+
42+ namespaceName := "kust-" + randStringRunes (5 )
43+ namespace := & corev1.Namespace {
44+ ObjectMeta : metav1.ObjectMeta {Name : namespaceName },
45+ }
46+ g .Expect (k8sClient .Create (ctx , namespace )).ToNot (HaveOccurred ())
47+ t .Cleanup (func () {
48+ g .Expect (k8sClient .Delete (ctx , namespace )).NotTo (HaveOccurred ())
49+ })
50+
51+ err := createKubeConfigSecret (namespaceName )
52+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create kubeconfig secret" )
53+
54+ artifactName := "val-" + randStringRunes (5 )
55+ artifactChecksum , err := testServer .ArtifactFromDir ("testdata/crds" , artifactName )
56+ g .Expect (err ).ToNot (HaveOccurred ())
57+
58+ repositoryName := types.NamespacedName {
59+ Name : fmt .Sprintf ("val-%s" , randStringRunes (5 )),
60+ Namespace : namespaceName ,
61+ }
62+
63+ err = applyGitRepository (repositoryName , artifactName , "main/" + artifactChecksum )
64+ g .Expect (err ).NotTo (HaveOccurred ())
65+
66+ kustomization := & kustomizev1.Kustomization {}
67+ kustomization .Name = "test-kust"
68+ kustomization .Namespace = namespaceName
69+ kustomization .Spec = kustomizev1.KustomizationSpec {
70+ Interval : metav1.Duration {Duration : interval },
71+ Prune : true ,
72+ Path : "./" ,
73+ SourceRef : kustomizev1.CrossNamespaceSourceReference {
74+ Name : repositoryName .Name ,
75+ Namespace : repositoryName .Namespace ,
76+ Kind : sourcev1 .GitRepositoryKind ,
77+ },
78+ KubeConfig : & meta.KubeConfigReference {
79+ SecretRef : meta.SecretKeyReference {
80+ Name : "kubeconfig" ,
81+ },
82+ },
83+ }
84+
85+ g .Expect (k8sClient .Create (context .Background (), kustomization )).To (Succeed ())
86+
87+ g .Eventually (func () bool {
88+ var obj kustomizev1.Kustomization
89+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), & obj )
90+ return isReconcileSuccess (& obj ) && obj .Status .LastAttemptedRevision == "main/" + artifactChecksum
91+ }, timeout , time .Second ).Should (BeTrue ())
92+
93+ g .Expect (k8sClient .Delete (context .Background (), kustomization )).To (Succeed ())
94+
95+ g .Eventually (func () bool {
96+ var obj kustomizev1.Kustomization
97+ err = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), & obj )
98+ return errors .IsNotFound (err )
99+ }, timeout , time .Second ).Should (BeTrue ())
100+ }
101+
32102func TestKustomizationReconciler_deleteBeforeFinalizer (t * testing.T ) {
33103 g := NewWithT (t )
34104
0 commit comments