From bcfaffac240d74cd79bec3c2a9d3c144d215b495 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sat, 22 Feb 2020 11:57:50 +0100 Subject: Added tests to the state package, and reworked the code around that --- main.go | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 3e206a0..bc5945c 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "bareos-zabbix-check/spool" "bareos-zabbix-check/state" "fmt" + "log" "os" "time" ) @@ -14,22 +15,52 @@ func main() { var ( config config.Config spool spool.Spool - state state.State errorString string missingString string ) config.Init() - err := state.Parse(&config) + // Open the state file + stateFile, err := os.Open(config.StateFile()) if err != nil { - fmt.Print(err) + fmt.Printf("INFO Couldn't open state file : %s", err) os.Exit(0) } + defer stateFile.Close() + // parse the state file + header, err := state.ParseHeader(stateFile) + if err != nil { + fmt.Printf("INFO Could not parse state file header : %s", err) + os.Exit(0) + } + if config.Verbose() { + log.Printf("Parsed header: %+s\n", header) + } + // seek to the job entries in the state file + offset, err := stateFile.Seek(int64(header.LastJobsAddr), 0) + if err != nil { + fmt.Printf("INFO Couldn't seek to jobs position in state file : %s", err) + } + if uint64(offset) != header.LastJobsAddr { + fmt.Print("INFO Truncated state file") + } + // Then parse the jobs in the state file + jobs, err := state.ParseJobs(stateFile) + if err != nil { + fmt.Printf("INFO Could not parse jobs in state file : %s", err) + } + if config.Verbose() { + log.Printf("%d jobs found in state file\n", len(jobs)) + for i := 0; i < len(jobs); i++ { + log.Print(jobs[i]) + } + } + // We will check for errors in loading the spool file only at the end. If all jobs ran successfully without errors // in the state file and we manage to write a new spool file without errors, then we will ignore any error here to // avoid false positives during backup bootstrap err = spool.Load(&config) - jobs := job.KeepOldestOnly(append(state.Jobs(), spool.Jobs()...)) + jobs = job.KeepOldestOnly(append(jobs, spool.Jobs()...)) spool.SetJobs(job.KeepSuccessOnly(jobs)) // we write this new spool -- cgit v1.2.3