fleet/datastore/gorm_test.go
Victor Vrantchan 5d8ca61472 Domain Types (#83)
* create datastore package with New func to create a db connection

* separate creating a user object from saving to db
temporarily pass db around through gin context to compile app
main should create the datastore and pass it in to http handler explicitly instead

* create datastore from config params

* move gorm specific code to gorm.go

* re-export app.NewUser

* test new user

* add User() method for getting a user
temporary, the API will improve once I add filters

refactor test func to use sqlite by default and mysql if available

* add save user

* move some users tests to datastore, temporarily remove user tests from app

* add EnrollHost and test

* move enrollhost to datastore

* all enrollment tests now in datastore

* add datastore_test for re-enroll

* it compiles now...

* move other interfaces to models

* start wrapping errors in database error

* add tests for campaign

* move users to package kolide

* move hosts and passwordrequests

* package kolide

* moving all types to package kolide

* making new osquery endpoints use groob's new pattern
2016-08-18 14:16:44 -07:00

75 lines
1.4 KiB
Go

package datastore
import (
"fmt"
"os"
"testing"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
)
func TestEnrollHostMySQLGORM(t *testing.T) {
address := os.Getenv("MYSQL_ADDR")
if address == "" {
t.SkipNow()
}
db := setupMySQLGORM(t)
defer teardownMySQLGORM(t, db)
testEnrollHost(t, db)
}
// 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)
}
func setupMySQLGORM(t *testing.T) Datastore {
// TODO use ENV vars from docker config
user := "kolide"
password := "kolide"
host := "127.0.0.1:3306"
dbName := "kolide"
conn := fmt.Sprintf("%s:%s@(%s)/%s?charset=utf8&parseTime=True&loc=Local", user, password, host, dbName)
db, err := New("gorm", conn, LimitAttempts(1))
if err != nil {
t.Fatal(err)
}
backend := db.(gormDB)
if err := backend.Migrate(); err != nil {
t.Fatal(err)
}
return db
}
func teardownMySQLGORM(t *testing.T, db Datastore) {
if err := db.Drop(); err != nil {
t.Fatal(err)
}
}