From 752c32571ba81af51fd9998307e35430dd643cb2 Mon Sep 17 00:00:00 2001 From: Maximilian Hafer Date: Wed, 11 Sep 2024 15:17:39 +0200 Subject: [PATCH] add integration test and fix wron crontab name for updated fsnotify monitor --- integration/reload.bats | 43 +++++++++++++++++++++++++++++++++++++++++ integration/test.bats | 0 main.go | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) mode change 100644 => 100755 integration/reload.bats mode change 100644 => 100755 integration/test.bats diff --git a/integration/reload.bats b/integration/reload.bats old mode 100644 new mode 100755 index 903f9b8..e8219d7 --- a/integration/reload.bats +++ b/integration/reload.bats @@ -42,3 +42,46 @@ grep_test_file() { kill -s TERM "$PID" wait } + +@test "if inotify is enabled it reloads on file change when receiving a WRITE event" { + echo '* * * * * * * echo a > "$TEST_FILE"' > "$CRONTAB_FILE" + + "${BATS_TEST_DIRNAME}/../supercronic" -inotify "$CRONTAB_FILE" 3>&- & + PID="$!" + + wait_for grep_test_file a + + echo '* * * * * * * echo b > "$TEST_FILE"' > "$CRONTAB_FILE" + + wait_for grep_test_file b + + kill -s TERM "$PID" + wait +} + +@test "if inotify is enabled it handles kubernetes like atomic writes using updated symlinks and folder deletion" { + CRONTAB_FILE_NAME="$(basename $(mktemp --dry-run --tmpdir))" + + WORK_DIR="$(mktemp -d)" + CRONTAB_PRE_DIR="$(mktemp -d)" + CRONTAB_POST_DIR="$(mktemp -d)" + + echo '* * * * * * * echo a > "$TEST_FILE"' > "$CRONTAB_PRE_DIR"/"$CRONTAB_FILE_NAME" + echo '* * * * * * * echo b > "$TEST_FILE"' > "$CRONTAB_POST_DIR"/"$CRONTAB_FILE_NAME" + + ln -s "$CRONTAB_PRE_DIR"/"$CRONTAB_FILE_NAME" "$WORK_DIR"/"$CRONTAB_FILE_NAME" + + "${BATS_TEST_DIRNAME}/../supercronic" -inotify -debug "$WORK_DIR"/"$CRONTAB_FILE_NAME" 3>&- & + PID="$!" + + wait_for grep_test_file a + + ln -sf "$CRONTAB_POST_DIR"/"$CRONTAB_FILE_NAME" "$WORK_DIR"/"$CRONTAB_FILE_NAME" + + rm -r "$CRONTAB_PRE_DIR" + + wait_for grep_test_file b + + kill -s TERM "$PID" + wait +} diff --git a/integration/test.bats b/integration/test.bats old mode 100644 new mode 100755 diff --git a/main.go b/main.go index 7b224d3..2b06036 100644 --- a/main.go +++ b/main.go @@ -190,7 +190,7 @@ func main() { // workaround for k8s configmap and secret mounts case event.Op & fsnotify.Remove: logrus.Debug("watched file changed") - if err := watcher.Add(event.Name); err != nil { + if err := watcher.Add(crontabFileName); err != nil { logrus.Fatal(err) return }