forked from Altinity/clickhouse-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
353 lines (316 loc) · 12 KB
/
integration_test.go
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// +build integration
package main
import (
"context"
"fmt"
"os/exec"
"strings"
"testing"
"time"
_ "github.com/kshvakov/clickhouse"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const dbName = "testdb"
type TestDataStuct struct {
Database string
Table string
Schema string
Rows []map[string]interface{}
Fields []string
OrderBy string
}
var testData = []TestDataStuct{
TestDataStuct{
Database: dbName,
Table: "table1",
Schema: "(Date Date, TimeStamp DateTime, Log String) ENGINE = MergeTree(Date, (TimeStamp, Log), 8192)",
Rows: []map[string]interface{}{
map[string]interface{}{"Date": toDate("2018-10-23"), "TimeStamp": toTS("2018-10-23 07:37:14"), "Log": "One"},
map[string]interface{}{"Date": toDate("2018-10-23"), "TimeStamp": toTS("2018-10-23 07:37:15"), "Log": "Two"},
map[string]interface{}{"Date": toDate("2018-10-24"), "TimeStamp": toTS("2018-10-24 07:37:16"), "Log": "Three"},
map[string]interface{}{"Date": toDate("2018-10-24"), "TimeStamp": toTS("2018-10-24 07:37:17"), "Log": "Four"},
map[string]interface{}{"Date": toDate("2019-10-25"), "TimeStamp": toTS("2019-01-25 07:37:18"), "Log": "Five"},
map[string]interface{}{"Date": toDate("2019-10-25"), "TimeStamp": toTS("2019-01-25 07:37:19"), "Log": "Six"},
},
Fields: []string{"Date", "TimeStamp", "Log"},
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: dbName,
Table: "table2",
Schema: "(id UInt64, User String) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
map[string]interface{}{"id": uint64(1), "User": "Alice"},
map[string]interface{}{"id": uint64(2), "User": "Bob"},
map[string]interface{}{"id": uint64(3), "User": "John"},
map[string]interface{}{"id": uint64(4), "User": "Frank"},
map[string]interface{}{"id": uint64(5), "User": "Nancy"},
map[string]interface{}{"id": uint64(6), "User": "Brandon"},
},
Fields: []string{"id", "User"},
OrderBy: "id",
},
TestDataStuct{
Database: dbName,
Table: "table3",
Schema: "(TimeStamp DateTime, Item String, Date Date MATERIALIZED toDate(TimeStamp)) ENGINE = MergeTree() PARTITION BY Date ORDER BY TimeStamp SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
map[string]interface{}{"TimeStamp": toTS("2018-10-23 07:37:14"), "Item": "One"},
map[string]interface{}{"TimeStamp": toTS("2018-10-23 07:37:15"), "Item": "Two"},
map[string]interface{}{"TimeStamp": toTS("2018-10-24 07:37:16"), "Item": "Three"},
map[string]interface{}{"TimeStamp": toTS("2018-10-24 07:37:17"), "Item": "Four"},
map[string]interface{}{"TimeStamp": toTS("2019-01-25 07:37:18"), "Item": "Five"},
map[string]interface{}{"TimeStamp": toTS("2019-01-25 07:37:19"), "Item": "Six"},
},
Fields: []string{"TimeStamp", "Item"},
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: dbName,
Table: "table4",
Schema: "(id UInt64, Col1 String, Col2 String, Col3 String, Col4 String, Col5 String) ENGINE = MergeTree PARTITION BY id ORDER BY (id, Col1, Col2, Col3, Col4, Col5) SETTINGS index_granularity = 8192",
Rows: func() []map[string]interface{} {
result := []map[string]interface{}{}
for i := 0; i < 100; i++ {
result = append(result, map[string]interface{}{"id": uint64(i), "Col1": "Text1", "Col2": "Text2", "Col3": "Text3", "Col4": "Text4", "Col5": "Text5"})
}
return result
}(),
Fields: []string{"id", "Col1", "Col2", "Col3", "Col4", "Col5"},
OrderBy: "id",
},
}
var incrementData = []TestDataStuct{
TestDataStuct{
Database: dbName,
Table: "table1",
Schema: "(Date Date, TimeStamp DateTime, Log String) ENGINE = MergeTree(Date, (TimeStamp, Log), 8192)",
Rows: []map[string]interface{}{
map[string]interface{}{"Date": toDate("2019-10-26"), "TimeStamp": toTS("2019-01-26 07:37:19"), "Log": "Seven"},
},
Fields: []string{"Date", "TimeStamp", "Log"},
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: dbName,
Table: "table2",
Schema: "(id UInt64, User String) ENGINE = MergeTree ORDER BY id SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
map[string]interface{}{"id": uint64(7), "User": "Alice"},
map[string]interface{}{"id": uint64(8), "User": "Bob"},
map[string]interface{}{"id": uint64(9), "User": "John"},
map[string]interface{}{"id": uint64(10), "User": "Frank"},
},
Fields: []string{"id", "User"},
OrderBy: "id",
},
TestDataStuct{
Database: dbName,
Table: "table3",
Schema: "(TimeStamp DateTime, Item String, Date Date MATERIALIZED toDate(TimeStamp)) ENGINE = MergeTree() PARTITION BY Date ORDER BY TimeStamp SETTINGS index_granularity = 8192",
Rows: []map[string]interface{}{
map[string]interface{}{"TimeStamp": toTS("2019-01-26 07:37:18"), "Item": "Seven"},
map[string]interface{}{"TimeStamp": toTS("2019-01-27 07:37:19"), "Item": "Eight"},
},
Fields: []string{"TimeStamp", "Item"},
OrderBy: "TimeStamp",
},
TestDataStuct{
Database: dbName,
Table: "table4",
Schema: "(id UInt64, Col1 String, Col2 String, Col3 String, Col4 String, Col5 String) ENGINE = MergeTree PARTITION BY id ORDER BY (id, Col1, Col2, Col3, Col4, Col5) SETTINGS index_granularity = 8192",
Rows: func() []map[string]interface{} {
result := []map[string]interface{}{}
for i := 200; i < 220; i++ {
result = append(result, map[string]interface{}{"id": uint64(i), "Col1": "Text1", "Col2": "Text2", "Col3": "Text3", "Col4": "Text4", "Col5": "Text5"})
}
return result
}(),
Fields: []string{"id", "Col1", "Col2", "Col3", "Col4", "Col5"},
OrderBy: "id",
},
}
func TestRestoreLegacyBackupFormat(t *testing.T) {
ch := &ClickHouse{
Config: &ClickHouseConfig{
Host: "localhost",
Port: 9000,
},
}
r := require.New(t)
r.NoError(ch.Connect())
r.NoError(ch.dropDatabase(dbName))
fmt.Println("Generate test data")
for _, data := range testData {
r.NoError(ch.createTestData(data))
}
time.Sleep(time.Second * 5)
fmt.Println("Create backup")
r.NoError(dockerExec("clickhouse-backup", "freeze"))
dockerExec("mkdir", "-p", "/var/lib/clickhouse/backup/old_format")
r.NoError(dockerExec("cp", "-r", "/var/lib/clickhouse/metadata", "/var/lib/clickhouse/backup/old_format/"))
r.NoError(dockerExec("mv", "/var/lib/clickhouse/shadow", "/var/lib/clickhouse/backup/old_format/"))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup/old_format/")
fmt.Println("Upload")
r.NoError(dockerExec("clickhouse-backup", "upload", "old_format"))
fmt.Println("Create backup")
r.NoError(dockerExec("clickhouse-backup", "create", "increment_old_format"))
fmt.Println("Upload increment")
r.Error(dockerExec("clickhouse-backup", "upload", "increment_old_format", "--diff-from", "old_format"))
fmt.Println("Drop database")
r.NoError(ch.dropDatabase(dbName))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Delete backup")
r.NoError(dockerExec("/bin/rm", "-rf", "/var/lib/clickhouse/backup/old_format"))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Download")
r.NoError(dockerExec("clickhouse-backup", "download", "old_format"))
fmt.Println("Restore schema")
r.NoError(dockerExec("clickhouse-backup", "restore-schema", "old_format"))
fmt.Println("Restore data")
r.NoError(dockerExec("clickhouse-backup", "restore-data", "old_format"))
fmt.Println("Check data")
for i := range testData {
r.NoError(ch.checkData(t, testData[i]))
}
}
func TestIntegration(t *testing.T) {
ch := &ClickHouse{
Config: &ClickHouseConfig{
Host: "localhost",
Port: 9000,
},
}
r := require.New(t)
r.NoError(ch.Connect())
r.NoError(ch.dropDatabase(dbName))
fmt.Println("Generate test data")
for _, data := range testData {
r.NoError(ch.createTestData(data))
}
time.Sleep(time.Second * 5)
fmt.Println("Create backup")
r.NoError(dockerExec("clickhouse-backup", "create", "test_backup"))
fmt.Println("Generate increment test data")
for _, data := range incrementData {
r.NoError(ch.createTestData(data))
}
time.Sleep(time.Second * 5)
r.NoError(dockerExec("clickhouse-backup", "create", "increment"))
fmt.Println("Upload")
r.NoError(dockerExec("clickhouse-backup", "upload", "test_backup"))
r.NoError(dockerExec("clickhouse-backup", "upload", "increment", "--diff-from", "test_backup"))
fmt.Println("Drop database")
r.NoError(ch.dropDatabase(dbName))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Delete backup")
r.NoError(dockerExec("/bin/rm", "-rf", "/var/lib/clickhouse/backup/test_backup", "/var/lib/clickhouse/backup/increment"))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Download")
r.NoError(dockerExec("clickhouse-backup", "download", "test_backup"))
fmt.Println("Restore schema")
r.NoError(dockerExec("clickhouse-backup", "restore-schema", "test_backup"))
fmt.Println("Restore data")
r.NoError(dockerExec("clickhouse-backup", "restore-data", "test_backup"))
fmt.Println("Check data")
for i := range testData {
r.NoError(ch.checkData(t, testData[i]))
}
// test increment
fmt.Println("Drop database")
r.NoError(ch.dropDatabase(dbName))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Delete backup")
r.NoError(dockerExec("/bin/rm", "-rf", "/var/lib/clickhouse/backup/test_backup", "/var/lib/clickhouse/backup/increment"))
dockerExec("ls", "-lha", "/var/lib/clickhouse/backup")
fmt.Println("Download increment")
r.NoError(dockerExec("clickhouse-backup", "download", "increment"))
fmt.Println("Restore schema")
r.NoError(dockerExec("clickhouse-backup", "restore-schema", "increment"))
fmt.Println("Restore data")
r.NoError(dockerExec("clickhouse-backup", "restore-data", "increment"))
fmt.Println("Check increment data")
for i := range testData {
ti := testData[i]
ti.Rows = append(ti.Rows, incrementData[i].Rows...)
r.NoError(ch.checkData(t, ti))
}
}
func (ch *ClickHouse) createTestData(data TestDataStuct) error {
if err := ch.CreateDatabase(data.Database); err != nil {
return err
}
if err := ch.CreateTable(RestoreTable{
Database: data.Database,
Table: data.Table,
Query: fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%s`.`%s` %s", data.Database, data.Table, data.Schema),
}); err != nil {
return err
}
for _, row := range data.Rows {
tx, err := ch.conn.Beginx()
if err != nil {
return fmt.Errorf("can't begin transaction with: %v", err)
}
if _, err := tx.NamedExec(
fmt.Sprintf("INSERT INTO `%s`.`%s` (%s) VALUES (:%s)",
data.Database,
data.Table,
strings.Join(data.Fields, ","),
strings.Join(data.Fields, ",:"),
), row); err != nil {
return fmt.Errorf("can't add insert to transaction with: %v", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("can't commit with: %v", err)
}
}
return nil
}
func (ch *ClickHouse) dropDatabase(database string) error {
fmt.Println("DROP DATABASE IF EXISTS ", database)
_, err := ch.conn.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", database))
return err
}
func (ch *ClickHouse) checkData(t *testing.T, data TestDataStuct) error {
fmt.Printf("Check '%d' rows in '%s.%s'\n", len(data.Rows), data.Database, data.Table)
rows, err := ch.conn.Queryx(fmt.Sprintf("SELECT * FROM `%s`.`%s` ORDER BY %s", data.Database, data.Table, data.OrderBy))
if err != nil {
return err
}
result := []map[string]interface{}{}
for rows.Next() {
row := map[string]interface{}{}
if rows.MapScan(row) != nil {
return err
}
result = append(result, row)
}
assert.Equal(t, len(data.Rows), len(result))
for i := range data.Rows {
assert.EqualValues(t, data.Rows[i], result[i])
}
return nil
}
func dockerExec(cmd ...string) error {
out, err := dockerExecOut(cmd...)
fmt.Println(string(out))
return err
}
func dockerExecOut(cmd ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
dcmd := []string{"exec", "clickhouse"}
dcmd = append(dcmd, cmd...)
out, err := exec.CommandContext(ctx, "docker", dcmd...).CombinedOutput()
cancel()
return string(out), err
}
func toDate(s string) time.Time {
result, _ := time.Parse("2006-01-02", s)
return result
}
func toTS(s string) time.Time {
result, _ := time.Parse("2006-01-02 15:04:05", s)
return result
}