blob: d1d00d27563d735076705c1370b4471fe4facb0d [file] [log] [blame]
ss412g07ef76d2019-08-12 17:26:40 +03001//
2// Copyright 2019 AT&T Intellectual Property
3// Copyright 2019 Nokia
4//
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
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
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.
nm755n15d39822019-11-28 16:56:00 +000016
17// This source code is part of the near-RT RIC (RAN Intelligent Controller)
18// platform project (RICP).
19
ss412g07ef76d2019-08-12 17:26:40 +030020
21package logger
22
23import (
24 "bytes"
25 "github.com/stretchr/testify/assert"
26 "go.uber.org/zap"
27 "go.uber.org/zap/zapcore"
28 "io"
29 "os"
30 "testing"
31)
32
33func TestInitDebugLoggerSuccess(t *testing.T) {
34 log, err := InitLogger(DebugLevel)
35 assert.Nil(t, err)
36 assert.NotNil(t, log)
37 assert.True(t, log.Logger.Core().Enabled(zap.DebugLevel))
38}
39
40func TestInitInfoLoggerSuccess(t *testing.T) {
41 log, err := InitLogger(InfoLevel)
42 assert.Nil(t, err)
43 assert.NotNil(t, log)
44 assert.True(t, log.Logger.Core().Enabled(zap.InfoLevel))
45}
46
ss412gefcb4522019-12-02 16:59:19 +020047func TestInitWarnLoggerSuccess(t *testing.T) {
48 log, err := InitLogger(WarnLevel)
49 assert.Nil(t, err)
50 assert.NotNil(t, log)
51 assert.True(t, log.Logger.Core().Enabled(zap.WarnLevel))
52}
53
54func TestInitErrorLoggerSuccess(t *testing.T) {
55 log, err := InitLogger(ErrorLevel)
56 assert.Nil(t, err)
57 assert.NotNil(t, log)
58 assert.True(t, log.Logger.Core().Enabled(zap.ErrorLevel))
59}
60
61func TestInitDPanicLoggerSuccess(t *testing.T) {
62 log, err := InitLogger(DPanicLevel)
63 assert.Nil(t, err)
64 assert.NotNil(t, log)
65 assert.True(t, log.Logger.Core().Enabled(zap.DPanicLevel))
66}
67
68func TestInitPanicLoggerSuccess(t *testing.T) {
69 log, err := InitLogger(PanicLevel)
70 assert.Nil(t, err)
71 assert.NotNil(t, log)
72 assert.True(t, log.Logger.Core().Enabled(zap.PanicLevel))
73}
74
ss412g07ef76d2019-08-12 17:26:40 +030075func TestInitInfoLoggerFailure(t *testing.T) {
76 log, err := InitLogger(99)
77 assert.NotNil(t, err)
78 assert.Nil(t, log)
79}
80
81func TestSyncSuccess(t *testing.T){
82 logFile, err := os.Create("./loggerTest.txt")
83 if err != nil{
84 t.Errorf("logger_test.TestSyncSuccess - failed to create file, error: %s", err)
85 }
86 old := os.Stdout
87 os.Stdout = logFile
88 log, err := InitLogger(DebugLevel)
89 if err != nil {
90 t.Errorf("logger_test.TestSyncSuccess - failed to initialize logger, error: %s", err)
91 }
92 err = log.Sync()
93 assert.Nil(t, err)
ss412gefcb4522019-12-02 16:59:19 +020094
ss412g07ef76d2019-08-12 17:26:40 +030095 os.Stdout = old
96 logFile, err = os.Open("./loggerTest.txt")
97 if err != nil{
98 t.Errorf("logger_test.TestSyncSuccess - failed to open file, error: %s", err)
99 }
100 var buf bytes.Buffer
101 _, err = io.Copy(&buf, logFile)
102 if err != nil {
103 t.Errorf("logger_test.TestSyncSuccess - failed to copy bytes, error: %s", err)
104 }
105 debugRecord,_ :=buf.ReadString('\n')
106 errorRecord,_ :=buf.ReadString('\n')
107
108 assert.NotEmpty(t, debugRecord)
109 assert.Empty(t, errorRecord)
110 err = os.Remove("./loggerTest.txt")
111 if err != nil {
112 t.Errorf("logger_test.TestSyncSuccess - failed to remove file, error: %s", err)
113 }
114
115}
116
117func TestSyncFailure(t *testing.T){
118 log, err := InitLogger(DebugLevel)
119 err = log.Sync()
120 assert.NotNil(t, err)
121}
122
123func TestDebugEnabledFalse(t *testing.T){
124 entryNum, log := countRecords(InfoLevel, t)
125 assert.False(t, log.DebugEnabled())
126 assert.Equal(t,3, entryNum)
127}
128
129func TestDebugEnabledTrue(t *testing.T){
130 entryNum, log := countRecords(DebugLevel, t)
131 assert.True(t, log.DebugEnabled())
132 assert.Equal(t,4, entryNum)
133}
134
135func TestDPanicfDebugLevel(t *testing.T){
136 assert.True(t,validateRecordExists(DebugLevel, zap.DPanicLevel, t))
137}
138
139func TestDPanicfInfoLevel(t *testing.T){
140 assert.True(t,validateRecordExists(InfoLevel, zap.DPanicLevel, t))
141}
142
143func TestErrorfDebugLevel(t *testing.T) {
144 assert.True(t,validateRecordExists(DebugLevel, zap.ErrorLevel, t))
145}
146
147func TestErrorfInfoLevel(t *testing.T) {
148 assert.True(t,validateRecordExists(InfoLevel, zap.ErrorLevel, t))
149}
150
151func TestInfofDebugLevel(t *testing.T) {
152 assert.True(t,validateRecordExists(DebugLevel, zap.InfoLevel, t))
153}
154
155func TestInfofInfoLevel(t *testing.T) {
156 assert.True(t,validateRecordExists(InfoLevel, zap.InfoLevel, t))
157}
158
159func TestDebugfDebugLevel(t *testing.T) {
160 assert.True(t,validateRecordExists(DebugLevel, zap.DebugLevel, t))
161}
162
163func TestDebugfInfoLevel(t *testing.T) {
164 assert.False(t,validateRecordExists(InfoLevel, zap.DebugLevel, t))
165}
166
167func TestInfofFatalLevel(t *testing.T) {
168 assert.False(t,validateRecordExists(FatalLevel, zap.InfoLevel, t))
169}
170
171func TestDebugfFatalLevel(t *testing.T) {
172 assert.False(t,validateRecordExists(FatalLevel, zap.DebugLevel, t))
173}
174
ss412gefcb4522019-12-02 16:59:19 +0200175func TestWarnfWarnLevel(t *testing.T) {
176 assert.True(t,validateRecordExists(WarnLevel, zap.WarnLevel, t))
177}
178
179func TestWarnfDebugLevel(t *testing.T) {
180 assert.True(t,validateRecordExists(DebugLevel, zap.WarnLevel, t))
181}
182
183func TestWarnfInfoLevel(t *testing.T) {
184 assert.True(t,validateRecordExists(InfoLevel, zap.WarnLevel, t))
185}
186
187func TestWarnfFatalLevel(t *testing.T) {
188 assert.False(t,validateRecordExists(FatalLevel, zap.WarnLevel, t))
189}
190
ss412g07ef76d2019-08-12 17:26:40 +0300191func TestLogLevelTokenToLevel(t *testing.T) {
192 level, ok := LogLevelTokenToLevel("deBug")
193 assert.True(t, ok)
194 assert.True(t, level == DebugLevel)
195
196 level, ok = LogLevelTokenToLevel("infO")
197 assert.True(t, ok)
198 assert.True(t, level == InfoLevel)
199
200 level, ok = LogLevelTokenToLevel("Warn")
201 assert.True(t, ok)
202 assert.True(t, level == WarnLevel)
203
204 level, ok = LogLevelTokenToLevel("eRror")
205 assert.True(t, ok)
206 assert.True(t, level == ErrorLevel)
207
208 level, ok = LogLevelTokenToLevel("Dpanic ")
209 assert.True(t, ok)
210 assert.True(t, level == DPanicLevel)
211
212 level, ok = LogLevelTokenToLevel(" panic ")
213 assert.True(t, ok)
214 assert.True(t, level == PanicLevel)
215
216 level, ok = LogLevelTokenToLevel("fatal")
217 assert.True(t, ok)
218 assert.True(t, level == FatalLevel)
219
220 level, ok = LogLevelTokenToLevel("zzz")
221 assert.False(t, ok)
222 assert.True(t, level > FatalLevel)
223
224}
225func countRecords(logLevel LogLevel, t *testing.T) (int, *Logger){
226 old := os.Stdout
227 r, w, _ :=os.Pipe()
228 os.Stdout = w
229 log, err := InitLogger(logLevel)
230 if err != nil {
231 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
232 }
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)
237 err = w.Close()
238 if err != nil {
239 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
240 }
241 os.Stdout = old
242 var buf bytes.Buffer
243 _, err = io.Copy(&buf, r)
244 if err != nil {
245 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
246 }
247 entryNum := 0
248 s,_:= buf.ReadString('\n')
249 for len(s) > 0{
250 entryNum +=1
251 s,_= buf.ReadString('\n')
252 }
253 return entryNum, log
254}
255
256func validateRecordExists(logLevel LogLevel, recordLevel zapcore.Level, t *testing.T) bool {
257 old := os.Stdout
258 r, w, _ :=os.Pipe()
259 os.Stdout = w
260 log, err := InitLogger(logLevel)
261 if err != nil {
262 t.Errorf("logger_test.TestSyncFailure - failed to initialize logger, error: %s", err)
263 }
264 switch recordLevel{
ss412gefcb4522019-12-02 16:59:19 +0200265 case zap.DebugLevel:
266 log.Debugf("%v, %v, %v", 1, "abc", 0.1)
267 case zap.InfoLevel:
268 log.Infof("%v, %v, %v", 1, "abc", 0.1)
269 case zap.WarnLevel:
270 log.Warnf("%v, %v, %v", 1, "abc", 0.1)
271 case zap.ErrorLevel:
272 log.Errorf("%v, %v, %v", 1, "abc", 0.1)
273 case zap.DPanicLevel:
274 log.DPanicf("%v, %v, %v", 1, "abc", 0.1)
ss412g07ef76d2019-08-12 17:26:40 +0300275 }
276 err = w.Close()
277 if err != nil {
278 t.Errorf("logger_test.TestSyncFailure - failed to close writer, error: %s", err)
279 }
280 os.Stdout = old
281 var buf bytes.Buffer
282 _, err = io.Copy(&buf, r)
283 if err != nil {
284 t.Errorf("logger_test.TestSyncFailure - failed to copy bytes, error: %s", err)
285 }
286 entryNum := 0
287 s,_:= buf.ReadString('\n')
288 for len(s) > 0{
289 entryNum +=1
290 s,_= buf.ReadString('\n')
291 }
292 return entryNum == 1
ss412gefcb4522019-12-02 16:59:19 +0200293}