From 92209822db7cf0d4d75a85f5149a85b1155399d6 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Mon, 28 Mar 2022 22:01:18 -0700 Subject: [PATCH] Add more tests and update config examples --- Makefile | 4 +- config.hcl | 59 ++++++++------- job.go | 22 ++---- job_test.go | 189 +++++++++++++++++++++++++++++++++++++++++++++++++ main_test.go | 24 +++++++ restic_test.go | 4 ++ shell_test.go | 149 ++++++++++++++++++++++++++++++++++++++ tasks.go | 32 +++++++-- 8 files changed, 428 insertions(+), 55 deletions(-) create mode 100644 main_test.go create mode 100644 shell_test.go diff --git a/Makefile b/Makefile index bc88899..4c8ad94 100644 --- a/Makefile +++ b/Makefile @@ -27,10 +27,8 @@ build: $(APP_NAME) # Run all tests .PHONY: test test: - go test -coverprofile=coverage.out + go test -coverprofile=coverage.out # -short go tool cover -func=coverage.out - @go tool cover -func=coverage.out | awk -v target=80.0% \ - '/^total:/ { print "Total coverage: " $3 " Minimum coverage: " target; if ($3+0.0 >= target+0.0) print "ok"; else { print "fail"; exit 1; } }' # Installs pre-commit hooks .PHONY: install-hooks diff --git a/config.hcl b/config.hcl index 2849cb5..19368cb 100644 --- a/config.hcl +++ b/config.hcl @@ -16,53 +16,52 @@ job "MyApp" { mysql "DumpMainDB" { hostname = "foo" username = "bar" + dump_to = "/data/main.sql" } sqlite "DumpSqlite" { - path = "/db/path" + path = "/db/sqlite.db" + dump_to = "/data/sqlite.db.bak" } - task "RunSomePreScripts" { - script { - on_backup = < /biz.txt - EOF + task "Create biz file" { - on_restore = "/foo/bar.sh" - } - - script { + pre_script { on_backup = <> /biz.txt EOF } - } - task "ActuallyBackupSomeStuff" { - backup { - files =[ - "/foo/bar", - "/biz.txt", - ] - - backup_opts { - Tags = ["service"] - } - - restore_opts { - Verify = true - } - } - } - - task "RunSomePostScripts" { - script { + post_script { on_backup = < 0 && t.Database == "" { return fmt.Errorf( - "mysql task %s is invalid. Must specify a database to use tables: %w", + "task %s is invalid. Must specify a database to use tables: %w", t.Name, ErrMissingField, ) @@ -170,12 +174,16 @@ func (t JobTaskSqlite) Paths() []string { } func (t JobTaskSqlite) Validate() error { + if t.DumpToPath == "" { + return fmt.Errorf("task %s is missing dump_to path: %w", t.Name, ErrMissingField) + } + if s, err := os.Stat(t.DumpToPath); err != nil { if !errors.Is(err, fs.ErrNotExist) { - return fmt.Errorf("Could not stat dump file path: %w", err) + return fmt.Errorf("task %s: invalid dump_to: could not stat path: %v: %w", t.Name, err, ErrInvalidConfigValue) } } else if s.Mode().IsDir() { - return fmt.Errorf("dump_to cannot be a directory: %w", ErrInvalidConfigValue) + return fmt.Errorf("task %s: dump_to cannot be a directory: %w", t.Name, ErrInvalidConfigValue) } return nil @@ -202,7 +210,7 @@ func (t JobTaskSqlite) GetPostTask() ExecutableTask { } type BackupFilesTask struct { - Files []string `hcl:"files"` + Paths []string `hcl:"files"` BackupOpts *BackupOpts `hcl:"backup_opts,block"` RestoreOpts *RestoreOpts `hcl:"restore_opts,block"` name string @@ -247,6 +255,14 @@ func (t *BackupFilesTask) SetName(name string) { t.name = name } +func (t *BackupFilesTask) Validate() error { + if len(t.Paths) == 0 { + return fmt.Errorf("backup config doesn't include any paths: %w", ErrInvalidConfigValue) + } + + return nil +} + // JobTask represents a single task within a backup job. type JobTask struct { Name string `hcl:"name,label"` @@ -255,6 +271,10 @@ type JobTask struct { } func (t JobTask) Validate() error { + if t.Name == "" { + return fmt.Errorf("task is missing a name: %w", ErrMissingField) + } + return nil }