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