Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert chart tests from the custom bash script to be BATS unit tests #256

Merged
merged 16 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/flow-deploy-release-artifact.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ jobs:
push: ${{ github.event.inputs.dry-run-enabled != 'true' }}
tags: ghcr.io/${{ github.repository }}/ubi8-init-java17:${{ needs.prepare-release.outputs.version }}

- name: Build Docker Image (kubectl-bats)
uses: docker/build-push-action@2eb1c1961a95fc15694676618e422e8ba1d63825 # pin@v4
with:
context: docker/kubectl-bats
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64, linux/arm64
push: ${{ github.event.inputs.dry-run-enabled != 'true' }}
tags: ghcr.io/${{ github.repository }}/kubectl-bats:${{ needs.prepare-release.outputs.version }}

create-github-release:
name: Github / Release
runs-on: [self-hosted, Linux, medium, ephemeral]
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ secring.*

### Ignore Helm resources
**/src/main/resources/software/**
dev/.env
.env
*.run
dev/local-node/config.txt
dev/temp/.env
Expand Down
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "dev/bats/bats-core"]
path = dev/bats/bats-core
url = https://github.com/bats-core/bats-core.git
[submodule "dev/bats/test_helper/bats-support"]
path = dev/bats/test_helper/bats-support
url = https://github.com/bats-core/bats-support.git
[submodule "dev/bats/test_helper/bats-assert"]
path = dev/bats/test_helper/bats-assert
url = https://github.com/bats-core/bats-assert.git
2 changes: 1 addition & 1 deletion charts/hedera-network/templates/configmaps/test-cm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: test-cm
data:
{{- $total_nodes := len $.Values.hedera.nodes -}}
{{- range $path, $_ := .Files.Glob "tests/*.sh" }}
{{- range $path, $_ := .Files.Glob "tests/*.*" }}
{{ base $path }}: |
{{- tpl ($.Files.Get $path) ( dict "total_nodes" $total_nodes "Template" $.Template ) | nindent 4 }}
{{- end }}
13 changes: 11 additions & 2 deletions charts/hedera-network/templates/tests/test-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@ spec:
name: test-cm
defaultMode: 0777 # we need the test files to be executable
containers:
{{- $tester := $.Values.tester }}
- name: tester
image: bitnami/kubectl:latest
image: {{ include "fullstack.container.image" (dict "image" $tester.image "Chart" $.Chart "defaults" $tester) }}
imagePullPolicy: {{ include "fullstack.images.pullPolicy" (dict "image" $tester.image "defaults" $tester) }}
{{- with $tester.resources }}
resources:
{{- toYaml . | nindent 8 }}
{{- end }}
volumeMounts:
- mountPath: /tests
name: test-volume
env:
- name: TESTS_DIR
value: "/tests" # should be same as mountPath
command:
- "/bin/bash"
- "-c"
- /tests/basic_deployment_test.sh
- /tests/run.sh
#- "while true;do echo sleeping; sleep 10;done"
restartPolicy: Never
20 changes: 20 additions & 0 deletions charts/hedera-network/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Helm Chart Tests
This directory contains the BATS tests for helm chart.

`run.sh` is the entrypoint to execute the tests locally assuming
you have the network deployed already.

## Development
Use the `test_basic_deployment.bats` file as the template while creating new tests.

## Run
- First create a .env file in this directory from the `env.template` file
- From `dev` folder run `make deploy-network`
- Once network is deployed, you can run `./run.sh` to run the tests.
- When tests are working, then redeploy and run the helm tests: `make destroy-network deploy-network helm-test`

## Notes
- Any new template variables should be added in `helpers.sh` with prefix `TMPL_` (e.g TMPL_TOTAL_NODES)
- Any new required env variable should be added in `env.sh`
- Any new helper function should be added in `helpers.sh`
- If a new script file is added, load it in `load.sh`
39 changes: 0 additions & 39 deletions charts/hedera-network/tests/basic_deployment_test.sh

This file was deleted.

23 changes: 23 additions & 0 deletions charts/hedera-network/tests/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
# This file initializes the core mandatory env variables
# Every script must load (source) this in the beginning
# Warning: avoid making these variables readonly since it can be sourced multiple times

# load .env file if it exists in order to load variables with custom values
ENV_FILE="$(dirname "${BASH_SOURCE[0]}")/.env"
if [[ -f "${ENV_FILE}" ]]; then
export $(cat "${ENV_FILE}" | xargs)
fi


# set global env variables if not set
BATS_HOME="${BATS_HOME:-../../../dev/bats}"
TESTS_DIR="${TESTS_DIR:-.}"

echo ""
echo "Mandatory variables"
echo "=============================================="
echo "ENV_FILE: ${ENV_FILE}"
echo "BATS_HOME: ${BATS_HOME}"
echo "TESTS_DIR: ${TESTS_DIR}"

1 change: 1 addition & 0 deletions charts/hedera-network/tests/env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TOTAL_NODES=3
115 changes: 61 additions & 54 deletions charts/hedera-network/tests/helper.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,74 +1,81 @@
#!/bin/bash
#!/usr/bin/env bash

# setup test constants
readonly EX_OK=0
readonly EX_ERR=1

readonly TOTAL_NODES="{{ .total_nodes }}"
readonly PASS="PASS"
readonly FAIL="FAIL"

# template variables to be rendered during helm chart deployment
readonly TMPL_TOTAL_NODES="{{ .total_nodes }}"

# Setup test variables
if [[ -z "${TOTAL_NODES}" ]]; then
TOTAL_NODES="${TMPL_TOTAL_NODES}"
fi

####
# Imports a bash file using the built-in source command.
#
# @param $1 - the bash file to be imported
####
function import {
if [[ -z "${1}" || ! -f "${1}" ]]; then
return "${EX_OSFILE}"
elif [[ -f "${1}" ]]; then
echo "Import: Dependency Included [ file = '${1}' ]"
# shellcheck disable=SC1090
source "${1}"
return "$?"
fi
}

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
function run_test_cases() {
local test_cases=("${@}")
if [ $# -eq 0 ]; then
test_cases=("${TEST_CASES[@]}")
fi

echo "Test cases"
echo "-------------------------------------------------------------"
echo "Checking total number of network node containers"
echo "-------------------------------------------------------------"
kubectl wait --for=jsonpath='{.status.phase}'=Running pod -l fullstack.hedera.com/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 - ${TOTAL_NODES})"
for test_case in "${test_cases[@]}"; do
echo "- ${test_case}"
done
echo ""

# assert true
if [[ "${node_total}" -ne "${TOTAL_NODES}" ]]; then
return "${EX_ERR}"
fi

return "${EX_OK}"
}

function test_systemctl() {
local resp="$(get_pod_list network-node)"
local nodes=(${resp}) # convert into an array
local status="${EX_OK}"
local test_results=()
for test_case in "${test_cases[@]}"; do
"${test_case}"
local test_status="$?"
if [[ "${test_status}" = "${EX_OK}" ]]; then
test_results+=("${test_case}: ${PASS}")
else
test_results+=("${test_case}: ${FAIL}")
status="${EX_ERR}"
fi
done

echo ""
echo "-------------------------------------------------------------"
echo "Checking systemctl is running in all network node containers"
echo "Nodes: " "${nodes[@]}" ", Total:" "${#nodes[@]}"
echo "Test results"
echo "-------------------------------------------------------------"

local attempts=0
local status="${EX_ERR}"
local MAX_ATTEMPTS=10

for node in "${nodes[@]}"
do
attempts=0
status="${EX_ERR}"

# make few attempts to check systemctl status
while [[ "${attempts}" -lt "${MAX_ATTEMPTS}" && "${status}" -ne "${EX_OK}" ]]; do
attempts=$((attempts + 1))
kubectl exec "${node}" -c root-container -- systemctl status --no-pager
status="${?}"
echo "Checked systemctl status in ${node} (Attempt #${attempts})... >>>>> status: ${status} <<<<<"
if [[ "${status}" -ne "${EX_OK}" ]]; then
echo "Sleeping 5s..."
sleep 5
fi
done

if [[ "${status}" -ne "${EX_OK}" ]]; then
echo "Error status: ${status}" && return "${EX_ERR}" # break at first error
fi
for test_result in "${test_results[@]}"; do
echo "${test_result}"
done

return "${EX_OK}"
return "${status}"
}

# This is to check the bats test execution status
function check_test_status() {
echo "status = ${status}"
echo "output = ${output}"
[[ "${status}" -eq 0 ]]
}
26 changes: 26 additions & 0 deletions charts/hedera-network/tests/load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash
source "$(dirname "${BASH_SOURCE[0]}")/env.sh"

function load_bats_helpers() {
if [[ -z "${BATS_HOME}" ]]; then
echo "ERROR: BATS_HOME is not defined"
exit 1
fi

echo "Loading bats helper..."
load "${BATS_HOME}/test_helper/bats-support/load"
load "${BATS_HOME}/test_helper/bats-assert/load"
}

function load_test_helpers() {
# load test helper scripts
echo "Loading test helper scripts..."
load "${TESTS_DIR}/helper.sh"
}

function _common_setup() {
load_bats_helpers
load_test_helpers
}

_common_setup
20 changes: 20 additions & 0 deletions charts/hedera-network/tests/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash
source "$(dirname "${BASH_SOURCE[0]}")/env.sh"

echo ""
echo "BATS directory: $BATS_HOME"
echo "============================================================="
ls -la "${BATS_HOME}"

echo ""
echo "Tests directory: $TESTS_DIR"
echo "============================================================="
ls -la "${TESTS_DIR}"

echo ""
echo "Running BATS: '${BATS_HOME}/bats-core/bin/bats ${TESTS_DIR}'"
echo "============================================================="
"${BATS_HOME}/bats-core/bin/bats" "${TESTS_DIR}"

# uncomment in order to inspect tmpdir
#"${BATS_HOME}/bats-core/bin/bats" --no-tempdir-cleanup .
Loading