mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Reduce test log verbosity (#150)
* Reduce test verbosity * Divert gin's log to the test buffer * Divert stdlib's log to the test buffer * Add bolt tests into log buffer * Add a linebreak to improve log output layout
This commit is contained in:
committed by
Seif Lotfy سيف لطفي
parent
5a1d9d4825
commit
4cbfb3ccfd
@@ -2,17 +2,30 @@ package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/iron-io/functions/api/datastore"
|
||||
"github.com/iron-io/functions/api/models"
|
||||
"github.com/iron-io/functions/api/mqs"
|
||||
)
|
||||
|
||||
func TestAppCreate(t *testing.T) {
|
||||
func setLogBuffer() *bytes.Buffer {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteByte('\n')
|
||||
logrus.SetOutput(&buf)
|
||||
gin.DefaultErrorWriter = &buf
|
||||
gin.DefaultWriter = &buf
|
||||
log.SetOutput(&buf)
|
||||
return &buf
|
||||
}
|
||||
|
||||
func TestAppCreate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -38,6 +51,7 @@ func TestAppCreate(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "POST", test.path, body)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -46,6 +60,7 @@ func TestAppCreate(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -54,6 +69,7 @@ func TestAppCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppDelete(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -69,6 +85,7 @@ func TestAppDelete(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "DELETE", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -77,6 +94,7 @@ func TestAppDelete(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -85,6 +103,7 @@ func TestAppDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppList(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -99,6 +118,7 @@ func TestAppList(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "GET", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -107,6 +127,7 @@ func TestAppList(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -115,6 +136,7 @@ func TestAppList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -129,6 +151,7 @@ func TestAppGet(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "GET", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -137,6 +160,7 @@ func TestAppGet(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -145,6 +169,7 @@ func TestAppGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAppUpdate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -164,6 +189,7 @@ func TestAppUpdate(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "PUT", test.path, body)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -172,6 +198,7 @@ func TestAppUpdate(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func TestRouteCreate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -37,6 +38,7 @@ func TestRouteCreate(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "POST", test.path, body)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -45,6 +47,7 @@ func TestRouteCreate(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -53,6 +56,7 @@ func TestRouteCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteDelete(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -68,6 +72,7 @@ func TestRouteDelete(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "DELETE", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -76,6 +81,7 @@ func TestRouteDelete(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -84,6 +90,7 @@ func TestRouteDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteList(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -98,6 +105,7 @@ func TestRouteList(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "GET", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -106,6 +114,7 @@ func TestRouteList(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -114,6 +123,7 @@ func TestRouteList(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -128,6 +138,7 @@ func TestRouteGet(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "GET", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -136,6 +147,7 @@ func TestRouteGet(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -144,6 +156,7 @@ func TestRouteGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteUpdate(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{}, &mqs.Mock{}, testRunner(t))
|
||||
router := testRouter()
|
||||
|
||||
@@ -165,6 +178,7 @@ func TestRouteUpdate(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "PUT", test.path, body)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -173,6 +187,7 @@ func TestRouteUpdate(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ func testRunner(t *testing.T) *runner.Runner {
|
||||
}
|
||||
|
||||
func TestRouteRunnerGet(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{
|
||||
FakeApps: []*models.App{
|
||||
{Name: "myapp", Config: models.Config{}},
|
||||
@@ -42,6 +43,7 @@ func TestRouteRunnerGet(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "GET", test.path, nil)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -50,6 +52,7 @@ func TestRouteRunnerGet(t *testing.T) {
|
||||
resp := getErrorResponse(t, rec)
|
||||
|
||||
if !strings.Contains(resp.Error.Message, test.expectedError.Error()) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -58,6 +61,7 @@ func TestRouteRunnerGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteRunnerPost(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{
|
||||
FakeApps: []*models.App{
|
||||
{Name: "myapp", Config: models.Config{}},
|
||||
@@ -79,6 +83,7 @@ func TestRouteRunnerPost(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "POST", test.path, body)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -89,6 +94,7 @@ func TestRouteRunnerPost(t *testing.T) {
|
||||
expMsg := test.expectedError.Error()
|
||||
fmt.Println(respMsg == expMsg)
|
||||
if respMsg != expMsg && !strings.Contains(respMsg, expMsg) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected error message to have `%s`",
|
||||
i, test.expectedError.Error())
|
||||
}
|
||||
@@ -97,6 +103,7 @@ func TestRouteRunnerPost(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRouteRunnerExecution(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
New(&datastore.Mock{
|
||||
FakeApps: []*models.App{
|
||||
{Name: "myapp", Config: models.Config{}},
|
||||
@@ -125,6 +132,7 @@ func TestRouteRunnerExecution(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, "GET", test.path, body)
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
@@ -132,6 +140,7 @@ func TestRouteRunnerExecution(t *testing.T) {
|
||||
if test.expectedHeaders != nil {
|
||||
for name, header := range test.expectedHeaders {
|
||||
if header[0] != rec.Header().Get(name) {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected header `%s` to be %s but was %s",
|
||||
i, name, header[0], rec.Header().Get(name))
|
||||
}
|
||||
@@ -141,6 +150,7 @@ func TestRouteRunnerExecution(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMatchRoute(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
for i, test := range []struct {
|
||||
baseRoute string
|
||||
route string
|
||||
@@ -154,12 +164,14 @@ func TestMatchRoute(t *testing.T) {
|
||||
if test.expectedParams != nil {
|
||||
for j, param := range test.expectedParams {
|
||||
if params[j].Key != param.Key || params[j].Value != param.Value {
|
||||
t.Log(buf.String())
|
||||
fmt.Println(params[j])
|
||||
t.Errorf("Test %d: expected param %d, key = %s, value = %s", i, j, param.Key, param.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: %s should match %s", i, test.route, test.baseRoute)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ func prepareBolt(t *testing.T) (models.Datastore, func()) {
|
||||
}
|
||||
|
||||
func TestFullStack(t *testing.T) {
|
||||
buf := setLogBuffer()
|
||||
ds, close := prepareBolt(t)
|
||||
defer close()
|
||||
|
||||
@@ -116,6 +117,7 @@ func TestFullStack(t *testing.T) {
|
||||
_, rec := routerRequest(t, router, test.method, test.path, bytes.NewBuffer([]byte(test.body)))
|
||||
|
||||
if rec.Code != test.expectedCode {
|
||||
t.Log(buf.String())
|
||||
t.Errorf("Test %d: Expected status code to be %d but was %d",
|
||||
i, test.expectedCode, rec.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user