-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks_test.go
More file actions
251 lines (228 loc) · 6.07 KB
/
tasks_test.go
File metadata and controls
251 lines (228 loc) · 6.07 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main_test
import (
"bytes"
"errors"
"log"
"testing"
main "git.iamthefij.com/iamthefij/restic-scheduler"
)
func NewBufferedLogger(prefix string) (*bytes.Buffer, *log.Logger) {
outputBuffer := bytes.Buffer{}
logger := log.New(&outputBuffer, prefix, 0)
return &outputBuffer, logger
}
func TestJobTaskScript(t *testing.T) {
t.Parallel()
cases := []struct {
name string
script main.JobTaskScript
config main.TaskConfig
expectedErr error
expectedOutput string
}{
{
name: "simple",
config: main.TaskConfig{
BackupPaths: nil,
Env: nil,
Logger: nil,
Restic: nil,
},
script: main.JobTaskScript{
Cwd: "./test",
OnBackup: "echo yass",
OnRestore: "echo yass",
},
expectedErr: nil,
expectedOutput: "t yass\nt \n",
},
{
name: "check from job dir",
config: main.TaskConfig{
BackupPaths: nil,
Env: nil,
Logger: nil,
Restic: nil,
},
script: main.JobTaskScript{
Cwd: "./test",
OnBackup: "basename `pwd`",
OnRestore: "basename `pwd`",
},
expectedErr: nil,
expectedOutput: "t test\nt \n",
},
{
name: "check env",
config: main.TaskConfig{
BackupPaths: nil,
Env: map[string]string{"TEST": "OK"},
Logger: nil,
Restic: nil,
},
script: main.JobTaskScript{
Cwd: "./test",
OnBackup: "echo $TEST",
OnRestore: "echo $TEST",
},
expectedErr: nil,
expectedOutput: "t OK\nt \n",
},
}
for _, c := range cases {
testCase := c
buf, logger := NewBufferedLogger("t")
testCase.config.Logger = logger
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
actual := testCase.script.RunBackup(testCase.config)
if !errors.Is(actual, testCase.expectedErr) {
t.Errorf("expected error to wrap %v but found %v", testCase.expectedErr, actual)
}
output := buf.String()
if testCase.expectedOutput != output {
t.Errorf("Unexpected output. expected: %s actual: %s", testCase.expectedOutput, output)
}
})
}
}
func TestJobTaskSql(t *testing.T) {
t.Parallel()
type TaskGenerator interface {
Validate() error
GetPreTask() main.ExecutableTask
GetPostTask() main.ExecutableTask
}
cases := []struct {
name string
task TaskGenerator
validationErr error
preBackup string
postBackup string
preRestore string
postRestore string
}{
{
name: "mysql simple",
//nolint:exhaustruct
task: main.JobTaskMySQL{
Name: "simple",
DumpToPath: "./simple.sql",
},
validationErr: nil,
preBackup: "mysqldump --result-file ./simple.sql --all-databases",
postBackup: "",
preRestore: "",
postRestore: "mysql < ./simple.sql",
},
{
name: "mariadb simple",
//nolint:exhaustruct
task: main.JobTaskMySQL{
Name: "simple",
DumpToPath: "./simple.sql",
UseMariaDB: true,
},
validationErr: nil,
preBackup: "mariadb-dump --result-file ./simple.sql --all-databases",
postBackup: "",
preRestore: "",
postRestore: "mariadb < ./simple.sql",
},
{
name: "mysql tables no database",
//nolint:exhaustruct
task: main.JobTaskMySQL{
Name: "name",
Tables: []string{"table1", "table2"},
DumpToPath: "./simple.sql",
},
validationErr: main.ErrMissingField,
preBackup: "",
postBackup: "",
preRestore: "",
postRestore: "",
},
{
name: "mysql all options",
task: main.JobTaskMySQL{
Name: "simple",
Hostname: "host",
Port: 3306,
Username: "user",
Password: "pass",
Database: "db",
NoTablespaces: true,
Tables: []string{"table1", "table2"},
DumpToPath: "./simple.sql",
},
validationErr: nil,
preBackup: "mysqldump --result-file ./simple.sql --host host --port 3306" +
" --user user --no-tablespaces --password=pass db table1 table2",
postBackup: "",
preRestore: "",
postRestore: "mysql --host host --port 3306 --user user --password=pass db < ./simple.sql",
},
{
name: "psql all",
task: main.JobTaskPostgres{
Name: "simple",
Hostname: "host",
Port: 6543,
Username: "user",
Password: "pass",
Database: "db",
NoTablespaces: true,
Create: true,
Clean: true,
Tables: []string{"table1", "table2"},
DumpToPath: "./simple.sql",
},
validationErr: nil,
preBackup: "pg_dump --file ./simple.sql --host host --port 6543 --username user --no-tablespaces" +
" --clean --create --table table1 --table table2 db",
postBackup: "",
preRestore: "",
postRestore: "psql --host host --port 6543 --username user db < ./simple.sql",
},
// Sqlite
{
name: "sqlite simple",
task: main.JobTaskSqlite{
Name: "simple",
Path: "database.db",
DumpToPath: "./simple.db.bak",
},
validationErr: nil,
preBackup: "sqlite3 'database.db' '.backup ./simple.db.bak'",
postBackup: "",
preRestore: "",
postRestore: "cp './simple.db.bak' 'database.db'",
},
}
for _, c := range cases {
testCase := c
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
validateErr := testCase.task.Validate()
if !errors.Is(validateErr, testCase.validationErr) {
t.Errorf("unexpected validation result. expected: %v, actual: %v", testCase.validationErr, validateErr)
}
if validateErr != nil {
return
}
if preTask, ok := testCase.task.GetPreTask().(main.JobTaskScript); ok {
AssertEqual(t, "incorrect pre-backup", testCase.preBackup, preTask.OnBackup)
AssertEqual(t, "incorrect pre-restore", testCase.preRestore, preTask.OnRestore)
} else {
t.Error("pre task was not a JobTaskScript")
}
if postTask, ok := testCase.task.GetPostTask().(main.JobTaskScript); ok {
AssertEqual(t, "incorrect post-backup", testCase.postBackup, postTask.OnBackup)
AssertEqual(t, "incorrect post-restore", testCase.postRestore, postTask.OnRestore)
} else {
t.Error("post task was not a JobTaskScript")
}
})
}
}