From b4302763b7fd78f31518a2c2fc886ae57f3a5c80 Mon Sep 17 00:00:00 2001 From: Dean Roehrich Date: Tue, 21 Mar 2023 14:10:40 -0500 Subject: [PATCH 1/2] Use the live k8s client object in suite_test.go. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the kubebuilder book: https://book.kubebuilder.io/cronjob-tutorial/writing-tests.html It explains that we should be using the "live" k8s client rather than the one from the manager: "Note that we set up both a “live” k8s client and a separate client from the manager. This is because when making assertions in tests, you generally want to assert against the live state of the API server. If you use the client from the manager (k8sManager.GetClient), you’d end up asserting against the contents of the cache instead, which is slower and can introduce flakiness into your tests." --- controllers/suite_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 430922d5..a68de5ce 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -112,9 +112,9 @@ var _ = BeforeSuite(func() { //+kubebuilder:scaffold:scheme - //k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) - //Expect(err).NotTo(HaveOccurred()) - //Expect(k8sClient).NotTo(BeNil()) + k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme}) + Expect(err).NotTo(HaveOccurred()) + Expect(k8sClient).NotTo(BeNil()) // https://book.kubebuilder.io/cronjob-tutorial/writing-tests.html // One thing that this autogenerated file is missing, however, is a way to actually start your controller. @@ -140,9 +140,6 @@ var _ = BeforeSuite(func() { }).SetupWithManager(k8sManager) Expect(err).NotTo(HaveOccurred()) - k8sClient = k8sManager.GetClient() - Expect(k8sClient).NotTo(BeNil()) - go func() { defer GinkgoRecover() err := k8sManager.Start(ctx) From 7fe804abf53843fdb0012ba0e733e0979f7ecfda Mon Sep 17 00:00:00 2001 From: Dean Roehrich Date: Tue, 21 Mar 2023 16:09:44 -0500 Subject: [PATCH 2/2] remove ginkgo progress flag Signed-off-by: Dean Roehrich --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a5462eaa..7e10ddb6 100644 --- a/Makefile +++ b/Makefile @@ -111,7 +111,7 @@ test: manifests generate fmt vet envtest ## Run tests. failfast="-ginkgo.fail-fast"; \ fi; \ set -o errexit; \ - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path --bin-dir $(LOCALBIN))" go test -v ./... -coverprofile cover.out -ginkgo.v -ginkgo.progress $$failfast + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path --bin-dir $(LOCALBIN))" go test -v ./... -coverprofile cover.out -ginkgo.v $$failfast container-unit-test: VERSION ?= $(shell cat .version) container-unit-test: .version ## Run tests inside a container image