|
| 1 | +apiVersion: kuttl.dev/v1beta1 |
| 2 | +kind: TestAssert |
| 3 | +commands: |
| 4 | +# First, check that all containers in the instance pod are ready. |
| 5 | +# Then, grab the collector metrics output and check that a metric from both 5m |
| 6 | +# and 5s queries are present, as well as patroni metrics. |
| 7 | +# Then, check the collector logs for patroni, pgbackrest, and postgres logs. |
| 8 | +# Finally, ensure the monitoring user exists and is configured. |
| 9 | +- script: | |
| 10 | + retry() { bash -ceu 'printf "$1\nSleeping...\n" && sleep 5' - "$@"; } |
| 11 | + check_containers_ready() { bash -ceu 'echo "$1" | jq -e ".[] | select(.type==\"ContainersReady\") | .status==\"True\""' - "$@"; } |
| 12 | + contains() { bash -ceu '[[ "$1" == *"$2"* ]]' - "$@"; } |
| 13 | +
|
| 14 | + pod=$(kubectl get pods -o name -n "${NAMESPACE}" \ |
| 15 | + -l postgres-operator.crunchydata.com/cluster=otel-cluster,postgres-operator.crunchydata.com/data=postgres) |
| 16 | + [ "$pod" = "" ] && retry "Pod not found" && exit 1 |
| 17 | +
|
| 18 | + condition_json=$(kubectl get "${pod}" -n "${NAMESPACE}" -o jsonpath="{.status.conditions}") |
| 19 | + [ "$condition_json" = "" ] && retry "conditions not found" && exit 1 |
| 20 | + { check_containers_ready "$condition_json"; } || { |
| 21 | + retry "containers not ready" |
| 22 | + exit 1 |
| 23 | + } |
| 24 | +
|
| 25 | + scrape_metrics=$(kubectl exec "${pod}" -c collector -n "${NAMESPACE}" -- \ |
| 26 | + curl --insecure --silent http://localhost:9187/metrics) |
| 27 | + { contains "${scrape_metrics}" 'ccp_connection_stats_active'; } || { |
| 28 | + retry "5 second metric not found" |
| 29 | + exit 1 |
| 30 | + } |
| 31 | + { contains "${scrape_metrics}" 'ccp_database_size_bytes'; } || { |
| 32 | + retry "5 minute metric not found" |
| 33 | + exit 1 |
| 34 | + } |
| 35 | + { contains "${scrape_metrics}" 'patroni_postgres_running'; } || { |
| 36 | + retry "patroni metric not found" |
| 37 | + exit 1 |
| 38 | + } |
| 39 | +
|
| 40 | + logs=$(kubectl logs "${pod}" --namespace "${NAMESPACE}" -c collector | grep InstrumentationScope) |
| 41 | + { contains "${logs}" 'InstrumentationScope patroni'; } || { |
| 42 | + retry "patroni logs not found" |
| 43 | + exit 1 |
| 44 | + } |
| 45 | + { contains "${logs}" 'InstrumentationScope pgbackrest'; } || { |
| 46 | + retry "pgbackrest logs not found" |
| 47 | + exit 1 |
| 48 | + } |
| 49 | + { contains "${logs}" 'InstrumentationScope postgres'; } || { |
| 50 | + retry "postgres logs not found" |
| 51 | + exit 1 |
| 52 | + } |
| 53 | +
|
| 54 | + kubectl exec --stdin "${pod}" --namespace "${NAMESPACE}" -c database \ |
| 55 | + -- psql -qb --set ON_ERROR_STOP=1 --file=- <<'SQL' |
| 56 | + DO $$ |
| 57 | + DECLARE |
| 58 | + result record; |
| 59 | + BEGIN |
| 60 | + SELECT * INTO result FROM pg_catalog.pg_roles WHERE rolname = 'ccp_monitoring'; |
| 61 | + ASSERT FOUND, 'user not found'; |
| 62 | + END $$ |
| 63 | + SQL |
0 commit comments