From f3f26be72999007bd58f81c4b6df5d92b8c13307 Mon Sep 17 00:00:00 2001 From: Aterfax Date: Sun, 28 Apr 2024 16:22:48 +0100 Subject: [PATCH] Add CRON validation function testing. (#4) * Add CRON validation function testing. --- .github/workflows/docker-test.yml | 3 ++ docker/dos2unix.sh | 1 + .../helper_scripts/cron-validation-functions | 36 +++++++++++++++++++ docker/src/s6-services/cron-backup/run | 14 ++------ docker/src/tests/test-cron-validation.sh | 31 ++++++++++++++++ 5 files changed, 73 insertions(+), 12 deletions(-) create mode 100755 docker/src/helper_scripts/cron-validation-functions create mode 100755 docker/src/tests/test-cron-validation.sh diff --git a/.github/workflows/docker-test.yml b/.github/workflows/docker-test.yml index abcce48..7557a6b 100644 --- a/.github/workflows/docker-test.yml +++ b/.github/workflows/docker-test.yml @@ -18,6 +18,9 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Test CRON validation. + run: bash docker/src/tests/test-cron-validation.sh + - name: Force permissions fix. run: sudo apt-get update && sudo apt-get install dos2unix -y && cd docker && bash dos2unix.sh diff --git a/docker/dos2unix.sh b/docker/dos2unix.sh index be8d404..120648d 100755 --- a/docker/dos2unix.sh +++ b/docker/dos2unix.sh @@ -2,4 +2,5 @@ # shellcheck shell=bash find . -type f \( -name "*.sh" -o -name "up" -o -name "run*" \) -exec dos2unix {} \; find ./src/helper_scripts -type f -exec dos2unix {} \; +find ./src/tests -type f -exec dos2unix {} \; find . -type f | xargs git update-index --chmod=+x diff --git a/docker/src/helper_scripts/cron-validation-functions b/docker/src/helper_scripts/cron-validation-functions new file mode 100755 index 0000000..726ae9c --- /dev/null +++ b/docker/src/helper_scripts/cron-validation-functions @@ -0,0 +1,36 @@ +#!/bin/bash + +validate_cron_expression() { + local cron_expression="$1" + # https://stackoverflow.com/a/63729682 + # https://regex101.com/r/RtLgqG + local regex='^((((\d+,)+\d+|(\d+(\/|-|#)\d+)|\d+L?|\*(\/\d+)?|L(-\d+)?|\?|[A-Z]{3}(-[A-Z]{3})?) ?){5,7})|(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)$' + + if echo "$cron_expression" | grep -Pq "$regex"; then + return 0 # Valid cron expression + else + return 1 # Invalid cron expression + fi +} + +# Test valid cron expressions +assert_valid_cron() { + local cron_expr="$1" + if validate_cron_expression "$cron_expr"; then + echo "PASS: '$cron_expr' is a valid cron expression." + else + echo "FAIL: '$cron_expr' is expected to be a valid cron expression but is not." + return 1 # Set non-zero exit status for failure + fi +} + +# Test invalid cron expressions +assert_invalid_cron() { + local cron_expr="$1" + if ! validate_cron_expression "$cron_expr"; then + echo "PASS: '$cron_expr' is an invalid cron expression." + else + echo "FAIL: '$cron_expr' is expected to be an invalid cron expression but is not." + return 1 # Set non-zero exit status for failure + fi +} \ No newline at end of file diff --git a/docker/src/s6-services/cron-backup/run b/docker/src/s6-services/cron-backup/run index c1a0935..3c7f895 100755 --- a/docker/src/s6-services/cron-backup/run +++ b/docker/src/s6-services/cron-backup/run @@ -20,18 +20,8 @@ handle_error() { } trap handle_error ERR -validate_cron_expression() { - local cron_expression="$1" - # https://stackoverflow.com/a/63729682 - # https://regex101.com/r/oGYmrm/1 - local regex='^((((\d+,)+\d+|(\d+(\/|-|#)\d+)|\d+L?|\*(\/\d+)?|L(-\d+)?|\?|[A-Z]{3}(-[A-Z]{3})?) ?){5,7})|(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|µs|ms|s|m|h))+)$' - - if echo "$cron_expression" | grep -Pq "$regex"; then - return 0 # Valid cron expression - else - return 1 # Invalid cron expression - fi -} +# Load the Cron validation functions. +source /usr/local/sbin/cron-validation-functions if ! validate_cron_expression "$CRON_SCHEDULE"; then echo -e "Invalid cron expression: $CRON_SCHEDULE \n" diff --git a/docker/src/tests/test-cron-validation.sh b/docker/src/tests/test-cron-validation.sh new file mode 100755 index 0000000..dcb8757 --- /dev/null +++ b/docker/src/tests/test-cron-validation.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Load the Cron validation functions. +script_dir=$(dirname "${BASH_SOURCE[0]}") +source $script_dir/../helper_scripts/cron-validation-functions + +# Test valid cron expressions +assert_valid_cron "@annually" +assert_valid_cron "@yearly" +assert_valid_cron "@monthly" +assert_valid_cron "@weekly" +assert_valid_cron "@daily" +assert_valid_cron "@hourly" +assert_valid_cron "@reboot" + +assert_valid_cron "0 */4 * * *" +assert_valid_cron "*/5 * * * *" +assert_valid_cron "* */5 * * *" +assert_valid_cron "*/5 */5 * * *" +assert_valid_cron "0 0 12 * * ?" +assert_valid_cron "0 15 10 ? * *" +assert_valid_cron "0 15 10 ? * MON-FRI" +assert_valid_cron "0 15 10 ? * 6L" +assert_valid_cron "0 15 10 ? * 6L 2022-2024" +assert_valid_cron "0 15 10 ? * 6#3" +assert_valid_cron "0 11 11 11 11 ?" + +# Test invalid cron expressions +assert_invalid_cron "invalid expression" +assert_invalid_cron "@invalid" +assert_invalid_cron "0\15 0 * * ?" \ No newline at end of file