@@ -12,49 +12,81 @@ import (
12
12
"github.com/github/gh-ost/go/binlog"
13
13
"github.com/github/gh-ost/go/mysql"
14
14
"github.com/github/gh-ost/go/sql"
15
- "github.com/stretchr/testify/require "
15
+ "github.com/stretchr/testify/suite "
16
16
"github.com/testcontainers/testcontainers-go"
17
17
"github.com/testcontainers/testcontainers-go/wait"
18
18
)
19
19
20
- func TestCoordinator (t * testing.T ) {
20
+ type CoordinatorTestSuite struct {
21
+ suite.Suite
22
+
23
+ mysqlContainer testcontainers.Container
24
+ db * gosql.DB
25
+ }
26
+
27
+ func (suite * CoordinatorTestSuite ) SetupSuite () {
21
28
ctx := context .Background ()
22
29
req := testcontainers.ContainerRequest {
23
- Image : "mysql:8.0" ,
24
- Env : map [string ]string {"MYSQL_ROOT_PASSWORD" : "root" },
25
- WaitingFor : wait .ForLog ( "port: 3306 MySQL Community Server - GPL " ),
26
- ExposedPorts : []string {"3306" },
30
+ Image : "mysql:8.0.40 " ,
31
+ Env : map [string ]string {"MYSQL_ROOT_PASSWORD" : "root-password " },
32
+ WaitingFor : wait .ForListeningPort ( " 3306/tcp " ),
33
+ ExposedPorts : []string {"3306/tcp " },
27
34
}
28
35
29
36
mysqlContainer , err := testcontainers .GenericContainer (ctx , testcontainers.GenericContainerRequest {
30
37
ContainerRequest : req ,
31
38
Started : true ,
32
39
})
33
- require .NoError (t , err )
34
- t .Cleanup (func () {
35
- ctx := context .Background ()
36
- require .NoError (t , mysqlContainer .Terminate (ctx ))
37
- })
40
+ suite .Require ().NoError (err )
38
41
39
- host , err := mysqlContainer .Host (ctx )
40
- require .NoError (t , err )
42
+ suite .mysqlContainer = mysqlContainer
41
43
42
- mappedPort , err := mysqlContainer . MappedPort (ctx , "3306" )
43
- require . NoError (t , err )
44
+ dsn , err := GetDSN (ctx , mysqlContainer )
45
+ suite . Require (). NoError (err )
44
46
45
- db , err := gosql .Open ("mysql" , "root:root@tcp(" + host + ":" + mappedPort . Port () + ")/" )
46
- require . NoError (t , err )
47
+ db , err := gosql .Open ("mysql" , dsn )
48
+ suite . Require (). NoError (err )
47
49
48
- t .Cleanup (func () {
49
- require .NoError (t , db .Close ())
50
- })
50
+ suite .db = db
51
+ }
52
+
53
+ func (suite * CoordinatorTestSuite ) SetupTest () {
54
+ ctx := context .Background ()
55
+ _ , err := suite .db .ExecContext (ctx , "RESET MASTER" )
56
+ suite .Require ().NoError (err )
57
+
58
+ _ , err = suite .db .ExecContext (ctx , "SET @@GLOBAL.binlog_transaction_dependency_tracking = WRITESET" )
59
+ suite .Require ().NoError (err )
60
+
61
+ _ , err = suite .db .ExecContext (ctx , "CREATE DATABASE test" )
62
+ suite .Require ().NoError (err )
63
+ }
64
+
65
+ func (suite * CoordinatorTestSuite ) TearDownTest () {
66
+ ctx := context .Background ()
67
+ _ , err := suite .db .ExecContext (ctx , "DROP DATABASE test" )
68
+ suite .Require ().NoError (err )
69
+ }
70
+
71
+ func (suite * CoordinatorTestSuite ) TeardownSuite () {
72
+ ctx := context .Background ()
73
+
74
+ suite .Assert ().NoError (suite .db .Close ())
75
+ suite .Assert ().NoError (suite .mysqlContainer .Terminate (ctx ))
76
+ }
77
+
78
+ func (suite * CoordinatorTestSuite ) TestApplyDML () {
79
+ ctx := context .Background ()
80
+
81
+ connectionConfig , err := GetConnectionConfig (ctx , suite .mysqlContainer )
51
82
52
83
_ = os .Remove ("/tmp/gh-ost.sock" )
53
84
54
- //prepareDatabase(t, db)
85
+ _ , err = suite .db .Exec ("CREATE TABLE test.gh_ost_test (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255)) ENGINE=InnoDB" )
86
+ suite .Require ().NoError (err )
55
87
56
- _ , err = db .Exec ("CREATE TABLE test._gh_ost_test_gho (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))" )
57
- require . NoError (t , err )
88
+ _ , err = suite . db .Exec ("CREATE TABLE test._gh_ost_test_gho (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(255))" )
89
+ suite . Require (). NoError (err )
58
90
59
91
migrationContext := base .NewMigrationContext ()
60
92
migrationContext .DatabaseName = "test"
@@ -67,23 +99,8 @@ func TestCoordinator(t *testing.T) {
67
99
migrationContext .ThrottleHTTPIntervalMillis = 100
68
100
migrationContext .DMLBatchSize = 10
69
101
70
- migrationContext .ApplierConnectionConfig = & mysql.ConnectionConfig {
71
- Key : mysql.InstanceKey {
72
- Hostname : host ,
73
- Port : mappedPort .Int (),
74
- },
75
- User : "root" ,
76
- Password : "root" ,
77
- }
78
-
79
- migrationContext .InspectorConnectionConfig = & mysql.ConnectionConfig {
80
- Key : mysql.InstanceKey {
81
- Hostname : host ,
82
- Port : mappedPort .Int (),
83
- },
84
- User : "root" ,
85
- Password : "root" ,
86
- }
102
+ migrationContext .ApplierConnectionConfig = connectionConfig
103
+ migrationContext .InspectorConnectionConfig = connectionConfig
87
104
88
105
migrationContext .OriginalTableColumns = sql .NewColumnList ([]string {"id" , "name" })
89
106
migrationContext .GhostTableColumns = sql .NewColumnList ([]string {"id" , "name" })
@@ -96,41 +113,41 @@ func TestCoordinator(t *testing.T) {
96
113
}
97
114
98
115
migrationContext .SetConnectionConfig ("innodb" )
116
+ migrationContext .SkipPortValidation = true
99
117
migrationContext .NumWorkers = 4
100
- // HACK: so
101
- migrationContext .AzureMySQL = true
102
118
103
119
applier := NewApplier (migrationContext )
104
120
err = applier .InitDBConnections (migrationContext .NumWorkers )
105
- require . NoError (t , err )
121
+ suite . Require (). NoError (err )
106
122
107
123
err = applier .prepareQueries ()
108
- require . NoError (t , err )
124
+ suite . Require (). NoError (err )
109
125
110
126
err = applier .CreateChangelogTable ()
111
- require . NoError (t , err )
127
+ suite . Require (). NoError (err )
112
128
129
+ // TODO: use errgroup
113
130
for i := 0 ; i < 100 ; i ++ {
114
- tx , err := db .Begin ()
115
- require . NoError (t , err )
131
+ tx , err := suite . db .Begin ()
132
+ suite . Require (). NoError (err )
116
133
117
134
for j := 0 ; j < 100 ; j ++ {
118
135
_ , err = tx .Exec ("INSERT INTO test.gh_ost_test (name) VALUES ('test')" )
119
- require . NoError (t , err )
136
+ suite . Require (). NoError (err )
120
137
}
121
138
122
139
err = tx .Commit ()
123
- require . NoError (t , err )
140
+ suite . Require (). NoError (err )
124
141
}
125
142
126
- _ , err = db .Exec ("UPDATE test.gh_ost_test SET name = 'foobar' WHERE id = 1" )
127
- require . NoError (t , err )
143
+ _ , err = suite . db .Exec ("UPDATE test.gh_ost_test SET name = 'foobar' WHERE id = 1" )
144
+ suite . Require (). NoError (err )
128
145
129
- _ , err = db .Exec ("INSERT INTO test.gh_ost_test (name) VALUES ('test')" )
130
- require . NoError (t , err )
146
+ _ , err = suite . db .Exec ("INSERT INTO test.gh_ost_test (name) VALUES ('test')" )
147
+ suite . Require (). NoError (err )
131
148
132
149
_ , err = applier .WriteChangelogState ("completed" )
133
- require . NoError (t , err )
150
+ suite . Require (). NoError (err )
134
151
135
152
ctx , cancel := context .WithCancel (context .Background ())
136
153
@@ -148,15 +165,15 @@ func TestCoordinator(t *testing.T) {
148
165
LogFile : "binlog.000001" ,
149
166
LogPos : int64 (4 ),
150
167
}
151
- coord .InitializeWorkers (8 )
168
+ coord .InitializeWorkers (4 )
152
169
153
170
streamCtx , cancelStreaming := context .WithCancel (context .Background ())
154
171
canStopStreaming := func () bool {
155
172
return streamCtx .Err () != nil
156
173
}
157
174
go func () {
158
175
err = coord .StartStreaming (streamCtx , canStopStreaming )
159
- require . Equal (t , context .Canceled , err )
176
+ suite . Require (). Equal (context .Canceled , err )
160
177
}()
161
178
162
179
// Give streamer some time to start
@@ -171,8 +188,12 @@ func TestCoordinator(t *testing.T) {
171
188
}
172
189
173
190
err = coord .ProcessEventsUntilDrained ()
174
- require . NoError (t , err )
191
+ suite . Require (). NoError (err )
175
192
}
176
193
177
194
fmt .Printf ("Time taken: %s\n " , time .Since (startAt ))
178
195
}
196
+
197
+ func TestCoordinator (t * testing.T ) {
198
+ suite .Run (t , new (CoordinatorTestSuite ))
199
+ }
0 commit comments