|
| 1 | +/* |
| 2 | +Copyright 2025 SUSE LLC. |
| 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 | + |
| 17 | +package v1beta1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + . "github.com/onsi/ginkgo/v2" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + v1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/utils/ptr" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("RKE2ControlPlane webhook", func() { |
| 30 | + var ( |
| 31 | + oldRcp *RKE2ControlPlane |
| 32 | + rcp *RKE2ControlPlane |
| 33 | + defaulter = &RKE2ControlPlaneCustomDefaulter{} |
| 34 | + validator = &RKE2ControlPlaneCustomValidator{} |
| 35 | + ) |
| 36 | + BeforeEach(func() { |
| 37 | + rcp = &RKE2ControlPlane{ |
| 38 | + ObjectMeta: metav1.ObjectMeta{ |
| 39 | + Name: "test-control-plane", |
| 40 | + Namespace: "test", |
| 41 | + }, |
| 42 | + Spec: RKE2ControlPlaneSpec{ |
| 43 | + MachineTemplate: RKE2ControlPlaneMachineTemplate{ |
| 44 | + InfrastructureRef: v1.ObjectReference{ |
| 45 | + Name: "foo", |
| 46 | + Namespace: "bar", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | + oldRcp = rcp.DeepCopy() |
| 52 | + Expect(defaulter.Default(context.TODO(), rcp)).Should(Succeed()) |
| 53 | + Expect(defaulter.Default(context.TODO(), oldRcp)).Should(Succeed()) |
| 54 | + }) |
| 55 | + It("Should not create RKE2ControlPlane with 0 replicas", func() { |
| 56 | + rcp.Spec.Replicas = nil |
| 57 | + _, err := validator.ValidateCreate(context.TODO(), rcp) |
| 58 | + Expect(err).Should(HaveOccurred()) |
| 59 | + rcp.Spec.Replicas = ptr.To(int32(0)) |
| 60 | + _, err = validator.ValidateCreate(context.TODO(), rcp) |
| 61 | + Expect(err).Should(HaveOccurred()) |
| 62 | + rcp.Spec.Replicas = ptr.To(int32(1)) |
| 63 | + _, err = validator.ValidateCreate(context.TODO(), rcp) |
| 64 | + Expect(err).ShouldNot(HaveOccurred()) |
| 65 | + }) |
| 66 | + It("Should not update RKE2ControlPlane with 0 replicas", func() { |
| 67 | + rcp.Spec.Replicas = nil |
| 68 | + _, err := validator.ValidateUpdate(context.TODO(), oldRcp, rcp) |
| 69 | + Expect(err).Should(HaveOccurred()) |
| 70 | + rcp.Spec.Replicas = ptr.To(int32(0)) |
| 71 | + _, err = validator.ValidateUpdate(context.TODO(), oldRcp, rcp) |
| 72 | + Expect(err).Should(HaveOccurred()) |
| 73 | + rcp.Spec.Replicas = ptr.To(int32(1)) |
| 74 | + _, err = validator.ValidateUpdate(context.TODO(), oldRcp, rcp) |
| 75 | + Expect(err).ShouldNot(HaveOccurred()) |
| 76 | + }) |
| 77 | +}) |
0 commit comments