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 28, 2021
1 parent 3d59e2e commit 267e32f
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 1 deletion.
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/
*.iml
*.tap
shellcheck.html
*.shellcheck
7 changes: 7 additions & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source-path=.

disable=SC1091
disable=SC2155

disable=SC2027
disable=SC2086
2 changes: 1 addition & 1 deletion job.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ run_ssh "podman exec \
-e PULL_REQUEST_PROCESSOR_HOME="${PULL_REQUEST_PROCESSOR_HOME}" \
-e VERSION="${VERSION}" \
-e COMPONENT_UPGRADE_LOGGER="${COMPONENT_UPGRADE_LOGGER}" \
-ti ${CONTAINER_NAME} '${BUILD_SCRIPT}' ${@}"
-ti ${CONTAINER_NAME} '${BUILD_SCRIPT}' $*"
42 changes: 42 additions & 0 deletions tests/run.sh-tests.bats
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'
}
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
54 changes: 54 additions & 0 deletions tests/tests-suite.sh
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'

0 comments on commit 267e32f

Please sign in to comment.