diff --git a/tests/tests.sh b/tests/tests.sh index ab18bed3..b44bbd21 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -61,7 +61,7 @@ cecho() { pass() { local func_name=$1 local test_num=$2 - cecho "GREEN" "✔ PASS - $func_name $test_num" + cecho "GREEN" "✔ PASS - $func_name $test_num" } fail() { @@ -70,6 +70,93 @@ fail() { cecho "RED" "X $func_name $test_num FAIL" } +test_docker() { + local mock_docker_ps_lines + local disallowed_docker_output + local expected_docker_output + local test_name + + # Parse named parameters + while [[ "$#" -gt 0 ]]; do + case $1 in + --mock) + mock_docker_ps_lines="$2" + shift + ;; + --disallow) + disallowed_docker_output="$2" + shift + ;; + --expect) + expected_docker_output="$2" + shift + ;; + --name) + test_name="$2" + shift + ;; + *) + echo "Unknown parameter passed: $1" + exit 1 + ;; + esac + shift + done + + IFS=$'\n' read -rd '' -a mock_docker_ps_lines_arr <<<"$mock_docker_ps_lines" + IFS=$'\n' read -rd '' -a disallowed_docker_output_arr <<<"$disallowed_docker_output" + IFS=$'\n' read -rd '' -a expected_docker_output_arr <<<"$expected_docker_output" + + # Set what the next docker ps command should return + MOCK_DOCKER_PS_OUTPUT=$(printf "%s\n" "${mock_docker_ps_lines_arr[@]}") + + source pkg/entry.sh + + test_passed=true # Initialize a flag to indicate test status + + mapfile -t docker_actual_output <"$DOCKER_COMMANDS_FILE" + + # Check if each expected command is in the actual output + for expected_docker in "${expected_docker_output_arr[@]}"; do # Use the _arr array here + found=false + for docker_actual in "${docker_actual_output[@]}"; do + if [[ "$docker_actual" == "$expected_docker" ]]; then + found=true + break + fi + done + if [ "$found" = false ]; then + fail "$test_name" + echo "'$expected_docker' not found in expected_docker_output." + test_passed=false + exit 1 + fi + done + + # Check if any disallowed command is in the actual output + for disallowed_docker in "${disallowed_docker_output_arr[@]}"; do # Use the _arr array here + for docker_actual in "${docker_actual_output[@]}"; do + if [[ "$docker_actual" == "$disallowed_docker" ]]; then + fail "$test_name" + echo "'$disallowed_docker' found in actual output but is disallowed." + test_passed=false + exit 1 + fi + done + done + + if [ "$test_passed" = true ]; then + pass "$test_name" + else + fail "$test_name" "Commands do not match expected output." + echo "Expected:" + printf "%s\n" "${expected_docker_output_arr[@]}" + echo "Actual:" + printf "%s\n" "${docker_actual_output[@]}" + exit 1 + fi +} + # ---- Actual Tests ---- test_docker_ps() { @@ -302,116 +389,31 @@ test_skip_containers() { } -test_docker() { - local mock_docker_ps_lines - local disallowed_docker_output - local expected_docker_output - local test_name - - # Parse named parameters - while [[ "$#" -gt 0 ]]; do - case $1 in - --mock) - mock_docker_ps_lines="$2" - shift - ;; - --disallow) - disallowed_docker_output="$2" - shift - ;; - --expect) - expected_docker_output="$2" - shift - ;; - --name) - test_name="$2" - shift - ;; - *) - echo "Unknown parameter passed: $1" - exit 1 - ;; - esac - shift - done - - IFS=$'\n' read -rd '' -a mock_docker_ps_lines_arr <<<"$mock_docker_ps_lines" - IFS=$'\n' read -rd '' -a disallowed_docker_output_arr <<<"$disallowed_docker_output" - IFS=$'\n' read -rd '' -a expected_docker_output_arr <<<"$expected_docker_output" - +test_docker_commands() { clear_files export BACKUP_ON_START="true" mkdir -p tests/src/container1 && touch tests/src/container1/test.txt mkdir -p tests/dest - # Set what the next docker ps command should return - MOCK_DOCKER_PS_OUTPUT=$(printf "%s\n" "${mock_docker_ps_lines_arr[@]}") - - source pkg/entry.sh - - test_passed=true # Initialize a flag to indicate test status - - mapfile -t docker_actual_output <"$DOCKER_COMMANDS_FILE" - - # Check if each expected command is in the actual output - for expected_docker in "${expected_docker_output_arr[@]}"; do # Use the _arr array here - found=false - for docker_actual in "${docker_actual_output[@]}"; do - if [[ "$docker_actual" == "$expected_docker" ]]; then - found=true - break - fi - done - if [ "$found" = false ]; then - fail "$test_name" - echo "'$expected_docker' not found in expected_docker_output." - test_passed=false - exit 1 - fi - done - - # Check if any disallowed command is in the actual output - for disallowed_docker in "${disallowed_docker_output_arr[@]}"; do # Use the _arr array here - for docker_actual in "${docker_actual_output[@]}"; do - if [[ "$docker_actual" == "$disallowed_docker" ]]; then - fail "$test_name" - echo "'$disallowed_docker' found in actual output but is disallowed." - test_passed=false - exit 1 - fi - done - done + mock_docker_ps_lines=$( + echo "abc123:container1" && + echo "def456:container2" && + echo "ghi789:container3" + ) - if [ "$test_passed" = true ]; then - pass "$test_name" - else - fail "$test_name" "Commands do not match expected output." - echo "Expected:" - printf "%s\n" "${expected_docker_output_arr[@]}" - echo "Actual:" - printf "%s\n" "${docker_actual_output[@]}" - exit 1 - fi -} + disallowed_docker_output=$( + echo "stop container2" && + echo "start container2" && + echo "stop container3" && + echo "start container3" + ) -test_docker_commands() { - read -r -d '' mock_docker_ps_lines <