Skip to content

Commit

Permalink
Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Minituff committed Oct 24, 2023
1 parent a023bde commit c3e6895
Showing 1 changed file with 121 additions and 124 deletions.
245 changes: 121 additions & 124 deletions tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,28 @@ clear_files() {
>$DOCKER_COMMANDS_FILE
}

# Color echo
teardown(){
rm "$DOCKER_COMMANDS_FILE"
rm "$RSYNC_COMMANDS_RFILE"
rm -rf tests/src
rm -rf tests/dest

source pkg/logger.sh

delete_report_file
}

cleanup_on_success() {
clear_files
rm -rf tests/src
rm -rf tests/dest
}

cleanup_on_fail() {
cleanup_on_success
exit 1
}

cecho() {
RED="\033[0;31m"
GREEN="\033[0;32m" # <-- [0 means not bold
Expand Down Expand Up @@ -79,6 +100,10 @@ test_docker() {
# Parse named parameters
while [[ "$#" -gt 0 ]]; do
case $1 in
--name)
test_name="$2"
shift
;;
--mock)
mock_docker_ps_lines="$2"
shift
Expand All @@ -91,13 +116,9 @@ test_docker() {
expected_docker_output="$2"
shift
;;
--name)
test_name="$2"
shift
;;
*)
echo "Unknown parameter passed: $1"
exit 1
cleanup_on_fail
;;
esac
shift
Expand Down Expand Up @@ -129,7 +150,7 @@ test_docker() {
fail "$test_name"
echo "'$expected_docker' not found in expected_docker_output."
test_passed=false
exit 1
cleanup_on_fail
fi
done

Expand All @@ -140,7 +161,7 @@ test_docker() {
fail "$test_name"
echo "'$disallowed_docker' found in actual output but is disallowed."
test_passed=false
exit 1
cleanup_on_fail
fi
done
done
Expand All @@ -153,146 +174,95 @@ test_docker() {
printf "%s\n" "${expected_docker_output_arr[@]}"
echo "Actual:"
printf "%s\n" "${docker_actual_output[@]}"
exit 1
cleanup_on_fail
fi
}

# ---- Actual Tests ----
test_rsync() {
local test_name
local mock_docker_ps_lines
local expected_rsync_output
local disallowed_rsync_output

test_docker_ps() {
clear_files
export BACKUP_ON_START="true"
mkdir -p tests/src/container1 && touch tests/src/container1/test.txt
mkdir -p tests/dest
# Parse named parameters
while [[ "$#" -gt 0 ]]; do
case $1 in
--name)
test_name="$2"
shift
;;
--mock)
mock_docker_ps_lines="$2"
shift
;;
--expect)
expected_rsync_output="$2"
shift
;;
--disallow)
disallowed_rsync_output="$2"
shift
;;
*)
echo "Unknown parameter passed: $1"
cleanup_on_fail
;;
esac
shift
done

IFS=$'\n' read -rd '' -a mock_docker_ps_lines_arr <<<"$mock_docker_ps_lines"
IFS=$'\n' read -rd '' -a expected_rsync_output_arr <<<"$expected_rsync_output"
IFS=$'\n' read -rd '' -a disallowed_rsync_output_arr <<<"$disallowed_rsync_output"

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[@]}")
MOCK_DOCKER_PS_OUTPUT=$(printf "%s\n" "${mock_docker_ps_lines_arr[@]}")

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"
"stop container1"
"start container1"
)

declare -a disallowed_docker_output=(
"stop container2"
"start container2"
"stop container3"
"start container3"
)

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"
mapfile -t rsync_actual_output <"$RSYNC_COMMANDS_RFILE"

# Check if each expected command is in the actual output
for expected_docker in "${expected_docker_output[@]}"; do
for expected_rsync in "${expected_rsync_output_arr[@]}"; do
found=false
for docker_actual in "${docker_actual_output[@]}"; do
if [[ "$docker_actual" == "$expected_docker" ]]; then
for actual_rsync in "${rsync_actual_output[@]}"; do
if [[ "$actual_rsync" == "$expected_rsync" ]]; then
found=true
break
fi
done
if [ "$found" = false ]; then
fail "${FUNCNAME[0]}"
echo "'$expected_docker' not found in expected_docker_output."
fail $test_name
echo "RSYNC '$expected_rsync' not found in actual 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[@]}"; do
for docker_actual in "${docker_actual_output[@]}"; do
if [[ "$docker_actual" == "$disallowed_docker" ]]; then
fail "${FUNCNAME[0]}"
echo "'$disallowed_docker' found in actual output but is disallowed."
for disallowed_rsync in "${disallowed_rsync_output_arr[@]}"; do
for actual_rsync in "${rsync_actual_output[@]}"; do
if [[ "$actual_rsync" == "$disallowed_rsync" ]]; then
fail $test_name
echo "RSYNC '$disallowed_rsync' found in actual output but is disallowed."
test_passed=false
exit 1
fi
done
done

if [ "$test_passed" = true ]; then
pass ${FUNCNAME[0]}
pass $test_name
else
fail ${FUNCNAME[0]}
fail "$test_name"
echo "Expected:"
print_array "${expected_docker_output[@]}"
printf "%s\n" "${expected_rsync_output_arr[@]}"
echo "Actual:"
print_array "${docker_actual_output[@]}"
exit 1
printf "%s\n" "${rsync_actual_output[@]}"
fi

}

test_rsync() {
clear_files
export BACKUP_ON_START="true"

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"
"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_rsync_output=(
"-ahq tests/src/container1/ tests/dest/container1/"
"-ahq tests/src/container2/ tests/dest/container2/"
)

test_passed=true # Initialize a flag to indicate test status
# Read the lines from the file into an array

# Check if each expected command is in the actual output

mapfile -t rsync_actual_output <"$RSYNC_COMMANDS_RFILE"

for expected_rsync in "${expected_rsync_output[@]}"; do
found=false
for actual_rsync in "${rsync_actual_output[@]}"; do
if [[ "$actual_rsync" == "$expected_rsync" ]]; then
found=true
break
fi
done
if [ "$found" = false ]; then
echo "${FUNCNAME[0]} FAIL: RSYNC '$expected_rsync' not found in actual output."
test_passed=false
fi
done

if [ "$test_passed" = true ]; then
pass ${FUNCNAME[0]}
else
fail ${FUNCNAME[0]}
echo "Expected:"
print_array "${expected_rsync_output[@]}"
echo "Actual:"
print_array "${rsync_actual_output[@]}"
fi

}
# ---- Actual Tests ----

test_skip_containers() {
clear_files
Expand Down Expand Up @@ -337,7 +307,7 @@ test_skip_containers() {
fail "${FUNCNAME[0]}"
echo "'$expected_docker' not found in expected_docker_output."
test_passed=false
exit 1
cleanup_on_fail
fi
done

Expand Down Expand Up @@ -384,7 +354,7 @@ test_skip_containers() {
print_array "${expected_output[@]}"
echo "Actual:"
print_array "${docker_actual_output[@]}"
exit 1
cleanup_on_fail
fi

}
Expand Down Expand Up @@ -420,20 +390,47 @@ test_docker_commands() {
--mock "$mock_docker_ps_lines" \
--expect "$expected_docker_output" \
--disallow "$disallowed_docker_output"

cleanup_on_success
}

test_rsync_commands() {
clear_files
export BACKUP_ON_START="true"

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

mock_docker_ps_lines=$(
echo "abc123:container1" &&
echo "def456:container2" &&
echo "ghi789:container3"
)

disallowed_rsync_output=$(
echo "stop container3" &&
echo "start container3"
)

expected_rsync_output=$(
echo "-ahq tests/src/container1/ tests/dest/container1/" &&
echo "-ahq tests/src/container2/ tests/dest/container2/"
)

test_rsync \
--name "Test Rsync on default settings" \
--mock "$mock_docker_ps_lines" \
--expect "$expected_rsync_output" \
--disallow "$disallowed_rsync_output"

cleanup_on_success
}

# Run the tests
test_rsync_commands
test_docker_commands
# test_docker_ps
# test_rsync
# test_skip_containers
# Cleanup
rm "$DOCKER_COMMANDS_FILE"
rm "$RSYNC_COMMANDS_RFILE"
rm -rf tests/src
rm -rf tests/dest

source pkg/logger.sh

delete_report_file
# Cleanup
teardown

0 comments on commit c3e6895

Please sign in to comment.