diff --git a/stable/mongodb-replicaset/templates/mongo-up-test.yaml b/stable/mongodb-replicaset/templates/mongo-up-test.yaml new file mode 100644 index 0000000000..8978469cc2 --- /dev/null +++ b/stable/mongodb-replicaset/templates/mongo-up-test.yaml @@ -0,0 +1,37 @@ +apiVersion: v1 +kind: Pod +metadata: + name: {{ template "fullname" . }}-test + annotations: + "helm.sh/hook": test-success + "pod.beta.kubernetes.io/init-containers": '[ + { + "name": "test-framework", + "image": "dduportal/bats:0.4.0", + "command": ["bash", "-c", " + set -ex\n + # copy bats to tools dir\n + cp -R /usr/local/libexec/ /tools/bats/\n + "], + "volumeMounts": [ + {"name": "tools", "mountPath": "/tools"} + ] + } + ]' +spec: + containers: + - name: mongo + image: "{{ .Values.image.name }}:{{ .Values.image.tag }}" + command: ["/tools/bats/bats", "-t", "/tests/mongo-up-test.sh"] + volumeMounts: + - name: tools + mountPath: /tools + - name: tests + mountPath: /tests + volumes: + - name: tools + emptyDir: {} + - name: tests + configMap: + name: {{ template "fullname" . }}-tests + restartPolicy: Never diff --git a/stable/mongodb-replicaset/templates/mongodb-up-test-config.yaml b/stable/mongodb-replicaset/templates/mongodb-up-test-config.yaml new file mode 100644 index 0000000000..e8ce946c8a --- /dev/null +++ b/stable/mongodb-replicaset/templates/mongodb-up-test-config.yaml @@ -0,0 +1,85 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ template "fullname" . }}-tests + labels: + app: {{ template "fullname" . }}-tests +data: + mongo-up-test.sh: | + pod_name() { + echo {{ template "fullname" . }}-$1.{{ template "fullname" . }}.default.svc.cluster.local + } + + master_pod() { + for i in {0..2} + do + response=$(mongo \ + --host=$(pod_name $i) \ + --eval="rs.isMaster().ismaster") + if [[ $response =~ "true" ]]; then + echo $(pod_name $i) + break + fi + done + } + + setup() { + ready=0 + until [[ $ready -eq 3 ]]; + do + echo "Waiting for application to become ready" >&2 + sleep 1 + ready=0 + + for i in {0..2} + do + response=$(mongo \ + --host=$(pod_name $i) \ + --eval="rs.status()") || true + if [[ $response =~ .*ok.* ]]; then + ready=$((ready+1)) + fi + done + done + } + + @test "Testing mongodb client is accessible" { + mongo -h + [ "$?" -eq 0 ] + } + + @test "Connect mongodb client to mongodb pods" { + for i in {0..2} + do + response=$(mongo \ + --host=$(pod_name $i) \ + --eval="rs.status()") + if [[ ! $response =~ .*ok.* ]]; then + exit 1 + fi + done + } + + @test "Write key to master" { + response=$(mongo \ + --host=$(master_pod) \ + --eval="db.test.insert({\"abc\": \"def\"}).nInserted") + if [[ ! $response =~ "1" ]]; then + exit 1 + fi + } + + @test "Read key from slaves" { + # wait for slaves to catch up + sleep 10 + + for i in {0..2} + do + response=$(mongo \ + --host=$(pod_name $i) \ + --eval="rs.slaveOk(); db.test.find({\"abc\":\"def\"})") + if [[ ! $response =~ .*def.* ]]; then + exit 1 + fi + done + }