diff --git a/internal/controller/kustomization_controller_test.go b/internal/controller/kustomization_controller_test.go index 90c03bbd..1e12bb22 100644 --- a/internal/controller/kustomization_controller_test.go +++ b/internal/controller/kustomization_controller_test.go @@ -17,11 +17,18 @@ limitations under the License. package controller import ( + "context" + "fmt" "testing" + "time" + "github.com/fluxcd/pkg/apis/meta" + sourcev1 "github.com/fluxcd/source-controller/api/v1" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/record" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -29,6 +36,69 @@ import ( kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" ) +func TestKustomizationReconciler_StagedApply(t *testing.T) { + g := NewWithT(t) + + namespaceName := "kust-" + randStringRunes(5) + namespace := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{Name: namespaceName}, + } + g.Expect(k8sClient.Create(ctx, namespace)).ToNot(HaveOccurred()) + t.Cleanup(func() { + g.Expect(k8sClient.Delete(ctx, namespace)).NotTo(HaveOccurred()) + }) + + err := createKubeConfigSecret(namespaceName) + g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret") + + artifactName := "val-" + randStringRunes(5) + artifactChecksum, err := testServer.ArtifactFromDir("testdata/crds", artifactName) + g.Expect(err).ToNot(HaveOccurred()) + + repositoryName := types.NamespacedName{ + Name: fmt.Sprintf("val-%s", randStringRunes(5)), + Namespace: namespaceName, + } + + err = applyGitRepository(repositoryName, artifactName, "main/"+artifactChecksum) + g.Expect(err).NotTo(HaveOccurred()) + + kustomization := &kustomizev1.Kustomization{} + kustomization.Name = "test-kust" + kustomization.Namespace = namespaceName + kustomization.Spec = kustomizev1.KustomizationSpec{ + Interval: metav1.Duration{Duration: interval}, + Prune: true, + Path: "./", + SourceRef: kustomizev1.CrossNamespaceSourceReference{ + Name: repositoryName.Name, + Namespace: repositoryName.Namespace, + Kind: sourcev1.GitRepositoryKind, + }, + KubeConfig: &meta.KubeConfigReference{ + SecretRef: meta.SecretKeyReference{ + Name: "kubeconfig", + }, + }, + } + + g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) + + g.Eventually(func() bool { + var obj kustomizev1.Kustomization + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &obj) + return isReconcileSuccess(&obj) && obj.Status.LastAttemptedRevision == "main/"+artifactChecksum + }, timeout, time.Second).Should(BeTrue()) + + g.Expect(k8sClient.Delete(context.Background(), kustomization)).To(Succeed()) + + g.Eventually(func() bool { + var obj kustomizev1.Kustomization + err = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &obj) + return errors.IsNotFound(err) + }, timeout, time.Second).Should(BeTrue()) +} + func TestKustomizationReconciler_deleteBeforeFinalizer(t *testing.T) { g := NewWithT(t) diff --git a/internal/controller/testdata/crds/crd.yaml b/internal/controller/testdata/crds/crd.yaml new file mode 100644 index 00000000..155c2b06 --- /dev/null +++ b/internal/controller/testdata/crds/crd.yaml @@ -0,0 +1,69 @@ +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + name: namespaces.servicebus.azure.com +spec: + group: servicebus.azure.com + names: + kind: Namespace + listKind: NamespaceList + plural: namespaces + singular: namespace + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.type + name: TYPE + type: string + name: v1api20210101preview + schema: + openAPIV3Schema: + description: Test is the Schema for the testing API + properties: + apiVersion: + type: string + kind: + type: string + metadata: + type: object + spec: + description: TestSpec defines the desired state of a test run + properties: + type: + description: Type of test + type: string + enum: + - unit + - integration + valuesFrom: + description: config reference + type: string + type: object + status: + default: + observedGeneration: -1 + properties: + observedGeneration: + description: ObservedGeneration is the last observed generation. + format: int64 + type: integer + type: object + type: object + served: true + storage: true + subresources: + status: {} +--- +apiVersion: servicebus.azure.com/v1api20210101preview +kind: Namespace +metadata: + name: aso-namespace + namespace: test-namespace +spec: + type: integration + valuesFrom: test-config +--- +apiVersion: v1 +kind: Namespace +metadata: + name: test-namespace