Adding more tests and fixed viper finding the /etc config file

This commit is contained in:
Marcel Otte 2022-01-08 11:13:05 +01:00
parent 5602907eb0
commit 8c054a66d3
5 changed files with 81 additions and 13 deletions

View File

@ -37,5 +37,54 @@ func TestFindBackupsForDevice(t *testing.T) {
t.Fail()
}
}
}
func TestCanRun(t *testing.T) {
var bkpTargetPathMissing = Backup{
Name: "targetPathMissing",
ScriptPath: "Somethingsomething",
}
err := bkpTargetPathMissing.CanRun()
if err == nil {
t.Log("Missing targetPath has to fail function 'CanRun()'")
t.Fail()
}
var bkpScriptPathMissing = Backup{
Name: "scriptPathMissing",
TargetPath: "somethingsomething",
}
err = bkpScriptPathMissing.CanRun()
if err == nil {
t.Log("Missing scriptPath has to fail function 'CanRun()'")
t.Fail()
}
var bkpFrequencyZero = Backup{
Name: "testFrequencyZero",
TargetPath: "somewhere",
ScriptPath: "somehwere_else",
Frequency: 0,
}
var bkpFrequencySeven = Backup{
Name: "testFrequencySeven",
TargetPath: "somewhere",
ScriptPath: "somewhere_else",
Frequency: 7,
}
database.Load()
runs.Load(database)
runs.RegisterRun(&bkpFrequencyZero)
err = bkpFrequencyZero.CanRun()
if err != nil {
t.Log("Frequency zero can be executed anytime.")
t.Fail()
}
runs.RegisterRun(&bkpFrequencySeven)
err = bkpFrequencySeven.CanRun()
if err == nil {
t.Log("Frequency 7 must give an error about not having reached the interval")
t.Fail()
}
}

View File

@ -21,6 +21,7 @@ func setupLogging() {
panic("no logfile no info")
}
log.SetOutput(logfile)
log.Println("Logging initialized")
}
// Global variables for backive
@ -122,6 +123,10 @@ func main() {
code := <-exitChan
database.Save()
log.Printf("Received exit code (%d), shutting down.", code)
err := os.Remove(config.Settings.UnixSocketLocation)
if err != nil {
log.Printf("Removal of %s failed.", config.Settings.UnixSocketLocation)
}
os.Exit(code)
}()

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
@ -26,16 +27,27 @@ type Settings struct {
// CreateViper creates a viper instance for usage later
func (c *Configuration) CreateViper() {
vconfig := viper.New()
// vconfig.Debug()
vconfig.SetConfigName("backive")
vconfig.SetConfigFile("backive.yml")
//vconfig.SetConfigFile("backive.yaml")
vconfig.SetConfigType("yaml")
vconfig.AddConfigPath("/etc/backive/") // system config
//vconfig.AddConfigPath("$HOME/.backive/")
vconfig.AddConfigPath(".")
c.Vconfig = vconfig
if c.Vconfig == nil {
vconfig := viper.New()
// vconfig.Debug()
vconfig.SetConfigName("backive")
// do not set config file explicitly or viper doesnt search for it, and /etc search fails
//vconfig.SetConfigFile("backive.yml")
//vconfig.SetConfigFile("backive.yaml")
vconfig.SetConfigType("yaml")
//vconfig.AddConfigPath("$HOME/.backive/")
vconfig.AddConfigPath(".") // backup config in local dir
vconfig.AddConfigPath("/etc/backive/") // system config
vconfig.OnConfigChange(func(e fsnotify.Event) {
log.Printf("Event: %s", e)
if e.Op == fsnotify.Write {
log.Printf("Reloading %s", e.Name)
c.Load()
}
})
vconfig.WatchConfig()
c.Vconfig = vconfig
}
}
// Load loads the configuration from the disk

6
go.mod
View File

@ -2,10 +2,12 @@ module github.com/qwc/backive
go 1.17
require github.com/spf13/viper v1.9.0
require (
github.com/fsnotify/fsnotify v1.5.1
github.com/spf13/viper v1.9.0
)
require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect