Add CRON validation function testing. (#4)

* Add CRON validation function testing.
This commit is contained in:
Aterfax 2024-04-28 16:22:48 +01:00 committed by GitHub
parent 1a42c094f0
commit f3f26be729
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 73 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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"

View File

@ -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 * * ?"