]> gitweb.hhaalo.de Git - simple-backup.git/commitdiff
add write log entry tests
authorBastian Dehn <hhaalo@arcor.de>
Sun, 9 Aug 2026 09:02:34 +0000 (11:02 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 9 Aug 2026 09:04:35 +0000 (11:04 +0200)
src/backup
src/config-example.yaml
tests/backup_tests.bats

index 8fbac70c0642a419d20678d716e2855b3e7d3d70..c69d08669ee3dc9b22fa852b94d602a2a9708b67 100755 (executable)
@@ -9,6 +9,41 @@ errlog()
        return $lastexit
 }
 
+get_date_time()
+{
+       date --iso-8601=secondes
+}
+
+get_backup_type()
+{
+       local config="$1"
+       local backup_path=
+       backup_path=$(echo "$config" | jq --raw-output '.backup_path')
+
+       [ -f "$backup_path/day" ] && echo "day" && return 0
+       [ -f "$backup_path/week" ] && echo "week" && return 0
+       [ -f "$backup_path/month" ] && echo "month" && return 0
+
+       echo "none"
+}
+
+log()
+{
+       local config="$1"
+       local msg="$2"
+       local logfile=
+       local datetime=
+       logfile=$(echo "$config" | jq --raw-output '.backup_logfile')
+       [ -n "$logfile" ] || return 0
+       [ "$logfile" != "null" ] || return 0
+       datetime=$(get_date_time)
+       type=$(get_backup_type "$config")
+
+       logmsg="$datetime [$type] $msg"
+       echo "ℹ️ INFO: $logmsg"
+       echo "$logmsg" >> "$logfile"
+}
+
 exists_needed_commands()
 {
        command -v yq > /dev/null \
index 70c8bbadeb8704e8e9a77006fe769bde0de57a79..12a1640a6167f84f2fb36fca1435dccb77e507ae 100644 (file)
@@ -1,6 +1,7 @@
 backup_path: build
 backup_name: backup
 backup_count: 15
+backup_logfile: build/backup.log
 
 machines:
   testpc1:
index 7a97e543c7af59eac3e87bf0721127aa1df619ef..1d06ab24ef300b52db2a637bf18c5129dd6e0a3d 100755 (executable)
@@ -847,4 +847,49 @@ teardown() {
        [ -d build/2000-01-01_19_backup ]
        rm --recursive build/2000-01-01_19_backup
        rmdir build/2000-01-01_*
-}
\ No newline at end of file
+}
+
+# bats test_tags=backup:log
+@test "success no config" {
+       mkdir --parents build
+       run log "$config"
+
+       assert_success
+       assert_output ""
+}
+
+# bats test_tags=backup:log
+@test "success no log config has no logfile entry" {
+       mkdir --parents build
+       config="{}"
+       run log "$config"
+
+       assert_success
+       assert_output ""
+}
+
+write_log_entry() {
+       local type="$1"
+       mkdir --parents build
+       touch "build/$type"
+       local config=
+       config=$(yq '.' "$CONFIG_FILE")
+       get_date_time() {
+               echo "2000-01-01T23:30:45+02:00"
+       }
+
+       run log "$config" "log entry"
+
+       assert_success
+       assert_output "ℹ️ INFO: 2000-01-01T23:30:45+02:00 [$type] log entry"
+       assert_equal "$(cat "build/backup.log")" "2000-01-01T23:30:45+02:00 [$type] log entry"
+       rm "build/$type"
+       rm "build/backup.log"
+}
+
+for type in none day week month; do
+       bats_test_function --tags "backup:log" \
+               --description "success log has logfile entry for type $type" \
+               -- \
+               write_log_entry "$type"
+done
\ No newline at end of file