Skip to content

Commit 12b3acb

Browse files
committed
Add test for RESTMapper with CRD/CR
Signed-off-by: Stefan Prodan <[email protected]>
1 parent 8b6f4bc commit 12b3acb

File tree

3 files changed

+2690
-0
lines changed

3 files changed

+2690
-0
lines changed

internal/controller/kustomization_wait_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ import (
2222
"testing"
2323
"time"
2424

25+
runtimeClient "github.com/fluxcd/pkg/runtime/client"
2526
. "github.com/onsi/gomega"
2627
apierrors "k8s.io/apimachinery/pkg/api/errors"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/runtime/schema"
2830
"k8s.io/apimachinery/pkg/types"
2931
"sigs.k8s.io/controller-runtime/pkg/client"
3032
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -368,3 +370,79 @@ data: {}
368370
g.Expect(msg).
369371
To(ContainSubstring("failed to evaluate the CEL expression 'has(data.foo.bar)': no such attribute(s): data.foo.bar"))
370372
}
373+
374+
func TestKustomizationReconciler_RESTMapper(t *testing.T) {
375+
g := NewWithT(t)
376+
id := "rm-" + randStringRunes(5)
377+
resultK := &kustomizev1.Kustomization{}
378+
379+
restMapper, err := runtimeClient.NewDynamicRESTMapper(testEnv.Config)
380+
g.Expect(err).NotTo(HaveOccurred())
381+
382+
err = createNamespace(id)
383+
g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace")
384+
385+
err = createKubeConfigSecret(id)
386+
g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret")
387+
388+
artifactName := "val-" + randStringRunes(5)
389+
artifactChecksum, err := testServer.ArtifactFromDir("testdata/restmapper", artifactName)
390+
g.Expect(err).ToNot(HaveOccurred())
391+
392+
repositoryName := types.NamespacedName{
393+
Name: fmt.Sprintf("val-%s", randStringRunes(5)),
394+
Namespace: id,
395+
}
396+
397+
err = applyGitRepository(repositoryName, artifactName, "main/"+artifactChecksum)
398+
g.Expect(err).NotTo(HaveOccurred())
399+
400+
kustomization := &kustomizev1.Kustomization{}
401+
kustomization.Name = id
402+
kustomization.Namespace = id
403+
kustomization.Spec = kustomizev1.KustomizationSpec{
404+
Interval: metav1.Duration{Duration: 10 * time.Minute},
405+
Prune: true,
406+
Path: "./",
407+
Wait: true,
408+
SourceRef: kustomizev1.CrossNamespaceSourceReference{
409+
Name: repositoryName.Name,
410+
Namespace: repositoryName.Namespace,
411+
Kind: sourcev1.GitRepositoryKind,
412+
},
413+
KubeConfig: &meta.KubeConfigReference{
414+
SecretRef: meta.SecretKeyReference{
415+
Name: "kubeconfig",
416+
},
417+
},
418+
}
419+
420+
g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed())
421+
422+
g.Eventually(func() bool {
423+
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK)
424+
return isReconcileSuccess(resultK) && resultK.Status.LastAttemptedRevision == "main/"+artifactChecksum
425+
}, timeout, time.Second).Should(BeTrue())
426+
427+
t.Run("discovers newly registered CRD and preferred version", func(t *testing.T) {
428+
mapping, err := restMapper.RESTMapping(schema.GroupKind{Kind: "ClusterCleanupPolicy", Group: "kyverno.io"})
429+
g.Expect(err).NotTo(HaveOccurred())
430+
g.Expect(mapping.Resource.Version).To(Equal("v2"))
431+
})
432+
433+
t.Run("finalizes object", func(t *testing.T) {
434+
g.Expect(k8sClient.Delete(context.Background(), resultK)).To(Succeed())
435+
436+
g.Eventually(func() bool {
437+
err = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK)
438+
return apierrors.IsNotFound(err)
439+
}, timeout, time.Second).Should(BeTrue())
440+
})
441+
442+
t.Run("discovery fails for deleted CRD", func(t *testing.T) {
443+
newMapper, err := runtimeClient.NewDynamicRESTMapper(testEnv.Config)
444+
g.Expect(err).NotTo(HaveOccurred())
445+
_, err = newMapper.RESTMapping(schema.GroupKind{Kind: "ClusterCleanupPolicy", Group: "kyverno.io"})
446+
g.Expect(err).To(HaveOccurred())
447+
})
448+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: kyverno.io/v2
2+
kind: ClusterCleanupPolicy
3+
metadata:
4+
name: test-cluster-cleanup-policy
5+
spec:
6+
conditions:
7+
all:
8+
- key: '{{ time_since('''', ''{{ target.metadata.creationTimestamp }}'', '''') }}'
9+
operator: GreaterThan
10+
value: 168h
11+
match:
12+
any:
13+
- resources:
14+
annotations:
15+
openshift.io/description: review-*
16+
openshift.io/requester: system:serviceaccount:*
17+
kinds:
18+
- Namespace
19+
selector:
20+
matchLabels:
21+
test/project-name: "review"
22+
schedule: '*/5 * * * *'

0 commit comments

Comments
 (0)