Skip to content

Commit 9e7980a

Browse files
committed
Allow to run tests in local environment (inside docker) + fix tests for it
1 parent 81966e1 commit 9e7980a

19 files changed

+120
-71
lines changed

Makefile

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ build:
1111
test:
1212
go test --race -timeout 2m ./...
1313

14+
test-local:
15+
docker run --rm -d --network=host --name go-mysql-server \
16+
-e MYSQL_ALLOW_EMPTY_PASSWORD=true \
17+
-e MYSQL_DATABASE=test \
18+
-v $${PWD}/docker/resources/replication.cnf:/etc/mysql/conf.d/replication.cnf \
19+
mysql:5.7
20+
docker/resources/waitfor.sh 127.0.0.1 3306 \
21+
&& go test -race -v -timeout 2m ./... -gocheck.v
22+
docker stop go-mysql-server
23+
1424
fmt:
1525
golangci-lint run --fix
1626

1727
clean:
1828
go clean -i ./...
19-
@rm -rf ./bin
29+
@rm -rf ./bin

canal/canal_test.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package canal
22

33
import (
4-
"flag"
54
"fmt"
65
"testing"
76
"time"
87

9-
"github.com/go-mysql-org/go-mysql/mysql"
10-
"github.com/go-mysql-org/go-mysql/replication"
118
. "github.com/pingcap/check"
129
"github.com/pingcap/errors"
1310
"github.com/pingcap/tidb/parser"
1411
"github.com/siddontang/go-log/log"
15-
)
1612

17-
var testHost = flag.String("host", "127.0.0.1", "MySQL host")
13+
"github.com/go-mysql-org/go-mysql/mysql"
14+
"github.com/go-mysql-org/go-mysql/replication"
15+
"github.com/go-mysql-org/go-mysql/test_util"
16+
)
1817

1918
func Test(t *testing.T) {
2019
TestingT(t)
@@ -38,7 +37,7 @@ const (
3837

3938
func (s *canalTestSuite) SetUpSuite(c *C) {
4039
cfg := NewDefaultConfig()
41-
cfg.Addr = fmt.Sprintf("%s:3306", *testHost)
40+
cfg.Addr = fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
4241
cfg.User = "root"
4342
cfg.HeartbeatPeriod = 200 * time.Millisecond
4443
cfg.ReadTimeout = 300 * time.Millisecond

client/client_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/pingcap/errors"
1010

1111
"github.com/go-mysql-org/go-mysql/mysql"
12+
"github.com/go-mysql-org/go-mysql/test_util"
1213
"github.com/go-mysql-org/go-mysql/test_util/test_keys"
1314
)
1415

@@ -19,7 +20,7 @@ type clientTestSuite struct {
1920

2021
func (s *clientTestSuite) SetUpSuite(c *C) {
2122
var err error
22-
addr := fmt.Sprintf("%s:%s", *testHost, s.port)
23+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
2324
s.c, err = Connect(addr, *testUser, *testPassword, "")
2425
if err != nil {
2526
c.Fatal(err)
@@ -117,7 +118,7 @@ func (s *clientTestSuite) TestConn_SetCapability(c *C) {
117118
func (s *clientTestSuite) TestConn_TLS_Verify(c *C) {
118119
// Verify that the provided tls.Config is used when attempting to connect to mysql.
119120
// An empty tls.Config will result in a connection error.
120-
addr := fmt.Sprintf("%s:%s", *testHost, s.port)
121+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
121122
_, err := Connect(addr, *testUser, *testPassword, *testDB, func(c *Conn) {
122123
c.UseSSL(false)
123124
})
@@ -133,7 +134,7 @@ func (s *clientTestSuite) TestConn_TLS_Verify(c *C) {
133134

134135
func (s *clientTestSuite) TestConn_TLS_Skip_Verify(c *C) {
135136
// An empty tls.Config will result in a connection error but we can configure to skip it.
136-
addr := fmt.Sprintf("%s:%s", *testHost, s.port)
137+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
137138
_, err := Connect(addr, *testUser, *testPassword, *testDB, func(c *Conn) {
138139
c.UseSSL(true)
139140
})
@@ -145,7 +146,7 @@ func (s *clientTestSuite) TestConn_TLS_Certificate(c *C) {
145146
// And if server uses auto-generated certificates, it will be an error like:
146147
// "x509: certificate is valid for MySQL_Server_8.0.12_Auto_Generated_Server_Certificate, not not-a-valid-name"
147148
tlsConfig := NewClientTLSConfig(test_keys.CaPem, test_keys.CertPem, test_keys.KeyPem, false, "not-a-valid-name")
148-
addr := fmt.Sprintf("%s:%s", *testHost, s.port)
149+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
149150
_, err := Connect(addr, *testUser, *testPassword, *testDB, func(c *Conn) {
150151
c.SetTLSConfig(tlsConfig)
151152
})

client/common_test.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@ import (
66
"testing"
77

88
. "github.com/pingcap/check"
9-
)
109

11-
var testHost = flag.String("host", "127.0.0.1", "MySQL server host")
10+
"github.com/go-mysql-org/go-mysql/test_util"
11+
)
1212

13-
// We cover the whole range of MySQL server versions using docker-compose to bind them to different ports for testing.
14-
// MySQL is constantly updating auth plugin to make it secure:
15-
// starting from MySQL 8.0.4, a new auth plugin is introduced, causing plain password auth to fail with error:
16-
// ERROR 1251 (08004): Client does not support authentication protocol requested by server; consider upgrading MySQL client
17-
// Hint: use docker-compose to start corresponding MySQL docker containers and add the their ports here
18-
var testPort = flag.String("port", "3306", "MySQL server port") // choose one or more form 5561,5641,3306,5722,8003,8012,8013, e.g. '3306,5722,8003'
1913
var testUser = flag.String("user", "root", "MySQL user")
2014
var testPassword = flag.String("pass", "", "MySQL password")
2115
var testDB = flag.String("db", "test", "MySQL test database")
2216

2317
func Test(t *testing.T) {
24-
segs := strings.Split(*testPort, ",")
18+
// We cover the whole range of MySQL server versions using docker-compose to bind them to different ports for testing.
19+
// MySQL is constantly updating auth plugin to make it secure:
20+
// starting from MySQL 8.0.4, a new auth plugin is introduced, causing plain password auth to fail with error:
21+
// ERROR 1251 (08004): Client does not support authentication protocol requested by server; consider upgrading MySQL client
22+
// Hint: use docker-compose to start corresponding MySQL docker containers and add their ports here
23+
24+
segs := strings.Split(*test_util.MysqlPort, ",")
2525
for _, seg := range segs {
2626
Suite(&clientTestSuite{port: seg})
2727
Suite(&connTestSuite{port: seg})

client/conn_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
. "github.com/pingcap/check"
88

99
"github.com/go-mysql-org/go-mysql/mysql"
10+
"github.com/go-mysql-org/go-mysql/test_util"
1011
)
1112

1213
type connTestSuite struct {
@@ -16,7 +17,7 @@ type connTestSuite struct {
1617

1718
func (s *connTestSuite) SetUpSuite(c *C) {
1819
var err error
19-
addr := fmt.Sprintf("%s:%s", *testHost, s.port)
20+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
2021
s.c, err = Connect(addr, *testUser, *testPassword, "", func(c *Conn) {
2122
// required for the ExecuteMultiple test
2223
c.SetCapability(mysql.CLIENT_MULTI_STATEMENTS)

docker/resources/replication.cnf

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[mysqld]
2+
server-id=1
3+
log-bin=mysql
4+
binlog-format=row
5+
gtid-mode=ON
6+
enforce_gtid_consistency=ON
7+
#log_error_verbosity=2

docker/resources/waitfor.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
host=$1
3+
port=$2
4+
5+
echo "Waiting for mysql at $host:$port"
6+
while true; do
7+
docker run --rm -it --network=host mysql:5.7 mysql -h$host -P$port -e "SELECT RAND()" >/dev/null
8+
if [[ $? -eq 0 ]]; then
9+
echo 'Connected'
10+
break
11+
fi
12+
13+
echo 'Still waiting...'
14+
sleep 1
15+
done

driver/driver_test.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ import (
99

1010
"github.com/jmoiron/sqlx"
1111
. "github.com/pingcap/check"
12-
)
1312

14-
// Use docker mysql to test, mysql is 3306
15-
var testHost = flag.String("host", "127.0.0.1", "MySQL master host")
13+
"github.com/go-mysql-org/go-mysql/test_util"
14+
)
1615

17-
// possible choices for different MySQL versions are: 5561,5641,3306,5722,8003,8012
18-
var testPort = flag.Int("port", 3306, "MySQL server port")
1916
var testUser = flag.String("user", "root", "MySQL user")
2017
var testPassword = flag.String("pass", "", "MySQL password")
2118
var testDB = flag.String("db", "test", "MySQL test database")
@@ -31,8 +28,8 @@ type testDriverSuite struct {
3128
var _ = Suite(&testDriverSuite{})
3229

3330
func (s *testDriverSuite) SetUpSuite(c *C) {
34-
addr := fmt.Sprintf("%s:%d", *testHost, *testPort)
35-
dsn := fmt.Sprintf("%s:%s@%s?%s", *testUser, *testPassword, addr, *testDB)
31+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
32+
dsn := fmt.Sprintf("%s:%s@%s/%s", *testUser, *testPassword, addr, *testDB)
3633

3734
var err error
3835
s.db, err = sqlx.Open("mysql", dsn)

dump/schema_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"io"
77
"os"
88

9-
"github.com/go-mysql-org/go-mysql/client"
109
. "github.com/pingcap/check"
10+
11+
"github.com/go-mysql-org/go-mysql/client"
12+
"github.com/go-mysql-org/go-mysql/test_util"
1113
)
1214

1315
type schemaTestSuite struct {
@@ -18,11 +20,13 @@ type schemaTestSuite struct {
1820
var _ = Suite(&schemaTestSuite{})
1921

2022
func (s *schemaTestSuite) SetUpSuite(c *C) {
23+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
24+
2125
var err error
22-
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, *port), "root", "", "")
26+
s.conn, err = client.Connect(addr, "root", "", "")
2327
c.Assert(err, IsNil)
2428

25-
s.d, err = NewDumper(*execution, fmt.Sprintf("%s:%d", *host, *port), "root", "")
29+
s.d, err = NewDumper(*execution, addr, "root", "")
2630
c.Assert(err, IsNil)
2731
c.Assert(s.d, NotNil)
2832

dump/setup_test.go

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
. "github.com/pingcap/check"
99
)
1010

11-
// use docker mysql for test
12-
var host = flag.String("host", "127.0.0.1", "MySQL host")
13-
var port = flag.Int("port", 3306, "MySQL port")
14-
1511
var execution = flag.String("exec", "mysqldump", "mysqldump execution path")
1612

1713
func Test(t *testing.T) {

failover/failover_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"testing"
77

88
. "github.com/pingcap/check"
9+
10+
"github.com/go-mysql-org/go-mysql/test_util"
911
)
1012

11-
// We will use go-mysql docker to test
12-
// go-mysql docker will build mysql 1-3 instances
13-
var host = flag.String("host", "127.0.0.1", "go-mysql docker container address")
1413
var enable_failover_test = flag.Bool("test-failover", false, "enable test failover")
1514

1615
func Test(t *testing.T) {
@@ -33,7 +32,7 @@ func (s *failoverTestSuite) SetUpSuite(c *C) {
3332
s.s = make([]*Server, len(ports))
3433

3534
for i := 0; i < len(ports); i++ {
36-
s.s[i] = NewServer(fmt.Sprintf("%s:%d", *host, ports[i]), User{"root", ""}, User{"root", ""})
35+
s.s[i] = NewServer(fmt.Sprintf("%s:%d", *test_util.MysqlHost, ports[i]), User{"root", ""}, User{"root", ""})
3736
}
3837

3938
var err error

mysql/mysql_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77

88
"github.com/google/uuid"
99
"github.com/pingcap/check"
10+
11+
_ "github.com/go-mysql-org/go-mysql/test_util" // Will register common flags
1012
)
1113

1214
func Test(t *testing.T) {

replication/backup_test.go

+5-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package replication
33
import (
44
"context"
55
"os"
6-
"sync"
76
"time"
87

98
. "github.com/pingcap/check"
@@ -17,27 +16,20 @@ func (t *testSyncerSuite) TestStartBackupEndInGivenTime(c *C) {
1716

1817
t.testExecute(c, "RESET MASTER")
1918

20-
var wg sync.WaitGroup
21-
wg.Add(1)
22-
defer wg.Wait()
23-
24-
go func() {
25-
defer wg.Done()
26-
19+
for times := 1; times <= 2; times++ {
2720
t.testSync(c, nil)
28-
2921
t.testExecute(c, "FLUSH LOGS")
22+
}
3023

31-
t.testSync(c, nil)
32-
}()
24+
binlogDir := "./var"
3325

34-
os.RemoveAll("./var")
26+
os.RemoveAll(binlogDir)
3527
timeout := 2 * time.Second
3628

3729
done := make(chan bool)
3830

3931
go func() {
40-
err := t.b.StartBackup("./var", mysql.Position{Name: "", Pos: uint32(0)}, timeout)
32+
err := t.b.StartBackup(binlogDir, mysql.Position{Name: "", Pos: uint32(0)}, timeout)
4133
c.Assert(err, IsNil)
4234
done <- true
4335
}()

replication/replication_test.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ import (
1515

1616
"github.com/go-mysql-org/go-mysql/client"
1717
"github.com/go-mysql-org/go-mysql/mysql"
18+
"github.com/go-mysql-org/go-mysql/test_util"
1819
)
1920

20-
// Use docker mysql to test, mysql is 3306, mariadb is 3316
21-
var testHost = flag.String("host", "127.0.0.1", "MySQL master host")
22-
2321
var testOutputLogs = flag.Bool("out", false, "output binlog event")
2422

2523
func TestBinLogSyncer(t *testing.T) {
@@ -267,7 +265,7 @@ func (t *testSyncerSuite) setupTest(c *C, flavor string) {
267265
t.c.Close()
268266
}
269267

270-
t.c, err = client.Connect(fmt.Sprintf("%s:%d", *testHost, port), "root", "", "")
268+
t.c, err = client.Connect(fmt.Sprintf("%s:%d", *test_util.MysqlHost, port), "root", "", "")
271269
if err != nil {
272270
c.Skip(err.Error())
273271
}
@@ -285,7 +283,7 @@ func (t *testSyncerSuite) setupTest(c *C, flavor string) {
285283
cfg := BinlogSyncerConfig{
286284
ServerID: 100,
287285
Flavor: flavor,
288-
Host: *testHost,
286+
Host: *test_util.MysqlHost,
289287
Port: port,
290288
User: "root",
291289
Password: "",

schema/schema_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010

1111
"github.com/go-mysql-org/go-mysql/client"
1212
_ "github.com/go-mysql-org/go-mysql/driver"
13+
"github.com/go-mysql-org/go-mysql/test_util"
1314
)
1415

15-
// use docker mysql for test
16-
var host = flag.String("host", "127.0.0.1", "MySQL host")
1716
var schema = flag.String("schema", "test", "MySQL Database")
1817
var pwd = flag.String("pwd", "", "MySQL password")
1918

@@ -29,11 +28,13 @@ type schemaTestSuite struct {
2928
var _ = Suite(&schemaTestSuite{})
3029

3130
func (s *schemaTestSuite) SetUpSuite(c *C) {
31+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, *test_util.MysqlPort)
32+
3233
var err error
33-
s.conn, err = client.Connect(fmt.Sprintf("%s:%d", *host, 3306), "root", *pwd, *schema)
34+
s.conn, err = client.Connect(addr, "root", *pwd, *schema)
3435
c.Assert(err, IsNil)
3536

36-
s.sqlDB, err = sql.Open("mysql", fmt.Sprintf("root:%s@%s:3306", *pwd, *host))
37+
s.sqlDB, err = sql.Open("mysql", fmt.Sprintf("root:%s@%s", *pwd, addr))
3738
c.Assert(err, IsNil)
3839
}
3940

0 commit comments

Comments
 (0)