From 97968ec1125fafe50f10ce58272dc6d4a96420dc Mon Sep 17 00:00:00 2001 From: Markus Linnala Date: Tue, 16 May 2023 17:24:13 +0300 Subject: [PATCH] fix: execute_tests: handle older versions Seems Debian 10 has 0.4.0, so need to disable some features. --- .../bash/bash_os_linux_conditional.bats.jinja | 33 ++++++++++++++++++- tests/unit/bash/execute_tests.sh | 28 +++++++++++++--- .../test_bash_ensure_ini_config.bats.jinja | 33 ++++++++++++++++++- .../test_bash_replace_or_append.bats.jinja | 33 ++++++++++++++++++- .../unit/bash/test_set_config_file.bats.jinja | 33 ++++++++++++++++++- 5 files changed, 152 insertions(+), 8 deletions(-) diff --git a/tests/unit/bash/bash_os_linux_conditional.bats.jinja b/tests/unit/bash/bash_os_linux_conditional.bats.jinja index dc8a08c0a69e..75e706b9ff3c 100644 --- a/tests/unit/bash/bash_os_linux_conditional.bats.jinja +++ b/tests/unit/bash/bash_os_linux_conditional.bats.jinja @@ -2,11 +2,42 @@ set -pu +is_old_bats=0 + setup() { - pushd "${BATS_TEST_TMPDIR}" + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + BATS_TEST_TMPDIR="$(mktemp -d)" # 1.4.0 + BATS_TEARDOWN_STARTED= # 1.3.0 + is_old_bats=1 + else + is_old_bats=0 + fi + pushd "${BATS_TEST_TMPDIR}" || exit 1 os_release_path="os-release" } +teardown() { + if (( is_old_bats )); then + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local xpwd="$(readlink -f -- "$(pwd)")" + if [[ "${tmppath}" != "${xpwd}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + popd || exit 1 + rm -rf -- "${tmppath}" + BATS_TEST_TMPDIR="" + fi +} + @test "bash_os_linux_conditional - test OS release - RHEL" { cat << EOF > "$os_release_path" NAME="Red Hat Enterprise Linux" diff --git a/tests/unit/bash/execute_tests.sh b/tests/unit/bash/execute_tests.sh index 1b62dad774b3..d2ac0ba59456 100755 --- a/tests/unit/bash/execute_tests.sh +++ b/tests/unit/bash/execute_tests.sh @@ -36,6 +36,23 @@ while (( $# )); do esac done +bats_version="$(bats -v)" || : +case "${bats_version##* }" in + # Debian 10 v0.4.0 + # Usage: bats [-c] [-p | -t] + ""|"0."*|"1.0."*|"1.1."*) + OPT_parallel=0 + OPT_verbose=0 + OPT_debug=0 + ;; + # Ubuntu 22.04 + # Error: Bad command line option '--print-output-on-failure' + "1.2."*|"1.3."*|"1.4."*) + OPT_verbose=0 + OPT_debug=0 + ;; +esac + PYTHON_EXECUTABLE="$1"; shift TESTS_ROOT="$1"; shift TESTDIR="$1"; shift @@ -46,17 +63,20 @@ mkdir -p "${OUTDIR}" bats_opts=() if (( OPT_parallel )); then - bats_opts+=(--jobs "$(nproc)") + bats_opts+=(--jobs "$(nproc)") # 1.2.0 fi if (( OPT_verbose > 1 )); then - bats_opts+=(--verbose-run) + bats_opts+=(--verbose-run) # 1.5.0 elif (( OPT_verbose == 1 )); then - bats_opts+=(--print-output-on-failure) + bats_opts+=(--print-output-on-failure) # 1.5.0 fi if (( OPT_debug )); then - bats_opts+=(--no-tempdir-cleanup --trace) + bats_opts+=( + --no-tempdir-cleanup # 1.4.0 + --trace # 1.5.0 + ) fi rc=0 diff --git a/tests/unit/bash/test_bash_ensure_ini_config.bats.jinja b/tests/unit/bash/test_bash_ensure_ini_config.bats.jinja index 163bb503c824..eadd512a0697 100644 --- a/tests/unit/bash/test_bash_ensure_ini_config.bats.jinja +++ b/tests/unit/bash/test_bash_ensure_ini_config.bats.jinja @@ -6,11 +6,42 @@ function call_bash_ensure_ini_config { {{{ bash_ensure_ini_config("$1", "$2", "$3", "$4") | indent(4) }}} } +is_old_bats=0 + setup() { - pushd "${BATS_TEST_TMPDIR}" + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + BATS_TEST_TMPDIR="$(mktemp -d)" # 1.4.0 + BATS_TEARDOWN_STARTED= # 1.3.0 + is_old_bats=1 + else + is_old_bats=0 + fi + pushd "${BATS_TEST_TMPDIR}" || exit 1 mkdir -p sssd_test } +teardown() { + if (( is_old_bats )); then + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local xpwd="$(readlink -f -- "$(pwd)")" + if [[ "${tmppath}" != "${xpwd}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + popd || exit 1 + rm -rf -- "${tmppath}" + BATS_TEST_TMPDIR="" + fi +} + @test "bash_ensure_ini_config - Basic value remediation" { printf "[pam]\npam_cert_auth = false\n" > sssd_test/sssd.conf expected_output="[pam]\npam_cert_auth = true\n" diff --git a/tests/unit/bash/test_bash_replace_or_append.bats.jinja b/tests/unit/bash/test_bash_replace_or_append.bats.jinja index ffb54d3e8c3b..be87fd14ddbc 100644 --- a/tests/unit/bash/test_bash_replace_or_append.bats.jinja +++ b/tests/unit/bash/test_bash_replace_or_append.bats.jinja @@ -10,12 +10,43 @@ function call_bash_replace_or_append_w_format { {{{ bash_replace_or_append("$1", "$2", "$3", "$4") | indent(4) }}} } +is_old_bats=0 + setup() { - pushd "${BATS_TEST_TMPDIR}" + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + BATS_TEST_TMPDIR="$(mktemp -d)" # 1.4.0 + BATS_TEARDOWN_STARTED= # 1.3.0 + is_old_bats=1 + else + is_old_bats=0 + fi + pushd "${BATS_TEST_TMPDIR}" || exit 1 tmp_file=test.sh touch "$tmp_file" } +teardown() { + if (( is_old_bats )); then + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local xpwd="$(readlink -f -- "$(pwd)")" + if [[ "${tmppath}" != "${xpwd}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + popd || exit 1 + rm -rf -- "${tmppath}" + BATS_TEST_TMPDIR="" + fi +} + @test "bash_replace_or_append - Basic value remediation" { printf "%s\n" "kernel.randomize_va_space = 5" > "$tmp_file" expected_output="kernel.randomize_va_space = 2\n" diff --git a/tests/unit/bash/test_set_config_file.bats.jinja b/tests/unit/bash/test_set_config_file.bats.jinja index b01f7a2ba184..d88024604a52 100644 --- a/tests/unit/bash/test_set_config_file.bats.jinja +++ b/tests/unit/bash/test_set_config_file.bats.jinja @@ -17,12 +17,43 @@ function call_set_config_file_rsyslog { create=true, separator=" ", separator_regex=" ") }}} } +is_old_bats=0 + setup() { - pushd "${BATS_TEST_TMPDIR}" + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + BATS_TEST_TMPDIR="$(mktemp -d)" # 1.4.0 + BATS_TEARDOWN_STARTED= # 1.3.0 + is_old_bats=1 + else + is_old_bats=0 + fi + pushd "${BATS_TEST_TMPDIR}" || exit 1 tmp_file=test.conf touch "$tmp_file" } +teardown() { + if (( is_old_bats )); then + if [[ -z "${BATS_TEST_TMPDIR:-}" ]] || [[ ! -d "${BATS_TEST_TMPDIR}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + local xpwd="$(readlink -f -- "$(pwd)")" + if [[ "${tmppath}" != "${xpwd}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + popd || exit 1 + rm -rf -- "${tmppath}" + BATS_TEST_TMPDIR="" + fi +} + @test "set_config_file - Basic value remediation" { printf "%s\n" "Compression yes" > "$tmp_file" expected_output="Compression no\n"