Skip to content

Commit

Permalink
Fix unit test directories
Browse files Browse the repository at this point in the history
  • Loading branch information
Minituff committed Oct 24, 2023
1 parent c5ff6b6 commit cb9907d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
4 changes: 2 additions & 2 deletions pkg/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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------ #

Expand Down
85 changes: 73 additions & 12 deletions tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit cb9907d

Please sign in to comment.