kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 17 | //Package core provides the utility for olt devices, flows and statistics |
| 18 | package core |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 19 | |
| 20 | import ( |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 21 | "context" |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 22 | conf "github.com/opencord/voltha-lib-go/v4/pkg/config" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 23 | "net" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 24 | "reflect" |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 25 | "sync" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 26 | "testing" |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 27 | "time" |
| 28 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 29 | "github.com/golang/protobuf/ptypes" |
| 30 | "github.com/golang/protobuf/ptypes/any" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 31 | "github.com/opencord/voltha-lib-go/v4/pkg/db" |
| 32 | fu "github.com/opencord/voltha-lib-go/v4/pkg/flows" |
| 33 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 34 | "github.com/opencord/voltha-lib-go/v4/pkg/pmmetrics" |
| 35 | ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager" |
kesavand | 494c208 | 2020-08-31 11:16:12 +0530 | [diff] [blame] | 36 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/config" |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 37 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors" |
Scott Baker | dbd960e | 2020-02-28 08:57:51 -0800 | [diff] [blame] | 38 | "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager" |
| 39 | "github.com/opencord/voltha-openolt-adapter/pkg/mocks" |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 40 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 41 | of "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 42 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 43 | oop "github.com/opencord/voltha-protos/v4/go/openolt" |
| 44 | "github.com/opencord/voltha-protos/v4/go/voltha" |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 45 | ) |
| 46 | |
Girish Gowdra | 9602eb4 | 2020-09-09 15:50:39 -0700 | [diff] [blame] | 47 | const ( |
Girish Gowdra | fb3d610 | 2020-10-16 16:32:36 -0700 | [diff] [blame] | 48 | NumPonPorts = 16 |
Girish Gowdra | 9602eb4 | 2020-09-09 15:50:39 -0700 | [diff] [blame] | 49 | OnuIDStart = 1 |
| 50 | OnuIDEnd = 32 |
| 51 | AllocIDStart = 1 |
| 52 | AllocIDEnd = 10 |
| 53 | GemIDStart = 1 |
| 54 | GemIDEnd = 10 |
| 55 | FlowIDStart = 1 |
| 56 | FlowIDEnd = 10 |
| 57 | ) |
| 58 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 59 | func newMockCoreProxy() *mocks.MockCoreProxy { |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 60 | mcp := mocks.MockCoreProxy{ |
| 61 | Devices: make(map[string]*voltha.Device), |
| 62 | DevicePorts: make(map[string][]*voltha.Port), |
| 63 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 64 | var pm []*voltha.PmConfig |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 65 | mcp.Devices["olt"] = &voltha.Device{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 66 | Id: "olt", |
| 67 | Root: true, |
| 68 | ParentId: "logical_device", |
| 69 | ParentPortNo: 1, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 70 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 71 | DeviceId: "olt", |
| 72 | DeviceType: "onu", |
| 73 | ChannelId: 1, |
| 74 | ChannelGroupId: 1, |
| 75 | }, |
| 76 | ConnectStatus: 1, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 77 | PmConfigs: &voltha.PmConfigs{ |
| 78 | DefaultFreq: 10, |
| 79 | Id: "olt", |
| 80 | FreqOverride: false, |
| 81 | Grouped: false, |
| 82 | Metrics: pm, |
| 83 | }, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 84 | } |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 85 | mcp.DevicePorts["olt"] = []*voltha.Port{ |
| 86 | {PortNo: 1, Label: "pon"}, |
| 87 | {PortNo: 2, Label: "nni"}, |
| 88 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 89 | |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 90 | mcp.Devices["onu1"] = &voltha.Device{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 91 | Id: "1", |
| 92 | Root: false, |
| 93 | ParentId: "olt", |
| 94 | ParentPortNo: 1, |
| 95 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 96 | OperStatus: 4, |
| 97 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 98 | OnuId: 1, |
| 99 | ChannelId: 1, |
| 100 | ChannelGroupId: 1, |
| 101 | }, |
| 102 | ConnectStatus: 1, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 103 | PmConfigs: &voltha.PmConfigs{ |
| 104 | DefaultFreq: 10, |
| 105 | Id: "olt", |
| 106 | FreqOverride: false, |
| 107 | Grouped: false, |
| 108 | Metrics: pm, |
| 109 | }, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 110 | } |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 111 | mcp.DevicePorts["onu1"] = []*voltha.Port{ |
| 112 | {PortNo: 1, Label: "pon"}, |
| 113 | {PortNo: 2, Label: "uni"}, |
| 114 | } |
| 115 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 116 | mcp.Devices["onu2"] = &voltha.Device{ |
| 117 | Id: "2", |
| 118 | Root: false, |
| 119 | ParentId: "olt", |
| 120 | OperStatus: 2, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 121 | |
| 122 | ParentPortNo: 1, |
| 123 | |
| 124 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 125 | OnuId: 2, |
| 126 | ChannelId: 1, |
| 127 | ChannelGroupId: 1, |
| 128 | }, |
| 129 | ConnectStatus: 1, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 130 | PmConfigs: &voltha.PmConfigs{ |
| 131 | DefaultFreq: 10, |
| 132 | Id: "olt", |
| 133 | FreqOverride: false, |
| 134 | Grouped: false, |
| 135 | Metrics: pm, |
| 136 | }, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 137 | } |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 138 | mcp.DevicePorts["onu2"] = []*voltha.Port{ |
| 139 | {PortNo: 1, Label: "pon"}, |
| 140 | {PortNo: 2, Label: "uni"}, |
| 141 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 142 | return &mcp |
| 143 | } |
| 144 | func newMockDeviceHandler() *DeviceHandler { |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 145 | device := &voltha.Device{ |
| 146 | Id: "olt", |
| 147 | Root: true, |
| 148 | ParentId: "logical_device", |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 149 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 150 | DeviceId: "olt", |
| 151 | DeviceType: "onu", |
| 152 | ChannelId: 1, |
| 153 | ChannelGroupId: 1, |
| 154 | }, |
| 155 | ConnectStatus: 1, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 156 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 157 | cp := newMockCoreProxy() |
| 158 | ap := &mocks.MockAdapterProxy{} |
| 159 | ep := &mocks.MockEventProxy{} |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 160 | cm := &conf.ConfigManager{} |
kesavand | 494c208 | 2020-08-31 11:16:12 +0530 | [diff] [blame] | 161 | cfg := &config.AdapterFlags{OmccEncryption: true} |
| 162 | openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep, config: cfg} |
Matteo Scandolo | dfa7a97 | 2020-11-06 13:03:40 -0800 | [diff] [blame] | 163 | dh := NewDeviceHandler(cp, ap, ep, device, openOLT, cm) |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 164 | oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{ |
Girish Gowdra | fb3d610 | 2020-10-16 16:32:36 -0700 | [diff] [blame] | 165 | IntfIds: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 166 | Technology: "xgs-pon", |
| 167 | Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}}, |
| 168 | }} |
| 169 | |
Girish Gowdra | 9602eb4 | 2020-09-09 15:50:39 -0700 | [diff] [blame] | 170 | deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts} |
Thomas Lee S | 985938d | 2020-05-04 11:40:41 +0530 | [diff] [blame] | 171 | rsrMgr := resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf, |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 172 | KVStore: &db.Backend{ |
| 173 | Client: &mocks.MockKVClient{}, |
| 174 | }} |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 175 | rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts) |
| 176 | rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts) |
| 177 | rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts) |
| 178 | |
| 179 | dh.resourceMgr = &rsrMgr |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 180 | dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager) |
| 181 | ranges := make(map[string]interface{}) |
| 182 | sharedIdxByType := make(map[string]string) |
| 183 | sharedIdxByType["ALLOC_ID"] = "ALLOC_ID" |
| 184 | sharedIdxByType["ONU_ID"] = "ONU_ID" |
| 185 | sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID" |
| 186 | sharedIdxByType["FLOW_ID"] = "FLOW_ID" |
| 187 | ranges["ONU_ID"] = uint32(0) |
| 188 | ranges["GEMPORT_ID"] = uint32(0) |
| 189 | ranges["ALLOC_ID"] = uint32(0) |
| 190 | ranges["FLOW_ID"] = uint32(0) |
| 191 | ranges["onu_id_shared"] = uint32(0) |
| 192 | ranges["alloc_id_shared"] = uint32(0) |
| 193 | ranges["gemport_id_shared"] = uint32(0) |
| 194 | ranges["flow_id_shared"] = uint32(0) |
| 195 | |
| 196 | ponmgr := &ponrmgr.PONResourceManager{ |
| 197 | DeviceID: "onu-1", |
Girish Gowdra | fb3d610 | 2020-10-16 16:32:36 -0700 | [diff] [blame] | 198 | IntfIDs: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, |
Abhilash Laxmeshwar | ab0bd52 | 2019-10-21 15:05:15 +0530 | [diff] [blame] | 199 | KVStore: &db.Backend{ |
| 200 | Client: &mocks.MockKVClient{}, |
| 201 | }, |
| 202 | PonResourceRanges: ranges, |
| 203 | SharedIdxByType: sharedIdxByType, |
| 204 | } |
Girish Gowdra | fb3d610 | 2020-10-16 16:32:36 -0700 | [diff] [blame] | 205 | for i := 0; i < NumPonPorts; i++ { |
| 206 | dh.resourceMgr.ResourceMgrs[uint32(i)] = ponmgr |
| 207 | } |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 208 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 209 | defer cancel() |
Girish Gowdra | 9602eb4 | 2020-09-09 15:50:39 -0700 | [diff] [blame] | 210 | dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr) |
| 211 | dh.totalPonPorts = NumPonPorts |
| 212 | dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts) |
| 213 | for i := 0; i < int(dh.totalPonPorts); i++ { |
| 214 | // Instantiate flow manager |
Girish Gowdra | a09aeab | 2020-09-14 16:30:52 -0700 | [diff] [blame] | 215 | if dh.flowMgr[i] = NewFlowManager(ctx, dh, dh.resourceMgr, dh.groupMgr, uint32(i)); dh.flowMgr[i] == nil { |
Girish Gowdra | 9602eb4 | 2020-09-09 15:50:39 -0700 | [diff] [blame] | 216 | return nil |
| 217 | } |
| 218 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 219 | dh.Client = &mocks.MockOpenoltClient{} |
kesavand | 39e0aa3 | 2020-01-28 20:58:50 -0500 | [diff] [blame] | 220 | dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh} |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 221 | dh.transitionMap = &TransitionMap{} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 222 | dh.portStats = &OpenOltStatisticsMgr{} |
Amit Ghosh | d4cbe48 | 2019-11-21 12:07:14 +0000 | [diff] [blame] | 223 | |
| 224 | var pmNames = []string{ |
| 225 | "rx_bytes", |
| 226 | "rx_packets", |
| 227 | "rx_mcast_packets", |
| 228 | "rx_bcast_packets", |
| 229 | "tx_bytes", |
| 230 | "tx_packets", |
| 231 | "tx_mcast_packets", |
| 232 | "tx_bcast_packets", |
| 233 | } |
| 234 | |
| 235 | dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames)) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 236 | return dh |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 237 | } |
| 238 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 239 | func negativeDeviceHandler() *DeviceHandler { |
| 240 | dh := newMockDeviceHandler() |
| 241 | device := dh.device |
| 242 | device.Id = "" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 243 | return dh |
| 244 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 245 | func Test_generateMacFromHost(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 246 | ctx := context.Background() |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 247 | type args struct { |
| 248 | host string |
| 249 | } |
| 250 | tests := []struct { |
| 251 | name string |
| 252 | args args |
| 253 | want string |
| 254 | wantErr bool |
| 255 | }{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 256 | {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false}, |
| 257 | {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false}, |
| 258 | //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false}, |
| 259 | {"generateMacFromHost-4", args{host: "testing3"}, "", true}, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 260 | } |
| 261 | for _, tt := range tests { |
| 262 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 263 | got, err := generateMacFromHost(ctx, tt.args.host) |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 264 | if (err != nil) != tt.wantErr { |
| 265 | t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr) |
| 266 | return |
| 267 | } |
| 268 | if got != tt.want { |
| 269 | t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want) |
| 270 | } |
| 271 | }) |
| 272 | } |
| 273 | } |
| 274 | func Test_macifyIP(t *testing.T) { |
| 275 | type args struct { |
| 276 | ip net.IP |
| 277 | } |
| 278 | tests := []struct { |
| 279 | name string |
| 280 | args args |
| 281 | want string |
| 282 | }{{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 283 | "macifyIP-1", |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 284 | args{ip: net.ParseIP("10.10.10.10")}, |
| 285 | "00:00:0a:0a:0a:0a", |
| 286 | }, |
| 287 | { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 288 | "macifyIP-2", |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 289 | args{ip: net.ParseIP("127.0.0.1")}, |
| 290 | "00:00:7f:00:00:01", |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 291 | }, |
| 292 | { |
| 293 | "macifyIP-3", |
| 294 | args{ip: net.ParseIP("127.0.0.1/24")}, |
| 295 | "", |
| 296 | }, |
| 297 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 298 | for _, tt := range tests { |
| 299 | t.Run(tt.name, func(t *testing.T) { |
| 300 | if got := macifyIP(tt.args.ip); got != tt.want { |
| 301 | t.Errorf("macifyIP() = %v, want %v", got, tt.want) |
| 302 | } |
| 303 | }) |
| 304 | } |
| 305 | } |
| 306 | |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 307 | func sparseCompare(keys []string, spec, target interface{}) bool { |
| 308 | if spec == target { |
| 309 | return true |
| 310 | } |
| 311 | if spec == nil || target == nil { |
| 312 | return false |
| 313 | } |
| 314 | typeSpec := reflect.TypeOf(spec) |
| 315 | typeTarget := reflect.TypeOf(target) |
| 316 | if typeSpec != typeTarget { |
| 317 | return false |
| 318 | } |
| 319 | |
| 320 | vSpec := reflect.ValueOf(spec) |
| 321 | vTarget := reflect.ValueOf(target) |
| 322 | if vSpec.Kind() == reflect.Ptr { |
| 323 | vSpec = vSpec.Elem() |
| 324 | vTarget = vTarget.Elem() |
| 325 | } |
| 326 | |
| 327 | for _, key := range keys { |
| 328 | fSpec := vSpec.FieldByName(key) |
| 329 | fTarget := vTarget.FieldByName(key) |
| 330 | if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) { |
| 331 | return false |
| 332 | } |
| 333 | } |
| 334 | return true |
| 335 | } |
| 336 | |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 337 | func TestDeviceHandler_GetChildDevice(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 338 | ctx := context.Background() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 339 | dh1 := newMockDeviceHandler() |
| 340 | dh2 := negativeDeviceHandler() |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 341 | type args struct { |
| 342 | parentPort uint32 |
| 343 | onuID uint32 |
| 344 | } |
| 345 | tests := []struct { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 346 | name string |
| 347 | devicehandler *DeviceHandler |
| 348 | args args |
| 349 | want *voltha.Device |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 350 | errType reflect.Type |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 351 | }{ |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 352 | {"GetChildDevice-1", dh1, |
| 353 | args{parentPort: 1, |
| 354 | onuID: 1}, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 355 | &voltha.Device{ |
| 356 | Id: "1", |
| 357 | ParentId: "olt", |
| 358 | ParentPortNo: 1, |
| 359 | }, |
| 360 | nil, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 361 | }, |
| 362 | {"GetChildDevice-2", dh2, |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 363 | args{parentPort: 1, |
| 364 | onuID: 1}, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 365 | nil, |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 366 | reflect.TypeOf(&olterrors.ErrNotFound{}), |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 367 | }, |
| 368 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s) |
| 372 | device_handler_test.go:309: GetportLabel() => want=(, <nil>) got=(id:"1" parent_id:"olt" parent_port_no:1 proxy_address:<channel_id:1 channel_group_id:1 onu_id:1 > oper_status:ACTIVE connect_status:UNREACHABLE ports:<port_no:1 label:"pon" > ports:<port_no:2 label:"uni" > pm_configs:<id:"olt" default_freq:10 > , <nil>) |
| 373 | --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s) |
| 374 | */ |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 375 | for _, tt := range tests { |
| 376 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 377 | got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID) |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 378 | if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) { |
| 379 | t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)", |
| 380 | tt.want, tt.errType, got, reflect.TypeOf(err)) |
| 381 | return |
| 382 | } |
kdarapu | 381c690 | 2019-07-31 18:23:16 +0530 | [diff] [blame] | 383 | t.Log("onu device id", got) |
| 384 | }) |
| 385 | } |
| 386 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 387 | |
| 388 | func TestGetportLabel(t *testing.T) { |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 389 | invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 390 | type args struct { |
| 391 | portNum uint32 |
| 392 | portType voltha.Port_PortType |
| 393 | } |
| 394 | tests := []struct { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 395 | name string |
| 396 | args args |
| 397 | want string |
| 398 | errType reflect.Type |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 399 | }{ |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 400 | {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid}, |
| 401 | {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil}, |
| 402 | {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid}, |
| 403 | {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil}, |
| 404 | {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid}, |
| 405 | {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid}, |
| 406 | {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 407 | } |
| 408 | for _, tt := range tests { |
| 409 | t.Run(tt.name, func(t *testing.T) { |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 410 | got, err := GetportLabel(tt.args.portNum, tt.args.portType) |
| 411 | if reflect.TypeOf(err) != tt.errType || got != tt.want { |
| 412 | t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)", |
| 413 | tt.want, tt.errType, got, reflect.TypeOf(err)) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 414 | } |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 415 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 416 | }) |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 421 | ctx := context.Background() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 422 | dh := newMockDeviceHandler() |
| 423 | proxyAddr := dh.device.ProxyAddress |
| 424 | body := &ic.InterAdapterOmciMessage{ |
| 425 | Message: []byte("asdfasdfasdfasdfas"), |
| 426 | ProxyAddress: proxyAddr, |
| 427 | } |
| 428 | body2 := &ic.InterAdapterOmciMessage{ |
| 429 | Message: []byte("asdfasdfasdfasdfas"), |
| 430 | //ProxyAddress: &voltha.Device_ProxyAddress{}, |
| 431 | } |
| 432 | body3 := &ic.InterAdapterTechProfileDownloadMessage{} |
| 433 | var marshalledData *any.Any |
| 434 | var err error |
| 435 | |
| 436 | if marshalledData, err = ptypes.MarshalAny(body); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 437 | logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | var marshalledData1 *any.Any |
| 441 | |
| 442 | if marshalledData1, err = ptypes.MarshalAny(body2); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 443 | logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 444 | } |
| 445 | var marshalledData2 *any.Any |
| 446 | |
| 447 | if marshalledData2, err = ptypes.MarshalAny(body3); err != nil { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 448 | logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 449 | } |
| 450 | type args struct { |
| 451 | msg *ic.InterAdapterMessage |
| 452 | } |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 453 | invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{}) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 454 | tests := []struct { |
| 455 | name string |
| 456 | args args |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 457 | wantErr reflect.Type |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 458 | }{ |
| 459 | {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{ |
| 460 | Header: &ic.InterAdapterHeader{ |
| 461 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 462 | Type: ic.InterAdapterMessageType_FLOW_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 463 | }, |
| 464 | Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 465 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 466 | {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{ |
| 467 | Header: &ic.InterAdapterHeader{ |
| 468 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 469 | Type: ic.InterAdapterMessageType_FLOW_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 470 | }, |
| 471 | Body: marshalledData1, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 472 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 473 | {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{ |
| 474 | Header: &ic.InterAdapterHeader{ |
| 475 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 476 | Type: ic.InterAdapterMessageType_OMCI_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 477 | }, |
| 478 | Body: marshalledData, |
Thomas Lee S | 94109f1 | 2020-03-03 16:39:29 +0530 | [diff] [blame] | 479 | }}, reflect.TypeOf(&olterrors.ErrCommunication{})}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 480 | {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{ |
| 481 | Header: &ic.InterAdapterHeader{ |
| 482 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 483 | Type: ic.InterAdapterMessageType_OMCI_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 484 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 485 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 486 | {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{ |
| 487 | Header: &ic.InterAdapterHeader{ |
| 488 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 489 | Type: ic.InterAdapterMessageType_METRICS_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 490 | }, Body: marshalledData1, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 491 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 492 | {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{ |
| 493 | Header: &ic.InterAdapterHeader{ |
| 494 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 495 | Type: ic.InterAdapterMessageType_METRICS_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 496 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 497 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 498 | {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{ |
| 499 | Header: &ic.InterAdapterHeader{ |
| 500 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 501 | Type: ic.InterAdapterMessageType_ONU_IND_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 502 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 503 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 504 | {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{ |
| 505 | Header: &ic.InterAdapterHeader{ |
| 506 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 507 | Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 508 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 509 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 510 | {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{ |
| 511 | Header: &ic.InterAdapterHeader{ |
| 512 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 513 | Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 514 | }, Body: marshalledData, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 515 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 516 | {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{ |
| 517 | Header: &ic.InterAdapterHeader{ |
| 518 | Id: "012345", |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 519 | Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 520 | }, Body: marshalledData2, |
David K. Bainbridge | 794735f | 2020-02-11 21:01:37 -0800 | [diff] [blame] | 521 | }}, invalid}, |
| 522 | {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{ |
| 523 | Header: &ic.InterAdapterHeader{ |
| 524 | Id: "012345", |
| 525 | Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST, |
| 526 | }, Body: marshalledData2, |
| 527 | }}, invalid}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 528 | } |
| 529 | for _, tt := range tests { |
| 530 | t.Run(tt.name, func(t *testing.T) { |
| 531 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 532 | if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 533 | t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr) |
| 534 | } |
| 535 | }) |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | func TestDeviceHandler_sendProxiedMessage(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 540 | ctx := context.Background() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 541 | dh1 := newMockDeviceHandler() |
| 542 | dh2 := negativeDeviceHandler() |
| 543 | device1 := &voltha.Device{ |
| 544 | Id: "onu1", |
| 545 | Root: false, |
| 546 | ParentId: "logical_device", |
| 547 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 548 | DeviceId: "onu1", |
| 549 | DeviceType: "onu", |
| 550 | ChannelId: 1, |
| 551 | ChannelGroupId: 1, |
| 552 | }, |
| 553 | ConnectStatus: 1, |
| 554 | } |
| 555 | device2 := device1 |
| 556 | device2.ConnectStatus = 2 |
| 557 | iaomciMsg1 := &ic.InterAdapterOmciMessage{ |
| 558 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 559 | DeviceId: "onu2", |
| 560 | DeviceType: "onu", |
| 561 | ChannelId: 1, |
| 562 | ChannelGroupId: 1, |
| 563 | //OnuId: 2, |
| 564 | }, |
| 565 | ConnectStatus: 1, |
| 566 | } |
| 567 | iaomciMsg2 := &ic.InterAdapterOmciMessage{ |
| 568 | ProxyAddress: &voltha.Device_ProxyAddress{ |
| 569 | DeviceId: "onu3", |
| 570 | DeviceType: "onu", |
| 571 | ChannelId: 1, |
| 572 | ChannelGroupId: 1, |
| 573 | }, |
| 574 | ConnectStatus: 1, |
| 575 | } |
| 576 | type args struct { |
| 577 | onuDevice *voltha.Device |
| 578 | omciMsg *ic.InterAdapterOmciMessage |
| 579 | } |
| 580 | tests := []struct { |
| 581 | name string |
| 582 | devicehandler *DeviceHandler |
| 583 | args args |
| 584 | }{ |
| 585 | {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}}, |
| 586 | {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}}, |
| 587 | {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}}, |
| 588 | {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}}, |
| 589 | {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}}, |
| 590 | {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}}, |
| 591 | } |
| 592 | for _, tt := range tests { |
| 593 | t.Run(tt.name, func(t *testing.T) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 594 | _ = tt.devicehandler.sendProxiedMessage(ctx, tt.args.onuDevice, tt.args.omciMsg) |
| 595 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 596 | }) |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | func TestDeviceHandler_SendPacketInToCore(t *testing.T) { |
| 601 | dh1 := newMockDeviceHandler() |
| 602 | dh2 := negativeDeviceHandler() |
| 603 | |
| 604 | type args struct { |
| 605 | logicalPort uint32 |
| 606 | packetPayload []byte |
| 607 | } |
| 608 | tests := []struct { |
| 609 | name string |
| 610 | devicehandler *DeviceHandler |
| 611 | args args |
| 612 | }{ |
| 613 | {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}}, |
| 614 | {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}}, |
| 615 | {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}}, |
| 616 | } |
| 617 | for _, tt := range tests { |
| 618 | t.Run(tt.name, func(t *testing.T) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 619 | _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload) |
| 620 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 621 | }) |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | func TestDeviceHandler_DisableDevice(t *testing.T) { |
| 626 | dh1 := newMockDeviceHandler() |
| 627 | dh2 := negativeDeviceHandler() |
| 628 | type args struct { |
| 629 | device *voltha.Device |
| 630 | } |
| 631 | tests := []struct { |
| 632 | name string |
| 633 | devicehandler *DeviceHandler |
| 634 | args args |
| 635 | wantErr bool |
| 636 | }{ |
| 637 | {"DisableDevice-1", dh1, args{device: dh1.device}, false}, |
Chaitrashree G S | 3b4c035 | 2019-09-09 20:59:29 -0400 | [diff] [blame] | 638 | {"DisableDevice-2", dh1, args{device: dh2.device}, true}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 639 | } |
| 640 | for _, tt := range tests { |
| 641 | t.Run(tt.name, func(t *testing.T) { |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 642 | if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 643 | t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr) |
| 644 | } |
| 645 | }) |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | func TestDeviceHandler_ReenableDevice(t *testing.T) { |
| 650 | dh1 := newMockDeviceHandler() |
| 651 | dh2 := negativeDeviceHandler() |
| 652 | type args struct { |
| 653 | device *voltha.Device |
| 654 | } |
| 655 | tests := []struct { |
| 656 | name string |
| 657 | devicehandler *DeviceHandler |
| 658 | args args |
| 659 | wantErr bool |
| 660 | }{ |
| 661 | {"ReenableDevice-1", dh1, args{device: dh1.device}, false}, |
| 662 | {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true}, |
| 663 | {"ReenableDevice-3", dh2, args{device: dh1.device}, false}, |
| 664 | } |
| 665 | for _, tt := range tests { |
| 666 | t.Run(tt.name, func(t *testing.T) { |
| 667 | dh := tt.devicehandler |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 668 | if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 669 | t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr) |
| 670 | } |
| 671 | }) |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | func TestDeviceHandler_RebootDevice(t *testing.T) { |
| 676 | dh1 := newMockDeviceHandler() |
| 677 | dh2 := newMockDeviceHandler() |
| 678 | type args struct { |
| 679 | device *voltha.Device |
| 680 | } |
| 681 | tests := []struct { |
| 682 | name string |
| 683 | devicehandler *DeviceHandler |
| 684 | args args |
| 685 | wantErr bool |
| 686 | }{ |
| 687 | // TODO: Add test cases. |
| 688 | {"RebootDevice-1", dh1, args{device: dh1.device}, false}, |
| 689 | {"RebootDevice-2", dh1, args{device: dh2.device}, true}, |
| 690 | {"RebootDevice-3", dh2, args{device: dh2.device}, false}, |
| 691 | } |
| 692 | for _, tt := range tests { |
| 693 | t.Run(tt.name, func(t *testing.T) { |
| 694 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 695 | if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 696 | t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr) |
| 697 | } |
| 698 | }) |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | func TestDeviceHandler_handleIndication(t *testing.T) { |
| 703 | dh1 := newMockDeviceHandler() |
| 704 | dh2 := negativeDeviceHandler() |
| 705 | dh3 := newMockDeviceHandler() |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 706 | dh3.onus = sync.Map{} |
Thiyagarajan Subramani | 34a0028 | 2020-03-10 20:19:31 +0530 | [diff] [blame] | 707 | dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false)) |
| 708 | dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false)) |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 709 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 710 | type args struct { |
| 711 | indication *oop.Indication |
| 712 | } |
| 713 | tests := []struct { |
| 714 | name string |
| 715 | deviceHandler *DeviceHandler |
| 716 | args args |
| 717 | }{ |
| 718 | // TODO: Add test cases. |
| 719 | {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}}, |
| 720 | {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}}, |
| 721 | {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}}, |
| 722 | {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}}, |
| 723 | {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}}, |
| 724 | {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}}, |
| 725 | {"handleIndication-7", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuDiscInd{OnuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}}}, |
| 726 | {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}}, |
| 727 | {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}}, |
| 728 | {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}}, |
| 729 | {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}}, |
| 730 | {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}}, |
| 731 | {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}}, |
| 732 | {"handleIndication-14", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PortStats{PortStats: &oop.PortStatistics{IntfId: 1, RxBytes: 100, RxPackets: 100, RxUcastPackets: 100, RxMcastPackets: 100, RxBcastPackets: 100, RxErrorPackets: 100, TxBytes: 100, TxPackets: 100, TxUcastPackets: 100, TxMcastPackets: 100, TxBcastPackets: 100, TxErrorPackets: 100, RxCrcErrors: 100, BipErrors: 100, Timestamp: 1000}}}}}, |
| 733 | {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}}, |
| 734 | {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}}, |
| 735 | {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}}, |
| 736 | {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}}, |
| 737 | |
| 738 | // Negative testcases |
| 739 | {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}}, |
| 740 | {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}}, |
| 741 | {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}}, |
| 742 | {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}}, |
| 743 | {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}}, |
| 744 | {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}}, |
| 745 | {"handleIndication-25", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuDiscInd{OnuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}}}, |
| 746 | {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}}, |
| 747 | {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}}, |
| 748 | {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}}, |
| 749 | {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}}, |
| 750 | {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}}, |
| 751 | {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}}, |
| 752 | {"handleIndication-32", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PortStats{PortStats: &oop.PortStatistics{IntfId: 1, RxBytes: 100, RxPackets: 100, RxUcastPackets: 100, RxMcastPackets: 100, RxBcastPackets: 100, RxErrorPackets: 100, TxBytes: 100, TxPackets: 100, TxUcastPackets: 100, TxMcastPackets: 100, TxBcastPackets: 100, TxErrorPackets: 100, RxCrcErrors: 100, BipErrors: 100, Timestamp: 1000}}}}}, |
| 753 | {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}}, |
| 754 | {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}}, |
| 755 | // |
| 756 | {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}}, |
| 757 | {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}}, |
| 758 | {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}}, |
| 759 | {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}}, |
| 760 | {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}}, |
| 761 | {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}}, |
| 762 | } |
| 763 | for _, tt := range tests { |
| 764 | t.Run(tt.name, func(t *testing.T) { |
| 765 | dh := tt.deviceHandler |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 766 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 767 | defer cancel() |
| 768 | dh.handleIndication(ctx, tt.args.indication) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 769 | }) |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | func TestDeviceHandler_addPort(t *testing.T) { |
| 774 | dh1 := newMockDeviceHandler() |
| 775 | dh2 := negativeDeviceHandler() |
| 776 | type args struct { |
| 777 | intfID uint32 |
| 778 | portType voltha.Port_PortType |
| 779 | state string |
| 780 | } |
| 781 | tests := []struct { |
| 782 | name string |
| 783 | devicehandler *DeviceHandler |
| 784 | args args |
| 785 | }{ |
| 786 | // State up |
| 787 | {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}}, |
| 788 | {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}}, |
| 789 | {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}}, |
| 790 | {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}}, |
| 791 | {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}}, |
| 792 | {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}}, |
| 793 | {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}}, |
| 794 | {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}}, |
| 795 | // state discovery |
| 796 | {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}}, |
| 797 | {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}}, |
| 798 | {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}}, |
| 799 | {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}}, |
| 800 | {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}}, |
| 801 | {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}}, |
| 802 | {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}}, |
| 803 | {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}}, |
| 804 | |
| 805 | {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}}, |
| 806 | {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}}, |
| 807 | {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}}, |
| 808 | {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}}, |
| 809 | } |
| 810 | for _, tt := range tests { |
| 811 | t.Run(tt.name, func(t *testing.T) { |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 812 | _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state) |
| 813 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 814 | }) |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | func Test_macAddressToUint32Array(t *testing.T) { |
| 819 | type args struct { |
| 820 | mac string |
| 821 | } |
| 822 | tests := []struct { |
| 823 | name string |
| 824 | args args |
| 825 | want []uint32 |
| 826 | }{ |
| 827 | // TODO: Add test cases. |
| 828 | {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}}, |
| 829 | {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}}, |
| 830 | {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}}, |
| 831 | } |
| 832 | for _, tt := range tests { |
| 833 | t.Run(tt.name, func(t *testing.T) { |
| 834 | if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) { |
| 835 | t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want) |
| 836 | } |
| 837 | }) |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | func TestDeviceHandler_handleOltIndication(t *testing.T) { |
| 842 | |
| 843 | type args struct { |
| 844 | oltIndication *oop.OltIndication |
| 845 | } |
| 846 | tests := []struct { |
| 847 | name string |
| 848 | args args |
| 849 | }{ |
| 850 | {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}}, |
| 851 | {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}}, |
| 852 | } |
| 853 | for _, tt := range tests { |
| 854 | t.Run(tt.name, func(t *testing.T) { |
| 855 | dh := newMockDeviceHandler() |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 856 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 857 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 858 | if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil { |
| 859 | t.Error(err) |
| 860 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 861 | }) |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | func TestDeviceHandler_AdoptDevice(t *testing.T) { |
| 866 | dh1 := newMockDeviceHandler() |
| 867 | dh2 := negativeDeviceHandler() |
| 868 | type args struct { |
| 869 | device *voltha.Device |
| 870 | } |
| 871 | tests := []struct { |
| 872 | name string |
| 873 | devicehandler *DeviceHandler |
| 874 | args args |
| 875 | }{ |
| 876 | // TODO: Add test cases. |
| 877 | {"AdoptDevice-1", dh1, args{device: dh1.device}}, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 878 | {"AdoptDevice-2", dh2, args{device: dh2.device}}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 879 | } |
| 880 | for _, tt := range tests { |
| 881 | t.Run(tt.name, func(t *testing.T) { |
| 882 | //dh.doStateInit() |
| 883 | // context. |
| 884 | //dh.AdoptDevice(tt.args.device) |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 885 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 886 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 887 | if err := tt.devicehandler.postInit(ctx); err != nil { |
| 888 | t.Error(err) |
| 889 | } |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 890 | }) |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | func TestDeviceHandler_activateONU(t *testing.T) { |
| 895 | dh := newMockDeviceHandler() |
| 896 | dh1 := negativeDeviceHandler() |
| 897 | type args struct { |
| 898 | intfID uint32 |
| 899 | onuID int64 |
| 900 | serialNum *oop.SerialNumber |
| 901 | serialNumber string |
| 902 | } |
| 903 | tests := []struct { |
| 904 | name string |
| 905 | devicehandler *DeviceHandler |
| 906 | args args |
| 907 | }{ |
Girish Gowdra | 38d533d | 2020-03-30 20:38:51 -0700 | [diff] [blame] | 908 | {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}}, |
| 909 | {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}}, |
| 910 | {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}}, |
| 911 | {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 912 | } |
| 913 | for _, tt := range tests { |
| 914 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 915 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 916 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 917 | _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber) |
| 918 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 919 | }) |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | func TestDeviceHandler_start(t *testing.T) { |
| 924 | dh := newMockDeviceHandler() |
| 925 | dh1 := negativeDeviceHandler() |
| 926 | dh.start(context.Background()) |
| 927 | dh.stop(context.Background()) |
| 928 | |
| 929 | dh1.start(context.Background()) |
| 930 | dh1.stop(context.Background()) |
| 931 | |
| 932 | } |
| 933 | |
| 934 | func TestDeviceHandler_PacketOut(t *testing.T) { |
| 935 | dh1 := newMockDeviceHandler() |
| 936 | dh2 := negativeDeviceHandler() |
| 937 | acts := []*ofp.OfpAction{ |
| 938 | fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))), |
| 939 | fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)), |
| 940 | fu.Output(1), |
| 941 | } |
| 942 | pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")} |
| 943 | type args struct { |
| 944 | egressPortNo int |
| 945 | packet *of.OfpPacketOut |
| 946 | } |
| 947 | tests := []struct { |
| 948 | name string |
| 949 | devicehandler *DeviceHandler |
| 950 | args args |
| 951 | wantErr bool |
| 952 | }{ |
| 953 | // TODO: Add test cases. |
| 954 | //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true}, |
| 955 | {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false}, |
| 956 | {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false}, |
Matteo Scandolo | 2c0d274 | 2020-06-10 11:28:42 -0700 | [diff] [blame] | 957 | {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false}, |
| 958 | {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false}, |
| 959 | {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 960 | } |
| 961 | for _, tt := range tests { |
| 962 | t.Run(tt.name, func(t *testing.T) { |
| 963 | dh := tt.devicehandler |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 964 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 965 | defer cancel() |
| 966 | if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 967 | t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr) |
| 968 | } |
| 969 | }) |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // |
| 974 | func TestDeviceHandler_doStateUp(t *testing.T) { |
| 975 | dh1 := newMockDeviceHandler() |
| 976 | dh2 := newMockDeviceHandler() |
| 977 | |
Thomas Lee S | 985938d | 2020-05-04 11:40:41 +0530 | [diff] [blame] | 978 | dh2.device.Id = "" |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 979 | dh3 := negativeDeviceHandler() |
| 980 | |
| 981 | tests := []struct { |
| 982 | name string |
| 983 | devicehandler *DeviceHandler |
| 984 | wantErr bool |
| 985 | }{ |
| 986 | {"dostateup-1", dh1, false}, |
| 987 | {"dostateup-2", dh2, false}, |
| 988 | {"dostateup-3", dh3, true}, |
| 989 | } |
| 990 | for _, tt := range tests { |
| 991 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 992 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 993 | defer cancel() |
| 994 | if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 995 | t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr) |
| 996 | } |
Thomas Lee S | 85f3731 | 2020-04-03 17:06:12 +0530 | [diff] [blame] | 997 | tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 998 | }) |
| 999 | } |
| 1000 | } |
| 1001 | func TestDeviceHandler_doStateDown(t *testing.T) { |
| 1002 | dh1 := newMockDeviceHandler() |
| 1003 | dh2 := negativeDeviceHandler() |
| 1004 | dh3 := newMockDeviceHandler() |
| 1005 | dh3.device.OperStatus = voltha.OperStatus_UNKNOWN |
| 1006 | tests := []struct { |
| 1007 | name string |
| 1008 | devicehandler *DeviceHandler |
| 1009 | wantErr bool |
| 1010 | }{ |
| 1011 | {"dostatedown-1", dh1, false}, |
| 1012 | {"dostatedown-2", dh2, true}, |
| 1013 | {"dostatedown-2", dh3, true}, |
| 1014 | } |
| 1015 | for _, tt := range tests { |
| 1016 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1017 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1018 | defer cancel() |
| 1019 | if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr { |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1020 | t.Logf("DeviceHandler.doStateDown() error = %v", err) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1021 | //TODO: should fail this test case (Errorf) if result is not as expected |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1022 | } |
| 1023 | }) |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) { |
| 1028 | dh1 := newMockDeviceHandler() |
| 1029 | dh2 := negativeDeviceHandler() |
| 1030 | type args struct { |
| 1031 | device *voltha.Device |
| 1032 | } |
| 1033 | tests := []struct { |
| 1034 | name string |
| 1035 | devicehandler *DeviceHandler |
| 1036 | args args |
| 1037 | wantErr bool |
| 1038 | }{ |
| 1039 | // TODO: Add test cases. |
| 1040 | {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false}, |
| 1041 | {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false}, |
| 1042 | {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false}, |
| 1043 | } |
| 1044 | for _, tt := range tests { |
| 1045 | t.Run(tt.name, func(t *testing.T) { |
| 1046 | dh := tt.devicehandler |
| 1047 | _, err := dh.GetOfpDeviceInfo(tt.args.device) |
| 1048 | if (err != nil) != tt.wantErr { |
| 1049 | t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 1050 | return |
| 1051 | } |
| 1052 | }) |
| 1053 | } |
| 1054 | } |
| 1055 | |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1056 | func TestDeviceHandler_onuDiscIndication(t *testing.T) { |
| 1057 | |
| 1058 | dh1 := newMockDeviceHandler() |
Naga Manjunath | a8dc937 | 2019-10-31 23:01:18 +0530 | [diff] [blame] | 1059 | dh1.discOnus = sync.Map{} |
| 1060 | dh1.discOnus.Store("onu1", true) |
| 1061 | dh1.discOnus.Store("onu2", false) |
Thiyagarajan Subramani | 34a0028 | 2020-03-10 20:19:31 +0530 | [diff] [blame] | 1062 | dh1.discOnus.Store("onu3", true) |
| 1063 | dh1.discOnus.Store("onu4", true) |
| 1064 | dh1.onus = sync.Map{} |
| 1065 | dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true)) |
| 1066 | dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true)) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1067 | dh2 := negativeDeviceHandler() |
| 1068 | type args struct { |
| 1069 | onuDiscInd *oop.OnuDiscIndication |
| 1070 | sn string |
| 1071 | } |
| 1072 | tests := []struct { |
| 1073 | name string |
| 1074 | devicehandler *DeviceHandler |
| 1075 | args args |
| 1076 | }{ |
| 1077 | // TODO: Add test cases. |
| 1078 | {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}, |
| 1079 | {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}}, |
| 1080 | {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}}, |
| 1081 | {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}}, |
| 1082 | {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}}, |
| 1083 | {"onuDiscIndication-6", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu2"}}, |
Thiyagarajan Subramani | 34a0028 | 2020-03-10 20:19:31 +0530 | [diff] [blame] | 1084 | {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}}, |
| 1085 | {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}}, |
| 1086 | {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}, |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1087 | } |
| 1088 | for _, tt := range tests { |
| 1089 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1090 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1091 | defer cancel() |
Mahir Gunyel | b004675 | 2021-02-26 13:51:05 -0800 | [diff] [blame] | 1092 | _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd) |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1093 | //TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1094 | }) |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | func TestDeviceHandler_populateDeviceInfo(t *testing.T) { |
| 1099 | dh1 := newMockDeviceHandler() |
| 1100 | dh2 := negativeDeviceHandler() |
| 1101 | tests := []struct { |
| 1102 | name string |
| 1103 | devicehandler *DeviceHandler |
| 1104 | |
| 1105 | wantErr bool |
| 1106 | }{ |
| 1107 | // TODO: Add test cases. |
| 1108 | {"populateDeviceInfo-1", dh1, false}, |
| 1109 | {"populateDeviceInfo-2", dh1, true}, |
| 1110 | {"populateDeviceInfo-3", dh1, true}, |
| 1111 | {"populateDeviceInfo-4", dh1, true}, |
| 1112 | {"populateDeviceInfo-5", dh2, true}, |
| 1113 | } |
| 1114 | for _, tt := range tests { |
| 1115 | t.Run(tt.name, func(t *testing.T) { |
| 1116 | |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1117 | _, err := tt.devicehandler.populateDeviceInfo(context.Background()) |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1118 | if (err != nil) != tt.wantErr { |
| 1119 | t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 1120 | return |
| 1121 | } |
| 1122 | |
| 1123 | }) |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | func TestDeviceHandler_readIndications(t *testing.T) { |
| 1128 | dh1 := newMockDeviceHandler() |
| 1129 | dh2 := newMockDeviceHandler() |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1130 | dh3 := newMockDeviceHandler() |
| 1131 | dh3.device.AdminState = voltha.AdminState_DISABLED |
| 1132 | dh4 := negativeDeviceHandler() |
| 1133 | tests := []struct { |
| 1134 | name string |
| 1135 | devicehandler *DeviceHandler |
| 1136 | }{ |
| 1137 | // TODO: Add test cases. |
| 1138 | {"readIndications-1", dh1}, |
| 1139 | {"readIndications-2", dh2}, |
| 1140 | {"readIndications-3", dh2}, |
| 1141 | {"readIndications-4", dh2}, |
| 1142 | {"readIndications-5", dh2}, |
| 1143 | {"readIndications-6", dh3}, |
| 1144 | {"readIndications-7", dh3}, |
| 1145 | {"readIndications-8", dh4}, |
| 1146 | } |
| 1147 | for _, tt := range tests { |
| 1148 | t.Run(tt.name, func(t *testing.T) { |
npujar | ec5762e | 2020-01-01 14:08:48 +0530 | [diff] [blame] | 1149 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1150 | defer cancel() |
Kent Hagerman | e6ff101 | 2020-07-14 15:07:53 -0400 | [diff] [blame] | 1151 | _ = tt.devicehandler.readIndications(ctx) |
| 1152 | // TODO: actually verify test cases |
kdarapu | 891693b | 2019-09-16 12:33:49 +0530 | [diff] [blame] | 1153 | }) |
| 1154 | } |
| 1155 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1156 | |
| 1157 | func Test_startCollector(t *testing.T) { |
| 1158 | type args struct { |
| 1159 | dh *DeviceHandler |
| 1160 | } |
| 1161 | dh := newMockDeviceHandler() |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 1162 | dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{ |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1163 | {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT}, |
| 1164 | {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI}, |
| 1165 | {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI}, |
| 1166 | } |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1167 | dh.portStats.NorthBoundPort = make(map[uint32]*NniPort) |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1168 | dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"} |
| 1169 | dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1170 | dh.portStats.SouthBoundPort = make(map[uint32]*PonPort) |
| 1171 | dh.portStats.Device = dh |
| 1172 | for i := 0; i < 16; i++ { |
| 1173 | dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"} |
| 1174 | } |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1175 | dh1 := newMockDeviceHandler() |
Kent Hagerman | f1db18b | 2020-07-08 13:38:15 -0400 | [diff] [blame] | 1176 | dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{} |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1177 | tests := []struct { |
| 1178 | name string |
| 1179 | args args |
| 1180 | }{ |
| 1181 | // TODO: Add test cases. |
| 1182 | {"StartCollector-1", args{dh}}, |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1183 | {"StartCollector-2", args{dh1}}, |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1184 | } |
| 1185 | for _, tt := range tests { |
| 1186 | t.Run(tt.name, func(t *testing.T) { |
| 1187 | go func() { |
Kishore Darapu | aaf9c10 | 2020-05-04 13:06:57 +0530 | [diff] [blame] | 1188 | time.Sleep(1 * time.Second) // simulated wait time to stop startCollector |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1189 | tt.args.dh.stopCollector <- true |
| 1190 | }() |
Neha Sharma | 96b7bf2 | 2020-06-15 10:37:32 +0000 | [diff] [blame] | 1191 | startCollector(context.Background(), tt.args.dh) |
Naga Manjunath | 7615e55 | 2019-10-11 22:35:47 +0530 | [diff] [blame] | 1192 | }) |
| 1193 | } |
| 1194 | } |
Gamze Abaka | c2c32a6 | 2021-03-11 11:44:18 +0000 | [diff] [blame] | 1195 | |
| 1196 | func TestDeviceHandler_TestReconcileStatus(t *testing.T) { |
| 1197 | |
| 1198 | // olt disconnect (not reboot) |
| 1199 | dh1 := newMockDeviceHandler() |
| 1200 | dh1.adapterPreviouslyConnected = false |
| 1201 | dh1.agentPreviouslyConnected = true |
| 1202 | |
| 1203 | // adapter restart |
| 1204 | dh2 := newMockDeviceHandler() |
| 1205 | dh2.Client = &mocks.MockOpenoltClient{} |
| 1206 | dh2.adapterPreviouslyConnected = true |
| 1207 | dh2.agentPreviouslyConnected = true |
| 1208 | |
| 1209 | // first connection or olt restart |
| 1210 | dh3 := newMockDeviceHandler() |
| 1211 | dh3.Client = &mocks.MockOpenoltClient{} |
| 1212 | dh3.adapterPreviouslyConnected = false |
| 1213 | dh3.agentPreviouslyConnected = false |
| 1214 | |
| 1215 | // olt and adapter restart at the same time (first case) |
| 1216 | dh4 := newMockDeviceHandler() |
| 1217 | dh4.Client = &mocks.MockOpenoltClient{} |
| 1218 | dh4.adapterPreviouslyConnected = true |
| 1219 | dh4.agentPreviouslyConnected = false |
| 1220 | |
| 1221 | // adapter restart and olt disconnect at the same time |
| 1222 | dh5 := newMockDeviceHandler() |
| 1223 | dh5.Client = &mocks.MockOpenoltClient{} |
| 1224 | dh5.adapterPreviouslyConnected = true |
| 1225 | dh5.agentPreviouslyConnected = true |
| 1226 | |
| 1227 | tests := []struct { |
| 1228 | name string |
| 1229 | devicehandler *DeviceHandler |
| 1230 | expectedRestart bool |
| 1231 | wantErr bool |
| 1232 | }{ |
| 1233 | {"dostateup-1", dh1, true, false}, |
| 1234 | {"dostateup-2", dh2, false, false}, |
| 1235 | {"dostateup-3", dh3, false, false}, |
| 1236 | {"dostateup-4", dh4, true, false}, |
| 1237 | {"dostateup-5", dh5, false, false}, |
| 1238 | } |
| 1239 | for _, tt := range tests { |
| 1240 | t.Run(tt.name, func(t *testing.T) { |
| 1241 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 1242 | defer cancel() |
| 1243 | if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr { |
| 1244 | t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr) |
| 1245 | } |
| 1246 | tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp |
| 1247 | isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted |
| 1248 | if tt.expectedRestart != isRestarted { |
| 1249 | t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted) |
| 1250 | } |
| 1251 | }) |
| 1252 | } |
| 1253 | } |