@@ -22,7 +22,6 @@ import (
22
22
. "github.com/onsi/ginkgo"
23
23
. "github.com/onsi/gomega"
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
- clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
26
25
)
27
26
28
27
var _ = Describe ("CloudStackCluster webhooks" , func () {
@@ -33,83 +32,59 @@ var _ = Describe("CloudStackCluster webhooks", func() {
33
32
clusterNamespace = "default"
34
33
clusterID = "0"
35
34
identitySecretName = "IdentitySecret"
36
- zone = "Zone"
35
+ zoneName = "Zone"
37
36
network = "Network"
38
37
)
39
38
39
+ var ( // Shared base test vars.
40
+ cloudStackCluster * CloudStackCluster
41
+ testZone1 Zone
42
+ testZone2 Zone
43
+ )
44
+
45
+ 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
+
66
+ })
67
+
40
68
Context ("When creating a CloudStackCluster with all validated attributes" , func () {
41
69
It ("Should succeed" , func () {
42
70
ctx := context .Background ()
43
- cloudStackCluster := & CloudStackCluster {
44
- TypeMeta : metav1.TypeMeta {
45
- APIVersion : apiVersion ,
46
- Kind : clusterKind ,
47
- },
48
- ObjectMeta : metav1.ObjectMeta {
49
- Name : clusterName ,
50
- Namespace : clusterNamespace ,
51
- UID : clusterID ,
52
- },
53
- Spec : CloudStackClusterSpec {
54
- IdentityRef : & CloudStackIdentityReference {
55
- Kind : defaultIdentityRefKind ,
56
- Name : identitySecretName ,
57
- },
58
- Zone : zone ,
59
- Network : network ,
60
- },
61
- }
62
- Expect (k8sClient .Create (ctx , cloudStackCluster )).Should (Succeed ())
71
+ Ω (k8sClient .Create (ctx , cloudStackCluster )).Should (Succeed ())
63
72
})
64
73
})
65
74
66
75
Context ("When creating a CloudStackCluster with missing Network attribute" , func () {
67
76
It ("Should be rejected by the validating webhooks" , func () {
68
77
ctx := context .Background ()
69
- cloudStackCluster := & CloudStackCluster {
70
- TypeMeta : metav1.TypeMeta {
71
- APIVersion : apiVersion ,
72
- Kind : clusterKind ,
73
- },
74
- ObjectMeta : metav1.ObjectMeta {
75
- Name : clusterName ,
76
- Namespace : clusterNamespace ,
77
- UID : clusterID ,
78
- },
79
- Spec : CloudStackClusterSpec {
80
- IdentityRef : & CloudStackIdentityReference {
81
- Kind : defaultIdentityRefKind ,
82
- Name : identitySecretName ,
83
- },
84
- Zone : zone ,
85
- },
86
- }
87
- Expect (k8sClient .Create (ctx , cloudStackCluster ).Error ()).Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Network" ))
78
+ Ω (k8sClient .Create (ctx , cloudStackCluster ).Error ()).
79
+ Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Network" ))
88
80
})
89
81
})
90
82
91
83
Context ("When creating a CloudStackCluster with missing Zone attribute" , func () {
92
84
It ("Should be rejected by the validating webhooks" , func () {
93
85
ctx := context .Background ()
94
- cloudStackCluster := & CloudStackCluster {
95
- TypeMeta : metav1.TypeMeta {
96
- APIVersion : apiVersion ,
97
- Kind : clusterKind ,
98
- },
99
- ObjectMeta : metav1.ObjectMeta {
100
- Name : clusterName ,
101
- Namespace : clusterNamespace ,
102
- UID : clusterID ,
103
- },
104
- Spec : CloudStackClusterSpec {
105
- IdentityRef : & CloudStackIdentityReference {
106
- Kind : defaultIdentityRefKind ,
107
- Name : identitySecretName ,
108
- },
109
- Network : network ,
110
- },
111
- }
112
- Expect (k8sClient .Create (ctx , cloudStackCluster ).Error ()).Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Zone" ))
86
+ Ω (k8sClient .Create (ctx , cloudStackCluster ).Error ()).
87
+ Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Zone" ))
113
88
})
114
89
})
115
90
@@ -121,26 +96,8 @@ var _ = Describe("CloudStackCluster webhooks", func() {
121
96
122
97
It ("Should be rejected by the validating webhooks" , func () {
123
98
ctx := context .Background ()
124
- cloudStackCluster := & CloudStackCluster {
125
- TypeMeta : metav1.TypeMeta {
126
- APIVersion : apiVersion ,
127
- Kind : clusterKind ,
128
- },
129
- ObjectMeta : metav1.ObjectMeta {
130
- Name : clusterName ,
131
- Namespace : clusterNamespace ,
132
- UID : clusterID ,
133
- },
134
- Spec : CloudStackClusterSpec {
135
- IdentityRef : & CloudStackIdentityReference {
136
- Kind : configMapKind ,
137
- Name : configMapName ,
138
- },
139
- Zone : zone ,
140
- Network : network ,
141
- },
142
- }
143
- Expect (k8sClient .Create (ctx , cloudStackCluster ).Error ()).Should (MatchRegexp ("admission webhook.*denied the request.*Forbidden\\ : must be a Secret" ))
99
+ Ω (k8sClient .Create (ctx , cloudStackCluster ).Error ()).
100
+ Should (MatchRegexp ("admission webhook.*denied the request.*Forbidden\\ : must be a Secret" ))
144
101
})
145
102
146
103
Context ("When updating a CloudStackCluster" , func () {
@@ -152,59 +109,44 @@ var _ = Describe("CloudStackCluster webhooks", func() {
152
109
153
110
BeforeEach (func () {
154
111
ctx = context .Background ()
155
- cloudStackCluster = & CloudStackCluster {
156
- TypeMeta : metav1.TypeMeta {
157
- APIVersion : "infrastructure.cluster.x-k8s.io/v1beta1" ,
158
- Kind : "CloudStackCluster" ,
159
- },
160
- ObjectMeta : metav1.ObjectMeta {
161
- Name : "test-cluster2" ,
162
- Namespace : clusterNamespace ,
163
- },
164
- Spec : CloudStackClusterSpec {
165
- IdentityRef : & CloudStackIdentityReference {
166
- Kind : defaultIdentityRefKind ,
167
- Name : identitySecretName ,
168
- },
169
- Zone : zone ,
170
- Network : network ,
171
- // Need CP Endpoint not to be nil before test or webhook will allow modification.
172
- ControlPlaneEndpoint : clusterv1.APIEndpoint {Host : "fakeIP" , Port : int32 (1234 )},
173
- },
174
- }
175
112
cloudStackClusterUpdate = & CloudStackCluster {}
176
113
})
177
114
178
115
It ("Should be rejected by the validating webhooks" , func () {
179
- Expect (k8sClient .Create (ctx , cloudStackCluster )).Should (Succeed ())
116
+ Ω (k8sClient .Create (ctx , cloudStackCluster )).Should (Succeed ())
180
117
181
118
forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\ : %s"
182
119
183
120
cloudStackCluster .DeepCopyInto (cloudStackClusterUpdate )
184
- cloudStackClusterUpdate .Spec .Zone = "Zone2"
185
- Expect (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).Should (MatchRegexp (forbiddenRegex , "zone" ))
121
+ cloudStackClusterUpdate .Spec .Zones = []Zone {testZone2 }
122
+ Ω (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).
123
+ Should (MatchRegexp (forbiddenRegex , "zone" ))
186
124
187
125
cloudStackCluster .DeepCopyInto (cloudStackClusterUpdate )
188
- cloudStackClusterUpdate .Spec .Network = "Network2"
189
- Expect (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).Should (MatchRegexp (forbiddenRegex , "network" ))
126
+ //cloudStackClusterUpdate.Spec.Network = "Network2"
127
+ Ω (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).
128
+ Should (MatchRegexp (forbiddenRegex , "network" ))
190
129
191
130
cloudStackCluster .DeepCopyInto (cloudStackClusterUpdate )
192
131
cloudStackClusterUpdate .Spec .ControlPlaneEndpoint .Host = "1.1.1.1"
193
- Expect (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).Should (MatchRegexp (forbiddenRegex , "controlplaneendpointhost" ))
132
+ Ω (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).
133
+ Should (MatchRegexp (forbiddenRegex , "controlplaneendpointhost" ))
194
134
195
135
cloudStackCluster .DeepCopyInto (cloudStackClusterUpdate )
196
136
cloudStackClusterUpdate .Spec .IdentityRef .Kind = "ConfigMap"
197
- Expect (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).Should (MatchRegexp (forbiddenRegex , "identityRef\\ .Kind" ))
137
+ Ω (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).
138
+ Should (MatchRegexp (forbiddenRegex , "identityRef\\ .Kind" ))
198
139
199
140
cloudStackCluster .DeepCopyInto (cloudStackClusterUpdate )
200
141
cloudStackClusterUpdate .Spec .IdentityRef .Name = configMapName
201
- Expect (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).Should (MatchRegexp (forbiddenRegex , "identityRef\\ .Name" ))
142
+ Ω (k8sClient .Update (ctx , cloudStackClusterUpdate ).Error ()).
143
+ Should (MatchRegexp (forbiddenRegex , "identityRef\\ .Name" ))
202
144
})
203
145
204
146
It ("Should reject changing the port" , func () {
205
147
cloudStackCluster .DeepCopyInto (cloudStackClusterUpdate )
206
148
cloudStackClusterUpdate .Spec .ControlPlaneEndpoint .Port = int32 (1234 )
207
- Expect (k8sClient .Update (ctx , cloudStackClusterUpdate )).ShouldNot (Succeed ()) //(forbiddenRegex, "controlplaneendpointport" ))
149
+ Ω (k8sClient .Update (ctx , cloudStackClusterUpdate )).ShouldNot (Succeed ())
208
150
})
209
151
})
210
152
})
0 commit comments