blob: 68d6ba3a90499d0df46b3fe3673804b016e4792f [file] [log] [blame]
khenaidoo7da372d2018-09-21 16:03:09 -04001/*
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 */
16package log
17
18import (
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/*
26Prerequite: Start the kafka/zookeeper containers.
27*/
28
29var testLogger log.Logger
30
khenaidoo89b0e942018-10-21 21:11:33 -040031func TestInit(t *testing.T) {
khenaidoo7da372d2018-09-21 16:03:09 -040032 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
38func 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)
khenaidoo89b0e942018-10-21 21:11:33 -040043 if i == 1 && minimumLevel == 2 {
khenaidoo7da372d2018-09-21 16:03:09 -040044 // 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
khenaidoo89b0e942018-10-21 21:11:33 -040056func TestLogLevelDebug(t *testing.T) {
khenaidoo7da372d2018-09-21 16:03:09 -040057 for i := 0; i < 6; i++ {
58 verifyLogLevel(t, i)
59 }
60}
61
khenaidoo89b0e942018-10-21 21:11:33 -040062func TestUpdateAllLoggers(t *testing.T) {
khenaidoo7da372d2018-09-21 16:03:09 -040063 err := log.UpdateAllLoggers(log.Fields{"update": "update"})
64 assert.Nil(t, err)
65}
66
khenaidoo89b0e942018-10-21 21:11:33 -040067func TestUpdateLoggers(t *testing.T) {
khenaidoo7da372d2018-09-21 16:03:09 -040068 testLogger, err := log.UpdateLogger(log.Fields{"update": "update"})
69 assert.Nil(t, err)
70 assert.NotNil(t, testLogger)
71}
72
73func 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)
khenaidoo89b0e942018-10-21 21:11:33 -040078}