Skip to content

Commit 038c11f

Browse files
committed
API validations: cleanup objects after creation
* Adds a helper function which registers a cleanup helper if an object was created * Moves infrav1 tests under a separate context in anticipation of adding other version tests.
1 parent 55918b1 commit 038c11f

File tree

1 file changed

+135
-118
lines changed

1 file changed

+135
-118
lines changed

test/e2e/suites/apivalidations/openstackcluster_test.go

Lines changed: 135 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -23,142 +23,159 @@ 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

28+
"sigs.k8s.io/cluster-api-provider-openstack/api/v1alpha7"
2729
infrav1 "sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1"
2830
)
2931

3032
var _ = Describe("OpenStackCluster API validations", func() {
31-
var cluster *infrav1.OpenStackCluster
3233
var namespace *corev1.Namespace
3334

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,
35+
create := func(obj client.Object) error {
36+
err := k8sClient.Create(ctx, obj)
37+
if err == nil {
38+
DeferCleanup(func() error {
39+
return k8sClient.Delete(ctx, obj)
40+
})
5541
}
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")
42+
return err
43+
}
8644

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")
45+
BeforeEach(func() {
46+
namespace = createNamespace()
8947
})
9048

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"),
49+
Context("infrav1", func() {
50+
var cluster *infrav1.OpenStackCluster
51+
52+
BeforeEach(func() {
53+
// Initialise a basic cluster object in the correct namespace
54+
cluster = &infrav1.OpenStackCluster{}
55+
cluster.Namespace = namespace.Name
56+
cluster.GenerateName = "cluster-"
57+
})
58+
59+
It("should allow the smallest permissible cluster spec", func() {
60+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
61+
})
62+
63+
It("should only allow controlPlaneEndpoint to be set once", func() {
64+
By("Creating a bare cluster")
65+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
66+
67+
By("Setting the control plane endpoint")
68+
cluster.Spec.ControlPlaneEndpoint = &clusterv1.APIEndpoint{
69+
Host: "foo",
70+
Port: 1234,
71+
}
72+
Expect(k8sClient.Update(ctx, cluster)).To(Succeed(), "Setting control plane endpoint should succeed")
73+
74+
By("Modifying the control plane endpoint")
75+
cluster.Spec.ControlPlaneEndpoint.Host = "bar"
76+
Expect(k8sClient.Update(ctx, cluster)).NotTo(Succeed(), "Updating control plane endpoint should fail")
77+
})
78+
79+
It("should allow an empty managed security groups definition", func() {
80+
cluster.Spec.ManagedSecurityGroups = &infrav1.ManagedSecurityGroups{}
81+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
82+
})
83+
84+
It("should default enabled to true if APIServerLoadBalancer is specified without enabled=true", func() {
85+
cluster.Spec.APIServerLoadBalancer = &infrav1.APIServerLoadBalancer{}
86+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
87+
88+
// Fetch the cluster and check the defaulting
89+
fetchedCluster := &infrav1.OpenStackCluster{}
90+
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, fetchedCluster)).To(Succeed(), "OpenStackCluster fetch should succeed")
91+
92+
Expect(fetchedCluster.Spec.APIServerLoadBalancer.Enabled).ToNot(BeNil(), "APIServerLoadBalancer.Enabled should have been defaulted")
93+
Expect(*fetchedCluster.Spec.APIServerLoadBalancer.Enabled).To(BeTrue(), "APIServerLoadBalancer.Enabled should default to true")
94+
})
95+
96+
It("should not default APIServerLoadBalancer if it is not specifid", func() {
97+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
98+
99+
// Fetch the cluster and check the defaulting
100+
fetchedCluster := &infrav1.OpenStackCluster{}
101+
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, fetchedCluster)).To(Succeed(), "OpenStackCluster fetch should succeed")
102+
103+
Expect(fetchedCluster.Spec.APIServerLoadBalancer).To(BeNil(), "APIServerLoadBalancer should not have been defaulted")
104+
Expect(fetchedCluster.Spec.APIServerLoadBalancer.IsEnabled()).To(BeFalse(), "APIServerLoadBalancer.Enabled should not have been defaulted")
105+
})
106+
107+
It("should allow bastion.enabled=true with a spec", func() {
108+
cluster.Spec.Bastion = &infrav1.Bastion{
109+
Enabled: pointer.Bool(true),
110+
Spec: &infrav1.OpenStackMachineSpec{
111+
Image: infrav1.ImageParam{
112+
Filter: &infrav1.ImageFilter{
113+
Name: pointer.String("fake-image"),
114+
},
98115
},
99116
},
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"),
117+
}
118+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
119+
})
120+
121+
It("should not allow bastion.enabled=true without a spec", func() {
122+
cluster.Spec.Bastion = &infrav1.Bastion{
123+
Enabled: pointer.Bool(true),
124+
}
125+
Expect(create(cluster)).NotTo(Succeed(), "OpenStackCluster creation should not succeed")
126+
})
127+
128+
It("should not allow an empty Bastion", func() {
129+
cluster.Spec.Bastion = &infrav1.Bastion{}
130+
Expect(create(cluster)).NotTo(Succeed(), "OpenStackCluster creation should not succeed")
131+
})
132+
133+
It("should default bastion.enabled=true", func() {
134+
cluster.Spec.Bastion = &infrav1.Bastion{
135+
Spec: &infrav1.OpenStackMachineSpec{
136+
Image: infrav1.ImageParam{
137+
Filter: &infrav1.ImageFilter{
138+
Name: pointer.String("fake-image"),
139+
},
123140
},
124141
},
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"),
142+
}
143+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should not succeed")
144+
145+
// Fetch the cluster and check the defaulting
146+
fetchedCluster := &infrav1.OpenStackCluster{}
147+
Expect(k8sClient.Get(ctx, types.NamespacedName{Name: cluster.Name, Namespace: cluster.Namespace}, fetchedCluster)).To(Succeed(), "OpenStackCluster fetch should succeed")
148+
Expect(fetchedCluster.Spec.Bastion.Enabled).ToNot(BeNil(), "Bastion.Enabled should have been defaulted")
149+
Expect(*fetchedCluster.Spec.Bastion.Enabled).To(BeTrueBecause("Bastion.Enabled should default to true"))
150+
})
151+
152+
It("should allow IPv4 as bastion floatingIP", func() {
153+
cluster.Spec.Bastion = &infrav1.Bastion{
154+
Enabled: pointer.Bool(true),
155+
Spec: &infrav1.OpenStackMachineSpec{
156+
Image: infrav1.ImageParam{
157+
Filter: &infrav1.ImageFilter{
158+
Name: pointer.String("fake-image"),
159+
},
143160
},
144161
},
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"),
162+
FloatingIP: pointer.String("10.0.0.0"),
163+
}
164+
Expect(create(cluster)).To(Succeed(), "OpenStackCluster creation should succeed")
165+
})
166+
167+
It("should not allow non-IPv4 as bastion floating IP", func() {
168+
cluster.Spec.Bastion = &infrav1.Bastion{
169+
Spec: &infrav1.OpenStackMachineSpec{
170+
Image: infrav1.ImageParam{
171+
Filter: &infrav1.ImageFilter{
172+
Name: pointer.String("fake-image"),
173+
},
157174
},
158175
},
159-
},
160-
FloatingIP: pointer.String("foobar"),
161-
}
162-
Expect(k8sClient.Create(ctx, cluster)).NotTo(Succeed(), "OpenStackCluster creation should not succeed")
176+
FloatingIP: pointer.String("foobar"),
177+
}
178+
Expect(create(cluster)).NotTo(Succeed(), "OpenStackCluster creation should not succeed")
179+
})
163180
})
164181
})

0 commit comments

Comments
 (0)