From e6822a74ed1fccf7bc47cd0098c1884966259368 Mon Sep 17 00:00:00 2001 From: Petr Šplíchal Date: Apr 08 2019 14:43:16 +0000 Subject: Move basic role test execution to a separate file Adds a new script 'run-basic-test' which contains test execution logic for the standard-test-basic role. Uses environment variables to hand over necessary parameters. Fails when required parameters are not provided or test command is not found. --- diff --git a/roles/standard-test-basic/files/run-basic-test b/roles/standard-test-basic/files/run-basic-test new file mode 100644 index 0000000..99c8c31 --- /dev/null +++ b/roles/standard-test-basic/files/run-basic-test @@ -0,0 +1,59 @@ +#!/bin/bash -ef + +# Called from standard-test-basic to run basic tests. +# This script is copied to the test-environment. +# Expects the following environment variables: +# +# TEST_NAME ........ name of the test for reporting +# TEST_DIR ......... directory for test execution +# TEST_CMD ......... shell command to run the test +# TEST_ARTIFACTS ... directory for storing artifacts + +TEST_ARTIFACTS="${TEST_ARTIFACTS:-/tmp}" +TEST_LOG="$TEST_ARTIFACTS/test.log" + +# Log a failure and exit +error() { + echo "ERROR $1" >> $TEST_LOG + exit 1 +} + +# Make sure that test name, directory and command are defined +[[ -z ${TEST_NAME} ]] && error "Test name is not set." +[[ -z ${TEST_DIR} ]] && error "Test directory for $TEST_NAME not found." +[[ -z ${TEST_CMD} ]] && error "Test command for $TEST_NAME not provided." + +# Log all output to the test-specific log file +log_file_name=$(echo $TEST_NAME | sed -e 's/\//-/g').log +log_file=$TEST_ARTIFACTS/${log_file_name} +exec 2>>$log_file 1>>$log_file + +# Make sure we always exit in a clean way +finish() { + code=$? + echo "Test $TEST_NAME finished with exit code $code." + trap - SIGINT SIGTERM SIGABRT EXIT # Clear the trap + + # Return non-zero when test command not found + if [[ $code -eq 127 ]]; then + error "$TEST_NAME (problem with test execution)" + fi + + # Log result and prefix test-specific log file with status + [[ $code -eq 0 ]] && status="PASS" || status="FAIL" + echo "${status} $TEST_NAME" >> $TEST_LOG + mv ${log_file} $TEST_ARTIFACTS/${status}_${log_file_name} + exit 0 +} +trap finish SIGINT SIGTERM SIGABRT EXIT + +# Print a short summary +echo " +Test name: $TEST_NAME +Test directory: $TEST_DIR +Test command: $TEST_CMD +" + +# Execute the test +cd $TEST_DIR +eval $TEST_CMD diff --git a/roles/standard-test-basic/tasks/main.yml b/roles/standard-test-basic/tasks/main.yml index d51e295..5c5b58c 100644 --- a/roles/standard-test-basic/tasks/main.yml +++ b/roles/standard-test-basic/tasks/main.yml @@ -2,48 +2,20 @@ - block: - name: Execute tests - shell: | - if [[ -z ${TEST} ]]; then - echo "FAIL: Test case name is not set" >> {{ remote_artifacts }}/test.log - exit - fi - if [[ -z ${TEST_DIR} ]]; then - echo "FAIL: Test directory for $TEST not found" >> {{ remote_artifacts }}/test.log - exit - fi - if [[ -z ${TEST_CMD} ]]; then - echo "FAIL: Does not know how to run $TEST" >> {{ remote_artifacts }}/test.log - exit - fi - log_file_name=$(echo $TEST | sed -e 's/\//-/g').log - logfile={{ remote_artifacts }}/str_${log_file_name} - exec 2>>$logfile 1>>$logfile - cd $TEST_DIR - #if command is a file make it executable - cmd="$(echo $TEST_CMD | awk '{print $1;}')" - if [ -f "$cmd" ]; then - chmod 0775 "$cmd" - fi - status="FAIL" - #execute the test - eval $TEST_CMD - if [ $? -eq 0 ]; then - status="PASS" - fi - echo "${status} $TEST" >> {{ remote_artifacts }}/test.log - # Add test status as prefix to test case log - mv ${logfile} {{ remote_artifacts }}/${status}_str_${log_file_name} + script: run-basic-test environment: - #Allow to use tests as a simple string which means the test is expected - #to be in a directory with same name and test script `runtest.sh` - #It is also possible to define the test as dictionary, in that case it is possible - #to change test directory and test command line parameters while test name is item key - TEST: + # Allow to use tests as a simple string which means the test is expected + # to be in a directory with same name and test script `runtest.sh` + # It is also possible to define the test as dictionary, in that case it is possible + # to change test directory and test command line parameters while test name is item key + TEST_NAME: "{{ item if item.keys is not defined else (item.keys()|list)[0] }}" TEST_DIR: - "{{ tenv_workdir }}/{{ item if item.keys is not defined else item[(item.keys()|list)[0]]['dir']|default((item.keys()|list)[0]) }}" + "{{ tenv_workdir }}{{ item if item.keys is not defined else item[(item.keys()|list)[0]]['dir']|default((item.keys()|list)[0]) }}" TEST_CMD: "{{'./runtest.sh' if item.keys is not defined else item[(item.keys()|list)[0]]['run']|default('./runtest.sh')}}" + TEST_ARTIFACTS: + "{{ remote_artifacts }}" with_items: - "{{ tests }}"