2016-08-18 21:16:44 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/mysql"
|
|
|
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
2016-09-26 18:48:55 +00:00
|
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
2016-09-14 18:40:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-08-18 21:16:44 +00:00
|
|
|
)
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
func setupMySQLGORM(t *testing.T) kolide.Datastore {
|
2016-08-24 02:30:55 +00:00
|
|
|
user := "kolide"
|
|
|
|
password := "kolide"
|
|
|
|
dbName := "kolide"
|
|
|
|
|
|
|
|
// try container first
|
|
|
|
host := os.Getenv("MYSQL_PORT_3306_TCP_ADDR")
|
|
|
|
if host == "" {
|
|
|
|
host = "127.0.0.1"
|
|
|
|
}
|
|
|
|
host = fmt.Sprintf("%s:3306", host)
|
|
|
|
|
|
|
|
conn := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local", user, password, host, dbName)
|
|
|
|
db, err := New("gorm-mysql", conn, LimitAttempts(1))
|
2016-09-14 18:40:51 +00:00
|
|
|
assert.Nil(t, err)
|
2016-08-24 02:30:55 +00:00
|
|
|
|
|
|
|
backend := db.(gormDB)
|
2016-09-14 18:40:51 +00:00
|
|
|
err = backend.Migrate()
|
|
|
|
assert.Nil(t, err)
|
2016-08-24 02:30:55 +00:00
|
|
|
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2016-08-28 03:59:17 +00:00
|
|
|
func teardownMySQLGORM(t *testing.T, db kolide.Datastore) {
|
2016-09-14 18:40:51 +00:00
|
|
|
err := db.Drop()
|
|
|
|
assert.Nil(t, err)
|
2016-08-24 02:30:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-18 21:16:44 +00:00
|
|
|
func TestEnrollHostMySQLGORM(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
db := setupMySQLGORM(t)
|
|
|
|
defer teardownMySQLGORM(t, db)
|
|
|
|
|
|
|
|
testEnrollHost(t, db)
|
|
|
|
}
|
|
|
|
|
2016-08-19 04:57:00 +00:00
|
|
|
func TestAuthenticateHostMySQLGORM(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
db := setup(t)
|
|
|
|
defer teardown(t, db)
|
|
|
|
|
|
|
|
testAuthenticateHost(t, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUserByIDMySQLGORM(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
db := setup(t)
|
|
|
|
defer teardown(t, db)
|
|
|
|
|
|
|
|
testUserByID(t, db)
|
|
|
|
}
|
|
|
|
|
2016-08-18 21:16:44 +00:00
|
|
|
// TestCreateUser tests the UserStore interface
|
|
|
|
// this test uses the MySQL GORM backend
|
|
|
|
func TestCreateUserMySQLGORM(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
db := setupMySQLGORM(t)
|
|
|
|
defer teardownMySQLGORM(t, db)
|
|
|
|
|
|
|
|
testCreateUser(t, db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveUserMySQLGORM(t *testing.T) {
|
|
|
|
address := os.Getenv("MYSQL_ADDR")
|
|
|
|
if address == "" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
db := setupMySQLGORM(t)
|
|
|
|
defer teardownMySQLGORM(t, db)
|
|
|
|
|
|
|
|
testSaveUser(t, db)
|
|
|
|
}
|
|
|
|
|
2016-08-24 02:30:55 +00:00
|
|
|
func TestSaveQuery(t *testing.T) {
|
|
|
|
ds := setup(t)
|
|
|
|
testSaveQuery(t, ds)
|
|
|
|
}
|
2016-08-18 21:16:44 +00:00
|
|
|
|
2016-08-24 02:30:55 +00:00
|
|
|
func TestDeleteQuery(t *testing.T) {
|
|
|
|
ds := setup(t)
|
|
|
|
testDeleteQuery(t, ds)
|
|
|
|
}
|
2016-08-18 21:16:44 +00:00
|
|
|
|
2016-08-24 02:30:55 +00:00
|
|
|
func TestDeletePack(t *testing.T) {
|
|
|
|
ds := setup(t)
|
|
|
|
testDeletePack(t, ds)
|
2016-08-18 21:16:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-24 02:30:55 +00:00
|
|
|
func TestAddAndRemoveQueryFromPack(t *testing.T) {
|
|
|
|
ds := setup(t)
|
|
|
|
testAddAndRemoveQueryFromPack(t, ds)
|
2016-08-18 21:16:44 +00:00
|
|
|
}
|