Skip to content

Commit

Permalink
improve test coverage of the logging components
Browse files Browse the repository at this point in the history
  • Loading branch information
kpacha committed May 7, 2017
1 parent b6572f5 commit 30bfdef
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 7 deletions.
10 changes: 4 additions & 6 deletions config/viper/parser.go
Expand Up @@ -24,14 +24,12 @@ func (p parser) Parse(configFile string) (config.ServiceConfig, error) {
p.viper.AutomaticEnv()
var cfg config.ServiceConfig
if err := p.viper.ReadInConfig(); err != nil {
return cfg, fmt.Errorf("Fatal error config file: %s \n", err)
return cfg, fmt.Errorf("Fatal error config file: %s \n", err.Error())
}
if err := p.viper.Unmarshal(&cfg); err != nil {
return cfg, fmt.Errorf("Fatal error unmarshalling config file: %s \n", err)
}
if err := cfg.Init(); err != nil {
return cfg, err
return cfg, fmt.Errorf("Fatal error unmarshalling config file: %s \n", err.Error())
}
err := cfg.Init()

return cfg, nil
return cfg, err
}
15 changes: 14 additions & 1 deletion logging/gologging/log_test.go
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"regexp"
"testing"

gologging "github.com/op/go-logging"
)

const (
Expand Down Expand Up @@ -34,6 +36,17 @@ func TestNewLogger(t *testing.T) {
}
}

func TestNewLogger_unknownLevel(t *testing.T) {
_, err := NewLogger("UNKNOWN", bytes.NewBuffer(make([]byte, 1024)), "pref")
if err == nil {
t.Error("The factory didn't return the expected error")
return
}
if err != gologging.ErrInvalidLogLevel {
t.Errorf("The factory didn't return the expected error. Got: %s", err.Error())
}
}

func logSomeStuff(level string) string {
buff := bytes.NewBuffer(make([]byte, 1024))
logger, _ := NewLogger(level, buff, "pref")
Expand All @@ -44,5 +57,5 @@ func logSomeStuff(level string) string {
logger.Error(errorMsg)
logger.Critical(criticalMsg)

return string(buff.Bytes())
return buff.String()
}
136 changes: 136 additions & 0 deletions proxy/logging_test.go
@@ -0,0 +1,136 @@
package proxy

import (
"bytes"
"context"
"fmt"
"strings"
"testing"

"github.com/devopsfaith/krakend/logging/gologging"
)

func TestNewLoggingMiddleware_multipleNext(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("The code did not panic")
}
}()
buff := bytes.NewBuffer(make([]byte, 1024))
logger, _ := gologging.NewLogger("INFO", buff, "pref")
mw := NewLoggingMiddleware(logger, "supu")
mw(explosiveProxy(t), explosiveProxy(t))
}

func TestNewLoggingMiddleware_ok(t *testing.T) {
buff := bytes.NewBuffer(make([]byte, 1024))
logger, _ := gologging.NewLogger("DEBUG", buff, "pref")
resp := &Response{IsComplete: true}
mw := NewLoggingMiddleware(logger, "supu")
p := mw(dummyProxy(resp))
r, err := p(context.Background(), &Request{})
if r != resp {
t.Error("The proxy didn't return the expected response")
return
}
if err != nil {
t.Errorf("The proxy returned an unexpected error: %s", err.Error())
return
}
logMsg := buff.String()
if strings.Count(logMsg, "pref") != 3 {
t.Error("The logs don't have the injected prefix")
}
if strings.Count(logMsg, "INFO") != 2 {
t.Error("The logs don't have the expected INFO messages")
}
if strings.Count(logMsg, "DEBU") != 1 {
t.Error("The logs don't have the expected DEBUG messages")
}
if !strings.Contains(logMsg, "supu Calling backend") {
t.Error("The logs didn't mark the start of the execution")
}
if !strings.Contains(logMsg, "supu Call to backend took") {
t.Error("The logs didn't mark the end of the execution")
}
}

func TestNewLoggingMiddleware_erroredResponse(t *testing.T) {
buff := bytes.NewBuffer(make([]byte, 1024))
logger, _ := gologging.NewLogger("DEBUG", buff, "pref")
resp := &Response{IsComplete: true}
mw := NewLoggingMiddleware(logger, "supu")
expextedError := fmt.Errorf("NO-body expects the %s Inquisition!", "Spanish")
p := mw(func(_ context.Context, _ *Request) (*Response, error) {
return resp, expextedError
})
r, err := p(context.Background(), &Request{})
if r != resp {
t.Error("The proxy didn't return the expected response")
return
}
if err != expextedError {
t.Errorf("The proxy didn't return the expected error: %s", err.Error())
return
}
logMsg := buff.String()
if strings.Count(logMsg, "pref") != 4 {
t.Error("The logs don't have the injected prefix")
}
if strings.Count(logMsg, "INFO") != 2 {
t.Error("The logs don't have the expected INFO messages")
}
if strings.Count(logMsg, "DEBU") != 1 {
t.Error("The logs don't have the expected DEBUG messages")
}
if strings.Count(logMsg, "WARN") != 1 {
t.Error("The logs don't have the expected DEBUG messages")
}
if !strings.Contains(logMsg, "supu Call to backend failed: NO-body expects the Spanish Inquisition!") {
t.Error("The logs didn't mark the fail of the execution")
}
if !strings.Contains(logMsg, "supu Calling backend") {
t.Error("The logs didn't mark the start of the execution")
}
if !strings.Contains(logMsg, "supu Call to backend took") {
t.Error("The logs didn't mark the end of the execution")
}
}

func TestNewLoggingMiddleware_nullResponse(t *testing.T) {
buff := bytes.NewBuffer(make([]byte, 1024))
logger, _ := gologging.NewLogger("DEBUG", buff, "pref")
mw := NewLoggingMiddleware(logger, "supu")
p := mw(dummyProxy(nil))
r, err := p(context.Background(), &Request{})
if r != nil {
t.Error("The proxy didn't return the expected response")
return
}
if err != nil {
t.Errorf("The proxy returned an unexpected error: %s", err.Error())
return
}
logMsg := buff.String()
if strings.Count(logMsg, "pref") != 4 {
t.Error("The logs don't have the injected prefix")
}
if strings.Count(logMsg, "INFO") != 2 {
t.Error("The logs don't have the expected INFO messages")
}
if strings.Count(logMsg, "DEBU") != 1 {
t.Error("The logs don't have the expected DEBUG messages")
}
if strings.Count(logMsg, "WARN") != 1 {
t.Error("The logs don't have the expected DEBUG messages")
}
if !strings.Contains(logMsg, "supu Call to backend returned a null response") {
t.Error("The logs didn't mark the fail of the execution")
}
if !strings.Contains(logMsg, "supu Calling backend") {
t.Error("The logs didn't mark the start of the execution")
}
if !strings.Contains(logMsg, "supu Call to backend took") {
t.Error("The logs didn't mark the end of the execution")
}
}

0 comments on commit 30bfdef

Please sign in to comment.