blob: d24274cc15b2a102ad6687b4ae980e4fa5cf5b51 [file] [log] [blame]
gururajarao79542b9d12024-11-22 14:28:41 +01001// -
2// ========================LICENSE_START=================================
3// Copyright (C) 2024: Deutsche Telecom
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.
16// ========================LICENSE_END===================================
17//
18
19package log_test
20
21import (
22 "testing"
23
24 "bytes"
25 "github.com/sirupsen/logrus"
26 "policy-opa-pdp/pkg/log"
27)
28
29func TestSetOutput_Success(t *testing.T) {
30 var buf bytes.Buffer
31 log.SetOutput(&buf)
32 log.SetLevel(logrus.InfoLevel)
33
34 log.Info("Testing SetOutput")
35 if !bytes.Contains(buf.Bytes(), []byte("Testing SetOutput")) {
36 t.Errorf("Expected message to be logged")
37 }
38}
39
40func TestInit_Success(t *testing.T) {
41 var buf bytes.Buffer
42
43 log.SetOutput(&buf)
44 log.InitLogger("/tmp/logfile.log", 10, 5, "debug")
45 log.Info("Logger initialized")
46
47 if !bytes.Contains(buf.Bytes(), []byte("Logger initialized")) {
48 t.Errorf("Expected message to be logged after initialization")
49 }
50}
51
52func TestInitLogger_Success(t *testing.T) {
53 var buf bytes.Buffer
54
55 log.SetOutput(&buf)
56
57 log.InitLogger("/tmp/logfile.log", 10, 5, "info")
58
59 log.Info("Logger Initialized Test")
60 if !bytes.Contains(buf.Bytes(), []byte("Logger Initialized Test")) {
61 t.Errorf("Expected message to be logged")
62 }
63}
64
65func TestParseLevel_Success(t *testing.T) {
66 var buf bytes.Buffer
67 log.SetOutput(&buf)
68
69 level, err := logrus.ParseLevel("info")
70 if err != nil {
71 t.Fatalf("Failed to parse log level: %v", err)
72 }
73 log.SetLevel(level)
74
75 log.Info("Info level set")
76
77 if !bytes.Contains(buf.Bytes(), []byte("Info level set")) {
78 t.Errorf("Expected info level to be set")
79 }
80}
81
82func TestSetLevel_Success(t *testing.T) {
83 var buf bytes.Buffer
84 log.SetOutput(&buf)
85 log.SetLevel(logrus.DebugLevel)
86
87 log.Debug("This is a debug message")
88 if !bytes.Contains(buf.Bytes(), []byte("This is a debug message")) {
89 t.Errorf("Expected debug message to be logged")
90 }
91}
92
93func TestError_Success(t *testing.T) {
94 var buf bytes.Buffer
95 log.SetOutput(&buf)
96 log.SetLevel(logrus.ErrorLevel)
97
98 log.Error("This is an error message")
99 if !bytes.Contains(buf.Bytes(), []byte("This is an error message")) {
100 t.Errorf("Expected error message to be logged")
101 }
102}
103
104func TestInfo_Success(t *testing.T) {
105 var buf bytes.Buffer
106 log.SetOutput(&buf)
107 log.SetLevel(logrus.InfoLevel)
108
109 log.Info("This is an info message")
110 if !bytes.Contains(buf.Bytes(), []byte("This is an info message")) {
111 t.Errorf("Expected info message to be logged")
112 }
113}
114
115func TestDebug_Success(t *testing.T) {
116 var buf bytes.Buffer
117 log.SetOutput(&buf)
118 log.SetLevel(logrus.DebugLevel)
119
120 log.Debug("This is a debug message")
121 if !bytes.Contains(buf.Bytes(), []byte("This is a debug message")) {
122 t.Errorf("Expected debug message to be logged")
123 }
124}
125
126func TestWarn_Success(t *testing.T) {
127 var buf bytes.Buffer
128 log.SetOutput(&buf)
129 log.SetLevel(logrus.WarnLevel)
130
131 log.Warn("This is a warning message")
132 if !bytes.Contains(buf.Bytes(), []byte("This is a warning message")) {
133 t.Errorf("Expected warning message to be logged")
134 }
135}
136
137func TestPanic_Success(t *testing.T) {
138 defer func() {
139 if r := recover(); r == nil {
140 t.Errorf("Expected panic, but did not get one")
141 }
142 }()
143
144 log.SetLevel(logrus.PanicLevel)
145 log.Panic("This is a panic message")
146}
147
148func TestTrace_Success(t *testing.T) {
149 var buf bytes.Buffer
150 log.SetOutput(&buf)
151 log.SetLevel(logrus.TraceLevel)
152
153 log.Trace("This is a trace message")
154 if !bytes.Contains(buf.Bytes(), []byte("This is a trace message")) {
155 t.Errorf("Expected trace message to be logged")
156 }
157}
158
159func TestErrorf_Success(t *testing.T) {
160 var buf bytes.Buffer
161 log.SetOutput(&buf)
162 log.SetLevel(logrus.ErrorLevel)
163
164 log.Errorf("Error occurred: %s", "test error")
165 if !bytes.Contains(buf.Bytes(), []byte("Error occurred: test error")) {
166 t.Errorf("Expected error message to be logged")
167 }
168}
169
170func TestInfof_Success(t *testing.T) {
171 var buf bytes.Buffer
172 log.SetOutput(&buf)
173 log.SetLevel(logrus.InfoLevel)
174
175 log.Infof("Info log: %s", "test info")
176 if !bytes.Contains(buf.Bytes(), []byte("Info log: test info")) {
177 t.Errorf("Expected info message to be logged")
178 }
179}
180
181func TestDebugf_Success(t *testing.T) {
182 var buf bytes.Buffer
183 log.SetOutput(&buf)
184 log.SetLevel(logrus.DebugLevel)
185
186 log.Debugf("Debug message: %s", "should log")
187 if !bytes.Contains(buf.Bytes(), []byte("Debug message: should log")) {
188 t.Errorf("Expected debug message to be logged")
189 }
190}
191
192func TestWarnf_Success(t *testing.T) {
193 var buf bytes.Buffer
194 log.SetOutput(&buf)
195 log.SetLevel(logrus.WarnLevel)
196
197 log.Warnf("Warning message: %s", "should log")
198 if !bytes.Contains(buf.Bytes(), []byte("Warning message: should log")) {
199 t.Errorf("Expected warning message to be logged")
200 }
201}
202
203func TestPanicf_Success(t *testing.T) {
204 defer func() {
205 if r := recover(); r == nil {
206 t.Errorf("Expected panic, but did not get one")
207 }
208 }()
209
210 log.SetLevel(logrus.PanicLevel)
211 log.Panicf("Panic message: %s", "should panic")
212}
213
214func TestTracef_Success(t *testing.T) {
215 var buf bytes.Buffer
216 log.SetOutput(&buf)
217 log.SetLevel(logrus.TraceLevel)
218
219 log.Tracef("Trace message: %s", "should log")
220 if !bytes.Contains(buf.Bytes(), []byte("Trace message: should log")) {
221 t.Errorf("Expected trace message to be logged")
222 }
223}
224
225func TestError_Failure(t *testing.T) {
226 var buf bytes.Buffer
227 log.SetOutput(&buf)
228 log.SetLevel(logrus.FatalLevel) // Set level higher than Error
229
230 log.Error("This is an error message")
231 if bytes.Contains(buf.Bytes(), []byte("This is an error message")) {
232 t.Errorf("Expected error message not to be logged")
233 }
234}
235
236func TestInfo_Failure(t *testing.T) {
237 var buf bytes.Buffer
238 log.SetOutput(&buf)
239 log.SetLevel(logrus.WarnLevel) // Set level higher than Info
240
241 log.Info("This is an info message")
242 if bytes.Contains(buf.Bytes(), []byte("This is an info message")) {
243 t.Errorf("Expected info message not to be logged")
244 }
245}
246
247func TestDebug_Failure(t *testing.T) {
248 var buf bytes.Buffer
249 log.SetOutput(&buf)
250 log.SetLevel(logrus.InfoLevel) // Set level higher than Debug
251
252 log.Debug("This is a debug message")
253 if bytes.Contains(buf.Bytes(), []byte("This is a debug message")) {
254 t.Errorf("Expected debug message not to be logged")
255 }
256}
257
258func TestWarn_Failure(t *testing.T) {
259 var buf bytes.Buffer
260 log.SetOutput(&buf)
261 log.SetLevel(logrus.ErrorLevel) // Set level higher than Warn
262
263 log.Warn("This is a warning message")
264 if bytes.Contains(buf.Bytes(), []byte("This is a warning message")) {
265 t.Errorf("Expected warning message not to be logged")
266 }
267}
268
269func TestPanic_Failure(t *testing.T) {
270 defer func() {
271 if r := recover(); r == nil {
272 t.Errorf("Expected a panic at PanicLevel, but did not get one")
273 }
274 }()
275 log.SetLevel(logrus.PanicLevel) // Set to PanicLevel so a panic should occur
276 log.Panic("This should cause a panic at PanicLevel")
277}
278
279func TestTrace_Failure(t *testing.T) {
280 var buf bytes.Buffer
281 log.SetOutput(&buf)
282 log.SetLevel(logrus.DebugLevel) // Set level higher than Trace
283
284 log.Trace("This is a trace message")
285 if bytes.Contains(buf.Bytes(), []byte("This is a trace message")) {
286 t.Errorf("Expected trace message not to be logged")
287 }
288}
289
290func TestErrorf_Failure(t *testing.T) {
291 var buf bytes.Buffer
292 log.SetOutput(&buf)
293 log.SetLevel(logrus.FatalLevel) // Set level higher than Error
294
295 log.Errorf("Error occurred: %s", "test error")
296 if bytes.Contains(buf.Bytes(), []byte("Error occurred: test error")) {
297 t.Errorf("Expected error message not to be logged")
298 }
299}
300
301func TestInfof_Failure(t *testing.T) {
302 var buf bytes.Buffer
303 log.SetOutput(&buf)
304 log.SetLevel(logrus.WarnLevel) // Set level higher than Info
305
306 log.Infof("Info log: %s", "test info")
307 if bytes.Contains(buf.Bytes(), []byte("Info log: test info")) {
308 t.Errorf("Expected info message not to be logged")
309 }
310}
311
312func TestDebugf_Failure(t *testing.T) {
313 var buf bytes.Buffer
314 log.SetOutput(&buf)
315 log.SetLevel(logrus.InfoLevel) // Set level higher than Debug
316
317 log.Debugf("Debug message: %s", "should not log")
318 if bytes.Contains(buf.Bytes(), []byte("Debug message: should not log")) {
319 t.Errorf("Expected debug message not to be logged")
320 }
321}
322
323func TestWarnf_Failure(t *testing.T) {
324 var buf bytes.Buffer
325 log.SetOutput(&buf)
326 log.SetLevel(logrus.ErrorLevel) // Set level higher than Warn
327
328 log.Warnf("Warning message: %s", "should not log")
329 if bytes.Contains(buf.Bytes(), []byte("Warning message: should not log")) {
330 t.Errorf("Expected warning message not to be logged")
331 }
332}
333
334func TestPanicf_Failure(t *testing.T) {
335 defer func() {
336 if r := recover(); r == nil {
337 t.Errorf("Expected a panic at PanicLevel, but did not get one")
338 }
339 }()
340
341 log.SetLevel(logrus.PanicLevel) // Set to PanicLevel so a panic should occur
342 log.Panicf("Panicf message: %s", "should panic at PanicLevel")
343}
344
345func TestTracef_Failure(t *testing.T) {
346 var buf bytes.Buffer
347 log.SetOutput(&buf)
348 log.SetLevel(logrus.DebugLevel) // Set level higher than Trace
349
350 log.Tracef("Trace message: %s", "should not log")
351 if bytes.Contains(buf.Bytes(), []byte("Trace message: should not log")) {
352 t.Errorf("Expected trace message not to be logged")
353 }
354}