2017-08-03 17:24:27 +00:00
|
|
|
package mysql
|
2015-06-08 10:32:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2015-06-08 10:32:44 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMySQLBackend(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
database := os.Getenv("MYSQL_DB")
|
|
|
|
if database == "" {
|
|
|
|
database = "test"
|
|
|
|
}
|
|
|
|
|
|
|
|
table := os.Getenv("MYSQL_TABLE")
|
|
|
|
if table == "" {
|
|
|
|
table = "test"
|
|
|
|
}
|
|
|
|
|
|
|
|
username := os.Getenv("MYSQL_USERNAME")
|
|
|
|
password := os.Getenv("MYSQL_PASSWORD")
|
|
|
|
|
2015-06-12 17:31:46 +00:00
|
|
|
// Run vault tests
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewMySQLBackend(map[string]string{
|
2015-06-12 09:47:45 +00:00
|
|
|
"address": address,
|
|
|
|
"database": database,
|
|
|
|
"table": table,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2015-06-12 09:47:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-18 21:31:00 +00:00
|
|
|
defer func() {
|
|
|
|
mysql := b.(*MySQLBackend)
|
2018-08-16 18:03:16 +00:00
|
|
|
_, err := mysql.client.Exec("DROP TABLE IF EXISTS " + mysql.dbTable + " ," + mysql.dbLockTable)
|
2015-06-18 21:31:00 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to drop table: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2015-06-08 10:32:44 +00:00
|
|
|
}
|
2018-08-13 21:02:31 +00:00
|
|
|
|
|
|
|
func TestMySQLHABackend(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
database := os.Getenv("MYSQL_DB")
|
|
|
|
if database == "" {
|
|
|
|
database = "test"
|
|
|
|
}
|
|
|
|
|
|
|
|
table := os.Getenv("MYSQL_TABLE")
|
|
|
|
if table == "" {
|
|
|
|
table = "test"
|
|
|
|
}
|
|
|
|
|
|
|
|
username := os.Getenv("MYSQL_USERNAME")
|
|
|
|
password := os.Getenv("MYSQL_PASSWORD")
|
|
|
|
|
|
|
|
// Run vault tests
|
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2018-11-01 17:31:09 +00:00
|
|
|
config := map[string]string{
|
2018-08-16 18:03:16 +00:00
|
|
|
"address": address,
|
|
|
|
"database": database,
|
|
|
|
"table": table,
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
"ha_enabled": "true",
|
2018-11-01 17:31:09 +00:00
|
|
|
}
|
2018-08-13 21:02:31 +00:00
|
|
|
|
2018-11-01 17:31:09 +00:00
|
|
|
b, err := NewMySQLBackend(config, logger)
|
2018-08-13 21:02:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
mysql := b.(*MySQLBackend)
|
2018-08-16 18:03:16 +00:00
|
|
|
_, err := mysql.client.Exec("DROP TABLE IF EXISTS " + mysql.dbTable + " ," + mysql.dbLockTable)
|
2018-08-13 21:02:31 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to drop table: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-11-01 17:31:09 +00:00
|
|
|
b2, err := NewMySQLBackend(config, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create new backend: %v", err)
|
2018-08-13 21:02:31 +00:00
|
|
|
}
|
2018-11-01 17:31:09 +00:00
|
|
|
|
|
|
|
physical.ExerciseHABackend(t, b.(physical.HABackend), b2.(physical.HABackend))
|
2018-08-13 21:02:31 +00:00
|
|
|
}
|