Skip to content

Commit 5ce5cd9

Browse files
committed
Chainsaw test for PGO CLI show
1 parent 65101b3 commit 5ce5cd9

File tree

1 file changed

+374
-0
lines changed

1 file changed

+374
-0
lines changed
Lines changed: 374 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,374 @@
1+
apiVersion: chainsaw.kyverno.io/v1alpha1
2+
kind: Test
3+
metadata:
4+
name: chainsaw-show
5+
spec:
6+
skipDelete: true
7+
bindings:
8+
- name: cluster
9+
value: chainsaw-show
10+
11+
- name: postgresVersion
12+
value: (to_string($values.versions.postgres))
13+
14+
steps:
15+
16+
- name: 'Create Cluster with PGO CLI'
17+
use:
18+
template: '../templates/create-cluster.yaml'
19+
20+
- name: 'Confirm cluster is created'
21+
use:
22+
template: '../templates/confirm-created.yaml'
23+
24+
- name: 'Confirm Replica backup completed'
25+
use:
26+
template: '../templates/replica-backup-complete.yaml'
27+
28+
29+
- name: Show pgbackrest info
30+
try:
31+
- script:
32+
env:
33+
- name: "NAMESPACE"
34+
value: ($namespace)
35+
- name: "CLUSTER"
36+
value: ($cluster)
37+
content: |
38+
39+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
40+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
41+
EXEC_INFO=$(
42+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
43+
pgbackrest info
44+
)
45+
CLI_INFO=$(
46+
kubectl-pgo --namespace "${NAMESPACE}" show backup $CLUSTER
47+
)
48+
status=$?
49+
if [ "$status" -ne 0 ]; then
50+
echo "pgo command unsuccessful"
51+
exit 1
52+
fi
53+
54+
# check command output is not empty and equals 'exec' output
55+
if [ -n "$CLI_INFO" ] && [ "$EXEC_INFO" = "$CLI_INFO" ]; then
56+
exit 0
57+
fi
58+
59+
exit 1
60+
61+
- name: Show pgbackrest info repo1
62+
try:
63+
- script:
64+
env:
65+
- name: "NAMESPACE"
66+
value: ($namespace)
67+
- name: "CLUSTER"
68+
value: ($cluster)
69+
content: |
70+
71+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
72+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
73+
EXEC_INFO=$(
74+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
75+
pgbackrest info --repo=1
76+
)
77+
CLI_INFO=$(
78+
kubectl-pgo --namespace "${NAMESPACE}" show backup $CLUSTER --repoName=repo1
79+
)
80+
status=$?
81+
if [ "$status" -ne 0 ]; then
82+
echo "pgo command unsuccessful"
83+
exit 1
84+
fi
85+
86+
# check command output is not empty and equals 'exec' output
87+
if [ -n "$CLI_INFO" ] && [ "$EXEC_INFO" = "$CLI_INFO" ]; then
88+
exit 0
89+
fi
90+
91+
exit 1
92+
93+
94+
- name: Show pgbackrest info output json
95+
try:
96+
- script:
97+
env:
98+
- name: "NAMESPACE"
99+
value: ($namespace)
100+
- name: "CLUSTER"
101+
value: ($cluster)
102+
content: |
103+
104+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
105+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
106+
EXEC_INFO=$(
107+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
108+
pgbackrest info --output=json
109+
)
110+
CLI_INFO=$(
111+
kubectl-pgo --namespace "${NAMESPACE}" show backup $CLUSTER --output=json
112+
)
113+
status=$?
114+
if [ "$status" -ne 0 ]; then
115+
echo "pgo command unsuccessful"
116+
exit 1
117+
fi
118+
119+
# check command output is not empty and equals 'exec' output
120+
if [ -n "$CLI_INFO" ] && [ "$EXEC_INFO" = "$CLI_INFO" ]; then
121+
exit 0
122+
fi
123+
124+
exit 1
125+
126+
- name: Show pgbackrest info output bad format
127+
try:
128+
- script:
129+
env:
130+
- name: "NAMESPACE"
131+
value: ($namespace)
132+
- name: "CLUSTER"
133+
value: ($cluster)
134+
content: |
135+
136+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
137+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
138+
CLI_INFO=$(
139+
kubectl-pgo --namespace "${NAMESPACE}" show backup $CLUSTER -o bad 2>&1
140+
)
141+
status=$?
142+
if [ "$status" -ne 1 ]; then
143+
echo "expected bad format to fail"
144+
exit 1
145+
fi
146+
147+
# check command output is not empty and contains the expected error
148+
# Note: case is used as it allows for the use of a wildcard (*)
149+
# and is POSIX compliant
150+
case "$CLI_INFO" in
151+
"")
152+
exit 1
153+
;;
154+
*"must be one of \"text\", \"json\""*)
155+
exit 0
156+
;;
157+
esac
158+
159+
exit 1
160+
161+
162+
- name: Show patronictl list
163+
try:
164+
- script:
165+
env:
166+
- name: "NAMESPACE"
167+
value: ($namespace)
168+
- name: "CLUSTER"
169+
value: ($cluster)
170+
content: |
171+
172+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
173+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
174+
EXEC_INFO=$(
175+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
176+
patronictl list
177+
)
178+
CLI_HA=$(
179+
kubectl-pgo --namespace "${NAMESPACE}" show ha $CLUSTER
180+
)
181+
status=$?
182+
if [ "$status" -ne 0 ]; then
183+
echo "pgo command unsuccessful"
184+
exit 1
185+
fi
186+
187+
# check command output is not empty and equals 'exec' output
188+
if [ -n "$CLI_HA" ] && [ "$EXEC_INFO" = "$CLI_HA" ]; then
189+
exit 0
190+
fi
191+
192+
exit 1
193+
194+
195+
- name: Show patronictl list output json
196+
try:
197+
- script:
198+
env:
199+
- name: "NAMESPACE"
200+
value: ($namespace)
201+
- name: "CLUSTER"
202+
value: ($cluster)
203+
content: |
204+
205+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
206+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
207+
EXEC_INFO=$(
208+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
209+
patronictl list -f json
210+
)
211+
CLI_HA=$(
212+
kubectl-pgo --namespace "${NAMESPACE}" show ha $CLUSTER -o json
213+
)
214+
status=$?
215+
if [ "$status" -ne 0 ]; then
216+
echo "pgo command unsuccessful"
217+
exit 1
218+
fi
219+
220+
# check command output is not empty and equals 'exec' output
221+
if [ -n "$CLI_HA" ] && [ "$EXEC_INFO" = "$CLI_HA" ]; then
222+
exit 0
223+
fi
224+
225+
exit 1
226+
227+
- name: Show patronictl list output bad
228+
try:
229+
- script:
230+
env:
231+
- name: "NAMESPACE"
232+
value: ($namespace)
233+
- name: "CLUSTER"
234+
value: ($cluster)
235+
content: |
236+
237+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
238+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
239+
240+
CLI_HA=$(
241+
kubectl-pgo --namespace "${NAMESPACE}" show ha $CLUSTER -o bad 2>&1
242+
)
243+
status=$?
244+
if [ "$status" -ne 1 ]; then
245+
echo "expected bad format to fail"
246+
exit 1
247+
fi
248+
249+
# check command output contains the expected error
250+
# Note: case is used as it allows for the use of a wildcard (*)
251+
# and is POSIX compliant
252+
case "$CLI_HA" in
253+
"")
254+
exit 1
255+
;;
256+
*"must be one of \"pretty\", \"tsv\", \"json\", \"yaml\""*)
257+
exit 0
258+
;;
259+
esac
260+
261+
exit 1
262+
263+
264+
- name: Show user
265+
try:
266+
- script:
267+
env:
268+
- name: "NAMESPACE"
269+
value: ($namespace)
270+
- name: "CLUSTER"
271+
value: ($cluster)
272+
content: |
273+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
274+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
275+
276+
CLI_USER=$(
277+
kubectl-pgo --namespace "${NAMESPACE}" show user --cluster $CLUSTER
278+
)
279+
280+
status=$?
281+
if [ "$status" -ne 0 ]; then
282+
echo "pgo command unsuccessful"
283+
exit 1
284+
fi
285+
286+
# expected output
287+
SHOW_USER_OUTPUT="
288+
CLUSTER USERNAME
289+
chainsaw-show chainsaw-show"
290+
291+
292+
# check command output is not empty and equals the expected output
293+
if [ -z ${CLI_USER} ] || [ "${CLI_USER}" != "${SHOW_USER_OUTPUT}" ]; then
294+
echo "pgo command output unexpected: expected ${SHOW_USER_OUTPUT} got ${CLI_USER}"
295+
exit 1
296+
fi
297+
298+
CLI_USER_SENSITIVE=$(
299+
echo yes | kubectl-pgo --namespace "${NAMESPACE}" show user --cluster ${CLUSTER} --show-connection-info
300+
)
301+
302+
SECRET_DATA=$(kubectl get secret -n "${NAMESPACE}" ${CLUSTER}-pguser-${CLUSTER} -o jsonpath={.data})
303+
304+
PASSWORD=$(echo "${SECRET_DATA}" | jq -r .password | base64 -d)
305+
USER=$(echo "${SECRET_DATA}" | jq -r .user | base64 -d)
306+
HOST=$(echo "${SECRET_DATA}" | jq -r .host | base64 -d)
307+
PORT=$(echo "${SECRET_DATA}" | jq -r .port | base64 -d)
308+
309+
# check command output is not empty and contains the connection URL field
310+
# Note: case is used as it allows for the use of a wildcard (*)
311+
# and is POSIX compliant
312+
case "$CLI_USER_SENSITIVE" in
313+
"")
314+
exit 1
315+
;;
316+
*"postgres://${USER}:${PASSWORD}@${HOST}:${PORT}/${CLUSTER}"*)
317+
exit 0
318+
;;
319+
esac
320+
321+
echo "pgo command output for connection info unexpected: got ${CLI_USER_SENSITIVE}"
322+
exit 1
323+
324+
325+
- name: Show entire command
326+
try:
327+
- script:
328+
env:
329+
- name: "NAMESPACE"
330+
value: ($namespace)
331+
- name: "CLUSTER"
332+
value: ($cluster)
333+
content: |
334+
SELECTOR=postgres-operator.crunchydata.com/cluster=$CLUSTER,postgres-operator.crunchydata.com/role=master
335+
PRIMARY=$(kubectl get pod --namespace "${NAMESPACE}" --output name --selector ${SELECTOR})
336+
337+
PGBACKREST_INFO_EXEC=$(
338+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
339+
pgbackrest info
340+
)
341+
342+
PATRONI_LIST_EXEC=$(
343+
kubectl exec --namespace "${NAMESPACE}" "${PRIMARY}" -- \
344+
patronictl list
345+
)
346+
347+
EXPECTED_SHOW_COMMAND="BACKUP
348+
349+
$PGBACKREST_INFO_EXEC
350+
351+
HA
352+
353+
$PATRONI_LIST_EXEC"
354+
355+
echo $EXPECTED_SHOW_COMMAND
356+
357+
CLI_SHOW=$(
358+
kubectl-pgo --namespace "${NAMESPACE}" show $CLUSTER
359+
)
360+
361+
status=$?
362+
if [ "$status" -ne 0 ]; then
363+
echo "pgo command unsuccessful"
364+
exit 1
365+
fi
366+
367+
# check command output is not empty and equals expected output
368+
if [ -n "$CLI_SHOW" ] && [ "$EXPECTED_SHOW_COMMAND" = "$CLI_SHOW" ]; then
369+
exit 0
370+
fi
371+
372+
exit 1
373+
374+

0 commit comments

Comments
 (0)