Skip to content

Commit ef90eed

Browse files
authored
Merge pull request #1169 from srm09/backport/controller-tests-0.7
Adds test setup for controller tests
2 parents 7c63e73 + 12ed314 commit ef90eed

File tree

11 files changed

+637
-103
lines changed

11 files changed

+637
-103
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ help: ## Display this help
107107

108108
.PHONY: test
109109
test: generate lint-go ## Run tests
110-
go test -v ./api/... ./controllers/... ./pkg/...
110+
source ./hack/fetch_ext_bins.sh; fetch_tools; setup_envs; go test -v ./api/... ./controllers/... ./pkg/...
111111

112112
.PHONY: e2e-image
113113
e2e-image: ## Build the e2e manager image
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2021 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 controllers
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"testing"
23+
24+
. "github.com/onsi/ginkgo"
25+
. "github.com/onsi/gomega"
26+
27+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
28+
"k8s.io/client-go/kubernetes/scheme"
29+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
30+
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
31+
32+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha3"
33+
"sigs.k8s.io/cluster-api-provider-vsphere/test/helpers"
34+
// +kubebuilder:scaffold:imports
35+
)
36+
37+
func TestControllers(t *testing.T) {
38+
RegisterFailHandler(Fail)
39+
RunSpecsWithDefaultAndCustomReporters(t,
40+
"Controller Suite",
41+
[]Reporter{printer.NewlineReporter{}})
42+
}
43+
44+
var (
45+
testEnv *helpers.TestEnvironment
46+
)
47+
48+
func TestMain(m *testing.M) {
49+
setup()
50+
defer func() {
51+
fmt.Println("Tearing down test suite")
52+
teardown()
53+
}()
54+
code := m.Run()
55+
os.Exit(code)
56+
}
57+
58+
func setup() {
59+
fmt.Println("Creating new test environment")
60+
utilruntime.Must(infrav1.AddToScheme(scheme.Scheme))
61+
utilruntime.Must(clusterv1.AddToScheme(scheme.Scheme))
62+
63+
testEnv = helpers.NewTestEnvironment()
64+
65+
if err := AddClusterControllerToManager(testEnv.GetContext(), testEnv.Manager); err != nil {
66+
panic(fmt.Sprintf("unable to setup VsphereCluster controller: %v", err))
67+
}
68+
if err := AddMachineControllerToManager(testEnv.GetContext(), testEnv.Manager); err != nil {
69+
panic(fmt.Sprintf("unable to setup VsphereMachine controller: %v", err))
70+
}
71+
if err := AddVMControllerToManager(testEnv.GetContext(), testEnv.Manager); err != nil {
72+
panic(fmt.Sprintf("unable to setup VsphereVM controller: %v", err))
73+
}
74+
if err := AddHAProxyLoadBalancerControllerToManager(testEnv.GetContext(), testEnv.Manager); err != nil {
75+
panic(fmt.Sprintf("unable to setup HAProxyLB controller: %v", err))
76+
}
77+
78+
go func() {
79+
fmt.Println("Starting the manager")
80+
if err := testEnv.StartManager(); err != nil {
81+
panic(fmt.Sprintf("failed to start the envtest manager: %v", err))
82+
}
83+
}()
84+
85+
// wait for webhook port to be open prior to running tests
86+
testEnv.WaitForWebhooks()
87+
}
88+
89+
func teardown() {
90+
if err := testEnv.Stop(); err != nil {
91+
panic(fmt.Sprintf("Failed to stop envtest: %v", err))
92+
}
93+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2021 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 controllers
18+
19+
import (
20+
goctx "context"
21+
"time"
22+
23+
. "github.com/onsi/ginkgo"
24+
. "github.com/onsi/gomega"
25+
26+
corev1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
29+
"sigs.k8s.io/cluster-api/util/patch"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
31+
32+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha3"
33+
)
34+
35+
const (
36+
timeout = time.Second * 30
37+
)
38+
39+
var _ = Describe("ClusterReconciler", func() {
40+
BeforeEach(func() {})
41+
AfterEach(func() {})
42+
43+
Context("Reconcile an VSphereCluster", func() {
44+
It("should create a cluster", func() {
45+
ctx := goctx.Background()
46+
47+
capiCluster := &clusterv1.Cluster{
48+
ObjectMeta: metav1.ObjectMeta{
49+
GenerateName: "test1-",
50+
Namespace: "default",
51+
},
52+
Spec: clusterv1.ClusterSpec{
53+
InfrastructureRef: &corev1.ObjectReference{
54+
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha4",
55+
Kind: "VsphereCluster",
56+
Name: "vsphere-test1",
57+
},
58+
},
59+
}
60+
// Create the CAPI cluster (owner) object
61+
Expect(testEnv.Create(ctx, capiCluster)).To(Succeed())
62+
63+
instance := &infrav1.VSphereCluster{
64+
ObjectMeta: metav1.ObjectMeta{
65+
Name: "vsphere-test1",
66+
Namespace: "default",
67+
},
68+
Spec: infrav1.VSphereClusterSpec{},
69+
}
70+
71+
// Create the VSphereCluster object
72+
Expect(testEnv.Create(ctx, instance)).To(Succeed())
73+
key := client.ObjectKey{Namespace: instance.Namespace, Name: instance.Name}
74+
defer func() {
75+
err := testEnv.Delete(ctx, instance)
76+
Expect(err).NotTo(HaveOccurred())
77+
}()
78+
79+
// Make sure the VSphereCluster exists.
80+
Eventually(func() bool {
81+
err := testEnv.Get(ctx, key, instance)
82+
return err == nil
83+
}, timeout).Should(BeTrue())
84+
85+
By("setting the OwnerRef on the VSphereCluster")
86+
Eventually(func() bool {
87+
ph, err := patch.NewHelper(instance, testEnv)
88+
Expect(err).ShouldNot(HaveOccurred())
89+
instance.OwnerReferences = append(instance.OwnerReferences, metav1.OwnerReference{Kind: "Cluster", APIVersion: clusterv1.GroupVersion.String(), Name: capiCluster.Name, UID: "blah"})
90+
Expect(ph.Patch(ctx, instance, patch.WithStatusObservedGeneration{})).ShouldNot(HaveOccurred())
91+
return true
92+
}, timeout).Should(BeTrue())
93+
94+
Eventually(func() bool {
95+
if err := testEnv.Get(ctx, key, instance); err != nil {
96+
return false
97+
}
98+
return len(instance.Finalizers) > 0
99+
}, timeout).Should(BeTrue())
100+
})
101+
})
102+
})

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ require (
1616
gopkg.in/gcfg.v1 v1.2.3
1717
gopkg.in/warnings.v0 v0.1.2 // indirect
1818
k8s.io/api v0.17.9
19+
k8s.io/apiextensions-apiserver v0.17.9
1920
k8s.io/apimachinery v0.17.9
2021
k8s.io/client-go v0.17.9
2122
k8s.io/klog v1.0.0

0 commit comments

Comments
 (0)