2016-08-18 21:16:44 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
2016-10-19 14:46:33 +00:00
|
|
|
"os"
|
2016-08-18 21:16:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
|
|
|
"github.com/go-kit/kit/log"
|
2016-11-18 17:02:51 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
2016-12-01 18:31:16 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/config"
|
2016-11-16 13:47:49 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/datastore/mysql"
|
2016-10-25 15:01:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-18 21:16:44 +00:00
|
|
|
)
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func setupMySQL(t *testing.T) (ds *mysql.Datastore, teardown func()) {
|
2016-12-01 18:31:16 +00:00
|
|
|
config := config.MysqlConfig{
|
|
|
|
Username: "kolide",
|
|
|
|
Password: "kolide",
|
|
|
|
Database: "kolide",
|
|
|
|
Address: "127.0.0.1:3306",
|
|
|
|
}
|
2016-10-25 15:01:14 +00:00
|
|
|
|
|
|
|
if h, ok := os.LookupEnv("MYSQL_PORT_3306_TCP_ADDR"); ok {
|
2016-12-01 18:31:16 +00:00
|
|
|
config.Address = h + ":3306"
|
2016-10-19 14:46:33 +00:00
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2017-01-03 16:54:24 +00:00
|
|
|
ds, err := mysql.New(config, clock.NewMockClock(), mysql.Logger(log.NewNopLogger()), mysql.LimitAttempts(1))
|
2016-10-25 15:01:14 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
teardown = func() {
|
2016-11-16 13:47:49 +00:00
|
|
|
ds.Close()
|
2016-10-25 15:01:14 +00:00
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2016-10-25 15:01:14 +00:00
|
|
|
return ds, teardown
|
2016-08-24 02:30:55 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 13:47:49 +00:00
|
|
|
func TestMySQL(t *testing.T) {
|
2016-10-25 15:01:14 +00:00
|
|
|
if _, ok := os.LookupEnv("MYSQL_TEST"); !ok {
|
2016-10-19 14:46:33 +00:00
|
|
|
t.SkipNow()
|
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
|
|
|
ds, teardown := setupMySQL(t)
|
|
|
|
defer teardown()
|
|
|
|
// get rid of database if it is hanging around
|
|
|
|
err := ds.Drop()
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
2016-10-04 20:34:36 +00:00
|
|
|
for _, f := range testFunctions {
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2016-10-04 20:34:36 +00:00
|
|
|
t.Run(functionName(f), func(t *testing.T) {
|
2016-11-16 13:47:49 +00:00
|
|
|
defer func() { require.Nil(t, ds.Drop()) }()
|
2017-01-05 17:27:56 +00:00
|
|
|
require.Nil(t, ds.MigrateTables())
|
2016-10-04 20:34:36 +00:00
|
|
|
f(t, ds)
|
|
|
|
})
|
2016-08-18 21:16:44 +00:00
|
|
|
}
|
2016-11-16 13:47:49 +00:00
|
|
|
|
2016-10-03 03:14:35 +00:00
|
|
|
}
|