Skip to content

Commit 07749f7

Browse files
Use testcontainers to spawn MySQL server container in unit tests.
1 parent 96f841a commit 07749f7

File tree

1,276 files changed

+204505
-4253
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,276 files changed

+204505
-4253
lines changed

go/logic/applier_test.go

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
package logic
77

88
import (
9+
"context"
10+
gosql "database/sql"
911
"strings"
1012
"testing"
1113

1214
"github.com/stretchr/testify/require"
15+
"github.com/stretchr/testify/suite"
16+
17+
"github.com/testcontainers/testcontainers-go"
18+
"github.com/testcontainers/testcontainers-go/wait"
1319

1420
"github.com/github/gh-ost/go/base"
1521
"github.com/github/gh-ost/go/binlog"
22+
"github.com/github/gh-ost/go/mysql"
1623
"github.com/github/gh-ost/go/sql"
1724
)
1825

@@ -185,3 +192,180 @@ func TestApplierInstantDDL(t *testing.T) {
185192
require.Equal(t, "ALTER /* gh-ost */ TABLE `test`.`mytable` ADD INDEX (foo), ALGORITHM=INSTANT", stmt)
186193
})
187194
}
195+
196+
type ApplierTestSuite struct {
197+
suite.Suite
198+
199+
mysqlContainer testcontainers.Container
200+
}
201+
202+
func (suite *ApplierTestSuite) SetupSuite() {
203+
ctx := context.Background()
204+
req := testcontainers.ContainerRequest{
205+
Image: "mysql:8.0",
206+
Env: map[string]string{"MYSQL_ROOT_PASSWORD": "root-password"},
207+
WaitingFor: wait.ForLog("port: 3306 MySQL Community Server - GPL"),
208+
}
209+
210+
mysqlContainer, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
211+
ContainerRequest: req,
212+
Started: true,
213+
})
214+
suite.Require().NoError(err)
215+
216+
suite.mysqlContainer = mysqlContainer
217+
}
218+
219+
func (suite *ApplierTestSuite) TeardownSuite() {
220+
ctx := context.Background()
221+
222+
suite.Require().NoError(suite.mysqlContainer.Terminate(ctx))
223+
}
224+
225+
func (suite *ApplierTestSuite) SetupTest() {
226+
ctx := context.Background()
227+
228+
rc, _, err := suite.mysqlContainer.Exec(ctx, []string{"mysql", "-uroot", "-proot-password", "-e", "CREATE DATABASE test;"})
229+
suite.Require().NoError(err)
230+
suite.Require().Equalf(0, rc, "failed to created database: expected exit code 0, got %d", rc)
231+
232+
rc, _, err = suite.mysqlContainer.Exec(ctx, []string{"mysql", "-uroot", "-proot-password", "-e", "CREATE TABLE test.testing (id INT, item_id INT);"})
233+
suite.Require().NoError(err)
234+
suite.Require().Equalf(0, rc, "failed to created table: expected exit code 0, got %d", rc)
235+
}
236+
237+
func (suite *ApplierTestSuite) TearDownTest() {
238+
ctx := context.Background()
239+
240+
rc, _, err := suite.mysqlContainer.Exec(ctx, []string{"mysql", "-uroot", "-proot-password", "-e", "DROP DATABASE test;"})
241+
suite.Require().NoError(err)
242+
suite.Require().Equalf(0, rc, "failed to created database: expected exit code 0, got %d", rc)
243+
}
244+
245+
func (suite *ApplierTestSuite) TestInitDBConnections() {
246+
ctx := context.Background()
247+
248+
host, err := suite.mysqlContainer.ContainerIP(ctx)
249+
suite.Require().NoError(err)
250+
251+
migrationContext := base.NewMigrationContext()
252+
migrationContext.ApplierConnectionConfig = mysql.NewConnectionConfig()
253+
migrationContext.ApplierConnectionConfig.Key.Hostname = host
254+
migrationContext.ApplierConnectionConfig.Key.Port = 3306
255+
migrationContext.ApplierConnectionConfig.User = "root"
256+
migrationContext.ApplierConnectionConfig.Password = "root-password"
257+
migrationContext.DatabaseName = "test"
258+
migrationContext.OriginalTableName = "testing"
259+
migrationContext.SetConnectionConfig("innodb")
260+
261+
applier := NewApplier(migrationContext)
262+
defer applier.Teardown()
263+
264+
err = applier.InitDBConnections()
265+
suite.Require().NoError(err)
266+
267+
suite.Require().Equal("8.0.40", migrationContext.ApplierMySQLVersion)
268+
suite.Require().Equal(int64(28800), migrationContext.ApplierWaitTimeout)
269+
suite.Require().Equal("SYSTEM", migrationContext.ApplierTimeZone)
270+
271+
suite.Require().Equal(sql.NewColumnList([]string{"id", "item_id"}), migrationContext.OriginalTableColumnsOnApplier)
272+
}
273+
274+
func (suite *ApplierTestSuite) TestApplyDMLEventQueries() {
275+
ctx := context.Background()
276+
277+
host, err := suite.mysqlContainer.ContainerIP(ctx)
278+
suite.Require().NoError(err)
279+
280+
migrationContext := base.NewMigrationContext()
281+
migrationContext.ApplierConnectionConfig = mysql.NewConnectionConfig()
282+
migrationContext.ApplierConnectionConfig.Key.Hostname = host
283+
migrationContext.ApplierConnectionConfig.Key.Port = 3306
284+
migrationContext.ApplierConnectionConfig.User = "root"
285+
migrationContext.ApplierConnectionConfig.Password = "root-password"
286+
migrationContext.DatabaseName = "test"
287+
migrationContext.OriginalTableName = "testing"
288+
migrationContext.SetConnectionConfig("innodb")
289+
290+
migrationContext.OriginalTableColumns = sql.NewColumnList([]string{"id", "item_id"})
291+
migrationContext.SharedColumns = sql.NewColumnList([]string{"id", "item_id"})
292+
migrationContext.MappedSharedColumns = sql.NewColumnList([]string{"id", "item_id"})
293+
294+
applier := NewApplier(migrationContext)
295+
defer applier.Teardown()
296+
297+
err = applier.InitDBConnections()
298+
suite.Require().NoError(err)
299+
300+
rc, _, err := suite.mysqlContainer.Exec(ctx, []string{"mysql", "-uroot", "-proot-password", "-e", "CREATE TABLE test._testing_gho (id INT, item_id INT);"})
301+
suite.Require().NoError(err)
302+
suite.Require().Equalf(0, rc, "failed to created table: expected exit code 0, got %d", rc)
303+
304+
dmlEvents := []*binlog.BinlogDMLEvent{
305+
{
306+
DatabaseName: "test",
307+
TableName: "testing",
308+
DML: binlog.InsertDML,
309+
NewColumnValues: sql.ToColumnValues([]interface{}{123456, 42}),
310+
},
311+
}
312+
err = applier.ApplyDMLEventQueries(dmlEvents)
313+
suite.Require().NoError(err)
314+
315+
// Check that the row was inserted
316+
db, err := gosql.Open("mysql", "root:root-password@tcp("+host+":3306)/test")
317+
suite.Require().NoError(err)
318+
defer db.Close()
319+
320+
rows, err := db.Query("SELECT * FROM test._testing_gho")
321+
suite.Require().NoError(err)
322+
defer rows.Close()
323+
324+
var count, id, item_id int
325+
for rows.Next() {
326+
err = rows.Scan(&id, &item_id)
327+
suite.Require().NoError(err)
328+
count += 1
329+
}
330+
331+
suite.Require().Equal(1, count)
332+
suite.Require().Equal(123456, id)
333+
suite.Require().Equal(42, item_id)
334+
335+
suite.Require().Equal(int64(1), migrationContext.TotalDMLEventsApplied)
336+
suite.Require().Equal(int64(0), migrationContext.RowsDeltaEstimate)
337+
}
338+
339+
func (suite *ApplierTestSuite) TestValidateOrDropExistingTables() {
340+
ctx := context.Background()
341+
342+
host, err := suite.mysqlContainer.ContainerIP(ctx)
343+
suite.Require().NoError(err)
344+
345+
migrationContext := base.NewMigrationContext()
346+
migrationContext.ApplierConnectionConfig = mysql.NewConnectionConfig()
347+
migrationContext.ApplierConnectionConfig.Key.Hostname = host
348+
migrationContext.ApplierConnectionConfig.Key.Port = 3306
349+
migrationContext.ApplierConnectionConfig.User = "root"
350+
migrationContext.ApplierConnectionConfig.Password = "root-password"
351+
migrationContext.DatabaseName = "test"
352+
migrationContext.OriginalTableName = "testing"
353+
migrationContext.SetConnectionConfig("innodb")
354+
355+
migrationContext.OriginalTableColumns = sql.NewColumnList([]string{"id", "item_id"})
356+
migrationContext.SharedColumns = sql.NewColumnList([]string{"id", "item_id"})
357+
migrationContext.MappedSharedColumns = sql.NewColumnList([]string{"id", "item_id"})
358+
359+
applier := NewApplier(migrationContext)
360+
defer applier.Teardown()
361+
362+
err = applier.InitDBConnections()
363+
suite.Require().NoError(err)
364+
365+
err = applier.ValidateOrDropExistingTables()
366+
suite.Require().NoError(err)
367+
}
368+
369+
func TestApplier(t *testing.T) {
370+
suite.Run(t, new(ApplierTestSuite))
371+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version = 1
2+
3+
test_patterns = [
4+
"*_test.go"
5+
]
6+
7+
[[analyzers]]
8+
name = "go"
9+
enabled = true
10+
11+
[analyzers.meta]
12+
import_path = "dario.cat/mergo"

vendor/dario.cat/mergo/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#### joe made this: http://goel.io/joe
2+
3+
#### go ####
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, build with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
17+
.glide/
18+
19+
#### vim ####
20+
# Swap
21+
[._]*.s[a-v][a-z]
22+
[._]*.sw[a-p]
23+
[._]s[a-v][a-z]
24+
[._]sw[a-p]
25+
26+
# Session
27+
Session.vim
28+
29+
# Temporary
30+
.netrwhist
31+
*~
32+
# Auto-generated tag files
33+
tags

vendor/dario.cat/mergo/.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: go
2+
arch:
3+
- amd64
4+
- ppc64le
5+
install:
6+
- go get -t
7+
- go get golang.org/x/tools/cmd/cover
8+
- go get github.com/mattn/goveralls
9+
script:
10+
- go test -race -v ./...
11+
after_script:
12+
- $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

0 commit comments

Comments
 (0)