]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
add test start backupmount
authorBastian Dehn <hhaalo@arcor.de>
Sun, 16 Aug 2026 13:23:21 +0000 (15:23 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 16 Aug 2026 13:23:21 +0000 (15:23 +0200)
Makefile
src/backupmount
tests/backupmount_tests.bats [new file with mode: 0755]

index 276c8aa870d43bf05004ea633c4fb781122c4a9d..7ee255f8a8f29e287df2ec8d44c572bdf197b7c0 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,6 +15,7 @@ all: test package
 test:
        ./tests/backup_tests.bats
        ./tests/restore_tests.bats
+       ./tests/backupmount_tests.bats
 
 build: $(BINDIR)/backup \
        $(BINDIR)/backupmount \
index f547491674c09aba8a7e0a7d5a958f776369d1f7..9b35334c4e578ddfa3d993c4d6274cfe72de3256 100755 (executable)
@@ -1,167 +1,9 @@
 #!/bin/bash
 
-errlog()
-{
-       local lastexit=$?
-       local msg="$1"
-
-       echo "❌ ERROR: $msg"
-       echo "🛑 EXIT $lastexit"
-       return $lastexit
-}
-
-infolog()
-{
-       local lastexit=$?
-       local msg="$1"
-
-       echo "â„šī¸  INFO: $msg"
-       return $lastexit
-}
-
-partition_mount()
-{
-       local media="$1"
-       local partition=
-       local backup_path=
-       partition=$(yq --raw-output ".media.\"$media\".partition" config.yaml)
-       [ "$partition" != "null" ] \
-               || errlog "config.yaml partition for $media missing" \
-               || return $?
-       [ -b "$partition" ] \
-               || errlog "partition $partition not found" \
-               || return $?
-       backup_path=$(yq --raw-output ".backup_path" config.yaml)
-       [ "$backup_path" != "null" ] \
-               || errlog "config.yaml backup path missing" \
-               || return $?
-
-       infolog "create backup path $backup_path"
-       mkdir --parents "$backup_path" \
-               || errlog "could not create directory $backup_path" \
-               || return $?
-       infolog "mount partition $partition to $backup_path"
-       mount "$partition" "$backup_path" \
-               || errlog "cloud not mount $partition to $backup_path" \
-               || return $?
-}
-
-device_mount()
-{
-       local media="$1"
-       local device="$2"
-       local key=
-       local volume=
-
-       [ -b "$device" ] \
-               || infolog "block device $device for $media not found" \
-               || return $?
-       infolog "media $media with device $device found"
-       key=$(yq --raw-output ".media.\"$media\".key" config.yaml)
-       [ "$key" != "null" ] \
-               || errlog "config.yaml key for $media missing" \
-               || return $?
-       [ -f "$key" ] \
-               || errlog "key $key not exists" \
-               || return $?
-       infolog "use key $key"
-       volume=$(yq --raw-output ".backup_volume" config.yaml)
-       [ "$volume" != "null" ] \
-               || errlog "config.yaml volume for $media missing" \
-               || return $?
-
-       infolog "volume open $volume"
-       cryptsetup open --key-file "$key" \
-               "$device" \
-               "$volume" \
-               || errlog "could not open $device with $key" \
-               || return $?
-
-       partition_mount "$media" || return $?
-}
-
-mount_backup()
-{
-       local medias=
-       medias=$(yq --raw-output '.media | keys[]' config.yaml)
-
-       [ -n "$medias" ] && [ "$medias" != "null" ] \
-               || errlog "config.yaml missing media section" \
-               || return $?
-
-       local media=
-       local device=
-       for media in $medias; do
-               device=$(yq --raw-output ".media.\"$media\".device" config.yaml)
-               [ "$device" != "null" ] \
-                       || errlog "missing path for $media" \
-                       || return $?
-               device_mount "$media" "$device" && return 0
-       done
-
-       errlog "no device was mounted"
-}
-
-umount_backup()
-{
-       local backup_path=
-       local volume=
-
-       backup_path=$(yq --raw-output ".backup_path" config.yaml)
-       [ "$backup_path" != "null" ] \
-               || errlog "config.yaml backup path missing" \
-               || return $?
-
-       infolog "umount backup path $backup_path"
-       umount "$backup_path" \
-               || errlog "could not unmount $backup_path" \
-               || return $?
-
-       infolog "remove backup path directory $backup_path"
-       rmdir "$backup_path" \
-               || errlog "clound not remote $backup_path" \
-               || return $?
-
-       volume=$(yq --raw-output ".backup_volume" config.yaml)
-       [ "$volume" != "null" ] \
-               || errlog "config.yaml volume for $media missing" \
-               || return $?
-
-       infolog "close volume $volume"
-       cryptsetup close "$volume" \
-               || errlog "could not close $volume" \
-               || return $?
-}
-
 main()
 {
-       local cmd="$1"
-
        echo "⭐ START: backupmount"
-
-       command -v yq > /dev/null \
-               || errlog "command yq not found" \
-               || return $?
-       command -v cryptsetup > /dev/null \
-               || errlog "command cryptsetup not found" \
-               || return $?
-
-       [ -n "$cmd" ] \
-               || errlog "missing command" \
-               || return $?
-       [ -f "config.yaml" ] \
-               || errlog "file config.yaml not exists" \
-               || return $?
-
-       if [ "$cmd" == "mount" ]; then
-               mount_backup || return $?
-       elif [ "$cmd" == "umount" ]; then
-               umount_backup || return $?
-       else
-               errlog "unknown command" || return $?
-       fi
-
-       echo "✅ SUCCESS: backupmount"
 }
 
-main "$*"
+[ "$TESTMODE" == "ON" ] && return 0
+main "$@"
diff --git a/tests/backupmount_tests.bats b/tests/backupmount_tests.bats
new file mode 100755 (executable)
index 0000000..16f11c7
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/bats
+
+setup() {
+       bats_load_library 'bats-support'
+       bats_load_library 'bats-assert'
+       export TESTMODE="ON"
+       export CONFIG_FILE="src/config-example.yaml"
+       source src/backupmount
+}
+
+teardown() {
+       rm --recursive --force "build/test"
+       unset TESTMODE
+       unset CONFIG_FILE
+}
+
+@test "success start backupmount" {
+       run main
+
+       assert_line --index 0 "⭐ START: backupmount"
+}
\ No newline at end of file