|
| 1 | +package localtests |
| 2 | + |
| 3 | +import ( |
| 4 | + "database/sql" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/google/shlex" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 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" |
| 29 | +) |
| 30 | + |
| 31 | +type Config struct { |
| 32 | + Host string |
| 33 | + Port int64 |
| 34 | + Username string |
| 35 | + Password string |
| 36 | + GhostBinary string |
| 37 | + MysqlBinary string |
| 38 | +} |
| 39 | + |
| 40 | +type Test struct { |
| 41 | + Name string |
| 42 | + Path string |
| 43 | + CreateSQLFile string |
| 44 | + ExtraArgs []string |
| 45 | + IgnoreVersions []string |
| 46 | +} |
| 47 | + |
| 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 | + |
| 67 | +// Prepare runs a 'mysql' client/shell command to populate the test schema. |
| 68 | +// The create.sql file is read by golang and passed to 'mysql' over stdin. |
| 69 | +func (test *Test) Prepare(config Config) error { |
| 70 | + if test.CreateSQLFile == "" { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + defaultsFile, err := writeMysqlClientDefaultsFile(config) |
| 75 | + if err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + defer os.Remove(defaultsFile) |
| 79 | + |
| 80 | + flags := []string{ |
| 81 | + fmt.Sprintf("--defaults-file=%s", defaultsFile), |
| 82 | + fmt.Sprintf("--host=%s", PrimaryHost), // TODO: fix this |
| 83 | + fmt.Sprintf("--port=%d", config.Port), |
| 84 | + "--default-character-set=utf8mb4", |
| 85 | + testDatabase, |
| 86 | + } |
| 87 | + log.Printf("[%s] running command: %s\n %s", test.Name, config.MysqlBinary, strings.Join(flags, "\n ")) |
| 88 | + |
| 89 | + createSQL, err := os.Open(test.CreateSQLFile) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + defer createSQL.Close() |
| 94 | + log.Printf("[%s] loaded sql from: %s", test.Name, test.CreateSQLFile) |
| 95 | + |
| 96 | + cmd := exec.Command(config.MysqlBinary, flags...) |
| 97 | + cmd.Stdin = createSQL |
| 98 | + cmd.Stdout = os.Stdout |
| 99 | + cmd.Stderr = os.Stderr |
| 100 | + return cmd.Run() |
| 101 | +} |
| 102 | + |
| 103 | +func (test *Test) Migrate(db *sql.DB, config Config) error { |
| 104 | + mysqlInfo, err := getMysqlHostInfo(db) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + log.Printf("[%s] detected MySQL %s host %s:%d", test.Name, mysqlInfo.Version, config.Host, config.Port) |
| 109 | + |
| 110 | + flags := []string{ |
| 111 | + fmt.Sprintf("--user=%s", config.Username), |
| 112 | + fmt.Sprintf("--password=%s", config.Password), |
| 113 | + fmt.Sprintf("--host=%s", config.Host), |
| 114 | + fmt.Sprintf("--port=%d", config.Port), |
| 115 | + fmt.Sprintf("--assume-master-host=primary:%d", mysqlInfo.Port), // TODO: fix this |
| 116 | + fmt.Sprintf("--database=%s", testDatabase), |
| 117 | + fmt.Sprintf("--table=%s", testTable), |
| 118 | + "--assume-rbr", |
| 119 | + "--chunk-size=10", |
| 120 | + "--default-retries=3", |
| 121 | + "--exact-rowcount", |
| 122 | + "--initially-drop-old-table", |
| 123 | + "--initially-drop-ghost-table", |
| 124 | + "--initially-drop-socket-file", |
| 125 | + fmt.Sprintf("--throttle-query=%s", throttleQuery), |
| 126 | + fmt.Sprintf("--throttle-flag-file=%s", throttleFlagFile), |
| 127 | + fmt.Sprintf("--serve-socket-file=%s", testSocketFile), |
| 128 | + //"--test-on-replica", |
| 129 | + "--allow-on-master", |
| 130 | + "--debug", |
| 131 | + "--execute", |
| 132 | + "--stack", |
| 133 | + "--verbose", |
| 134 | + } |
| 135 | + if len(test.ExtraArgs) > 0 { |
| 136 | + flags = append(flags, test.ExtraArgs...) |
| 137 | + } else { |
| 138 | + flags = append(flags, `--alter='ENGINE=InnoDB'`) |
| 139 | + } |
| 140 | + |
| 141 | + log.Printf("[%s] running gh-ost command: %s\n %s", test.Name, config.GhostBinary, strings.Join(flags, "\n ")) |
| 142 | + cmd := exec.Command(config.GhostBinary, flags...) |
| 143 | + cmd.Stdout = os.Stdout |
| 144 | + cmd.Stderr = os.Stderr |
| 145 | + return cmd.Run() |
| 146 | +} |
| 147 | + |
| 148 | +func ReadTests(testsDir string) (tests []Test, err error) { |
| 149 | + subdirs, err := ioutil.ReadDir(testsDir) |
| 150 | + if err != nil { |
| 151 | + return tests, err |
| 152 | + } |
| 153 | + |
| 154 | + for _, subdir := range subdirs { |
| 155 | + test := Test{ |
| 156 | + Name: subdir.Name(), |
| 157 | + Path: filepath.Join(testsDir, subdir.Name()), |
| 158 | + } |
| 159 | + |
| 160 | + stat, err := os.Stat(test.Path) |
| 161 | + if err != nil || !stat.IsDir() { |
| 162 | + continue |
| 163 | + } |
| 164 | + |
| 165 | + test.CreateSQLFile = filepath.Join(test.Path, "create.sql") |
| 166 | + if _, err = os.Stat(test.CreateSQLFile); err != nil { |
| 167 | + log.Printf("Failed to find create.sql file %q: %+v", test.CreateSQLFile, err) |
| 168 | + return tests, err |
| 169 | + } |
| 170 | + |
| 171 | + extraArgsFile := filepath.Join(test.Path, "extra_args") |
| 172 | + if _, err = os.Stat(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 { |
| 179 | + log.Printf("Failed to read extra_args file %q: %+v", extraArgsFile, err) |
| 180 | + return tests, err |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + tests = append(tests, test) |
| 185 | + } |
| 186 | + |
| 187 | + return tests, err |
| 188 | +} |
| 189 | + |
| 190 | +func RunTest(db *sql.DB, config Config, test Test) error { |
| 191 | + if err := test.Prepare(config); err != nil { |
| 192 | + return err |
| 193 | + } |
| 194 | + log.Printf("[%s] prepared test", test.Name) |
| 195 | + |
| 196 | + if err := test.Migrate(db, config); err != nil { |
| 197 | + return err |
| 198 | + } |
| 199 | + log.Printf("[%s] migrated test", test.Name) |
| 200 | + |
| 201 | + return nil |
| 202 | +} |
0 commit comments