Skip to content

Commit b7a6685

Browse files
authored
Merge pull request #2001 from shiftstack/validationtestcleanups
🌱 Minor improvements to api validation tests
2 parents d2d1cb2 + c59ee80 commit b7a6685

File tree

3 files changed

+155
-139
lines changed

3 files changed

+155
-139
lines changed

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ ifdef KUBEBUILDER_ASSETS_DIR
133133
setup_envtest_extra_args += --bin-dir $(KUBEBUILDER_ASSETS_DIR)
134134
endif
135135

136+
.PHONY: kubebuilder_assets
137+
kubebuilder_assets: $(SETUP_ENVTEST)
138+
@echo Fetching assets for $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION)
139+
$(eval KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(setup_envtest_extra_args) $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION)))
140+
136141
.PHONY: test
137142
TEST_PATHS ?= ./...
138-
test: $(SETUP_ENVTEST) ## Run tests
139-
set -xeuf -o pipefail; \
140-
if [ -z "$(KUBEBUILDER_ASSETS)" ]; then \
141-
KUBEBUILDER_ASSETS=`$(SETUP_ENVTEST) use --use-env -p path $(setup_envtest_extra_args) $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION)`; \
142-
fi; \
143-
KUBEBUILDER_ASSETS="$$KUBEBUILDER_ASSETS" go test -v $(TEST_PATHS) $(TEST_ARGS)
143+
test: kubebuilder_assets
144+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -v $(TEST_PATHS) $(TEST_ARGS)
144145

145146
E2E_TEMPLATES_DIR=test/e2e/data/infrastructure-openstack
146147
E2E_KUSTOMIZE_DIR=test/e2e/data/kustomize

test/e2e/suites/apivalidations/openstackcluster_test.go

Lines changed: 134 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -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

3031
var _ = 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
})

test/e2e/suites/apivalidations/suite_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,21 @@ func TestAPIs(t *testing.T) {
6363
}
6464

6565
var _ = BeforeSuite(func() {
66+
testScheme = scheme.Scheme
67+
for _, f := range []func(*runtime.Scheme) error{
68+
infrav1alpha1.AddToScheme,
69+
infrav1alpha5.AddToScheme,
70+
infrav1alpha6.AddToScheme,
71+
infrav1alpha7.AddToScheme,
72+
infrav1.AddToScheme,
73+
} {
74+
Expect(f(testScheme)).To(Succeed())
75+
}
76+
6677
By("bootstrapping test environment")
78+
testCRDs := filepath.Join("..", "..", "..", "..", "config", "crd", "bases")
6779
testEnv = &envtest.Environment{
68-
CRDDirectoryPaths: []string{
69-
// NOTE: These are the bare CRDs without conversion webhooks
70-
filepath.Join("..", "..", "..", "..", "config", "crd", "bases"),
71-
},
80+
CRDDirectoryPaths: []string{testCRDs},
7281
ErrorIfCRDPathMissing: true,
7382
WebhookInstallOptions: envtest.WebhookInstallOptions{
7483
Paths: []string{
@@ -86,17 +95,6 @@ var _ = BeforeSuite(func() {
8695
return testEnv.Stop()
8796
})
8897

89-
testScheme = scheme.Scheme
90-
for _, f := range []func(*runtime.Scheme) error{
91-
infrav1alpha1.AddToScheme,
92-
infrav1alpha5.AddToScheme,
93-
infrav1alpha6.AddToScheme,
94-
infrav1alpha7.AddToScheme,
95-
infrav1.AddToScheme,
96-
} {
97-
Expect(f(testScheme)).To(Succeed())
98-
}
99-
10098
k8sClient, err = client.New(cfg, client.Options{Scheme: testScheme})
10199
Expect(err).NotTo(HaveOccurred())
102100
Expect(k8sClient).NotTo(BeNil())
@@ -128,6 +126,7 @@ var _ = BeforeSuite(func() {
128126
Host: testEnv.WebhookInstallOptions.LocalServingHost,
129127
CertDir: testEnv.WebhookInstallOptions.LocalServingCertDir,
130128
}),
129+
Logger: GinkgoLogr,
131130
})
132131
Expect(err).ToNot(HaveOccurred(), "Manager setup should succeed")
133132
Expect(webhooks.RegisterAllWithManager(mgr)).To(BeEmpty(), "Failed to register webhooks")

0 commit comments

Comments
 (0)