Skip to content

Commit

Permalink
Added initial bats tests and github action set up
Browse files Browse the repository at this point in the history
  • Loading branch information
gaol committed Sep 24, 2021
1 parent 3d59e2e commit fb0020b
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/ci.yml
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea/
*.iml
*.tap
shellcheck.html
43 changes: 43 additions & 0 deletions tests/run.sh-tests.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/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}"
unset JBOSS_FOLDER
}

@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'
}
4 changes: 4 additions & 0 deletions tests/ssh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "ssh ${@}" >&2

79 changes: 79 additions & 0 deletions tests/tests-common.sh
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
37 changes: 37 additions & 0 deletions tests/tests-suite.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash
readonly WORKDIR="${WORKDIR}"

if [ -n "${WORKDIR}" ]; then
if [ -d "${WORKDIR}" ]; then
cd "${WORKDIR}" || exit 1
echo "Run testsuite from provided WORKDIR: $(pwd)"
else
echo "Invalid WORKDIR provided: ${WORKDIR}."
fi
fi

runTests() {
for tests in tests/*.bats
do
local tests_file=$(basename "${tests}")
bats -t "${tests}" > "tests/${tests_file%.bats}.tap"
done
}

echo -n 'Run Tests...'
runTests
echo 'Done.'
echo ''
echo -n 'Run Shellcheck on scripts...'
shellcheck_report='shellcheck.html'
echo '<html><title>Shellcheck report</title><body>' > "${shellcheck_report}"
for script_file in *.sh
do
echo "<h1>${script_file}</h1>" >> "${shellcheck_report}"
shellcheck --format=quiet "${script_file}"
if [ "${?}" -ne 0 ] ; then
echo "Violations found" >> "${shellcheck_report}"
fi
done
echo '</body></html>' >> "${shellcheck_report}"
echo 'Done.'

0 comments on commit fb0020b

Please sign in to comment.