@@ -18,85 +18,87 @@ package v1beta1_test
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
23
22
infrav1 "github.com/aws/cluster-api-provider-cloudstack/api/v1beta1"
24
23
"github.com/aws/cluster-api-provider-cloudstack/test/dummies"
25
24
. "github.com/onsi/ginkgo"
26
- . "github.com/onsi/ginkgo/extensions/table"
27
25
. "github.com/onsi/gomega"
26
+ "github.com/onsi/gomega/types"
28
27
)
29
28
30
- var _ = Describe ("CloudStackCluster webhooks" , func () {
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
+ }
31
34
35
+ var _ = Describe ("CloudStackCluster webhooks" , func () {
32
36
var ctx context.Context
37
+ forbiddenRegex := "admission webhook.*denied the request.*Forbidden\\ : %s"
38
+ requiredRegex := "admission webhook.*denied the request.*Required value\\ : %s"
33
39
34
40
BeforeEach (func () { // Reset test vars to initial state.
35
- dummies .SetDummyVars ()
36
41
ctx = context .Background ()
42
+ dummies .SetDummyVars () // Reset cluster var.
43
+ _ = k8sClient .Delete (ctx , dummies .CSCluster ) // Delete any remnants.
44
+ dummies .SetDummyVars () // Reset again since the k8s client can set this on delete.
37
45
})
38
46
39
- Context ("When creating a CloudStackCluster with all validated attributes " , func () {
40
- It ("Should succeed " , func () {
47
+ Context ("When creating a CloudStackCluster" , func () {
48
+ It ("Should accept a CloudStackCluster with all attributes present " , func () {
41
49
Ω (k8sClient .Create (ctx , dummies .CSCluster )).Should (Succeed ())
42
50
})
43
- })
44
51
45
- Context ( "When creating a CloudStackCluster with missing Network attribute" , func () {
46
- It ( "Should be rejected by the validating webhooks" , func () {
47
- Ω (k8sClient .Create (ctx , dummies .CSCluster ).Error ()).
48
- Should ( MatchRegexp ("admission webhook.*denied the request.*Required value \\ : Network" ))
52
+ It ( "Should reject a CloudStackCluster with missing Zones. Network attribute" , func () {
53
+ 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 " ))
49
56
})
50
- })
51
57
52
- Context ("When creating a CloudStackCluster with missing Zone attribute" , func () {
53
- It ("Should be rejected by the validating webhooks" , func () {
54
- Ω (k8sClient .Create (ctx , dummies .CSCluster ).Error ()).
55
- Should (MatchRegexp ("admission webhook.*denied the request.*Required value\\ : Zone" ))
58
+ It ("Should reject a CloudStackCluster with missing Zones attribute" , func () {
59
+ dummies .CSCluster .Spec .Zones = []infrav1.Zone {}
60
+ Ω (k8sClient .Create (ctx , dummies .CSCluster ).Error ()).Should (MatchRegexp (requiredRegex , "Zones" ))
56
61
})
57
- })
58
62
59
- Context ("When creating a CloudStackCluster with the wrong kind of IdentityReference" , func () {
60
- It ("Should be rejected by the validating webhooks" , func () {
61
- dummies .CSCluster .Spec .IdentityRef .Kind = "Wrong"
62
- Ω (k8sClient .Create (ctx , dummies .CSCluster ).Error ()).
63
- Should (MatchRegexp ("admission webhook.*denied the request.*Forbidden\\ : must be a Secret" ))
63
+ It ("Should reject a CloudStackCluster with IdentityRef not of kind 'Secret'" , func () {
64
+ dummies .CSCluster .Spec .IdentityRef .Kind = "ConfigMap"
65
+ Ω (k8sClient .Create (ctx , dummies .CSCluster ).Error ()).Should (MatchRegexp (forbiddenRegex , "must be a Secret" ))
64
66
})
65
67
})
66
68
67
69
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 ())
78
- forbiddenRegex := "admission webhook.*denied the request.*Forbidden \\ : %s"
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
- )
70
+ BeforeEach ( func () {
71
+ Ω ( k8sClient . Create ( ctx , dummies . CSCluster )). Should ( Succeed ())
72
+ })
73
+
74
+ It ( "Should reject updates to CloudStackCluster Zones " , func () {
75
+ dummies . CSCluster . Spec . Zones = []infrav1. Zone { dummies . Zone1 }
76
+ Ω ( k8sClient . Update ( ctx , dummies . CSCluster )). Should ( BeErrorAndMatchRegexp ( forbiddenRegex , "Zones and sub" ))
77
+ })
78
+ It ( "Should reject updates to CloudStackCluster Zones" , func ( ) {
79
+ dummies .CSCluster . Spec . Zones [ 0 ]. Network . Name = "ArbitraryUpdateNetworkName"
80
+ Ω ( k8sClient . Update ( ctx , dummies . CSCluster )). Should ( BeErrorAndMatchRegexp ( forbiddenRegex , "Zones and sub" ))
81
+ } )
82
+ It ( "Should reject updates to CloudStackCluster controlplaneendpoint.host" , func () {
83
+ dummies . CSCluster . Spec . ControlPlaneEndpoint . Host = "1.1.1.1"
84
+ Ω ( k8sClient . Update ( ctx , dummies . CSCluster )).
85
+ Should ( BeErrorAndMatchRegexp ( forbiddenRegex , "controlplaneendpoint \\ .host" ))
86
+ })
87
+
88
+ It ( "Should reject updates to CloudStackCluster controlplaneendpoint.port" , func () {
89
+ dummies . CSCluster . Spec . ControlPlaneEndpoint . Port = int32 ( 1234 )
90
+ Ω ( k8sClient . Update ( ctx , dummies . CSCluster )).
91
+ Should ( BeErrorAndMatchRegexp ( forbiddenRegex , "controlplaneendpoint \\ .port" ))
92
+ })
93
+ It ( "Should reject updates to the CloudStackCluster identity reference kind" , func () {
94
+ dummies . CSCluster . Spec .IdentityRef .Kind = "ConfigMap "
95
+ Ω ( k8sClient . Update ( ctx , dummies . CSCluster )).
96
+ Should ( BeErrorAndMatchRegexp ( forbiddenRegex , "identityRef\\ .Kind" ))
97
+ })
98
+ It ( "Should reject updates to the CloudStackCluster identity reference name" , func () {
99
+ dummies . CSCluster . Spec . IdentityRef . Name = "ConfigMap"
100
+ Ω ( k8sClient . Update ( ctx , dummies . CSCluster )).
101
+ Should ( BeErrorAndMatchRegexp ( forbiddenRegex , "identityRef \\ .name" ))
102
+ } )
101
103
})
102
104
})
0 commit comments