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 | |
| 31 | |
| 32 | func TestInit (t *testing.T) { |
| 33 | var err error |
| 34 | testLogger, err = log.AddPackage(log.JSON, log.ErrorLevel, nil) |
| 35 | assert.NotNil(t, testLogger) |
| 36 | assert.Nil(t, err) |
| 37 | } |
| 38 | |
| 39 | func verifyLogLevel(t *testing.T, minimumLevel int) { |
| 40 | log.SetAllLogLevel(minimumLevel) |
| 41 | var success bool |
| 42 | for i := 0; i < 6; i++ { |
| 43 | success = testLogger.V(i) |
| 44 | if i == 1 && minimumLevel == 2{ |
| 45 | // TODO: Update the test when a new version of Zap logger is available. It has a bug with that |
| 46 | // specific combination |
| 47 | continue |
| 48 | } |
| 49 | if i < minimumLevel { |
| 50 | assert.False(t, success) |
| 51 | } else { |
| 52 | assert.True(t, success) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func TestLogLevelDebug (t *testing.T) { |
| 58 | for i := 0; i < 6; i++ { |
| 59 | verifyLogLevel(t, i) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestUpdateAllLoggers (t *testing.T) { |
| 64 | err := log.UpdateAllLoggers(log.Fields{"update": "update"}) |
| 65 | assert.Nil(t, err) |
| 66 | } |
| 67 | |
| 68 | func TestUpdateLoggers (t *testing.T) { |
| 69 | testLogger, err := log.UpdateLogger(log.Fields{"update": "update"}) |
| 70 | assert.Nil(t, err) |
| 71 | assert.NotNil(t, testLogger) |
| 72 | } |
| 73 | |
| 74 | func TestUseAsGrpcLoggerV2(t *testing.T) { |
| 75 | var grpcLogger grpclog.LoggerV2 |
| 76 | thisLogger, _ := log.AddPackage(log.JSON, log.ErrorLevel, nil) |
| 77 | grpcLogger = thisLogger |
| 78 | assert.NotNil(t, grpcLogger) |
| 79 | } |