From 5602907eb04bab2e910dd0b1af69f1031aa6f094 Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Fri, 7 Jan 2022 23:41:31 +0100 Subject: [PATCH] Add first unit test --- backup.go | 5 ++--- backup_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ cmd/backive/main.go | 4 ++-- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 backup_test.go diff --git a/backup.go b/backup.go index 48eb747..89763d8 100644 --- a/backup.go +++ b/backup.go @@ -65,10 +65,9 @@ func (b *Backup) PrepareRun() error { ) CreateDirectoryIfNotExists(backupPath) // configure extra logger - logname := "/var/log/backive/backive.log" - logdir, _ := path.Split(logname) + logdir := config.Settings.LogLocation CreateDirectoryIfNotExists(logdir) - logname = path.Join(logdir, b.Name) + ".log" + logname := path.Join(logdir, b.Name) + ".log" logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) if err != nil { log.Println("Error creating logfile!") diff --git a/backup_test.go b/backup_test.go new file mode 100644 index 0000000..5d553fd --- /dev/null +++ b/backup_test.go @@ -0,0 +1,41 @@ +package backive + +import "testing" + +func TestFindBackupsForDevice(t *testing.T) { + var testBackups = Backups{} + + testBackups["backup1"] = &Backup{ + Name: "backup1", + TargetDevice: "dev1", + } + testBackups["backup2"] = &Backup{ + Name: "backup2", + TargetDevice: "dev1", + } + testBackups["backup3"] = &Backup{ + Name: "backup3", + TargetDevice: "dev2", + } + + var testDevice = Device{ + Name: "dev1", + } + bkps, found := testBackups.FindBackupsForDevice(testDevice) + + if !found { + t.Log("found has to be true") + t.Fail() + } + if len(bkps) != 2 { + t.Log("Length of the returned backup slice has to be 2") + t.Fail() + } + for _, b := range bkps { + if b.TargetDevice != testDevice.Name { + t.Log("All resulting elements of the returned slice have to have the questioned device as TargetDevice!") + t.Fail() + } + } + +} diff --git a/cmd/backive/main.go b/cmd/backive/main.go index 818cc31..a492c12 100644 --- a/cmd/backive/main.go +++ b/cmd/backive/main.go @@ -12,7 +12,7 @@ import ( ) func setupLogging() { - logname := "/var/log/backive/backive.log" + logname := path.Join(config.Settings.LogLocation, "backive.log") logdir, _ := path.Split(logname) backive.CreateDirectoryIfNotExists(logdir) logfile, err := os.OpenFile(logname, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666) @@ -87,7 +87,6 @@ func defaultCallback(envMap map[string]string) { } func main() { - setupLogging() signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGHUP, @@ -131,6 +130,7 @@ func main() { // find and load config database.Load() config.Load() + setupLogging() backive.Init(config, database) // init scheduler and check for next needed runs?