From 15a63c1a1bbcc4d1e58997640a8d796be120f8c9 Mon Sep 17 00:00:00 2001 From: Erik Godding Boye Date: Fri, 2 Feb 2024 23:41:16 +0100 Subject: [PATCH] add test for reconcile consistency --- controllers/namespace_controller_test.go | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/controllers/namespace_controller_test.go b/controllers/namespace_controller_test.go index 15337c6..fa7dc2f 100644 --- a/controllers/namespace_controller_test.go +++ b/controllers/namespace_controller_test.go @@ -476,4 +476,60 @@ var _ = Describe("Namespace controller", func() { HaveField("Annotations", HaveKeyWithValue("memo", "tama")), )) }) + + Context("templated namespace", func() { + var ns1 *corev1.Namespace + + BeforeEach(func() { + tmpl := &corev1.Namespace{} + tmpl.GenerateName = "tmpl" + tmpl.Labels = map[string]string{ + constants.LabelType: constants.NSTypeTemplate, + "team": "label", + } + tmpl.Annotations = map[string]string{"memo": "annot"} + Expect(k8sClient.Create(ctx, tmpl)).To(Succeed()) + + ns1 = &corev1.Namespace{} + ns1.GenerateName = "ns1" + ns1.Labels = map[string]string{constants.LabelTemplate: tmpl.Name} + Expect(k8sClient.Create(ctx, ns1)).To(Succeed()) + + Eventually(komega.Object(ns1)).Should(HaveField("Labels", HaveKeyWithValue("team", "label"))) + Expect(ns1.Annotations).To(HaveKeyWithValue("memo", "annot")) + }) + + It("should have stable resourceVersion", func() { + resourceVersion := ns1.ResourceVersion + Consistently(komega.Object(ns1)).Should(HaveField("ResourceVersion", Equal(resourceVersion))) + }) + }) + + Context("sub namespace", func() { + var sub1 *corev1.Namespace + + BeforeEach(func() { + root := &corev1.Namespace{} + root.GenerateName = "root" + root.Labels = map[string]string{ + constants.LabelType: constants.NSTypeRoot, + "team": "label", + } + root.Annotations = map[string]string{"memo": "annot"} + Expect(k8sClient.Create(ctx, root)).To(Succeed()) + + sub1 = &corev1.Namespace{} + sub1.GenerateName = "sub1" + sub1.Labels = map[string]string{constants.LabelParent: root.Name} + Expect(k8sClient.Create(ctx, sub1)).To(Succeed()) + + Eventually(komega.Object(sub1)).Should(HaveField("Labels", HaveKeyWithValue("team", "label"))) + Expect(sub1.Annotations).To(HaveKeyWithValue("memo", "annot")) + }) + + It("should have stable resourceVersion", func() { + resourceVersion := sub1.ResourceVersion + Consistently(komega.Object(sub1)).Should(HaveField("ResourceVersion", Equal(resourceVersion))) + }) + }) })