-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
102 lines (85 loc) · 2.19 KB
/
main_test.go
File metadata and controls
102 lines (85 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main_test
import (
"errors"
"fmt"
"os"
"testing"
main "git.iamthefij.com/iamthefij/restic-scheduler"
"github.com/stretchr/testify/assert"
)
const MinCoverage = 0.5
func TestMain(m *testing.M) {
testResult := m.Run()
if testResult == 0 && testing.CoverMode() != "" {
c := testing.Coverage()
if c < MinCoverage {
fmt.Printf("WARNING: Tests passed but coverage failed at %0.2f and minimum to pass is %0.2f\n", c, MinCoverage)
testResult = 0
}
}
os.Exit(testResult)
}
func TestReadJobs(t *testing.T) {
t.Parallel()
jobs, err := main.ReadJobs([]string{"./test/sample.hcl"})
if err != nil {
t.Errorf("Unexpected error reading jobs: %v", err)
}
if len(jobs) == 0 {
t.Error("Expected read jobs but found none")
}
}
func TestRunJobs(t *testing.T) {
t.Parallel()
validJob := main.Job{
Name: "Valid job",
Schedule: "@daily",
Config: ValidResticConfig(),
Tasks: []main.JobTask{},
Backup: main.BackupFilesTask{Paths: []string{"/test"}}, //nolint:exhaustruct
Forget: nil,
MySQL: []main.JobTaskMySQL{},
Postgres: []main.JobTaskPostgres{},
Sqlite: []main.JobTaskSqlite{},
}
cases := []struct {
name string
jobs []main.Job
names []string
expected []main.Job
expectedError error
}{
{
name: "Found job",
jobs: []main.Job{validJob},
names: []string{"Valid job"},
expected: []main.Job{validJob},
expectedError: nil,
},
{
name: "Run all",
jobs: []main.Job{validJob},
names: []string{"all"},
expected: []main.Job{validJob},
expectedError: nil,
},
{
name: "Extra, missing job",
jobs: []main.Job{validJob},
names: []string{"Valid job", "Not Found"},
expected: []main.Job{validJob},
expectedError: main.ErrJobNotFound,
},
}
for _, c := range cases {
testCase := c
t.Run(testCase.name+" backup", func(t *testing.T) {
t.Parallel()
jobs, err := main.FilterJobs(testCase.jobs, testCase.names)
_ = assert.Equal(t, jobs, testCase.expected)
if !errors.Is(err, testCase.expectedError) {
t.Errorf("expected %v but found %v", testCase.expectedError, err)
}
})
}
}