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()
|
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")
|
panic("no logfile no info")
|
||||||
}
|
}
|
||||||
log.SetOutput(logfile)
|
log.SetOutput(logfile)
|
||||||
|
log.Println("Logging initialized")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Global variables for backive
|
// Global variables for backive
|
||||||
|
@ -122,6 +123,10 @@ func main() {
|
||||||
code := <-exitChan
|
code := <-exitChan
|
||||||
database.Save()
|
database.Save()
|
||||||
log.Printf("Received exit code (%d), shutting down.", code)
|
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)
|
os.Exit(code)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|
32
config.go
32
config.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,16 +27,27 @@ type Settings struct {
|
||||||
|
|
||||||
// CreateViper creates a viper instance for usage later
|
// CreateViper creates a viper instance for usage later
|
||||||
func (c *Configuration) CreateViper() {
|
func (c *Configuration) CreateViper() {
|
||||||
vconfig := viper.New()
|
if c.Vconfig == nil {
|
||||||
// vconfig.Debug()
|
vconfig := viper.New()
|
||||||
vconfig.SetConfigName("backive")
|
// vconfig.Debug()
|
||||||
vconfig.SetConfigFile("backive.yml")
|
vconfig.SetConfigName("backive")
|
||||||
//vconfig.SetConfigFile("backive.yaml")
|
// do not set config file explicitly or viper doesnt search for it, and /etc search fails
|
||||||
vconfig.SetConfigType("yaml")
|
//vconfig.SetConfigFile("backive.yml")
|
||||||
vconfig.AddConfigPath("/etc/backive/") // system config
|
//vconfig.SetConfigFile("backive.yaml")
|
||||||
//vconfig.AddConfigPath("$HOME/.backive/")
|
vconfig.SetConfigType("yaml")
|
||||||
vconfig.AddConfigPath(".")
|
//vconfig.AddConfigPath("$HOME/.backive/")
|
||||||
c.Vconfig = vconfig
|
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
|
// 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
|
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 (
|
require (
|
||||||
github.com/fsnotify/fsnotify v1.5.1 // indirect
|
|
||||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/magiconair/properties v1.8.5 // indirect
|
github.com/magiconair/properties v1.8.5 // indirect
|
||||||
|
|
Loading…
Reference in New Issue