|
| 1 | +/* |
| 2 | +Copyright 2022 The Kubernetes Authors. |
| 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 v1beta2_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2" |
| 23 | + dummies "sigs.k8s.io/cluster-api-provider-cloudstack/test/dummies/v1beta2" |
| 24 | + |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 27 | +) |
| 28 | + |
| 29 | +var _ = Describe("CloudStackMachine webhook", func() { |
| 30 | + var ctx context.Context |
| 31 | + forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s" |
| 32 | + requiredRegex := "admission webhook.*denied the request.*Required value\\: %s" |
| 33 | + |
| 34 | + BeforeEach(func() { // Reset test vars to initial state. |
| 35 | + dummies.SetDummyVars() |
| 36 | + ctx = context.Background() |
| 37 | + _ = k8sClient.Delete(ctx, dummies.CSMachine1) // Clear out any remaining machines. Ignore errors. |
| 38 | + }) |
| 39 | + |
| 40 | + Context("When creating a CloudStackMachine", func() { |
| 41 | + It("should accept CloudStackMachine with all attributes", func() { |
| 42 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed()) |
| 43 | + }) |
| 44 | + |
| 45 | + It("should accept a CloudStackMachine with disk Offering attribute", func() { |
| 46 | + dummies.CSMachine1.Spec.DiskOffering = dummies.DiskOffering |
| 47 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed()) |
| 48 | + }) |
| 49 | + |
| 50 | + It("should accept a CloudStackMachine with positive disk Offering size attribute", func() { |
| 51 | + dummies.CSMachine1.Spec.DiskOffering = dummies.DiskOffering |
| 52 | + dummies.CSMachine1.Spec.DiskOffering.CustomSize = 1 |
| 53 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed()) |
| 54 | + }) |
| 55 | + |
| 56 | + It("should not accept a CloudStackMachine with negative disk Offering size attribute", func() { |
| 57 | + dummies.CSMachine1.Spec.DiskOffering = dummies.DiskOffering |
| 58 | + dummies.CSMachine1.Spec.DiskOffering.CustomSize = -1 |
| 59 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)).Should(MatchError(MatchRegexp(forbiddenRegex, "customSizeInGB"))) |
| 60 | + }) |
| 61 | + |
| 62 | + It("should reject a CloudStackMachine with missing Offering attribute", func() { |
| 63 | + dummies.CSMachine1.Spec.Offering = infrav1.CloudStackResourceIdentifier{ID: "", Name: ""} |
| 64 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)). |
| 65 | + Should(MatchError(MatchRegexp(requiredRegex, "Offering"))) |
| 66 | + }) |
| 67 | + |
| 68 | + It("should reject a CloudStackMachine with missing Template attribute", func() { |
| 69 | + dummies.CSMachine1.Spec.Template = infrav1.CloudStackResourceIdentifier{ID: "", Name: ""} |
| 70 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)). |
| 71 | + Should(MatchError(MatchRegexp(requiredRegex, "Template"))) |
| 72 | + }) |
| 73 | + |
| 74 | + It("should reject a CloudStackMachine with IdentityRef not of kind 'Secret'", func() { |
| 75 | + dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap" |
| 76 | + Expect(k8sClient.Create(ctx, dummies.CSMachine1)). |
| 77 | + Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret"))) |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + Context("When updating a CloudStackMachine", func() { |
| 82 | + BeforeEach(func() { |
| 83 | + Ω(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed()) |
| 84 | + }) |
| 85 | + |
| 86 | + It("should reject VM offering updates to the CloudStackMachine", func() { |
| 87 | + dummies.CSMachine1.Spec.Offering = infrav1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateOffering"} |
| 88 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 89 | + Should(MatchError(MatchRegexp(forbiddenRegex, "offering"))) |
| 90 | + }) |
| 91 | + |
| 92 | + It("should reject VM template updates to the CloudStackMachine", func() { |
| 93 | + dummies.CSMachine1.Spec.Template = infrav1.CloudStackResourceIdentifier{Name: "ArbitraryUpdateTemplate"} |
| 94 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 95 | + Should(MatchError(MatchRegexp(forbiddenRegex, "template"))) |
| 96 | + }) |
| 97 | + |
| 98 | + It("should reject VM disk offering updates to the CloudStackMachine", func() { |
| 99 | + dummies.CSMachine1.Spec.DiskOffering.Name = "medium" |
| 100 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 101 | + Should(MatchError(MatchRegexp(forbiddenRegex, "diskOffering"))) |
| 102 | + }) |
| 103 | + |
| 104 | + It("should reject updates to VM details of the CloudStackMachine", func() { |
| 105 | + dummies.CSMachine1.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"} |
| 106 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 107 | + Should(MatchError(MatchRegexp(forbiddenRegex, "details"))) |
| 108 | + }) |
| 109 | + |
| 110 | + It("should reject identity reference kind updates to the CloudStackMachine", func() { |
| 111 | + dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap" |
| 112 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 113 | + Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))) |
| 114 | + }) |
| 115 | + |
| 116 | + It("should reject identity reference name updates to the CloudStackMachine", func() { |
| 117 | + dummies.CSMachine1.Spec.IdentityRef.Name = "IdentityConfigMap" |
| 118 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 119 | + Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))) |
| 120 | + }) |
| 121 | + |
| 122 | + It("should reject updates to the list of affinty groups of the CloudStackMachine", func() { |
| 123 | + dummies.CSMachine1.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"} |
| 124 | + Ω(k8sClient.Update(ctx, dummies.CSMachine1)). |
| 125 | + Should(MatchError(MatchRegexp(forbiddenRegex, "AffinityGroupIDs"))) |
| 126 | + }) |
| 127 | + }) |
| 128 | +}) |
0 commit comments