|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package functional |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + |
| 21 | + . "github.com/onsi/ginkgo/v2" |
| 22 | + . "github.com/onsi/gomega" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 24 | + |
| 25 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 26 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("Glance validation", func() { |
| 30 | + It("webhooks reject the request - invalid keystoneEndpoint", func() { |
| 31 | + // GlanceEmptySpec is used to provide a standard Glance CR where no |
| 32 | + // field is customized: we can inject our parameters to test webhooks |
| 33 | + spec := GetGlanceDefaultSpec() |
| 34 | + spec["keystoneEndpoint"] = "foo" |
| 35 | + raw := map[string]interface{}{ |
| 36 | + "apiVersion": "glance.openstack.org/v1beta1", |
| 37 | + "kind": "Glance", |
| 38 | + "metadata": map[string]interface{}{ |
| 39 | + "name": glanceTest.Instance.Name, |
| 40 | + "namespace": glanceTest.Instance.Namespace, |
| 41 | + }, |
| 42 | + "spec": spec, |
| 43 | + } |
| 44 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 45 | + _, err := controllerutil.CreateOrPatch( |
| 46 | + ctx, k8sClient, unstructuredObj, func() error { return nil }) |
| 47 | + |
| 48 | + Expect(err).Should(HaveOccurred()) |
| 49 | + var statusError *k8s_errors.StatusError |
| 50 | + Expect(errors.As(err, &statusError)).To(BeTrue()) |
| 51 | + Expect(statusError.ErrStatus.Message).To( |
| 52 | + ContainSubstring( |
| 53 | + "KeystoneEndpoint is assigned to an invalid glanceAPI instance"), |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + It("webhooks reject the request - invalid backend", func() { |
| 58 | + spec := GetGlanceDefaultSpec() |
| 59 | + |
| 60 | + gapis := map[string]interface{}{ |
| 61 | + "glanceAPIs": map[string]interface{}{ |
| 62 | + "default": map[string]interface{}{ |
| 63 | + "replicas": 1, |
| 64 | + "type": "split", |
| 65 | + }, |
| 66 | + "edge1": map[string]interface{}{ |
| 67 | + "replicas": 1, |
| 68 | + "type": "edge", |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | + |
| 73 | + spec["keystoneEndpoint"] = "edge1" |
| 74 | + spec["glanceAPIs"] = gapis |
| 75 | + |
| 76 | + raw := map[string]interface{}{ |
| 77 | + "apiVersion": "glance.openstack.org/v1beta1", |
| 78 | + "kind": "Glance", |
| 79 | + "metadata": map[string]interface{}{ |
| 80 | + "name": glanceTest.Instance.Name, |
| 81 | + "namespace": glanceTest.Instance.Namespace, |
| 82 | + }, |
| 83 | + "spec": spec, |
| 84 | + } |
| 85 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 86 | + _, err := controllerutil.CreateOrPatch( |
| 87 | + ctx, k8sClient, unstructuredObj, func() error { return nil }) |
| 88 | + |
| 89 | + Expect(err).Should(HaveOccurred()) |
| 90 | + var statusError *k8s_errors.StatusError |
| 91 | + Expect(errors.As(err, &statusError)).To(BeTrue()) |
| 92 | + // Webhooks catch that no backend is set before even realize that an |
| 93 | + // invalid endpoint has been set |
| 94 | + Expect(statusError.ErrStatus.Message).To( |
| 95 | + ContainSubstring( |
| 96 | + "Invalid backend configuration detected"), |
| 97 | + ) |
| 98 | + }) |
| 99 | + |
| 100 | + It("webhooks reject the request - invalid instance", func() { |
| 101 | + spec := GetGlanceDefaultSpec() |
| 102 | + |
| 103 | + gapis := map[string]interface{}{ |
| 104 | + "edge2": map[string]interface{}{ |
| 105 | + "replicas": 1, |
| 106 | + "type": "edge", |
| 107 | + // inject a valid Ceph backend |
| 108 | + "customServiceConfig": GetDummyBackend(), |
| 109 | + }, |
| 110 | + "default": map[string]interface{}{ |
| 111 | + "replicas": 1, |
| 112 | + "type": "split", |
| 113 | + // inject a valid Ceph backend |
| 114 | + "customServiceConfig": GetDummyBackend(), |
| 115 | + }, |
| 116 | + "edge1": map[string]interface{}{ |
| 117 | + "replicas": 1, |
| 118 | + "type": "edge", |
| 119 | + // inject a valid Ceph backend |
| 120 | + "customServiceConfig": GetDummyBackend(), |
| 121 | + }, |
| 122 | + } |
| 123 | + // Set the KeystoneEndpoint to the wrong instance |
| 124 | + spec["keystoneEndpoint"] = "edge1" |
| 125 | + // Deploy multiple GlanceAPIs |
| 126 | + spec["glanceAPIs"] = gapis |
| 127 | + |
| 128 | + raw := map[string]interface{}{ |
| 129 | + "apiVersion": "glance.openstack.org/v1beta1", |
| 130 | + "kind": "Glance", |
| 131 | + "metadata": map[string]interface{}{ |
| 132 | + "name": glanceTest.Instance.Name, |
| 133 | + "namespace": glanceTest.Instance.Namespace, |
| 134 | + }, |
| 135 | + "spec": spec, |
| 136 | + } |
| 137 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 138 | + _, err := controllerutil.CreateOrPatch( |
| 139 | + ctx, k8sClient, unstructuredObj, func() error { return nil }) |
| 140 | + |
| 141 | + Expect(err).Should(HaveOccurred()) |
| 142 | + var statusError *k8s_errors.StatusError |
| 143 | + Expect(errors.As(err, &statusError)).To(BeTrue()) |
| 144 | + // We shouldn't fail again for the backend, but because the endpoint is |
| 145 | + // not valid |
| 146 | + Expect(statusError.ErrStatus.Message).To( |
| 147 | + ContainSubstring( |
| 148 | + "KeystoneEndpoint is assigned to an invalid glanceAPI instance"), |
| 149 | + ) |
| 150 | + }) |
| 151 | +}) |
0 commit comments