blob: bc78dfecf48845202e7a0d0f4595b70e1159ca85 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package log
17
18import (
19 "github.com/stretchr/testify/assert"
Scott Baker2c1c4822019-10-16 11:02:41 -070020 "testing"
21)
22
23/*
24Prerequite: Start the kafka/zookeeper containers.
25*/
26
Girish Kumar950f21e2020-08-19 17:42:29 +000027var testLogger CLogger
Scott Baker2c1c4822019-10-16 11:02:41 -070028
29func TestInit(t *testing.T) {
30 var err error
Girish Kumar950f21e2020-08-19 17:42:29 +000031 testLogger, err = RegisterPackage(JSON, ErrorLevel, nil)
Scott Baker2c1c4822019-10-16 11:02:41 -070032 assert.NotNil(t, testLogger)
33 assert.Nil(t, err)
34}
35
36func verifyLogLevel(t *testing.T, minimumLevel int) {
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +000037 SetAllLogLevel(LogLevel(minimumLevel))
Scott Baker2c1c4822019-10-16 11:02:41 -070038 var success bool
Rohan Agrawal6a99a452020-01-14 07:58:25 +000039 for i := 0; i < 5; i++ {
Scott Bakerd7c25f42020-02-21 08:12:06 -080040 success = testLogger.V(LogLevel(i))
Scott Baker2c1c4822019-10-16 11:02:41 -070041 if i == 1 && minimumLevel == 2 {
42 // TODO: Update the test when a new version of Zap logger is available. It has a bug with that
43 // specific combination
44 continue
45 }
46 if i < minimumLevel {
47 assert.False(t, success)
48 } else {
49 assert.True(t, success)
50 }
51 }
52}
53
54func TestLogLevelDebug(t *testing.T) {
Rohan Agrawal6a99a452020-01-14 07:58:25 +000055 for i := 0; i < 5; i++ {
Scott Baker2c1c4822019-10-16 11:02:41 -070056 verifyLogLevel(t, i)
57 }
58}
59
60func TestUpdateAllLoggers(t *testing.T) {
61 err := UpdateAllLoggers(Fields{"update": "update"})
62 assert.Nil(t, err)
63}
64
65func TestUpdateLoggers(t *testing.T) {
Girish Kumarc9b0e712020-02-27 17:50:52 +000066 err := UpdateLogger(Fields{"update": "update"})
Scott Baker2c1c4822019-10-16 11:02:41 -070067 assert.Nil(t, err)
Scott Baker2c1c4822019-10-16 11:02:41 -070068}
69
Scott Baker2c1c4822019-10-16 11:02:41 -070070func TestUpdateLogLevel(t *testing.T) {
71 // Let's create a bunch of logger each with a separate package
Girish Kumar950f21e2020-08-19 17:42:29 +000072 myLoggers := make(map[string]CLogger)
Scott Baker2c1c4822019-10-16 11:02:41 -070073 pkgNames := []string{"/rw_core/core", "/db/model", "/kafka"}
74 for _, name := range pkgNames {
Girish Kumar950f21e2020-08-19 17:42:29 +000075 myLoggers[name], _ = RegisterPackage(JSON, ErrorLevel, nil, []string{name}...)
Scott Baker2c1c4822019-10-16 11:02:41 -070076 }
77 //Test updates to log levels
Scott Bakerd7c25f42020-02-21 08:12:06 -080078 levels := []LogLevel{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel}
Scott Baker2c1c4822019-10-16 11:02:41 -070079 for _, expectedLevel := range levels {
80 for _, name := range pkgNames {
Scott Bakerd7c25f42020-02-21 08:12:06 -080081 SetPackageLogLevel(name, expectedLevel)
Scott Baker2c1c4822019-10-16 11:02:41 -070082 l, err := GetPackageLogLevel(name)
83 assert.Nil(t, err)
Scott Bakerd7c25f42020-02-21 08:12:06 -080084 assert.Equal(t, l, expectedLevel)
khenaidoob332f9b2020-01-16 16:25:26 -050085 // Get the package log level by invoking the specific logger created for this package
86 // This is a less expensive operation that the GetPackageLogLevel() request
87 level := myLoggers[name].GetLogLevel()
Scott Bakerd7c25f42020-02-21 08:12:06 -080088 assert.Equal(t, level, expectedLevel)
khenaidoob332f9b2020-01-16 16:25:26 -050089 // Check the verbosity level
90 for _, level := range levels {
91 toDisplay := myLoggers[name].V(level)
92 if level < expectedLevel {
93 assert.False(t, toDisplay)
94 } else {
95 assert.True(t, toDisplay)
96 }
97 }
Scott Baker2c1c4822019-10-16 11:02:41 -070098 }
99 }
100 //Test set all package level
101 for _, expectedLevel := range levels {
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000102 SetAllLogLevel(LogLevel(expectedLevel))
Scott Baker2c1c4822019-10-16 11:02:41 -0700103 for _, name := range pkgNames {
104 l, err := GetPackageLogLevel(name)
105 assert.Nil(t, err)
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000106 assert.Equal(t, l, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -0500107 level := myLoggers[name].GetLogLevel()
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000108 assert.Equal(t, level, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -0500109 // Check the verbosity level
110 for _, level := range levels {
111 toDisplay := myLoggers[name].V(level)
112 if level < expectedLevel {
113 assert.False(t, toDisplay)
114 } else {
115 assert.True(t, toDisplay)
116 }
117 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700118 }
119 }
120}