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 cd0d9ab commit a023bde
Showing 1 changed file with 107 additions and 104 deletions.
211 changes: 107 additions & 104 deletions tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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 <<EOM
abc123:container1
def456:container2
ghi789:container3
EOM

read -r -d '' disallowed_docker_output <<EOM
stop container2
start container2
stop container3
start container3
EOM

read -r -d '' expected_docker_output <<EOM
ps --no-trunc --format={{.ID}}:{{.Names}}
inspect --format {{json .Config.Labels}} abc123
EOM
expected_docker_output=$(
echo "ps --no-trunc --format={{.ID}}:{{.Names}}" &&
echo "inspect --format {{json .Config.Labels}} abc123" &&
echo "stop container1" &&
echo "start container1"
)

test_docker \
--name "Test Docker Commands on default settings" \
Expand All @@ -420,6 +422,7 @@ EOM
--disallow "$disallowed_docker_output"
}


# Run the tests
test_docker_commands
# test_docker_ps
Expand Down

0 comments on commit a023bde

Please sign in to comment.