Skip to content

Commit

Permalink
fix: create log file if doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
williarin committed Aug 10, 2022
1 parent e2532d8 commit bec60a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
14 changes: 10 additions & 4 deletions src/run-cron
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ test_env=$(get_env "TEST_ENV" false)

echo "$cron_time backup >> /var/log/backup.log" > /etc/crontabs/root

if [ "$test_env" = false ]; then
echo "Launching cron service..."
tail -F /var/log/backup.log &
exec crond -f -L /var/log/cron.log
echo "Launching cron service..."

if [ ! -f "/var/log/backup.log" ]; then
touch /var/log/backup.log
fi

if [ "$test_env" = true ]; then
timeout 1 run-cron-service
else
run-cron-service
fi
6 changes: 6 additions & 0 deletions src/run-cron-service
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail

tail -F /var/log/backup.log &
exec crond -f -L /var/log/cron.log
17 changes: 13 additions & 4 deletions test/functional/run_cron.bats
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
@test "run-cron command runs without error" {
export TEST_ENV=true

run run-cron
[ "$status" -eq 124 ]
[ "${lines[0]}" = "Launching cron service..." ]
[ "${#lines[@]}" -eq 1 ]
}

@test "run-cron command creates default cron entry" {
export TEST_ENV=true

run run-cron
[ "$status" -eq 0 ]
[ "$status" -eq 124 ]
[ "$(cat /etc/crontabs/root)" = "0 0 * * * backup >> /var/log/backup.log" ]
}

Expand All @@ -11,7 +20,7 @@
export CRON_MINUTE=12

run run-cron
[ "$status" -eq 0 ]
[ "$status" -eq 124 ]
[ "$(cat /etc/crontabs/root)" = "12 0 * * * backup >> /var/log/backup.log" ]
}

Expand All @@ -20,7 +29,7 @@
export CRON_HOUR=5

run run-cron
[ "$status" -eq 0 ]
[ "$status" -eq 124 ]
[ "$(cat /etc/crontabs/root)" = "0 5 * * * backup >> /var/log/backup.log" ]
}

Expand All @@ -29,6 +38,6 @@
export CRON_TIME="*/10 9 * * sun"

run run-cron
[ "$status" -eq 0 ]
[ "$status" -eq 124 ]
[ "$(cat /etc/crontabs/root)" = "*/10 9 * * sun backup >> /var/log/backup.log" ]
}

0 comments on commit bec60a7

Please sign in to comment.