Skip to content

Commit 2378498

Browse files
author
Joshua Reed
committed
Updated webhook tests to use/reuse dummy vars.
1 parent 3f5d4a7 commit 2378498

File tree

5 files changed

+132
-281
lines changed

5 files changed

+132
-281
lines changed

api/v1beta1/cloudstackcluster_webhook_test.go

Lines changed: 47 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -14,140 +14,89 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package v1beta1
17+
package v1beta1_test
1818

1919
import (
2020
"context"
21+
"fmt"
2122

23+
infrav1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
24+
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
2225
. "github.com/onsi/ginkgo"
26+
. "github.com/onsi/ginkgo/extensions/table"
2327
. "github.com/onsi/gomega"
24-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2528
)
2629

2730
var _ = Describe("CloudStackCluster webhooks", func() {
28-
const (
29-
apiVersion = "infrastructure.cluster.x-k8s.io/v1beta1"
30-
clusterKind = "CloudStackCluster"
31-
clusterName = "test-cluster"
32-
clusterNamespace = "default"
33-
clusterID = "0"
34-
identitySecretName = "IdentitySecret"
35-
zoneName = "Zone"
36-
network = "Network"
37-
)
3831

39-
var ( // Shared base test vars.
40-
cloudStackCluster *CloudStackCluster
41-
testZone1 Zone
42-
testZone2 Zone
43-
)
32+
var ctx context.Context
4433

4534
BeforeEach(func() { // Reset test vars to initial state.
46-
testZone1 = Zone{Name: zoneName}
47-
testZone2 = Zone{Name: zoneName}
48-
cloudStackCluster = &CloudStackCluster{
49-
TypeMeta: metav1.TypeMeta{
50-
APIVersion: apiVersion,
51-
Kind: clusterKind,
52-
},
53-
ObjectMeta: metav1.ObjectMeta{
54-
Name: clusterName,
55-
Namespace: clusterNamespace,
56-
},
57-
Spec: CloudStackClusterSpec{
58-
IdentityRef: &CloudStackIdentityReference{
59-
Kind: defaultIdentityRefKind,
60-
Name: identitySecretName,
61-
},
62-
Zones: []Zone{testZone1, testZone2},
63-
},
64-
}
65-
35+
dummies.SetDummyVars()
36+
ctx = context.Background()
6637
})
6738

6839
Context("When creating a CloudStackCluster with all validated attributes", func() {
6940
It("Should succeed", func() {
70-
ctx := context.Background()
71-
Ω(k8sClient.Create(ctx, cloudStackCluster)).Should(Succeed())
41+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(Succeed())
7242
})
7343
})
7444

7545
Context("When creating a CloudStackCluster with missing Network attribute", func() {
7646
It("Should be rejected by the validating webhooks", func() {
77-
ctx := context.Background()
78-
Ω(k8sClient.Create(ctx, cloudStackCluster).Error()).
47+
Ω(k8sClient.Create(ctx, dummies.CSCluster).Error()).
7948
Should(MatchRegexp("admission webhook.*denied the request.*Required value\\: Network"))
8049
})
8150
})
8251

8352
Context("When creating a CloudStackCluster with missing Zone attribute", func() {
8453
It("Should be rejected by the validating webhooks", func() {
85-
ctx := context.Background()
86-
Ω(k8sClient.Create(ctx, cloudStackCluster).Error()).
54+
Ω(k8sClient.Create(ctx, dummies.CSCluster).Error()).
8755
Should(MatchRegexp("admission webhook.*denied the request.*Required value\\: Zone"))
8856
})
8957
})
9058

9159
Context("When creating a CloudStackCluster with the wrong kind of IdentityReference", func() {
92-
const (
93-
configMapKind = "ConfigMap"
94-
configMapName = "IdentityConfigMap"
95-
)
96-
9760
It("Should be rejected by the validating webhooks", func() {
98-
ctx := context.Background()
99-
Ω(k8sClient.Create(ctx, cloudStackCluster).Error()).
61+
dummies.CSCluster.Spec.IdentityRef.Kind = "Wrong"
62+
Ω(k8sClient.Create(ctx, dummies.CSCluster).Error()).
10063
Should(MatchRegexp("admission webhook.*denied the request.*Forbidden\\: must be a Secret"))
10164
})
65+
})
10266

103-
Context("When updating a CloudStackCluster", func() {
104-
var (
105-
ctx context.Context
106-
cloudStackCluster *CloudStackCluster
107-
cloudStackClusterUpdate *CloudStackCluster
108-
)
109-
110-
BeforeEach(func() {
111-
ctx = context.Background()
112-
cloudStackClusterUpdate = &CloudStackCluster{}
113-
})
114-
115-
It("Should be rejected by the validating webhooks", func() {
116-
Ω(k8sClient.Create(ctx, cloudStackCluster)).Should(Succeed())
117-
67+
Context("When updating a CloudStackCluster", func() {
68+
type CSClusterModFunc func(*infrav1.CloudStackCluster)
69+
description := func() func(string, CSClusterModFunc) string {
70+
return func(field string, mod CSClusterModFunc) string {
71+
return fmt.Sprintf(
72+
"CloudStackCluster.Spec %s modification should be rejected by the validating webhooks", field)
73+
}
74+
}
75+
DescribeTable("Forbidden field modification",
76+
func(field string, mod CSClusterModFunc) {
77+
Ω(k8sClient.Create(ctx, dummies.CSCluster)).Should(Succeed())
11878
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\: %s"
119-
120-
cloudStackCluster.DeepCopyInto(cloudStackClusterUpdate)
121-
cloudStackClusterUpdate.Spec.Zones = []Zone{testZone2}
122-
Ω(k8sClient.Update(ctx, cloudStackClusterUpdate).Error()).
123-
Should(MatchRegexp(forbiddenRegex, "zone"))
124-
125-
cloudStackCluster.DeepCopyInto(cloudStackClusterUpdate)
126-
//cloudStackClusterUpdate.Spec.Network = "Network2"
127-
Ω(k8sClient.Update(ctx, cloudStackClusterUpdate).Error()).
128-
Should(MatchRegexp(forbiddenRegex, "network"))
129-
130-
cloudStackCluster.DeepCopyInto(cloudStackClusterUpdate)
131-
cloudStackClusterUpdate.Spec.ControlPlaneEndpoint.Host = "1.1.1.1"
132-
Ω(k8sClient.Update(ctx, cloudStackClusterUpdate).Error()).
133-
Should(MatchRegexp(forbiddenRegex, "controlplaneendpointhost"))
134-
135-
cloudStackCluster.DeepCopyInto(cloudStackClusterUpdate)
136-
cloudStackClusterUpdate.Spec.IdentityRef.Kind = "ConfigMap"
137-
Ω(k8sClient.Update(ctx, cloudStackClusterUpdate).Error()).
138-
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Kind"))
139-
140-
cloudStackCluster.DeepCopyInto(cloudStackClusterUpdate)
141-
cloudStackClusterUpdate.Spec.IdentityRef.Name = configMapName
142-
Ω(k8sClient.Update(ctx, cloudStackClusterUpdate).Error()).
143-
Should(MatchRegexp(forbiddenRegex, "identityRef\\.Name"))
144-
})
145-
146-
It("Should reject changing the port", func() {
147-
cloudStackCluster.DeepCopyInto(cloudStackClusterUpdate)
148-
cloudStackClusterUpdate.Spec.ControlPlaneEndpoint.Port = int32(1234)
149-
Ω(k8sClient.Update(ctx, cloudStackClusterUpdate)).ShouldNot(Succeed())
150-
})
151-
})
79+
mod(dummies.CSCluster)
80+
Ω(k8sClient.Update(ctx, dummies.CSCluster).Error()).Should(MatchRegexp(forbiddenRegex, field))
81+
},
82+
Entry(description(), "zone", func(CSC *infrav1.CloudStackCluster) {
83+
CSC.Spec.Zones = []infrav1.Zone{dummies.Zone1}
84+
}),
85+
Entry(description(), "zonenetwork", func(CSC *infrav1.CloudStackCluster) {
86+
CSC.Spec.Zones[0].Network.Name = "ArbitraryNetworkName"
87+
}),
88+
Entry(description(), "controlplaneendpoint\\.host", func(CSC *infrav1.CloudStackCluster) {
89+
CSC.Spec.ControlPlaneEndpoint.Host = "1.1.1.1"
90+
}),
91+
Entry(description(), "identityRef\\.Kind", func(CSC *infrav1.CloudStackCluster) {
92+
CSC.Spec.IdentityRef.Kind = "ArbitraryKind"
93+
}),
94+
Entry(description(), "identityRef\\.Name", func(CSC *infrav1.CloudStackCluster) {
95+
CSC.Spec.IdentityRef.Name = "ArbitraryName"
96+
}),
97+
Entry(description(), "controlplaneendpoint\\.port", func(CSC *infrav1.CloudStackCluster) {
98+
CSC.Spec.ControlPlaneEndpoint.Port = int32(1234)
99+
}),
100+
)
152101
})
153102
})

0 commit comments

Comments
 (0)