Skip to content

K8SPG-590 Add e2e test for database init sql functionality #1215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e-tests/run-minikube.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
backup-enable-disable
custom-extensions
custom-tls
database-init-sql
demand-backup
finalizers
init-deploy
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/run-pr.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
backup-enable-disable
custom-extensions
custom-tls
database-init-sql
demand-backup
finalizers
init-deploy
Expand Down
1 change: 1 addition & 0 deletions e2e-tests/run-release.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
backup-enable-disable
custom-extensions
custom-tls
database-init-sql
demand-backup
finalizers
init-deploy
Expand Down
24 changes: 24 additions & 0 deletions e2e-tests/tests/database-init-sql/00-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 120
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: perconapgclusters.pgv2.percona.com
spec:
group: pgv2.percona.com
names:
kind: PerconaPGCluster
listKind: PerconaPGClusterList
plural: perconapgclusters
singular: perconapgcluster
scope: Namespaced
---
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
metadata:
name: check-operator-deploy-status
timeout: 120
commands:
- script: kubectl assert exist-enhanced deployment percona-postgresql-operator -n ${OPERATOR_NS:-$NAMESPACE} --field-selector status.readyReplicas=1
13 changes: 13 additions & 0 deletions e2e-tests/tests/database-init-sql/00-deploy-operator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
timeout: 10
commands:
- script: |-
set -o errexit
set -o xtrace

source ../../functions
init_temp_dir # do this only in the first TestStep

deploy_operator
deploy_client
12 changes: 12 additions & 0 deletions e2e-tests/tests/database-init-sql/01-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 30
---
apiVersion: v1
kind: ConfigMap
metadata:
name: init-database-sql
data:
init.sql: |
CREATE TABLE e2e_init_table(id INT PRIMARY KEY);
INSERT INTO e2e_init_table VALUES (42);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: init-database-sql
data:
init.sql: |
CREATE TABLE e2e_init_table(id INT PRIMARY KEY);
INSERT INTO e2e_init_table VALUES (42);
88 changes: 88 additions & 0 deletions e2e-tests/tests/database-init-sql/02-assert.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
apiVersion: kuttl.dev/v1beta1
kind: TestAssert
timeout: 300
---
kind: StatefulSet
apiVersion: apps/v1
metadata:
labels:
postgres-operator.crunchydata.com/cluster: database-init-sql
postgres-operator.crunchydata.com/data: postgres
postgres-operator.crunchydata.com/instance-set: instance1
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
name: database-init-sql
controller: true
blockOwnerDeletion: true
status:
observedGeneration: 1
replicas: 1
readyReplicas: 1
updatedReplicas: 1
collisionCount: 0
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: database-init-sql-pgbouncer
labels:
postgres-operator.crunchydata.com/cluster: database-init-sql
postgres-operator.crunchydata.com/role: pgbouncer
annotations:
deployment.kubernetes.io/revision: '1'
ownerReferences:
- apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
name: database-init-sql
controller: true
blockOwnerDeletion: true
status:
observedGeneration: 1
replicas: 3
updatedReplicas: 3
readyReplicas: 3
---
apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: database-init-sql
ownerReferences:
- apiVersion: pgv2.percona.com/v2
kind: PerconaPGCluster
name: database-init-sql
controller: true
blockOwnerDeletion: true
finalizers:
- postgres-operator.crunchydata.com/finalizer
spec:
backups:
pgbackrest: {}
status:
instances:
- name: instance1
readyReplicas: 3
replicas: 3
updatedReplicas: 3
observedGeneration: 1
proxy:
pgBouncer:
readyReplicas: 3
replicas: 3
---
apiVersion: pgv2.percona.com/v2
kind: PerconaPGCluster
metadata:
name: database-init-sql
status:
pgbouncer:
ready: 3
size: 3
postgres:
instances:
- name: instance1
ready: 3
size: 3
ready: 3
size: 3
state: ready
14 changes: 14 additions & 0 deletions e2e-tests/tests/database-init-sql/02-create-cluster.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
timeout: 10
commands:
- script: |-
set -o errexit
set -o xtrace

source ../../functions

get_cr \
| yq '.spec.databaseInitSQL.name = "init-database-sql"' \
| yq '.spec.databaseInitSQL.key = "init.sql"' \
| kubectl -n "${NAMESPACE}" apply -f -
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
commands:
- script: |-
set -o errexit
set -o xtrace

source ../../functions

result=$(run_psql_local \
"SELECT id FROM e2e_init_table WHERE id = 42" \
"postgres:$(get_psql_user_pass database-init-sql-pguser-postgres)@$(get_psql_user_host database-init-sql-pguser-postgres)/postgres")

if [[ "$result" != *"42"* ]]; then
echo "Database has not been initialized"
exit 1
fi
timeout: 360
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: kuttl.dev/v1beta1
kind: TestStep
delete:
- apiVersion: pgv2.percona.com/v2
kind: PerconaPGCluster
metadata:
name: database-init-sql
- apiVersion: postgres-operator.crunchydata.com/v1beta1
kind: PostgresCluster
metadata:
name: database-init-sql
commands:
- script: |-
set -o errexit
set -o xtrace

source ../../functions

remove_all_finalizers
destroy_operator
timeout: 60
Loading