khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [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/opencord/voltha-go/common/log" |
| 20 | "github.com/stretchr/testify/assert" |
| 21 | "google.golang.org/grpc/grpclog" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | /* |
| 26 | Prerequite: Start the kafka/zookeeper containers. |
| 27 | */ |
| 28 | |
| 29 | var testLogger log.Logger |
| 30 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 31 | func TestInit(t *testing.T) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 32 | var err error |
| 33 | testLogger, err = log.AddPackage(log.JSON, log.ErrorLevel, nil) |
| 34 | assert.NotNil(t, testLogger) |
| 35 | assert.Nil(t, err) |
| 36 | } |
| 37 | |
| 38 | func verifyLogLevel(t *testing.T, minimumLevel int) { |
| 39 | log.SetAllLogLevel(minimumLevel) |
| 40 | var success bool |
| 41 | for i := 0; i < 6; i++ { |
| 42 | success = testLogger.V(i) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 43 | if i == 1 && minimumLevel == 2 { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 44 | // TODO: Update the test when a new version of Zap logger is available. It has a bug with that |
| 45 | // specific combination |
| 46 | continue |
| 47 | } |
| 48 | if i < minimumLevel { |
| 49 | assert.False(t, success) |
| 50 | } else { |
| 51 | assert.True(t, success) |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 56 | func TestLogLevelDebug(t *testing.T) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 57 | for i := 0; i < 6; i++ { |
| 58 | verifyLogLevel(t, i) |
| 59 | } |
| 60 | } |
| 61 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 62 | func TestUpdateAllLoggers(t *testing.T) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 63 | err := log.UpdateAllLoggers(log.Fields{"update": "update"}) |
| 64 | assert.Nil(t, err) |
| 65 | } |
| 66 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 67 | func TestUpdateLoggers(t *testing.T) { |
khenaidoo | 7da372d | 2018-09-21 16:03:09 -0400 | [diff] [blame] | 68 | testLogger, err := log.UpdateLogger(log.Fields{"update": "update"}) |
| 69 | assert.Nil(t, err) |
| 70 | assert.NotNil(t, testLogger) |
| 71 | } |
| 72 | |
| 73 | func TestUseAsGrpcLoggerV2(t *testing.T) { |
| 74 | var grpcLogger grpclog.LoggerV2 |
| 75 | thisLogger, _ := log.AddPackage(log.JSON, log.ErrorLevel, nil) |
| 76 | grpcLogger = thisLogger |
| 77 | assert.NotNil(t, grpcLogger) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 78 | } |