|
| 1 | +/* |
| 2 | +Copyright 2023. |
| 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" //revive:disable:dot-imports |
| 22 | + . "github.com/onsi/gomega" //revive:disable:dot-imports |
| 23 | + k8s_errors "k8s.io/apimachinery/pkg/api/errors" |
| 24 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 25 | + "k8s.io/apimachinery/pkg/types" |
| 26 | + |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 28 | +) |
| 29 | + |
| 30 | +var _ = Describe("OpenStackProvisionServer Webhook", func() { |
| 31 | + |
| 32 | + var provisionServerName types.NamespacedName |
| 33 | + |
| 34 | + BeforeEach(func() { |
| 35 | + provisionServerName = types.NamespacedName{ |
| 36 | + Name: "test-provisionserver", |
| 37 | + Namespace: namespace, |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + When("Creating ProvisionServer with valid configuration", func() { |
| 42 | + It("should succeed with all required fields", func() { |
| 43 | + spec := map[string]interface{}{ |
| 44 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 45 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 46 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 47 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 48 | + } |
| 49 | + raw := map[string]interface{}{ |
| 50 | + "apiVersion": "baremetal.openstack.org/v1beta1", |
| 51 | + "kind": "OpenStackProvisionServer", |
| 52 | + "metadata": map[string]interface{}{ |
| 53 | + "name": provisionServerName.Name, |
| 54 | + "namespace": provisionServerName.Namespace, |
| 55 | + }, |
| 56 | + "spec": spec, |
| 57 | + } |
| 58 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 59 | + _, err := controllerutil.CreateOrPatch( |
| 60 | + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) |
| 61 | + Expect(err).ShouldNot(HaveOccurred()) |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + When("Creating ProvisionServer with invalid name", func() { |
| 66 | + It("should fail with name not matching RFC1123", func() { |
| 67 | + invalidName := types.NamespacedName{ |
| 68 | + Name: "Test_Invalid_Name", |
| 69 | + Namespace: namespace, |
| 70 | + } |
| 71 | + spec := map[string]interface{}{ |
| 72 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 73 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 74 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 75 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 76 | + } |
| 77 | + raw := map[string]interface{}{ |
| 78 | + "apiVersion": "baremetal.openstack.org/v1beta1", |
| 79 | + "kind": "OpenStackProvisionServer", |
| 80 | + "metadata": map[string]interface{}{ |
| 81 | + "name": invalidName.Name, |
| 82 | + "namespace": invalidName.Namespace, |
| 83 | + }, |
| 84 | + "spec": spec, |
| 85 | + } |
| 86 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 87 | + _, err := controllerutil.CreateOrPatch( |
| 88 | + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) |
| 89 | + Expect(err).Should(HaveOccurred()) |
| 90 | + var statusError *k8s_errors.StatusError |
| 91 | + Expect(errors.As(err, &statusError)).To(BeTrue()) |
| 92 | + Expect(statusError.ErrStatus.Message).To(ContainSubstring("RFC 1123")) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + When("Creating ProvisionServer with port out of range", func() { |
| 97 | + It("should fail with port below minimum", func() { |
| 98 | + spec := map[string]interface{}{ |
| 99 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 100 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 101 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 102 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 103 | + "port": int64(6100), // Below minimum 6190 |
| 104 | + } |
| 105 | + raw := map[string]interface{}{ |
| 106 | + "apiVersion": "baremetal.openstack.org/v1beta1", |
| 107 | + "kind": "OpenStackProvisionServer", |
| 108 | + "metadata": map[string]interface{}{ |
| 109 | + "name": provisionServerName.Name, |
| 110 | + "namespace": provisionServerName.Namespace, |
| 111 | + }, |
| 112 | + "spec": spec, |
| 113 | + } |
| 114 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 115 | + err := k8sClient.Create(ctx, unstructuredObj) |
| 116 | + Expect(err).Should(HaveOccurred()) |
| 117 | + }) |
| 118 | + |
| 119 | + It("should fail with port above maximum", func() { |
| 120 | + spec := map[string]interface{}{ |
| 121 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 122 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 123 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 124 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 125 | + "port": int64(6230), // Above maximum 6220 |
| 126 | + } |
| 127 | + raw := map[string]interface{}{ |
| 128 | + "apiVersion": "baremetal.openstack.org/v1beta1", |
| 129 | + "kind": "OpenStackProvisionServer", |
| 130 | + "metadata": map[string]interface{}{ |
| 131 | + "name": provisionServerName.Name, |
| 132 | + "namespace": provisionServerName.Namespace, |
| 133 | + }, |
| 134 | + "spec": spec, |
| 135 | + } |
| 136 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 137 | + err := k8sClient.Create(ctx, unstructuredObj) |
| 138 | + Expect(err).Should(HaveOccurred()) |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + When("Creating ProvisionServer with duplicate port", func() { |
| 143 | + var provisionServer2Name types.NamespacedName |
| 144 | + |
| 145 | + BeforeEach(func() { |
| 146 | + provisionServer2Name = types.NamespacedName{ |
| 147 | + Name: "test-provisionserver-2", |
| 148 | + Namespace: namespace, |
| 149 | + } |
| 150 | + // Create first provision server with specific port |
| 151 | + spec := map[string]interface{}{ |
| 152 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 153 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 154 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 155 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 156 | + "port": int64(6200), |
| 157 | + } |
| 158 | + DeferCleanup(th.DeleteInstance, CreateProvisionServer(provisionServerName, spec)) |
| 159 | + }) |
| 160 | + |
| 161 | + It("should fail when trying to use same port", func() { |
| 162 | + spec := map[string]interface{}{ |
| 163 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 164 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 165 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 166 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 167 | + "port": int64(6200), // Same as first server |
| 168 | + } |
| 169 | + raw := map[string]interface{}{ |
| 170 | + "apiVersion": "baremetal.openstack.org/v1beta1", |
| 171 | + "kind": "OpenStackProvisionServer", |
| 172 | + "metadata": map[string]interface{}{ |
| 173 | + "name": provisionServer2Name.Name, |
| 174 | + "namespace": provisionServer2Name.Namespace, |
| 175 | + }, |
| 176 | + "spec": spec, |
| 177 | + } |
| 178 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 179 | + _, err := controllerutil.CreateOrPatch( |
| 180 | + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) |
| 181 | + Expect(err).Should(HaveOccurred()) |
| 182 | + var statusError *k8s_errors.StatusError |
| 183 | + Expect(errors.As(err, &statusError)).To(BeTrue()) |
| 184 | + Expect(statusError.ErrStatus.Message).To(ContainSubstring("already in use")) |
| 185 | + }) |
| 186 | + |
| 187 | + It("should succeed when using different port", func() { |
| 188 | + spec := map[string]interface{}{ |
| 189 | + "osImage": "edpm-hardened-uefi.qcow2", |
| 190 | + "osContainerImageUrl": "quay.io/podified-antelope-centos9/edpm-hardened-uefi:current-podified", |
| 191 | + "apacheImageUrl": "registry.redhat.io/ubi9/httpd-24:latest", |
| 192 | + "agentImageUrl": "quay.io/openstack-k8s-operators/openstack-baremetal-operator-agent:latest", |
| 193 | + "port": int64(6201), // Different port |
| 194 | + } |
| 195 | + raw := map[string]interface{}{ |
| 196 | + "apiVersion": "baremetal.openstack.org/v1beta1", |
| 197 | + "kind": "OpenStackProvisionServer", |
| 198 | + "metadata": map[string]interface{}{ |
| 199 | + "name": provisionServer2Name.Name, |
| 200 | + "namespace": provisionServer2Name.Namespace, |
| 201 | + }, |
| 202 | + "spec": spec, |
| 203 | + } |
| 204 | + unstructuredObj := &unstructured.Unstructured{Object: raw} |
| 205 | + _, err := controllerutil.CreateOrPatch( |
| 206 | + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) |
| 207 | + Expect(err).ShouldNot(HaveOccurred()) |
| 208 | + }) |
| 209 | + }) |
| 210 | + |
| 211 | + When("Creating ProvisionServer with defaulting", func() { |
| 212 | + It("should auto-assign port and default image URLs when not specified", func() { |
| 213 | + spec := map[string]interface{}{ |
| 214 | + // Not specifying osImage, port, or image URLs to test defaulting |
| 215 | + } |
| 216 | + DeferCleanup(th.DeleteInstance, CreateProvisionServer(provisionServerName, spec)) |
| 217 | + |
| 218 | + instance := GetProvisionServerDirect(provisionServerName) |
| 219 | + // Port should be auto-assigned |
| 220 | + Expect(instance.Spec.Port).Should(BeNumerically(">", 0)) |
| 221 | + // Image URLs and OSImage should be defaulted |
| 222 | + Expect(instance.Spec.OSImage).ShouldNot(BeEmpty()) |
| 223 | + Expect(instance.Spec.OSContainerImageURL).ShouldNot(BeEmpty()) |
| 224 | + Expect(instance.Spec.ApacheImageURL).ShouldNot(BeEmpty()) |
| 225 | + Expect(instance.Spec.AgentImageURL).ShouldNot(BeEmpty()) |
| 226 | + }) |
| 227 | + }) |
| 228 | +}) |
0 commit comments