|
| 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