Skip to content

Commit f6e3668

Browse files
jmckulkValClarkson
andauthored
Initial E2E test (CrunchyData#2808)
* Add initial Kuttl e2e tests This change adds a quick end-to-end test that use the kuttl test framework. The layout for kuttl tests is `testing/kuttl/e2e` with individual tests in subdirectories of the `e2e` directory. Run test(s) by using the `make check-kuttl` target. The initial creates a cluster then checks the postgrescluster status and that the initial backup completes. the subsequent test uses the above cluster and 2 scripts to run a portforward and to connect to psql and run select version() to return the psql version Co-authored-by: ValClarkson <[email protected]>
1 parent a2c8ad9 commit f6e3668

File tree

8 files changed

+114
-0
lines changed

8 files changed

+114
-0
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ GO ?= go
6262
GO_BUILD = $(GO_CMD) build -trimpath
6363
GO_CMD = $(GO_ENV) $(GO)
6464
GO_TEST ?= $(GO) test
65+
KUTTL_TEST ?= kuttl test
6566

6667
# Disable optimizations if creating a debug build
6768
ifeq ("$(DEBUG_BUILD)", "true")
@@ -202,6 +203,11 @@ check-envtest-existing:
202203
USE_EXISTING_CLUSTER=true $(GO_TEST) -count=1 -cover -p=1 -tags=envtest ./...
203204
${PGO_KUBE_CLIENT} delete -k ./config/dev
204205

206+
# Expects operator to be running
207+
.PHONY: check-kuttl
208+
check-kuttl:
209+
${PGO_KUBE_CLIENT} ${KUTTL_TEST} \
210+
--config testing/kuttl/kuttl-test.yaml
205211

206212
.PHONY: check-generate
207213
check-generate: generate-crd generate-deepcopy generate-rbac

testing/kuttl/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Kuttl
2+
3+
## Installing
4+
Docs for install: https://kuttl.dev/docs/cli.html#setup-the-kuttl-kubectl-plugin
5+
6+
Options:
7+
- Download and install the binary
8+
- Install the `kubectl krew` [plugin manager](https://github.com/kubernetes-sigs/krew)
9+
and `kubectl krew install kuttl`
10+
## Cheat sheet
11+
12+
### Run test suite
13+
14+
Make sure that the operator is running in your kubernetes environment and that your `kubeconfig` is
15+
set up. Then run the make target:
16+
17+
```
18+
make check-kuttl
19+
```
20+
21+
### Running a single test
22+
A single test is considered to be one directory under `kuttl/e2e`, for example
23+
`kuttl/e2e/restore` would run the `restore` test.
24+
25+
There are two ways to run a single test in isolation:
26+
- using an env var with the make target: `KUTTL_TEST='kuttl test --test <test-name>' make check-kuttl`
27+
- using `kubectl kuttl --test` flag: `kubectl kuttl test testing/kuttl/e2e --test <test-name>`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apiVersion: postgres-operator.crunchydata.com/v1beta1
2+
kind: PostgresCluster
3+
metadata:
4+
name: cluster-start
5+
status:
6+
instances:
7+
- name: instance1
8+
readyReplicas: 1
9+
replicas: 1
10+
updatedReplicas: 1
11+
---
12+
apiVersion: batch/v1
13+
kind: Job
14+
metadata:
15+
labels:
16+
postgres-operator.crunchydata.com/cluster: cluster-start
17+
postgres-operator.crunchydata.com/pgbackrest-backup: replica-create
18+
status:
19+
succeeded: 1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: postgres-operator.crunchydata.com/v1beta1
2+
kind: PostgresCluster
3+
metadata:
4+
name: cluster-start
5+
spec:
6+
image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:centos8-13.4-1
7+
postgresVersion: 13
8+
instances:
9+
- name: instance1
10+
dataVolumeClaimSpec:
11+
accessModes:
12+
- "ReadWriteOnce"
13+
resources:
14+
requests:
15+
storage: 1Gi
16+
backups:
17+
pgbackrest:
18+
image: registry.developers.crunchydata.com/crunchydata/crunchy-pgbackrest:centos8-2.35-0
19+
repos:
20+
- name: repo1
21+
volume:
22+
volumeClaimSpec:
23+
accessModes:
24+
- "ReadWriteOnce"
25+
resources:
26+
requests:
27+
storage: 1Gi
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestStep
3+
commands:
4+
- script: CLUSTER=cluster-start $HOME/postgres-operator/testing/kuttl/script/port-forward.sh
5+
background: true
6+
# this sleep is needed when running in cloud environments to ensure connection-psql.sh
7+
# will not fail when attempting the port-forward and has suffient time to connect, may
8+
# not be needed when running locally
9+
- script: sleep 10
10+
- script: CLUSTER=cluster-start $HOME/postgres-operator/testing/kuttl/script/connect-psql.sh

testing/kuttl/kuttl-test.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: kuttl.dev/v1beta1
2+
kind: TestSuite
3+
testDirs:
4+
- testing/kuttl/e2e/
5+
timeout: 120
6+
parallel: 2
7+
# by default kuttl will run in a generated namespace to override
8+
# that functionality simply uncomment the line below and replace
9+
# postgres-operator with the desired namespace to run in.
10+
# namespace: postgres-operator
11+
suppress:
12+
- events
13+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
CLUSTER=${CLUSTER:-default}
3+
PG_CLUSTER_USER_SECRET_NAME=${PG_CLUSTER_USER_SECRET_NAME:-$CLUSTER-pguser-$CLUSTER}
4+
5+
PGPASSWORD=$(kubectl get secrets -n $NAMESPACE "${PG_CLUSTER_USER_SECRET_NAME}" -o go-template='{{.data.password | base64decode}}') \
6+
PGUSER=$(kubectl get secrets -n $NAMESPACE "${PG_CLUSTER_USER_SECRET_NAME}" -o go-template='{{.data.user | base64decode}}') \
7+
PGDATABASE=$(kubectl get secrets -n $NAMESPACE "${PG_CLUSTER_USER_SECRET_NAME}" -o go-template='{{.data.dbname | base64decode}}') \
8+
psql -h localhost -c "select version();"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
CLUSTER=${CLUSTER:-default}
3+
export PG_CLUSTER_PRIMARY_POD=$(kubectl get pod -n $NAMESPACE -o name -l postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master)
4+
kubectl -n $NAMESPACE port-forward "${PG_CLUSTER_PRIMARY_POD}" 5432:5432

0 commit comments

Comments
 (0)