|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +// Copyright 2020-2023 Project Capsule Authors. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package e2e |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + . "github.com/onsi/ginkgo/v2" |
| 12 | + . "github.com/onsi/gomega" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + |
| 15 | + capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" |
| 16 | +) |
| 17 | + |
| 18 | +var _ = Describe("creating a Namespace with Tenant name prefix enforcement at Tenant scope", func() { |
| 19 | + t1 := &capsulev1beta2.Tenant{ |
| 20 | + ObjectMeta: metav1.ObjectMeta{ |
| 21 | + Name: "awesome", |
| 22 | + }, |
| 23 | + Spec: capsulev1beta2.TenantSpec{ |
| 24 | + ForceTenantPrefix: &[]bool{true}[0], |
| 25 | + Owners: capsulev1beta2.OwnerListSpec{ |
| 26 | + { |
| 27 | + Name: "john", |
| 28 | + Kind: "User", |
| 29 | + }, |
| 30 | + }, |
| 31 | + }, |
| 32 | + } |
| 33 | + t2 := &capsulev1beta2.Tenant{ |
| 34 | + ObjectMeta: metav1.ObjectMeta{ |
| 35 | + Name: "awesome-tenant", |
| 36 | + }, |
| 37 | + Spec: capsulev1beta2.TenantSpec{ |
| 38 | + ForceTenantPrefix: &[]bool{false}[0], |
| 39 | + Owners: capsulev1beta2.OwnerListSpec{ |
| 40 | + { |
| 41 | + Name: "john", |
| 42 | + Kind: "User", |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + JustBeforeEach(func() { |
| 49 | + EventuallyCreation(func() error { |
| 50 | + t1.ResourceVersion = "" |
| 51 | + return k8sClient.Create(context.TODO(), t1) |
| 52 | + }).Should(Succeed()) |
| 53 | + EventuallyCreation(func() error { |
| 54 | + t2.ResourceVersion = "" |
| 55 | + return k8sClient.Create(context.TODO(), t2) |
| 56 | + }).Should(Succeed()) |
| 57 | + }) |
| 58 | + JustAfterEach(func() { |
| 59 | + Expect(k8sClient.Delete(context.TODO(), t1)).Should(Succeed()) |
| 60 | + Expect(k8sClient.Delete(context.TODO(), t2)).Should(Succeed()) |
| 61 | + |
| 62 | + ModifyCapsuleConfigurationOpts(func(configuration *capsulev1beta2.CapsuleConfiguration) { |
| 63 | + configuration.Spec.ForceTenantPrefix = false |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + It("should fail when not using prefix, with tenant label for a tenant with ForceTenantPrefix true and global ForceTenantPrefix false", func() { |
| 68 | + ModifyCapsuleConfigurationOpts(func(configuration *capsulev1beta2.CapsuleConfiguration) { |
| 69 | + configuration.Spec.ForceTenantPrefix = false |
| 70 | + }) |
| 71 | + labels := map[string]string{ |
| 72 | + "capsule.clastix.io/tenant": t1.GetName(), |
| 73 | + } |
| 74 | + ns := NewNamespace("awesome", labels) |
| 75 | + NamespaceCreation(ns, t1.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) |
| 76 | + }) |
| 77 | + |
| 78 | + It("should fail using prefix without capsule.clastix.io/tenant label, where the user owns more than one Tenant, for a tenant with ForceTenantPrefix true and global ForceTenantPrefix false", func() { |
| 79 | + ns := NewNamespace("awesome-namespace") |
| 80 | + NamespaceCreation(ns, t1.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) |
| 81 | + }) |
| 82 | + |
| 83 | + It("should fail using prefix without capsule.clastix.io/tenant label, where the user owns more than one Tenant, for a tenant with ForceTenantPrefix false and global ForceTenantPrefix true", func() { |
| 84 | + ns := NewNamespace("awesome-namespace") |
| 85 | + NamespaceCreation(ns, t2.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) |
| 86 | + }) |
| 87 | + |
| 88 | + It("should succeed and be assigned with prefix and label, for a tenant with ForceTenantPrefix true and global ForceTenantPrefix false", func() { |
| 89 | + labels := map[string]string{ |
| 90 | + "capsule.clastix.io/tenant": t1.GetName(), |
| 91 | + } |
| 92 | + ns := NewNamespace("awesome-tenant", labels) |
| 93 | + NamespaceCreation(ns, t1.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 94 | + |
| 95 | + TenantNamespaceList(t1, defaultTimeoutInterval).Should(ContainElement(ns.GetName())) |
| 96 | + }) |
| 97 | + |
| 98 | + It("should fail when not using prefix, with tenant label for a tenant with ForceTenantPrefix true and global ForceTenantPrefix true", func() { |
| 99 | + ModifyCapsuleConfigurationOpts(func(configuration *capsulev1beta2.CapsuleConfiguration) { |
| 100 | + configuration.Spec.ForceTenantPrefix = true |
| 101 | + }) |
| 102 | + labels := map[string]string{ |
| 103 | + "capsule.clastix.io/tenant": t1.GetName(), |
| 104 | + } |
| 105 | + ns := NewNamespace("awesome", labels) |
| 106 | + NamespaceCreation(ns, t1.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) |
| 107 | + }) |
| 108 | + |
| 109 | + It("should succeed and be assigned with prefix and label, for a tenant with ForceTenantPrefix true and global ForceTenantPrefix true", func() { |
| 110 | + ModifyCapsuleConfigurationOpts(func(configuration *capsulev1beta2.CapsuleConfiguration) { |
| 111 | + configuration.Spec.ForceTenantPrefix = true |
| 112 | + }) |
| 113 | + labels := map[string]string{ |
| 114 | + "capsule.clastix.io/tenant": t1.GetName(), |
| 115 | + } |
| 116 | + ns := NewNamespace("awesome-tenant", labels) |
| 117 | + NamespaceCreation(ns, t1.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 118 | + |
| 119 | + TenantNamespaceList(t1, defaultTimeoutInterval).Should(ContainElement(ns.GetName())) |
| 120 | + }) |
| 121 | + |
| 122 | + It("should fail using prefix without capsule.clastix.io/tenant label, for a tenant with ForceTenantPrefix true and global ForceTenantPrefix true", func() { |
| 123 | + ModifyCapsuleConfigurationOpts(func(configuration *capsulev1beta2.CapsuleConfiguration) { |
| 124 | + configuration.Spec.ForceTenantPrefix = true |
| 125 | + }) |
| 126 | + ns := NewNamespace("awesome-namespace") |
| 127 | + NamespaceCreation(ns, t1.Spec.Owners[0], defaultTimeoutInterval).ShouldNot(Succeed()) |
| 128 | + }) |
| 129 | + |
| 130 | + It("should succeed when not using prefix, with tenant label for a tenant with ForceTenantPrefix false and global ForceTenantPrefix false", func() { |
| 131 | + labels := map[string]string{ |
| 132 | + "capsule.clastix.io/tenant": t2.GetName(), |
| 133 | + } |
| 134 | + ns := NewNamespace("awesome", labels) |
| 135 | + NamespaceCreation(ns, t2.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 136 | + }) |
| 137 | + |
| 138 | + It("should succeed when not using prefix, with tenant label for a tenant with ForceTenantPrefix false and global ForceTenantPrefix true", func() { |
| 139 | + ModifyCapsuleConfigurationOpts(func(configuration *capsulev1beta2.CapsuleConfiguration) { |
| 140 | + configuration.Spec.ForceTenantPrefix = true |
| 141 | + }) |
| 142 | + labels := map[string]string{ |
| 143 | + "capsule.clastix.io/tenant": t2.GetName(), |
| 144 | + } |
| 145 | + ns := NewNamespace("awesome", labels) |
| 146 | + NamespaceCreation(ns, t2.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 147 | + }) |
| 148 | +}) |
0 commit comments