Skip to content

Commit 8b01d09

Browse files
Adopting according to new readability and code style (#553)
* Adopting according to new readability and code style * Removing whitespace * Making shellcheck happy * chore(test): revert previous styling changes See: #553 (comment) * chore(test): wrap variables in double quotes This would be picked up by `shellcheck` in CI if the script wasn't inlined. We should consider moving test scripts out of yaml files. * chore(test): use bash strict mode According to [kuttl docs], scripts are executed via `sh -c`. The docs do not suggest if strict mode is enabled, so we should set it. There also doesn't seem to be a way to choose the shell (whithout calling a shell youself from within the script). Without strict mode on, only the final commands exist status is considered - meaning assertions before the last could fail and never be picked up. [kuttl docs]: https://kuttl.dev/docs/testing/steps.html#shell-scripts * chore(test): add comments to tests It might seem trivial, but it can help reviewers quickly understand what the command line is for, and quickly assert whether that lines up with what the command line is actually doing. * chore(test): make assertions more precise with exact matching with grep We should test exact matches with grep. By default, `yq` will output strings with double quotes, so those should be removed with `-r` or `--unwrapScalar` for yq-go (the python yq which has the same options as jq can use `-r` or `--raw-output`). Note: I just leart about the `-w` option, which would remove the need for '^$'. * chore(test): split long commands into multi-line commands, split label options into multiple args Instead of splitting by line-length, I am: - Splitting by pipe - Splitting by flag/option This is to make reviews simpler when using split panes (side-by-side). * chore(test): suggestion: use long-options where possible I went to town on this one. You could argue that trivial cases (eg: `sed -e`) are ok. This is just for demonstration. * chore(test): fix yamllint * Revert "chore(test): suggestion: use long-options where possible" This reverts commit fc6fd79. * chore(test): fix yamllint * chore(test): remove -o pipefail bash option * chore(test): undo ORed labels --------- Co-authored-by: Nick Larsen <[email protected]>
1 parent 4b052f6 commit 8b01d09

File tree

1 file changed

+97
-16
lines changed

1 file changed

+97
-16
lines changed

tests/templates/kuttl/overrides/31-assert.yaml

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,104 @@ apiVersion: kuttl.dev/v1beta1
33
kind: TestAssert
44
timeout: 30
55
commands:
6+
# master, default RG
67
- script: |
7-
POD=$(kubectl -n $NAMESPACE get pod -l app.kubernetes.io/component=master,app.kubernetes.io/role-group=default -o name | head -n 1 | sed -e 's#pod/##')
8-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_MASTER").value' | grep 'MASTER'
9-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_MRG").value' | grep 'MASTER'
10-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' | grep 'MASTER_RG'
8+
set -eu
9+
10+
# Get the name of the first pod by labels
11+
POD=$(
12+
kubectl get pod -n "$NAMESPACE" \
13+
-l app.kubernetes.io/component=master,app.kubernetes.io/role-group=default \
14+
-o name \
15+
| head -n 1 \
16+
| sed -e 's#pod/##'
17+
)
18+
19+
# Assert that environment variables have the correct values
20+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
21+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_MASTER").value' \
22+
| grep '^MASTER$'
23+
24+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
25+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_MRG").value' \
26+
| grep '^MASTER$'
27+
28+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
29+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' \
30+
| grep '^MASTER_RG$'
31+
32+
# regionserver, resources-from-role RG
1133
- script: |
12-
POD=$(kubectl -n $NAMESPACE get pod -l app.kubernetes.io/component=regionserver,app.kubernetes.io/role-group=resources-from-role -o name | head -n 1 | sed -e 's#pod/##')
13-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RS").value' | grep 'REGIONSERVER'
14-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RFR").value' | grep 'REGIONSERVER'
15-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' | grep 'REGIONSERVER_RFR'
34+
set -eu
35+
36+
# Get the name of the first pod by labels
37+
POD=$(
38+
kubectl get pod -n "$NAMESPACE" \
39+
-l app.kubernetes.io/component=regionserver,app.kubernetes.io/role-group=resources-from-role \
40+
-o name \
41+
| head -n 1 \
42+
| sed -e 's#pod/##'
43+
)
44+
45+
# Assert that environment variables have the correct values
46+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
47+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RS").value' \
48+
| grep '^REGIONSERVER$'
49+
50+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
51+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RFR").value' \
52+
| grep '^REGIONSERVER$'
53+
54+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
55+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' \
56+
| grep '^REGIONSERVER_RFR$'
57+
58+
# regionserver, resources-from-role-group RG
1659
- script: |
17-
POD=$(kubectl -n $NAMESPACE get pod -l app.kubernetes.io/component=regionserver,app.kubernetes.io/role-group=resources-from-role-group -o name | head -n 1 | sed -e 's#pod/##')
18-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RS").value' | grep 'REGIONSERVER'
19-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RFRG").value' | grep 'REGIONSERVER'
20-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' | grep 'REGIONSERVER_RFRG'
60+
set -eu
61+
62+
# Get the name of the first pod by labels
63+
POD=$(
64+
kubectl get pod \
65+
-n "$NAMESPACE" \
66+
-l app.kubernetes.io/component=regionserver,app.kubernetes.io/role-group=resources-from-role-group \
67+
-o name | head -n 1 | sed -e 's#pod/##'
68+
)
69+
70+
# Assert that environment variables have the correct values
71+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
72+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RS").value' \
73+
| grep '^REGIONSERVER$'
74+
75+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
76+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_RFRG").value' \
77+
| grep '^REGIONSERVER$'
78+
79+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
80+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' \
81+
| grep '^REGIONSERVER_RFRG$'
82+
83+
# restserver, default RG
2184
- script: |
22-
POD=$(kubectl -n $NAMESPACE get pod -l app.kubernetes.io/component=restserver,app.kubernetes.io/role-group=default -o name | head -n 1 | sed -e 's#pod/##')
23-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_REST").value' | grep 'RESTSERVER'
24-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_REST_RG").value' | grep 'RESTSERVER'
25-
kubectl -n $NAMESPACE get pod $POD -o yaml | yq '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' | grep 'RESTSERVER_RG'
85+
set -eu
86+
87+
# Get the name of the first pod by labels
88+
POD=$(
89+
kubectl get pod \
90+
-n "$NAMESPACE" \
91+
-l app.kubernetes.io/component=restserver,app.kubernetes.io/role-group=default \
92+
-o name | head -n 1 | sed -e 's#pod/##'
93+
)
94+
95+
# Assert that environment variables have the correct values
96+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
97+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_REST").value' \
98+
| grep '^RESTSERVER$'
99+
100+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
101+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR_FROM_REST_RG").value' \
102+
| grep '^RESTSERVER$'
103+
104+
kubectl get pod "$POD" -n "$NAMESPACE" -o yaml \
105+
| yq -r '.spec.containers[0].env[] | select (.name == "TEST_VAR").value' \
106+
| grep '^RESTSERVER_RG$'

0 commit comments

Comments
 (0)