Skip to content

Commit 1378269

Browse files
committed
Add tests for kubectl-rabbitmq
See #440
1 parent dda2be1 commit 1378269

File tree

2 files changed

+216
-53
lines changed

2 files changed

+216
-53
lines changed

bin/kubectl-rabbitmq

+65-53
Original file line numberDiff line numberDiff line change
@@ -16,53 +16,59 @@ password=""
1616
service=""
1717

1818
usage() {
19-
echo "USAGE:"
20-
echo " Install RabbitMQ Cluster Operator (optionally provide image to use a relocated image or a specific version)"
21-
echo " kubectl rabbitmq install-cluster-operator [IMAGE]"
22-
echo
23-
echo " Open Management UI for an instance"
24-
echo " kubectl rabbitmq manage INSTANCE"
25-
echo
26-
echo " Print default-user secrets for an instance"
27-
echo " kubectl rabbitmq secrets INSTANCE"
28-
echo
29-
echo " List all RabbitMQ clusters"
30-
echo " kubectl rabbitmq list"
31-
echo
32-
echo " Delete a RabbitMQ cluster (or multiple clusters)"
33-
echo " kubectl rabbitmq delete INSTANCE ..."
34-
echo
35-
echo " Create a RabbitMQ custom resource - INSTANCE name required, all other flags optional"
36-
echo " kubectl rabbitmq create INSTANCE --replicas 1 --service ClusterIP --image rabbitmq:3.8.9-management --image-pull-secret mysecret"
37-
echo " --tls-secret secret-name --storage-class mystorageclass"
38-
echo
39-
echo " Get a RabbitMQ custom resource and dependant objects"
40-
echo " kubectl rabbitmq get INSTANCE"
41-
echo
42-
echo " Set log level to 'debug' on all nodes"
43-
echo " kubectl rabbitmq debug INSTANCE"
44-
echo
45-
echo " Run 'rabbitmq-diagnostics observer' on a specific INSTANCE NODE"
46-
echo " kubectl rabbitmq observe INSTANCE 0"
47-
echo
48-
echo " Enable all feature flags on an INSTANCE"
49-
echo " kubectl rabbitmq enable-all-feature-flags INSTANCE"
50-
echo
51-
echo " Run perf-test against an instance - you can pass as many perf test parameters as you want"
52-
echo " kubectl rabbitmq perf-test INSTANCE --rate 100"
53-
echo
54-
echo "If you want to monitor perf-test, create the following ServiceMonitor"
55-
echo " apiVersion: monitoring.coreos.com/v1"
56-
echo " kind: ServiceMonitor"
57-
echo " metadata:"
58-
echo " name: kubectl-perf-test"
59-
echo " spec:"
60-
echo " endpoints:"
61-
echo " - interval: 15s"
62-
echo " targetPort: 8080"
63-
echo " selector:"
64-
echo " matchLabels:"
65-
echo " app: perf-test"
19+
usage=$(cat <<-END
20+
USAGE:
21+
Install (latest released) RabbitMQ Cluster Operator
22+
kubectl rabbitmq install-cluster-operator
23+
24+
Create a RabbitMQ custom resource - INSTANCE name required, all other flags optional
25+
kubectl rabbitmq create INSTANCE --replicas 1 --service ClusterIP --image rabbitmq:3.8.9-management --image-pull-secret mysecret
26+
--tls-secret secret-name --storage-class mystorageclass
27+
28+
Get a RabbitMQ custom resource and dependant objects
29+
kubectl rabbitmq get INSTANCE
30+
31+
List all RabbitMQ clusters
32+
kubectl rabbitmq list
33+
34+
Delete a RabbitMQ cluster (or multiple clusters)
35+
kubectl rabbitmq delete INSTANCE ...
36+
37+
Print default-user secrets for an instance
38+
kubectl rabbitmq secrets INSTANCE
39+
40+
Open Management UI for an instance
41+
kubectl rabbitmq manage INSTANCE
42+
43+
Set log level to 'debug' on all nodes
44+
kubectl rabbitmq debug INSTANCE
45+
46+
Run 'rabbitmq-diagnostics observer' on a specific INSTANCE NODE
47+
kubectl rabbitmq observe INSTANCE 0
48+
49+
Enable all feature flags on an INSTANCE
50+
kubectl rabbitmq enable-all-feature-flags INSTANCE
51+
52+
Run perf-test against an instance - you can pass as many perf test parameters as you want
53+
kubectl rabbitmq perf-test INSTANCE --rate 100
54+
If you want to monitor perf-test, create the following ServiceMonitor:
55+
apiVersion: monitoring.coreos.com/v1
56+
kind: ServiceMonitor
57+
metadata:
58+
name: kubectl-perf-test
59+
spec:
60+
endpoints:
61+
- interval: 15s
62+
targetPort: 8080
63+
selector:
64+
matchLabels:
65+
app: perf-test
66+
67+
Print this help
68+
kubectl rabbitmq help
69+
END
70+
)
71+
echo "$usage"
6672
}
6773

6874
get_instance_details() {
@@ -119,7 +125,6 @@ create() {
119125
fi
120126

121127
while [[ "$#" -ne 0 ]]; do
122-
#statements
123128
case "$1" in
124129
"--replicas")
125130
shift 1
@@ -161,9 +166,8 @@ create() {
161166
shift 1
162167
;;
163168
*)
164-
# Unrecognised or unsupported option
165169
echo "Option '$1' not recongnised"
166-
shift 1
170+
exit 1
167171
;;
168172
esac
169173
done
@@ -286,13 +290,21 @@ main() {
286290
;;
287291
"install-cluster-operator")
288292
shift 1
289-
if [[ "$#" -gt 1 ]]; then
293+
if [[ "$#" -gt 0 ]]; then
290294
usage
291295
exit 1
292296
fi
293-
install_cluster_operator "${1:-}"
297+
install_cluster_operator
298+
;;
299+
"help")
300+
usage
301+
;;
302+
"--help")
303+
usage
304+
;;
305+
"-h")
306+
usage
294307
;;
295-
296308
*)
297309
usage
298310
exit 1

bin/kubectl-rabbitmq.bats

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/usr/bin/env bats
2+
3+
eventually() {
4+
assertion="$1"
5+
timeout_in_seconds="$2"
6+
7+
for _ in $(seq 0 "$timeout_in_seconds")
8+
do
9+
if eval "$assertion" ; then
10+
return 0
11+
fi
12+
sleep 1
13+
done
14+
15+
echo assertion timed out: "$assertion"
16+
return 1
17+
}
18+
19+
@test "install-cluster-operator with too many args fails" {
20+
run kubectl rabbitmq install-cluster-operator too-many-args
21+
22+
[ "$status" -eq 1 ]
23+
[ "${lines[0]}" = "USAGE:" ]
24+
}
25+
26+
@test "install-cluster-operator installs cluster operator" {
27+
kubectl rabbitmq install-cluster-operator
28+
29+
eventually 'kubectl -n rabbitmq-system get deployment rabbitmq-cluster-operator | grep 1/1' 600
30+
}
31+
32+
@test "create creates RabbitMQ cluster" {
33+
kubectl rabbitmq create bats-default
34+
35+
eventually '[[ $(kubectl get rabbitmqcluster bats-default -o json | jq -r '"'"'.status.conditions | .[] | select(.type=="AllReplicasReady").status'"'"') == "True" ]]' 600
36+
}
37+
38+
@test "create with invalid flag does not create RabbitMQ cluster" {
39+
run kubectl rabbitmq create bats-invalid \
40+
--replicas 3 \
41+
--invalid "flag"
42+
43+
[ "$status" -eq 1 ]
44+
[[ "${lines[0]}" == "Option '--invalid' not recongnised" ]]
45+
}
46+
47+
@test "create with flags creates RabbitMQ cluster configured accordingly" {
48+
replicas=3
49+
image="rabbitmq:3.8.8"
50+
service="NodePort"
51+
storage_class="my-storage-class"
52+
53+
kubectl rabbitmq create bats-configured \
54+
--replicas "$replicas" \
55+
--image "$image" \
56+
--service "$service" \
57+
--storage-class "$storage_class"
58+
59+
sleep 10 # let the RabbitMQ controller create the K8s objects
60+
61+
sts_spec=$(kubectl get statefulset bats-configured-rabbitmq-server -o jsonpath='{.spec}')
62+
[[ $(jq -r '.replicas' <<< "$sts_spec") -eq "$replicas" ]]
63+
[[ $(jq -r '.template.spec.containers | .[0].image' <<< "$sts_spec") == "$image" ]]
64+
65+
[[ $(kubectl get service bats-configured-rabbitmq-client -o jsonpath='{.spec.type}') == "$service" ]]
66+
[[ $(kubectl get pvc persistence-bats-configured-rabbitmq-server-0 -o jsonpath='{.spec.storageClassName}') == "$storage_class" ]]
67+
}
68+
69+
@test "list lists all RabbitMQ clusters" {
70+
run kubectl rabbitmq list
71+
72+
[ "$status" -eq 0 ]
73+
[[ "${lines[0]}" =~ ^NAME ]]
74+
[[ "${lines[1]}" =~ ^bats-configured ]]
75+
[[ "${lines[2]}" =~ ^bats-default ]]
76+
}
77+
78+
@test "get gets child resources" {
79+
run kubectl rabbitmq get bats-default
80+
81+
[ "$status" -eq 0 ]
82+
[[ "$output" == *"statefulset.apps/bats-default-rabbitmq-server"* ]]
83+
[[ "$output" == *"pod/bats-default-rabbitmq-server-0"* ]]
84+
[[ "$output" == *"service/bats-default-rabbitmq-headless"* ]]
85+
[[ "$output" == *"service/bats-default-rabbitmq-client"* ]]
86+
[[ "$output" == *"configmap/bats-default-rabbitmq-server-conf"* ]]
87+
[[ "$output" == *"configmap/bats-default-rabbitmq-plugins-conf"* ]]
88+
[[ "$output" == *"secret/bats-default-rabbitmq-default-user"* ]]
89+
[[ "$output" == *"secret/bats-default-rabbitmq-erlang-cookie"* ]]
90+
}
91+
92+
@test "secrets prints secrets of default-user" {
93+
run kubectl rabbitmq secrets bats-default
94+
95+
[ "$status" -eq 0 ]
96+
# 24 bytes base64 encoded makes 32 characters
97+
username_regex='^username: .{32}$'
98+
password_regex='^password: .{32}$'
99+
[[ "${lines[0]}" =~ $username_regex ]]
100+
[[ "${lines[1]}" =~ $password_regex ]]
101+
}
102+
103+
@test "enable-all-feature-flags enables all feature flags" {
104+
kubectl rabbitmq enable-all-feature-flags bats-default
105+
106+
states=$(kubectl exec bats-default-rabbitmq-server-0 -- rabbitmqctl list_feature_flags --silent state --formatter=json)
107+
[[ $(jq 'map(select(.state=="disabled")) | length' <<< "$states") -eq 0 ]]
108+
}
109+
110+
@test "perf-test runs perf-test" {
111+
kubectl rabbitmq perf-test bats-default --rate 1
112+
113+
eventually "kubectl exec bats-default-rabbitmq-server-0 -- rabbitmqctl list_connections client_properties | grep perf-test " 600
114+
115+
kubectl delete pod -l "app=perf-test,run=perf-test"
116+
kubectl delete svc -l "app=perf-test,run=perf-test"
117+
}
118+
119+
@test "debug sets log level to debug" {
120+
kubectl rabbitmq debug bats-default
121+
122+
# '[debug] <pid> Lager installed handler' is logged even without enabling debug logging
123+
eventually "kubectl logs bats-default-rabbitmq-server-0 | grep -v ' \[debug\] .* Lager installed handler ' | grep ' \[debug\] '" 30
124+
}
125+
126+
@test "delete deletes RabbitMQ cluster" {
127+
kubectl rabbitmq delete bats-configured bats-default
128+
129+
[[ $(kubectl get rabbitmqclusters -o jsonpath='{.items}' | jq length) -eq 0 ]]
130+
}
131+
132+
@test "help prints help" {
133+
run kubectl rabbitmq help
134+
135+
[ "$status" -eq 0 ]
136+
[ "${lines[0]}" = "USAGE:" ]
137+
}
138+
139+
@test "--help prints help" {
140+
run kubectl rabbitmq --help
141+
142+
[ "$status" -eq 0 ]
143+
[ "${lines[0]}" = "USAGE:" ]
144+
}
145+
146+
@test "-h prints help" {
147+
run kubectl rabbitmq -h
148+
149+
[ "$status" -eq 0 ]
150+
[ "${lines[0]}" = "USAGE:" ]
151+
}

0 commit comments

Comments
 (0)