Skip to content

Commit

Permalink
🌱 Refactor testenv and remove unused ones
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Jun 8, 2020
1 parent 03cff23 commit 87bb607
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 640 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ help: ## Display this help

.PHONY: test
test: ## Run tests
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; go test -v ./...
source ./scripts/fetch_ext_bins.sh; fetch_tools; setup_envs; go test -v ./... -count=1

.PHONY: docker-build-e2e
docker-build-e2e: ## Rebuild all Cluster API provider images to be used in the e2e tests
Expand Down

This file was deleted.

85 changes: 0 additions & 85 deletions bootstrap/kubeadm/api/v1alpha2/suite_test.go

This file was deleted.

48 changes: 0 additions & 48 deletions bootstrap/kubeadm/api/v1alpha3/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,69 +17,21 @@ limitations under the License.
package v1alpha3

import (
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog"
"k8s.io/klog/klogr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment

func init() {
klog.InitFlags(nil)
klog.SetOutput(GinkgoWriter)
logf.SetLogger(klogr.New())
}

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

RunSpecsWithDefaultAndCustomReporters(t,
"v1alpha3 Suite",
[]Reporter{printer.NewlineReporter{}})
}

var _ = BeforeSuite(func(done Done) {
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "..", "config", "crd", "bases"),
filepath.Join("..", "..", "..", "..", "controlplane", "kubeadm", "config", "crd", "bases"),
filepath.Join("..", "..", "..", "..", "config", "crd", "bases"),
},
}

err := SchemeBuilder.AddToScheme(scheme.Scheme)
Expect(err).NotTo(HaveOccurred())

cfg, err = testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())

close(done)
}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
})
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ var _ = Describe("KubeadmConfigReconciler", func() {
Context("Reconcile a KubeadmConfig", func() {
It("should wait until infrastructure is ready", func() {
cluster := newCluster("cluster1")
Expect(k8sClient.Create(context.Background(), cluster)).To(Succeed())
Expect(testEnv.Create(context.Background(), cluster)).To(Succeed())

machine := newMachine(cluster, "my-machine")
Expect(k8sClient.Create(context.Background(), machine)).To(Succeed())
Expect(testEnv.Create(context.Background(), machine)).To(Succeed())

config := newKubeadmConfig(machine, "my-machine-config")
Expect(k8sClient.Create(context.Background(), config)).To(Succeed())
Expect(testEnv.Create(context.Background(), config)).To(Succeed())

reconciler := KubeadmConfigReconciler{
Log: log.Log,
Client: k8sClient,
Client: testEnv,
}
By("Calling reconcile should requeue")
result, err := reconciler.Reconcile(ctrl.Request{
Expand Down
52 changes: 14 additions & 38 deletions bootstrap/kubeadm/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,22 @@ limitations under the License.
package controllers

import (
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

"k8s.io/client-go/kubernetes/scheme"
"k8s.io/klog"
"k8s.io/klog/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/cluster-api/test/helpers"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
// +kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var k8sClient client.Client
var testEnv *envtest.Environment

func init() {
klog.InitFlags(nil)
klog.SetOutput(GinkgoWriter)
logf.SetLogger(klogr.New())
}
var (
testEnv *helpers.TestEnvironment
)

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)
Expand All @@ -57,31 +44,20 @@ func TestAPIs(t *testing.T) {

var _ = BeforeSuite(func(done Done) {
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join("..", "config", "crd", "bases"),
filepath.Join("..", "..", "..", "controlplane", "kubeadm", "config", "crd", "bases"),
filepath.Join("..", "..", "..", "config", "crd", "bases"),
},
}
testEnv = helpers.NewTestEnvironment()

cfg, err := testEnv.Start()
Expect(err).ToNot(HaveOccurred())
Expect(cfg).ToNot(BeNil())

Expect(bootstrapv1.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
Expect(clusterv1.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
// +kubebuilder:scaffold:scheme

k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).ToNot(HaveOccurred())
Expect(k8sClient).ToNot(BeNil())
By("starting the manager")
go func() {
defer GinkgoRecover()
Expect(testEnv.StartManager()).To(Succeed())
}()

close(done)
}, 60)

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).ToNot(HaveOccurred())
if testEnv != nil {
By("tearing down the test environment")
Expect(testEnv.Stop()).To(Succeed())
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand All @@ -44,10 +43,6 @@ const (
clusterNamespace = "test-namespace"
)

func init() {
klog.InitFlags(nil)
}

func TestControlPlaneInitMutex_Lock(t *testing.T) {
g := NewWithT(t)

Expand Down
Loading

0 comments on commit 87bb607

Please sign in to comment.