diff --git a/.github/workflows/test-entrypoint.yaml b/.github/workflows/test-entrypoint.yaml index cb4addd2..bfe0741a 100644 --- a/.github/workflows/test-entrypoint.yaml +++ b/.github/workflows/test-entrypoint.yaml @@ -18,5 +18,4 @@ jobs: uses: actions/checkout@v3 - name: Run unit tests for entrypoint.sh run: | - cd test/entrypoint - bash entrypoint_unittest.sh + make acceptance/runner/entrypoint diff --git a/Makefile b/Makefile index 6ab49fb4..a8f6da8b 100644 --- a/Makefile +++ b/Makefile @@ -197,6 +197,9 @@ acceptance/deploy: acceptance/tests: acceptance/checks.sh +acceptance/runner/entrypoint: + cd test/entrypoint/ && bash test.sh + # We use -count=1 instead of `go clean -testcache` # See https://terratest.gruntwork.io/docs/testing-best-practices/avoid-test-caching/ .PHONY: e2e diff --git a/runner/Dockerfile b/runner/Dockerfile index d6562771..720b41ec 100644 --- a/runner/Dockerfile +++ b/runner/Dockerfile @@ -111,7 +111,6 @@ RUN mkdir /opt/hostedtoolcache \ && chmod g+rwx /opt/hostedtoolcache COPY entrypoint.sh / -COPY --chown=runner:docker patched $RUNNER_ASSETS_DIR/patched # Add the Python "User Script Directory" to the PATH ENV PATH="${PATH}:${HOME}/.local/bin" diff --git a/runner/Dockerfile.dindrunner b/runner/Dockerfile.dindrunner index 19d9bfe2..00ad5e1f 100644 --- a/runner/Dockerfile.dindrunner +++ b/runner/Dockerfile.dindrunner @@ -114,8 +114,6 @@ RUN export ARCH=$(echo ${TARGETPLATFORM} | cut -d / -f2) \ VOLUME /var/lib/docker -COPY --chown=runner:docker patched $RUNNER_ASSETS_DIR/patched - # Add the Python "User Script Directory" to the PATH ENV PATH="${PATH}:${HOME}/.local/bin" ENV ImageOS=ubuntu20 diff --git a/runner/entrypoint.sh b/runner/entrypoint.sh index 4fbbfae9..ba46e645 100755 --- a/runner/entrypoint.sh +++ b/runner/entrypoint.sh @@ -92,7 +92,7 @@ cd ${RUNNER_HOME} # past that point, it's all relative pathes from /runner 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) echo "Passing --ephemeral to config.sh to enable the ephemeral runner." fi @@ -154,22 +154,17 @@ if [ -z "${UNITTEST:-}" ]; then mkdir ./externals # Hack due to the DinD volumes 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 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) 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." 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 # 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/issues/1703 -mapfile -t env { - 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); -}); \ No newline at end of file diff --git a/runner/patched/runsvc.sh b/runner/patched/runsvc.sh deleted file mode 100755 index 39cebc05..00000000 --- a/runner/patched/runsvc.sh +++ /dev/null @@ -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 \ No newline at end of file diff --git a/test/entrypoint/should_work_disable_update/config.sh b/test/entrypoint/assets/config.sh similarity index 66% rename from test/entrypoint/should_work_disable_update/config.sh rename to test/entrypoint/assets/config.sh index 1065f616..618a1b7a 100755 --- a/test/entrypoint/should_work_disable_update/config.sh +++ b/test/entrypoint/assets/config.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash export LIGHTGREEN='\e[0;32m' export LIGHTRED='\e[0;31m' @@ -18,11 +18,15 @@ error(){ } 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 -# 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=$((count + 1)) echo ${count} > counter diff --git a/test/entrypoint/logging.sh b/test/entrypoint/assets/logging.sh similarity index 92% rename from test/entrypoint/logging.sh rename to test/entrypoint/assets/logging.sh index 3d13bb9b..44edb56f 100755 --- a/test/entrypoint/logging.sh +++ b/test/entrypoint/assets/logging.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash export LIGHTGREEN='\e[0;32m' export LIGHTRED='\e[0;31m' diff --git a/test/entrypoint/should_retry_configuring/runsvc.sh b/test/entrypoint/assets/run.sh similarity index 64% rename from test/entrypoint/should_retry_configuring/runsvc.sh rename to test/entrypoint/assets/run.sh index a46a801b..484c4972 100755 --- a/test/entrypoint/should_retry_configuring/runsvc.sh +++ b/test/entrypoint/assets/run.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -euo pipefail @@ -20,12 +20,9 @@ error(){ exit 1 } +log "Dumping set runner arguments" +echo "$@" > runner_args +success "Pretending to run service..." +touch run_sh_ran +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 "" - - diff --git a/test/entrypoint/should_retry_configuring/config.sh b/test/entrypoint/should_retry_configuring/config.sh deleted file mode 100755 index bd18f4fd..00000000 --- a/test/entrypoint/should_retry_configuring/config.sh +++ /dev/null @@ -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 - diff --git a/test/entrypoint/should_retry_configuring/test.sh b/test/entrypoint/should_retry_configuring/test.sh index feb66e91..99c0dc4e 100755 --- a/test/entrypoint/should_retry_configuring/test.sh +++ b/test/entrypoint/should_retry_configuring/test.sh @@ -1,12 +1,12 @@ -#!/bin/bash +#!/usr/bin/env bash # UNITTEST: retry config # Will simulate a configuration failure and expects: # - the configuration step to be run 10 times # - 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() { while read I; do @@ -14,17 +14,22 @@ entrypoint_log() { 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 RUNNER_HOME=localhome +export FAIL_RUNNER_CONFIG_SETUP=true export RUNNER_NAME="example_runner_name" export RUNNER_REPO="myorg/myrepo" export RUNNER_TOKEN="xxxxxxxxxxxxx" -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 +# 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} @@ -33,41 +38,44 @@ cleanup() { unset RUNNER_NAME unset RUNNER_REPO 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 log "Running the entrypoint" 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) if [ "$?" != "2" ]; then error "=========================================" - error "Configuration should have thrown an error" + error "FAIL | Configuration should have thrown an error" exit 1 fi -success "Entrypoint didn't complete successfully" -success "" + +success "PASS | Entrypoint didn't complete successfully" log "Checking the counter, should have 10 iterations" count=`cat ${RUNNER_HOME}/counter || "notfound"` if [ "${count}" != "10" ]; then error "=============================================" - error "The retry loop should have done 10 iterations" + error "FAIL | The retry loop should have done 10 iterations" exit 1 fi -success "Retry loop went up to 10" -success +success "PASS | Retry loop went up to 10" -log "Checking that runsvc never ran" -if [ -f ${RUNNER_HOME}/runsvc_ran ]; then +log "Checking that run.sh never ran" +if [ -f ${RUNNER_HOME}/run_sh_ran ]; then 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 fi -success "runsvc.sh never ran" +success "PASS | run.sh never ran" success success "===========================" success "Test completed successfully" diff --git a/test/entrypoint/should_switch_to_ephemeral_upstream_feature/config.sh b/test/entrypoint/should_switch_to_ephemeral_upstream_feature/config.sh deleted file mode 100755 index 12f1e0bb..00000000 --- a/test/entrypoint/should_switch_to_ephemeral_upstream_feature/config.sh +++ /dev/null @@ -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 - diff --git a/test/entrypoint/should_switch_to_ephemeral_upstream_feature/runsvc.sh b/test/entrypoint/should_switch_to_ephemeral_upstream_feature/runsvc.sh deleted file mode 100755 index fa766433..00000000 --- a/test/entrypoint/should_switch_to_ephemeral_upstream_feature/runsvc.sh +++ /dev/null @@ -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 "" - - diff --git a/test/entrypoint/should_switch_to_ephemeral_upstream_feature/test.sh b/test/entrypoint/should_switch_to_ephemeral_upstream_feature/test.sh deleted file mode 100755 index 86f92531..00000000 --- a/test/entrypoint/should_switch_to_ephemeral_upstream_feature/test.sh +++ /dev/null @@ -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" diff --git a/test/entrypoint/should_work_disable_update/runsvc.sh b/test/entrypoint/should_work_disable_update/runsvc.sh deleted file mode 100755 index 8eb268fe..00000000 --- a/test/entrypoint/should_work_disable_update/runsvc.sh +++ /dev/null @@ -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 "" - - diff --git a/test/entrypoint/should_work_non_ephemeral/config.sh b/test/entrypoint/should_work_non_ephemeral/config.sh deleted file mode 100755 index 12f1e0bb..00000000 --- a/test/entrypoint/should_work_non_ephemeral/config.sh +++ /dev/null @@ -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 - diff --git a/test/entrypoint/should_work_non_ephemeral/runsvc.sh b/test/entrypoint/should_work_non_ephemeral/runsvc.sh deleted file mode 100755 index fa766433..00000000 --- a/test/entrypoint/should_work_non_ephemeral/runsvc.sh +++ /dev/null @@ -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 "" - - diff --git a/test/entrypoint/should_work_non_ephemeral/test.sh b/test/entrypoint/should_work_non_ephemeral/test.sh index a983c877..93cf8041 100755 --- a/test/entrypoint/should_work_non_ephemeral/test.sh +++ b/test/entrypoint/should_work_non_ephemeral/test.sh @@ -1,12 +1,12 @@ -#!/bin/bash +#!/usr/bin/env 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 +# - the run.sh script to run without the --once flag -source ../logging.sh +source ../assets/logging.sh entrypoint_log() { while read I; do @@ -14,18 +14,22 @@ entrypoint_log() { done } +log "Setting up test area" +export RUNNER_HOME=testarea +mkdir -p ${RUNNER_HOME} + 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=false -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 +# 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} @@ -37,16 +41,19 @@ cleanup() { unset RUNNER_EPHEMERAL } +# Always run cleanup when test ends regardless of how it ends trap cleanup SIGINT SIGTERM SIGQUIT EXIT log "Running the entrypoint" 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) if [ "$?" != "0" ]; then error "===========================================" - error "Entrypoint script did not exit successfully" + error "FAIL | Entrypoint script did not exit successfully" exit 1 fi @@ -54,19 +61,19 @@ 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" + error "FAIL | The configuration step was not run exactly once" exit 1 fi -success "The configuration ran ${count} time(s)" +success "PASS | The configuration ran ${count} time(s)" -log "Testing if runsvc ran" -if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then +log "Testing if run.sh ran" +if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then error "==============================" - error "The runner service has not run" + error "FAIL | The runner service has not run" exit 1 fi -success "The service ran" +success "PASS | run.sh ran" success "" success "===========================" success "Test completed successfully" diff --git a/test/entrypoint/should_work_normally/config.sh b/test/entrypoint/should_work_normally/config.sh deleted file mode 100755 index 1065f616..00000000 --- a/test/entrypoint/should_work_normally/config.sh +++ /dev/null @@ -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 - diff --git a/test/entrypoint/should_work_normally/runsvc.sh b/test/entrypoint/should_work_normally/runsvc.sh deleted file mode 100755 index 8eb268fe..00000000 --- a/test/entrypoint/should_work_normally/runsvc.sh +++ /dev/null @@ -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 "" - - diff --git a/test/entrypoint/should_work_normally/test.sh b/test/entrypoint/should_work_normally/test.sh index 4ef1762b..2331b263 100755 --- a/test/entrypoint/should_work_normally/test.sh +++ b/test/entrypoint/should_work_normally/test.sh @@ -1,12 +1,12 @@ -#!/bin/bash +#!/usr/bin/env bash # UNITTEST: should work normally # Will simulate a normal execution scenario. expects: # - the configuration step to be run exactly once # - 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() { while read I; do @@ -14,17 +14,21 @@ entrypoint_log() { done } +log "Setting up test area" +export RUNNER_HOME=testarea +mkdir -p ${RUNNER_HOME} + 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" -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 +# 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} @@ -35,11 +39,14 @@ cleanup() { unset RUNNER_TOKEN } +# Always run cleanup when test ends regardless of how it ends trap cleanup SIGINT SIGTERM SIGQUIT EXIT log "Running the entrypoint" 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) 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"` if [ ${count} != "1" ]; then error "===============================================" - error "The configuration step was not run exactly once" + error "FAIL | The configuration step was not run exactly once" exit 1 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" if grep -q -- '--ephemeral' ${RUNNER_HOME}/runner_config; then error "===============================================" - error "The configuration should not include the --ephemeral flag" + error "FAIL | The configuration should not include the --ephemeral flag" exit 1 fi -log "Testing if runsvc ran" -if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then +success "PASS | The --ephemeral switch was included in the configuration" + +log "Testing if run.sh ran" +if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then error "==============================" - error "The runner service has not run" + error "FAIL | The runner service has not run" exit 1 fi -success "The service ran" +success "PASS | run.sh ran" success "" success "===========================" success "Test completed successfully" diff --git a/test/entrypoint/should_work_disable_update/test.sh b/test/entrypoint/should_work_use_disable_update_switch/test.sh similarity index 54% rename from test/entrypoint/should_work_disable_update/test.sh rename to test/entrypoint/should_work_use_disable_update_switch/test.sh index c6875ae9..e74129db 100755 --- a/test/entrypoint/should_work_disable_update/test.sh +++ b/test/entrypoint/should_work_use_disable_update_switch/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # UNITTEST: should work disable update # Will simulate a scneario where disableupdate=true. expects: @@ -6,7 +6,7 @@ # - the entrypoint script to exit with no error # - the config.sh script to run with the --disableupdate flag set to 'true'. -source ../logging.sh +source ../assets/logging.sh entrypoint_log() { while read I; do @@ -14,18 +14,22 @@ entrypoint_log() { done } +log "Setting up test area" +export RUNNER_HOME=testarea +mkdir -p ${RUNNER_HOME} + 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 DISABLE_RUNNER_UPDATE="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 +# 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} @@ -36,16 +40,19 @@ cleanup() { unset RUNNER_TOKEN } +# 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 "Test completed with errors" + error "FAIL | Test completed with errors" exit 1 fi @@ -53,26 +60,28 @@ log "Testing if the configuration step was run only once" count=`cat ${RUNNER_HOME}/counter || echo "not_found"` if [ ${count} != "1" ]; then error "===============================================" - error "The configuration step was not run exactly once" + error "FAIL | The configuration step was not run exactly once" exit 1 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" if ! grep -q -- '--disableupdate' ${RUNNER_HOME}/runner_config; then error "===============================================" - error "The configuration should not include the --disableupdate flag" + error "FAIL | The configuration should not include the --disableupdate flag" exit 1 fi -log "Testing if runsvc ran" -if [ ! -f "${RUNNER_HOME}/runsvc_ran" ]; then +success "PASS | The --disableupdate switch was included in the configuration" + +log "Testing if run.sh ran" +if [ ! -f "${RUNNER_HOME}/run_sh_ran" ]; then error "==============================" - error "The runner service has not run" + error "FAIL | The runner service has not run" exit 1 fi -success "The service ran" +success "PASS | run.sh ran" success "" success "===========================" success "Test completed successfully" diff --git a/test/entrypoint/should_work_use_once_switch/test.sh b/test/entrypoint/should_work_use_once_switch/test.sh new file mode 100755 index 00000000..7a9a8d79 --- /dev/null +++ b/test/entrypoint/should_work_use_once_switch/test.sh @@ -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" diff --git a/test/entrypoint/entrypoint_unittest.sh b/test/entrypoint/test.sh similarity index 94% rename from test/entrypoint/entrypoint_unittest.sh rename to test/entrypoint/test.sh index 28e24fe9..556605fc 100755 --- a/test/entrypoint/entrypoint_unittest.sh +++ b/test/entrypoint/test.sh @@ -1,13 +1,12 @@ -#!/bin/bash +#!/usr/bin/env bash -source logging.sh +source assets/logging.sh for unittest in ./should*; do log "**********************************" log " UNIT TEST: ${unittest}" log "**********************************" log "" - cd ${unittest} ./test.sh ret_code=$?