blob: a62f8e503d525291c5090420d12e5faff5395325 [file] [log] [blame]
Daniele Rossid5a6fb72019-11-13 15:37:07 +01001/*
2 * Copyright 2019-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 core
17
18import (
19 "context"
npujar03b018e2019-11-13 15:29:36 +053020 "strconv"
21 "testing"
22
Daniele Rossid5a6fb72019-11-13 15:37:07 +010023 "github.com/opencord/voltha-go/ro_core/config"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080024 "github.com/opencord/voltha-lib-go/v3/pkg/log"
25 "github.com/opencord/voltha-protos/v3/go/common"
26 "github.com/opencord/voltha-protos/v3/go/voltha"
Daniele Rossid5a6fb72019-11-13 15:37:07 +010027 "github.com/phayes/freeport"
28 "github.com/stretchr/testify/assert"
Daniele Rossid5a6fb72019-11-13 15:37:07 +010029)
30
31func MakeTestGrpcNbiConfig() *Core {
Thomas Lee Se5a44012019-11-07 20:32:24 +053032 var ctx context.Context
Daniele Rossid5a6fb72019-11-13 15:37:07 +010033 var core *Core
34 var roCoreFlgs *config.ROCoreFlags
35 var roC *roCore
36
37 freePort, errP := freeport.GetFreePort()
38 if errP == nil {
39 freePortStr := strconv.Itoa(freePort)
40
41 roCoreFlgs = config.NewROCoreFlags()
42 roC = newROCore(roCoreFlgs)
43 if (roC != nil) && (roCoreFlgs != nil) {
44 addr := "127.0.0.1" + ":" + freePortStr
45 cli, err := newKVClient("etcd", addr, 5)
46 if err == nil {
47 roC.kvClient = cli
Thomas Lee Se5a44012019-11-07 20:32:24 +053048 core = NewCore(ctx, "ro_core", roCoreFlgs, roC.kvClient)
Daniele Rossid5a6fb72019-11-13 15:37:07 +010049 }
50 }
51 }
52
53 return core
54}
55
56func TestNewAPIHandler_grpc(t *testing.T) {
57 core := MakeTestGrpcNbiConfig()
58 assert.NotNil(t, core)
59
60 // NewAPIHandler declares, initializes and returns an handler struct
61 apiHdl := NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
62 assert.NotNil(t, apiHdl)
63}
64
65func TestUpdateLogLevel_grpc(t *testing.T) {
66 core := MakeTestGrpcNbiConfig()
67 assert.NotNil(t, core)
68
69 var testCtx = context.Background()
70 testLogDef := &voltha.Logging{
71 ComponentName: "testing",
72 PackageName: "default",
serkant.uluderya2ae470f2020-01-21 11:13:09 -080073 Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())}
Daniele Rossid5a6fb72019-11-13 15:37:07 +010074 testLogEmpty := &voltha.Logging{
75 ComponentName: "testing",
76 PackageName: "",
serkant.uluderya2ae470f2020-01-21 11:13:09 -080077 Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())}
Daniele Rossid5a6fb72019-11-13 15:37:07 +010078 testLog := &voltha.Logging{
79 ComponentName: "testing",
80 PackageName: "testing",
serkant.uluderya2ae470f2020-01-21 11:13:09 -080081 Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())}
npujar03b018e2019-11-13 15:29:36 +053082 testLog3 := &voltha.Logging{
Daniele Rossid5a6fb72019-11-13 15:37:07 +010083 ComponentName: "testing",
84 PackageName: "github.com/opencord/voltha-go/ro_core/core",
85 Level: 3 /*voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())*/}
86 ahndl := NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
87
88 type args struct {
89 ctx context.Context
90 logging *voltha.Logging
91 }
92 tests := []struct {
93 name string
94 ah *APIHandler
95 args args
96 want int
97 wantErr error
98 }{
serkant.uluderya2ae470f2020-01-21 11:13:09 -080099 {"TestUpdateLogLevel-1", ahndl, args{testCtx, testLogDef}, log.DebugLevel, nil},
100 {"TestUpdateLogLevel-2", ahndl, args{testCtx, testLogEmpty}, log.FatalLevel, nil},
101 {"TestUpdateLogLevel-3", ahndl, args{testCtx, testLog}, log.FatalLevel, nil},
102 {"TestUpdateLogLevel-4", ahndl, args{testCtx, testLog3}, log.ErrorLevel, nil},
Daniele Rossid5a6fb72019-11-13 15:37:07 +0100103 }
104 for _, tt := range tests {
105 t.Run(tt.name, func(t *testing.T) {
106 _, gotErr := tt.ah.UpdateLogLevel(tt.args.ctx, tt.args.logging)
107 if tt.wantErr != gotErr {
108 t.Errorf("Error")
109 }
110 for _, packageNames := range log.GetPackageNames() {
111 logLev, errLogLev := log.GetPackageLogLevel(packageNames)
112 if errLogLev == nil {
113 if packageNames == "github.com/opencord/voltha-go/ro_core/core" {
114 if logLev != tt.want {
115 t.Errorf("Error. Want %d, Got %d", tt.want, logLev)
116 }
117 }
118 }
119 }
120 })
121 }
122}
123
124func TestGetLogLevels_grpc(t *testing.T) {
125 core := MakeTestGrpcNbiConfig()
126 assert.NotNil(t, core)
127
128 testCtx := context.Background()
129 testLc := new(common.LoggingComponent)
130 testLc.ComponentName = "testing"
131
132 ahndl := NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
133
134 type args struct {
135 ctx context.Context
136 testLc *voltha.LoggingComponent
137 }
138 tests := []struct {
139 name string
140 ah *APIHandler
141 args args
142 want int
143 wantErr error
144 }{
serkant.uluderya2ae470f2020-01-21 11:13:09 -0800145 {"TestGetLogLevels-1", ahndl, args{testCtx, testLc}, log.DebugLevel, nil},
146 {"TestGetLogLevels-2", ahndl, args{testCtx, testLc}, log.InfoLevel, nil},
147 {"TestGetLogLevels-3", ahndl, args{testCtx, testLc}, log.WarnLevel, nil},
148 {"TestGetLogLevels-4", ahndl, args{testCtx, testLc}, log.ErrorLevel, nil},
149 {"TestGetLogLevels-5", ahndl, args{testCtx, testLc}, log.FatalLevel, nil},
150 {"TestGetLogLevels-6", ahndl, args{testCtx, testLc}, log.ErrorLevel, nil},
Daniele Rossid5a6fb72019-11-13 15:37:07 +0100151 }
152 for itt, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154 // itt index match log levels to test (from DEBUG=0, to FATAL=6)
155 log.SetPackageLogLevel("github.com/opencord/voltha-go/ro_core/core", itt)
156 logLevs, gotErr := tt.ah.GetLogLevels(tt.args.ctx, tt.args.testLc)
157 if (logLevs == nil) || (gotErr != nil) {
158 t.Errorf("Error %v\n", gotErr)
159 }
160 for _, lli := range logLevs.Items {
161 if lli.PackageName == "github.com/opencord/voltha-go/ro_core/core" {
162 if int(lli.Level) != tt.want {
163 t.Errorf("logLev wanted %v logLev %v \n", tt.want, lli.Level)
164 }
165 }
166 }
167 })
168 }
169
170}