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