diff --git a/tests/unit/bash/bash_os_linux_conditional.bats.jinja b/tests/unit/bash/bash_os_linux_conditional.bats.jinja index dc8a08c0a69..c25a4500627 100644 --- a/tests/unit/bash/bash_os_linux_conditional.bats.jinja +++ b/tests/unit/bash/bash_os_linux_conditional.bats.jinja @@ -2,11 +2,44 @@ 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 + # shellcheck disable=SC2034 + 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 xpwd + tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + xpwd="$(readlink -f -- .)" + 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 73cbb31637d..1766ddaebb4 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 163bb503c82..b680fc939c0 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,44 @@ 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 + # shellcheck disable=SC2034 + 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 xpwd + tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + xpwd="$(readlink -f -- .)" + 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 856ab3efd23..9df981d4e9b 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,45 @@ 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 + # shellcheck disable=SC2034 + 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 xpwd + tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + xpwd="$(readlink -f -- .)" + 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 db0d1c3b8d2..f989eb5ecb4 100644 --- a/tests/unit/bash/test_set_config_file.bats.jinja +++ b/tests/unit/bash/test_set_config_file.bats.jinja @@ -17,12 +17,45 @@ 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 + # shellcheck disable=SC2034 + 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 xpwd + tmppath="$(readlink -f -- "${BATS_TEST_TMPDIR}")" + if [[ ! "${tmppath}" =~ ^/tmp/ ]] || [[ ! -d "${tmppath}" ]]; then + >&2 echo "INTERNAL ERROR" + exit 3 + fi + xpwd="$(readlink -f -- .)" + 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"