From 184f054f2f25e27bb2a69ebec1af8e964d213d54 Mon Sep 17 00:00:00 2001 From: Dario Tranchitella Date: Sat, 26 Jun 2021 17:18:27 +0200 Subject: [PATCH] test(e2e): adding further tests for collisions --- ...ngress_hostnames_allowed_collision_test.go | 66 ++++++++++++++++--- ...ngress_hostnames_collision_allowed_test.go | 31 +++++++-- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/e2e/ingress_hostnames_allowed_collision_test.go b/e2e/ingress_hostnames_allowed_collision_test.go index 67ec7be4..2c226f8d 100644 --- a/e2e/ingress_hostnames_allowed_collision_test.go +++ b/e2e/ingress_hostnames_allowed_collision_test.go @@ -72,6 +72,7 @@ var _ = Describe("when handling Ingress hostnames collision", func() { configuration.Spec.AllowIngressHostnameCollision = true }) }) + JustAfterEach(func() { Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) @@ -80,6 +81,51 @@ var _ = Describe("when handling Ingress hostnames collision", func() { }) }) + It("should not allow creating several Ingress with same hostname", func() { + ModifyCapsuleConfigurationOpts(func(configuration *v1alpha1.CapsuleConfiguration) { + configuration.Spec.AllowIngressHostnameCollision = false + }) + + maj, min, _ := GetKubernetesSemVer() + + ns := NewNamespace("denied-collision") + cs := ownerClient(tnt) + + NamespaceCreation(ns, tnt, defaultTimeoutInterval).Should(Succeed()) + TenantNamespaceList(tnt, defaultTimeoutInterval).Should(ContainElement(ns.GetName())) + + if maj == 1 && min > 18 { + By("testing networking.k8s.io", func() { + EventuallyCreation(func() (err error) { + obj := networkingIngress("networking-1", "kubernetes.io") + _, err = cs.NetworkingV1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) + return + }).Should(Succeed()) + EventuallyCreation(func() (err error) { + obj := networkingIngress("networking-2", "kubernetes.io") + _, err = cs.NetworkingV1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) + return + }).ShouldNot(Succeed()) + }) + } + + if maj == 1 && min < 22 { + By("testing extensions", func() { + EventuallyCreation(func() (err error) { + obj := extensionsIngress("extensions-1", "cncf.io") + _, err = cs.ExtensionsV1beta1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) + return + }).Should(Succeed()) + EventuallyCreation(func() (err error) { + obj := extensionsIngress("extensions-2", "cncf.io") + _, err = cs.ExtensionsV1beta1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) + return + }).ShouldNot(Succeed()) + }) + } + + }) + It("should allow creating several Ingress with same hostname", func() { maj, min, _ := GetKubernetesSemVer() @@ -91,31 +137,31 @@ var _ = Describe("when handling Ingress hostnames collision", func() { if maj == 1 && min > 18 { By("testing networking.k8s.io", func() { - Eventually(func() (err error) { + EventuallyCreation(func() (err error) { obj := networkingIngress("networking-1", "kubernetes.io") _, err = cs.NetworkingV1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) return - }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) - Eventually(func() (err error) { + }).Should(Succeed()) + EventuallyCreation(func() (err error) { obj := networkingIngress("networking-2", "kubernetes.io") _, err = cs.NetworkingV1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) return - }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) + }).Should(Succeed()) }) } if maj == 1 && min < 22 { By("testing extensions", func() { - Eventually(func() (err error) { - obj := extensionsIngress("extensions-1", "kubernetes.io") + EventuallyCreation(func() (err error) { + obj := extensionsIngress("extensions-1", "cncf.io") _, err = cs.ExtensionsV1beta1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) return - }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) - Eventually(func() (err error) { - obj := extensionsIngress("extensions-2", "kubernetes.io") + }).Should(Succeed()) + EventuallyCreation(func() (err error) { + obj := extensionsIngress("extensions-2", "cncf.io") _, err = cs.ExtensionsV1beta1().Ingresses(ns.GetName()).Create(context.TODO(), obj, metav1.CreateOptions{}) return - }, defaultTimeoutInterval, defaultPollInterval).Should(Succeed()) + }).Should(Succeed()) }) } }) diff --git a/e2e/tenant_ingress_hostnames_collision_allowed_test.go b/e2e/tenant_ingress_hostnames_collision_allowed_test.go index ee421f22..be85a748 100644 --- a/e2e/tenant_ingress_hostnames_collision_allowed_test.go +++ b/e2e/tenant_ingress_hostnames_collision_allowed_test.go @@ -37,11 +37,8 @@ var _ = Describe("when a second Tenant contains an already declared allowed Ingr tnt.ResourceVersion = "" return k8sClient.Create(context.TODO(), tnt) }).Should(Succeed()) - - ModifyCapsuleConfigurationOpts(func(configuration *v1alpha1.CapsuleConfiguration) { - configuration.Spec.AllowTenantIngressHostnamesCollision = true - }) }) + JustAfterEach(func() { Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) @@ -50,7 +47,33 @@ var _ = Describe("when a second Tenant contains an already declared allowed Ingr }) }) + It("should block creation if contains collided Ingress hostnames", func() { + for i, h := range tnt.Spec.IngressHostnames.Exact { + tnt2 := &v1alpha1.Tenant{ + ObjectMeta: metav1.ObjectMeta{ + Name: fmt.Sprintf("%s-%d", tnt.GetName(), i), + }, + Spec: v1alpha1.TenantSpec{ + Owner: v1alpha1.OwnerSpec{ + Name: "second-user", + Kind: "User", + }, + IngressHostnames: &v1alpha1.AllowedListSpec{ + Exact: []string{h}, + }, + }, + } + EventuallyCreation(func() error { + return k8sClient.Create(context.TODO(), tnt2) + }).ShouldNot(Succeed()) + } + }) + It("should not block creation if contains collided Ingress hostnames", func() { + ModifyCapsuleConfigurationOpts(func(configuration *v1alpha1.CapsuleConfiguration) { + configuration.Spec.AllowTenantIngressHostnamesCollision = true + }) + for i, h := range tnt.Spec.IngressHostnames.Exact { tnt2 := &v1alpha1.Tenant{ ObjectMeta: metav1.ObjectMeta{