update mysql test setup (#343)

reuse a single conneciton pool
close connection gracefully at the end of tests
setup and teardown tables in between each test
This commit is contained in:
Victor Vrantchan 2016-10-25 11:01:14 -04:00 committed by GitHub
parent ee3d96eb53
commit efffe50a55

View File

@ -6,44 +6,47 @@ import (
"testing" "testing"
"github.com/kolide/kolide-ose/server/kolide" "github.com/kolide/kolide-ose/server/kolide"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/require"
) )
func setupGorm(t *testing.T) kolide.Datastore { func setupGorm(t *testing.T) (ds kolide.Datastore, teardown func()) {
user := "kolide" var (
password := "kolide" user = "kolide"
dbName := "kolide" password = "kolide"
dbName = "kolide"
host = "127.0.0.1"
)
// try container first // use linked container if available.
host := os.Getenv("MYSQL_PORT_3306_TCP_ADDR") if h, ok := os.LookupEnv("MYSQL_PORT_3306_TCP_ADDR"); ok {
if host == "" { host = h
host = "127.0.0.1"
} }
host = fmt.Sprintf("%s:3306", host) connString := fmt.Sprintf("%s:%s@(%s:3306)/%s?charset=utf8&parseTime=True&loc=Local", user, password, host, dbName)
connString := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local", user, password, host, dbName)
ds, err := New("gorm-mysql", connString) ds, err := New("gorm-mysql", connString)
require.Nil(t, err)
err = ds.Migrate() teardown = func() {
assert.Nil(t, err) db, ok := ds.(gormDB)
return ds if !ok {
} panic("expected gormDB datastore")
}
func teardownGorm(t *testing.T, ds kolide.Datastore) { db.DB.Close()
err := ds.Drop() }
assert.Nil(t, err) return ds, teardown
} }
func TestGorm(t *testing.T) { func TestGorm(t *testing.T) {
address := os.Getenv("MYSQL_ADDR") if _, ok := os.LookupEnv("MYSQL_TEST"); !ok {
if address == "" {
t.SkipNow() t.SkipNow()
} }
ds, teardown := setupGorm(t)
defer teardown()
for _, f := range testFunctions { for _, f := range testFunctions {
t.Run(functionName(f), func(t *testing.T) { t.Run(functionName(f), func(t *testing.T) {
ds := setupGorm(t) err := ds.Migrate()
defer teardownGorm(t, ds) require.Nil(t, err)
f(t, ds) f(t, ds)
err = ds.Drop()
require.Nil(t, err)
}) })
} }
} }