Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌱 Refactor tests to plain go in bootstrap/kubeadm/controllers #4603

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ limitations under the License.
package controllers

import (
. "github.com/onsi/ginkgo"
"testing"

. "github.com/onsi/gomega"

ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -26,36 +27,35 @@ import (
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
)

var _ = Describe("KubeadmConfigReconciler", func() {
BeforeEach(func() {})
AfterEach(func() {})
func TestKubeadmConfigReconciler(t *testing.T) {
t.Run("Reconcile a KubeadmConfig", func(t *testing.T) {
t.Run("should wait until infrastructure is ready", func(t *testing.T) {
g := NewWithT(t)

Context("Reconcile a KubeadmConfig", func() {
It("should wait until infrastructure is ready", func() {
cluster := newCluster("cluster1")
Expect(testEnv.Create(ctx, cluster)).To(Succeed())
g.Expect(testEnv.Create(ctx, cluster)).To(Succeed())

machine := newMachine(cluster, "my-machine")
Expect(testEnv.Create(ctx, machine)).To(Succeed())
g.Expect(testEnv.Create(ctx, machine)).To(Succeed())

config := newKubeadmConfig(machine, "my-machine-config")
Expect(testEnv.Create(ctx, config)).To(Succeed())
g.Expect(testEnv.Create(ctx, config)).To(Succeed())

reconciler := KubeadmConfigReconciler{
Client: testEnv,
}
By("Calling reconcile should requeue")
t.Log("Calling reconcile should requeue")
result, err := reconciler.Reconcile(ctx, ctrl.Request{
NamespacedName: client.ObjectKey{
Namespace: "default",
Name: "my-machine-config",
},
})
Expect(err).To(Succeed())
Expect(result.Requeue).To(BeFalse())
g.Expect(err).To(Succeed())
g.Expect(result.Requeue).To(BeFalse())
})
})
})
}

// getKubeadmConfig returns a KubeadmConfig object from the cluster.
func getKubeadmConfig(c client.Client, name string) (*bootstrapv1.KubeadmConfig, error) {
Expand Down
42 changes: 16 additions & 26 deletions bootstrap/kubeadm/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,49 +17,39 @@ limitations under the License.
package controllers

import (
"fmt"
"os"
"testing"

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

"sigs.k8s.io/cluster-api/test/helpers"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
// +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 (
testEnv *helpers.TestEnvironment
ctx = ctrl.SetupSignalHandler()
)

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

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

var _ = BeforeSuite(func() {
By("bootstrapping test environment")
func TestMain(m *testing.M) {
fmt.Println("Creating new test environment")
testEnv = helpers.NewTestEnvironment()

By("starting the manager")
go func() {
defer GinkgoRecover()
Expect(testEnv.StartManager(ctx)).To(Succeed())
fmt.Println("Starting the manager")
if err := testEnv.StartManager(ctx); err != nil {
panic(fmt.Sprintf("Failed to start the envtest manager: %v", err))
}
}()
<-testEnv.Manager.Elected()
testEnv.WaitForWebhooks()
}, 60)

var _ = AfterSuite(func() {
if testEnv != nil {
By("tearing down the test environment")
Expect(testEnv.Stop()).To(Succeed())
code := m.Run()

fmt.Println("Tearing down test suite")
if err := testEnv.Stop(); err != nil {
panic(fmt.Sprintf("Failed to stop envtest: %v", err))
}
})

os.Exit(code)
}