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 | |
| 27 | var testLogger Logger |
| 28 | |
| 29 | func 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 | |
| 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) { |
| 66 | testLogger, err := UpdateLogger(Fields{"update": "update"}) |
| 67 | assert.Nil(t, err) |
| 68 | assert.NotNil(t, testLogger) |
| 69 | } |
| 70 | |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 71 | func 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 Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 79 | levels := []LogLevel{DebugLevel, InfoLevel, WarnLevel, ErrorLevel, FatalLevel} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 80 | for _, expectedLevel := range levels { |
| 81 | for _, name := range pkgNames { |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 82 | SetPackageLogLevel(name, expectedLevel) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 83 | l, err := GetPackageLogLevel(name) |
| 84 | assert.Nil(t, err) |
Scott Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 85 | assert.Equal(t, l, expectedLevel) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 86 | // 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 Baker | d7c25f4 | 2020-02-21 08:12:06 -0800 | [diff] [blame] | 89 | assert.Equal(t, level, expectedLevel) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 90 | // 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | //Test set all package level |
| 102 | for _, expectedLevel := range levels { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 103 | SetAllLogLevel(LogLevel(expectedLevel)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 104 | for _, name := range pkgNames { |
| 105 | l, err := GetPackageLogLevel(name) |
| 106 | assert.Nil(t, err) |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 107 | assert.Equal(t, l, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 108 | level := myLoggers[name].GetLogLevel() |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 109 | assert.Equal(t, level, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 110 | // 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |