|
| 1 | +package e2e_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/client-go/kubernetes" |
| 10 | + |
| 11 | + capsulev1beta2 "github.com/projectcapsule/capsule/api/v1beta2" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("Namespaces", func() { |
| 15 | + var aliceClient, bobClient *kubernetes.Clientset |
| 16 | + |
| 17 | + // Create Global Proxy Settings |
| 18 | + wind := &capsulev1beta2.Tenant{ |
| 19 | + ObjectMeta: metav1.ObjectMeta{ |
| 20 | + Name: "wind", |
| 21 | + Labels: e2eLabels(), |
| 22 | + }, |
| 23 | + Spec: capsulev1beta2.TenantSpec{ |
| 24 | + Owners: capsulev1beta2.OwnerListSpec{ |
| 25 | + { |
| 26 | + Name: "alice", |
| 27 | + Kind: "User", |
| 28 | + }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + } |
| 32 | + |
| 33 | + // Create Global Proxy Settings |
| 34 | + solar := &capsulev1beta2.Tenant{ |
| 35 | + ObjectMeta: metav1.ObjectMeta{ |
| 36 | + Name: "solar", |
| 37 | + Labels: e2eLabels(), |
| 38 | + }, |
| 39 | + Spec: capsulev1beta2.TenantSpec{ |
| 40 | + Owners: capsulev1beta2.OwnerListSpec{ |
| 41 | + { |
| 42 | + Name: "bob", |
| 43 | + Kind: "User", |
| 44 | + }, |
| 45 | + { |
| 46 | + Name: "alice", |
| 47 | + Kind: "User", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + BeforeEach(func() { |
| 54 | + var err error |
| 55 | + |
| 56 | + aliceClient, err = loadKubeConfig("alice") |
| 57 | + Expect(err).ToNot(HaveOccurred()) |
| 58 | + bobClient, err = loadKubeConfig("bob") |
| 59 | + Expect(err).ToNot(HaveOccurred()) |
| 60 | + |
| 61 | + for _, tnt := range []*capsulev1beta2.Tenant{solar, wind} { |
| 62 | + Eventually(func() error { |
| 63 | + tnt.ResourceVersion = "" |
| 64 | + |
| 65 | + return k8sClient.Create(context.TODO(), tnt) |
| 66 | + }).Should(Succeed()) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + JustAfterEach(func() { |
| 71 | + for _, tnt := range []*capsulev1beta2.Tenant{solar, wind} { |
| 72 | + Expect(k8sClient.Delete(context.TODO(), tnt)).Should(Succeed()) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + It("Should correctly list", func() { |
| 77 | + nsAlice1 := NewNamespace("") |
| 78 | + nsAlice1.Labels = map[string]string{ |
| 79 | + "capsule.clastix.io/tenant": "wind", |
| 80 | + } |
| 81 | + NamespaceCreation(nsAlice1, wind.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 82 | + |
| 83 | + nsAlice2 := NewNamespace("") |
| 84 | + nsAlice2.Labels = map[string]string{ |
| 85 | + "capsule.clastix.io/tenant": "wind", |
| 86 | + } |
| 87 | + NamespaceCreation(nsAlice2, wind.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 88 | + |
| 89 | + listNamespaces := func(clientset *kubernetes.Clientset) ([]string, error) { |
| 90 | + ns, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{}) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + var nsNames []string |
| 95 | + for _, name := range ns.Items { |
| 96 | + nsNames = append(nsNames, name.Name) |
| 97 | + } |
| 98 | + |
| 99 | + return nsNames, nil |
| 100 | + } |
| 101 | + |
| 102 | + Eventually(func() ([]string, error) { |
| 103 | + return listNamespaces(aliceClient) |
| 104 | + }).Should(ConsistOf(nsAlice1.GetName(), nsAlice2.GetName()), "Alice should only have access to the expected namespaces, order does not matter") |
| 105 | + |
| 106 | + nsBob1 := NewNamespace("") |
| 107 | + nsBob1.Labels = map[string]string{ |
| 108 | + "capsule.clastix.io/tenant": "solar", |
| 109 | + } |
| 110 | + NamespaceCreation(nsBob1, solar.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 111 | + |
| 112 | + nsBob2 := NewNamespace("") |
| 113 | + nsBob2.Labels = map[string]string{ |
| 114 | + "capsule.clastix.io/tenant": "solar", |
| 115 | + } |
| 116 | + NamespaceCreation(nsBob2, solar.Spec.Owners[0], defaultTimeoutInterval).Should(Succeed()) |
| 117 | + |
| 118 | + Eventually(func() ([]string, error) { |
| 119 | + return listNamespaces(bobClient) |
| 120 | + }).Should(ConsistOf(nsBob1.GetName(), nsBob2.GetName()), "Alice should only have access to the expected namespaces, order does not matter") |
| 121 | + |
| 122 | + }) |
| 123 | +}) |
0 commit comments