|
| 1 | +/* |
| 2 | +Copyright 2024 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/fluxcd/pkg/apis/meta" |
| 26 | + "github.com/fluxcd/pkg/testserver" |
| 27 | + sourcev1 "github.com/fluxcd/source-controller/api/v1" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 34 | + |
| 35 | + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" |
| 36 | +) |
| 37 | + |
| 38 | +func TestKustomizationReconciler_DeletionPolicyDelete(t *testing.T) { |
| 39 | + tests := []struct { |
| 40 | + name string |
| 41 | + prune bool |
| 42 | + deletionPolicy string |
| 43 | + wantDelete bool |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "should delete when deletionPolicy overrides pruning disabled", |
| 47 | + prune: false, |
| 48 | + deletionPolicy: kustomizev1.DeletionPolicyDelete, |
| 49 | + wantDelete: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "should delete when deletionPolicy mirrors prune and pruning enabled", |
| 53 | + prune: true, |
| 54 | + deletionPolicy: kustomizev1.DeletionPolicyMirrorPrune, |
| 55 | + wantDelete: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "should orphan when deletionPolicy overrides pruning enabled", |
| 59 | + prune: true, |
| 60 | + deletionPolicy: kustomizev1.DeletionPolicyOrphan, |
| 61 | + wantDelete: false, |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "should orphan when deletionPolicy mirrors prune and pruning disabled", |
| 65 | + prune: false, |
| 66 | + deletionPolicy: kustomizev1.DeletionPolicyMirrorPrune, |
| 67 | + wantDelete: false, |
| 68 | + }, |
| 69 | + } |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + g := NewWithT(t) |
| 73 | + id := "gc-" + randStringRunes(5) |
| 74 | + revision := "v1.0.0" |
| 75 | + |
| 76 | + err := createNamespace(id) |
| 77 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") |
| 78 | + |
| 79 | + err = createKubeConfigSecret(id) |
| 80 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret") |
| 81 | + |
| 82 | + manifests := func(name string, data string) []testserver.File { |
| 83 | + return []testserver.File{ |
| 84 | + { |
| 85 | + Name: "config.yaml", |
| 86 | + Body: fmt.Sprintf(`--- |
| 87 | +apiVersion: v1 |
| 88 | +kind: ConfigMap |
| 89 | +metadata: |
| 90 | + name: %[1]s |
| 91 | +data: |
| 92 | + key: "%[2]s" |
| 93 | +`, name, data), |
| 94 | + }, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + artifact, err := testServer.ArtifactFromFiles(manifests(id, id)) |
| 99 | + g.Expect(err).NotTo(HaveOccurred()) |
| 100 | + |
| 101 | + repositoryName := types.NamespacedName{ |
| 102 | + Name: fmt.Sprintf("gc-%s", randStringRunes(5)), |
| 103 | + Namespace: id, |
| 104 | + } |
| 105 | + |
| 106 | + err = applyGitRepository(repositoryName, artifact, revision) |
| 107 | + g.Expect(err).NotTo(HaveOccurred()) |
| 108 | + |
| 109 | + kustomizationKey := types.NamespacedName{ |
| 110 | + Name: fmt.Sprintf("gc-%s", randStringRunes(5)), |
| 111 | + Namespace: id, |
| 112 | + } |
| 113 | + kustomization := &kustomizev1.Kustomization{ |
| 114 | + ObjectMeta: metav1.ObjectMeta{ |
| 115 | + Name: kustomizationKey.Name, |
| 116 | + Namespace: kustomizationKey.Namespace, |
| 117 | + }, |
| 118 | + Spec: kustomizev1.KustomizationSpec{ |
| 119 | + Interval: metav1.Duration{Duration: reconciliationInterval}, |
| 120 | + Path: "./", |
| 121 | + KubeConfig: &meta.KubeConfigReference{ |
| 122 | + SecretRef: meta.SecretKeyReference{ |
| 123 | + Name: "kubeconfig", |
| 124 | + }, |
| 125 | + }, |
| 126 | + SourceRef: kustomizev1.CrossNamespaceSourceReference{ |
| 127 | + Name: repositoryName.Name, |
| 128 | + Namespace: repositoryName.Namespace, |
| 129 | + Kind: sourcev1.GitRepositoryKind, |
| 130 | + }, |
| 131 | + TargetNamespace: id, |
| 132 | + Prune: tt.prune, |
| 133 | + DeletionPolicy: tt.deletionPolicy, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) |
| 138 | + |
| 139 | + resultK := &kustomizev1.Kustomization{} |
| 140 | + resultConfig := &corev1.ConfigMap{} |
| 141 | + |
| 142 | + g.Eventually(func() bool { |
| 143 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) |
| 144 | + return resultK.Status.LastAppliedRevision == revision |
| 145 | + }, timeout, time.Second).Should(BeTrue()) |
| 146 | + |
| 147 | + g.Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: id, Namespace: id}, resultConfig)).Should(Succeed()) |
| 148 | + |
| 149 | + g.Expect(k8sClient.Delete(context.Background(), kustomization)).To(Succeed()) |
| 150 | + g.Eventually(func() bool { |
| 151 | + err = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), kustomization) |
| 152 | + return apierrors.IsNotFound(err) |
| 153 | + }, timeout, time.Second).Should(BeTrue()) |
| 154 | + |
| 155 | + if tt.wantDelete { |
| 156 | + err = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(resultConfig), resultConfig) |
| 157 | + g.Expect(apierrors.IsNotFound(err)).To(BeTrue()) |
| 158 | + } else { |
| 159 | + g.Expect(k8sClient.Get(context.Background(), client.ObjectKeyFromObject(resultConfig), resultConfig)).Should(Succeed()) |
| 160 | + } |
| 161 | + |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments