forked from rpelisse/hera
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial bats tests and github action set up
- Loading branch information
Showing
8 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: "CI" | ||
on: [push, pull_request] | ||
jobs: | ||
build: | ||
name: Tests Hera Scripts | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Setup BATS | ||
uses: mig4/setup-bats@v1 | ||
with: | ||
bats-version: 1.4.1 | ||
- name: Check out code | ||
uses: actions/checkout@v1 | ||
- name: Run ShellCheck and Bats tests | ||
run: ./tests/tests-suite.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.idea/ | ||
*.iml | ||
*.tap | ||
shellcheck.html | ||
*.shellcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source-path=. | ||
|
||
disable=SC1091 | ||
disable=SC2155 | ||
|
||
disable=SC2027 | ||
disable=SC2086 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
|
||
readonly SCRIPT_NAME='run.sh' | ||
source ./tests/tests-common.sh | ||
|
||
setup() { | ||
export WORKSPACE=$(mktemp -d) | ||
# run tests within workspace | ||
cd "${WORKSPACE}" | ||
} | ||
|
||
teardown() { | ||
deleteIfExist "${WORKSPACE}" | ||
} | ||
|
||
@test "No JOB_NAME Defined" { | ||
run "${SCRIPT}" | ||
[ "${status}" -eq 3 ] | ||
[ "${lines[0]}" = 'No JOB_NAME provided.' ] | ||
} | ||
|
||
@test "No BUILD_ID Defined" { | ||
export JOB_NAME="test" | ||
run "${SCRIPT}" | ||
[ "${status}" -eq 4 ] | ||
[ "${lines[0]}" = 'No BUILD_ID provided.' ] | ||
} | ||
|
||
@test "Run in container" { | ||
export JOB_NAME="test" | ||
export BUILD_ID="1" | ||
run "${SCRIPT}" | ||
[ "${status}" -eq 0 ] | ||
echo "$output" | grep 'ssh -o StrictHostKeyChecking=no [email protected] podman run' | ||
echo "$output" | grep ' --userns=keep-id -u 1000:1000' | ||
echo "$output" | grep ' --name automaton-slave-test-1' | ||
echo "$output" | grep ' --add-host=olympus:10.88.0.1' | ||
echo "$output" | grep ' -v /home/jenkins//.ssh/:/var/jenkins_home/.ssh/:ro' | ||
echo "$output" | grep ' -v /home/jenkins//.gitconfig:/var/jenkins_home/.gitconfig:ro' | ||
echo "$output" | grep ' -v /home/jenkins//.netrc:/var/jenkins_home/.netrc:ro' | ||
echo "$output" | grep ' -d localhost/automatons' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
|
||
echo "ssh ${@}" >&2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#!/bin/bash | ||
|
||
debugBatsTest() { | ||
|
||
for i in "${!lines[@]}" | ||
do | ||
echo "${lines[${i}]}" | ||
done | ||
echo "${status}" | ||
} | ||
|
||
deleteIfExist() { | ||
local file=${1} | ||
|
||
if [ -n "${file}" -a -e "${file}" ]; then | ||
rm -rf "${file}" | ||
fi | ||
} | ||
|
||
setupDummyCommandHomeDir() { | ||
|
||
readonly DUMMY_COMMAND_DIR=${DUMMY_COMMAND_DIR:-$(mktemp -d)} | ||
export DUMMY_COMMAND_DIR | ||
trap 'deleteIfExist ${DUMMY_COMMAND_DIR}' EXIT | ||
export PATH="${DUMMY_COMMAND_DIR}":"${PATH}" | ||
|
||
} | ||
|
||
setupDummySSH() { | ||
export DUMMY_SSH=${DUMMY_SSH:-"$(pwd)/tests/"} | ||
export PATH="${DUMMY_SSH}":"${PATH}" | ||
} | ||
|
||
createDummyCommand() { | ||
local command=${1} | ||
|
||
if [ -z "${command}" ]; then | ||
echo "No command provided - abort." | ||
exit 1 | ||
fi | ||
local path_to_command="${DUMMY_COMMAND_DIR}/${command}" | ||
|
||
echo "echo ${command} \${@}" > "${path_to_command}" | ||
chmod +x "${path_to_command}" | ||
trap 'deleteIfExist ${path_to_command}' EXIT | ||
} | ||
|
||
createDummyBackgroundCommand() { | ||
createDummyCommand ${@} | ||
|
||
local path_to_command="${DUMMY_COMMAND_DIR}/${1}" | ||
# simple sleep 20 waits until end of sleep before being killed | ||
echo 'for i in {1...20}; do sleep 1; done' >> "${path_to_command}" | ||
echo 'echo done' >> "${path_to_command}" | ||
|
||
} | ||
|
||
setupDummyCommandHomeDir | ||
setupDummySSH | ||
|
||
if [ -z "${SCRIPT_NAME}" ]; then | ||
echo "No script name provided." | ||
exit 1 | ||
fi | ||
|
||
|
||
readonly SCRIPT_HOME=${SCRIPT_HOME:-$(pwd)} | ||
export HERA_HOME=${SCRIPT_HOME} | ||
readonly SCRIPT="${SCRIPT_HOME}/${SCRIPT_NAME}" | ||
|
||
if [ ! -d "${SCRIPT_HOME}" ]; then | ||
echo "Invalid home for ${SCRIPT_NAME}: ${SCRIPT_HOME}." | ||
exit 2 | ||
fi | ||
|
||
if [ ! -e "${SCRIPT}" ]; then | ||
echo "Invalid path to script: ${SCRIPT}." | ||
exit 3 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/bin/bash | ||
|
||
dir_name="$(dirname "$0")" | ||
work_dir="$(cd "${dir_name}/.." && pwd)" | ||
|
||
runTests() { | ||
for tests in "${work_dir}"/tests/*.bats | ||
do | ||
local tests_file | ||
tests_file=$(basename "${tests}") | ||
if ! bats -t "${tests}" > "${work_dir}/tests/${tests_file%.bats}.tap"; then | ||
echo "${tests_file}" | ||
else | ||
rm "${work_dir}/tests/${tests_file%.bats}.tap" | ||
fi | ||
done | ||
} | ||
|
||
echo -n 'Run Tests...' | ||
test_results=$(runTests) | ||
if [ -n "${test_results}" ]; then | ||
for test_file in ${test_results} | ||
do | ||
echo -e "\nTest failures found in ${work_dir}/tests/${test_file}" | ||
cat "${work_dir}/tests/${test_file%.bats}.tap" | ||
done | ||
exit 1 | ||
fi | ||
echo 'Done - PASSED' | ||
echo '' | ||
|
||
|
||
runShellCheck() { | ||
for script in "${work_dir}"/*.sh | ||
do | ||
if ! shellcheck "${script}" > "${script}.shellcheck"; then | ||
echo "${script}" | ||
else | ||
rm "${script}.shellcheck" | ||
fi | ||
done | ||
} | ||
|
||
echo -n 'Run Shellcheck on scripts...' | ||
shellcheck_result=$(runShellCheck) | ||
if [ -n "${shellcheck_result}" ]; then | ||
for script_file in ${shellcheck_result} | ||
do | ||
echo -e "\nShellCheck violations are found in ${script_file}" | ||
cat "${script_file}.shellcheck" | ||
done | ||
exit 1 | ||
fi | ||
echo 'Done - PASSED' |