From 47a3cb1258ea154740a8f8cafd859e797bf9b337 Mon Sep 17 00:00:00 2001 From: Pieter Callewaert Date: Thu, 3 Apr 2025 13:16:40 +0200 Subject: [PATCH] Set us basic e2e tests --- .github/workflows/e2e-test.yml | 44 +++++++++++++++++++ .gitignore | 3 ++ Makefile | 6 ++- README.md | 5 ++- tests/e2e/basic-operations/01-assert.yaml | 20 +++++++++ tests/e2e/basic-operations/01-postgres.yaml | 11 +++++ tests/e2e/basic-operations/02-assert.yaml | 15 +++++++ .../e2e/basic-operations/02-postgresuser.yaml | 9 ++++ tests/e2e/basic-operations/03-assert.yaml | 8 ++++ .../03-delete-postgresuser.yaml | 6 +++ tests/e2e/basic-operations/04-assert.yaml | 8 ++++ .../basic-operations/04-delete-postgres.yaml | 6 +++ tests/kuttl-test-self-hosted-postgres.yaml | 28 ++++++++++++ 13 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/e2e-test.yml create mode 100644 tests/e2e/basic-operations/01-assert.yaml create mode 100644 tests/e2e/basic-operations/01-postgres.yaml create mode 100644 tests/e2e/basic-operations/02-assert.yaml create mode 100644 tests/e2e/basic-operations/02-postgresuser.yaml create mode 100644 tests/e2e/basic-operations/03-assert.yaml create mode 100644 tests/e2e/basic-operations/03-delete-postgresuser.yaml create mode 100644 tests/e2e/basic-operations/04-assert.yaml create mode 100644 tests/e2e/basic-operations/04-delete-postgres.yaml create mode 100644 tests/kuttl-test-self-hosted-postgres.yaml diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml new file mode 100644 index 000000000..8d546edd2 --- /dev/null +++ b/.github/workflows/e2e-test.yml @@ -0,0 +1,44 @@ +name: E2E Test + +on: + pull_request: + branches: + - master + push: + branches: + - master + +jobs: + e2e: + runs-on: ubuntu-latest + name: End-to-End Test + timeout-minutes: 30 # Add timeout to prevent hanging jobs + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - name: Build Docker image + uses: docker/build-push-action@v4 + with: + context: . + file: ./build/Dockerfile.dist + push: false + load: true + tags: | + postgres-operator:build + - name: Install kubectl & krew + uses: marcofranssen/setup-kubectl@v1.3.0 + with: + enablePlugins: true + - name: Install KUTTL + run: kubectl krew install kuttl + - name: Run tests + run: kubectl kuttl test --config ./tests/kuttl-test-self-hosted-postgres.yaml + - name: Upload test artifacts + if: always() # Run even if tests fail + uses: actions/upload-artifact@v4 + with: + name: test-results + path: tests/kind-logs-*/ + retention-days: 7 diff --git a/.gitignore b/.gitignore index 9be2ba236..9c55478cf 100644 --- a/.gitignore +++ b/.gitignore @@ -83,3 +83,6 @@ tags deploy/secret.yaml # build artifact operator +# kuttl/kind +tests/kind-logs-*/ +kubeconfig diff --git a/Makefile b/Makefile index f6209a4a3..e16f5f22f 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: gen build +.PHONY: gen build e2e e2e-build gen: operator-sdk generate k8s @@ -17,3 +17,7 @@ linux-build: @GOBIN=/work/bin GO111MODULE=on GOOS=linux GOARC=x86_64 go build --mod=vendor -o operator github.com/movetokube/postgres-operator/cmd/manager docker-build: docker run -ti -v $(PWD):/work -w /work golang:1.13.15-stretch make linux-build +e2e-build: + docker buildx build -t postgres-operator:build -f ./build/Dockerfile.dist . +e2e: e2e-build + kubectl kuttl test --config ./tests/kuttl-test-self-hosted-postgres.yaml diff --git a/README.md b/README.md index 0e0f06799..f9c7ccaa4 100644 --- a/README.md +++ b/README.md @@ -213,6 +213,10 @@ You can contribute to this project by opening a PR to merge to `master`, or one Please write tests and fix any broken tests before you open a PR. Tests should cover at least 80% of your code. +#### e2e-tests + +End-to-end tests are implemented using [kuttl](https://kuttl.dev/), a Kubernetes test framework. To execute these tests locally, first install kuttl on your system, then run the command `make e2e` from the project root directory. + ### Compatibility Postgres operator uses Operator SDK, which uses kubernetes client. Kubernetes client compatibility with Kubernetes cluster @@ -225,4 +229,3 @@ Postgres operator compatibility with Operator SDK version is in the table below | `postgres-operator 0.4.x` | v0.17 | v1beta1 | | `postgres-operator 1.x.x` | v0.18 | v1 | | `HEAD` | v0.18 | v1 | - diff --git a/tests/e2e/basic-operations/01-assert.yaml b/tests/e2e/basic-operations/01-assert.yaml new file mode 100644 index 000000000..ddbb16e21 --- /dev/null +++ b/tests/e2e/basic-operations/01-assert.yaml @@ -0,0 +1,20 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +collectors: + - type: pod + selector: app.kubernetes.io/name=ext-postgres-operator + tail: 100 +--- +apiVersion: db.movetokube.com/v1alpha1 +kind: Postgres +metadata: + name: my-db +status: + roles: + owner: test-db-group + reader: test-db-reader + writer: test-db-writer + schemas: + - stores + - customers + succeeded: true diff --git a/tests/e2e/basic-operations/01-postgres.yaml b/tests/e2e/basic-operations/01-postgres.yaml new file mode 100644 index 000000000..e992e92c1 --- /dev/null +++ b/tests/e2e/basic-operations/01-postgres.yaml @@ -0,0 +1,11 @@ +apiVersion: db.movetokube.com/v1alpha1 +kind: Postgres +metadata: + name: my-db +spec: + database: test-db + dropOnDelete: true + masterRole: test-db-group + schemas: + - stores + - customers diff --git a/tests/e2e/basic-operations/02-assert.yaml b/tests/e2e/basic-operations/02-assert.yaml new file mode 100644 index 000000000..05b3fa24d --- /dev/null +++ b/tests/e2e/basic-operations/02-assert.yaml @@ -0,0 +1,15 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +collectors: + - type: pod + selector: app.kubernetes.io/name=ext-postgres-operator + tail: 100 +--- +apiVersion: db.movetokube.com/v1alpha1 +kind: PostgresUser +metadata: + name: my-db-user +status: + databaseName: test-db + postgresGroup: test-db-group + succeeded: true diff --git a/tests/e2e/basic-operations/02-postgresuser.yaml b/tests/e2e/basic-operations/02-postgresuser.yaml new file mode 100644 index 000000000..bc08e61a4 --- /dev/null +++ b/tests/e2e/basic-operations/02-postgresuser.yaml @@ -0,0 +1,9 @@ +apiVersion: db.movetokube.com/v1alpha1 +kind: PostgresUser +metadata: + name: my-db-user +spec: + role: username + database: my-db + secretName: my-secret + privileges: OWNER diff --git a/tests/e2e/basic-operations/03-assert.yaml b/tests/e2e/basic-operations/03-assert.yaml new file mode 100644 index 000000000..1ef4c1d8e --- /dev/null +++ b/tests/e2e/basic-operations/03-assert.yaml @@ -0,0 +1,8 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +collectors: + - type: pod + selector: app.kubernetes.io/name=ext-postgres-operator + tail: 100 +commands: + - command: bash -c "! kubectl get postgresuser my-db-user -n $NAMESPACE" diff --git a/tests/e2e/basic-operations/03-delete-postgresuser.yaml b/tests/e2e/basic-operations/03-delete-postgresuser.yaml new file mode 100644 index 000000000..774b74e5d --- /dev/null +++ b/tests/e2e/basic-operations/03-delete-postgresuser.yaml @@ -0,0 +1,6 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestStep +delete: + - apiVersion: db.movetokube.com/v1alpha1 + kind: PostgresUser + name: my-db-user diff --git a/tests/e2e/basic-operations/04-assert.yaml b/tests/e2e/basic-operations/04-assert.yaml new file mode 100644 index 000000000..fac098c0e --- /dev/null +++ b/tests/e2e/basic-operations/04-assert.yaml @@ -0,0 +1,8 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestAssert +collectors: + - type: pod + selector: app.kubernetes.io/name=ext-postgres-operator + tail: 100 +commands: + - command: bash -c "! kubectl get postgres my-db -n $NAMESPACE" diff --git a/tests/e2e/basic-operations/04-delete-postgres.yaml b/tests/e2e/basic-operations/04-delete-postgres.yaml new file mode 100644 index 000000000..dd34be07f --- /dev/null +++ b/tests/e2e/basic-operations/04-delete-postgres.yaml @@ -0,0 +1,6 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestStep +delete: + - apiVersion: db.movetokube.com/v1alpha1 + kind: Postgres + name: my-db diff --git a/tests/kuttl-test-self-hosted-postgres.yaml b/tests/kuttl-test-self-hosted-postgres.yaml new file mode 100644 index 000000000..ab06990a0 --- /dev/null +++ b/tests/kuttl-test-self-hosted-postgres.yaml @@ -0,0 +1,28 @@ +apiVersion: kuttl.dev/v1beta1 +kind: TestSuite +testDirs: + - ./tests/e2e/ +# crdDir: ./deploy/crds/ +startKIND: true +kindContext: self-hosted-postgres +kindContainers: + - postgres-operator:build +artifactsDir: ./tests/ +commands: + - command: helm repo add ext-postgres-operator https://movetokube.github.io/postgres-operator/ + - command: >- + helm install -n $NAMESPACE postgresql oci://registry-1.docker.io/bitnamicharts/postgresql + --version 16.6.0 + --set global.postgresql.auth.password=postgres + --set global.postgresql.auth.username=postgres + --wait + timeout: 120 + - command: >- + helm install -n $NAMESPACE ext-postgres-operator ext-postgres-operator/ext-postgres-operator + --set image.repository=postgres-operator + --set image.tag=build + --set postgres.host=postgresql + --set postgres.user=postgres + --set postgres.password=postgres + --set postgres.uri_args="sslmode=disable" + --wait