From 9c747be9eba1adce25b62c7effb12df5e68e7570 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Wed, 15 Mar 2023 11:06:13 +0100 Subject: [PATCH 1/6] added envtest-less unit tests and carve out integration tests --- Makefile | 4 + controllers/srlinux_controller_test.go | 243 +++++++++--------- go.mod | 1 + go.sum | 1 + tests/integration/srlinux_controller_test.go | 145 +++++++++++ .../integration}/suite_test.go | 7 +- 6 files changed, 277 insertions(+), 124 deletions(-) create mode 100644 tests/integration/srlinux_controller_test.go rename {controllers => tests/integration}/suite_test.go (94%) diff --git a/Makefile b/Makefile index 386704a..ecbf769 100644 --- a/Makefile +++ b/Makefile @@ -60,6 +60,10 @@ vet: ## Run go vet against code. test: manifests generate fmt vet envtest ## Run tests. KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out +.PHONY: unit-test +unit-test: manifests generate fmt vet ## Run unit tests which do not involve envtest. + go test -cover ./api/... ./controllers/... -coverprofile cover.out + ##@ Build .PHONY: build diff --git a/controllers/srlinux_controller_test.go b/controllers/srlinux_controller_test.go index e3ec546..568718c 100644 --- a/controllers/srlinux_controller_test.go +++ b/controllers/srlinux_controller_test.go @@ -1,145 +1,146 @@ -/* -Copyright (c) 2021 Nokia. All rights reserved. - - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - package controllers import ( "context" - "fmt" + "os" + "testing" "time" - . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + srlinuxv1 "github.com/srl-labs/srl-controller/api/v1" + corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" -) - -var _ = Describe("Srlinux controller", func() { - // Define utility constants for object names and testing timeouts/durations and intervals. - const ( - SrlinuxName = "test-srlinux" - SrlinuxNamespace = "test" - ) - - Context("Srlinux controller test", func() { - ctx := context.Background() - - namespace := &corev1.Namespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: SrlinuxNamespace, - }, - } - - typeNamespaceName := types.NamespacedName{Name: SrlinuxName, Namespace: SrlinuxNamespace} - - BeforeEach(func() { - By("Creating the Namespace to perform the tests") - Expect(k8sClient.Create(ctx, namespace)).Should(Succeed()) - }) - - AfterEach(func() { - By("Deleting the Namespace to perform the tests") - _ = k8sClient.Delete(ctx, namespace) - }) + "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/log/zap" + "sigs.k8s.io/controller-runtime/pkg/reconcile" - It("should successfully reconcile a custom resource for Srlinux created with just an image referenced", func() { - By("Checking that Srlinux resource doesn't exist in the cluster") - - srlinux := &srlinuxv1.Srlinux{} - - err := k8sClient.Get(ctx, typeNamespaceName, srlinux) + ctrl "sigs.k8s.io/controller-runtime" +) - Expect(errors.IsNotFound(err)).To(BeTrue()) +var ( + ctx = context.TODO() + defaultCRName = "srlinux-test" + defaultNamespace = "test" + namespacedName = types.NamespacedName{Name: defaultCRName, Namespace: defaultNamespace} + defaultSrlinuxImage = "srlinux:latest" +) - By("Creating the custom resource for the Kind Srlinux") - srlinux = &srlinuxv1.Srlinux{ - ObjectMeta: metav1.ObjectMeta{ - Name: SrlinuxName, - Namespace: SrlinuxNamespace, - }, - TypeMeta: metav1.TypeMeta{ - Kind: "Srlinux", - APIVersion: "kne.srlinux.dev/v1", - }, - Spec: srlinuxv1.SrlinuxSpec{ - Config: &srlinuxv1.NodeConfig{ - Image: "srlinux:latest", +func TestMain(m *testing.M) { + err := srlinuxv1.AddToScheme(scheme.Scheme) + if err != nil { + panic(err) + } + opts := zap.Options{ + Development: true, + } + ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) + + // default timers for gomega Eventually + SetDefaultEventuallyPollingInterval(100 * time.Millisecond) + SetDefaultEventuallyTimeout(30 * time.Second) + + os.Exit(m.Run()) + +} +func TestSrlinuxReconcile(t *testing.T) { + testsCases := []struct { + descr string + clientObjs []runtime.Object + // testFn is a test function that is called for a particukar test case + testFn func(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) + }{ + { + descr: "valid SR Linux CR exists", + clientObjs: []runtime.Object{ + &srlinuxv1.Srlinux{ + ObjectMeta: ctrl.ObjectMeta{ + Name: defaultCRName, + Namespace: defaultNamespace, + }, + Spec: srlinuxv1.SrlinuxSpec{ + Config: &srlinuxv1.NodeConfig{ + Image: defaultSrlinuxImage, + }, }, }, + }, + testFn: testSrlinuxReconcile_succesful, + }, + { + descr: "SR Linux CR doesn't exists (e.g. deleted)", + clientObjs: []runtime.Object{}, + testFn: testSrlinuxReconcile_deleted, + }, + } + + for _, tc := range testsCases { + t.Run(tc.descr, func(t *testing.T) { + fakeClient := fake.NewClientBuilder().WithRuntimeObjects(tc.clientObjs...).Build() + + g := NewWithT(t) + reconciler := SrlinuxReconciler{ + Scheme: scheme.Scheme, + Client: fakeClient, } - Expect(k8sClient.Create(ctx, srlinux)).Should(Succeed()) - - By("Checking if the custom resource was successfully created") - Eventually(func() error { - found := &srlinuxv1.Srlinux{} - return k8sClient.Get(ctx, typeNamespaceName, found) - }, 10*time.Second, time.Second).Should(Succeed()) - - // Reconcile is triggered by the creation of the custom resource - - By("Checking if Srlinux Pod was successfully created in the reconciliation") - Eventually(func() error { - found := &corev1.Pod{} - - return k8sClient.Get(ctx, typeNamespaceName, found) - }, 10*time.Second, time.Second).Should(Succeed()) - - By("Ensuring the Srlinux CR Status has been updated") - Eventually(func() error { - Expect(k8sClient.Get(ctx, typeNamespaceName, srlinux)).Should(Succeed()) - - if srlinux.Status.Image != "srlinux:latest" { - return fmt.Errorf("got Srlinux.Status.Image: %s, want: %s", srlinux.Status.Image, "srlinux:latest") - } + tc.testFn(t, fakeClient, reconciler, g) + }) + } +} - return nil - }, 10*time.Second, time.Second).Should(Succeed()) +func testSrlinuxReconcile_succesful(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { + g.Eventually(func() bool { + res, err := reconciler.Reconcile(context.TODO(), reconcile.Request{ + NamespacedName: namespacedName, + }) + return res.IsZero() && err == nil + }, 10*time.Second, time.Second).Should(BeTrue()) + + // check if the Pod for Srlinux CR has been created + pod := &corev1.Pod{} + g.Expect(c.Get(ctx, namespacedName, pod)).To(Succeed()) + + // check if CR status is updated + srlinux := &srlinuxv1.Srlinux{} + g.Expect(c.Get(ctx, namespacedName, srlinux)).To(Succeed()) + g.Expect(srlinux.Status.Image).To(Equal(defaultSrlinuxImage)) +} + +func testSrlinuxReconcile_deleted(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { + g.Eventually(func() bool { + res, err := reconciler.Reconcile(context.TODO(), reconcile.Request{ + NamespacedName: namespacedName, + }) + return res.IsZero() && err == nil + }, 10*time.Second, time.Second).Should(BeTrue()) - By("Deleting the custom resource for the Kind Srlinux") - Expect(k8sClient.Delete(ctx, srlinux)).Should(Succeed()) + // check if CR hasn't been created by reconciliation loop + srlinux := &srlinuxv1.Srlinux{} + g.Expect(c.Get(ctx, namespacedName, srlinux)).ToNot(Succeed()) - By("Checking if the custom resource was successfully deleted") - Eventually(func() error { - found := &srlinuxv1.Srlinux{} + // check if CR hasn't been created + pod := &corev1.Pod{} + g.Expect(c.Get(ctx, namespacedName, pod)).ToNot(Succeed()) +} - return k8sClient.Get(ctx, typeNamespaceName, found) - }, 10*time.Second, time.Second).ShouldNot(Succeed()) +func TestSetupWithManger(t *testing.T) { + g := NewWithT(t) - // because there are no controllers monitoring built-in resources in the envtest cluster, - // objects do not get deleted, even if an OwnerReference is set up + c := fake.NewClientBuilder().WithRuntimeObjects([]runtime.Object{}...).Build() - // Reconcile is triggered by the deletion of the custom resource - }) + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ + Scheme: scheme.Scheme, }) -}) + g.Expect(err).ToNot(HaveOccurred()) + + reconciler := &SrlinuxReconciler{ + Scheme: scheme.Scheme, + Client: c, + } + g.Expect(reconciler.SetupWithManager(mgr)).To(Succeed()) +} diff --git a/go.mod b/go.mod index 554c844..1aa1867 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( github.com/creack/pty v1.1.18 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/emicklei/go-restful/v3 v3.10.1 // indirect + github.com/evanphx/json-patch v5.6.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.6.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/go-logr/zapr v1.2.3 // indirect diff --git a/go.sum b/go.sum index f72e1b5..bde5ec5 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/evanphx/json-patch v0.5.2/go.mod h1:ZWS5hhDbVDyob71nXKNL0+PWn6ToqBHMikGIFbs31qQ= github.com/evanphx/json-patch v5.6.0+incompatible h1:jBYDEEiFBPxA0v50tFdvOzQQTCvpL6mnFh5mB2/l16U= +github.com/evanphx/json-patch v5.6.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww= github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4= github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0= diff --git a/tests/integration/srlinux_controller_test.go b/tests/integration/srlinux_controller_test.go new file mode 100644 index 0000000..e3ec546 --- /dev/null +++ b/tests/integration/srlinux_controller_test.go @@ -0,0 +1,145 @@ +/* +Copyright (c) 2021 Nokia. All rights reserved. + + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +package controllers + +import ( + "context" + "fmt" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + srlinuxv1 "github.com/srl-labs/srl-controller/api/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +var _ = Describe("Srlinux controller", func() { + // Define utility constants for object names and testing timeouts/durations and intervals. + const ( + SrlinuxName = "test-srlinux" + SrlinuxNamespace = "test" + ) + + Context("Srlinux controller test", func() { + ctx := context.Background() + + namespace := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: SrlinuxNamespace, + }, + } + + typeNamespaceName := types.NamespacedName{Name: SrlinuxName, Namespace: SrlinuxNamespace} + + BeforeEach(func() { + By("Creating the Namespace to perform the tests") + Expect(k8sClient.Create(ctx, namespace)).Should(Succeed()) + }) + + AfterEach(func() { + By("Deleting the Namespace to perform the tests") + _ = k8sClient.Delete(ctx, namespace) + }) + + It("should successfully reconcile a custom resource for Srlinux created with just an image referenced", func() { + By("Checking that Srlinux resource doesn't exist in the cluster") + + srlinux := &srlinuxv1.Srlinux{} + + err := k8sClient.Get(ctx, typeNamespaceName, srlinux) + + Expect(errors.IsNotFound(err)).To(BeTrue()) + + By("Creating the custom resource for the Kind Srlinux") + srlinux = &srlinuxv1.Srlinux{ + ObjectMeta: metav1.ObjectMeta{ + Name: SrlinuxName, + Namespace: SrlinuxNamespace, + }, + TypeMeta: metav1.TypeMeta{ + Kind: "Srlinux", + APIVersion: "kne.srlinux.dev/v1", + }, + Spec: srlinuxv1.SrlinuxSpec{ + Config: &srlinuxv1.NodeConfig{ + Image: "srlinux:latest", + }, + }, + } + Expect(k8sClient.Create(ctx, srlinux)).Should(Succeed()) + + By("Checking if the custom resource was successfully created") + Eventually(func() error { + found := &srlinuxv1.Srlinux{} + + return k8sClient.Get(ctx, typeNamespaceName, found) + }, 10*time.Second, time.Second).Should(Succeed()) + + // Reconcile is triggered by the creation of the custom resource + + By("Checking if Srlinux Pod was successfully created in the reconciliation") + Eventually(func() error { + found := &corev1.Pod{} + + return k8sClient.Get(ctx, typeNamespaceName, found) + }, 10*time.Second, time.Second).Should(Succeed()) + + By("Ensuring the Srlinux CR Status has been updated") + Eventually(func() error { + Expect(k8sClient.Get(ctx, typeNamespaceName, srlinux)).Should(Succeed()) + + if srlinux.Status.Image != "srlinux:latest" { + return fmt.Errorf("got Srlinux.Status.Image: %s, want: %s", srlinux.Status.Image, "srlinux:latest") + } + + return nil + }, 10*time.Second, time.Second).Should(Succeed()) + + By("Deleting the custom resource for the Kind Srlinux") + Expect(k8sClient.Delete(ctx, srlinux)).Should(Succeed()) + + By("Checking if the custom resource was successfully deleted") + Eventually(func() error { + found := &srlinuxv1.Srlinux{} + + return k8sClient.Get(ctx, typeNamespaceName, found) + }, 10*time.Second, time.Second).ShouldNot(Succeed()) + + // because there are no controllers monitoring built-in resources in the envtest cluster, + // objects do not get deleted, even if an OwnerReference is set up + + // Reconcile is triggered by the deletion of the custom resource + }) + }) +}) diff --git a/controllers/suite_test.go b/tests/integration/suite_test.go similarity index 94% rename from controllers/suite_test.go rename to tests/integration/suite_test.go index deeea78..3a23ce9 100644 --- a/controllers/suite_test.go +++ b/tests/integration/suite_test.go @@ -39,6 +39,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + srlctrl "github.com/srl-labs/srl-controller/controllers" "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/rest" ctrl "sigs.k8s.io/controller-runtime" @@ -70,7 +71,7 @@ func prepareEnvTest() { if os.Getenv("KUBEBUILDER_ASSETS") == "" { // Use the default location of the envtest binaries. // note that the k8s version must match the version used in the Makefile. - assetstPath := filepath.Join("..", "bin", "k8s", "1.25.0-linux-amd64") + assetstPath := filepath.Join("..", "..", "bin", "k8s", "1.25.0-linux-amd64") Expect(os.Setenv("KUBEBUILDER_ASSETS", assetstPath)).To(Succeed()) } } @@ -89,7 +90,7 @@ var _ = BeforeSuite(func() { By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, + CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, } @@ -113,7 +114,7 @@ var _ = BeforeSuite(func() { }) Expect(err).ToNot(HaveOccurred()) - err = (&SrlinuxReconciler{ + err = (&srlctrl.SrlinuxReconciler{ Client: k8sManager.GetClient(), Scheme: k8sManager.GetScheme(), }).SetupWithManager(k8sManager) From 3792fc0eab092f8b477a2115dcd3b96a823dd21a Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Wed, 15 Mar 2023 11:08:06 +0100 Subject: [PATCH 2/6] updated kne ref for e2e tests --- .github/workflows/e2e.yml | 2 +- .mk/e2e.mk | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 742c2dd..e8618fa 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -6,7 +6,7 @@ name: E2E kne_ref: description: "openconfig/kne reference (tag, commit, branch)" type: string - default: v0.1.7 + default: v0.1.8 required: true kind_version: description: "KinD version" diff --git a/.mk/e2e.mk b/.mk/e2e.mk index a725de4..920cf96 100644 --- a/.mk/e2e.mk +++ b/.mk/e2e.mk @@ -1,5 +1,5 @@ # KNE_REF is a git reference to use for KNE. It can be a branch, tag, or commit hash. -KNE_REF ?= dee1995a7ec0f446b159a35fad201df9185ee75d +KNE_REF ?= v0.1.8 KNE_REPO := https://github.com/openconfig/kne.git KNE_TEMP_DIR := /tmp/.srlcontroller-tests/kne KNE_TEST_DEPLOYMENT_FILE := ${KNE_TEMP_DIR}/deploy/kne/kind-bridge-no-controllers.yaml From 4230dd69f4f7985fa956c1f3bee6accb61d8fad6 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Wed, 15 Mar 2023 11:43:35 +0100 Subject: [PATCH 3/6] remove unit testing setup with manager --- controllers/srlinux_controller_test.go | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/controllers/srlinux_controller_test.go b/controllers/srlinux_controller_test.go index 568718c..01ab37d 100644 --- a/controllers/srlinux_controller_test.go +++ b/controllers/srlinux_controller_test.go @@ -127,20 +127,3 @@ func testSrlinuxReconcile_deleted(t *testing.T, c client.Client, reconciler Srli pod := &corev1.Pod{} g.Expect(c.Get(ctx, namespacedName, pod)).ToNot(Succeed()) } - -func TestSetupWithManger(t *testing.T) { - g := NewWithT(t) - - c := fake.NewClientBuilder().WithRuntimeObjects([]runtime.Object{}...).Build() - - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ - Scheme: scheme.Scheme, - }) - g.Expect(err).ToNot(HaveOccurred()) - - reconciler := &SrlinuxReconciler{ - Scheme: scheme.Scheme, - Client: c, - } - g.Expect(reconciler.SetupWithManager(mgr)).To(Succeed()) -} From 06ec85d30d01f445aa74d108ce2c23879f3c1c04 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Wed, 15 Mar 2023 12:07:23 +0100 Subject: [PATCH 4/6] renamed test functions and addressed linters --- controllers/srlinux_controller_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/controllers/srlinux_controller_test.go b/controllers/srlinux_controller_test.go index 01ab37d..0be2ce3 100644 --- a/controllers/srlinux_controller_test.go +++ b/controllers/srlinux_controller_test.go @@ -35,6 +35,7 @@ func TestMain(m *testing.M) { if err != nil { panic(err) } + opts := zap.Options{ Development: true, } @@ -45,8 +46,8 @@ func TestMain(m *testing.M) { SetDefaultEventuallyTimeout(30 * time.Second) os.Exit(m.Run()) - } + func TestSrlinuxReconcile(t *testing.T) { testsCases := []struct { descr string @@ -69,12 +70,12 @@ func TestSrlinuxReconcile(t *testing.T) { }, }, }, - testFn: testSrlinuxReconcile_succesful, + testFn: testReconcileForBasicSrlCR, }, { descr: "SR Linux CR doesn't exists (e.g. deleted)", clientObjs: []runtime.Object{}, - testFn: testSrlinuxReconcile_deleted, + testFn: testReconcileForDeletedCR, }, } @@ -93,11 +94,12 @@ func TestSrlinuxReconcile(t *testing.T) { } } -func testSrlinuxReconcile_succesful(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { +func testReconcileForBasicSrlCR(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { g.Eventually(func() bool { res, err := reconciler.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: namespacedName, }) + return res.IsZero() && err == nil }, 10*time.Second, time.Second).Should(BeTrue()) @@ -111,11 +113,12 @@ func testSrlinuxReconcile_succesful(t *testing.T, c client.Client, reconciler Sr g.Expect(srlinux.Status.Image).To(Equal(defaultSrlinuxImage)) } -func testSrlinuxReconcile_deleted(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { +func testReconcileForDeletedCR(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { g.Eventually(func() bool { res, err := reconciler.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: namespacedName, }) + return res.IsZero() && err == nil }, 10*time.Second, time.Second).Should(BeTrue()) From c2bd69877a080f3e5ababa4fa942c68b2b3786b1 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Wed, 15 Mar 2023 12:11:26 +0100 Subject: [PATCH 5/6] hide unused param --- controllers/srlinux_controller_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/srlinux_controller_test.go b/controllers/srlinux_controller_test.go index 0be2ce3..b9bd867 100644 --- a/controllers/srlinux_controller_test.go +++ b/controllers/srlinux_controller_test.go @@ -94,7 +94,7 @@ func TestSrlinuxReconcile(t *testing.T) { } } -func testReconcileForBasicSrlCR(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { +func testReconcileForBasicSrlCR(_ *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { g.Eventually(func() bool { res, err := reconciler.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: namespacedName, @@ -113,7 +113,7 @@ func testReconcileForBasicSrlCR(t *testing.T, c client.Client, reconciler Srlinu g.Expect(srlinux.Status.Image).To(Equal(defaultSrlinuxImage)) } -func testReconcileForDeletedCR(t *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { +func testReconcileForDeletedCR(_ *testing.T, c client.Client, reconciler SrlinuxReconciler, g *GomegaWithT) { g.Eventually(func() bool { res, err := reconciler.Reconcile(context.TODO(), reconcile.Request{ NamespacedName: namespacedName, From 75d6cdaf6c69e503ffda79ee01ee8d9ec46d4b78 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Wed, 15 Mar 2023 12:22:53 +0100 Subject: [PATCH 6/6] test coverpkg --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ecbf769..f7c642c 100644 --- a/Makefile +++ b/Makefile @@ -58,7 +58,7 @@ vet: ## Run go vet against code. .PHONY: test test: manifests generate fmt vet envtest ## Run tests. - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out + KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverpkg=./... -coverprofile cover.out .PHONY: unit-test unit-test: manifests generate fmt vet ## Run unit tests which do not involve envtest.