mirror of https://github.com/qwc/backive.git
Adding more tests and fixed viper finding the /etc config file
This commit is contained in:
parent
5602907eb0
commit
8c054a66d3
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}()
|
||||
|
||||
|
|
18
config.go
18
config.go
|
@ -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() {
|
||||
if c.Vconfig == nil {
|
||||
vconfig := viper.New()
|
||||
// vconfig.Debug()
|
||||
vconfig.SetConfigName("backive")
|
||||
vconfig.SetConfigFile("backive.yml")
|
||||
// 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("/etc/backive/") // system config
|
||||
//vconfig.AddConfigPath("$HOME/.backive/")
|
||||
vconfig.AddConfigPath(".")
|
||||
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
6
go.mod
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue