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 TestInitInfoLoggerFailure(t *testing.T) {
45 log, err := InitLogger(99)
50 func TestSyncSuccess(t *testing.T){
51 logFile, err := os.Create("./loggerTest.txt")
53 t.Errorf("logger_test.TestSyncSuccess - failed to create file, error: %s", err)
57 log, err := InitLogger(DebugLevel)
59 t.Errorf("logger_test.TestSyncSuccess - failed to initialize logger, error: %s", err)
65 logFile, err = os.Open("./loggerTest.txt")
67 t.Errorf("logger_test.TestSyncSuccess - failed to open file, error: %s", err)
70 _, err = io.Copy(&buf, logFile)
72 t.Errorf("logger_test.TestSyncSuccess - failed to copy bytes, error: %s", err)
74 debugRecord,_ :=buf.ReadString('\n')
75 errorRecord,_ :=buf.ReadString('\n')
77 assert.NotEmpty(t, debugRecord)
78 assert.Empty(t, errorRecord)
79 err = os.Remove("./loggerTest.txt")
81 t.Errorf("logger_test.TestSyncSuccess - failed to remove file, error: %s", err)
86 func TestSyncFailure(t *testing.T){
87 log, err := InitLogger(DebugLevel)
92 func TestDebugEnabledFalse(t *testing.T){
93 entryNum, log := countRecords(InfoLevel, t)
94 assert.False(t, log.DebugEnabled())
95 assert.Equal(t,3, entryNum)
98 func TestDebugEnabledTrue(t *testing.T){
99 entryNum, log := countRecords(DebugLevel, t)
100 assert.True(t, log.DebugEnabled())
101 assert.Equal(t,4, entryNum)
104 func TestDPanicfDebugLevel(t *testing.T){
105 assert.True(t,validateRecordExists(DebugLevel, zap.DPanicLevel, t))
108 func TestDPanicfInfoLevel(t *testing.T){
109 assert.True(t,validateRecordExists(InfoLevel, zap.DPanicLevel, t))
112 func TestErrorfDebugLevel(t *testing.T) {
113 assert.True(t,validateRecordExists(DebugLevel, zap.ErrorLevel, t))
116 func TestErrorfInfoLevel(t *testing.T) {
117 assert.True(t,validateRecordExists(InfoLevel, zap.ErrorLevel, t))
120 func TestInfofDebugLevel(t *testing.T) {
121 assert.True(t,validateRecordExists(DebugLevel, zap.InfoLevel, t))
124 func TestInfofInfoLevel(t *testing.T) {
125 assert.True(t,validateRecordExists(InfoLevel, zap.InfoLevel, t))
128 func TestDebugfDebugLevel(t *testing.T) {
129 assert.True(t,validateRecordExists(DebugLevel, zap.DebugLevel, t))
132 func TestDebugfInfoLevel(t *testing.T) {
133 assert.False(t,validateRecordExists(InfoLevel, zap.DebugLevel, t))
136 func TestInfofFatalLevel(t *testing.T) {
137 assert.False(t,validateRecordExists(FatalLevel, zap.InfoLevel, t))
140 func TestDebugfFatalLevel(t *testing.T) {
141 assert.False(t,validateRecordExists(FatalLevel, zap.DebugLevel, t))
144 func TestLogLevelTokenToLevel(t *testing.T) {
145 level, ok := LogLevelTokenToLevel("deBug")
147 assert.True(t, level == DebugLevel)
149 level, ok = LogLevelTokenToLevel("infO")
151 assert.True(t, level == InfoLevel)
153 level, ok = LogLevelTokenToLevel("Warn")
155 assert.True(t, level == WarnLevel)
157 level, ok = LogLevelTokenToLevel("eRror")
159 assert.True(t, level == ErrorLevel)
161 level, ok = LogLevelTokenToLevel("Dpanic ")
163 assert.True(t, level == DPanicLevel)
165 level, ok = LogLevelTokenToLevel(" panic ")
167 assert.True(t, level == PanicLevel)
169 level, ok = LogLevelTokenToLevel("fatal")
171 assert.True(t, level == FatalLevel)
173 level, ok = LogLevelTokenToLevel("zzz")
175 assert.True(t, level > FatalLevel)
178 func countRecords(logLevel LogLevel, t *testing.T) (int, *Logger){
182 log, err := InitLogger(logLevel)
184 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
186 log.Infof("%v, %v, %v", 1, "abc", 0.1)
187 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
188 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
189 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
192 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
196 _, err = io.Copy(&buf, r)
198 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
201 s,_:= buf.ReadString('\n')
204 s,_= buf.ReadString('\n')
209 func validateRecordExists(logLevel LogLevel, recordLevel zapcore.Level, t *testing.T) bool {
213 log, err := InitLogger(logLevel)
215 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
219 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
221 log.Infof("%v, %v, %v", 1, "abc", 0.1)
223 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
224 case zap.DPanicLevel:
225 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
229 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
233 _, err = io.Copy(&buf, r)
235 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
238 s,_:= buf.ReadString('\n')
241 s,_= buf.ReadString('\n')