1- #! /bin/bash
1+ #! /bin/bash -e
22SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd ) "
33source " $SCRIPT_DIR /../utils.bash"
44SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd ) "
@@ -16,31 +16,165 @@ export PATH="$(go env GOPATH)/bin:$PATH"
1616# * It takes some time to delete all PVs, check with `kubectl get pv`.
1717# * If you want more details or helm dry run just append `--debug` of
1818# `--dry-run`.
19+ BOLT_SERVER=" localhost:10000" # Just tmp value -> each coordinator should have a different value.
20+ # E.g. if kubectl port-foward is used, the configured host values should be passed as `bolt_server`.
21+
22+ setup_coordinator () {
23+ local i=$1
24+ echo " ADD COORDINATOR $i WITH CONFIG {\" bolt_server\" : \" $BOLT_SERVER \" , \" management_server\" : \" memgraph-coordinator-$i .default.svc.cluster.local:10000\" , \" coordinator_server\" : \" memgraph-coordinator-$i .default.svc.cluster.local:12000\" };" | $MEMGRAPH_CONSOLE_BINARY --port 17687
25+ echo " coordinator $i DONE"
26+ }
27+ setup_replica () {
28+ local i=$1
29+ echo " REGISTER INSTANCE instance_$i WITH CONFIG {\" bolt_server\" : \" $BOLT_SERVER \" , \" management_server\" : \" memgraph-data-$i .default.svc.cluster.local:10000\" , \" replication_server\" : \" memgraph-data-$i .default.svc.cluster.local:20000\" };" | $MEMGRAPH_CONSOLE_BINARY --port 17687
30+ echo " replica $i DONE"
31+ }
32+ setup_main () {
33+ local i=$1
34+ echo " SET INSTANCE instance_$i TO MAIN;" | $MEMGRAPH_CONSOLE_BINARY --port 17687
35+ echo " main DONE"
36+ }
37+ setup_cluster () {
38+ with_kubectl_portforward memgraph-coordinator-1-0 17687:7687 ' wait_for_memgraph_coordinator localhost 17687 5' -- \
39+ ' setup_coordinator 1' \
40+ ' setup_coordinator 2' \
41+ ' setup_coordinator 3' \
42+ ' setup_replica 0' \
43+ ' setup_main 0'
44+ }
45+
46+ execute_query_against_main () {
47+ query=" $1 "
48+ with_kubectl_portforward memgraph-coordinator-1-0 17687:7687 ' wait_for_memgraph_coordinator localhost 17687 5' -- \
49+ " MAIN_INSTANCE=\$ (echo \" SHOW INSTANCES;\" | $MEMGRAPH_CONSOLE_BINARY --port 17687 --output-format=csv | python3 $SCRIPT_DIR /../validator.py get_main_parser)" \
50+ " echo \" NOTE: MAIN instance is \$ MAIN_INSTANCE\" " \
51+ " echo \" MG_MAIN=\$ MAIN_INSTANCE\" > $SCRIPT_DIR /mg_main.out" # Couldn't get export to move the info -> used file instead.
52+ source $SCRIPT_DIR /mg_main.out
53+ # NOTE: Waiting for MAIN is required because sometimes all instances are up, but MAIN is not yet fully configured.
54+ with_kubectl_portforward " $MG_MAIN -0" 17687:7687 ' wait_for_memgraph localhost 17687 5' -- \
55+ " wait_for_memgraph_main localhost 17687 10" \
56+ " echo \" $query \" | $MEMGRAPH_CONSOLE_BINARY --port 17687"
57+ }
58+
59+ validate_nodes_against_main () {
60+ expected=$1
61+ with_kubectl_portforward memgraph-coordinator-1-0 17687:7687 ' wait_for_memgraph_coordinator localhost 17687 5' -- \
62+ " MAIN_INSTANCE=\$ (echo \" SHOW INSTANCES;\" | $MEMGRAPH_CONSOLE_BINARY --port 17687 --output-format=csv | python3 $SCRIPT_DIR /../validator.py get_main_parser)" \
63+ " echo \" NOTE: MAIN instance is \$ MAIN_INSTANCE\" " \
64+ " echo \" MG_MAIN=\$ MAIN_INSTANCE\" > $SCRIPT_DIR /mg_main.out" # Couldn't get export to move the info -> used file instead.
65+ source $SCRIPT_DIR /mg_main.out
66+ with_kubectl_portforward " $MG_MAIN -0" 17687:7687 ' wait_for_memgraph localhost 17687 5' -- \
67+ " echo \" MATCH (n) RETURN n;\" | $MEMGRAPH_CONSOLE_BINARY --port 17687 --output-format=csv | python3 $SCRIPT_DIR /../validator.py validate_number_of_results -e $expected "
68+ echo " validate_nodes_against_main PASSED"
69+ }
1970
2071test_k8s_single () {
2172 echo " Test k8s single memgraph instance using image: $MEMGRAPH_NEXT_DOCKERHUB_IMAGE "
2273 kind load docker-image $MEMGRAPH_NEXT_DOCKERHUB_IMAGE -n smoke-release-testing
2374 MEMGRAPH_NEXT_DOCKERHUB_TAG=" ${MEMGRAPH_NEXT_DOCKERHUB_IMAGE##*: } "
2475 helm install memgraph-single-smoke memgraph/memgraph \
25- -f " $SCRIPT_DIR /values-single.yaml" --set " image.tag=$MEMGRAPH_NEXT_DOCKERHUB_TAG "
76+ -f " $SCRIPT_DIR /values-single.yaml" \
77+ --set " image.tag=$MEMGRAPH_NEXT_DOCKERHUB_TAG "
2678 kubectl wait --for=condition=Ready pod/memgraph-single-smoke-0 --timeout=120s
27- kubectl port-forward memgraph-single-smoke-0 17687:7687 &
28- PF_PID=$!
29- wait_for_memgraph localhost 17687
30- echo " CREATE ();" | $MEMGRAPH_CONSOLE_BINARY --port 17687
31- echo " MATCH (n) RETURN n;" | $MEMGRAPH_CONSOLE_BINARY --port 17687
32- kill $PF_PID
33- wait $PF_PID 2> /dev/null || true
79+
80+ with_kubectl_portforward memgraph-single-smoke-0 17687:7687 " wait_for_memgraph localhost 17687 5" -- \
81+ " echo \" CREATE ();\" | $MEMGRAPH_CONSOLE_BINARY --port 17687" \
82+ " echo \" MATCH (n) RETURN n;\" | $MEMGRAPH_CONSOLE_BINARY --port 17687"
83+
3484 helm uninstall memgraph-single-smoke
3585}
3686
37- test_k8s_ha () {
38- helm install myhadb memgraph/memgraph-high-availability \
87+ helm_install_myhadb () {
88+ chart_path=" $1 "
89+ image_tag=" $2 "
90+ helm install myhadb $chart_path \
3991 --set env.MEMGRAPH_ENTERPRISE_LICENSE=$MEMGRAPH_ENTERPRISE_LICENSE ,env.MEMGRAPH_ORGANIZATION_NAME=$MEMGRAPH_ORGANIZATION_NAME \
40- -f $SCRIPT_DIR /values-ha.yaml
41- # TODO(gitbuda): Setup cluster commands + routing + test query.
92+ -f " $SCRIPT_DIR /values-ha.yaml" \
93+ --set " image.tag=$image_tag "
94+ }
95+
96+ test_k8s_help () {
97+ echo " usage: test_k8s_ha LAST|NEXT [-p|--chart-path PATH]"
98+ echo " [-s|--skip-cluster-setup] [-u|--skip-helm-uninstall] [-c|--skip-cleanup]"
99+ echo " [-n|--expected-nodes-no]"
100+ echo " [-h|--help]"
101+ exit 1
102+ }
103+
104+ cleanup_k8s_all () {
105+ # NOTE: An attempt to cleanup any leftovers from kubectl port-forward...
106+ kill -9 $( pgrep kubectl) || true
107+ if helm status myhadb > /dev/null 2>&1 ; then
108+ helm uninstall myhadb
109+ fi
110+ if helm status myhadb > /dev/null 2>&1 ; then
111+ helm uninstall memgraph-single-smoke
112+ fi
113+ kubectl delete pvc --all
114+ }
115+
116+ test_k8s_ha () {
117+ if [ " $# " -lt 1 ]; then
118+ test_k8s_help
119+ fi
120+ WHICH=" $1 "
121+ WHICH_TMP=" MEMGRAPH_${WHICH} _DOCKERHUB_IMAGE"
122+ WHICH_IMAGE=" ${! WHICH_TMP} "
123+ MEMGRAPH_DOCKERHUB_TAG=" ${WHICH_IMAGE##*: } "
124+ shift
125+ CHART_PATH=" memgraph/memgraph-high-availability"
126+ SKIP_CLUSTER_SETUP=false
127+ SKIP_CLEANUP=false
128+ SKIP_HELM_UNINSTALL=false
129+ EXPECTED_NODES_COUNT=1
130+ while [ " $# " -gt 0 ]; do
131+ case $1 in
132+ -p|--chart-path) CHART_PATH=" $2 " ; shift 2 ;;
133+ -s|--skip-cluster-setup) SKIP_CLUSTER_SETUP=true; shift ;;
134+ -u|--skip-helm-uninstall) SKIP_HELM_UNINSTALL=true; shift ;;
135+ -c|--skip-cleanup) SKIP_CLEANUP=true; shift ;;
136+ -n|--expected-nodes-no) EXPECTED_NODES_COUNT=" $2 " ; shift 2 ;;
137+ -h|--help) test_k8s_help; ;;
138+ * ) shift ; break ;;
139+ esac
140+ done
141+ echo " Test k8s HA memgraph cluster using image:"
142+ echo " * image: $WHICH_IMAGE "
143+ echo " * tag: $MEMGRAPH_DOCKERHUB_TAG "
144+ echo " * chart: $CHART_PATH "
145+ echo " * expected nodes number: $EXPECTED_NODES_COUNT "
146+ echo " * skip cluster setup: $SKIP_CLUSTER_SETUP "
147+ echo " * skip helm uninstall: $SKIP_HELM_UNINSTALL "
148+ echo " * skip cleanup: $SKIP_CLEANUP "
149+
150+ kind load docker-image $WHICH_IMAGE -n smoke-release-testing
151+ helm_install_myhadb $CHART_PATH $MEMGRAPH_DOCKERHUB_TAG
152+ sleep 1 # NOTE: Sometimes there is an Error from Server -> pod XYZ not found...
153+ kubectl wait --for=condition=Ready pod -l role=coordinator --timeout=120s
154+ kubectl wait --for=condition=Ready pod -l role=data --timeout=120s
155+
156+ if [ " $SKIP_CLUSTER_SETUP " = false ]; then
157+ setup_cluster
158+ fi
159+ execute_query_against_main " SHOW VERSION;"
160+ execute_query_against_main " CREATE ();"
161+ validate_nodes_against_main $EXPECTED_NODES_COUNT
162+ if [ " $SKIP_HELM_UNINSTALL " = false ]; then
163+ helm uninstall myhadb
164+ fi
165+ if [ " $SKIP_CLEANUP " = false ]; then
166+ kubectl delete pvc --all
167+ fi
42168}
43169
44170if [ " ${BASH_SOURCE[0]} " -ef " $0 " ]; then
45- test_k8s_single
171+ # NOTE: Developing workflow: download+load required images and defined MEMGRAPH_NEXT_DOCKERHUB_IMAGE.
172+ echo " Running $0 directly..."
173+
174+ # test_k8s_single
175+ # test_k8s_ha LAST -c # Skip cleanup because we want to recover from existing PVCs.
176+ # test_k8s_ha NEXT -s # Skip cluster setup because that should be recovered from PVCs.
177+
178+ # How to inject local version of the helm chart because we want to test any local fixes upfront.
179+ # test_k8s_ha NEXT ~/Workspace/code/memgraph/helm-charts/charts/memgraph-high-availability
46180fi
0 commit comments