@@ -2,32 +2,37 @@ package localtests
22
33import (
44 "database/sql"
5+ "errors"
56 "fmt"
67 "io/ioutil"
78 "log"
89 "os"
910 "os/exec"
1011 "path/filepath"
1112 "strings"
13+ "time"
1214
1315 "github.com/google/shlex"
1416)
1517
1618const (
17- testUsername = "gh-ost"
18- testPassword = "gh-ost"
19- testDatabase = "test"
20- testTable = "gh_ost_test"
21- testSocketFile = "/tmp/gh-ost.test.sock"
22- throttleFlagFile = "/tmp/gh-ost-test.ghost.throttle.flag"
23- throttleQuery = "select timestampdiff(second, min(last_update), now()) < 5 from _gh_ost_test_ghc"
19+ PrimaryHost = "primary"
20+ DefaultHost = "replica"
21+ DefaultPort int64 = 3306
22+ DefaultUsername = "gh-ost"
23+ DefaultPassword = "gh-ost"
24+ testDatabase = "test"
25+ testTable = "gh_ost_test"
26+ testSocketFile = "/tmp/gh-ost.test.sock"
27+ throttleFlagFile = "/tmp/gh-ost-test.ghost.throttle.flag"
28+ throttleQuery = "select timestampdiff(second, min(last_update), now()) < 5 from _gh_ost_test_ghc"
2429)
2530
2631type Config struct {
27- Username string
28- Password string
2932 Host string
3033 Port int64
34+ Username string
35+ Password string
3136 GhostBinary string
3237 MysqlBinary string
3338}
@@ -36,10 +41,29 @@ type Test struct {
3641 Name string
3742 Path string
3843 CreateSQLFile string
39- ExtraArgs string
44+ ExtraArgs [] string
4045 IgnoreVersions []string
4146}
4247
48+ func WaitForMySQLAvailable (db * sql.DB ) error {
49+ ticker := time .NewTicker (time .Second )
50+ defer ticker .Stop ()
51+
52+ for {
53+ select {
54+ case <- time .After (10 * time .Minute ):
55+ return errors .New ("timed out waiting for mysql" )
56+ case <- ticker .C :
57+ if err := db .Ping (); err != nil {
58+ log .Println ("Waiting for mysql to become available" )
59+ } else {
60+ log .Println ("MySQL is available" )
61+ return nil
62+ }
63+ }
64+ }
65+ }
66+
4367// Prepare runs a 'mysql' client/shell command to populate the test schema.
4468// The create.sql file is read by golang and passed to 'mysql' over stdin.
4569func (test * Test ) Prepare (config Config ) error {
@@ -55,7 +79,7 @@ func (test *Test) Prepare(config Config) error {
5579
5680 flags := []string {
5781 fmt .Sprintf ("--defaults-file=%s" , defaultsFile ),
58- fmt .Sprintf ("--host=%s" , config . Host ),
82+ fmt .Sprintf ("--host=%s" , PrimaryHost ), // TODO: fix this
5983 fmt .Sprintf ("--port=%d" , config .Port ),
6084 "--default-character-set=utf8mb4" ,
6185 testDatabase ,
@@ -84,11 +108,11 @@ func (test *Test) Migrate(db *sql.DB, config Config) error {
84108 log .Printf ("[%s] detected MySQL %s host %s:%d" , test .Name , mysqlInfo .Version , config .Host , config .Port )
85109
86110 flags := []string {
87- fmt .Sprintf ("--user=%s" , testUsername ),
88- fmt .Sprintf ("--password=%s" , testPassword ),
111+ fmt .Sprintf ("--user=%s" , config . Username ),
112+ fmt .Sprintf ("--password=%s" , config . Password ),
89113 fmt .Sprintf ("--host=%s" , config .Host ),
90114 fmt .Sprintf ("--port=%d" , config .Port ),
91- fmt .Sprintf ("--assume-master-host=%s :%d" , mysqlInfo .Host , mysqlInfo . Port ),
115+ fmt .Sprintf ("--assume-master-host=primary :%d" , mysqlInfo .Port ), // TODO: fix this
92116 fmt .Sprintf ("--database=%s" , testDatabase ),
93117 fmt .Sprintf ("--table=%s" , testTable ),
94118 "--assume-rbr" ,
@@ -108,14 +132,10 @@ func (test *Test) Migrate(db *sql.DB, config Config) error {
108132 "--stack" ,
109133 "--verbose" ,
110134 }
111- if test .ExtraArgs == "" {
112- flags = append (flags , "--alter='engine=innodb'" )
135+ if len ( test .ExtraArgs ) > 0 {
136+ flags = append (flags , test . ExtraArgs ... )
113137 } else {
114- extraArgs , err := shlex .Split (test .ExtraArgs )
115- if err != nil {
116- return err
117- }
118- flags = append (flags , extraArgs ... )
138+ flags = append (flags , `--alter='ENGINE=InnoDB'` )
119139 }
120140
121141 log .Printf ("[%s] running gh-ost command: %s\n %s" , test .Name , config .GhostBinary , strings .Join (flags , "\n " ))
@@ -150,7 +170,12 @@ func ReadTests(testsDir string) (tests []Test, err error) {
150170
151171 extraArgsFile := filepath .Join (test .Path , "extra_args" )
152172 if _ , err = os .Stat (extraArgsFile ); err == nil {
153- if test .ExtraArgs , err = readTestFile (extraArgsFile ); err != nil {
173+ extraArgsStr , err := readTestFile (extraArgsFile )
174+ if err != nil {
175+ log .Printf ("Failed to read extra_args file %q: %+v" , extraArgsFile , err )
176+ return tests , err
177+ }
178+ if test .ExtraArgs , err = shlex .Split (extraArgsStr ); err != nil {
154179 log .Printf ("Failed to read extra_args file %q: %+v" , extraArgsFile , err )
155180 return tests , err
156181 }
0 commit comments