Skip to content

Commit

Permalink
fix: execute_tests: handle older versions
Browse files Browse the repository at this point in the history
Seems Debian 10 has 0.4.0, so need to disable some features.
  • Loading branch information
maage committed May 18, 2023
1 parent c8928e0 commit 628c5fb
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 8 deletions.
35 changes: 34 additions & 1 deletion tests/unit/bash/bash_os_linux_conditional.bats.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 24 additions & 4 deletions tests/unit/bash/execute_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
35 changes: 34 additions & 1 deletion tests/unit/bash/test_bash_ensure_ini_config.bats.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 34 additions & 1 deletion tests/unit/bash/test_bash_replace_or_append.bats.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
35 changes: 34 additions & 1 deletion tests/unit/bash/test_set_config_file.bats.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 628c5fb

Please sign in to comment.