* feat: move to new run.sh container friendly file (#1244)

* fix: unit tests were very broken

Co-authored-by: toast-gear <toast-gear@users.noreply.github.com>
This commit is contained in:
Callum Tait 2022-03-22 19:02:51 +00:00 committed by GitHub
parent 366f8927d8
commit 2cb04ddde7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 221 additions and 530 deletions

View File

@ -18,5 +18,4 @@ jobs:
uses: actions/checkout@v3 uses: actions/checkout@v3
- name: Run unit tests for entrypoint.sh - name: Run unit tests for entrypoint.sh
run: | run: |
cd test/entrypoint make acceptance/runner/entrypoint
bash entrypoint_unittest.sh

View File

@ -197,6 +197,9 @@ acceptance/deploy:
acceptance/tests: acceptance/tests:
acceptance/checks.sh acceptance/checks.sh
acceptance/runner/entrypoint:
cd test/entrypoint/ && bash test.sh
# We use -count=1 instead of `go clean -testcache` # We use -count=1 instead of `go clean -testcache`
# See https://terratest.gruntwork.io/docs/testing-best-practices/avoid-test-caching/ # See https://terratest.gruntwork.io/docs/testing-best-practices/avoid-test-caching/
.PHONY: e2e .PHONY: e2e

View File

@ -111,7 +111,6 @@ RUN mkdir /opt/hostedtoolcache \
&& chmod g+rwx /opt/hostedtoolcache && chmod g+rwx /opt/hostedtoolcache
COPY entrypoint.sh / COPY entrypoint.sh /
COPY --chown=runner:docker patched $RUNNER_ASSETS_DIR/patched
# Add the Python "User Script Directory" to the PATH # Add the Python "User Script Directory" to the PATH
ENV PATH="${PATH}:${HOME}/.local/bin" ENV PATH="${PATH}:${HOME}/.local/bin"

View File

@ -114,8 +114,6 @@ RUN export ARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) \
VOLUME /var/lib/docker VOLUME /var/lib/docker
COPY --chown=runner:docker patched $RUNNER_ASSETS_DIR/patched
# Add the Python "User Script Directory" to the PATH # Add the Python "User Script Directory" to the PATH
ENV PATH="${PATH}:${HOME}/.local/bin" ENV PATH="${PATH}:${HOME}/.local/bin"
ENV ImageOS=ubuntu20 ENV ImageOS=ubuntu20

View File

@ -92,7 +92,7 @@ cd ${RUNNER_HOME}
# past that point, it's all relative pathes from /runner # past that point, it's all relative pathes from /runner
config_args=() config_args=()
if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" == "true" -a "${RUNNER_EPHEMERAL}" != "false" ]; then if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" == "true" -a "${RUNNER_EPHEMERAL}" == "true" ]; then
config_args+=(--ephemeral) config_args+=(--ephemeral)
echo "Passing --ephemeral to config.sh to enable the ephemeral runner." echo "Passing --ephemeral to config.sh to enable the ephemeral runner."
fi fi
@ -154,22 +154,17 @@ if [ -z "${UNITTEST:-}" ]; then
mkdir ./externals mkdir ./externals
# Hack due to the DinD volumes # Hack due to the DinD volumes
mv ./externalstmp/* ./externals/ mv ./externalstmp/* ./externals/
for f in runsvc.sh RunnerService.js; do
diff {bin,patched}/${f} || :
sudo mv bin/${f}{,.bak}
sudo mv {patched,bin}/${f}
done
fi fi
args=() args=()
if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" != "true" -a "${RUNNER_EPHEMERAL}" != "false" ]; then if [ "${RUNNER_FEATURE_FLAG_EPHEMERAL:-}" != "true" -a "${RUNNER_EPHEMERAL}" == "true" ]; then
args+=(--once) args+=(--once)
echo "[WARNING] Passing --once is deprecated and will be removed as an option from the image and ARC at the release of 0.24.0." echo "[WARNING] Passing --once is deprecated and will be removed as an option from the image and ARC at the release of 0.24.0."
echo "[WARNING] Upgrade to GHES => 3.3 to continue using actions-runner-controller. If you are using github.com ignore this warning." echo "[WARNING] Upgrade to GHES => 3.3 to continue using actions-runner-controller. If you are using github.com ignore this warning."
fi fi
unset RUNNER_NAME RUNNER_REPO RUNNER_TOKEN # Unset entrypoint environment variables so they don't leak into the runner environment
unset RUNNER_NAME RUNNER_REPO RUNNER_TOKEN STARTUP_DELAY_IN_SECONDS DISABLE_WAIT_FOR_DOCKER
# Docker ignores PAM and thus never loads the system environment variables that # Docker ignores PAM and thus never loads the system environment variables that
# are meant to be set in every environment of every user. We emulate the PAM # are meant to be set in every environment of every user. We emulate the PAM
@ -177,5 +172,10 @@ unset RUNNER_NAME RUNNER_REPO RUNNER_TOKEN
# #
# https://github.com/actions-runner-controller/actions-runner-controller/issues/1135 # https://github.com/actions-runner-controller/actions-runner-controller/issues/1135
# https://github.com/actions/runner/issues/1703 # https://github.com/actions/runner/issues/1703
mapfile -t env </etc/environment
exec env -- "${env[@]}" ./bin/runsvc.sh "${args[@]}" # /etc/environment may not exist when running unit tests depending on the platform being used
# (e.g. Mac OS) so we just skip the mapping entirely
if [ -z "${UNITTEST:-}" ]; then
mapfile -t env </etc/environment
fi
exec env -- "${env[@]}" ./run.sh "${args[@]}"

View File

@ -1,91 +0,0 @@
#!/usr/bin/env node
// Copyright (c) GitHub. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
var childProcess = require("child_process");
var path = require("path")
var supported = ['linux', 'darwin']
if (supported.indexOf(process.platform) == -1) {
console.log('Unsupported platform: ' + process.platform);
console.log('Supported platforms are: ' + supported.toString());
process.exit(1);
}
var stopping = false;
var listener = null;
var runService = function() {
var listenerExePath = path.join(__dirname, '../bin/Runner.Listener');
var interactive = process.argv[2] === "interactive";
if(!stopping) {
try {
if (interactive) {
console.log('Starting Runner listener interactively');
listener = childProcess.spawn(listenerExePath, ['run'].concat(process.argv.slice(3)), { env: process.env });
} else {
console.log('Starting Runner listener with startup type: service');
listener = childProcess.spawn(listenerExePath, ['run', '--startuptype', 'service'].concat(process.argv.slice(2)), { env: process.env });
}
console.log('Started listener process');
listener.stdout.on('data', (data) => {
process.stdout.write(data.toString('utf8'));
});
listener.stderr.on('data', (data) => {
process.stdout.write(data.toString('utf8'));
});
listener.on('close', (code) => {
console.log(`Runner listener exited with error code ${code}`);
if (code === 0) {
console.log('Runner listener exit with 0 return code, stop the service, no retry needed.');
stopping = true;
} else if (code === 1) {
console.log('Runner listener exit with terminated error, stop the service, no retry needed.');
stopping = true;
} else if (code === 2) {
console.log('Runner listener exit with retryable error, re-launch runner in 5 seconds.');
} else if (code === 3) {
console.log('Runner listener exit because of updating, re-launch runner in 5 seconds.');
} else {
console.log('Runner listener exit with undefined return code, re-launch runner in 5 seconds.');
}
if(!stopping) {
setTimeout(runService, 5000);
}
});
} catch(ex) {
console.log(ex);
}
}
}
runService();
console.log('Started running service');
var gracefulShutdown = function(code) {
console.log('Shutting down runner listener');
stopping = true;
if (listener) {
console.log('Sending SIGINT to runner listener to stop');
listener.kill('SIGINT');
// TODO wait for 30 seconds and send a SIGKILL
}
}
process.on('SIGINT', () => {
gracefulShutdown(0);
});
process.on('SIGTERM', () => {
gracefulShutdown(0);
});

View File

@ -1,20 +0,0 @@
#!/bin/bash
# convert SIGTERM signal to SIGINT
# for more info on how to propagate SIGTERM to a child process see: http://veithen.github.io/2014/11/16/sigterm-propagation.html
trap 'kill -INT $PID' TERM INT
if [ -f ".path" ]; then
# configure
export PATH=`cat .path`
echo ".path=${PATH}"
fi
# insert anything to setup env when running as a service
# run the host process which keep the listener alive
./externals/node12/bin/node ./bin/RunnerService.js $* &
PID=$!
wait $PID
trap - TERM INT
wait $PID

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
export LIGHTGREEN='\e[0;32m' export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m' export LIGHTRED='\e[0;31m'
@ -18,11 +18,15 @@ error(){
} }
success "I'm configured normally" success "I'm configured normally"
touch .runner
echo "$*" > runner_config # Condition for should_retry_configuring test
if [ -z "${FAIL_RUNNER_CONFIG_SETUP}" ]; then
touch .runner
fi
echo "$@" > runner_config
success "created a dummy config file" success "created a dummy config file"
success # adding a counter to see how many times we've gone through the configuration step
# Adding a counter to see how many times we've gone through the configuration step
count=`cat counter 2>/dev/null|| echo "0"` count=`cat counter 2>/dev/null|| echo "0"`
count=$((count + 1)) count=$((count + 1))
echo ${count} > counter echo ${count} > counter

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
export LIGHTGREEN='\e[0;32m' export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m' export LIGHTRED='\e[0;31m'

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
@ -20,12 +20,9 @@ error(){
exit 1 exit 1
} }
log "Dumping set runner arguments"
echo "$@" > runner_args
success "Pretending to run service..."
touch run_sh_ran
success "Success"
success "" success ""
success "Running the service..."
# SHOULD NOT HAPPEN
# creating a file to show this script has run
touch runsvc_ran
success "...successful"
success ""

View File

@ -1,29 +0,0 @@
#!/bin/bash
set -euo pipefail
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
exit 1
}
echo "$*" > runner_config
success "I'm pretending the configuration is not successful"
# increasing a counter to measure how many times we restarted
count=`cat counter 2>/dev/null|| echo "0"`
count=$((count + 1))
echo ${count} > counter

View File

@ -1,12 +1,12 @@
#!/bin/bash #!/usr/bin/env bash
# UNITTEST: retry config # UNITTEST: retry config
# Will simulate a configuration failure and expects: # Will simulate a configuration failure and expects:
# - the configuration step to be run 10 times # - the configuration step to be run 10 times
# - the entrypoint script to exit with error code 2 # - the entrypoint script to exit with error code 2
# - the runsvc.sh script to never run. # - the run.sh script to never run.
source ../logging.sh source ../assets/logging.sh
entrypoint_log() { entrypoint_log() {
while read I; do while read I; do
@ -14,17 +14,22 @@ entrypoint_log() {
done done
} }
log "Setting up the test" log "Setting up test area"
export RUNNER_HOME=testarea
mkdir -p ${RUNNER_HOME}
log "Setting up the test config"
export UNITTEST=true export UNITTEST=true
export RUNNER_HOME=localhome export FAIL_RUNNER_CONFIG_SETUP=true
export RUNNER_NAME="example_runner_name" export RUNNER_NAME="example_runner_name"
export RUNNER_REPO="myorg/myrepo" export RUNNER_REPO="myorg/myrepo"
export RUNNER_TOKEN="xxxxxxxxxxxxx" export RUNNER_TOKEN="xxxxxxxxxxxxx"
mkdir -p ${RUNNER_HOME}/bin # run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# add up the config.sh and runsvc.sh # We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
ln -s ../config.sh ${RUNNER_HOME}/config.sh log "Symlink dummy config.sh and run.sh"
ln -s ../../runsvc.sh ${RUNNER_HOME}/bin/runsvc.sh ln -s ../../assets/config.sh ${RUNNER_HOME}/config.sh
ln -s ../../assets/run.sh ${RUNNER_HOME}/run.sh
cleanup() { cleanup() {
rm -rf ${RUNNER_HOME} rm -rf ${RUNNER_HOME}
@ -33,41 +38,44 @@ cleanup() {
unset RUNNER_NAME unset RUNNER_NAME
unset RUNNER_REPO unset RUNNER_REPO
unset RUNNER_TOKEN unset RUNNER_TOKEN
unset FAIL_RUNNER_CONFIG_SETUP
} }
# Always run cleanup when test ends regardless of how it ends
trap cleanup SIGINT SIGTERM SIGQUIT EXIT trap cleanup SIGINT SIGTERM SIGQUIT EXIT
log "Running the entrypoint" log "Running the entrypoint"
log "" log ""
# Run the runner entrypoint script which as a final step runs this
# unit tests run.sh as it was symlinked
../../../runner/entrypoint.sh 2> >(entrypoint_log) ../../../runner/entrypoint.sh 2> >(entrypoint_log)
if [ "$?" != "2" ]; then if [ "$?" != "2" ]; then
error "=========================================" error "========================================="
error "Configuration should have thrown an error" error "FAIL | Configuration should have thrown an error"
exit 1 exit 1
fi fi
success "Entrypoint didn't complete successfully"
success "" success "PASS | Entrypoint didn't complete successfully"
log "Checking the counter, should have 10 iterations" log "Checking the counter, should have 10 iterations"
count=`cat ${RUNNER_HOME}/counter || "notfound"` count=`cat ${RUNNER_HOME}/counter || "notfound"`
if [ "${count}" != "10" ]; then if [ "${count}" != "10" ]; then
error "=============================================" error "============================================="
error "The retry loop should have done 10 iterations" error "FAIL | The retry loop should have done 10 iterations"
exit 1 exit 1
fi fi
success "Retry loop went up to 10" success "PASS | Retry loop went up to 10"
success
log "Checking that runsvc never ran" log "Checking that run.sh never ran"
if [ -f ${RUNNER_HOME}/runsvc_ran ]; then if [ -f ${RUNNER_HOME}/run_sh_ran ]; then
error "=================================================================" error "================================================================="
error "runsvc was invoked, entrypoint.sh should have failed before that." error "FAIL | run.sh was invoked, entrypoint.sh should have failed before that."
exit 1 exit 1
fi fi
success "runsvc.sh never ran" success "PASS | run.sh never ran"
success success
success "===========================" success "==========================="
success "Test completed successfully" success "Test completed successfully"

View File

@ -1,29 +0,0 @@
#!/bin/bash
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
}
success "I'm configured normally"
touch .runner
echo "$*" > runner_config
success "created a dummy config file"
success
# adding a counter to see how many times we've gone through a configuration step
count=`cat counter 2>/dev/null|| echo "0"`
count=$((count + 1))
echo ${count} > counter

View File

@ -1,31 +0,0 @@
#!/bin/bash
set -euo pipefail
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
exit 1
}
success ""
success "Running the service..."
# test if --once is present as a parameter
echo "$*" | grep -q 'once' && error "Should not include --once in the parameters"
success "...successful"
touch runsvc_ran
success ""

View File

@ -1,81 +0,0 @@
#!/bin/bash
# UNITTEST: should work as non ephemeral
# Will simulate a scenario where ephemeral=false. expects:
# - the configuration step to be run exactly once
# - the entrypoint script to exit with no error
# - the runsvc.sh script to run without the --once flag
source ../logging.sh
entrypoint_log() {
while read I; do
printf "\tentrypoint.sh: $I\n"
done
}
log "Setting up the test"
export UNITTEST=true
export RUNNER_HOME=localhome
export RUNNER_NAME="example_runner_name"
export RUNNER_REPO="myorg/myrepo"
export RUNNER_TOKEN="xxxxxxxxxxxxx"
export RUNNER_EPHEMERAL=true
export RUNNER_FEATURE_FLAG_EPHEMERAL=true
mkdir -p ${RUNNER_HOME}/bin
# add up the config.sh and runsvc.sh
ln -s ../config.sh ${RUNNER_HOME}/config.sh
ln -s ../../runsvc.sh ${RUNNER_HOME}/bin/runsvc.sh
cleanup() {
rm -rf ${RUNNER_HOME}
unset UNITTEST
unset RUNNERHOME
unset RUNNER_NAME
unset RUNNER_REPO
unset RUNNER_TOKEN
unset RUNNER_EPHEMERAL
unset RUNNER_FEATURE_FLAG_EPHEMERAL
}
trap cleanup SIGINT SIGTERM SIGQUIT EXIT
log "Running the entrypoint"
log ""
../../../runner/entrypoint.sh 2> >(entrypoint_log)
if [ "$?" != "0" ]; then
error "==========================================="
error "Entrypoint script did not exit successfully"
exit 1
fi
log "Testing if we went through the configuration step only once"
count=`cat ${RUNNER_HOME}/counter || echo "not_found"`
if [ ${count} != "1" ]; then
error "==============================================="
error "The configuration step was not run exactly once"
exit 1
fi
log "Testing if the configuration included the --ephemeral flag"
if ! grep -q -- '--ephemeral' ${RUNNER_HOME}/runner_config; then
error "==============================================="
error "The configuration did not include the --ephemeral flag"
exit 1
fi
success "The configuration ran ${count} time(s)"
log "Testing if runsvc ran"
if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then
error "=============================="
error "The runner service has not run"
exit 1
fi
success "The service ran"
success ""
success "==========================="
success "Test completed successfully"

View File

@ -1,31 +0,0 @@
#!/bin/bash
set -euo pipefail
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
exit 1
}
success ""
success "Running the service..."
# test if --once is present as a parameter
echo "$*" | grep -q 'once' || error "Should include --once in the parameters"j
success "...successful"
touch runsvc_ran
success ""

View File

@ -1,29 +0,0 @@
#!/bin/bash
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
}
success "I'm configured normally"
touch .runner
echo "$*" > runner_config
success "created a dummy config file"
success
# adding a counter to see how many times we've gone through a configuration step
count=`cat counter 2>/dev/null|| echo "0"`
count=$((count + 1))
echo ${count} > counter

View File

@ -1,31 +0,0 @@
#!/bin/bash
set -euo pipefail
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
exit 1
}
success ""
success "Running the service..."
# test if --once is present as a parameter
echo "$*" | grep -q 'once' && error "Should not include --once in the parameters"
success "...successful"
touch runsvc_ran
success ""

View File

@ -1,12 +1,12 @@
#!/bin/bash #!/usr/bin/env bash
# UNITTEST: should work as non ephemeral # UNITTEST: should work as non ephemeral
# Will simulate a scenario where ephemeral=false. expects: # Will simulate a scenario where ephemeral=false. expects:
# - the configuration step to be run exactly once # - the configuration step to be run exactly once
# - the entrypoint script to exit with no error # - the entrypoint script to exit with no error
# - the runsvc.sh script to run without the --once flag # - the run.sh script to run without the --once flag
source ../logging.sh source ../assets/logging.sh
entrypoint_log() { entrypoint_log() {
while read I; do while read I; do
@ -14,18 +14,22 @@ entrypoint_log() {
done done
} }
log "Setting up test area"
export RUNNER_HOME=testarea
mkdir -p ${RUNNER_HOME}
log "Setting up the test" log "Setting up the test"
export UNITTEST=true export UNITTEST=true
export RUNNER_HOME=localhome
export RUNNER_NAME="example_runner_name" export RUNNER_NAME="example_runner_name"
export RUNNER_REPO="myorg/myrepo" export RUNNER_REPO="myorg/myrepo"
export RUNNER_TOKEN="xxxxxxxxxxxxx" export RUNNER_TOKEN="xxxxxxxxxxxxx"
export RUNNER_EPHEMERAL=false export RUNNER_EPHEMERAL=false
mkdir -p ${RUNNER_HOME}/bin # run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# add up the config.sh and runsvc.sh # We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
ln -s ../config.sh ${RUNNER_HOME}/config.sh log "Symlink dummy config.sh and run.sh"
ln -s ../../runsvc.sh ${RUNNER_HOME}/bin/runsvc.sh ln -s ../../assets/config.sh ${RUNNER_HOME}/config.sh
ln -s ../../assets/run.sh ${RUNNER_HOME}/run.sh
cleanup() { cleanup() {
rm -rf ${RUNNER_HOME} rm -rf ${RUNNER_HOME}
@ -37,16 +41,19 @@ cleanup() {
unset RUNNER_EPHEMERAL unset RUNNER_EPHEMERAL
} }
# Always run cleanup when test ends regardless of how it ends
trap cleanup SIGINT SIGTERM SIGQUIT EXIT trap cleanup SIGINT SIGTERM SIGQUIT EXIT
log "Running the entrypoint" log "Running the entrypoint"
log "" log ""
# Run the runner entrypoint script which as a final step runs this
# unit tests run.sh as it was symlinked
../../../runner/entrypoint.sh 2> >(entrypoint_log) ../../../runner/entrypoint.sh 2> >(entrypoint_log)
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
error "===========================================" error "==========================================="
error "Entrypoint script did not exit successfully" error "FAIL | Entrypoint script did not exit successfully"
exit 1 exit 1
fi fi
@ -54,19 +61,19 @@ log "Testing if we went through the configuration step only once"
count=`cat ${RUNNER_HOME}/counter || echo "not_found"` count=`cat ${RUNNER_HOME}/counter || echo "not_found"`
if [ ${count} != "1" ]; then if [ ${count} != "1" ]; then
error "===============================================" error "==============================================="
error "The configuration step was not run exactly once" error "FAIL | The configuration step was not run exactly once"
exit 1 exit 1
fi fi
success "The configuration ran ${count} time(s)" success "PASS | The configuration ran ${count} time(s)"
log "Testing if runsvc ran" log "Testing if run.sh ran"
if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then
error "==============================" error "=============================="
error "The runner service has not run" error "FAIL | The runner service has not run"
exit 1 exit 1
fi fi
success "The service ran" success "PASS | run.sh ran"
success "" success ""
success "===========================" success "==========================="
success "Test completed successfully" success "Test completed successfully"

View File

@ -1,29 +0,0 @@
#!/bin/bash
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
}
success "I'm configured normally"
touch .runner
echo "$*" > runner_config
success "created a dummy config file"
success
# Adding a counter to see how many times we've gone through the configuration step
count=`cat counter 2>/dev/null|| echo "0"`
count=$((count + 1))
echo ${count} > counter

View File

@ -1,31 +0,0 @@
#!/bin/bash
set -euo pipefail
export LIGHTGREEN='\e[0;32m'
export LIGHTRED='\e[0;31m'
export WHITE='\e[0;97m'
export RESET='\e[0m'
log(){
printf "\t${WHITE}$@${RESET}\n" 2>&1
}
success(){
printf "\t${LIGHTGREEN}$@${RESET}\n" 2>&1
}
error(){
printf "\t${LIGHTRED}$@${RESET}\n" 2>&1
exit 1
}
success ""
success "Running the service..."
# test if --once is present as a parameter
echo "$*" | grep -q 'once' || error "Should include --once in the parameters"j
success "...successful"
touch runsvc_ran
success ""

View File

@ -1,12 +1,12 @@
#!/bin/bash #!/usr/bin/env bash
# UNITTEST: should work normally # UNITTEST: should work normally
# Will simulate a normal execution scenario. expects: # Will simulate a normal execution scenario. expects:
# - the configuration step to be run exactly once # - the configuration step to be run exactly once
# - the entrypoint script to exit with no error # - the entrypoint script to exit with no error
# - the runsvc.sh script to run with the --once flag activated. # - the run.sh script to run with the --once flag activated.
source ../logging.sh source ../assets/logging.sh
entrypoint_log() { entrypoint_log() {
while read I; do while read I; do
@ -14,17 +14,21 @@ entrypoint_log() {
done done
} }
log "Setting up test area"
export RUNNER_HOME=testarea
mkdir -p ${RUNNER_HOME}
log "Setting up the test" log "Setting up the test"
export UNITTEST=true export UNITTEST=true
export RUNNER_HOME=localhome
export RUNNER_NAME="example_runner_name" export RUNNER_NAME="example_runner_name"
export RUNNER_REPO="myorg/myrepo" export RUNNER_REPO="myorg/myrepo"
export RUNNER_TOKEN="xxxxxxxxxxxxx" export RUNNER_TOKEN="xxxxxxxxxxxxx"
mkdir -p ${RUNNER_HOME}/bin # run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# add up the config.sh and runsvc.sh # We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
ln -s ../config.sh ${RUNNER_HOME}/config.sh log "Symlink dummy config.sh and run.sh"
ln -s ../../runsvc.sh ${RUNNER_HOME}/bin/runsvc.sh ln -s ../../assets/config.sh ${RUNNER_HOME}/config.sh
ln -s ../../assets/run.sh ${RUNNER_HOME}/run.sh
cleanup() { cleanup() {
rm -rf ${RUNNER_HOME} rm -rf ${RUNNER_HOME}
@ -35,11 +39,14 @@ cleanup() {
unset RUNNER_TOKEN unset RUNNER_TOKEN
} }
# Always run cleanup when test ends regardless of how it ends
trap cleanup SIGINT SIGTERM SIGQUIT EXIT trap cleanup SIGINT SIGTERM SIGQUIT EXIT
log "Running the entrypoint" log "Running the entrypoint"
log "" log ""
# Run the runner entrypoint script which as a final step runs this
# unit tests run.sh as it was symlinked
../../../runner/entrypoint.sh 2> >(entrypoint_log) ../../../runner/entrypoint.sh 2> >(entrypoint_log)
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
@ -52,26 +59,29 @@ log "Testing if the configuration step was run only once"
count=`cat ${RUNNER_HOME}/counter || echo "not_found"` count=`cat ${RUNNER_HOME}/counter || echo "not_found"`
if [ ${count} != "1" ]; then if [ ${count} != "1" ]; then
error "===============================================" error "==============================================="
error "The configuration step was not run exactly once" error "FAIL | The configuration step was not run exactly once"
exit 1 exit 1
fi fi
success "The configuration ran ${count} time(s)"
success "PASS | The configuration ran ${count} time(s)"
log "Testing if the configuration included the --ephemeral flag" log "Testing if the configuration included the --ephemeral flag"
if grep -q -- '--ephemeral' ${RUNNER_HOME}/runner_config; then if grep -q -- '--ephemeral' ${RUNNER_HOME}/runner_config; then
error "===============================================" error "==============================================="
error "The configuration should not include the --ephemeral flag" error "FAIL | The configuration should not include the --ephemeral flag"
exit 1 exit 1
fi fi
log "Testing if runsvc ran" success "PASS | The --ephemeral switch was included in the configuration"
if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then
log "Testing if run.sh ran"
if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then
error "==============================" error "=============================="
error "The runner service has not run" error "FAIL | The runner service has not run"
exit 1 exit 1
fi fi
success "The service ran" success "PASS | run.sh ran"
success "" success ""
success "===========================" success "==========================="
success "Test completed successfully" success "Test completed successfully"

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
# UNITTEST: should work disable update # UNITTEST: should work disable update
# Will simulate a scneario where disableupdate=true. expects: # Will simulate a scneario where disableupdate=true. expects:
@ -6,7 +6,7 @@
# - the entrypoint script to exit with no error # - the entrypoint script to exit with no error
# - the config.sh script to run with the --disableupdate flag set to 'true'. # - the config.sh script to run with the --disableupdate flag set to 'true'.
source ../logging.sh source ../assets/logging.sh
entrypoint_log() { entrypoint_log() {
while read I; do while read I; do
@ -14,18 +14,22 @@ entrypoint_log() {
done done
} }
log "Setting up test area"
export RUNNER_HOME=testarea
mkdir -p ${RUNNER_HOME}
log "Setting up the test" log "Setting up the test"
export UNITTEST=true export UNITTEST=true
export RUNNER_HOME=localhome
export RUNNER_NAME="example_runner_name" export RUNNER_NAME="example_runner_name"
export RUNNER_REPO="myorg/myrepo" export RUNNER_REPO="myorg/myrepo"
export RUNNER_TOKEN="xxxxxxxxxxxxx" export RUNNER_TOKEN="xxxxxxxxxxxxx"
export DISABLE_RUNNER_UPDATE="true" export DISABLE_RUNNER_UPDATE="true"
mkdir -p ${RUNNER_HOME}/bin # run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# add up the config.sh and runsvc.sh # We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
ln -s ../config.sh ${RUNNER_HOME}/config.sh log "Symlink dummy config.sh and run.sh"
ln -s ../../runsvc.sh ${RUNNER_HOME}/bin/runsvc.sh ln -s ../../assets/config.sh ${RUNNER_HOME}/config.sh
ln -s ../../assets/run.sh ${RUNNER_HOME}/run.sh
cleanup() { cleanup() {
rm -rf ${RUNNER_HOME} rm -rf ${RUNNER_HOME}
@ -36,16 +40,19 @@ cleanup() {
unset RUNNER_TOKEN unset RUNNER_TOKEN
} }
# Always run cleanup when test ends regardless of how it ends
trap cleanup SIGINT SIGTERM SIGQUIT EXIT trap cleanup SIGINT SIGTERM SIGQUIT EXIT
log "Running the entrypoint" log "Running the entrypoint"
log "" log ""
# run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
../../../runner/entrypoint.sh 2> >(entrypoint_log) ../../../runner/entrypoint.sh 2> >(entrypoint_log)
if [ "$?" != "0" ]; then if [ "$?" != "0" ]; then
error "==========================" error "=========================="
error "Test completed with errors" error "FAIL | Test completed with errors"
exit 1 exit 1
fi fi
@ -53,26 +60,28 @@ log "Testing if the configuration step was run only once"
count=`cat ${RUNNER_HOME}/counter || echo "not_found"` count=`cat ${RUNNER_HOME}/counter || echo "not_found"`
if [ ${count} != "1" ]; then if [ ${count} != "1" ]; then
error "===============================================" error "==============================================="
error "The configuration step was not run exactly once" error "FAIL | The configuration step was not run exactly once"
exit 1 exit 1
fi fi
success "The configuration ran ${count} time(s)" success "PASS | The configuration ran ${count} time(s)"
log "Testing if the configuration included the --disableupdate flag" log "Testing if the configuration included the --disableupdate flag"
if ! grep -q -- '--disableupdate' ${RUNNER_HOME}/runner_config; then if ! grep -q -- '--disableupdate' ${RUNNER_HOME}/runner_config; then
error "===============================================" error "==============================================="
error "The configuration should not include the --disableupdate flag" error "FAIL | The configuration should not include the --disableupdate flag"
exit 1 exit 1
fi fi
log "Testing if runsvc ran" success "PASS | The --disableupdate switch was included in the configuration"
if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then
log "Testing if run.sh ran"
if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then
error "==============================" error "=============================="
error "The runner service has not run" error "FAIL | The runner service has not run"
exit 1 exit 1
fi fi
success "The service ran" success "PASS | run.sh ran"
success "" success ""
success "===========================" success "==========================="
success "Test completed successfully" success "Test completed successfully"

View File

@ -0,0 +1,90 @@
#!/usr/bin/env bash
# UNITTEST: should work legacy once switch set
# Will simulate a scenario where RUNNER_FEATURE_FLAG_EPHEMERAL=false. expects:
# - the configuration step to be run exactly once
# - the entrypoint script to exit with no error
# - the run.sh script to run with the --once flag
source ../assets/logging.sh
entrypoint_log() {
while read I; do
printf "\tentrypoint.sh: $I\n"
done
}
log "Setting up test area"
export RUNNER_HOME=testarea
mkdir -p ${RUNNER_HOME}
log "Setting up the test"
export UNITTEST=true
export RUNNER_NAME="example_runner_name"
export RUNNER_REPO="myorg/myrepo"
export RUNNER_TOKEN="xxxxxxxxxxxxx"
export RUNNER_FEATURE_FLAG_EPHEMERAL="false"
export RUNNER_EPHEMERAL="true"
# run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
log "Symlink dummy config.sh and run.sh"
ln -s ../../assets/config.sh ${RUNNER_HOME}/config.sh
ln -s ../../assets/run.sh ${RUNNER_HOME}/run.sh
cleanup() {
rm -rf ${RUNNER_HOME}
unset UNITTEST
unset RUNNERHOME
unset RUNNER_NAME
unset RUNNER_REPO
unset RUNNER_TOKEN
unset RUNNER_EPHEMERAL
unset RUNNER_FEATURE_FLAG_EPHEMERAL
}
# Always run cleanup when test ends regardless of how it ends
trap cleanup SIGINT SIGTERM SIGQUIT EXIT
log "Running the entrypoint"
log ""
# run.sh and config.sh get used by the runner's real entrypoint.sh and are part of actions/runner.
# We change symlink dummy versions so the entrypoint.sh can run allowing us to test the real entrypoint.sh
../../../runner/entrypoint.sh 2> >(entrypoint_log)
if [ "$?" != "0" ]; then
error "==========================================="
error "FAIL | Entrypoint script did not exit successfully"
exit 1
fi
log "Testing if we went through the configuration step only once"
count=`cat ${RUNNER_HOME}/counter || echo "not_found"`
if [ ${count} != "1" ]; then
error "==============================================="
error "FAIL | The configuration step was not run exactly once"
exit 1
fi
success "PASS | The configuration ran ${count} time(s)"
log "Testing if the configuration included the --once flag"
if ! grep -q -- '--once' ${RUNNER_HOME}/runner_args; then
error "==============================================="
error "FAIL | The configuration did not include the --once flag, config printed below:"
exit 1
fi
success "PASS | The --once argument was passed in"
log "Testing if run.sh ran"
if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then
error "=============================="
error "FAIL | The runner service has not run"
exit 1
fi
success "PASS | run.sh ran"
success ""
success "==========================="
success "Test completed successfully"

View File

@ -1,13 +1,12 @@
#!/bin/bash #!/usr/bin/env bash
source logging.sh source assets/logging.sh
for unittest in ./should*; do for unittest in ./should*; do
log "**********************************" log "**********************************"
log " UNIT TEST: ${unittest}" log " UNIT TEST: ${unittest}"
log "**********************************" log "**********************************"
log "" log ""
cd ${unittest} cd ${unittest}
./test.sh ./test.sh
ret_code=$? ret_code=$?