From 4361cdd8c038c258683fc266554bf8c4f05fa0d6 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Mon, 28 Oct 2024 13:20:33 -0400 Subject: Add environment setup script to test directory --- tests/setup.sh | 186 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) create mode 100644 tests/setup.sh (limited to 'tests/setup.sh') diff --git a/tests/setup.sh b/tests/setup.sh new file mode 100644 index 0000000..78c710d --- /dev/null +++ b/tests/setup.sh @@ -0,0 +1,186 @@ +#!/usr/bin/env bash +set -o pipefail + +export LOGFILE_STASIS="stasis.log" +export LOGFILE_INDEXER="stasis_indexer.log" +export CHECK_OUTPUT_PATTERNS=() + +unset STASIS_SYSCONFDIR +if [ -n "$GITHUB_TOKEN" ] && [ -z "$STASIS_GH_TOKEN"]; then + export STASIS_GH_TOKEN="$GITHUB_TOKEN" +else + export STASIS_GH_TOKEN="anonymous" +fi + +if [[ -z "$PYTHON_VERSIONS" ]]; then + PYTHON_VERSIONS=( + 3.10 + 3.11 + 3.12 + ) +fi + +setup_script_dir="$(dirname ${BASH_SOURCE[0]})" +TOPDIR=$(pwd) + +WS_DEFAULT=rt_workspace_ +setup_workspace() { + if [ -z "$1" ]; then + echo "setup_workspace requires a name argument" >&2 + return 1 + fi + WORKSPACE="${WS_DEFAULT}$1" + rm -rf "$WORKSPACE" + if ! mkdir -p "$WORKSPACE"; then + echo "directory creation failed. cannot continue" >&2 + return 1; + fi + WORKSPACE="$(realpath $WORKSPACE)" + + export PREFIX="$WORKSPACE"/local + if ! mkdir -p "$PREFIX"; then + echo "directory creation failed. cannot continue" >&2 + return 1; + fi + + export BUILD_DIR="$WORKSPACE"/build + if ! mkdir -p "$BUILD_DIR"; then + echo "directory creation failed. cannot continue" >&2 + return 1; + fi + + pushd "$WORKSPACE" + export LANG="C" + export HOME="$WORKSPACE" + . /etc/profile +} + +teardown_workspace() { + if [ -z "$1" ]; then + echo "teardown_workspace requires a workspace path" >&2 + return 1 + elif ! [[ ${WS_DEFAULT}$1 =~ ${WS_DEFAULT}.* ]]; then + echo "$1 is not a valid workspace" >&2 + return 1 + fi + popd + clean_up +} + +install_stasis() { + pushd "$BUILD_DIR" + if ! cmake -DCMAKE_INSTALL_PREFIX="$PREFIX" -DCMAKE_BUILD_TYPE=Debug "${TOPDIR}"/../..; then + echo "cmake failed" >&2 + return 1 + fi + + if ! make install; then + echo "make failed" >&2 + return 1 + fi + + export PATH="$PREFIX/bin:$PATH" + hash -r + if ! type -P stasis; then + echo "stasis program not on PATH" >&2 + return 1 + fi + + if ! type -P stasis_indexer; then + echo "stasis_indexer program not on PATH" >&2 + return 1 + fi + popd +} + +run_stasis() { + local logfile="$LOGFILE_STASIS" + $(type -P stasis) --unbuffered -v $@ 2>&1 | tee "$logfile" +} + +run_stasis_indexer() { + local logfile="$LOGFILE_INDEXER" + local root="$1" + if [ -z "$root" ]; then + echo "run_stasis_indexer root directory cannot be empty" >&2 + exit 1 + fi + $(type -P stasis_indexer) --web --unbuffered -v "$root"/* 2>&1 | tee "$logfile" +} + +check_output_add() { + local pattern="$1" + CHECK_OUTPUT_PATTERNS+=("$pattern") +} + +check_output_reset() { + CHECK_OUTPUT_PATTERNS=() +} + +check_output_stasis_dir() { + local startdir="$1" + local logfile="$LOGFILE_STASIS" + + echo "#### Files ####" + find $startdir | sort + echo + + echo "#### Contents ####" + files=$(find $startdir -type f \( -name "$logfile" -o -name '*.yml' -o -name '*.md' -o -name '*.stasis' -o -name '*.ini' \) | sort) + for x in $files; do + echo + echo "FILENAME: $x" + echo + if [ "$x" == "$logfile" ]; then + # do not print thousands of lines of output we _just_ sat through + echo "Output omitted" + else + cat "$x" + echo "[EOF]" + fi + echo + + for cond in "${CHECK_OUTPUT_PATTERNS[@]}"; do + if grep --color -H -n "$cond" "$x" >&2; then + echo "ERROR DETECTED IN $x!" >&2 + retcode+=1 + fi + done + done + + return $retcode +} + +check_output_indexed_dir() { + local retcode=0 + local startdir="$1" + local logfile="$2" + + echo "#### Files ####" + find $startdir | sort + + for cond in "${CHECK_OUTPUT_PATTERNS[@]}"; do + if grep --color -H -n "$cond" "$logfile" >&2; then + echo "ERROR DETECTED IN INDEX OPERATION!" >&2 + retcode+=1 + fi + done + + echo "#### Contents ####" + files=$(find $startdir -type f \( -name '*.html' \) | sort) + for x in $files; do + echo + echo "FILENAME: $x" + echo + cat "$x" + echo "[EOF]" + echo + done + return $retcode +} + +clean_up() { + if [ -z "$RT_KEEP_WORKSPACE" ] && [ -d "$WORKSPACE" ]; then + rm -rf "$WORKSPACE" + fi +} \ No newline at end of file -- cgit From 9aa6d8c681bcf6d6c0df25b2d0624e6ee6abd3c5 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 30 Oct 2024 09:16:55 -0400 Subject: Exposes TOPDIR and TEST_DATA variables to test script(s) --- tests/setup.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/setup.sh') diff --git a/tests/setup.sh b/tests/setup.sh index 78c710d..014f7fc 100644 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -21,7 +21,8 @@ if [[ -z "$PYTHON_VERSIONS" ]]; then fi setup_script_dir="$(dirname ${BASH_SOURCE[0]})" -TOPDIR=$(pwd) +export TOPDIR=$(pwd) +export TEST_DATA="$TOPDIR"/data WS_DEFAULT=rt_workspace_ setup_workspace() { -- cgit From 7a77598e0355c8d1df97745f1678363f050fc607 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 30 Oct 2024 09:18:14 -0400 Subject: Implement a runner (run_command) and generate a summary (run_summary) when the script ends --- tests/setup.sh | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) (limited to 'tests/setup.sh') diff --git a/tests/setup.sh b/tests/setup.sh index 014f7fc..743fafd 100644 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -94,6 +94,36 @@ install_stasis() { popd } + +STASIS_TEST_RESULT_FAIL=0 +STASIS_TEST_RESULT_PASS=0 +STASIS_TEST_RESULT_SKIP=0 +run_command() { + local logfile="$(mktemp).log" + local cmd="${@}" + local lines_on_error=100 + /bin/echo "Testing: $cmd " + if ! $cmd &>"$logfile"; then + echo "... FAIL" + echo "#" + echo "# Last $lines_on_error line(s) follow:" + echo "#" + tail -n $lines_on_error "$logfile" + (( STASIS_TEST_RESULT_FAIL++ )) + else + echo "... PASS" + (( STASIS_TEST_RESULT_PASS++ )) + fi + rm -f "$logfile" +} + +run_summary() { + local total=$(( STASIS_TEST_RESULT_PASS + STASIS_TEST_RESULT_FAIL + STASIS_TEST_RESULT_SKIP)) + echo + echo "[RT] ${STASIS_TEST_RESULT_PASS} tests passed, ${STASIS_TEST_RESULT_FAIL} failed, ${STASIS_TEST_RESULT_SKIP} skipped out of ${total}" + echo +} + run_stasis() { local logfile="$LOGFILE_STASIS" $(type -P stasis) --unbuffered -v $@ 2>&1 | tee "$logfile" @@ -119,6 +149,7 @@ check_output_reset() { } check_output_stasis_dir() { + local retcode=0 local startdir="$1" local logfile="$LOGFILE_STASIS" @@ -149,7 +180,11 @@ check_output_stasis_dir() { done done - return $retcode + if (( retcode )); then + return 1 + else + return 0 + fi } check_output_indexed_dir() { @@ -177,11 +212,46 @@ check_output_indexed_dir() { echo "[EOF]" echo done - return $retcode + if (( retcode )); then + return 1 + else + return 0 + fi +} + +assert_eq() { + local a="$1" + local b="$2" + local msg="$3" + if [[ "$a" == "$b" ]]; then + return 0 + else + [[ -n "$msg" ]] && echo "'$a' != '$b' :: $msg" >&2 + return 1 + fi +} + +assert_file_contains() { + local file="$1" + local str="$2" + local msg="$3" + if grep -E "$str" "$file" &>/dev/null; then + return 0 + else + [[ -n "$msg" ]] && echo "'$str' not in file '$file' :: $msg" >&2 + return 1 + fi } clean_up() { if [ -z "$RT_KEEP_WORKSPACE" ] && [ -d "$WORKSPACE" ]; then rm -rf "$WORKSPACE" fi + + run_summary + if (( STASIS_TEST_RESULT_FAIL )); then + exit 1 + else + exit 0 + fi } \ No newline at end of file -- cgit From caa444b44f1ce1974bd9f46c68e45ff26d251bdf Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 30 Oct 2024 11:28:53 -0400 Subject: Implement missing SKIP conditions * The not (bang) overrides the return code. Split the command from the if-statement to obtain the real code, not just true/false * Print the last n lines of the log only when there's something to print --- tests/setup.sh | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) (limited to 'tests/setup.sh') diff --git a/tests/setup.sh b/tests/setup.sh index 743fafd..0875cac 100644 --- a/tests/setup.sh +++ b/tests/setup.sh @@ -103,13 +103,23 @@ run_command() { local cmd="${@}" local lines_on_error=100 /bin/echo "Testing: $cmd " - if ! $cmd &>"$logfile"; then - echo "... FAIL" - echo "#" - echo "# Last $lines_on_error line(s) follow:" - echo "#" - tail -n $lines_on_error "$logfile" - (( STASIS_TEST_RESULT_FAIL++ )) + + $cmd &>"$logfile" + code=$? + if (( code )); then + if (( code == 127 )); then + echo "... SKIP" + (( STASIS_TEST_RESULT_SKIP++ )) + else + echo "... FAIL" + if (( $(wc -c "$logfile" | cut -d ' ' -f 1) > 1 )); then + echo "#" + echo "# Last $lines_on_error line(s) follow:" + echo "#" + tail -n $lines_on_error "$logfile" + fi + (( STASIS_TEST_RESULT_FAIL++ )) + fi else echo "... PASS" (( STASIS_TEST_RESULT_PASS++ )) -- cgit