Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package log |
| 17 | |
| 18 | import ( |
| 19 | "github.com/stretchr/testify/assert" |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | /* |
| 24 | Prerequite: Start the kafka/zookeeper containers. |
| 25 | */ |
| 26 | |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 27 | var testLogger CLogger |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 28 | |
| 29 | func TestInit(t *testing.T) { |
| 30 | var err error |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 31 | testLogger, err = RegisterPackage(JSON, ErrorLevel, nil) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 32 | assert.NotNil(t, testLogger) |
| 33 | assert.Nil(t, err) |
| 34 | } |
| 35 | |
| 36 | func verifyLogLevel(t *testing.T, minimumLevel int) { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 37 | SetAllLogLevel(LogLevel(minimumLevel)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 38 | var success bool |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 39 | for i := 0; i < 5; i++ { |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 40 | success = testLogger.V(LogLevel(i)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 41 | 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 | |
| 54 | func TestLogLevelDebug(t *testing.T) { |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 55 | for i := 0; i < 5; i++ { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 56 | verifyLogLevel(t, i) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestUpdateAllLoggers(t *testing.T) { |
| 61 | err := UpdateAllLoggers(Fields{"update": "update"}) |
| 62 | assert.Nil(t, err) |
| 63 | } |
| 64 | |
| 65 | func TestUpdateLoggers(t *testing.T) { |
Girish Kumar | c9b0e71 | 2020-02-27 17:50:52 +0000 | [diff] [blame] | 66 | err := UpdateLogger(Fields{"update": "update"}) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 67 | assert.Nil(t, err) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 70 | func TestUpdateLogLevel(t *testing.T) { |
| 71 | // Let's create a bunch of logger each with a separate package |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 72 | myLoggers := make(map[string]CLogger) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 73 | pkgNames := []string{"/rw_core/core", "/db/model", "/kafka"} |
| 74 | for _, name := range pkgNames { |
Girish Kumar | 950f21e | 2020-08-19 17:42:29 +0000 | [diff] [blame] | 75 | myLoggers[name], _ = RegisterPackage(JSON, ErrorLevel, nil, []string{name}...) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 76 | } |
| 77 | //Test updates to log levels |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 78 | levels := []LogLevel{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 79 | for _, expectedLevel := range levels { |
| 80 | for _, name := range pkgNames { |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 81 | SetPackageLogLevel(name, expectedLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 82 | l, err := GetPackageLogLevel(name) |
| 83 | assert.Nil(t, err) |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 84 | assert.Equal(t, l, expectedLevel) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 85 | // 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 Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 88 | assert.Equal(t, level, expectedLevel) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 89 | // 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | //Test set all package level |
| 101 | for _, expectedLevel := range levels { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 102 | SetAllLogLevel(LogLevel(expectedLevel)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 103 | for _, name := range pkgNames { |
| 104 | l, err := GetPackageLogLevel(name) |
| 105 | assert.Nil(t, err) |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 106 | assert.Equal(t, l, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 107 | level := myLoggers[name].GetLogLevel() |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 108 | assert.Equal(t, level, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 109 | // 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |