blob: 84a91d13660850aedc5be68ce331c2e9bdcc3a1c [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"
20 "google.golang.org/grpc/grpclog"
21 "testing"
22)
23
24/*
25Prerequite: Start the kafka/zookeeper containers.
26*/
27
28var testLogger Logger
29
30func TestInit(t *testing.T) {
31 var err error
32 testLogger, err = AddPackage(JSON, ErrorLevel, nil)
33 assert.NotNil(t, testLogger)
34 assert.Nil(t, err)
35}
36
37func verifyLogLevel(t *testing.T, minimumLevel int) {
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +000038 SetAllLogLevel(LogLevel(minimumLevel))
Scott Baker2c1c4822019-10-16 11:02:41 -070039 var success bool
Rohan Agrawal6a99a452020-01-14 07:58:25 +000040 for i := 0; i < 5; i++ {
Scott Baker2c1c4822019-10-16 11:02:41 -070041 success = testLogger.V(i)
42 if i == 1 && minimumLevel == 2 {
43 // TODO: Update the test when a new version of Zap logger is available. It has a bug with that
44 // specific combination
45 continue
46 }
47 if i < minimumLevel {
48 assert.False(t, success)
49 } else {
50 assert.True(t, success)
51 }
52 }
53}
54
55func TestLogLevelDebug(t *testing.T) {
Rohan Agrawal6a99a452020-01-14 07:58:25 +000056 for i := 0; i < 5; i++ {
Scott Baker2c1c4822019-10-16 11:02:41 -070057 verifyLogLevel(t, i)
58 }
59}
60
61func TestUpdateAllLoggers(t *testing.T) {
62 err := UpdateAllLoggers(Fields{"update": "update"})
63 assert.Nil(t, err)
64}
65
66func TestUpdateLoggers(t *testing.T) {
67 testLogger, err := UpdateLogger(Fields{"update": "update"})
68 assert.Nil(t, err)
69 assert.NotNil(t, testLogger)
70}
71
72func TestUseAsGrpcLoggerV2(t *testing.T) {
73 var grpcLogger grpclog.LoggerV2
74 thisLogger, _ := AddPackage(JSON, ErrorLevel, nil)
75 grpcLogger = thisLogger
76 assert.NotNil(t, grpcLogger)
77}
78
79func TestUpdateLogLevel(t *testing.T) {
80 // Let's create a bunch of logger each with a separate package
81 myLoggers := make(map[string]Logger)
82 pkgNames := []string{"/rw_core/core", "/db/model", "/kafka"}
83 for _, name := range pkgNames {
84 myLoggers[name], _ = AddPackage(JSON, ErrorLevel, nil, []string{name}...)
85 }
86 //Test updates to log levels
Rohan Agrawal6a99a452020-01-14 07:58:25 +000087 levels := []int{0, 1, 2, 3, 4}
Scott Baker2c1c4822019-10-16 11:02:41 -070088 for _, expectedLevel := range levels {
89 for _, name := range pkgNames {
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +000090 SetPackageLogLevel(name, LogLevel(expectedLevel))
Scott Baker2c1c4822019-10-16 11:02:41 -070091 l, err := GetPackageLogLevel(name)
92 assert.Nil(t, err)
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +000093 assert.Equal(t, l, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -050094 // Get the package log level by invoking the specific logger created for this package
95 // This is a less expensive operation that the GetPackageLogLevel() request
96 level := myLoggers[name].GetLogLevel()
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +000097 assert.Equal(t, level, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -050098 // Check the verbosity level
99 for _, level := range levels {
100 toDisplay := myLoggers[name].V(level)
101 if level < expectedLevel {
102 assert.False(t, toDisplay)
103 } else {
104 assert.True(t, toDisplay)
105 }
106 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700107 }
108 }
109 //Test set all package level
110 for _, expectedLevel := range levels {
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000111 SetAllLogLevel(LogLevel(expectedLevel))
Scott Baker2c1c4822019-10-16 11:02:41 -0700112 for _, name := range pkgNames {
113 l, err := GetPackageLogLevel(name)
114 assert.Nil(t, err)
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000115 assert.Equal(t, l, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -0500116 level := myLoggers[name].GetLogLevel()
Rohan Agrawal0c62b5d2020-02-04 09:56:21 +0000117 assert.Equal(t, level, LogLevel(expectedLevel))
khenaidoob332f9b2020-01-16 16:25:26 -0500118 // Check the verbosity level
119 for _, level := range levels {
120 toDisplay := myLoggers[name].V(level)
121 if level < expectedLevel {
122 assert.False(t, toDisplay)
123 } else {
124 assert.True(t, toDisplay)
125 }
126 }
Scott Baker2c1c4822019-10-16 11:02:41 -0700127 }
128 }
129}