2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
22 "github.com/stretchr/testify/assert"
24 "go.uber.org/zap/zapcore"
30 func TestInitDebugLoggerSuccess(t *testing.T) {
31 log, err := InitLogger(DebugLevel)
34 assert.True(t, log.Logger.Core().Enabled(zap.DebugLevel))
37 func TestInitInfoLoggerSuccess(t *testing.T) {
38 log, err := InitLogger(InfoLevel)
41 assert.True(t, log.Logger.Core().Enabled(zap.InfoLevel))
44 func TestInitWarnLoggerSuccess(t *testing.T) {
45 log, err := InitLogger(WarnLevel)
48 assert.True(t, log.Logger.Core().Enabled(zap.WarnLevel))
51 func TestInitErrorLoggerSuccess(t *testing.T) {
52 log, err := InitLogger(ErrorLevel)
55 assert.True(t, log.Logger.Core().Enabled(zap.ErrorLevel))
58 func TestInitDPanicLoggerSuccess(t *testing.T) {
59 log, err := InitLogger(DPanicLevel)
62 assert.True(t, log.Logger.Core().Enabled(zap.DPanicLevel))
65 func TestInitPanicLoggerSuccess(t *testing.T) {
66 log, err := InitLogger(PanicLevel)
69 assert.True(t, log.Logger.Core().Enabled(zap.PanicLevel))
72 func TestInitInfoLoggerFailure(t *testing.T) {
73 log, err := InitLogger(99)
78 func TestSyncSuccess(t *testing.T){
79 logFile, err := os.Create("./loggerTest.txt")
81 t.Errorf("logger_test.TestSyncSuccess - failed to create file, error: %s", err)
85 log, err := InitLogger(DebugLevel)
87 t.Errorf("logger_test.TestSyncSuccess - failed to initialize logger, error: %s", err)
93 logFile, err = os.Open("./loggerTest.txt")
95 t.Errorf("logger_test.TestSyncSuccess - failed to open file, error: %s", err)
98 _, err = io.Copy(&buf, logFile)
100 t.Errorf("logger_test.TestSyncSuccess - failed to copy bytes, error: %s", err)
102 debugRecord,_ :=buf.ReadString('\n')
103 errorRecord,_ :=buf.ReadString('\n')
105 assert.NotEmpty(t, debugRecord)
106 assert.Empty(t, errorRecord)
107 err = os.Remove("./loggerTest.txt")
109 t.Errorf("logger_test.TestSyncSuccess - failed to remove file, error: %s", err)
114 func TestSyncFailure(t *testing.T){
115 log, err := InitLogger(DebugLevel)
117 assert.NotNil(t, err)
120 func TestDebugEnabledFalse(t *testing.T){
121 entryNum, log := countRecords(InfoLevel, t)
122 assert.False(t, log.DebugEnabled())
123 assert.Equal(t,3, entryNum)
126 func TestDebugEnabledTrue(t *testing.T){
127 entryNum, log := countRecords(DebugLevel, t)
128 assert.True(t, log.DebugEnabled())
129 assert.Equal(t,4, entryNum)
132 func TestDPanicfDebugLevel(t *testing.T){
133 assert.True(t,validateRecordExists(DebugLevel, zap.DPanicLevel, t))
136 func TestDPanicfInfoLevel(t *testing.T){
137 assert.True(t,validateRecordExists(InfoLevel, zap.DPanicLevel, t))
140 func TestErrorfDebugLevel(t *testing.T) {
141 assert.True(t,validateRecordExists(DebugLevel, zap.ErrorLevel, t))
144 func TestErrorfInfoLevel(t *testing.T) {
145 assert.True(t,validateRecordExists(InfoLevel, zap.ErrorLevel, t))
148 func TestInfofDebugLevel(t *testing.T) {
149 assert.True(t,validateRecordExists(DebugLevel, zap.InfoLevel, t))
152 func TestInfofInfoLevel(t *testing.T) {
153 assert.True(t,validateRecordExists(InfoLevel, zap.InfoLevel, t))
156 func TestDebugfDebugLevel(t *testing.T) {
157 assert.True(t,validateRecordExists(DebugLevel, zap.DebugLevel, t))
160 func TestDebugfInfoLevel(t *testing.T) {
161 assert.False(t,validateRecordExists(InfoLevel, zap.DebugLevel, t))
164 func TestInfofFatalLevel(t *testing.T) {
165 assert.False(t,validateRecordExists(FatalLevel, zap.InfoLevel, t))
168 func TestDebugfFatalLevel(t *testing.T) {
169 assert.False(t,validateRecordExists(FatalLevel, zap.DebugLevel, t))
172 func TestWarnfWarnLevel(t *testing.T) {
173 assert.True(t,validateRecordExists(WarnLevel, zap.WarnLevel, t))
176 func TestWarnfDebugLevel(t *testing.T) {
177 assert.True(t,validateRecordExists(DebugLevel, zap.WarnLevel, t))
180 func TestWarnfInfoLevel(t *testing.T) {
181 assert.True(t,validateRecordExists(InfoLevel, zap.WarnLevel, t))
184 func TestWarnfFatalLevel(t *testing.T) {
185 assert.False(t,validateRecordExists(FatalLevel, zap.WarnLevel, t))
188 func TestLogLevelTokenToLevel(t *testing.T) {
189 level, ok := LogLevelTokenToLevel("deBug")
191 assert.True(t, level == DebugLevel)
193 level, ok = LogLevelTokenToLevel("infO")
195 assert.True(t, level == InfoLevel)
197 level, ok = LogLevelTokenToLevel("Warn")
199 assert.True(t, level == WarnLevel)
201 level, ok = LogLevelTokenToLevel("eRror")
203 assert.True(t, level == ErrorLevel)
205 level, ok = LogLevelTokenToLevel("Dpanic ")
207 assert.True(t, level == DPanicLevel)
209 level, ok = LogLevelTokenToLevel(" panic ")
211 assert.True(t, level == PanicLevel)
213 level, ok = LogLevelTokenToLevel("fatal")
215 assert.True(t, level == FatalLevel)
217 level, ok = LogLevelTokenToLevel("zzz")
219 assert.True(t, level > FatalLevel)
222 func countRecords(logLevel LogLevel, t *testing.T) (int, *Logger){
226 log, err := InitLogger(logLevel)
228 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
230 log.Infof("%v, %v, %v", 1, "abc", 0.1)
231 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
232 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
233 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
236 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
240 _, err = io.Copy(&buf, r)
242 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
245 s,_:= buf.ReadString('\n')
248 s,_= buf.ReadString('\n')
253 func validateRecordExists(logLevel LogLevel, recordLevel zapcore.Level, t *testing.T) bool {
257 log, err := InitLogger(logLevel)
259 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
263 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
265 log.Infof("%v, %v, %v", 1, "abc", 0.1)
267 log.Warnf("%v, %v, %v", 1, "abc", 0.1)
269 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
270 case zap.DPanicLevel:
271 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
275 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
279 _, err = io.Copy(&buf, r)
281 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
284 s,_:= buf.ReadString('\n')
287 s,_= buf.ReadString('\n')