@@ -14,140 +14,89 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package v1beta1
17
+ package v1beta1_test
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
23
+ infrav1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
24
+ "github.com/aws/cluster-api-provider-cloudstack/test/dummies"
22
25
. "github.com/onsi/ginkgo"
26
+ . "github.com/onsi/ginkgo/extensions/table"
23
27
. "github.com/onsi/gomega"
24
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
28
)
26
29
27
30
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
- )
38
31
39
- var ( // Shared base test vars.
40
- cloudStackCluster * CloudStackCluster
41
- testZone1 Zone
42
- testZone2 Zone
43
- )
32
+ var ctx context.Context
44
33
45
34
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 ()
66
37
})
67
38
68
39
Context ("When creating a CloudStackCluster with all validated attributes" , func () {
69
40
It ("Should succeed" , func () {
70
- ctx := context .Background ()
71
- Ω (k8sClient .Create (ctx , cloudStackCluster )).Should (Succeed ())
41
+ Ω (k8sClient .Create (ctx , dummies .CSCluster )).Should (Succeed ())
72
42
})
73
43
})
74
44
75
45
Context ("When creating a CloudStackCluster with missing Network attribute" , func () {
76
46
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 ()).
79
48
Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Network" ))
80
49
})
81
50
})
82
51
83
52
Context ("When creating a CloudStackCluster with missing Zone attribute" , func () {
84
53
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 ()).
87
55
Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Zone" ))
88
56
})
89
57
})
90
58
91
59
Context ("When creating a CloudStackCluster with the wrong kind of IdentityReference" , func () {
92
- const (
93
- configMapKind = "ConfigMap"
94
- configMapName = "IdentityConfigMap"
95
- )
96
-
97
60
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 ()).
100
63
Should (MatchRegexp ("admission webhook.*denied the request.*Forbidden\\ : must be a Secret" ))
101
64
})
65
+ })
102
66
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 ())
118
78
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
+ )
152
101
})
153
102
})
0 commit comments