From e9df52acb214a59a8a6bc82d7408cbfa16267b30 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 15 Nov 2024 18:15:39 +0100 Subject: [PATCH 1/3] fix the ClientProfile controller unit test Co-Authored-by: Raghavendra Talur Signed-off-by: Michael Adam --- .../clientprofile_controller_test.go | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/internal/controller/clientprofile_controller_test.go b/internal/controller/clientprofile_controller_test.go index 5c762600..8ba78e18 100644 --- a/internal/controller/clientprofile_controller_test.go +++ b/internal/controller/clientprofile_controller_test.go @@ -25,6 +25,7 @@ import ( "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/reconcile" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" csiv1alpha1 "github.com/ceph/ceph-csi-operator/api/v1alpha1" @@ -33,18 +34,39 @@ import ( var _ = Describe("ClientProfile Controller", func() { Context("When reconciling a resource", func() { const resourceName = "test-resource" + const cephConnectionName = "foo" + const namespaceName = "default" ctx := context.Background() typeNamespacedName := types.NamespacedName{ Name: resourceName, - Namespace: "default", // TODO(user):Modify as needed + Namespace: namespaceName, // TODO(user):Modify as needed } clientProfile := &csiv1alpha1.ClientProfile{} + typeCephConnectionName := types.NamespacedName{ + Name: cephConnectionName, + Namespace: namespaceName, + } + cephConn := &csiv1alpha1.CephConnection{} BeforeEach(func() { + err := k8sClient.Get(ctx, typeCephConnectionName, cephConn) + if err != nil && errors.IsNotFound(err) { + resource := &csiv1alpha1.CephConnection{ + ObjectMeta: metav1.ObjectMeta{ + Name: cephConnectionName, + Namespace: namespaceName, + }, + Spec: csiv1alpha1.CephConnectionSpec{ + Monitors: []string{"10.98.44.171:6789"}, + }, + } + Expect(k8sClient.Create(ctx, resource)).To(Succeed()) + } + By("creating the custom resource for the Kind ClientProfile") - err := k8sClient.Get(ctx, typeNamespacedName, clientProfile) + err = k8sClient.Get(ctx, typeNamespacedName, clientProfile) if err != nil && errors.IsNotFound(err) { resource := &csiv1alpha1.ClientProfile{ ObjectMeta: metav1.ObjectMeta{ @@ -52,6 +74,11 @@ var _ = Describe("ClientProfile Controller", func() { Namespace: "default", }, // TODO(user): Specify other spec details if needed. + Spec: csiv1alpha1.ClientProfileSpec{ + CephConnectionRef: corev1.LocalObjectReference{ + Name: "foo", + }, + }, } Expect(k8sClient.Create(ctx, resource)).To(Succeed()) } @@ -65,6 +92,13 @@ var _ = Describe("ClientProfile Controller", func() { By("Cleanup the specific resource instance ClientProfile") Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + + cephConnection := &csiv1alpha1.CephConnection{} + err = k8sClient.Get(ctx, typeCephConnectionName, cephConnection) + Expect(err).NotTo(HaveOccurred()) + + By("Cleanup the cephConnection") + Expect(k8sClient.Delete(ctx, cephConnection)).To(Succeed()) }) It("should successfully reconcile the resource", func() { By("Reconciling the created resource") From 31847c415c8c49b93693f164a5eb01840617e101 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 15 Nov 2024 18:15:39 +0100 Subject: [PATCH 2/3] fix the Driver controller unit test Co-Authored-by: Raghavendra Talur Signed-off-by: Michael Adam --- internal/controller/driver_controller_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/controller/driver_controller_test.go b/internal/controller/driver_controller_test.go index b63db492..48d1ac50 100644 --- a/internal/controller/driver_controller_test.go +++ b/internal/controller/driver_controller_test.go @@ -32,7 +32,7 @@ import ( var _ = Describe("Driver Controller", func() { Context("When reconciling a resource", func() { - const resourceName = "test-resource" + const resourceName = "test.rbd.csi.ceph.com" ctx := context.Background() From 8a420d995f182d2bf7badaf4e69792668535183a Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Wed, 20 Nov 2024 11:16:15 +0100 Subject: [PATCH 3/3] ci: add a unit-test workflow This is to make sure unit tests are run and pass oneach PR. Signed-off-by: Michael Adam --- .github/workflows/unit-test.yaml | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 .github/workflows/unit-test.yaml diff --git a/.github/workflows/unit-test.yaml b/.github/workflows/unit-test.yaml new file mode 100644 index 00000000..1c069025 --- /dev/null +++ b/.github/workflows/unit-test.yaml @@ -0,0 +1,33 @@ +name: Unit Tests +on: + pull_request: + paths-ignore: + - 'vendor/*' + branches: + - main + - release-* + +# cancel the in-progress workflow when PR is refreshed. +concurrency: + group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + unittests: + name: unittests + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: setup go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + check-latest: true + - name: run unit tests + run: make test