@@ -23,142 +23,158 @@ import (
2323 "k8s.io/apimachinery/pkg/types"
2424 "k8s.io/utils/pointer"
2525 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
26+ "sigs.k8s.io/controller-runtime/pkg/client"
2627
2728 infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
2829)
2930
3031var _ = Describe ("OpenStackCluster API validations" , func () {
31- var cluster * infrav1.OpenStackCluster
3232 var namespace * corev1.Namespace
3333
34- BeforeEach (func () {
35- namespace = createNamespace ()
36-
37- // Initialise a basic cluster object in the correct namespace
38- cluster = & infrav1.OpenStackCluster {}
39- cluster .Namespace = namespace .Name
40- cluster .GenerateName = "cluster-"
41- })
42-
43- It ("should allow the smallest permissible cluster spec" , func () {
44- Expect (k8sClient .Create (ctx , cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
45- })
46-
47- It ("should only allow controlPlaneEndpoint to be set once" , func () {
48- By ("Creating a bare cluster" )
49- Expect (k8sClient .Create (ctx , cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
50-
51- By ("Setting the control plane endpoint" )
52- cluster .Spec .ControlPlaneEndpoint = & clusterv1.APIEndpoint {
53- Host : "foo" ,
54- Port : 1234 ,
34+ create := func (obj client.Object ) error {
35+ err := k8sClient .Create (ctx , obj )
36+ if err == nil {
37+ DeferCleanup (func () error {
38+ return k8sClient .Delete (ctx , obj )
39+ })
5540 }
56- Expect (k8sClient .Update (ctx , cluster )).To (Succeed (), "Setting control plane endpoint should succeed" )
57-
58- By ("Modifying the control plane endpoint" )
59- cluster .Spec .ControlPlaneEndpoint .Host = "bar"
60- Expect (k8sClient .Update (ctx , cluster )).NotTo (Succeed (), "Updating control plane endpoint should fail" )
61- })
62-
63- It ("should allow an empty managed security groups definition" , func () {
64- cluster .Spec .ManagedSecurityGroups = & infrav1.ManagedSecurityGroups {}
65- Expect (k8sClient .Create (ctx , cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
66- })
67-
68- It ("should default enabled to true if APIServerLoadBalancer is specified without enabled=true" , func () {
69- cluster .Spec .APIServerLoadBalancer = & infrav1.APIServerLoadBalancer {}
70- Expect (k8sClient .Create (ctx , cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
71-
72- // Fetch the cluster and check the defaulting
73- fetchedCluster := & infrav1.OpenStackCluster {}
74- Expect (k8sClient .Get (ctx , types.NamespacedName {Name : cluster .Name , Namespace : cluster .Namespace }, fetchedCluster )).To (Succeed (), "OpenStackCluster fetch should succeed" )
75-
76- Expect (fetchedCluster .Spec .APIServerLoadBalancer .Enabled ).ToNot (BeNil (), "APIServerLoadBalancer.Enabled should have been defaulted" )
77- Expect (* fetchedCluster .Spec .APIServerLoadBalancer .Enabled ).To (BeTrue (), "APIServerLoadBalancer.Enabled should default to true" )
78- })
79-
80- It ("should not default APIServerLoadBalancer if it is not specifid" , func () {
81- Expect (k8sClient .Create (ctx , cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
82-
83- // Fetch the cluster and check the defaulting
84- fetchedCluster := & infrav1.OpenStackCluster {}
85- Expect (k8sClient .Get (ctx , types.NamespacedName {Name : cluster .Name , Namespace : cluster .Namespace }, fetchedCluster )).To (Succeed (), "OpenStackCluster fetch should succeed" )
41+ return err
42+ }
8643
87- Expect ( fetchedCluster . Spec . APIServerLoadBalancer ). To ( BeNil (), "APIServerLoadBalancer should not have been defaulted" )
88- Expect ( fetchedCluster . Spec . APIServerLoadBalancer . IsEnabled ()). To ( BeFalse (), "APIServerLoadBalancer.Enabled should not have been defaulted" )
44+ BeforeEach ( func () {
45+ namespace = createNamespace ( )
8946 })
9047
91- It ("should allow bastion.enabled=true with a spec" , func () {
92- cluster .Spec .Bastion = & infrav1.Bastion {
93- Enabled : pointer .Bool (true ),
94- Spec : & infrav1.OpenStackMachineSpec {
95- Image : infrav1.ImageParam {
96- Filter : & infrav1.ImageFilter {
97- Name : pointer .String ("fake-image" ),
48+ Context ("infrav1" , func () {
49+ var cluster * infrav1.OpenStackCluster
50+
51+ BeforeEach (func () {
52+ // Initialise a basic cluster object in the correct namespace
53+ cluster = & infrav1.OpenStackCluster {}
54+ cluster .Namespace = namespace .Name
55+ cluster .GenerateName = "cluster-"
56+ })
57+
58+ It ("should allow the smallest permissible cluster spec" , func () {
59+ Expect (create (cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
60+ })
61+
62+ It ("should only allow controlPlaneEndpoint to be set once" , func () {
63+ By ("Creating a bare cluster" )
64+ Expect (create (cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
65+
66+ By ("Setting the control plane endpoint" )
67+ cluster .Spec .ControlPlaneEndpoint = & clusterv1.APIEndpoint {
68+ Host : "foo" ,
69+ Port : 1234 ,
70+ }
71+ Expect (k8sClient .Update (ctx , cluster )).To (Succeed (), "Setting control plane endpoint should succeed" )
72+
73+ By ("Modifying the control plane endpoint" )
74+ cluster .Spec .ControlPlaneEndpoint .Host = "bar"
75+ Expect (k8sClient .Update (ctx , cluster )).NotTo (Succeed (), "Updating control plane endpoint should fail" )
76+ })
77+
78+ It ("should allow an empty managed security groups definition" , func () {
79+ cluster .Spec .ManagedSecurityGroups = & infrav1.ManagedSecurityGroups {}
80+ Expect (create (cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
81+ })
82+
83+ It ("should default enabled to true if APIServerLoadBalancer is specified without enabled=true" , func () {
84+ cluster .Spec .APIServerLoadBalancer = & infrav1.APIServerLoadBalancer {}
85+ Expect (create (cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
86+
87+ // Fetch the cluster and check the defaulting
88+ fetchedCluster := & infrav1.OpenStackCluster {}
89+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : cluster .Name , Namespace : cluster .Namespace }, fetchedCluster )).To (Succeed (), "OpenStackCluster fetch should succeed" )
90+
91+ Expect (fetchedCluster .Spec .APIServerLoadBalancer .Enabled ).ToNot (BeNil (), "APIServerLoadBalancer.Enabled should have been defaulted" )
92+ Expect (* fetchedCluster .Spec .APIServerLoadBalancer .Enabled ).To (BeTrue (), "APIServerLoadBalancer.Enabled should default to true" )
93+ })
94+
95+ It ("should not default APIServerLoadBalancer if it is not specifid" , func () {
96+ Expect (create (cluster )).To (Succeed (), "OpenStackCluster creation should succeed" )
97+
98+ // Fetch the cluster and check the defaulting
99+ fetchedCluster := & infrav1.OpenStackCluster {}
100+ Expect (k8sClient .Get (ctx , types.NamespacedName {Name : cluster .Name , Namespace : cluster .Namespace }, fetchedCluster )).To (Succeed (), "OpenStackCluster fetch should succeed" )
101+
102+ Expect (fetchedCluster .Spec .APIServerLoadBalancer ).To (BeNil (), "APIServerLoadBalancer should not have been defaulted" )
103+ Expect (fetchedCluster .Spec .APIServerLoadBalancer .IsEnabled ()).To (BeFalse (), "APIServerLoadBalancer.Enabled should not have been defaulted" )
104+ })
105+
106+ It ("should allow bastion.enabled=true with a spec" , func () {
107+ cluster .Spec .Bastion = & infrav1.Bastion {
108+ Enabled : pointer .Bool (true ),
109+ Spec : & infrav1.OpenStackMachineSpec {
110+ Image : infrav1.ImageParam {
111+ Filter : & infrav1.ImageFilter {
112+ Name : pointer .String ("fake-image" ),
113+ },
98114 },
99115 },
100- },
101- }
102- Expect ( k8sClient . Create ( ctx , cluster )). To ( Succeed (), "OpenStackCluster creation should succeed" )
103- })
104-
105- It ( "should not allow bastion.enabled=true without a spec" , func () {
106- cluster . Spec . Bastion = & infrav1. Bastion {
107- Enabled : pointer . Bool ( true ),
108- }
109- Expect ( k8sClient . Create ( ctx , cluster )). NotTo ( Succeed (), "OpenStackCluster creation should not succeed" )
110- })
111-
112- It ( "should not allow an empty Bastion" , func () {
113- cluster . Spec . Bastion = & infrav1. Bastion {}
114- Expect ( k8sClient . Create ( ctx , cluster )). NotTo ( Succeed (), "OpenStackCluster creation should not succeed" )
115- })
116-
117- It ( "should default bastion.enabled=true" , func () {
118- cluster . Spec . Bastion = & infrav1.Bastion {
119- Spec : & infrav1.OpenStackMachineSpec {
120- Image : infrav1.ImageParam {
121- Filter : & infrav1. ImageFilter {
122- Name : pointer . String ( "fake-image" ) ,
116+ }
117+ Expect ( create ( cluster )). To ( Succeed (), "OpenStackCluster creation should succeed" )
118+ } )
119+
120+ It ( "should not allow bastion.enabled=true without a spec" , func () {
121+ cluster . Spec . Bastion = & infrav1. Bastion {
122+ Enabled : pointer . Bool ( true ),
123+ }
124+ Expect ( create ( cluster )). NotTo ( Succeed (), "OpenStackCluster creation should not succeed" )
125+ } )
126+
127+ It ( "should not allow an empty Bastion" , func () {
128+ cluster . Spec . Bastion = & infrav1. Bastion {}
129+ Expect ( create ( cluster )). NotTo ( Succeed (), "OpenStackCluster creation should not succeed" )
130+ } )
131+
132+ It ( "should default bastion.enabled=true" , func () {
133+ cluster . Spec . Bastion = & infrav1. Bastion {
134+ Spec : & infrav1.OpenStackMachineSpec {
135+ Image : infrav1.ImageParam {
136+ Filter : & infrav1.ImageFilter {
137+ Name : pointer . String ( "fake-image" ),
138+ } ,
123139 },
124140 },
125- },
126- }
127- Expect ( k8sClient . Create ( ctx , cluster )). To ( Succeed (), "OpenStackCluster creation should not succeed" )
128-
129- // Fetch the cluster and check the defaulting
130- fetchedCluster := & infrav1. OpenStackCluster {}
131- Expect (k8sClient . Get ( ctx , types. NamespacedName { Name : cluster . Name , Namespace : cluster . Namespace }, fetchedCluster )). To ( Succeed (), "OpenStackCluster fetch should succeed " )
132- Expect (fetchedCluster .Spec .Bastion .Enabled ).ToNot ( BeNil (), "Bastion.Enabled should have been defaulted" )
133- Expect ( * fetchedCluster . Spec . Bastion . Enabled ). To ( BeTrueBecause ( "Bastion.Enabled should default to true" ) )
134- })
135-
136- It ( "should allow IPv4 as bastion floatingIP" , func () {
137- cluster . Spec . Bastion = & infrav1. Bastion {
138- Enabled : pointer . Bool ( true ),
139- Spec : & infrav1.OpenStackMachineSpec {
140- Image : infrav1.ImageParam {
141- Filter : & infrav1. ImageFilter {
142- Name : pointer . String ( "fake-image" ) ,
141+ }
142+ Expect ( create ( cluster )). To ( Succeed (), "OpenStackCluster creation should not succeed" )
143+
144+ // Fetch the cluster and check the defaulting
145+ fetchedCluster := & infrav1. OpenStackCluster {}
146+ Expect ( k8sClient . Get ( ctx , types. NamespacedName { Name : cluster . Name , Namespace : cluster . Namespace }, fetchedCluster )). To ( Succeed (), "OpenStackCluster fetch should succeed" )
147+ Expect (fetchedCluster . Spec . Bastion . Enabled ). ToNot ( BeNil (), "Bastion.Enabled should have been defaulted " )
148+ Expect (* fetchedCluster .Spec .Bastion .Enabled ).To ( BeTrueBecause ( "Bastion.Enabled should default to true" ) )
149+ } )
150+
151+ It ( "should allow IPv4 as bastion floatingIP" , func () {
152+ cluster . Spec . Bastion = & infrav1. Bastion {
153+ Enabled : pointer . Bool ( true ),
154+ Spec : & infrav1. OpenStackMachineSpec {
155+ Image : infrav1.ImageParam {
156+ Filter : & infrav1.ImageFilter {
157+ Name : pointer . String ( "fake-image" ),
158+ } ,
143159 },
144160 },
145- } ,
146- FloatingIP : pointer . String ( "10.0.0.0" ),
147- }
148- Expect ( k8sClient . Create ( ctx , cluster )). To ( Succeed (), "OpenStackCluster creation should succeed" )
149- })
150-
151- It ( "should not allow non-IPv4 as bastion floating IP" , func () {
152- cluster . Spec . Bastion = & infrav1.Bastion {
153- Spec : & infrav1.OpenStackMachineSpec {
154- Image : infrav1.ImageParam {
155- Filter : & infrav1. ImageFilter {
156- Name : pointer . String ( "fake-image" ) ,
161+ FloatingIP : pointer . String ( "10.0.0.0" ) ,
162+ }
163+ Expect ( create ( cluster )). To ( Succeed (), "OpenStackCluster creation should succeed" )
164+ } )
165+
166+ It ( "should not allow non-IPv4 as bastion floating IP" , func () {
167+ cluster . Spec . Bastion = & infrav1. Bastion {
168+ Spec : & infrav1.OpenStackMachineSpec {
169+ Image : infrav1.ImageParam {
170+ Filter : & infrav1.ImageFilter {
171+ Name : pointer . String ( "fake-image" ),
172+ } ,
157173 },
158174 },
159- } ,
160- FloatingIP : pointer . String ( "foobar" ),
161- }
162- Expect ( k8sClient . Create ( ctx , cluster )). NotTo ( Succeed (), "OpenStackCluster creation should not succeed" )
175+ FloatingIP : pointer . String ( "foobar" ) ,
176+ }
177+ Expect ( create ( cluster )). NotTo ( Succeed (), "OpenStackCluster creation should not succeed" )
178+ } )
163179 })
164180})
0 commit comments