Skip to content

Commit 801294d

Browse files
committed
Fix the issue where v1beta1 unit tests were not running
1 parent ee40fd4 commit 801294d

File tree

2 files changed

+147
-15
lines changed

2 files changed

+147
-15
lines changed

api/v1beta1/conversion_test.go

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ package v1beta1_test
1919
import (
2020
. "github.com/onsi/ginkgo/v2"
2121
. "github.com/onsi/gomega"
22-
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
22+
corev1 "k8s.io/api/core/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
v1beta1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
25+
v1beta2 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta2"
2326
capiv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2427
)
2528

@@ -28,19 +31,17 @@ var _ = Describe("Conversion", func() {
2831
})
2932

3033
Context("GetFailureDomains function", func() {
31-
It("Return the correct value when the last state update time is known", func() {
32-
csCluster := &infrav1.CloudStackCluster{
33-
Spec: infrav1.CloudStackClusterSpec{
34-
Zones: []infrav1.Zone{
35-
{
36-
Name: "zone-name1",
37-
Network: infrav1.Network{
38-
Name: "network1",
39-
},
40-
},
34+
It("Converts v1beta1 cluster spec to v1beta2 failure domains", func() {
35+
csCluster := &v1beta1.CloudStackCluster{
36+
ObjectMeta: metav1.ObjectMeta{
37+
Name: "cluster1",
38+
Namespace: "namespace1",
39+
},
40+
Spec: v1beta1.CloudStackClusterSpec{
41+
Zones: []v1beta1.Zone{
4142
{
4243
ID: "76472a84-d23f-4e97-b154-ee1b975ed936",
43-
Network: infrav1.Network{
44+
Network: v1beta1.Network{
4445
Name: "network1",
4546
},
4647
},
@@ -52,11 +53,26 @@ var _ = Describe("Conversion", func() {
5253
Account: "account1",
5354
Domain: "domain1",
5455
},
55-
Status: infrav1.CloudStackClusterStatus{},
56+
Status: v1beta1.CloudStackClusterStatus{},
57+
}
58+
failureDomains, err := v1beta1.GetFailureDomains(csCluster)
59+
expectedResult := []v1beta2.CloudStackFailureDomainSpec{
60+
{
61+
Name: "975ed936-cluster1",
62+
Zone: v1beta2.CloudStackZoneSpec{
63+
ID: "76472a84-d23f-4e97-b154-ee1b975ed936",
64+
Network: v1beta2.Network{Name: "network1"},
65+
},
66+
Account: "account1",
67+
Domain: "domain1",
68+
ACSEndpoint: corev1.SecretReference{
69+
Name: "global",
70+
Namespace: "namespace1",
71+
},
72+
},
5673
}
57-
failureDomains, err := infrav1.GetFailureDomains(csCluster)
5874
Ω(err).ShouldNot(HaveOccurred())
59-
Ω(failureDomains).Should(HaveLen(2))
75+
Ω(failureDomains).Should(Equal(expectedResult))
6076
})
6177
})
6278
})

api/v1beta1/webhook_suite_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1_test
18+
19+
import (
20+
"context"
21+
"path/filepath"
22+
"testing"
23+
"time"
24+
25+
"k8s.io/client-go/rest"
26+
27+
. "github.com/onsi/ginkgo/v2"
28+
. "github.com/onsi/gomega"
29+
30+
admissionv1beta1 "k8s.io/api/admission/v1beta1"
31+
//+kubebuilder:scaffold:imports
32+
"k8s.io/apimachinery/pkg/runtime"
33+
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta1"
34+
ctrl "sigs.k8s.io/controller-runtime"
35+
"sigs.k8s.io/controller-runtime/pkg/client"
36+
"sigs.k8s.io/controller-runtime/pkg/envtest"
37+
)
38+
39+
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
40+
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
41+
42+
var k8sClient client.Client
43+
var testEnv *envtest.Environment
44+
var ctx context.Context
45+
var cancel context.CancelFunc
46+
47+
func TestAPIs(t *testing.T) {
48+
RegisterFailHandler(Fail)
49+
50+
RunSpecs(t, "Webhook Suite")
51+
}
52+
53+
var _ = BeforeSuite(func() {
54+
ctx, cancel = context.WithCancel(context.TODO())
55+
56+
By("bootstrapping test environment")
57+
testEnv = &envtest.Environment{
58+
CRDDirectoryPaths: []string{filepath.Join("../../", "config", "crd", "bases")},
59+
ErrorIfCRDPathMissing: false,
60+
WebhookInstallOptions: envtest.WebhookInstallOptions{
61+
Paths: []string{filepath.Join("../../", "config", "webhook")},
62+
},
63+
}
64+
65+
var cfg *rest.Config
66+
var err error
67+
done := make(chan interface{})
68+
go func() {
69+
defer GinkgoRecover()
70+
cfg, err = testEnv.Start()
71+
close(done)
72+
}()
73+
Eventually(done).WithTimeout(time.Minute).Should(BeClosed())
74+
Expect(err).NotTo(HaveOccurred())
75+
Expect(cfg).NotTo(BeNil())
76+
77+
scheme := runtime.NewScheme()
78+
err = infrav1.AddToScheme(scheme)
79+
Expect(err).NotTo(HaveOccurred())
80+
81+
err = admissionv1beta1.AddToScheme(scheme)
82+
Expect(err).NotTo(HaveOccurred())
83+
84+
//+kubebuilder:scaffold:scheme
85+
86+
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme})
87+
Expect(err).NotTo(HaveOccurred())
88+
Expect(k8sClient).NotTo(BeNil())
89+
90+
// start webhook server using Manager
91+
webhookInstallOptions := &testEnv.WebhookInstallOptions
92+
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
93+
Scheme: scheme,
94+
Host: webhookInstallOptions.LocalServingHost,
95+
Port: webhookInstallOptions.LocalServingPort,
96+
CertDir: webhookInstallOptions.LocalServingCertDir,
97+
LeaderElection: false,
98+
MetricsBindAddress: "0",
99+
})
100+
Expect(err).NotTo(HaveOccurred())
101+
102+
//+kubebuilder:scaffold:webhook
103+
104+
go func() {
105+
defer GinkgoRecover()
106+
err = mgr.Start(ctrl.SetupSignalHandler())
107+
Expect(err).NotTo(HaveOccurred())
108+
}()
109+
})
110+
111+
var _ = AfterSuite(func() {
112+
cancel()
113+
By("tearing down the test environment")
114+
err := testEnv.Stop()
115+
Expect(err).NotTo(HaveOccurred())
116+
})

0 commit comments

Comments
 (0)