2016-09-11 18:17:08 +02:00
|
|
|
type mysqlTester struct {
|
2016-09-14 10:08:30 +02:00
|
|
|
dbConn *sql.DB
|
2016-09-12 08:49:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
dbName string
|
|
|
|
host string
|
|
|
|
user string
|
|
|
|
pass string
|
|
|
|
sslmode string
|
|
|
|
port int
|
2016-09-12 08:49:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
optionFile string
|
2016-09-12 08:49:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
testDBName string
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
func init() {
|
2016-09-14 10:08:30 +02:00
|
|
|
dbMain = &mysqlTester{}
|
2016-09-11 21:07:39 +02:00
|
|
|
}
|
2016-09-11 18:17:08 +02:00
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
func (m *mysqlTester) setup() error {
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
|
|
|
|
m.dbName = viper.GetString("mysql.dbname")
|
|
|
|
m.host = viper.GetString("mysql.host")
|
|
|
|
m.user = viper.GetString("mysql.user")
|
|
|
|
m.pass = viper.GetString("mysql.pass")
|
|
|
|
m.port = viper.GetInt("mysql.port")
|
|
|
|
m.sslmode = viper.GetString("mysql.sslmode")
|
|
|
|
// Create a randomized db name.
|
|
|
|
m.testDBName = randomize.StableDBName(m.dbName)
|
|
|
|
|
|
|
|
if err = m.makeOptionFile(); err != nil {
|
|
|
|
return errors.Wrap(err, "couldn't make option file")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = m.dropTestDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = m.createTestDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-14 17:36:36 +02:00
|
|
|
dumpCmd := exec.Command("mysqldump", m.defaultsFile(), "--no-data", m.dbName)
|
2016-09-14 10:08:30 +02:00
|
|
|
createCmd := exec.Command("mysql", m.defaultsFile(), "--database", m.testDBName)
|
|
|
|
|
|
|
|
r, w := io.Pipe()
|
|
|
|
dumpCmd.Stdout = w
|
|
|
|
createCmd.Stdin = newFKeyDestroyer(rgxMySQLkey, r)
|
|
|
|
|
|
|
|
if err = dumpCmd.Start(); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to start mysqldump command")
|
|
|
|
}
|
|
|
|
if err = createCmd.Start(); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to start mysql command")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = dumpCmd.Wait(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return errors.Wrap(err, "failed to wait for mysqldump command")
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Close() // After dumpCmd is done, close the write end of the pipe
|
|
|
|
|
|
|
|
if err = createCmd.Wait(); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return errors.Wrap(err, "failed to wait for mysql command")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mysqlTester) sslMode(mode string) string {
|
|
|
|
switch mode {
|
|
|
|
case "true":
|
|
|
|
return "REQUIRED"
|
|
|
|
case "false":
|
|
|
|
return "DISABLED"
|
|
|
|
default:
|
|
|
|
return "PREFERRED"
|
|
|
|
}
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 08:49:47 +02:00
|
|
|
func (m *mysqlTester) defaultsFile() string {
|
2016-09-14 10:08:30 +02:00
|
|
|
return fmt.Sprintf("--defaults-file=%s", m.optionFile)
|
2016-09-12 08:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mysqlTester) makeOptionFile() error {
|
2016-09-14 10:08:30 +02:00
|
|
|
tmp, err := ioutil.TempFile("", "optionfile")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to create option file")
|
|
|
|
}
|
|
|
|
|
2017-04-29 05:01:27 +02:00
|
|
|
isTCP := false
|
|
|
|
_, err = os.Stat(m.host)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
isTCP = true
|
|
|
|
} else if err != nil {
|
|
|
|
return errors.Wrap(err, "could not stat m.host")
|
|
|
|
}
|
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
fmt.Fprintln(tmp, "[client]")
|
|
|
|
fmt.Fprintf(tmp, "host=%s\n", m.host)
|
|
|
|
fmt.Fprintf(tmp, "port=%d\n", m.port)
|
|
|
|
fmt.Fprintf(tmp, "user=%s\n", m.user)
|
|
|
|
fmt.Fprintf(tmp, "password=%s\n", m.pass)
|
|
|
|
fmt.Fprintf(tmp, "ssl-mode=%s\n", m.sslMode(m.sslmode))
|
2017-04-29 05:01:27 +02:00
|
|
|
if isTCP {
|
|
|
|
fmt.Fprintln(tmp, "protocol=tcp")
|
|
|
|
}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
fmt.Fprintln(tmp, "[mysqldump]")
|
|
|
|
fmt.Fprintf(tmp, "host=%s\n", m.host)
|
|
|
|
fmt.Fprintf(tmp, "port=%d\n", m.port)
|
|
|
|
fmt.Fprintf(tmp, "user=%s\n", m.user)
|
|
|
|
fmt.Fprintf(tmp, "password=%s\n", m.pass)
|
|
|
|
fmt.Fprintf(tmp, "ssl-mode=%s\n", m.sslMode(m.sslmode))
|
2017-04-29 05:01:27 +02:00
|
|
|
if isTCP {
|
|
|
|
fmt.Fprintln(tmp, "protocol=tcp")
|
|
|
|
}
|
2016-09-14 10:08:30 +02:00
|
|
|
|
|
|
|
m.optionFile = tmp.Name()
|
|
|
|
|
|
|
|
return tmp.Close()
|
2016-09-12 08:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mysqlTester) createTestDB() error {
|
2016-09-14 10:08:30 +02:00
|
|
|
sql := fmt.Sprintf("create database %s;", m.testDBName)
|
|
|
|
return m.runCmd(sql, "mysql")
|
2016-09-12 08:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mysqlTester) dropTestDB() error {
|
2016-09-14 10:08:30 +02:00
|
|
|
sql := fmt.Sprintf("drop database if exists %s;", m.testDBName)
|
|
|
|
return m.runCmd(sql, "mysql")
|
2016-09-12 08:49:47 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 21:07:39 +02:00
|
|
|
func (m *mysqlTester) teardown() error {
|
2016-09-14 10:08:30 +02:00
|
|
|
if m.dbConn != nil {
|
|
|
|
m.dbConn.Close()
|
|
|
|
}
|
2016-09-12 08:49:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
if err := m.dropTestDB(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-09-14 06:46:58 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return os.Remove(m.optionFile)
|
2016-09-12 08:49:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mysqlTester) runCmd(stdin, command string, args ...string) error {
|
2016-09-14 10:08:30 +02:00
|
|
|
args = append([]string{m.defaultsFile()}, args...)
|
|
|
|
|
|
|
|
cmd := exec.Command(command, args...)
|
|
|
|
cmd.Stdin = strings.NewReader(stdin)
|
|
|
|
|
|
|
|
stdout := &bytes.Buffer{}
|
|
|
|
stderr := &bytes.Buffer{}
|
|
|
|
cmd.Stdout = stdout
|
|
|
|
cmd.Stderr = stderr
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
fmt.Println("failed running:", command, args)
|
|
|
|
fmt.Println(stdout.String())
|
|
|
|
fmt.Println(stderr.String())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-09-11 18:17:08 +02:00
|
|
|
}
|
|
|
|
|
2016-09-12 08:49:47 +02:00
|
|
|
func (m *mysqlTester) conn() (*sql.DB, error) {
|
2016-09-14 10:08:30 +02:00
|
|
|
if m.dbConn != nil {
|
|
|
|
return m.dbConn, nil
|
|
|
|
}
|
2016-09-12 08:49:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
var err error
|
|
|
|
m.dbConn, err = sql.Open("mysql", drivers.MySQLBuildQueryString(m.user, m.pass, m.testDBName, m.host, m.port, m.sslmode))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-12 08:49:47 +02:00
|
|
|
|
2016-09-14 10:08:30 +02:00
|
|
|
return m.dbConn, nil
|
2016-09-09 08:05:09 +02:00
|
|
|
}
|