Skip to content

Commit 9af3a07

Browse files
Merge pull request #1458 from github/arthur/use-testcontainers
Use testcontainers to spawn MySQL server container in unit tests. #1457
2 parents 00f450d + b34b86d commit 9af3a07

File tree

1,278 files changed

+204698
-4259
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,278 files changed

+204698
-4259
lines changed

go.mod

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,63 @@ require (
99
github.com/google/uuid v1.6.0
1010
github.com/openark/golib v0.0.0-20210531070646-355f37940af8
1111
github.com/stretchr/testify v1.9.0
12+
github.com/testcontainers/testcontainers-go v0.34.0
1213
golang.org/x/net v0.24.0
1314
golang.org/x/term v0.19.0
1415
golang.org/x/text v0.14.0
1516
)
1617

1718
require (
19+
dario.cat/mergo v1.0.0 // indirect
1820
filippo.io/edwards25519 v1.1.0 // indirect
21+
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
22+
github.com/Microsoft/go-winio v0.6.2 // indirect
23+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
24+
github.com/containerd/containerd v1.7.18 // indirect
25+
github.com/containerd/log v0.1.0 // indirect
26+
github.com/containerd/platforms v0.2.1 // indirect
27+
github.com/cpuguy83/dockercfg v0.3.2 // indirect
1928
github.com/davecgh/go-spew v1.1.1 // indirect
29+
github.com/distribution/reference v0.6.0 // indirect
30+
github.com/docker/docker v27.1.1+incompatible // indirect
31+
github.com/docker/go-connections v0.5.0 // indirect
32+
github.com/docker/go-units v0.5.0 // indirect
33+
github.com/felixge/httpsnoop v1.0.4 // indirect
34+
github.com/go-logr/logr v1.4.1 // indirect
35+
github.com/go-logr/stdr v1.2.2 // indirect
36+
github.com/go-ole/go-ole v1.2.6 // indirect
37+
github.com/gogo/protobuf v1.3.2 // indirect
38+
github.com/klauspost/compress v1.17.4 // indirect
39+
github.com/kr/text v0.2.0 // indirect
40+
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
41+
github.com/magiconair/properties v1.8.7 // indirect
42+
github.com/moby/docker-image-spec v1.3.1 // indirect
43+
github.com/moby/patternmatcher v0.6.0 // indirect
44+
github.com/moby/sys/sequential v0.5.0 // indirect
45+
github.com/moby/sys/user v0.1.0 // indirect
46+
github.com/moby/term v0.5.0 // indirect
47+
github.com/morikuni/aec v1.0.0 // indirect
48+
github.com/opencontainers/go-digest v1.0.0 // indirect
49+
github.com/opencontainers/image-spec v1.1.0 // indirect
2050
github.com/pingcap/errors v0.11.5-0.20210425183316-da1aaba5fb63 // indirect
51+
github.com/pkg/errors v0.9.1 // indirect
2152
github.com/pmezard/go-difflib v1.0.0 // indirect
53+
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
54+
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
55+
github.com/shoenig/go-m1cpu v0.1.6 // indirect
2256
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 // indirect
2357
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726 // indirect
2458
github.com/siddontang/go-log v0.0.0-20180807004314-8d05993dda07 // indirect
59+
github.com/sirupsen/logrus v1.9.3 // indirect
60+
github.com/tklauser/go-sysconf v0.3.12 // indirect
61+
github.com/tklauser/numcpus v0.6.1 // indirect
62+
github.com/yusufpapurcu/wmi v1.2.3 // indirect
63+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
64+
go.opentelemetry.io/otel v1.24.0 // indirect
65+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
66+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
2567
go.uber.org/atomic v1.7.0 // indirect
26-
golang.org/x/sys v0.19.0 // indirect
68+
golang.org/x/crypto v0.22.0 // indirect
69+
golang.org/x/sys v0.21.0 // indirect
2770
gopkg.in/yaml.v3 v3.0.1 // indirect
2871
)

go.sum

Lines changed: 148 additions & 5 deletions
Large diffs are not rendered by default.

go/logic/applier_test.go

Lines changed: 185 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,181 @@ 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+
suite.Require().NoError(rows.Err())
331+
332+
suite.Require().Equal(1, count)
333+
suite.Require().Equal(123456, id)
334+
suite.Require().Equal(42, item_id)
335+
336+
suite.Require().Equal(int64(1), migrationContext.TotalDMLEventsApplied)
337+
suite.Require().Equal(int64(0), migrationContext.RowsDeltaEstimate)
338+
}
339+
340+
func (suite *ApplierTestSuite) TestValidateOrDropExistingTables() {
341+
ctx := context.Background()
342+
343+
host, err := suite.mysqlContainer.ContainerIP(ctx)
344+
suite.Require().NoError(err)
345+
346+
migrationContext := base.NewMigrationContext()
347+
migrationContext.ApplierConnectionConfig = mysql.NewConnectionConfig()
348+
migrationContext.ApplierConnectionConfig.Key.Hostname = host
349+
migrationContext.ApplierConnectionConfig.Key.Port = 3306
350+
migrationContext.ApplierConnectionConfig.User = "root"
351+
migrationContext.ApplierConnectionConfig.Password = "root-password"
352+
migrationContext.DatabaseName = "test"
353+
migrationContext.OriginalTableName = "testing"
354+
migrationContext.SetConnectionConfig("innodb")
355+
356+
migrationContext.OriginalTableColumns = sql.NewColumnList([]string{"id", "item_id"})
357+
migrationContext.SharedColumns = sql.NewColumnList([]string{"id", "item_id"})
358+
migrationContext.MappedSharedColumns = sql.NewColumnList([]string{"id", "item_id"})
359+
360+
applier := NewApplier(migrationContext)
361+
defer applier.Teardown()
362+
363+
err = applier.InitDBConnections()
364+
suite.Require().NoError(err)
365+
366+
err = applier.ValidateOrDropExistingTables()
367+
suite.Require().NoError(err)
368+
}
369+
370+
func TestApplier(t *testing.T) {
371+
suite.Run(t, new(ApplierTestSuite))
372+
}
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)