Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 20 | "strconv" |
| 21 | "testing" |
| 22 | |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/ro_core/config" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 24 | "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 Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 27 | "github.com/phayes/freeport" |
| 28 | "github.com/stretchr/testify/assert" |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func MakeTestGrpcNbiConfig() *Core { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 32 | var ctx context.Context |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 33 | 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 S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 48 | core = NewCore(ctx, "ro_core", roCoreFlgs, roC.kvClient) |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return core |
| 54 | } |
| 55 | |
| 56 | func 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 | |
| 65 | func 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.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 73 | Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())} |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 74 | testLogEmpty := &voltha.Logging{ |
| 75 | ComponentName: "testing", |
| 76 | PackageName: "", |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 77 | Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())} |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 78 | testLog := &voltha.Logging{ |
| 79 | ComponentName: "testing", |
| 80 | PackageName: "testing", |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 81 | Level: voltha.LogLevel_Types(log.GetDefaultLogLevel())} |
npujar | 03b018e | 2019-11-13 15:29:36 +0530 | [diff] [blame] | 82 | testLog3 := &voltha.Logging{ |
Daniele Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 83 | 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.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 99 | {"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 Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 103 | } |
| 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 | |
| 124 | func 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.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 145 | {"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 Rossi | d5a6fb7 | 2019-11-13 15:37:07 +0100 | [diff] [blame] | 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 | } |