From da53a3aa5208d90973fab10b0d66f58387e49d59 2024-09-09 23:01:48
From: Branko Majic <branko@majic.rs>
Date: 2024-09-09 23:01:48
Subject: [PATCH] MAR-218: Fix the test run logic in test runner script:

- Linter tests should _always_ be run.
- Test report files should be appended to when using tee.
- Get rid of excess molecule destroy (probably not required any
  longer). This will reduce the output and speed up things a little
  bit.

---

diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh
index aa067d132196cda81fee9a7e79b0dc2bb64c22c8..059a8d418d28dc91aea638e80cf733465a9cdb9d 100755
--- a/scripts/run_tests.sh
+++ b/scripts/run_tests.sh
@@ -274,9 +274,6 @@ for role in "${roles_to_test[@]}"; do
     # Calculate directories between which we need to move.
     role_dir="$work_dir/roles/$role"
 
-    # Run tests.
-    cd "$role_dir"
-
     if [[ $report == 1 ]]; then
         report_file="$report_directory/role-${role}.txt"
     else
@@ -284,34 +281,45 @@ for role in "${roles_to_test[@]}"; do
     fi
 
     info "Running tests for: $role"
-    molecule_result=0
-    if [[ $tests == all ]]; then
 
-        molecule test --destroy always 2>&1 | tee "$report_file"
-        last_pipe_status="${PIPESTATUS[0]}"
-        [[ $last_pipe_status == 0 ]] || molecule_result="$last_pipe_status"
+    role_test_result=0
+
+    # Switch directory, make sure it is successful.
+    # shellcheck disable=SC2164  # failed directory switch is dealt with additional checks below.
+    cd "$role_dir" 2>&1 | tee -a "$report_file"
+    last_pipe_status="${PIPESTATUS[0]}"
+    [[ $last_pipe_status == 0 ]] || role_test_result="$last_pipe_status"
 
-    elif [[ $tests == lint ]]; then
+    # Run the linters.
+    if [[ $role_test_result == 0 ]]; then
+
+        flake8 . 2>&1 | tee -a "$report_file"
+        last_pipe_status="${PIPESTATUS[0]}"
+        [[ $last_pipe_status == 0 ]] || role_test_result="$last_pipe_status"
 
-        flake8 . 2>&1 | tee "$report_file"
+        yamllint . 2>&1 | tee -a "$report_file"
         last_pipe_status="${PIPESTATUS[0]}"
-        [[ $last_pipe_status == 0 ]] || molecule_result="$last_pipe_status"
+        [[ $last_pipe_status == 0 ]] || role_test_result="$last_pipe_status"
 
-        yamllint . 2>&1 | tee "$report_file"
+        ansible-lint -q . 2>&1 | tee -a "$report_file"
         last_pipe_status="${PIPESTATUS[0]}"
-        [[ $last_pipe_status == 0 ]] || molecule_result="$last_pipe_status"
+        [[ $last_pipe_status == 0 ]] || role_test_result="$last_pipe_status"
+    fi
+
+    # Run the unit/functional tests (if requested).
+    if [[ $role_test_result == 0 && $tests == all ]]; then
 
-        ansible-lint --quiet . 2>&1 | tee "$report_file"
+        molecule test --destroy always 2>&1 | tee -a "$report_file"
         last_pipe_status="${PIPESTATUS[0]}"
-        [[ $last_pipe_status == 0 ]] || molecule_result="$last_pipe_status"
+        [[ $last_pipe_status == 0 ]] || role_test_result="$last_pipe_status"
 
     fi
 
     # Determine result.
-    if [[ $molecule_result == 0 ]]; then
+    if [[ $role_test_result == 0 ]]; then
         passed_roles+=("$role")
 
-        # Log failure in summary if requested.
+        # Log success in summary if requested.
         [[ $report == 1 ]] && echo "[PASS] $role" >> "$report_summary"
     else
         test_result=$ERROR_FAILED_ROLES
@@ -321,11 +329,6 @@ for role in "${roles_to_test[@]}"; do
         [[ $report == 1 ]] && echo "[FAIL] $role" >> "$report_summary"
     fi
 
-    # Make sure the instances have been cleaned-up to avoid errors.
-    if [[ $tests == all ]]; then
-        molecule destroy
-    fi
-
 done
 
 for role in "${roles_to_skip[@]}"; do