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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
25 "github.com/stretchr/testify/assert"
27 "go.uber.org/zap/zapcore"
33 func TestInitDebugLoggerSuccess(t *testing.T) {
34 log, err := InitLogger(DebugLevel)
37 assert.True(t, log.Logger.Core().Enabled(zap.DebugLevel))
40 func TestInitInfoLoggerSuccess(t *testing.T) {
41 log, err := InitLogger(InfoLevel)
44 assert.True(t, log.Logger.Core().Enabled(zap.InfoLevel))
47 func TestInitWarnLoggerSuccess(t *testing.T) {
48 log, err := InitLogger(WarnLevel)
51 assert.True(t, log.Logger.Core().Enabled(zap.WarnLevel))
54 func TestInitErrorLoggerSuccess(t *testing.T) {
55 log, err := InitLogger(ErrorLevel)
58 assert.True(t, log.Logger.Core().Enabled(zap.ErrorLevel))
61 func TestInitDPanicLoggerSuccess(t *testing.T) {
62 log, err := InitLogger(DPanicLevel)
65 assert.True(t, log.Logger.Core().Enabled(zap.DPanicLevel))
68 func TestInitPanicLoggerSuccess(t *testing.T) {
69 log, err := InitLogger(PanicLevel)
72 assert.True(t, log.Logger.Core().Enabled(zap.PanicLevel))
75 func TestInitInfoLoggerFailure(t *testing.T) {
76 log, err := InitLogger(99)
81 func TestSyncSuccess(t *testing.T){
82 logFile, err := os.Create("./loggerTest.txt")
84 t.Errorf("logger_test.TestSyncSuccess - failed to create file, error: %s", err)
88 log, err := InitLogger(DebugLevel)
90 t.Errorf("logger_test.TestSyncSuccess - failed to initialize logger, error: %s", err)
96 logFile, err = os.Open("./loggerTest.txt")
98 t.Errorf("logger_test.TestSyncSuccess - failed to open file, error: %s", err)
101 _, err = io.Copy(&buf, logFile)
103 t.Errorf("logger_test.TestSyncSuccess - failed to copy bytes, error: %s", err)
105 debugRecord,_ :=buf.ReadString('\n')
106 errorRecord,_ :=buf.ReadString('\n')
108 assert.NotEmpty(t, debugRecord)
109 assert.Empty(t, errorRecord)
110 err = os.Remove("./loggerTest.txt")
112 t.Errorf("logger_test.TestSyncSuccess - failed to remove file, error: %s", err)
117 func TestSyncFailure(t *testing.T){
118 log, err := InitLogger(DebugLevel)
120 assert.NotNil(t, err)
123 func TestDebugEnabledFalse(t *testing.T){
124 entryNum, log := countRecords(InfoLevel, t)
125 assert.False(t, log.DebugEnabled())
126 assert.Equal(t,3, entryNum)
129 func TestDebugEnabledTrue(t *testing.T){
130 entryNum, log := countRecords(DebugLevel, t)
131 assert.True(t, log.DebugEnabled())
132 assert.Equal(t,4, entryNum)
135 func TestDPanicfDebugLevel(t *testing.T){
136 assert.True(t,validateRecordExists(DebugLevel, zap.DPanicLevel, t))
139 func TestDPanicfInfoLevel(t *testing.T){
140 assert.True(t,validateRecordExists(InfoLevel, zap.DPanicLevel, t))
143 func TestErrorfDebugLevel(t *testing.T) {
144 assert.True(t,validateRecordExists(DebugLevel, zap.ErrorLevel, t))
147 func TestErrorfInfoLevel(t *testing.T) {
148 assert.True(t,validateRecordExists(InfoLevel, zap.ErrorLevel, t))
151 func TestInfofDebugLevel(t *testing.T) {
152 assert.True(t,validateRecordExists(DebugLevel, zap.InfoLevel, t))
155 func TestInfofInfoLevel(t *testing.T) {
156 assert.True(t,validateRecordExists(InfoLevel, zap.InfoLevel, t))
159 func TestDebugfDebugLevel(t *testing.T) {
160 assert.True(t,validateRecordExists(DebugLevel, zap.DebugLevel, t))
163 func TestDebugfInfoLevel(t *testing.T) {
164 assert.False(t,validateRecordExists(InfoLevel, zap.DebugLevel, t))
167 func TestInfofFatalLevel(t *testing.T) {
168 assert.False(t,validateRecordExists(FatalLevel, zap.InfoLevel, t))
171 func TestDebugfFatalLevel(t *testing.T) {
172 assert.False(t,validateRecordExists(FatalLevel, zap.DebugLevel, t))
175 func TestWarnfWarnLevel(t *testing.T) {
176 assert.True(t,validateRecordExists(WarnLevel, zap.WarnLevel, t))
179 func TestWarnfDebugLevel(t *testing.T) {
180 assert.True(t,validateRecordExists(DebugLevel, zap.WarnLevel, t))
183 func TestWarnfInfoLevel(t *testing.T) {
184 assert.True(t,validateRecordExists(InfoLevel, zap.WarnLevel, t))
187 func TestWarnfFatalLevel(t *testing.T) {
188 assert.False(t,validateRecordExists(FatalLevel, zap.WarnLevel, t))
191 func TestLogLevelTokenToLevel(t *testing.T) {
192 level, ok := LogLevelTokenToLevel("deBug")
194 assert.True(t, level == DebugLevel)
196 level, ok = LogLevelTokenToLevel("infO")
198 assert.True(t, level == InfoLevel)
200 level, ok = LogLevelTokenToLevel("Warn")
202 assert.True(t, level == WarnLevel)
204 level, ok = LogLevelTokenToLevel("eRror")
206 assert.True(t, level == ErrorLevel)
208 level, ok = LogLevelTokenToLevel("Dpanic ")
210 assert.True(t, level == DPanicLevel)
212 level, ok = LogLevelTokenToLevel(" panic ")
214 assert.True(t, level == PanicLevel)
216 level, ok = LogLevelTokenToLevel("fatal")
218 assert.True(t, level == FatalLevel)
220 level, ok = LogLevelTokenToLevel("zzz")
222 assert.True(t, level > FatalLevel)
225 func countRecords(logLevel LogLevel, t *testing.T) (int, *Logger){
229 log, err := InitLogger(logLevel)
231 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
233 log.Infof("%v, %v, %v", 1, "abc", 0.1)
234 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
235 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
236 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
239 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
243 _, err = io.Copy(&buf, r)
245 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
248 s,_:= buf.ReadString('\n')
251 s,_= buf.ReadString('\n')
256 func validateRecordExists(logLevel LogLevel, recordLevel zapcore.Level, t *testing.T) bool {
260 log, err := InitLogger(logLevel)
262 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
266 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
268 log.Infof("%v, %v, %v", 1, "abc", 0.1)
270 log.Warnf("%v, %v, %v", 1, "abc", 0.1)
272 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
273 case zap.DPanicLevel:
274 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
278 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
282 _, err = io.Copy(&buf, r)
284 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
287 s,_:= buf.ReadString('\n')
290 s,_= buf.ReadString('\n')