-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathend_to_end_suite_test.go
466 lines (391 loc) · 16.3 KB
/
end_to_end_suite_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package end_to_end_test
import (
"fmt"
"os"
"os/exec"
path "path/filepath"
"strconv"
"strings"
"testing"
"time"
"github.com/cloudberrydb/cbcopy/internal/testhelper"
"github.com/cloudberrydb/cbcopy/internal/dbconn"
"github.com/cloudberrydb/cbcopy/testutils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/format"
)
var (
testFailure bool
sourceConn *dbconn.DBConn
destConn *dbconn.DBConn
cbcopyPath string
)
func cbcopy(cbcopyPath string, args ...string) []byte {
args = append([]string{"--verbose"}, args...)
command := exec.Command(cbcopyPath, args...)
return mustRunCommand(command)
}
func assertDataRestored(conn *dbconn.DBConn, tableToTupleCount map[string]int) {
for tableName, expectedNumTuples := range tableToTupleCount {
actualTupleCount := dbconn.MustSelectString(conn, fmt.Sprintf("SELECT count(*) AS string FROM %s", tableName))
if strconv.Itoa(expectedNumTuples) != actualTupleCount {
Fail(fmt.Sprintf("Expected:\n\t%s rows to have been restored into table %s\nActual:\n\t%s rows were restored", strconv.Itoa(expectedNumTuples), tableName, actualTupleCount))
}
}
}
func checkTableExists(conn *dbconn.DBConn, tableName string) bool {
var schema, table string
s := strings.Split(tableName, ".")
if len(s) == 2 {
schema, table = s[0], s[1]
} else if len(s) == 1 {
schema = "public"
table = s[0]
} else {
Fail(fmt.Sprintf("Table %s is not in a valid format", tableName))
}
exists := dbconn.MustSelectString(conn, fmt.Sprintf("SELECT EXISTS (SELECT * FROM pg_tables WHERE schemaname = '%s' AND tablename = '%s') AS string", schema, table))
return (exists == "true")
}
func assertTablesRestored(conn *dbconn.DBConn, tables []string) {
for _, tableName := range tables {
if !checkTableExists(conn, tableName) {
Fail(fmt.Sprintf("Table %s does not exist when it should", tableName))
}
}
}
func mustRunCommand(cmd *exec.Cmd) []byte {
output, err := cmd.CombinedOutput()
if err != nil {
testFailure = true
fmt.Printf("%s", output)
Fail(fmt.Sprintf("%v", err))
}
return output
}
func TestEndToEnd(t *testing.T) {
format.MaxLength = 0
RegisterFailHandler(Fail)
RunSpecs(t, "EndToEnd Suite")
}
var _ = BeforeSuite(func() {
testhelper.SetupTestLogger()
sourceConn = testutils.SetupTestDbConn("postgres")
destConn = testutils.SetupTestDbConn("postgres")
// default GUC setting varies between versions so set it explicitly
testhelper.AssertQueryRuns(sourceConn, "SET gp_autostats_mode='on_no_stats'")
// Create source and target databases
testhelper.AssertQueryRuns(sourceConn, "DROP DATABASE IF EXISTS source_db")
testhelper.AssertQueryRuns(sourceConn, "CREATE DATABASE source_db")
testhelper.AssertQueryRuns(destConn, "DROP DATABASE IF EXISTS target_db")
testhelper.AssertQueryRuns(destConn, "CREATE DATABASE target_db")
testutils.SetupTestTablespace(sourceConn, "/tmp/e2e_test_same_tablespace")
testutils.SetupTestTablespace(sourceConn, "/tmp/e2e_test_source_tablespace")
testutils.SetupTestTablespace(sourceConn, "/tmp/e2e_test_dest_tablespace")
projectRoot, err := os.Getwd()
if err != nil {
Fail(fmt.Sprintf("Could not get current directory: %v", err))
}
projectRoot = path.Dir(projectRoot)
cbcopyPath = path.Join(projectRoot, "cbcopy")
if _, err := os.Stat(cbcopyPath); err != nil {
Fail(fmt.Sprintf("cbcopy binary not found at %s: %v", cbcopyPath, err))
}
})
var _ = AfterSuite(func() {
if testFailure {
return
}
testutils.CleanupTestTablespace(sourceConn, "/tmp/e2e_test_same_tablespace")
testutils.CleanupTestTablespace(sourceConn, "/tmp/e2e_test_source_tablespace")
testutils.CleanupTestTablespace(sourceConn, "/tmp/e2e_test_dest_tablespace")
if sourceConn != nil {
testhelper.AssertQueryRuns(sourceConn, "DROP DATABASE IF EXISTS source_db")
sourceConn.Close()
}
if destConn != nil {
testhelper.AssertQueryRuns(destConn, "DROP DATABASE IF EXISTS target_db")
destConn.Close()
}
})
func end_to_end_setup() {
}
func end_to_end_teardown() {
}
var _ = Describe("Migration basic tests", func() {
BeforeEach(func() {
end_to_end_setup()
})
AfterEach(func() {
end_to_end_teardown()
})
It("runs cbcopy with --dbname mode", func() {
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
defer func() {
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.test_table1")
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.test_table2")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.test_table1")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.test_table2")
srcTestConn.Close()
destTestConn.Close()
}()
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.test_table1 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.test_table2 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.test_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.test_table2 SELECT generate_series(1,200)")
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--dbname", "source_db",
"--dest-dbname", "target_db",
"--truncate")
assertTablesRestored(destTestConn, []string{
"public.test_table1",
"public.test_table2",
})
assertDataRestored(destTestConn, map[string]int{
"public.test_table1": 100,
"public.test_table2": 200,
})
})
It("runs cbcopy with --schema mode", func() {
// Connect to source and target databases
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function to drop schemas and databases
defer func() {
// Drop source schema in source database
testhelper.AssertQueryRuns(srcTestConn, "DROP SCHEMA IF EXISTS source_schema CASCADE")
// Drop target schema in target database
testhelper.AssertQueryRuns(destTestConn, "DROP SCHEMA IF EXISTS target_schema CASCADE")
// Close database connections
srcTestConn.Close()
destTestConn.Close()
}()
// Create source schema and test tables in source database
testhelper.AssertQueryRuns(srcTestConn, "CREATE SCHEMA source_schema")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table1 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table2 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table2 SELECT generate_series(1,200)")
// Execute schema migration from source_schema to target_schema
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--schema", fmt.Sprintf("%s.source_schema", "source_db"),
"--dest-schema", fmt.Sprintf("%s.target_schema", "target_db"),
"--truncate")
assertTablesRestored(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
})
assertDataRestored(destTestConn, map[string]int{
"target_schema.test_table1": 100,
"target_schema.test_table2": 200,
})
})
It("runs cbcopy with --schema-mapping-file mode", func() {
// Connect to source and target databases
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function to drop schemas and databases
defer func() {
// Drop source schema in source database
testhelper.AssertQueryRuns(srcTestConn, "DROP SCHEMA IF EXISTS source_schema CASCADE")
// Drop target schema in target database
testhelper.AssertQueryRuns(destTestConn, "DROP SCHEMA IF EXISTS target_schema CASCADE")
// Close database connections
srcTestConn.Close()
destTestConn.Close()
}()
// Create source schema and test tables in source database
testhelper.AssertQueryRuns(srcTestConn, "CREATE SCHEMA source_schema")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table1 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE source_schema.test_table2 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO source_schema.test_table2 SELECT generate_series(1,200)")
// Create schema mapping file
mappingFile := "/tmp/schema_mapping.txt"
content := "source_db.source_schema,target_db.target_schema"
err := os.WriteFile(mappingFile, []byte(content), 0644)
Expect(err).NotTo(HaveOccurred())
defer os.Remove(mappingFile)
// Execute schema migration using mapping file
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--schema-mapping-file", mappingFile,
"--truncate")
// Verify tables and data were migrated to target schema
assertTablesRestored(destTestConn, []string{
"target_schema.test_table1",
"target_schema.test_table2",
})
assertDataRestored(destTestConn, map[string]int{
"target_schema.test_table1": 100,
"target_schema.test_table2": 200,
})
})
It("runs cbcopy with --include-table mode", func() {
// Set up test connections
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function
defer func() {
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.include_table1")
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.include_table2")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.include_table1")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.include_table2")
srcTestConn.Close()
destTestConn.Close()
}()
// Create test tables and insert data
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.include_table1 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.include_table2 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.include_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.include_table2 SELECT generate_series(1,200)")
testhelper.AssertQueryRuns(destTestConn, "CREATE TABLE public.include_table1 (i int)")
testhelper.AssertQueryRuns(destTestConn, "CREATE TABLE public.include_table2 (i int)")
// Execute migration
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--include-table", fmt.Sprintf("%s.public.include_table1,%s.public.include_table2", "source_db", "source_db"),
"--dest-table", "target_db.public.include_table1,target_db.public.include_table2",
"--truncate")
// Verify results
assertTablesRestored(destTestConn, []string{
"public.include_table1",
"public.include_table2",
})
assertDataRestored(destTestConn, map[string]int{
"public.include_table1": 100,
"public.include_table2": 200,
})
})
It("runs cbcopy with --include-table-file mode", func() {
// Set up test connections
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Cleanup function
defer func() {
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.include_table1")
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.include_table2")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.include_table1")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.include_table2")
srcTestConn.Close()
destTestConn.Close()
}()
// Create test tables and insert data
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.include_table1 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.include_table2 (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.include_table1 SELECT generate_series(1,100)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.include_table2 SELECT generate_series(1,200)")
// Create include table file
tableFile := "/tmp/include_tables.txt"
content := "source_db.public.include_table1\nsource_db.public.include_table2"
err := os.WriteFile(tableFile, []byte(content), 0644)
Expect(err).NotTo(HaveOccurred())
defer os.Remove(tableFile)
// Execute migration
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--include-table-file", tableFile,
"--dest-dbname", "target_db",
"--truncate")
// Verify results
assertTablesRestored(destTestConn, []string{
"public.include_table1",
"public.include_table2",
})
assertDataRestored(destTestConn, map[string]int{
"public.include_table1": 100,
"public.include_table2": 200,
})
})
It("runs cbcopy with --append mode", func() {
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
defer func() {
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.test_table")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.test_table")
srcTestConn.Close()
destTestConn.Close()
}()
// Create and populate source table
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.test_table (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.test_table SELECT generate_series(1,100)")
// Create and populate destination table with different data
testhelper.AssertQueryRuns(destTestConn, "CREATE TABLE public.test_table (i int)")
testhelper.AssertQueryRuns(destTestConn, "INSERT INTO public.test_table SELECT generate_series(101,200)")
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--include-table", fmt.Sprintf("%s.public.test_table", "source_db"),
"--dest-table", "target_db.public.test_table",
"--append")
assertDataRestored(destTestConn, map[string]int{
"public.test_table": 200, // 100 original rows + 100 appended rows
})
})
It("runs cbcopy with different copy strategies", func() {
strategies := []string{"CopyOnMaster", "CopyOnSegment", "ExtDestGeCopy", "ExtDestLtCopy"}
for _, strategy := range strategies {
By(fmt.Sprintf("Testing with %s strategy", strategy))
srcTestConn := testutils.SetupTestDbConn("source_db")
destTestConn := testutils.SetupTestDbConn("target_db")
// Create and populate source table
testhelper.AssertQueryRuns(srcTestConn, "CREATE TABLE public.test_table (i int)")
testhelper.AssertQueryRuns(srcTestConn, "INSERT INTO public.test_table SELECT generate_series(1,1000)")
testhelper.AssertQueryRuns(destTestConn, "CREATE TABLE public.test_table (i int)")
// Set environment variable for testing specific strategy
os.Setenv("TEST_COPY_STRATEGY", strategy)
defer os.Unsetenv("TEST_COPY_STRATEGY")
time.Sleep(1 * time.Second)
cbcopy(cbcopyPath,
"--source-host", sourceConn.Host,
"--source-port", strconv.Itoa(sourceConn.Port),
"--source-user", sourceConn.User,
"--dest-host", destConn.Host,
"--dest-port", strconv.Itoa(destConn.Port),
"--dest-user", destConn.User,
"--include-table", fmt.Sprintf("%s.public.test_table", "source_db"),
"--dest-table", "target_db.public.test_table",
"--truncate")
assertDataRestored(destTestConn, map[string]int{
"public.test_table": 1000,
})
testhelper.AssertQueryRuns(srcTestConn, "DROP TABLE IF EXISTS public.test_table")
testhelper.AssertQueryRuns(destTestConn, "DROP TABLE IF EXISTS public.test_table")
srcTestConn.Close()
destTestConn.Close()
}
})
})