Skip to content

Commit 4433992

Browse files
author
Joshua Reed
committed
Fixed up CloudStackMachineTemplate webhook tests.
1 parent 8064eb1 commit 4433992

File tree

4 files changed

+54
-60
lines changed

4 files changed

+54
-60
lines changed

api/v1beta1/cloudstackmachine_webhook_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,41 +65,41 @@ var _ = Describe("CloudStackMachine webhook", func() {
6565
})
6666

6767
// Need the `-- not template` here to make the context unique. Apparently ginkgo uses startswith.
68-
Context("When updating a CloudStackMachine -- not CloudStackMachineTemplate", func() {
68+
Context("When updating a CloudStackMachine", func() {
6969
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
7070

7171
BeforeEach(func() { // Reset test vars to initial state.
7272
Ω(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
7373
})
7474

75-
It("should reject VM offering updates.", func() {
75+
It("should reject VM offering updates to the CloudStackMachine", func() {
7676
dummies.CSMachine1.Spec.Offering = "ArbitraryUpdateOffering"
7777
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "offering"))
7878
})
7979

80-
It("should reject VM template updates.", func() {
80+
It("should reject VM template updates to the CloudStackMachine", func() {
8181
dummies.CSMachine1.Spec.Template = "ArbitraryUpdateTemplate"
8282
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "template"))
8383
})
8484

85-
It("should reject updates to VM details.", func() {
85+
It("should reject updates to VM details of the CloudStackMachine", func() {
8686
dummies.CSMachine1.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
8787
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "details"))
8888
})
8989

90-
It("should reject identity reference kind udpates.", func() {
90+
It("should reject identity reference kind udpates to the CloudStackMachine", func() {
9191
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
9292
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
9393
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
9494
})
9595

96-
It("should reject identity reference name udpates.", func() {
96+
It("should reject identity reference name udpates to the CloudStackMachine", func() {
9797
dummies.CSMachine1.Spec.IdentityRef.Name = "IdentityConfigMap"
9898
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
9999
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
100100
})
101101

102-
It("should reject udpates to the list of affinty groups.", func() {
102+
It("should reject udpates to the list of affinty groups of the CloudStackMachine", func() {
103103
dummies.CSMachine1.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
104104
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
105105
Should(MatchRegexp(forbiddenRegex, "AffinityGroupIDs"))

api/v1beta1/cloudstackmachinetemplate_webhook.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ var _ webhook.Validator = &CloudStackMachineTemplate{}
5454

5555
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
5656
func (r *CloudStackMachineTemplate) ValidateCreate() error {
57-
cloudstackmachinetemplatelog.Info("validate create", "name", r.Name)
57+
cloudstackmachinetemplatelog.V(1).Info("validate create", "name", r.Name)
5858

5959
var (
6060
errorList field.ErrorList
@@ -84,31 +84,29 @@ func (r *CloudStackMachineTemplate) ValidateCreate() error {
8484

8585
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
8686
func (r *CloudStackMachineTemplate) ValidateUpdate(old runtime.Object) error {
87-
cloudstackmachinetemplatelog.Info("validate update", "name", r.Name)
88-
89-
var (
90-
errorList field.ErrorList
91-
spec = r.Spec.Spec.Spec // CloudStackMachineTemplateSpec.CloudStackMachineTemplateResource.CloudStackMachineSpec
92-
)
87+
cloudstackmachinetemplatelog.V(1).Info("validate update", "name", r.Name)
9388

9489
oldMachineTemplate, ok := old.(*CloudStackMachineTemplate)
9590
if !ok {
9691
return errors.NewBadRequest(fmt.Sprintf("expected a CloudStackMachineTemplate but got a %T", old))
9792
}
93+
94+
// CloudStackMachineTemplateSpec.CloudStackMachineTemplateResource.CloudStackMachineSpec
95+
spec := r.Spec.Spec.Spec
9896
oldSpec := oldMachineTemplate.Spec.Spec.Spec
9997

98+
errorList := field.ErrorList(nil)
10099
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.Offering, oldSpec.Offering, "offering", errorList)
101100
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.SSHKey, oldSpec.SSHKey, "sshkey", errorList)
102101
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.Template, oldSpec.Template, "template", errorList)
103102
errorList = webhookutil.EnsureStringStringMapFieldsAreEqual(&spec.Details, &oldSpec.Details, "details", errorList)
104-
105103
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.Affinity, oldSpec.Affinity, "affinity", errorList)
106104

107105
if !reflect.DeepEqual(spec.AffinityGroupIDs, oldSpec.AffinityGroupIDs) { // Equivalent to other Ensure funcs.
108106
errorList = append(errorList, field.Forbidden(field.NewPath("spec", "AffinityGroupIDs"), "AffinityGroupIDs"))
109107
}
110108

111-
if spec.IdentityRef != nil && oldSpec.IdentityRef != nil {
109+
if spec.IdentityRef != nil && oldSpec.IdentityRef != nil { // Allow setting once.
112110
errorList = webhookutil.EnsureStringFieldsAreEqual(
113111
spec.IdentityRef.Kind, oldSpec.IdentityRef.Kind, "identityRef.Kind", errorList)
114112
errorList = webhookutil.EnsureStringFieldsAreEqual(

api/v1beta1/cloudstackmachinetemplate_webhook_test.go

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,86 +19,81 @@ package v1beta1_test
1919
import (
2020
"context"
2121

22-
infrav1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
2322
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
2423
. "github.com/onsi/ginkgo"
2524
. "github.com/onsi/gomega"
2625
)
2726

2827
var _ = Describe("CloudStackMachineTemplate webhook", func() {
2928
var ctx context.Context
29+
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
30+
requiredRegex := "admission webhook.*denied the request.*Required value\\: %s"
3031

3132
BeforeEach(func() { // Reset test vars to initial state.
3233
dummies.SetDummyVars()
3334
ctx = context.Background()
35+
_ = k8sClient.Delete(ctx, dummies.CSMachineTemplate1) // Delete any remnants.
3436
})
3537

36-
Context("When creating a CloudStackMachineTemplate with all attributes", func() {
37-
It("Should succeed", func() {
38+
Context("When creating a CloudStackMachineTemplate", func() {
39+
It("Should accept a CloudStackMachineTemplate with all attributes present", func() {
3840
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
3941
})
40-
})
4142

42-
Context("When creating a CloudStackMachineTemplate with missing Offering attribute", func() {
43-
It("Should be rejected by the validating webhooks", func() {
44-
dummies.CSMachine1.Spec.Offering = ""
43+
It("Should reject a CloudStackMachineTemplate when missing the VM Offering attribute", func() {
44+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = ""
4545
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1).Error()).
46-
Should(MatchRegexp("admission webhook.*denied the request.*Required value\\: Offering"))
46+
Should(MatchRegexp(requiredRegex, "Offering"))
4747
})
48-
})
4948

50-
Context("When creating a CloudStackMachineTemplate with missing Template attribute", func() {
51-
It("Should be rejected by the validating webhooks", func() {
52-
dummies.CSMachine1.Spec.Template = ""
49+
It("Should reject a CloudStackMachineTemplate when missing the VM Template attribute", func() {
50+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = ""
5351
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1).Error()).
54-
Should(MatchRegexp("admission webhook.*denied the request.*Required value\\: Template"))
52+
Should(MatchRegexp(requiredRegex, "Template"))
5553
})
56-
})
5754

58-
Context("When creating a CloudStackMachineTemplate with the wrong kind of IdentityReference", func() {
59-
It("Should be rejected by the validating webhooks", func() {
55+
It("Should be reject a CloudStackMachineTemplate with IdentityRef not of kind 'Secret'", func() {
6056
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
6157
Expect(k8sClient.Create(ctx, dummies.CSMachine1).Error()).
62-
Should(MatchRegexp("admission webhook.*denied the request.*Forbidden\\: must be a Secret"))
58+
Should(MatchRegexp(forbiddenRegex, "must be a Secret"))
6359
})
6460
})
6561

6662
Context("When updating a CloudStackMachineTemplate", func() {
67-
It("Should be rejected by the machine template validating webhooks", func() {
68-
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
69-
70-
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
71-
cloudStackMachineTemplateUpdate := &infrav1.CloudStackMachineTemplate{}
63+
BeforeEach(func() { // Reset test vars to initial state.
64+
Ω(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
65+
})
7266

73-
dummies.CSMachineTemplate1.DeepCopyInto(cloudStackMachineTemplateUpdate)
74-
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.Template = "Template2"
75-
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate).Error()).
76-
Should(MatchRegexp(forbiddenRegex, "template"))
67+
It("should reject VM template updates to the CloudStackMachineTemplate", func() {
68+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = "ArbitraryUpdateTemplate"
69+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).Should(MatchRegexp(forbiddenRegex, "template"))
70+
})
7771

78-
dummies.CSMachineTemplate1.DeepCopyInto(cloudStackMachineTemplateUpdate)
79-
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.Offering = "Offering2"
80-
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate).Error()).
81-
Should(MatchRegexp(forbiddenRegex, "offering"))
72+
It("should reject VM offering updates to the CloudStackMachineTemplate", func() {
73+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = "Offering2"
74+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).Should(MatchRegexp(forbiddenRegex, "offering"))
75+
})
8276

83-
dummies.CSMachineTemplate1.DeepCopyInto(cloudStackMachineTemplateUpdate)
84-
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
85-
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate).Error()).
86-
Should(MatchRegexp(forbiddenRegex, "details"))
77+
It("should reject updates to VM details of the CloudStackMachineTemplate", func() {
78+
dummies.CSMachineTemplate1.Spec.Spec.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
79+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).Should(MatchRegexp(forbiddenRegex, "details"))
80+
})
8781

88-
dummies.CSMachineTemplate1.DeepCopyInto(cloudStackMachineTemplateUpdate)
89-
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.IdentityRef.Kind = "configMap"
90-
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate).Error()).
82+
It("should reject identity reference kind updates to the CloudStackMachineTemplate", func() {
83+
dummies.CSMachineTemplate1.Spec.Spec.Spec.IdentityRef.Kind = "configMap"
84+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).
9185
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
86+
})
9287

93-
dummies.CSMachineTemplate1.DeepCopyInto(cloudStackMachineTemplateUpdate)
94-
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.IdentityRef.Name = "IDentityConfigMap"
95-
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate).Error()).
88+
It("should reject identity reference name updates to the CloudStackMachineTemplate", func() {
89+
dummies.CSMachineTemplate1.Spec.Spec.Spec.IdentityRef.Name = "IDentityConfigMap"
90+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).
9691
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
92+
})
9793

98-
dummies.CSMachineTemplate1.DeepCopyInto(cloudStackMachineTemplateUpdate)
99-
cloudStackMachineTemplateUpdate.Spec.Spec.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2ag"}
100-
Expect(k8sClient.Update(ctx, cloudStackMachineTemplateUpdate)).
101-
ShouldNot(Succeed())
94+
It("should reject updates to the list of AffinityGroupIDs of the CloudStackMachineTemplate", func() {
95+
dummies.CSMachineTemplate1.Spec.Spec.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2ag"}
96+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).ShouldNot(Succeed())
10297
})
10398
})
10499
})

test/dummies/vars.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func SetTestTags() {
5050
func SetDummyVars() {
5151
// These need to be in order as they build upon eachother.
5252
SetDummyCAPCClusterVars()
53+
SetDummyCSMachineTemplateVars()
5354
SetDummyCSMachineVars()
5455
SetDummyCAPIClusterVars()
5556
SetDummyTagVars()

0 commit comments

Comments
 (0)