Daniele Rossi | 7342208 | 2019-10-30 17:41:24 +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 ( |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 19 | "github.com/opencord/voltha-go/db/model" |
Daniele Rossi | 7342208 | 2019-10-30 17:41:24 +0100 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/ro_core/config" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 21 | "github.com/opencord/voltha-lib-go/v2/pkg/db" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Daniele Rossi | 7342208 | 2019-10-30 17:41:24 +0100 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
| 27 | func MakeTestDevManagerConfig() (*Core, error) { |
| 28 | var core Core |
| 29 | core.instanceId = "ro_core" |
| 30 | core.config = config.NewROCoreFlags() |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 31 | backend := db.Backend{ |
Daniele Rossi | 7342208 | 2019-10-30 17:41:24 +0100 | [diff] [blame] | 32 | Client: core.kvClient, |
| 33 | StoreType: core.config.KVStoreType, |
| 34 | Host: core.config.KVStoreHost, |
| 35 | Port: core.config.KVStorePort, |
| 36 | Timeout: core.config.KVStoreTimeout, |
| 37 | PathPrefix: "service/voltha"} |
| 38 | core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend) |
| 39 | core.genericMgr = newModelProxyManager(core.clusterDataProxy) |
| 40 | |
| 41 | return &core, nil |
| 42 | } |
| 43 | func TestNewDeviceManager(t *testing.T) { |
| 44 | |
| 45 | core, _ := MakeTestDevManagerConfig() |
| 46 | |
| 47 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 48 | assert.NotNil(t, core.deviceMgr) |
| 49 | } |
| 50 | |
| 51 | func TestListDeviceIds(t *testing.T) { |
| 52 | |
| 53 | core, _ := MakeTestDevManagerConfig() |
| 54 | |
| 55 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 56 | assert.NotNil(t, core.deviceMgr) |
| 57 | |
| 58 | myIds, _ := core.deviceMgr.ListDeviceIds() |
| 59 | assert.NotNil(t, myIds) |
| 60 | } |
| 61 | |
| 62 | func TestGetDeviceAgent(t *testing.T) { |
| 63 | |
| 64 | // Tests also methods addDeviceAgentToMap, |
| 65 | // listDeviceIdsFromMap |
| 66 | // deleteDeviceAgentToMap |
| 67 | |
| 68 | core, _ := MakeTestDevManagerConfig() |
| 69 | |
| 70 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 71 | assert.NotNil(t, core.deviceMgr) |
| 72 | |
| 73 | devAgent := newDeviceAgent(&voltha.Device{Id: "new_device"}, core.deviceMgr, core.clusterDataProxy) |
| 74 | assert.NotNil(t, devAgent) |
| 75 | |
| 76 | // addDeviceAgentToMap |
| 77 | core.deviceMgr.addDeviceAgentToMap(devAgent) |
| 78 | |
| 79 | // listDeviceIdsFromMap |
| 80 | myIDs := core.deviceMgr.listDeviceIdsFromMap() |
| 81 | assert.NotNil(t, myIDs) |
| 82 | |
| 83 | // getDeviceAgent |
| 84 | myDevAgent := core.deviceMgr.getDeviceAgent(devAgent.deviceId) |
| 85 | assert.NotNil(t, myDevAgent) |
| 86 | |
| 87 | // deleteDeviceAgentToMap |
| 88 | core.deviceMgr.deleteDeviceAgentToMap(devAgent) |
| 89 | } |
| 90 | |
| 91 | func TestLoadDevice(t *testing.T) { |
| 92 | |
| 93 | core, _ := MakeTestDevManagerConfig() |
| 94 | |
| 95 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 96 | assert.NotNil(t, core.deviceMgr) |
| 97 | |
| 98 | devAgent := newDeviceAgent(&voltha.Device{Id: "new_device"}, core.deviceMgr, core.clusterDataProxy) |
| 99 | assert.NotNil(t, devAgent) |
| 100 | |
| 101 | // addDeviceAgentToMap |
| 102 | core.deviceMgr.addDeviceAgentToMap(devAgent) |
| 103 | |
| 104 | myDev, err := core.deviceMgr.loadDevice("new_device") |
| 105 | assert.NotNil(t, myDev) |
| 106 | assert.Nil(t, err) |
| 107 | } |
| 108 | |
| 109 | func TestIsDeviceInCache(t *testing.T) { |
| 110 | |
| 111 | // Tests also methods addDeviceAgentToMap, |
| 112 | // listDeviceIdsFromMap |
| 113 | // deleteDeviceAgentToMap |
| 114 | |
| 115 | core, _ := MakeTestDevManagerConfig() |
| 116 | |
| 117 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 118 | assert.NotNil(t, core.deviceMgr) |
| 119 | |
| 120 | devAgent := newDeviceAgent(&voltha.Device{Id: "new_device"}, core.deviceMgr, core.clusterDataProxy) |
| 121 | assert.NotNil(t, devAgent) |
| 122 | |
| 123 | // addDeviceAgentToMap |
| 124 | core.deviceMgr.addDeviceAgentToMap(devAgent) |
| 125 | |
| 126 | isInCache := core.deviceMgr.IsDeviceInCache(devAgent.deviceId) |
| 127 | assert.True(t, isInCache) |
| 128 | |
| 129 | // deleteDeviceAgentToMap |
| 130 | core.deviceMgr.deleteDeviceAgentToMap(devAgent) |
| 131 | |
| 132 | isInCacheDel := core.deviceMgr.IsDeviceInCache(devAgent.deviceId) |
| 133 | assert.False(t, isInCacheDel) |
| 134 | } |
| 135 | |
| 136 | func TestLoadRootDeviceParentAndChildren(t *testing.T) { |
| 137 | |
| 138 | core, _ := MakeTestDevManagerConfig() |
| 139 | |
| 140 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 141 | assert.NotNil(t, core.deviceMgr) |
| 142 | |
| 143 | devAgent := newDeviceAgent(&voltha.Device{Id: "new_device"}, core.deviceMgr, core.clusterDataProxy) |
| 144 | assert.NotNil(t, devAgent) |
| 145 | |
| 146 | // addDeviceAgentToMap |
| 147 | core.deviceMgr.addDeviceAgentToMap(devAgent) |
| 148 | |
| 149 | err := core.deviceMgr.loadRootDeviceParentAndChildren(devAgent.lastData) |
| 150 | assert.Nil(t, err) |
| 151 | } |
| 152 | |
| 153 | func TestGetParentDevice(t *testing.T) { |
| 154 | |
| 155 | core, _ := MakeTestDevManagerConfig() |
| 156 | |
| 157 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 158 | assert.NotNil(t, core.deviceMgr) |
| 159 | |
| 160 | devAgent := newDeviceAgent(&voltha.Device{Id: "new_device"}, core.deviceMgr, core.clusterDataProxy) |
| 161 | assert.NotNil(t, devAgent) |
| 162 | |
| 163 | // addDeviceAgentToMap |
| 164 | core.deviceMgr.addDeviceAgentToMap(devAgent) |
| 165 | |
| 166 | myDev := core.deviceMgr.getParentDevice(devAgent.lastData) |
| 167 | assert.Nil(t, myDev) |
| 168 | } |
| 169 | |
| 170 | func TestGetAllChildDeviceIds(t *testing.T) { |
| 171 | |
| 172 | core, _ := MakeTestDevManagerConfig() |
| 173 | |
| 174 | core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId) |
| 175 | assert.NotNil(t, core.deviceMgr) |
| 176 | |
| 177 | devAgent := newDeviceAgent(&voltha.Device{Id: "new_device"}, core.deviceMgr, core.clusterDataProxy) |
| 178 | assert.NotNil(t, devAgent) |
| 179 | |
| 180 | // addDeviceAgentToMap |
| 181 | core.deviceMgr.addDeviceAgentToMap(devAgent) |
| 182 | |
| 183 | myIds, err := core.deviceMgr.getAllChildDeviceIds(devAgent.lastData) |
| 184 | assert.NotNil(t, myIds) |
| 185 | assert.Nil(t, err) |
| 186 | } |