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" |
| 20 | "google.golang.org/grpc/grpclog" |
| 21 | "testing" |
| 22 | ) |
| 23 | |
| 24 | /* |
| 25 | Prerequite: Start the kafka/zookeeper containers. |
| 26 | */ |
| 27 | |
| 28 | var testLogger Logger |
| 29 | |
| 30 | func 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 | |
| 37 | func verifyLogLevel(t *testing.T, minimumLevel int) { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 38 | SetAllLogLevel(LogLevel(minimumLevel)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 39 | var success bool |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 40 | for i := 0; i < 5; i++ { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 41 | 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 | |
| 55 | func TestLogLevelDebug(t *testing.T) { |
Rohan Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 56 | for i := 0; i < 5; i++ { |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 57 | verifyLogLevel(t, i) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestUpdateAllLoggers(t *testing.T) { |
| 62 | err := UpdateAllLoggers(Fields{"update": "update"}) |
| 63 | assert.Nil(t, err) |
| 64 | } |
| 65 | |
| 66 | func TestUpdateLoggers(t *testing.T) { |
| 67 | testLogger, err := UpdateLogger(Fields{"update": "update"}) |
| 68 | assert.Nil(t, err) |
| 69 | assert.NotNil(t, testLogger) |
| 70 | } |
| 71 | |
| 72 | func 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 | |
| 79 | func 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 Agrawal | 6a99a45 | 2020-01-14 07:58:25 +0000 | [diff] [blame] | 87 | levels := []int{0, 1, 2, 3, 4} |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 88 | for _, expectedLevel := range levels { |
| 89 | for _, name := range pkgNames { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 90 | SetPackageLogLevel(name, LogLevel(expectedLevel)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 91 | l, err := GetPackageLogLevel(name) |
| 92 | assert.Nil(t, err) |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 93 | assert.Equal(t, l, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 94 | // 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 Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 97 | assert.Equal(t, level, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 98 | // 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | //Test set all package level |
| 110 | for _, expectedLevel := range levels { |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 111 | SetAllLogLevel(LogLevel(expectedLevel)) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 112 | for _, name := range pkgNames { |
| 113 | l, err := GetPackageLogLevel(name) |
| 114 | assert.Nil(t, err) |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 115 | assert.Equal(t, l, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 116 | level := myLoggers[name].GetLogLevel() |
Rohan Agrawal | 0c62b5d | 2020-02-04 09:56:21 +0000 | [diff] [blame] | 117 | assert.Equal(t, level, LogLevel(expectedLevel)) |
khenaidoo | b332f9b | 2020-01-16 16:25:26 -0500 | [diff] [blame] | 118 | // 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 Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |