Skip to content

Commit

Permalink
feat: implement helm chart test (#152)
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <lenin.mehedy@swirldslabs.com>
Co-authored-by: Nathan Klick <nathan@swirldslabs.com>
  • Loading branch information
leninmehedy and nathanklick committed Jul 11, 2023
1 parent 2a18bb6 commit 9321e4c
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 2 deletions.
19 changes: 18 additions & 1 deletion .github/workflows/zxc-compile-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,26 @@ jobs:
verbosity: 3
wait: 120s

- name: Setup Helm
uses: azure/setup-helm@5119fcb9089d432beecbf79bb2c7915207344b78 # pin@v3.5
if: ${{ inputs.enable-unit-tests && !cancelled() }}
with:
version: "v3.12.1" # helm version

- name: Kubernetes Cluster Info
if: ${{ inputs.enable-unit-tests && !cancelled() }}
run: kubectl config current-context
run: |
kubectl config set-context --current --namespace=default
kubectl config get-contexts
- name: Helm Chart Test
id: helm-chart-test
if: ${{ inputs.enable-unit-tests && !cancelled() && !failure() }}
run: |
kubectl config get-contexts
helm install fst ./charts/hedera-network
helm test fst
kubectl logs network-test
- name: Compile
id: gradle-build
Expand Down
93 changes: 93 additions & 0 deletions charts/hedera-network/configs/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

EX_OK=0
EX_ERR=1

function get_pod_list() {
local pattern=$1
local resp=$(kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\n"}' | grep "${pattern}")
echo "${resp}"
}

function test_node_total() {
# set test expectations
local expected_node_total=3

echo "-------------------------------------------------------------"
echo "Checking total number of network node containers"
echo "-------------------------------------------------------------"
kubectl wait --for=condition=ready pod -l type=network-node --timeout=300s || return "${EX_ERR}"

local resp="$(get_pod_list network-node)"
local nodes=(${resp}) # convert into an array
echo "Nodes: " "${nodes[@]}"
local node_total=${#nodes[@]}

echo "Total network node: ${node_total} (expected: ${expected_node_total})"
echo ""

# assert equal
[ "${node_total}" = "${expected_node_total}" ] || return "${EX_ERR}"
return "${EX_OK}"
}

function test_systemctl() {
# test that systemctl status is ready in node containers
local systemctl_all_nodes="${EX_ERR}" # by default set to error status

echo "-------------------------------------------------------------"
echo "Checking systemctl is running in all network node containers"
echo "-------------------------------------------------------------"

local resp="$(get_pod_list network-node)"
local nodes=(${resp}) # convert into an array
echo "Nodes: " "${nodes[@]}"
for node in "${nodes[@]}"
do
local cmd="kubectl exec ${node} -c root-container -- systemctl status --no-pager"

kubectl exec "${node}" -c root-container -- systemctl status --no-pager
local status="$?"

echo "'${cmd}' => ${status} (expected: 0)"

if [ "${status}" != "${EX_OK}" ]; then
systemctl_all_nodes="${EX_ERR}"
break # break at first error
fi

systemctl_all_nodes="${EX_OK}"
done

# assert 0
[ "${systemctl_all_nodes}" = "${EX_OK}" ] || return "${EX_ERR}"

return "${EX_OK}"
}

function run_tests() {
local test_node_total_status
local test_systemctl_status

test_node_total
local status="$?"
[ "${status}" = "${EX_OK}" ] && test_node_total_status="PASS" || test_node_total_status="FAIL"

test_systemctl
local status="$?"
[ "${status}" = "${EX_OK}" ] && test_systemctl_status="PASS" || test_systemctl_status="FAIL"

echo "-------------------------------------------------------------"
echo "Test results"
echo "-------------------------------------------------------------"
echo "test_node_total: ${test_node_total_status}"
echo "test_systemctl: ${test_systemctl_status}"

[ "${test_node_total_status}" = "PASS" ] && \
[ "${test_systemctl_status}" = "PASS" ] || return "${EX_ERR}"

return "${EX_OK}"
}

run_tests

1 change: 0 additions & 1 deletion charts/hedera-network/templates/configmaps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ metadata:
data:
config.yaml:
{{- .Files.Get "configs/otel-collector-config.yaml" | toYaml | nindent 4 }}
{{ end }}
---
# Configmaps for envoy and haproxy, one for each network node
{{- range $nodeConfig := ($.Values.hedera.nodes) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ spec:
metadata:
labels:
app: network-{{ $node.name }}
type: network-node
spec:
volumes:
- name: hgcapp-storage # change me
Expand Down
42 changes: 42 additions & 0 deletions charts/hedera-network/templates/tester.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: tester
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: tester-role
rules:
- apiGroups: [ "" ]
resources:
- pods
- pods/log
verbs:
- get
- list
- apiGroups: [ "" ]
resources:
- pods/exec
verbs:
- create
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: tester-role-binding
subjects:
- kind: ServiceAccount
name: tester
roleRef:
kind: Role
name: tester-role
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
name: tester-cfg
data:
test.sh: |
{{- .Files.Get "configs/test.sh" | nindent 4 }}
26 changes: 26 additions & 0 deletions charts/hedera-network/templates/tests/test-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apiVersion: v1
kind: Pod
metadata:
name: "network-test"
annotations:
"helm.sh/hook": test
spec:
serviceAccountName: tester
volumes:
- name: test-cfg
configMap:
name: tester-cfg
defaultMode: 0777 # we need the test files to be executable
containers:
- name: tester
image: bitnami/kubectl:latest
volumeMounts:
- mountPath: /scripts/test.sh
name: test-cfg
subPath: test.sh
command:
- "/bin/bash"
- "-c"
- /scripts/test.sh
#- "while true;do echo sleeping; sleep 10;done"
restartPolicy: Never

0 comments on commit 9321e4c

Please sign in to comment.