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) { |
| 38 | SetAllLogLevel(minimumLevel) |
| 39 | var success bool |
| 40 | for i := 0; i < 6; i++ { |
| 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) { |
| 56 | for i := 0; i < 6; i++ { |
| 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 |
| 87 | levels := []int{0, 1, 2, 3, 4, 5} |
| 88 | for _, expectedLevel := range levels { |
| 89 | for _, name := range pkgNames { |
| 90 | SetPackageLogLevel(name, expectedLevel) |
| 91 | l, err := GetPackageLogLevel(name) |
| 92 | assert.Nil(t, err) |
| 93 | assert.Equal(t, l, expectedLevel) |
| 94 | } |
| 95 | } |
| 96 | //Test set all package level |
| 97 | for _, expectedLevel := range levels { |
| 98 | SetAllLogLevel(expectedLevel) |
| 99 | for _, name := range pkgNames { |
| 100 | l, err := GetPackageLogLevel(name) |
| 101 | assert.Nil(t, err) |
| 102 | assert.Equal(t, l, expectedLevel) |
| 103 | } |
| 104 | } |
| 105 | } |