Skip to content

Commit

Permalink
REGTEST: Added unit tests for logs (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
pierresouchay authored Apr 16, 2020
1 parent 6a6fdaa commit 3f37a0f
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions haproxy/halog/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package halog

import (
"fmt"
"testing"

"github.com/bmizerany/assert"
"github.com/sirupsen/logrus"
)

type fakeHook struct {
lastMessage map[logrus.Level]string
}

// newFakeHook build a fake hook to retrive last error messages
func newFakeHook() *fakeHook {
h := fakeHook{}
h.lastMessage = make(map[logrus.Level]string, 4)
return &h
}

func (*fakeHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *fakeHook) Fire(e *logrus.Entry) error {
h.lastMessage[e.Level] = e.Message
return nil
}

func ensureLogIsPresent(t *testing.T, hook *fakeHook, expectedLevel logrus.Level, prefix, msg string) {
haproxyLog("haproxy", fmt.Sprintf("%s%s", prefix, msg))
assert.Equal(t, fmt.Sprintf("haproxy: %s", msg), hook.lastMessage[expectedLevel])
}

func Test_log(t *testing.T) {
log := logrus.StandardLogger()
hook := newFakeHook()
log.AddHook(hook)
// Test the parsing that should fail being parsed
ensureLogIsPresent(t, hook, logrus.ErrorLevel, "", "lol")
ensureLogIsPresent(t, hook, logrus.ErrorLevel, "", "[lol")
ensureLogIsPresent(t, hook, logrus.ErrorLevel, "", "lol]")
ensureLogIsPresent(t, hook, logrus.ErrorLevel, "", "[]")

// while crap is well formatted, no recognized as valid type
ensureLogIsPresent(t, hook, logrus.ErrorLevel, "", "[CRAP] Unknown kind of error")

ensureLogIsPresent(t, hook, logrus.InfoLevel, "[NOTICE]", "yup")
ensureLogIsPresent(t, hook, logrus.InfoLevel, "[NOTICE]", "")

ensureLogIsPresent(t, hook, logrus.WarnLevel, "[WARNING]", "yup")
ensureLogIsPresent(t, hook, logrus.ErrorLevel, "[ALERT]", "yup")
}

0 comments on commit 3f37a0f

Please sign in to comment.