diff options
author | Lukasz Wojciechowski <l.wojciechow@partner.samsung.com> | 2018-07-24 15:42:15 +0200 |
---|---|---|
committer | Lukasz Wojciechowski <l.wojciechow@partner.samsung.com> | 2018-09-21 15:40:12 +0200 |
commit | 87633e0ae41ae8c680a327885dced4014f63657e (patch) | |
tree | 47df4fd02c947422f3f9159dd17b769b1c4fc245 | |
parent | 0c0a82de91cba991b7d3c93231fc4e29ae51d012 (diff) | |
download | slav-87633e0ae41ae8c680a327885dced4014f63657e.tar.gz slav-87633e0ae41ae8c680a327885dced4014f63657e.tar.bz2 slav-87633e0ae41ae8c680a327885dced4014f63657e.zip |
Add tests for Entry and Logger logging methods
After processing of a log message is completed, testing of all logging
methods becomes available.
Both Entry and Logger methods are tested using backend with mocked up
filter, serializer and writer.
In all tests threshold is set to Debug level, so that all messages
logged during the tests pass threshold and are passed to be processed
by backends.
Change-Id: I10a1365b255fb0caf083f5dee19de889d92e42a5
Signed-off-by: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
-rw-r--r-- | logger/entry_test.go | 78 | ||||
-rw-r--r-- | logger/logger_test.go | 86 |
2 files changed, 161 insertions, 3 deletions
diff --git a/logger/entry_test.go b/logger/entry_test.go index 39564d5..3c1a843 100644 --- a/logger/entry_test.go +++ b/logger/entry_test.go @@ -19,13 +19,17 @@ package logger import ( gomock "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" + T "github.com/onsi/ginkgo/extensions/table" . "github.com/onsi/gomega" ) var _ = Describe("Entry", func() { const ( - backendName = string("backendName") - testMessage = string("Test Message") + backendName = string("backendName") + testMessage = string("Test Message") + anotherTestMessage = string("Another Test Message") + format = string("%s >>> %s") + expectedMessage = string(testMessage + " >>> " + anotherTestMessage) ) var ( ctrl *gomock.Controller @@ -51,6 +55,7 @@ var _ = Describe("Entry", func() { L = NewLogger() L.AddBackend(backendName, mb) entry = &Entry{Logger: L} + L.SetThreshold(DebugLevel) }) AfterEach(func() { ctrl.Finish() @@ -58,7 +63,6 @@ var _ = Describe("Entry", func() { Describe("process", func() { It("should set level and message and pass entry to Logger's backends", func() { - L.SetThreshold(DebugLevel) mf.EXPECT().Verify(entry).DoAndReturn(func(entry *Entry) (bool, error) { Expect(entry.Level).To(Equal(WarningLevel)) Expect(entry.Message).To(Equal(testMessage)) @@ -73,4 +77,72 @@ var _ = Describe("Entry", func() { Expect(entry.Message).To(BeZero()) }) }) + Describe("Log", func() { + T.DescribeTable("should properly build log message and pass to backends", + func(level Level) { + mf.EXPECT().Verify(entry).Return(false, nil) + entry.Log(level, testMessage, anotherTestMessage) + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(testMessage + anotherTestMessage)) + }, + T.Entry("EmergLevel", EmergLevel), + T.Entry("AlertLevel", AlertLevel), + T.Entry("CritLevel", CritLevel), + T.Entry("ErrLevel", ErrLevel), + T.Entry("WarningLevel", WarningLevel), + T.Entry("NoticeLevel", NoticeLevel), + T.Entry("InfoLevel", InfoLevel), + T.Entry("DebugLevel", DebugLevel), + ) + T.DescribeTable("should properly set level and log message and pass to logger's backend", + func(level Level, testedFunction func(*Entry, ...interface{})) { + mf.EXPECT().Verify(entry).Return(false, nil) + testedFunction(entry, testMessage, anotherTestMessage) + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(testMessage + anotherTestMessage)) + }, + T.Entry("EmergLevel", EmergLevel, (*Entry).Emergency), + T.Entry("AlertLevel", AlertLevel, (*Entry).Alert), + T.Entry("CritLevel", CritLevel, (*Entry).Critical), + T.Entry("ErrLevel", ErrLevel, (*Entry).Error), + T.Entry("WarningLevel", WarningLevel, (*Entry).Warning), + T.Entry("NoticeLevel", NoticeLevel, (*Entry).Notice), + T.Entry("InfoLevel", InfoLevel, (*Entry).Info), + T.Entry("DebugLevel", DebugLevel, (*Entry).Debug), + ) + }) + Describe("Logf", func() { + T.DescribeTable("should properly build log message and pass to logger's backend", + func(level Level) { + mf.EXPECT().Verify(entry).Return(false, nil) + entry.Logf(level, format, testMessage, anotherTestMessage) + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(expectedMessage)) + }, + T.Entry("EmergLevel", EmergLevel), + T.Entry("AlertLevel", AlertLevel), + T.Entry("CritLevel", CritLevel), + T.Entry("ErrLevel", ErrLevel), + T.Entry("WarningLevel", WarningLevel), + T.Entry("NoticeLevel", NoticeLevel), + T.Entry("InfoLevel", InfoLevel), + T.Entry("DebugLevel", DebugLevel), + ) + T.DescribeTable("should properly set level and log message and pass to logger's backend", + func(level Level, testedFunction func(*Entry, string, ...interface{})) { + mf.EXPECT().Verify(entry).Return(false, nil) + testedFunction(entry, format, testMessage, anotherTestMessage) + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(expectedMessage)) + }, + T.Entry("EmergLevel", EmergLevel, (*Entry).Emergencyf), + T.Entry("AlertLevel", AlertLevel, (*Entry).Alertf), + T.Entry("CritLevel", CritLevel, (*Entry).Criticalf), + T.Entry("ErrLevel", ErrLevel, (*Entry).Errorf), + T.Entry("WarningLevel", WarningLevel, (*Entry).Warningf), + T.Entry("NoticeLevel", NoticeLevel, (*Entry).Noticef), + T.Entry("InfoLevel", InfoLevel, (*Entry).Infof), + T.Entry("DebugLevel", DebugLevel, (*Entry).Debugf), + ) + }) }) diff --git a/logger/logger_test.go b/logger/logger_test.go index 051cfb0..661cbca 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -49,6 +49,10 @@ var _ = Describe("Logger", func() { const ( backendName = string("backendName") anotherBackendName = string("anotherBackendName") + testMessage = string("Test Message") + anotherTestMessage = string("Another Test Message") + format = string("%s >>> %s") + expectedMessage = string(testMessage + " >>> " + anotherTestMessage) ) var ( ctrl *gomock.Controller @@ -272,4 +276,86 @@ var _ = Describe("Logger", func() { Expect(stderr).To(ContainSubstring(buildError(testError, anotherBackendName))) }) }) + Describe("Log methods", func() { + BeforeEach(func() { + L.AddBackend(backendName, mb) + L.SetThreshold(DebugLevel) + }) + Describe("Log", func() { + T.DescribeTable("should properly build log message and pass to logger's backend", + func(level Level) { + mf.EXPECT().Verify(gomock.Any()).DoAndReturn(func(entry *Entry) (bool, error) { + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(testMessage + anotherTestMessage)) + return false, nil + }) + L.Log(level, testMessage, anotherTestMessage) + }, + T.Entry("EmergLevel", EmergLevel), + T.Entry("AlertLevel", AlertLevel), + T.Entry("CritLevel", CritLevel), + T.Entry("ErrLevel", ErrLevel), + T.Entry("WarningLevel", WarningLevel), + T.Entry("NoticeLevel", NoticeLevel), + T.Entry("InfoLevel", InfoLevel), + T.Entry("DebugLevel", DebugLevel), + ) + T.DescribeTable("should properly set level and log message and pass to logger's backend", + func(level Level, testedFunction func(*Logger, ...interface{})) { + mf.EXPECT().Verify(gomock.Any()).DoAndReturn(func(entry *Entry) (bool, error) { + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(testMessage + anotherTestMessage)) + return false, nil + }) + testedFunction(L, testMessage, anotherTestMessage) + }, + T.Entry("EmergLevel", EmergLevel, (*Logger).Emergency), + T.Entry("AlertLevel", AlertLevel, (*Logger).Alert), + T.Entry("CritLevel", CritLevel, (*Logger).Critical), + T.Entry("ErrLevel", ErrLevel, (*Logger).Error), + T.Entry("WarningLevel", WarningLevel, (*Logger).Warning), + T.Entry("NoticeLevel", NoticeLevel, (*Logger).Notice), + T.Entry("InfoLevel", InfoLevel, (*Logger).Info), + T.Entry("DebugLevel", DebugLevel, (*Logger).Debug), + ) + }) + Describe("Logf", func() { + T.DescribeTable("should properly build log message and pass to logger's backend", + func(level Level) { + mf.EXPECT().Verify(gomock.Any()).DoAndReturn(func(entry *Entry) (bool, error) { + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(expectedMessage)) + return false, nil + }) + L.Logf(level, format, testMessage, anotherTestMessage) + }, + T.Entry("EmergLevel", EmergLevel), + T.Entry("AlertLevel", AlertLevel), + T.Entry("CritLevel", CritLevel), + T.Entry("ErrLevel", ErrLevel), + T.Entry("WarningLevel", WarningLevel), + T.Entry("NoticeLevel", NoticeLevel), + T.Entry("InfoLevel", InfoLevel), + T.Entry("DebugLevel", DebugLevel), + ) + T.DescribeTable("should properly set level and log message and pass to logger's backend", + func(level Level, testedFunction func(*Logger, string, ...interface{})) { + mf.EXPECT().Verify(gomock.Any()).DoAndReturn(func(entry *Entry) (bool, error) { + Expect(entry.Level).To(Equal(level)) + Expect(entry.Message).To(Equal(expectedMessage)) + return false, nil + }) + testedFunction(L, format, testMessage, anotherTestMessage) + }, + T.Entry("EmergLevel", EmergLevel, (*Logger).Emergencyf), + T.Entry("AlertLevel", AlertLevel, (*Logger).Alertf), + T.Entry("CritLevel", CritLevel, (*Logger).Criticalf), + T.Entry("ErrLevel", ErrLevel, (*Logger).Errorf), + T.Entry("WarningLevel", WarningLevel, (*Logger).Warningf), + T.Entry("NoticeLevel", NoticeLevel, (*Logger).Noticef), + T.Entry("InfoLevel", InfoLevel, (*Logger).Infof), + T.Entry("DebugLevel", DebugLevel, (*Logger).Debugf), + ) + }) + }) }) |