From: Bastian Dehn Date: Sun, 16 Aug 2026 13:23:21 +0000 (+0200) Subject: add test start backupmount X-Git-Tag: v1.0.7^2~1^2~27 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=295315c5caf3e1bcdd3e29af263f2434b71909e1;p=simple-backup.git add test start backupmount --- diff --git a/Makefile b/Makefile index 276c8aa..7ee255f 100644 --- 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 \ diff --git a/src/backupmount b/src/backupmount index f547491..9b35334 100755 --- a/src/backupmount +++ b/src/backupmount @@ -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 index 0000000..16f11c7 --- /dev/null +++ b/tests/backupmount_tests.bats @@ -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