From 0a0702c657914822e1d6806dbd5e319a9424f230 Mon Sep 17 00:00:00 2001 From: Andi Date: Tue, 16 Jul 2024 11:20:52 +0200 Subject: [PATCH] Helm tests for HA deployment (#41) --- .../lint-test-high-availability.yaml | 6 +- charts/memgraph-high-availability/Chart.yaml | 2 +- .../templates/tests/test-cluster-setup.yaml | 67 ++++++++++--------- .../templates/tests/test-connection.yaml | 34 ++++++---- charts/memgraph-high-availability/values.yaml | 1 - 5 files changed, 60 insertions(+), 50 deletions(-) diff --git a/.github/workflows/lint-test-high-availability.yaml b/.github/workflows/lint-test-high-availability.yaml index 8b64f1d..e31f671 100644 --- a/.github/workflows/lint-test-high-availability.yaml +++ b/.github/workflows/lint-test-high-availability.yaml @@ -1,4 +1,4 @@ -name: Lint and Test Charts +name: Lint and Test Charts High Availability on: pull_request: @@ -39,7 +39,6 @@ jobs: - name: Run chart-testing (lint) if: steps.list-changed.outputs.changed == 'true' run: ct lint --target-branch ${{ github.event.repository.default_branch }} - continue-on-error: true - name: Create kind cluster if: steps.list-changed.outputs.changed == 'true' @@ -77,5 +76,4 @@ jobs: - name: Run Helm tests if: steps.list-changed.outputs.changed == 'true' run: | - helm test mem-ha-test - kubectl logs -l app=memgraph + timeout 30 helm test mem-ha-test # If connection to some port is broken, this will timeout with code 124 diff --git a/charts/memgraph-high-availability/Chart.yaml b/charts/memgraph-high-availability/Chart.yaml index 468c44a..c9952e2 100644 --- a/charts/memgraph-high-availability/Chart.yaml +++ b/charts/memgraph-high-availability/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: memgraph-high-availability description: A Helm chart for Kubernetes with Memgraph High availabiliy capabilites -version: 0.1.1 +version: 0.1.2 appVersion: "2.18.0" type: application diff --git a/charts/memgraph-high-availability/templates/tests/test-cluster-setup.yaml b/charts/memgraph-high-availability/templates/tests/test-cluster-setup.yaml index 45b9927..39317b2 100644 --- a/charts/memgraph-high-availability/templates/tests/test-cluster-setup.yaml +++ b/charts/memgraph-high-availability/templates/tests/test-cluster-setup.yaml @@ -10,44 +10,47 @@ spec: template: metadata: labels: - app: memgraph + app: memgraph-ha-test-cluster-setup spec: containers: - name: memgraph-test-cluster-setup - image: memgraph/memgraph:2.17.0 + image: "{{ $.Values.memgraph.image.repository }}:{{ $.Values.memgraph.image.tag }}" command: ["/bin/bash", "-c"] args: - | - echo "Running cluster test setup..." - result=$(echo 'SHOW INSTANCES;' | mgconsole --host memgraph-coordinator-1.default.svc.cluster.local --port 7687) - echo "$result" + instances=$(echo 'SHOW INSTANCES;' | mgconsole --host memgraph-coordinator-1.default.svc.cluster.local --port 7687) + echo "$instances" + num_leaders=$(echo "$instances" | grep -c "leader") + if [ $num_leaders -ne 1 ]; then + echo "Expected 1 leader after registration, got $num_leaders." + exit 1 + fi + + num_followers=$(echo "$instances" | grep -c "follower") + if [ $num_followers -ne 2 ]; then + echo "Expected 2 followers after registration, got $num_followers." + exit 1 + fi + + num_mains=$(echo "$instances" | grep -c "main") + if [ $num_mains -ne 1 ]; then + echo "Expected 1 main after registration, got $num_mains." + exit 1 + fi + + num_replicas=$(echo "$instances" | grep -c "replica") + if [ $num_replicas -ne 1 ]; then + echo "Expected 1 replica after registration, got $num_replicas." + exit 1 + fi + + num_instances_down=$(echo "instances" | grep -c "down") + if [ $num_instances_down -ne 0 ]; then + echo "Expected 0 instances down after registration, got $num_instances_down." + exit 1 + fi + + exit 0 - # Use awk to check if all instances have health status "up" and there are exactly 5 instances - echo "$result" | awk ' - BEGIN { - FS = "|" - instance_count = 0 - health_ok = 1 - } - NR > 3 && NR <= 8 { - gsub(/^ *| *$/, "", $6) # Trim spaces from health - health = $6 - print "Health: [" health "]" # Debug print - instance_count++ - if (health != "up") { - health_ok = 0 - print "Non-up health found: [" health "]" # Debug print - } - } - END { - print "Instance count: " instance_count # Debug print - print "Health OK: " health_ok # Debug print - if (instance_count == 5 && health_ok == 1) { - print "All instances are up and there are exactly 5 instances." - } else { - print "Check failed. Either not all instances are up or the count is not 5." - } - } - ' restartPolicy: Never backoffLimit: 3 diff --git a/charts/memgraph-high-availability/templates/tests/test-connection.yaml b/charts/memgraph-high-availability/templates/tests/test-connection.yaml index ef61c87..9287672 100644 --- a/charts/memgraph-high-availability/templates/tests/test-connection.yaml +++ b/charts/memgraph-high-availability/templates/tests/test-connection.yaml @@ -1,3 +1,4 @@ +# Inspect logs of this test with kubectl logs -l app=memgraph-ha-test-connection apiVersion: batch/v1 kind: Job metadata: @@ -8,22 +9,31 @@ metadata: "helm.sh/hook": test spec: template: + metadata: + labels: + app: memgraph-ha-test-connection spec: containers: - name: memgraph-test - image: memgraph/memgraph:2.17.0 - command: ["/bin/sh", "-c"] + image: "{{ $.Values.memgraph.image.repository }}:{{ $.Values.memgraph.image.tag }}" + command: ["/bin/bash", "-c"] args: - | - echo "Running connection test on data instances..." - echo "RETURN 0;" | mgconsole --host memgraph-data-0.default.svc.cluster.local --port 7687 - echo "RETURN 0;" | mgconsole --host memgraph-data-1.default.svc.cluster.local --port 7687 - - echo "Running a connection test on coordinator instances..." - echo "SHOW INSTANCES;" | mgconsole --host memgraph-coordinator-1.default.svc.cluster.local --port 7687 - echo "SHOW INSTANCES;" | mgconsole --host memgraph-coordinator-2.default.svc.cluster.local --port 7687 - echo "SHOW INSTANCES;" | mgconsole --host memgraph-coordinator-3.default.svc.cluster.local --port 7687 - + commands=( + "echo 'SHOW INSTANCES;' | mgconsole --host memgraph-coordinator-1.default.svc.cluster.local --port 7687" + "echo 'SHOW INSTANCES;' | mgconsole --host memgraph-coordinator-2.default.svc.cluster.local --port 7687" + "echo 'SHOW INSTANCES;' | mgconsole --host memgraph-coordinator-3.default.svc.cluster.local --port 7687" + "echo 'RETURN 0;' | mgconsole --host memgraph-data-0.default.svc.cluster.local --port 7687" + "echo 'RETURN 0;' | mgconsole --host memgraph-data-1.default.svc.cluster.local --port 7687" + ) + for command in "${commands[@]}"; do + echo "Running command: $command" + eval $command + if [ $? -ne 0 ]; then + echo "Command failed: $command" + exit 1 + fi + done + exit 0 restartPolicy: Never - backoffLimit: 3 diff --git a/charts/memgraph-high-availability/values.yaml b/charts/memgraph-high-availability/values.yaml index fcbb06c..fafa1ab 100644 --- a/charts/memgraph-high-availability/values.yaml +++ b/charts/memgraph-high-availability/values.yaml @@ -38,7 +38,6 @@ memgraph: logPVCSize: "256Mi" - data: - id: "0" boltPort: 7687