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

Added initial bats tests and github action set up #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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}' $*"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be OK, but @rpelisse could you confirm this?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spyrkob @gaol I'm not sure about this change, I've never seen $* before... What is the reason for this change? What does $* that differs from ${@} ? In any case, I think it should be ${*}.

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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaol There seems to be quite a lot of code duplication from Harmonia's testsuite... I wonder if it would no be cleaner to have Jenkins checkout Harmonia inside the Hera folder and reuse the stuff... WDTK?