Skip to content

Commit feba8b8

Browse files
author
Joshua Reed
committed
Moved to use MatchErrror instead.
1 parent 201f829 commit feba8b8

File tree

5 files changed

+51
-50
lines changed

5 files changed

+51
-50
lines changed

api/v1beta1/cloudstackcluster_webhook.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ func (r *CloudStackCluster) ValidateUpdate(old runtime.Object) error {
105105
}
106106
if oldSpec.ControlPlaneEndpoint.Host != "" { // Need to allow one time endpoint setting via CAPC cluster controller.
107107
errorList = webhookutil.EnsureStringFieldsAreEqual(
108-
spec.ControlPlaneEndpoint.Host, oldSpec.ControlPlaneEndpoint.Host, "controlplaneendpointhost", errorList)
108+
spec.ControlPlaneEndpoint.Host, oldSpec.ControlPlaneEndpoint.Host, "controlplaneendpoint.host", errorList)
109109
errorList = webhookutil.EnsureStringFieldsAreEqual(
110-
string(spec.ControlPlaneEndpoint.Port), string(oldSpec.ControlPlaneEndpoint.Port), "controlplaneendpointport", errorList)
110+
string(spec.ControlPlaneEndpoint.Port), string(oldSpec.ControlPlaneEndpoint.Port),
111+
"controlplaneendpoint.port", errorList)
111112
}
112113
if spec.IdentityRef != nil && oldSpec.IdentityRef != nil {
113114
errorList = webhookutil.EnsureStringFieldsAreEqual(
114-
spec.IdentityRef.Kind, oldSpec.IdentityRef.Kind, "identityRef.Kind", errorList)
115-
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.IdentityRef.Name, oldSpec.IdentityRef.Name, "identityRef.Name", errorList)
115+
spec.IdentityRef.Kind, oldSpec.IdentityRef.Kind, "identityref.kind", errorList)
116+
errorList = webhookutil.EnsureStringFieldsAreEqual(spec.IdentityRef.Name, oldSpec.IdentityRef.Name,
117+
"identityref.name", errorList)
116118
}
117119

118120
// IdentityRefs must be Secrets.

api/v1beta1/cloudstackcluster_webhook_test.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ import (
2323
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
2424
. "github.com/onsi/ginkgo"
2525
. "github.com/onsi/gomega"
26-
"github.com/onsi/gomega/types"
2726
)
2827

29-
var errString = func(err error) string { return err.Error() }
30-
31-
func BeErrorAndMatchRegexp(regexp string, args ...interface{}) types.GomegaMatcher {
32-
return SatisfyAll(HaveOccurred(), WithTransform(errString, MatchRegexp(regexp, args)))
33-
}
34-
3528
var _ = Describe("CloudStackCluster webhooks", func() {
3629
var ctx context.Context
3730
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
@@ -51,18 +44,19 @@ var _ = Describe("CloudStackCluster webhooks", func() {
5144

5245
It("Should reject a CloudStackCluster with missing Zones.Network attribute", func() {
5346
dummies.CSCluster.Spec.Zones = []infrav1.Zone{{Name: "ZoneWNoNetwork"}}
54-
Ω(k8sClient.Create(ctx, dummies.CSCluster).Error()).Should(
55-
MatchRegexp(requiredRegex, "each Zone requires a Network specification"))
47+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(
48+
MatchError(MatchRegexp(requiredRegex, "each Zone requires a Network specification")))
5649
})
5750

5851
It("Should reject a CloudStackCluster with missing Zones attribute", func() {
5952
dummies.CSCluster.Spec.Zones = []infrav1.Zone{}
60-
Ω(k8sClient.Create(ctx, dummies.CSCluster).Error()).Should(MatchRegexp(requiredRegex, "Zones"))
53+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(MatchError(MatchRegexp(requiredRegex, "Zones")))
6154
})
6255

6356
It("Should reject a CloudStackCluster with IdentityRef not of kind 'Secret'", func() {
6457
dummies.CSCluster.Spec.IdentityRef.Kind = "ConfigMap"
65-
Ω(k8sClient.Create(ctx, dummies.CSCluster).Error()).Should(MatchRegexp(forbiddenRegex, "must be a Secret"))
58+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).
59+
Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret")))
6660
})
6761
})
6862

@@ -73,32 +67,32 @@ var _ = Describe("CloudStackCluster webhooks", func() {
7367

7468
It("Should reject updates to CloudStackCluster Zones", func() {
7569
dummies.CSCluster.Spec.Zones = []infrav1.Zone{dummies.Zone1}
76-
Ω(k8sClient.Update(ctx, dummies.CSCluster)).Should(BeErrorAndMatchRegexp(forbiddenRegex, "Zones and sub"))
70+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).Should(MatchError(MatchRegexp(forbiddenRegex, "Zones and sub")))
7771
})
7872
It("Should reject updates to CloudStackCluster Zones", func() {
7973
dummies.CSCluster.Spec.Zones[0].Network.Name = "ArbitraryUpdateNetworkName"
80-
Ω(k8sClient.Update(ctx, dummies.CSCluster)).Should(BeErrorAndMatchRegexp(forbiddenRegex, "Zones and sub"))
74+
Ω(k8sClient.Update(ctx, dummies.CSCluster)).Should(MatchError(MatchRegexp(forbiddenRegex, "Zones and sub")))
8175
})
8276
It("Should reject updates to CloudStackCluster controlplaneendpoint.host", func() {
8377
dummies.CSCluster.Spec.ControlPlaneEndpoint.Host = "1.1.1.1"
8478
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
85-
Should(BeErrorAndMatchRegexp(forbiddenRegex, "controlplaneendpoint\\.host"))
79+
Should(MatchError(MatchRegexp(forbiddenRegex, "controlplaneendpoint\\.host")))
8680
})
8781

8882
It("Should reject updates to CloudStackCluster controlplaneendpoint.port", func() {
8983
dummies.CSCluster.Spec.ControlPlaneEndpoint.Port = int32(1234)
9084
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
91-
Should(BeErrorAndMatchRegexp(forbiddenRegex, "controlplaneendpoint\\.port"))
85+
Should(MatchError(MatchRegexp(forbiddenRegex, "controlplaneendpoint\\.port")))
9286
})
9387
It("Should reject updates to the CloudStackCluster identity reference kind", func() {
9488
dummies.CSCluster.Spec.IdentityRef.Kind = "ConfigMap"
9589
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
96-
Should(BeErrorAndMatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
90+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityref\\.kind")))
9791
})
9892
It("Should reject updates to the CloudStackCluster identity reference name", func() {
9993
dummies.CSCluster.Spec.IdentityRef.Name = "ConfigMap"
10094
Ω(k8sClient.Update(ctx, dummies.CSCluster)).
101-
Should(BeErrorAndMatchRegexp(forbiddenRegex, "identityRef\\.name"))
95+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityref\\.name")))
10296
})
10397
})
10498
})

api/v1beta1/cloudstackmachine_webhook_test.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,20 @@ var _ = Describe("CloudStackMachine webhook", func() {
4242

4343
It("should reject a CloudStackMachine with missing Offering attribute", func() {
4444
dummies.CSMachine1.Spec.Offering = ""
45-
Expect(k8sClient.Create(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(requiredRegex, "Offering"))
45+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
46+
Should(MatchError(MatchRegexp(requiredRegex, "Offering")))
4647
})
4748

4849
It("should reject a CloudStackMachine with missing Template attribute", func() {
4950
dummies.CSMachine1.Spec.Template = ""
50-
Expect(k8sClient.Create(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(requiredRegex, "Template"))
51+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
52+
Should(MatchError(MatchRegexp(requiredRegex, "Template")))
5153
})
5254

5355
It("should reject a CloudStackMachine with IdentityRef not of kind 'Secret'", func() {
5456
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
55-
Expect(k8sClient.Create(ctx, dummies.CSMachine1).Error()).
56-
Should(MatchRegexp(forbiddenRegex, "must be a Secret"))
57+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
58+
Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret")))
5759
})
5860
})
5961

@@ -64,35 +66,38 @@ var _ = Describe("CloudStackMachine webhook", func() {
6466

6567
It("should reject VM offering updates to the CloudStackMachine", func() {
6668
dummies.CSMachine1.Spec.Offering = "ArbitraryUpdateOffering"
67-
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "offering"))
69+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
70+
Should(MatchError(MatchRegexp(forbiddenRegex, "offering")))
6871
})
6972

7073
It("should reject VM template updates to the CloudStackMachine", func() {
7174
dummies.CSMachine1.Spec.Template = "ArbitraryUpdateTemplate"
72-
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "template"))
75+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
76+
Should(MatchError(MatchRegexp(forbiddenRegex, "template")))
7377
})
7478

7579
It("should reject updates to VM details of the CloudStackMachine", func() {
7680
dummies.CSMachine1.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
77-
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).Should(MatchRegexp(forbiddenRegex, "details"))
81+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
82+
Should(MatchError(MatchRegexp(forbiddenRegex, "details")))
7883
})
7984

8085
It("should reject identity reference kind udpates to the CloudStackMachine", func() {
8186
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
82-
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
83-
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
87+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
88+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Kind")))
8489
})
8590

8691
It("should reject identity reference name udpates to the CloudStackMachine", func() {
8792
dummies.CSMachine1.Spec.IdentityRef.Name = "IdentityConfigMap"
88-
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
89-
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
93+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
94+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Name")))
9095
})
9196

9297
It("should reject udpates to the list of affinty groups of the CloudStackMachine", func() {
9398
dummies.CSMachine1.Spec.AffinityGroupIDs = []string{"28b907b8-75a7-4214-bd3d-6c61961fc2af"}
94-
Ω(k8sClient.Update(ctx, dummies.CSMachine1).Error()).
95-
Should(MatchRegexp(forbiddenRegex, "AffinityGroupIDs"))
99+
Ω(k8sClient.Update(ctx, dummies.CSMachine1)).
100+
Should(MatchError(MatchRegexp(forbiddenRegex, "AffinityGroupIDs")))
96101
})
97102
})
98103
})

api/v1beta1/cloudstackmachinetemplate_webhook_test.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
4242

4343
It("Should reject a CloudStackMachineTemplate when missing the VM Offering attribute", func() {
4444
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = ""
45-
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1).Error()).
46-
Should(MatchRegexp(requiredRegex, "Offering"))
45+
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).
46+
Should(MatchError(MatchRegexp(requiredRegex, "Offering")))
4747
})
4848

4949
It("Should reject a CloudStackMachineTemplate when missing the VM Template attribute", func() {
5050
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = ""
51-
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1).Error()).
52-
Should(MatchRegexp(requiredRegex, "Template"))
51+
Expect(k8sClient.Create(ctx, dummies.CSMachineTemplate1)).
52+
Should(MatchError(MatchRegexp(requiredRegex, "Template")))
5353
})
5454

5555
It("should reject a CloudStackMachineTemplate with IdentityRef not of kind 'Secret'", func() {
5656
dummies.CSMachine1.Spec.IdentityRef.Kind = "ConfigMap"
57-
Expect(k8sClient.Create(ctx, dummies.CSMachine1).Error()).
58-
Should(MatchRegexp(forbiddenRegex, "must be a Secret"))
57+
Expect(k8sClient.Create(ctx, dummies.CSMachine1)).
58+
Should(MatchError(MatchRegexp(forbiddenRegex, "must be a Secret")))
5959
})
6060
})
6161

@@ -66,29 +66,32 @@ var _ = Describe("CloudStackMachineTemplate webhook", func() {
6666

6767
It("should reject VM template updates to the CloudStackMachineTemplate", func() {
6868
dummies.CSMachineTemplate1.Spec.Spec.Spec.Template = "ArbitraryUpdateTemplate"
69-
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).Should(MatchRegexp(forbiddenRegex, "template"))
69+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
70+
Should(MatchError(MatchRegexp(forbiddenRegex, "template")))
7071
})
7172

7273
It("should reject VM offering updates to the CloudStackMachineTemplate", func() {
7374
dummies.CSMachineTemplate1.Spec.Spec.Spec.Offering = "Offering2"
74-
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).Should(MatchRegexp(forbiddenRegex, "offering"))
75+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
76+
Should(MatchError(MatchRegexp(forbiddenRegex, "offering")))
7577
})
7678

7779
It("should reject updates to VM details of the CloudStackMachineTemplate", func() {
7880
dummies.CSMachineTemplate1.Spec.Spec.Spec.Details = map[string]string{"memoryOvercommitRatio": "1.5"}
79-
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).Should(MatchRegexp(forbiddenRegex, "details"))
81+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
82+
Should(MatchError(MatchRegexp(forbiddenRegex, "details")))
8083
})
8184

8285
It("should reject identity reference kind updates to the CloudStackMachineTemplate", func() {
8386
dummies.CSMachineTemplate1.Spec.Spec.Spec.IdentityRef.Kind = "configMap"
84-
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).
85-
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
87+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
88+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Kind")))
8689
})
8790

8891
It("should reject identity reference name updates to the CloudStackMachineTemplate", func() {
8992
dummies.CSMachineTemplate1.Spec.Spec.Spec.IdentityRef.Name = "IDentityConfigMap"
90-
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1).Error()).
91-
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
93+
Ω(k8sClient.Update(ctx, dummies.CSMachineTemplate1)).
94+
Should(MatchError(MatchRegexp(forbiddenRegex, "identityRef\\.Name")))
9295
})
9396

9497
It("should reject updates to the list of AffinityGroupIDs of the CloudStackMachineTemplate", func() {

pkg/cloud/network.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ const (
4444
NetworkTypeIsolated = "Isolated"
4545
NetworkTypeShared = "Shared"
4646
NetworkProtocolTCP = "tcp"
47-
addCreatedByTag = true
48-
49-
// doNotAddCreatedByTag = false
5047
)
5148

5249
// usesIsolatedNetwork returns true if this cluster is specs an isolated network.

0 commit comments

Comments
 (0)