Skip to content

Commit 8064eb1

Browse files
author
Joshua Reed
committed
Fixed up CloudStackMachine webhook tests.
1 parent 062c7b3 commit 8064eb1

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

api/v1beta1/cloudstackmachine_webhook_test.go

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ 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"
@@ -31,6 +30,8 @@ var _ = Describe("CloudStackMachine webhook", func() {
3130
BeforeEach(func() { // Reset test vars to initial state.
3231
dummies.SetDummyVars()
3332
ctx = context.Background()
33+
// Clear out any remaining machines. Ignore errors.
34+
_ = k8sClient.Delete(ctx, dummies.CSMachine1)
3435
})
3536

3637
Context("When creating a CloudStackMachine with all attributes", func() {
@@ -63,38 +64,45 @@ var _ = Describe("CloudStackMachine webhook", func() {
6364
})
6465
})
6566

66-
Context("When updating a CloudStackMachine", func() {
67-
It("Should be rejected by the validating webhooks", func() {
68-
Ω(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
67+
// Need the `-- not template` here to make the context unique. Apparently ginkgo uses startswith.
68+
Context("When updating a CloudStackMachine -- not CloudStackMachineTemplate", func() {
69+
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
6970

70-
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
71-
cloudStackMachineUpdate := &infrav1.CloudStackMachine{}
71+
BeforeEach(func() { // Reset test vars to initial state.
72+
Ω(k8sClient.Create(ctx, dummies.CSMachine1)).Should(Succeed())
73+
})
7274

73-
dummies.CSMachine1.DeepCopyInto(cloudStackMachineUpdate)
74-
cloudStackMachineUpdate.Spec.Template = "Template2"
75-
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "template"))
75+
It("should reject VM offering updates.", func() {
76+
dummies.CSMachine1.Spec.Offering = "ArbitraryUpdateOffering"
77+
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "offering"))
78+
})
7679

77-
dummies.CSMachine1.DeepCopyInto(cloudStackMachineUpdate)
78-
cloudStackMachineUpdate.Spec.Offering = "Offering2"
79-
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "offering"))
80+
It("should reject VM template updates.", func() {
81+
dummies.CSMachine1.Spec.Template = "ArbitraryUpdateTemplate"
82+
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "template"))
83+
})
8084

81-
dummies.CSMachine1.DeepCopyInto(cloudStackMachineUpdate)
82-
cloudStackMachineUpdate.Spec.Details = map[string]string{
83-
"memoryOvercommitRatio": "1.5",
84-
}
85-
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "details"))
85+
It("should reject updates to VM details.", func() {
86+
dummies.CSMachine1.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
87+
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "details"))
88+
})
8689

87-
dummies.CSMachine1.DeepCopyInto(cloudStackMachineUpdate)
88-
cloudStackMachineUpdate.Spec.IdentityRef.Kind = "ConfigMap"
89-
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
90+
It("should reject identity reference kind udpates.", func() {
91+
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
92+
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
93+
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
94+
})
9095

91-
dummies.CSMachine1.DeepCopyInto(cloudStackMachineUpdate)
92-
cloudStackMachineUpdate.Spec.IdentityRef.Name = "IDentityConfigMap"
93-
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
96+
It("should reject identity reference name udpates.", func() {
97+
dummies.CSMachine1.Spec.IdentityRef.Name = "IdentityConfigMap"
98+
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
99+
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
100+
})
94101

95-
dummies.CSMachine1.DeepCopyInto(cloudStackMachineUpdate)
96-
cloudStackMachineUpdate.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
97-
Expect(k8sClient.Update(ctx, cloudStackMachineUpdate).Error()).Should(MatchRegexp(forbiddenRegex, "AffinityGroupIDs"))
102+
It("should reject udpates to the list of affinty groups.", func() {
103+
dummies.CSMachine1.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
104+
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
105+
Should(MatchRegexp(forbiddenRegex, "AffinityGroupIDs"))
98106
})
99107
})
100108
})

api/v1beta1/cloudstackmachinetemplate_webhook_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
6363
})
6464
})
6565

66-
Context("When updating a CloudStackMachine", func() {
67-
It("Should be rejected by the validating webhooks", func() {
66+
Context("When updating a CloudStackMachineTemplate", func() {
67+
It("Should be rejected by the machine template validating webhooks", func() {
6868
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).Should(Succeed())
6969

7070
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"

test/dummies/vars.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func SetDummyCSMachineTemplateVars() {
6363
Kind: "CloudStackMachineTemplate",
6464
},
6565
ObjectMeta: metav1.ObjectMeta{
66-
Name: "test-machinetemplate-2",
66+
Name: "test-machinetemplate-1",
6767
Namespace: "default",
6868
},
6969
Spec: infrav1.CloudStackMachineTemplateSpec{
@@ -74,7 +74,7 @@ func SetDummyCSMachineTemplateVars() {
7474
},
7575
Spec: infrav1.CloudStackMachineSpec{
7676
IdentityRef: &infrav1.CloudStackIdentityReference{
77-
Kind: "secret",
77+
Kind: "Secret",
7878
Name: "IdentitySecret",
7979
},
8080
Template: "Template",
@@ -101,7 +101,7 @@ func SetDummyCSMachineVars() {
101101
},
102102
Spec: infrav1.CloudStackMachineSpec{
103103
IdentityRef: &infrav1.CloudStackIdentityReference{
104-
Kind: "secret",
104+
Kind: "Secret",
105105
Name: "IdentitySecret",
106106
},
107107
Template: "Template",
@@ -131,8 +131,8 @@ func SetDummyCAPCClusterVars() {
131131
CSCluster = &infrav1.CloudStackCluster{
132132
Spec: infrav1.CloudStackClusterSpec{
133133
IdentityRef: &infrav1.CloudStackIdentityReference{
134-
Kind: "someName",
135-
Name: "someKind",
134+
Kind: "Secret",
135+
Name: "IdentitySecret",
136136
},
137137
Zones: []infrav1.Zone{Zone1, Zone2}},
138138
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)