blob: 3244fd34f6f780583506396cba909c191ac9b0e4 [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"
24 "github.com/opencord/voltha-lib-go/v2/pkg/log"
25 "github.com/opencord/voltha-protos/v2/go/common"
26 "github.com/opencord/voltha-protos/v2/go/voltha"
27 "github.com/phayes/freeport"
28 "github.com/stretchr/testify/assert"
Daniele Rossid5a6fb72019-11-13 15:37:07 +010029)
30
31func MakeTestGrpcNbiConfig() *Core {
32 var core *Core
33 var roCoreFlgs *config.ROCoreFlags
34 var roC *roCore
35
36 freePort, errP := freeport.GetFreePort()
37 if errP == nil {
38 freePortStr := strconv.Itoa(freePort)
39
40 roCoreFlgs = config.NewROCoreFlags()
41 roC = newROCore(roCoreFlgs)
42 if (roC != nil) && (roCoreFlgs != nil) {
43 addr := "127.0.0.1" + ":" + freePortStr
44 cli, err := newKVClient("etcd", addr, 5)
45 if err == nil {
46 roC.kvClient = cli
47 core = NewCore("ro_core", roCoreFlgs, roC.kvClient)
48 }
49 }
50 }
51
52 return core
53}
54
55func TestNewAPIHandler_grpc(t *testing.T) {
56 core := MakeTestGrpcNbiConfig()
57 assert.NotNil(t, core)
58
59 // NewAPIHandler declares, initializes and returns an handler struct
60 apiHdl := NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
61 assert.NotNil(t, apiHdl)
62}
63
64func TestUpdateLogLevel_grpc(t *testing.T) {
65 core := MakeTestGrpcNbiConfig()
66 assert.NotNil(t, core)
67
68 var testCtx = context.Background()
69 testLogDef := &voltha.Logging{
70 ComponentName: "testing",
71 PackageName: "default",
72 Level: voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())}
73 testLogEmpty := &voltha.Logging{
74 ComponentName: "testing",
75 PackageName: "",
76 Level: voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())}
77 testLog := &voltha.Logging{
78 ComponentName: "testing",
79 PackageName: "testing",
80 Level: voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())}
npujar03b018e2019-11-13 15:29:36 +053081 testLog3 := &voltha.Logging{
Daniele Rossid5a6fb72019-11-13 15:37:07 +010082 ComponentName: "testing",
83 PackageName: "github.com/opencord/voltha-go/ro_core/core",
84 Level: 3 /*voltha.LogLevel_LogLevel(log.GetDefaultLogLevel())*/}
85 ahndl := NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
86
87 type args struct {
88 ctx context.Context
89 logging *voltha.Logging
90 }
91 tests := []struct {
92 name string
93 ah *APIHandler
94 args args
95 want int
96 wantErr error
97 }{
98 {"TestUpdateLogLevel-1", ahndl, args{testCtx, testLogDef}, 0, nil},
99 {"TestUpdateLogLevel-2", ahndl, args{testCtx, testLogEmpty}, 5, nil},
100 {"TestUpdateLogLevel-3", ahndl, args{testCtx, testLog}, 5, nil},
npujar03b018e2019-11-13 15:29:36 +0530101 {"TestUpdateLogLevel-4", ahndl, args{testCtx, testLog3}, 3, nil},
Daniele Rossid5a6fb72019-11-13 15:37:07 +0100102 }
103 for _, tt := range tests {
104 t.Run(tt.name, func(t *testing.T) {
105 _, gotErr := tt.ah.UpdateLogLevel(tt.args.ctx, tt.args.logging)
106 if tt.wantErr != gotErr {
107 t.Errorf("Error")
108 }
109 for _, packageNames := range log.GetPackageNames() {
110 logLev, errLogLev := log.GetPackageLogLevel(packageNames)
111 if errLogLev == nil {
112 if packageNames == "github.com/opencord/voltha-go/ro_core/core" {
113 if logLev != tt.want {
114 t.Errorf("Error. Want %d, Got %d", tt.want, logLev)
115 }
116 }
117 }
118 }
119 })
120 }
121}
122
123func TestGetLogLevels_grpc(t *testing.T) {
124 core := MakeTestGrpcNbiConfig()
125 assert.NotNil(t, core)
126
127 testCtx := context.Background()
128 testLc := new(common.LoggingComponent)
129 testLc.ComponentName = "testing"
130
131 ahndl := NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
132
133 type args struct {
134 ctx context.Context
135 testLc *voltha.LoggingComponent
136 }
137 tests := []struct {
138 name string
139 ah *APIHandler
140 args args
141 want int
142 wantErr error
143 }{
144 {"TestGetLogLevels-1", ahndl, args{testCtx, testLc}, 0, nil},
145 {"TestGetLogLevels-2", ahndl, args{testCtx, testLc}, 1, nil},
146 {"TestGetLogLevels-3", ahndl, args{testCtx, testLc}, 2, nil},
147 {"TestGetLogLevels-4", ahndl, args{testCtx, testLc}, 3, nil},
148 {"TestGetLogLevels-5", ahndl, args{testCtx, testLc}, 4, nil},
149 {"TestGetLogLevels-6", ahndl, args{testCtx, testLc}, 5, nil},
150 {"TestGetLogLevels-7", ahndl, args{testCtx, testLc}, 3, nil},
151 }
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}