|
| 1 | +// Copyright 2022 Clastix Labs |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package handlers |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + . "github.com/onsi/ginkgo/v2" |
| 10 | + . "github.com/onsi/gomega" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + utilruntime "k8s.io/apimachinery/pkg/util/runtime" |
| 14 | + fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake" |
| 15 | + |
| 16 | + kamajiv1alpha1 "github.com/clastix/kamaji/api/v1alpha1" |
| 17 | +) |
| 18 | + |
| 19 | +var _ = Describe("TCP Datastore webhook", func() { |
| 20 | + var ( |
| 21 | + ctx context.Context |
| 22 | + t TenantControlPlaneDataStore |
| 23 | + tcp *kamajiv1alpha1.TenantControlPlane |
| 24 | + ) |
| 25 | + BeforeEach(func() { |
| 26 | + scheme := runtime.NewScheme() |
| 27 | + utilruntime.Must(kamajiv1alpha1.AddToScheme(scheme)) |
| 28 | + |
| 29 | + ctx = context.Background() |
| 30 | + t = TenantControlPlaneDataStore{ |
| 31 | + Client: fakeclient.NewClientBuilder().WithScheme(scheme).WithObjects(&kamajiv1alpha1.DataStore{ |
| 32 | + ObjectMeta: metav1.ObjectMeta{ |
| 33 | + Name: "foo", |
| 34 | + }, |
| 35 | + }, &kamajiv1alpha1.DataStore{ |
| 36 | + ObjectMeta: metav1.ObjectMeta{ |
| 37 | + Name: "bar", |
| 38 | + }, |
| 39 | + }).Build(), |
| 40 | + } |
| 41 | + tcp = &kamajiv1alpha1.TenantControlPlane{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: "tcp", |
| 44 | + Namespace: "default", |
| 45 | + }, |
| 46 | + Spec: kamajiv1alpha1.TenantControlPlaneSpec{}, |
| 47 | + } |
| 48 | + }) |
| 49 | + Describe("validation should succeed without DataStoreOverrides", func() { |
| 50 | + It("should validate TCP without DataStoreOverrides", func() { |
| 51 | + err := t.checkDataStoreOverrides(ctx, tcp) |
| 52 | + Expect(err).ToNot(HaveOccurred()) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + Describe("validation should fail with duplicate resources in DataStoreOverrides", func() { |
| 57 | + It("should fail to validate TCP with duplicate resources in DataStoreOverrides", func() { |
| 58 | + tcp.Spec.DataStoreOverrides = []kamajiv1alpha1.DataStoreOverride{{ |
| 59 | + Resource: "/event", |
| 60 | + DataStore: "foo", |
| 61 | + }, { |
| 62 | + Resource: "/event", |
| 63 | + DataStore: "bar", |
| 64 | + }} |
| 65 | + err := t.checkDataStoreOverrides(ctx, tcp) |
| 66 | + Expect(err).To(HaveOccurred()) |
| 67 | + }) |
| 68 | + }) |
| 69 | + |
| 70 | + Describe("validation should succeed with valid DataStoreOverrides", func() { |
| 71 | + It("should validate TCP with valid DataStoreOverrides", func() { |
| 72 | + tcp.Spec.DataStoreOverrides = []kamajiv1alpha1.DataStoreOverride{{ |
| 73 | + Resource: "/leases", |
| 74 | + DataStore: "foo", |
| 75 | + }, { |
| 76 | + Resource: "/event", |
| 77 | + DataStore: "bar", |
| 78 | + }} |
| 79 | + err := t.checkDataStoreOverrides(ctx, tcp) |
| 80 | + Expect(err).ToNot(HaveOccurred()) |
| 81 | + }) |
| 82 | + }) |
| 83 | + |
| 84 | + Describe("validation should fail with nonexistent DataStoreOverrides", func() { |
| 85 | + It("should fail to validate TCP with nonexistent DataStoreOverrides", func() { |
| 86 | + tcp.Spec.DataStoreOverrides = []kamajiv1alpha1.DataStoreOverride{{ |
| 87 | + Resource: "/leases", |
| 88 | + DataStore: "baz", |
| 89 | + }} |
| 90 | + err := t.checkDataStoreOverrides(ctx, tcp) |
| 91 | + Expect(err).To(HaveOccurred()) |
| 92 | + }) |
| 93 | + }) |
| 94 | +}) |
0 commit comments