From 8c054a66d3fb46a87696d44b90e25fba17ec6d9d Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Sat, 8 Jan 2022 11:13:05 +0100 Subject: [PATCH] Adding more tests and fixed viper finding the /etc config file --- backive.yml => backive-example.yml | 0 backup_test.go | 51 +++++++++++++++++++++++++++++- cmd/backive/main.go | 5 +++ config.go | 32 +++++++++++++------ go.mod | 6 ++-- 5 files changed, 81 insertions(+), 13 deletions(-) rename backive.yml => backive-example.yml (100%) diff --git a/backive.yml b/backive-example.yml similarity index 100% rename from backive.yml rename to backive-example.yml diff --git a/backup_test.go b/backup_test.go index 5d553fd..8077cef 100644 --- a/backup_test.go +++ b/backup_test.go @@ -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() + } } diff --git a/cmd/backive/main.go b/cmd/backive/main.go index a492c12..0bbbe59 100644 --- a/cmd/backive/main.go +++ b/cmd/backive/main.go @@ -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) }() diff --git a/config.go b/config.go index 1bed738..36c145a 100644 --- a/config.go +++ b/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() { - 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 diff --git a/go.mod b/go.mod index 8e81220..9583aee 100644 --- a/go.mod +++ b/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