This commit is contained in:
parent
dd23da80ee
commit
6a28cf96a8
28
job_test.go
28
job_test.go
@ -27,12 +27,12 @@ func TestResticConfigValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "missing passphrase",
|
name: "missing passphrase",
|
||||||
expectedErr: main.ErrMutuallyExclusive,
|
expectedErr: main.ErrMutuallyExclusive,
|
||||||
config: main.ResticConfig{}, // nolint:exhaustivestruct
|
config: main.ResticConfig{}, //nolint:exhaustivestruct
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "passphrase no file",
|
name: "passphrase no file",
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
// nolint:exhaustivestruct
|
//nolint:exhaustivestruct
|
||||||
config: main.ResticConfig{
|
config: main.ResticConfig{
|
||||||
Passphrase: "shh",
|
Passphrase: "shh",
|
||||||
},
|
},
|
||||||
@ -40,7 +40,7 @@ func TestResticConfigValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "file no passphrase",
|
name: "file no passphrase",
|
||||||
expectedErr: nil,
|
expectedErr: nil,
|
||||||
// nolint:exhaustivestruct
|
//nolint:exhaustivestruct
|
||||||
config: main.ResticConfig{
|
config: main.ResticConfig{
|
||||||
GlobalOpts: &main.ResticGlobalOpts{
|
GlobalOpts: &main.ResticGlobalOpts{
|
||||||
PasswordFile: "file",
|
PasswordFile: "file",
|
||||||
@ -50,7 +50,7 @@ func TestResticConfigValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "file and passphrase",
|
name: "file and passphrase",
|
||||||
expectedErr: main.ErrMutuallyExclusive,
|
expectedErr: main.ErrMutuallyExclusive,
|
||||||
// nolint:exhaustivestruct
|
//nolint:exhaustivestruct
|
||||||
config: main.ResticConfig{
|
config: main.ResticConfig{
|
||||||
Passphrase: "shh",
|
Passphrase: "shh",
|
||||||
GlobalOpts: &main.ResticGlobalOpts{
|
GlobalOpts: &main.ResticGlobalOpts{
|
||||||
@ -89,7 +89,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
@ -103,7 +103,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
@ -117,7 +117,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
Schedule: "shrug",
|
Schedule: "shrug",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
@ -129,9 +129,9 @@ func TestJobValidation(t *testing.T) {
|
|||||||
job: main.Job{
|
job: main.Job{
|
||||||
Name: "Test job",
|
Name: "Test job",
|
||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: main.ResticConfig{}, // nolint:exhaustivestruct
|
Config: main.ResticConfig{}, //nolint:exhaustivestruct
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
@ -145,7 +145,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{{}},
|
Tasks: []main.JobTask{{}},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
@ -159,7 +159,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{{}},
|
MySQL: []main.JobTaskMySQL{{}},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
@ -173,7 +173,7 @@ func TestJobValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{{}},
|
Sqlite: []main.JobTaskSqlite{{}},
|
||||||
@ -212,7 +212,7 @@ func TestConfigValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
}}},
|
}}},
|
||||||
@ -230,7 +230,7 @@ func TestConfigValidation(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
|
2
main.go
2
main.go
@ -110,7 +110,7 @@ func NewSetFrom(l []string) Set {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
/// FilterJobs filters a list of jobs by a list of names
|
// FilterJobs filters a list of jobs by a list of names.
|
||||||
func FilterJobs(jobs []Job, names []string) ([]Job, error) {
|
func FilterJobs(jobs []Job, names []string) ([]Job, error) {
|
||||||
nameSet := NewSetFrom(names)
|
nameSet := NewSetFrom(names)
|
||||||
if nameSet.Contains("all") {
|
if nameSet.Contains("all") {
|
||||||
|
@ -49,7 +49,7 @@ func TestRunJobs(t *testing.T) {
|
|||||||
Schedule: "@daily",
|
Schedule: "@daily",
|
||||||
Config: ValidResticConfig(),
|
Config: ValidResticConfig(),
|
||||||
Tasks: []main.JobTask{},
|
Tasks: []main.JobTask{},
|
||||||
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, // nolint:exhaustivestruct
|
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustivestruct
|
||||||
Forget: nil,
|
Forget: nil,
|
||||||
MySQL: []main.JobTaskMySQL{},
|
MySQL: []main.JobTaskMySQL{},
|
||||||
Sqlite: []main.JobTaskSqlite{},
|
Sqlite: []main.JobTaskSqlite{},
|
||||||
|
@ -335,7 +335,7 @@ type Snapshot struct {
|
|||||||
Hostname string `json:"hostname"`
|
Hostname string `json:"hostname"`
|
||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
ShortID string `json:"short_id"` // nolint:tagliatelle
|
ShortID string `json:"short_id"` //nolint:tagliatelle
|
||||||
Paths []string `json:"paths"`
|
Paths []string `json:"paths"`
|
||||||
Tags []string `json:"tags,omitempty"`
|
Tags []string `json:"tags,omitempty"`
|
||||||
}
|
}
|
||||||
|
@ -160,19 +160,19 @@ func TestBuildEnv(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "No Env",
|
name: "No Env",
|
||||||
cmd: main.Restic{}, // nolint:exhaustivestruct
|
cmd: main.Restic{}, //nolint:exhaustivestruct
|
||||||
expected: os.Environ(),
|
expected: os.Environ(),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "SetEnv",
|
name: "SetEnv",
|
||||||
cmd: main.Restic{ // nolint:exhaustivestruct
|
cmd: main.Restic{ //nolint:exhaustivestruct
|
||||||
Env: map[string]string{"TestKey": "Value"},
|
Env: map[string]string{"TestKey": "Value"},
|
||||||
},
|
},
|
||||||
expected: append(os.Environ(), "TestKey=Value"),
|
expected: append(os.Environ(), "TestKey=Value"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "SetEnv",
|
name: "SetEnv",
|
||||||
cmd: main.Restic{ // nolint:exhaustivestruct
|
cmd: main.Restic{ //nolint:exhaustivestruct
|
||||||
Passphrase: "Shhhhhhhh!!",
|
Passphrase: "Shhhhhhhh!!",
|
||||||
},
|
},
|
||||||
expected: append(os.Environ(), "RESTIC_PASSWORD=Shhhhhhhh!!"),
|
expected: append(os.Environ(), "RESTIC_PASSWORD=Shhhhhhhh!!"),
|
||||||
@ -210,7 +210,7 @@ func TestResticInterface(t *testing.T) {
|
|||||||
Repo: repoDir,
|
Repo: repoDir,
|
||||||
Env: map[string]string{},
|
Env: map[string]string{},
|
||||||
Passphrase: "Correct.Horse.Battery.Staple",
|
Passphrase: "Correct.Horse.Battery.Staple",
|
||||||
// nolint:exhaustivestruct
|
//nolint:exhaustivestruct
|
||||||
GlobalOpts: &main.ResticGlobalOpts{
|
GlobalOpts: &main.ResticGlobalOpts{
|
||||||
CacheDir: cacheDir,
|
CacheDir: cacheDir,
|
||||||
Options: map[string]string{
|
Options: map[string]string{
|
||||||
@ -231,7 +231,7 @@ func TestResticInterface(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try to backup when repo is not initialized
|
// Try to backup when repo is not initialized
|
||||||
err = restic.Backup([]string{dataDir}, main.BackupOpts{}) // nolint:exhaustivestruct
|
err = restic.Backup([]string{dataDir}, main.BackupOpts{}) //nolint:exhaustivestruct
|
||||||
if !errors.Is(err, main.ErrRepoNotFound) {
|
if !errors.Is(err, main.ErrRepoNotFound) {
|
||||||
AssertEqualFail(t, "unexpected error creating making backup", nil, err)
|
AssertEqualFail(t, "unexpected error creating making backup", nil, err)
|
||||||
}
|
}
|
||||||
@ -245,7 +245,7 @@ func TestResticInterface(t *testing.T) {
|
|||||||
AssertEqualFail(t, "unexpected error reinitializing repo", nil, err)
|
AssertEqualFail(t, "unexpected error reinitializing repo", nil, err)
|
||||||
|
|
||||||
// Backup for real this time
|
// Backup for real this time
|
||||||
err = restic.Backup([]string{dataDir}, main.BackupOpts{Tags: []string{"test"}}) // nolint:exhaustivestruct
|
err = restic.Backup([]string{dataDir}, main.BackupOpts{Tags: []string{"test"}}) //nolint:exhaustivestruct
|
||||||
AssertEqualFail(t, "unexpected error creating making backup", nil, err)
|
AssertEqualFail(t, "unexpected error creating making backup", nil, err)
|
||||||
|
|
||||||
// Check snapshots
|
// Check snapshots
|
||||||
@ -259,7 +259,7 @@ func TestResticInterface(t *testing.T) {
|
|||||||
AssertEqual(t, "unexpected snapshot value: tags", []string{"test"}, snapshots[0].Tags)
|
AssertEqual(t, "unexpected snapshot value: tags", []string{"test"}, snapshots[0].Tags)
|
||||||
|
|
||||||
// Backup again
|
// Backup again
|
||||||
err = restic.Backup([]string{dataDir}, main.BackupOpts{}) // nolint:exhaustivestruct
|
err = restic.Backup([]string{dataDir}, main.BackupOpts{}) //nolint:exhaustivestruct
|
||||||
AssertEqualFail(t, "unexpected error creating making second backup", nil, err)
|
AssertEqualFail(t, "unexpected error creating making second backup", nil, err)
|
||||||
|
|
||||||
// Check for second backup
|
// Check for second backup
|
||||||
@ -268,7 +268,7 @@ func TestResticInterface(t *testing.T) {
|
|||||||
AssertEqual(t, "unexpected number of snapshots", 2, len(snapshots))
|
AssertEqual(t, "unexpected number of snapshots", 2, len(snapshots))
|
||||||
|
|
||||||
// Forget one backup
|
// Forget one backup
|
||||||
err = restic.Forget(main.ForgetOpts{KeepLast: 1, Prune: true}) // nolint:exhaustivestruct
|
err = restic.Forget(main.ForgetOpts{KeepLast: 1, Prune: true}) //nolint:exhaustivestruct
|
||||||
AssertEqualFail(t, "unexpected error forgetting snapshot", nil, err)
|
AssertEqualFail(t, "unexpected error forgetting snapshot", nil, err)
|
||||||
|
|
||||||
// Check forgotten snapshot
|
// Check forgotten snapshot
|
||||||
@ -290,7 +290,7 @@ func TestResticInterface(t *testing.T) {
|
|||||||
AssertEqualFail(t, "incorrect value in test file (we expect the unexpected!)", "unexpected", string(value))
|
AssertEqualFail(t, "incorrect value in test file (we expect the unexpected!)", "unexpected", string(value))
|
||||||
|
|
||||||
// Restore files
|
// Restore files
|
||||||
err = restic.Restore("latest", main.RestoreOpts{Target: restoreTarget}) // nolint:exhaustivestruct
|
err = restic.Restore("latest", main.RestoreOpts{Target: restoreTarget}) //nolint:exhaustivestruct
|
||||||
AssertEqualFail(t, "unexpected error restoring latest snapshot", nil, err)
|
AssertEqualFail(t, "unexpected error restoring latest snapshot", nil, err)
|
||||||
|
|
||||||
// Check restored values
|
// Check restored values
|
||||||
|
@ -69,7 +69,7 @@ func RunHTTPHandlers(addr string) error {
|
|||||||
http.HandleFunc("/health", healthHandleFunc)
|
http.HandleFunc("/health", healthHandleFunc)
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
return fmt.Errorf("error on healthcheck: %w", http.ListenAndServe(addr, nil))
|
return fmt.Errorf("error on healthcheck: %w", http.ListenAndServe(addr, nil)) //#nosec: g114
|
||||||
}
|
}
|
||||||
|
|
||||||
func ScheduleAndRunJobs(jobs []Job) error {
|
func ScheduleAndRunJobs(jobs []Job) error {
|
||||||
|
2
shell.go
2
shell.go
@ -50,7 +50,7 @@ func (w *CapturedLogWriter) Write(content []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func RunShell(script string, cwd string, env map[string]string, logger *log.Logger) error {
|
func RunShell(script string, cwd string, env map[string]string, logger *log.Logger) error {
|
||||||
cmd := exec.Command("sh", "-c", strings.TrimSpace(script)) // nolint:gosec
|
cmd := exec.Command("sh", "-c", strings.TrimSpace(script)) //nolint:gosec
|
||||||
|
|
||||||
// Make both stderr and stdout go to logger
|
// Make both stderr and stdout go to logger
|
||||||
cmd.Stdout = NewCapturedLogWriter(logger)
|
cmd.Stdout = NewCapturedLogWriter(logger)
|
||||||
|
4
tasks.go
4
tasks.go
@ -230,7 +230,7 @@ type BackupFilesTask struct {
|
|||||||
|
|
||||||
func (t BackupFilesTask) RunBackup(cfg TaskConfig) error {
|
func (t BackupFilesTask) RunBackup(cfg TaskConfig) error {
|
||||||
if t.BackupOpts == nil {
|
if t.BackupOpts == nil {
|
||||||
t.BackupOpts = &BackupOpts{} // nolint:exhaustivestruct
|
t.BackupOpts = &BackupOpts{} //nolint:exhaustivestruct
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cfg.Restic.Backup(cfg.BackupPaths, *t.BackupOpts); err != nil {
|
if err := cfg.Restic.Backup(cfg.BackupPaths, *t.BackupOpts); err != nil {
|
||||||
@ -245,7 +245,7 @@ func (t BackupFilesTask) RunBackup(cfg TaskConfig) error {
|
|||||||
|
|
||||||
func (t BackupFilesTask) RunRestore(cfg TaskConfig) error {
|
func (t BackupFilesTask) RunRestore(cfg TaskConfig) error {
|
||||||
if t.RestoreOpts == nil {
|
if t.RestoreOpts == nil {
|
||||||
t.RestoreOpts = &RestoreOpts{} // nolint:exhaustivestruct
|
t.RestoreOpts = &RestoreOpts{} //nolint:exhaustivestruct
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make the snapshot configurable
|
// TODO: Make the snapshot configurable
|
||||||
|
@ -120,7 +120,7 @@ func TestJobTaskSql(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "mysql simple",
|
name: "mysql simple",
|
||||||
// nolint:exhaustivestruct
|
//nolint:exhaustivestruct
|
||||||
task: main.JobTaskMySQL{
|
task: main.JobTaskMySQL{
|
||||||
Name: "simple",
|
Name: "simple",
|
||||||
DumpToPath: "./simple.sql",
|
DumpToPath: "./simple.sql",
|
||||||
@ -133,7 +133,7 @@ func TestJobTaskSql(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "mysql tables no database",
|
name: "mysql tables no database",
|
||||||
// nolint:exhaustivestruct
|
//nolint:exhaustivestruct
|
||||||
task: main.JobTaskMySQL{
|
task: main.JobTaskMySQL{
|
||||||
Name: "name",
|
Name: "name",
|
||||||
Tables: []string{"table1", "table2"},
|
Tables: []string{"table1", "table2"},
|
||||||
|
Loading…
Reference in New Issue
Block a user