From cb9907df2cb6e3ec079274daa06e48b11046529d Mon Sep 17 00:00:00 2001 From: James Tufarelli Date: Tue, 24 Oct 2023 10:04:17 -0700 Subject: [PATCH] Fix unit test directories --- pkg/env.sh | 4 +-- tests/tests.sh | 85 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/pkg/env.sh b/pkg/env.sh index a0248537..3ab0bb89 100755 --- a/pkg/env.sh +++ b/pkg/env.sh @@ -61,8 +61,8 @@ DEFAULT_LOG_RSYNC_COMMANDS="false" DEFAULT_SOURCE_LOCATION="/app/source" DEFAULT_DEST_LOCATION=/app/destination # Test directories -DEFAULT_TEST_SOURCE_LOCATION="tests/source" -DEFAULT_TEST_DEST_LOCATION="tests/destination" +DEFAULT_TEST_SOURCE_LOCATION="tests/src" +DEFAULT_TEST_DEST_LOCATION="tests/dest" # ------ Default Empty Values------ # diff --git a/tests/tests.sh b/tests/tests.sh index 56ca423f..65f18993 100755 --- a/tests/tests.sh +++ b/tests/tests.sh @@ -41,13 +41,14 @@ clear_files() { >$DOCKER_COMMANDS_FILE } -# Test function +# ---- Actual Tests ---- + test_docker_ps() { clear_files export BACKUP_ON_START="true" - - mkdir -p tests/source/container1 && touch tests/source/container1/test.txt - mkdir -p tests/destination + pwd + mkdir -p tests/src/container1 && touch tests/src/container1/test.txt + mkdir -p tests/dest declare -a mock_docker_ps_lines=( "abc123:container1" @@ -98,9 +99,9 @@ test_rsync() { clear_files export BACKUP_ON_START="true" - mkdir -p tests/source/container1 && touch tests/source/container1/test.txt - mkdir -p tests/source/container2 && touch tests/source/container1/test.txt - mkdir -p tests/destination + mkdir -p tests/src/container1 && touch tests/src/container1/test.txt + mkdir -p tests/src/container2 && touch tests/src/container1/test.txt + mkdir -p tests/dest declare -a mock_docker_ps_lines=( "abc123:container1" @@ -113,8 +114,8 @@ test_rsync() { source pkg/entry.sh declare -a expected_rsync_output=( - "-ahq tests/source/container1/ tests/destination/container1/" - "-ahq tests/source/container2/ tests/destination/container2/" + "-ahq tests/src/container1/ tests/dest/container1/" + "-ahq tests/src/container2/ tests/dest/container2/" ) test_passed=true # Initialize a flag to indicate test status @@ -149,11 +150,71 @@ test_rsync() { fi } -# Run the test + + +test_skip_containers() { + clear_files + export BACKUP_ON_START="true" + SKIP_CONTAINERS=container1,container-name2,container-name3 + + + mkdir -p tests/source/container1 && touch tests/source/container1/test.txt + mkdir -p tests/destination + + declare -a mock_docker_ps_lines=( + "abc123:container1" + "def456:container2" + "ghi789:container3" + ) + # Set what the next docker ps command should return + MOCK_DOCKER_PS_OUTPUT=$(printf "%s\n" "${mock_docker_ps_lines[@]}") + + source pkg/entry.sh + + declare -a expected_docker_output=( + "ps --no-trunc --format={{.ID}}:{{.Names}}" + "inspect --format {{json .Config.Labels}} abc123" + "inspect --format {{json .Config.Labels}} def456" + "inspect --format {{json .Config.Labels}} ghi789" + ) + + test_passed=true # Initialize a flag to indicate test status + # Read the lines from the file into an array + + 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[@]}"; do + if ! printf '%s\n' "${docker_actual_output[@]}" | grep -q -F "$expected_docker"; then + echo "FAIL: DOCKER '$expected_docker' not found in actual output." + test_passed=false + fi + done + + if [ "$test_passed" = true ]; then + echo "PASS: All expected commands were found." + else + echo "FAIL: Commands do not match expected output." + echo "Expected:" + print_array "${expected_output[@]}" + echo "Actual:" + print_array "${docker_actual_output[@]}" + exit 1 + fi + +} + +# Run the tests test_docker_ps test_rsync +# test_skip_containers + +# Cleanup rm "$DOCKER_COMMANDS_FILE" rm "$RSYNC_COMMANDS_RFILE" -rm -rf tests/source -rm -rf tests/destination +rm -rf tests/src +rm -rf tests/dest + +source pkg/logger.sh + delete_report_file