blob: 332c81f4c480a3b1d74e9ed525d3b78c37420632 [file] [log] [blame]
Matteo Scandolo33523412018-04-12 15:21:13 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import unittest
16from mock import patch, call, Mock, PropertyMock
17import requests_mock
18
19import os, sys
20
Matteo Scandolo33523412018-04-12 15:21:13 -070021test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
Matteo Scandolo33523412018-04-12 15:21:13 -070022
23class TestSyncOLTDevice(unittest.TestCase):
24
25 def setUp(self):
26 global DeferredException
27
28 self.sys_path_save = sys.path
Matteo Scandolo33523412018-04-12 15:21:13 -070029
30 # Setting up the config module
31 from xosconfig import Config
Matteo Scandolof7ebb112018-09-18 16:17:22 -070032 config = os.path.join(test_path, "../test_config.yaml")
Matteo Scandolo33523412018-04-12 15:21:13 -070033 Config.clear()
34 Config.init(config, "synchronizer-config-schema.yaml")
35 # END Setting up the config module
36
Scott Baker47b47302019-01-30 16:55:07 -080037 from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
38 mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
39 ("vsg", "vsg.xproto"),
Matteo Scandoloe04a4882019-02-14 10:22:24 -080040 ("rcord", "rcord.xproto"),])
Matteo Scandolo19466a02018-05-16 17:43:39 -070041
Scott Baker47b47302019-01-30 16:55:07 -080042 import xossynchronizer.modelaccessor
43 reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
44
45 from xossynchronizer.modelaccessor import model_accessor
46 self.model_accessor = model_accessor
47
48 from pull_olts import OLTDevicePullStep
Matteo Scandolo33523412018-04-12 15:21:13 -070049
50 # import all class names to globals
51 for (k, v) in model_accessor.all_model_classes.items():
52 globals()[k] = v
53
54 self.sync_step = OLTDevicePullStep
55
56 # mock volt service
57 self.volt_service = Mock()
58 self.volt_service.id = "volt_service_id"
59 self.volt_service.voltha_url = "voltha_url"
60 self.volt_service.voltha_user = "voltha_user"
61 self.volt_service.voltha_pass = "voltha_pass"
Matteo Scandolo19466a02018-05-16 17:43:39 -070062 self.volt_service.voltha_port = 1234
Matteo Scandolo33523412018-04-12 15:21:13 -070063
64 # mock voltha responses
65 self.devices = {
66 "items": [
67 {
68 "id": "test_id",
69 "type": "simulated_olt",
70 "host_and_port": "172.17.0.1:50060",
71 "admin_state": "ENABLED",
Matteo Scandolod6fce512018-10-16 10:35:29 -070072 "oper_status": "ACTIVE",
73 "serial_number": "serial_number",
Matteo Scandolo33523412018-04-12 15:21:13 -070074 }
75 ]
76 }
77
78 self.logical_devices = {
79 "items": [
80 {
81 "root_device_id": "test_id",
82 "id": "of_id",
83 "datapath_id": "55334486016"
84 }
85 ]
86 }
87
Matteo Scandolo6be6ee92018-05-24 15:07:51 -070088 self.ports = {
89 "items": [
90 {
91 "label": "PON port",
92 "port_no": 1,
93 "type": "PON_OLT",
94 "admin_state": "ENABLED",
95 "oper_status": "ACTIVE"
96 },
97 {
98 "label": "NNI facing Ethernet port",
99 "port_no": 2,
100 "type": "ETHERNET_NNI",
101 "admin_state": "ENABLED",
102 "oper_status": "ACTIVE"
103 }
104 ]
105 }
106
Matteo Scandolo33523412018-04-12 15:21:13 -0700107 def tearDown(self):
108 sys.path = self.sys_path_save
109
110 @requests_mock.Mocker()
111 def test_missing_volt_service(self, m):
112 self.assertFalse(m.called)
113
114 @requests_mock.Mocker()
115 def test_pull(self, m):
116
117 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700118 patch.object(OLTDevice, "save") as mock_olt_save, \
119 patch.object(PONPort, "save") as mock_pon_save, \
120 patch.object(NNIPort, "save") as mock_nni_save:
Matteo Scandolo33523412018-04-12 15:21:13 -0700121 olt_service_mock.return_value = [self.volt_service]
122
Matteo Scandolo19466a02018-05-16 17:43:39 -0700123 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700124 m.get("http://voltha_url:1234/api/v1/devices/test_id/ports", status_code=200, json=self.ports)
Matteo Scandolo19466a02018-05-16 17:43:39 -0700125 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices)
Matteo Scandolo33523412018-04-12 15:21:13 -0700126
Scott Baker47b47302019-01-30 16:55:07 -0800127 self.sync_step(model_accessor=self.model_accessor).pull_records()
Matteo Scandolo33523412018-04-12 15:21:13 -0700128
129 # TODO how to asster this?
130 # self.assertEqual(existing_olt.admin_state, "ENABLED")
131 # self.assertEqual(existing_olt.oper_status, "ACTIVE")
132 # self.assertEqual(existing_olt.volt_service_id, "volt_service_id")
133 # self.assertEqual(existing_olt.device_id, "test_id")
134 # self.assertEqual(existing_olt.of_id, "of_id")
135 # self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000")
136
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700137 mock_olt_save.assert_called()
138 mock_pon_save.assert_called()
139 mock_nni_save.assert_called()
Matteo Scandolo33523412018-04-12 15:21:13 -0700140
141 @requests_mock.Mocker()
142 def test_pull_existing(self, m):
143
144 existing_olt = Mock()
Scott Baker09798d82019-01-17 08:34:59 -0800145 existing_olt.admin_state = "ENABLED"
Matteo Scandolo33523412018-04-12 15:21:13 -0700146 existing_olt.enacted = 2
147 existing_olt.updated = 1
148
149 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700150 patch.object(OLTDevice.objects, "filter") as mock_get, \
151 patch.object(PONPort, "save") as mock_pon_save, \
152 patch.object(NNIPort, "save") as mock_nni_save, \
153 patch.object(existing_olt, "save") as mock_olt_save:
Matteo Scandolo33523412018-04-12 15:21:13 -0700154 olt_service_mock.return_value = [self.volt_service]
155 mock_get.return_value = [existing_olt]
156
Matteo Scandolo19466a02018-05-16 17:43:39 -0700157 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700158 m.get("http://voltha_url:1234/api/v1/devices/test_id/ports", status_code=200, json=self.ports)
Matteo Scandolo19466a02018-05-16 17:43:39 -0700159 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices)
Matteo Scandolo33523412018-04-12 15:21:13 -0700160
Scott Baker47b47302019-01-30 16:55:07 -0800161 self.sync_step(model_accessor=self.model_accessor).pull_records()
Matteo Scandolo33523412018-04-12 15:21:13 -0700162
163 self.assertEqual(existing_olt.admin_state, "ENABLED")
164 self.assertEqual(existing_olt.oper_status, "ACTIVE")
165 self.assertEqual(existing_olt.volt_service_id, "volt_service_id")
166 self.assertEqual(existing_olt.device_id, "test_id")
167 self.assertEqual(existing_olt.of_id, "of_id")
168 self.assertEqual(existing_olt.dp_id, "of:0000000ce2314000")
169
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700170 mock_olt_save.assert_called()
171 mock_pon_save.assert_called()
172 mock_nni_save.assert_called()
Matteo Scandolo33523412018-04-12 15:21:13 -0700173
174 @requests_mock.Mocker()
175 def test_pull_existing_do_not_sync(self, m):
176 existing_olt = Mock()
177 existing_olt.enacted = 1
178 existing_olt.updated = 2
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700179 existing_olt.device_id = "test_id"
Matteo Scandolo33523412018-04-12 15:21:13 -0700180
181 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700182 patch.object(OLTDevice.objects, "filter") as mock_get, \
183 patch.object(PONPort, "save") as mock_pon_save, \
184 patch.object(NNIPort, "save") as mock_nni_save, \
185 patch.object(existing_olt, "save") as mock_olt_save:
186
Matteo Scandolo33523412018-04-12 15:21:13 -0700187 olt_service_mock.return_value = [self.volt_service]
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700188 mock_get.return_value = [existing_olt]
Matteo Scandolo33523412018-04-12 15:21:13 -0700189
Matteo Scandolo19466a02018-05-16 17:43:39 -0700190 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json=self.devices)
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700191 m.get("http://voltha_url:1234/api/v1/devices/test_id/ports", status_code=200, json=self.ports)
Matteo Scandolo19466a02018-05-16 17:43:39 -0700192 m.get("http://voltha_url:1234/api/v1/logical_devices", status_code=200, json=self.logical_devices)
Matteo Scandolo33523412018-04-12 15:21:13 -0700193
Scott Baker47b47302019-01-30 16:55:07 -0800194 self.sync_step(model_accessor=self.model_accessor).pull_records()
Matteo Scandolo33523412018-04-12 15:21:13 -0700195
Matteo Scandolo6be6ee92018-05-24 15:07:51 -0700196 mock_olt_save.assert_not_called()
197 mock_pon_save.assert_called()
198 mock_nni_save.assert_called()
Matteo Scandolo33523412018-04-12 15:21:13 -0700199
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700200 @requests_mock.Mocker()
201 def test_pull_deleted_object(self, m):
202 existing_olt = Mock()
203 existing_olt.enacted = 2
204 existing_olt.updated = 1
205 existing_olt.device_id = "test_id"
206
207 m.get("http://voltha_url:1234/api/v1/devices", status_code=200, json={"items": []})
208
209 with patch.object(VOLTService.objects, "all") as olt_service_mock, \
210 patch.object(OLTDevice.objects, "get_items") as mock_get, \
211 patch.object(existing_olt, "delete") as mock_olt_delete:
212
213 olt_service_mock.return_value = [self.volt_service]
214 mock_get.return_value = [existing_olt]
215
Scott Baker47b47302019-01-30 16:55:07 -0800216 self.sync_step(model_accessor=self.model_accessor).pull_records()
Matteo Scandoloa4fb0322018-08-20 17:35:29 -0700217
218 mock_olt_delete.assert_called()
219
220
Matteo Scandolo33523412018-04-12 15:21:13 -0700221if __name__ == "__main__":
222 unittest.main()