]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
add config check tests
authorBastian Dehn <hhaalo@arcor.de>
Sat, 15 Aug 2026 07:51:32 +0000 (09:51 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 15 Aug 2026 07:51:32 +0000 (09:51 +0200)
src/restore
tests/backup_tests.bats
tests/restore_tests.bats

index c1856345e274e84826e35293e0c11412de20dcc9..4f6629b3a2d5bc871828627fd37c544ab540d5a9 100755 (executable)
@@ -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
index 20d502e4eefacc712325f6dde930208d902d4b2e..2a497d760ae5e32c192bdfaafb22c7050298cb0d 100755 (executable)
@@ -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
index 72e3e33df9f5f6b18faeb8ad27c1cd55cec00514..fb8922afa4b37c5be5c8706846e050d045ce495a 100755 (executable)
@@ -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