From: Bastian Dehn Date: Sat, 15 Aug 2026 07:51:32 +0000 (+0200) Subject: add config check tests X-Git-Tag: v1.0.7^2~3^2~10 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=f86ed1fa938facd3a9c7c555da0de7b1041f9502;p=simple-backup.git add config check tests --- diff --git a/src/restore b/src/restore index c185634..4f6629b 100755 --- a/src/restore +++ b/src/restore @@ -25,10 +25,59 @@ exists_needed_commands() || return $? } +exists_config_file() +{ + local config_file="$1" + + [ -f "$config_file" ] \ + || errlog "file $config_file not found" \ + || return $? +} + +exists_backup_path() +{ + local config="$1" + local backup_path= + backup_path=$(echo "$config" | jq --raw-output '.backup_path') + [ "$backup_path" != "null" ] \ + || errlog "$CONFIG_FILE missing backup_path" \ + || return $? + echo "ℹ️ INFO: backup path $backup_path" +} + +exists_backup_name() +{ + local config="$1" + local backup_name= + backup_name=$(echo "$config" | jq --raw-output '.backup_name') + [ "$backup_name" != "null" ] \ + || errlog "$CONFIG_FILE missing backup_name" \ + || return $? + echo "ℹ️ INFO: backup name $backup_name" +} + +exists_machines() +{ + local config="$1" + echo "$config" \ + | jq --exit-status \ + --raw-output '.machines | keys[]' > /dev/null 2>&1 +} + main() { + local CONFIG_FILE=${CONFIG_FILE:="config.yaml"} exists_needed_commands || return $? + exists_config_file "$CONFIG_FILE" || return $? + config=$(yq '.' "$CONFIG_FILE") \ + || errlog "failed to load $CONFIG_FILE" \ + || return $? echo "⭐ START: restore" + exists_backup_path "$config" || return $? + exists_backup_name "$config" || return $? + exists_machines "$config" \ + || errlog "$CONFIG_FILE missing machines" \ + || return $? } [ "$TESTMODE" == "ON" ] && return 0 diff --git a/tests/backup_tests.bats b/tests/backup_tests.bats index 20d502e..2a497d7 100755 --- a/tests/backup_tests.bats +++ b/tests/backup_tests.bats @@ -91,7 +91,7 @@ teardown() { assert_line --index 0 "❌ ERROR: command ssh not found" } -# bats test_tags=backup:start,backup:savelog +# bats test_tags=backup:start,backup:cmd @test "failure savelog command not found" { command() { [ "$2" != "savelog" ] || return 1 diff --git a/tests/restore_tests.bats b/tests/restore_tests.bats index 72e3e33..fb8922a 100755 --- a/tests/restore_tests.bats +++ b/tests/restore_tests.bats @@ -83,4 +83,74 @@ teardown() { assert_failure assert_line --index 0 "❌ ERROR: command ssh not found" +} + +# bats test_tags=backup:start,backup:config +@test "failure config.yaml not found" { + exists_config_file() { + errlog "file config.yaml not found" + return 1 + } + + run main + + assert_failure + assert_line --index 0 "❌ ERROR: file config.yaml not found" +} + +# bats test_tags=backup:start,backup:config +@test "failure load config.yaml" { + yq() { + return 1 + } + + run main + + assert_failure + assert_line --index 0 "❌ ERROR: failed to load src/config-example.yaml" +} + +# bats test_tags=backup:start,backup:config +@test "failure no backup path in config" { + yq() { + echo '{}' + } + + run main + + assert_failure + assert_line --index 0 "⭐ START: restore" + assert_line --index 1 "❌ ERROR: src/config-example.yaml missing backup_path" +} + +# bats test_tags=backup:start,backup:config +@test "failure no backup name in config" { + yq() { + echo '{"backup_path": "path"}' + } + + run main + + assert_failure + assert_line --index 0 "⭐ START: restore" + assert_line --index 1 "ℹ️ INFO: backup path path" + assert_line --index 2 "❌ ERROR: src/config-example.yaml missing backup_name" +} + +# bats test_tags=backup:start,backup:config +@test "failure no machines in config" { + yq() { + echo '{ + "backup_path": "path", + "backup_name": "backup" + }' + } + + run main + + assert_failure + assert_line --index 0 "⭐ START: restore" + assert_line --index 1 "ℹ️ INFO: backup path path" + assert_line --index 2 "ℹ️ INFO: backup name backup" + assert_line --index 3 "❌ ERROR: src/config-example.yaml missing machines" } \ No newline at end of file