From eafbc58790d7f1b36783eae330b5581c2b4f0034 Mon Sep 17 00:00:00 2001 From: Aleksandr Aleksandrov Date: Mon, 24 Nov 2025 12:22:52 +0200 Subject: [PATCH] ci: add unit tests workflow and fix broken tests Add GitHub Actions workflow to run unit tests on every PR and push to main: - Runs `make test` which includes manifests generation, formatting, vetting - Uploads coverage report as artifact Fix unit tests to run without real cluster: - Set UseExistingCluster: false in envtest (use embedded API server) - Fix vector_controller_test.go to properly configure Vector spec and run double reconcile (finalizer + actual work) --- .github/workflows/unit-tests.yaml | 39 +++++++++++++++++++ api/v1alpha1/zz_generated.deepcopy.go | 2 +- internal/controller/suite_test.go | 2 +- internal/controller/vector_controller_test.go | 17 +++++++- 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/unit-tests.yaml diff --git a/.github/workflows/unit-tests.yaml b/.github/workflows/unit-tests.yaml new file mode 100644 index 00000000..c9748370 --- /dev/null +++ b/.github/workflows/unit-tests.yaml @@ -0,0 +1,39 @@ +name: Unit Tests + +on: + push: + branches: + - main + - master + pull_request: + branches: + - main + - master + workflow_dispatch: + +jobs: + unit-tests: + name: Run Unit Tests + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: 'go.mod' + cache: true + + - name: Run unit tests + run: make test + + - name: Upload coverage report + if: always() + uses: actions/upload-artifact@v4 + with: + name: coverage-report-${{ github.run_number }} + path: cover.out + retention-days: 7 diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 5b439271..88f3346a 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -21,7 +21,7 @@ limitations under the License. package v1alpha1 import ( - v1 "k8s.io/api/core/v1" + "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" ) diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index b82c3251..f6d596a6 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -66,7 +66,7 @@ var _ = BeforeSuite(func() { By("bootstrapping test environment") testEnv = &envtest.Environment{ - UseExistingCluster: ptr.To(true), + UseExistingCluster: ptr.To(false), CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, ErrorIfCRDPathMissing: true, diff --git a/internal/controller/vector_controller_test.go b/internal/controller/vector_controller_test.go index 24e1d23e..bdf6a7b5 100644 --- a/internal/controller/vector_controller_test.go +++ b/internal/controller/vector_controller_test.go @@ -52,7 +52,15 @@ var _ = Describe("Vector Controller", func() { Name: resourceName, Namespace: "default", }, - // TODO(user): Specify other spec details if needed. + Spec: v1alpha1.VectorSpec{ + Agent: &v1alpha1.VectorAgent{ + VectorCommon: v1alpha1.VectorCommon{ + ConfigCheck: v1alpha1.ConfigCheck{ + Disabled: true, + }, + }, + }, + }, } Expect(k8sClient.Create(ctx, resource)).To(Succeed()) } @@ -78,10 +86,17 @@ var _ = Describe("Vector Controller", func() { EventChan: make(chan event.GenericEvent, 1), } + // First reconcile adds finalizer _, err := controllerReconciler.Reconcile(ctx, reconcile.Request{ NamespacedName: typeNamespacedName, }) Expect(err).NotTo(HaveOccurred()) + + // Second reconcile performs actual reconciliation + _, err = controllerReconciler.Reconcile(ctx, reconcile.Request{ + NamespacedName: typeNamespacedName, + }) + Expect(err).NotTo(HaveOccurred()) // TODO(user): Add more specific assertions depending on your controller's reconciliation logic. // Example: If you expect a certain status condition after reconciliation, verify it here. })