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

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -26,17 +27,28 @@ 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() {
if c.Vconfig == nil {
vconfig := viper.New() vconfig := viper.New()
// vconfig.Debug() // vconfig.Debug()
vconfig.SetConfigName("backive") 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.SetConfigFile("backive.yaml")
vconfig.SetConfigType("yaml") vconfig.SetConfigType("yaml")
vconfig.AddConfigPath("/etc/backive/") // system config
//vconfig.AddConfigPath("$HOME/.backive/") //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 c.Vconfig = vconfig
} }
}
// Load loads the configuration from the disk // Load loads the configuration from the disk
func (c *Configuration) Load() { func (c *Configuration) Load() {

6
go.mod
View File

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