blob: be65b02b12bbb290dce9deed8d5761c42d155d2e [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
27var testLogger Logger
28
29func TestInit(t *testing.T) {
30 var err error
31 testLogger, err = AddPackage(JSON, ErrorLevel, nil)
32 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) {
66 testLogger, err := UpdateLogger(Fields{"update": "update"})
67 assert.Nil(t, err)
68 assert.NotNil(t, testLogger)
69}
70
Scott Baker2c1c4822019-10-16 11:02:41 -070071func TestUpdateLogLevel(t *testing.T) {
72 // Let's create a bunch of logger each with a separate package
73 myLoggers := make(map[string]Logger)
74 pkgNames := []string{"/rw_core/core", "/db/model", "/kafka"}
75 for _, name := range pkgNames {
76 myLoggers[name], _ = AddPackage(JSON, ErrorLevel, nil, []string{name}...)
77 }
78 //Test updates to log levels
Scott Bakerd7c25f42020-02-21 08:12:06 -080079 levels := []LogLevel{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel}
Scott Baker2c1c4822019-10-16 11:02:41 -070080 for _, expectedLevel := range levels {
81 for _, name := range pkgNames {
Scott Bakerd7c25f42020-02-21 08:12:06 -080082 SetPackageLogLevel(name, expectedLevel)
Scott Baker2c1c4822019-10-16 11:02:41 -070083 l, err := GetPackageLogLevel(name)
84 assert.Nil(t, err)
Scott Bakerd7c25f42020-02-21 08:12:06 -080085 assert.Equal(t, l, expectedLevel)
khenaidoob332f9b2020-01-16 16:25:26 -050086 // Get the package log level by invoking the specific logger created for this package
87 // This is a less expensive operation that the GetPackageLogLevel() request
88 level := myLoggers[name].GetLogLevel()
Scott Bakerd7c25f42020-02-21 08:12:06 -080089 assert.Equal(t, level, expectedLevel)
khenaidoob332f9b2020-01-16 16:25:26 -050090 // Check the verbosity level
91 for _, level := range levels {
92 toDisplay := myLoggers[name].V(level)
93 if level < expectedLevel {
94 assert.False(t, toDisplay)
95 } else {
96 assert.True(t, toDisplay)
97 }
98 }
Scott Baker2c1c4822019-10-16 11:02:41 -070099 }
100 }
101 //Test set all package level
102 for _, expectedLevel := range levels {
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000103 SetAllLogLevel(LogLevel(expectedLevel))
Scott Baker2c1c4822019-10-16 11:02:41 -0700104 for _, name := range pkgNames {
105 l, err := GetPackageLogLevel(name)
106 assert.Nil(t, err)
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000107 assert.Equal(t, l, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -0500108 level := myLoggers[name].GetLogLevel()
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000109 assert.Equal(t, level, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -0500110 // Check the verbosity level
111 for _, level := range levels {
112 toDisplay := myLoggers[name].V(level)
113 if level < expectedLevel {
114 assert.False(t, toDisplay)
115 } else {
116 assert.True(t, toDisplay)
117 }
118 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700119 }
120 }
121}