@@ -17,18 +17,88 @@ limitations under the License.
17
17
package controller
18
18
19
19
import (
20
+ "context"
21
+ "fmt"
20
22
"testing"
23
+ "time"
21
24
25
+ "github.com/fluxcd/pkg/apis/meta"
26
+ sourcev1 "github.com/fluxcd/source-controller/api/v1"
22
27
. "github.com/onsi/gomega"
23
28
corev1 "k8s.io/api/core/v1"
29
+ "k8s.io/apimachinery/pkg/api/errors"
24
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31
+ "k8s.io/apimachinery/pkg/types"
25
32
"k8s.io/client-go/tools/record"
26
33
ctrl "sigs.k8s.io/controller-runtime"
27
34
"sigs.k8s.io/controller-runtime/pkg/client"
28
35
29
36
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
30
37
)
31
38
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 : 10 * time .Minute },
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
+
32
102
func TestKustomizationReconciler_deleteBeforeFinalizer (t * testing.T ) {
33
103
g := NewWithT (t )
34
104
0 commit comments