diff --git a/auth_test.go b/auth_test.go index a9d0d470f..861593636 100644 --- a/auth_test.go +++ b/auth_test.go @@ -10,7 +10,7 @@ import ( ) func TestGenerateVC(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", true, false) if err != nil { @@ -42,7 +42,7 @@ func TestGenerateJWT(t *testing.T) { } func TestVC(t *testing.T) { - db := openTestDB() + db := openTestDB(t) r := createEmptyTestServer(db) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) @@ -112,7 +112,7 @@ func TestVC(t *testing.T) { } func TestIsUserID(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user1, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) if err != nil { @@ -131,7 +131,7 @@ func TestIsUserID(t *testing.T) { } func TestCanPerformActionsOnUser(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user1, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) if err != nil { diff --git a/kolide.go b/kolide.go index bdf88e155..4bfe2d64d 100644 --- a/kolide.go +++ b/kolide.go @@ -150,7 +150,8 @@ $7777777....$....$777$.....+DI..DDD..DDI...8D...D8......$D:..8D....8D...8D...... fmt.Println("=> Run `kolide help serve` for more startup options") fmt.Println("Use Ctrl-C to stop") fmt.Print("\n\n") - CreateServer(db).RunTLS( + + CreateServer(db, os.Stderr).RunTLS( config.Server.Address, config.Server.Cert, config.Server.Key) diff --git a/models.go b/models.go index 12988d4bb..3c9257203 100644 --- a/models.go +++ b/models.go @@ -179,20 +179,6 @@ func openDB(user, password, address, dbName string) (*gorm.DB, error) { return db, nil } -func openTestDB() *gorm.DB { - db, err := gorm.Open("sqlite3", ":memory:") - if err != nil { - panic(fmt.Sprintf("Error opening test DB: %s", err.Error())) - } - - setDBSettings(db) - createTables(db) - if db.Error != nil { - panic(fmt.Sprintf("Error creating test DB tables: %s", db.Error.Error())) - } - return db -} - func dropTables(db *gorm.DB) { for _, table := range tables { db.DropTableIfExists(table) diff --git a/server.go b/server.go index c18d96b6b..006b1ae19 100644 --- a/server.go +++ b/server.go @@ -1,6 +1,8 @@ package main import ( + "io" + _ "net/http/pprof" "time" "github.com/Sirupsen/logrus" @@ -54,7 +56,7 @@ func DatabaseMiddleware(db *gorm.DB) gin.HandlerFunc { // CreateServer creates a gin.Engine HTTP server and configures it to be in a // state such that it is ready to serve HTTP requests for the kolide application -func CreateServer(db *gorm.DB) *gin.Engine { +func CreateServer(db *gorm.DB, w io.Writer) *gin.Engine { server := gin.New() server.Use(DatabaseMiddleware(db)) server.Use(SessionBackendMiddleware) @@ -67,12 +69,14 @@ func CreateServer(db *gorm.DB) *gin.Engine { // Ginrus middleware logs details about incoming requests using the // logrus WithFields requestLogger := logrus.New() + requestLogger.Out = w server.Use(ginrus.Ginrus(requestLogger, time.RFC3339, false)) // Recovery middleware recovers from panic(), returning a 500 response // code and printing the panic information to the log recoveryLogger := logrus.New() recoveryLogger.WriterLevel(logrus.ErrorLevel) + recoveryLogger.Out = w server.Use(gin.RecoveryWithWriter(recoveryLogger.Writer())) v1 := server.Group("/api/v1") diff --git a/sessions_test.go b/sessions_test.go index 5492188a8..bc3589b9d 100644 --- a/sessions_test.go +++ b/sessions_test.go @@ -23,7 +23,7 @@ func (w *MockResponseWriter) WriteHeader(int) { } func TestSessionManagerVC(t *testing.T) { - db := openTestDB() + db := openTestDB(t) admin, err := NewUser(db, "admin", "foobar", "admin@kolide.co", true, false) if err != nil { @@ -74,7 +74,7 @@ func TestSessionManagerVC(t *testing.T) { } func TestSessionCreation(t *testing.T) { - db := openTestDB() + db := openTestDB(t) r := createEmptyTestServer(db) admin, _ := NewUser(db, "admin", "foobar", "admin@kolide.co", true, false) diff --git a/test_util.go b/test_util.go index ecd0cb2a3..e781a7c94 100644 --- a/test_util.go +++ b/test_util.go @@ -3,6 +3,7 @@ package main import ( "bytes" "encoding/json" + "fmt" "net/http" "net/http/httptest" "testing" @@ -11,6 +12,37 @@ import ( "github.com/jinzhu/gorm" ) +type testLogger struct { + t *testing.T +} + +func (t *testLogger) Print(v ...interface{}) { + t.t.Log(v...) +} + +func (t *testLogger) Write(p []byte) (n int, err error) { + t.t.Log(string(p)) + return len(p), nil +} + +func openTestDB(t *testing.T) *gorm.DB { + db, err := gorm.Open("sqlite3", ":memory:") + if err != nil { + panic(fmt.Sprintf("Error opening test DB: %s", err.Error())) + } + + createTables(db) + if db.Error != nil { + panic(fmt.Sprintf("Error creating test DB tables: %s", db.Error.Error())) + } + + // Log using t.Log so that output only shows up if the test fails + db.SetLogger(&testLogger{t: t}) + db.LogMode(true) + + return db +} + type IntegrationRequests struct { r *gin.Engine db *gorm.DB @@ -20,8 +52,7 @@ type IntegrationRequests struct { func (req *IntegrationRequests) New(t *testing.T) { req.t = t - *debug = false - req.db = openTestDB() + req.db = openTestDB(t) // Until we have a better solution for first-user onboarding, manually // create an admin @@ -30,7 +61,7 @@ func (req *IntegrationRequests) New(t *testing.T) { t.Fatalf("Error opening DB: %s", err.Error()) } - req.r = CreateServer(req.db) + req.r = CreateServer(req.db, &testLogger{t: t}) } func (req *IntegrationRequests) Login(username, password string, sessionOut *string) { diff --git a/users.go b/users.go index a6d01a3ca..091510be9 100644 --- a/users.go +++ b/users.go @@ -53,7 +53,6 @@ func NewUser(db *gorm.DB, username, password, email string, admin, needsPassword // supplied password with the stored password salt func (u *User) ValidatePassword(password string) error { saltAndPass := []byte(fmt.Sprintf("%s%s", password, u.Salt)) - logrus.Info(string(saltAndPass)) return bcrypt.CompareHashAndPassword(u.Password, saltAndPass) } diff --git a/users_test.go b/users_test.go index 874d2f253..61a1d4a68 100644 --- a/users_test.go +++ b/users_test.go @@ -7,7 +7,7 @@ import ( ) func TestNewUser(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", true, false) if err != nil { @@ -34,7 +34,7 @@ func TestNewUser(t *testing.T) { } func TestValidatePassword(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", true, false) if err != nil { @@ -57,7 +57,7 @@ func TestValidatePassword(t *testing.T) { } func TestMakeAdmin(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) if err != nil { @@ -91,7 +91,7 @@ func TestMakeAdmin(t *testing.T) { } func TestUpdatingUser(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) if err != nil { @@ -121,7 +121,7 @@ func TestUpdatingUser(t *testing.T) { } func TestDeletingUser(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) if err != nil { @@ -150,7 +150,7 @@ func TestDeletingUser(t *testing.T) { } func TestSetPassword(t *testing.T) { - db := openTestDB() + db := openTestDB(t) user, err := NewUser(db, "marpaia", "foobar", "mike@kolide.co", false, false) if err != nil {