Turn down logging in tests except after failure (#41)

When tests succeed, there is now no logging of DB queries or HTTP
requests. If a test fails, the logs will be output.
This commit is contained in:
Zachary Wasserman 2016-08-04 21:10:23 -07:00 committed by Mike Arpaia
parent 670aab219b
commit cd8057e860
8 changed files with 53 additions and 32 deletions

View File

@ -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 {

View File

@ -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)

View File

@ -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)

View File

@ -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")

View File

@ -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)

View File

@ -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) {

View File

@ -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)
}

View File

@ -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 {