From 1e1057a517f71e36e86775e7dbad69535fa109d8 Mon Sep 17 00:00:00 2001 From: Miguel Angel Ajo Pelayo Date: Fri, 13 Sep 2024 13:01:01 +0200 Subject: [PATCH] Fix and run existing unit/functional tests from operator-sdk --- .github/workflows/test.yaml | 18 ++++++++++++++++++ config/samples/v1alpha1_exporter.yaml | 2 +- internal/controller/client_controller_test.go | 15 ++++++++++++--- internal/controller/suite_test.go | 5 ++++- 4 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/test.yaml diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..7c0f3eb --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,18 @@ +name: Unit/Functional tests +on: + workflow_dispatch: + pull_request: + branches: + - main + +jobs: + tests: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Run make deploy + run: make test diff --git a/config/samples/v1alpha1_exporter.yaml b/config/samples/v1alpha1_exporter.yaml index 09b3dda..55d414a 100644 --- a/config/samples/v1alpha1_exporter.yaml +++ b/config/samples/v1alpha1_exporter.yaml @@ -3,6 +3,6 @@ kind: Exporter metadata: labels: app.kubernetes.io/name: jumpstarter-router - app.kubernetes.io/managed-by: kustomize + dut: fancy-hardware name: exporter-sample spec: {} diff --git a/internal/controller/client_controller_test.go b/internal/controller/client_controller_test.go index 68a6279..671191b 100644 --- a/internal/controller/client_controller_test.go +++ b/internal/controller/client_controller_test.go @@ -21,6 +21,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -40,11 +41,11 @@ var _ = Describe("Identity Controller", func() { Name: resourceName, Namespace: "default", // TODO(user):Modify as needed } - identity := &jumpstarterdevv1alpha1.Client{} + client := &jumpstarterdevv1alpha1.Client{} BeforeEach(func() { - By("creating the custom resource for the Kind Identity") - err := k8sClient.Get(ctx, typeNamespacedName, identity) + By("creating the custom resource for the Kind Client") + err := k8sClient.Get(ctx, typeNamespacedName, client) if err != nil && errors.IsNotFound(err) { resource := &jumpstarterdevv1alpha1.Client{ ObjectMeta: metav1.ObjectMeta{ @@ -65,6 +66,14 @@ var _ = Describe("Identity Controller", func() { By("Cleanup the specific resource instance Identity") Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + + // the cascade delete of secrets does not seem to work in this mocked env + Expect(k8sClient.Delete(ctx, &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: resourceName + "-token", + Namespace: "default", + }, + })).To(Succeed()) }) It("should successfully reconcile the resource", func() { By("Reconciling the created resource") diff --git a/internal/controller/suite_test.go b/internal/controller/suite_test.go index 9071229..e8a6abd 100644 --- a/internal/controller/suite_test.go +++ b/internal/controller/suite_test.go @@ -18,6 +18,7 @@ package controller import ( "fmt" + "os" "path/filepath" "runtime" "testing" @@ -52,9 +53,11 @@ func TestControllers(t *testing.T) { var _ = BeforeSuite(func() { logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) + os.Setenv("CONTROLLER_KEY", "somekey") + By("bootstrapping test environment") testEnv = &envtest.Environment{ - CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")}, + CRDDirectoryPaths: []string{filepath.Join("..", "..", "deploy", "helm", "jumpstarter", "charts", "jumpstarter-controller", "templates", "crds")}, ErrorIfCRDPathMissing: true, // The BinaryAssetsDirectory is only required if you want to run the tests directly