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