mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
5d8ca61472
* 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
75 lines
1.4 KiB
Go
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)
|
|
}
|
|
}
|