Add first unit test

This commit is contained in:
Marcel Otte 2022-01-07 23:41:31 +01:00
parent e5e7f7326a
commit 5602907eb0
3 changed files with 45 additions and 5 deletions

View File

@ -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!")

41
backup_test.go Normal file
View File

@ -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()
}
}
}

View File

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