Skip to content

Commit

Permalink
Better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Minituff committed Oct 23, 2023
1 parent c03fe11 commit 2f81f2a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/backup.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

if [ "$TEST_MODE" == "true" ]; then
echo "Running in test mode"
echo "--- Running logger.sh in test mode ---"
source "`dirname $0`"/logger.sh # Use the logger script
else
source /app/logger.sh # Use the logger script
Expand Down
38 changes: 22 additions & 16 deletions pkg/entry.sh
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
#!/bin/bash

if [ "$TEST_MODE" == "true" ]; then
echo "Running in test mode"
source "$(dirname $0)"/logger.sh # Use the logger script
source "$(dirname $0)"/utils.sh
source "$(dirname $0)"/env.sh
echo "--- Running entry.sh in TEST mode ---"
source ../pkg/logger.sh # Use the logger script
source ../pkg/utils.sh
source ../pkg/env.sh
else
source /app/logger.sh # Use the logger script
source /app/utils.sh
source app/env.sh
fi

# Echo the CRON schedule for logging/debugging
logThis "Installing CRON schedule: $CRON_SCHEDULE in TZ: $TZ" "DEBUG" "init"
if [ "$TEST_MODE" == "false" ]; then
echo "Skipping CRON schedule installation in test mode"

# Dump the current cron jobs to a temporary file
crontab -l >tempcron
# Echo the CRON schedule for logging/debugging
logThis "Installing CRON schedule: $CRON_SCHEDULE in TZ: $TZ" "DEBUG" "init"

# Remove the existing cron job for your backup script from the file
sed -i '/\/app\/backup.sh/d' tempcron
# Dump the current cron jobs to a temporary file
crontab -l >tempcron

# Add the new cron job to the file
echo "$CRON_SCHEDULE bash /app/backup.sh" >>tempcron
# Remove the existing cron job for your backup script from the file
sed -i '/\/app\/backup.sh/d' tempcron

# Install the new cron jobs and remove the tempcron file
crontab tempcron && rm tempcron
# Add the new cron job to the file
echo "$CRON_SCHEDULE bash /app/backup.sh" >>tempcron

# Install the new cron jobs and remove the tempcron file
crontab tempcron && rm tempcron
fi

# Verify the source and destination locations
verify_source_location $SOURCE_LOCATION
Expand All @@ -36,10 +40,12 @@ if [ "$BACKUP_ON_START" = "true" ]; then
fi

if [ "$EXIT_AFTER_INIT" = "true" ]; then
echo "Exiting since EXIT_AFTER_INITis true" "INFO" "init"
echo "Exiting since EXIT_AFTER_INIT is true" "INFO" "init"
exit 0
fi

logThis "Initialization complete. Awaiting CRON schedule: $CRON_SCHEDULE" "INFO" "init"

/usr/sbin/crond -f -l 8 # Start cron and keep container running
if [ "$TEST_MODE" == "false" ]; then
/usr/sbin/crond -f -l 8 # Start cron and keep container running
fi
16 changes: 12 additions & 4 deletions pkg/env.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#!/bin/bash

if [ -z "$TEST_MODE" ]; then
TEST_MODE="false"
fi
export TEST_MODE

if [ "$TEST_MODE" == "true" ]; then
echo "Running in test mode"
source "$(dirname $0)"/logger.sh # Use the logger script
source "$(dirname $0)"/utils.sh # Use the logger script
echo "--- Running env.sh in test mode ---"
source ../pkg/logger.sh # Use the logger script
source ../pkg/utils.sh

NAUTICAL_VERSION=Test
TARGETPLATFORM=TestPlatform
else
source /app/logger.sh # Use the logger script
source /app/utils.sh # Use the logger script
source /app/utils.sh
fi

create_new_report_file
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if [ "$TEST_MODE" == "true" ]; then
echo "Running in test mode"
source "$(dirname $0)"/logger.sh # Use the logger script
echo "--- Running utils.sh in test mode ---"
source ../pkg/logger.sh # Use the logger script
else
source /app/logger.sh # Use the logger script
fi
Expand Down
4 changes: 3 additions & 1 deletion tests/tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ DOCKER_COMMANDS_FILE=$(mktemp /tmp/docker_commands.XXXXXX)
export DOCKER_COMMANDS_FILE

export TEST_MODE="true"
export LOG_LEVEL="ERROR"

# Mock function for docker
docker() {
Expand Down Expand Up @@ -46,7 +47,8 @@ test_docker_ps() {
# Set what the next docker ps command should return
MOCK_DOCKER_PS_OUTPUT=$(printf "%s\n" "${mock_docker_ps_lines[@]}")

source ./pkg/entry.sh
source ../pkg/entry.sh
# source ../pkg/env.sh
../pkg/backup.sh

declare -a expected_output=(
Expand Down

0 comments on commit 2f81f2a

Please sign in to comment.