Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 1 | # 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 | |
| 15 | import unittest |
| 16 | from mock import patch, call, Mock, PropertyMock |
| 17 | import requests_mock |
| 18 | |
| 19 | import os, sys |
| 20 | |
| 21 | # Hack to load synchronizer framework |
| 22 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 23 | xos_dir=os.path.join(test_path, "../../..") |
| 24 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 25 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 26 | services_dir = os.path.join(xos_dir, "../../xos_services") |
| 27 | sys.path.append(xos_dir) |
| 28 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 29 | # END Hack to load synchronizer framework |
| 30 | |
| 31 | # generate model from xproto |
| 32 | def get_models_fn(service_name, xproto_name): |
| 33 | name = os.path.join(service_name, "xos", xproto_name) |
| 34 | if os.path.exists(os.path.join(services_dir, name)): |
| 35 | return name |
| 36 | else: |
| 37 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 38 | if os.path.exists(os.path.join(services_dir, name)): |
| 39 | return name |
| 40 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 41 | # END generate model from xproto |
| 42 | |
| 43 | def mock_get_westbound_service_instance_properties(prop): |
| 44 | return prop |
| 45 | |
Matteo Scandolo | d44ca99 | 2018-05-17 15:02:10 -0700 | [diff] [blame] | 46 | class TestSyncVOLTServiceInstance(unittest.TestCase): |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 47 | def setUp(self): |
| 48 | global DeferredException |
| 49 | |
| 50 | self.sys_path_save = sys.path |
| 51 | sys.path.append(xos_dir) |
| 52 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 53 | |
| 54 | # Setting up the config module |
| 55 | from xosconfig import Config |
| 56 | config = os.path.join(test_path, "../model_policies/test_config.yaml") |
| 57 | Config.clear() |
| 58 | Config.init(config, "synchronizer-config-schema.yaml") |
| 59 | # END Setting up the config module |
| 60 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 61 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
Matteo Scandolo | 19466a0 | 2018-05-16 17:43:39 -0700 | [diff] [blame] | 62 | # build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 63 | |
| 64 | # FIXME this is to get jenkins to pass the tests, somehow it is running tests in a different order |
| 65 | # and apparently it is not overriding the generated model accessor |
| 66 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto"), |
| 67 | get_models_fn("vsg", "vsg.xproto"), |
| 68 | get_models_fn("../profiles/rcord", "rcord.xproto")]) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 69 | import synchronizers.new_base.modelaccessor |
Scott Baker | 8699e7b | 2018-07-05 13:11:14 -0700 | [diff] [blame^] | 70 | from synchronizers.new_base.syncstep import DeferredException |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 71 | from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor |
| 72 | |
| 73 | # import all class names to globals |
| 74 | for (k, v) in model_accessor.all_model_classes.items(): |
| 75 | globals()[k] = v |
| 76 | |
| 77 | self.sync_step = SyncVOLTServiceInstance |
| 78 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 79 | volt_service = Mock() |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 80 | volt_service.onos_voltha_url = "onos_voltha_url" |
| 81 | volt_service.onos_voltha_port = 4321 |
| 82 | volt_service.onos_voltha_user = "onos_voltha_user" |
| 83 | volt_service.onos_voltha_pass = "onos_voltha_pass" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 84 | |
| 85 | si = Mock() |
| 86 | si.get_westbound_service_instance_properties = mock_get_westbound_service_instance_properties |
| 87 | |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 88 | uni_port = Mock() |
| 89 | uni_port.port_no = "uni_port_id" |
| 90 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 91 | onu_device = Mock() |
| 92 | onu_device.name = "BRCM1234" |
| 93 | onu_device.pon_port.olt_device.dp_id = None |
| 94 | onu_device.pon_port.olt_device.name = "Test OLT Device" |
Matteo Scandolo | d8ed60e | 2018-06-18 17:00:57 -0700 | [diff] [blame] | 95 | onu_device.uni_ports.first.return_value = uni_port |
| 96 | |
| 97 | # create a mock service instance |
| 98 | o = Mock() |
| 99 | o.id = 1 |
| 100 | o.owner_id = "volt_service" |
| 101 | o.onu_device = onu_device |
| 102 | o.tologdict.return_value = {} |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 103 | |
| 104 | self.o = o |
| 105 | self.si = si |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 106 | self.onu_device = onu_device |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 107 | self.volt_service = volt_service |
| 108 | |
| 109 | def tearDown(self): |
| 110 | self.o = None |
| 111 | sys.path = self.sys_path_save |
| 112 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 113 | @requests_mock.Mocker() |
| 114 | def test_do_not_sync(self, m): |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 115 | self.onu_device.pon_port.olt_device.dp_id = None |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 116 | |
| 117 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 118 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 119 | service_instance_mock.return_value = self.si |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 120 | olt_service_mock.return_value = self.volt_service |
| 121 | |
| 122 | with self.assertRaises(DeferredException) as e: |
| 123 | self.sync_step().sync_record(self.o) |
| 124 | |
| 125 | self.assertFalse(m.called) |
| 126 | self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized") |
| 127 | |
| 128 | @requests_mock.Mocker() |
| 129 | def test_do_sync(self, m): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 130 | m.post("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id/c_tag", status_code=200, json={}) |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 131 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 132 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 133 | |
| 134 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 135 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 136 | service_instance_mock.return_value = self.si |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 137 | olt_service_mock.return_value = self.volt_service |
| 138 | |
| 139 | self.sync_step().sync_record(self.o) |
| 140 | self.assertTrue(m.called) |
| 141 | |
| 142 | @requests_mock.Mocker() |
| 143 | def test_do_sync_fail(self, m): |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 144 | m.post("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id/c_tag", status_code=500, text="Mock Error") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 145 | |
Matteo Scandolo | e2cb8a4 | 2018-05-18 16:30:06 -0700 | [diff] [blame] | 146 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 147 | |
| 148 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 149 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 150 | service_instance_mock.return_value = self.si |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 151 | olt_service_mock.return_value = self.volt_service |
| 152 | |
| 153 | with self.assertRaises(Exception) as e: |
| 154 | self.sync_step().sync_record(self.o) |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 155 | self.assertTrue(m.called) |
| 156 | self.assertEqual(e.exception.message, "Failed to add subscriber in onos voltha: Mock Error") |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 157 | |
Matteo Scandolo | e48a464 | 2018-06-26 10:08:34 -0700 | [diff] [blame] | 158 | @requests_mock.Mocker() |
| 159 | def test_delete(self, m): |
| 160 | m.delete("http://onos_voltha_url:4321/onos/olt/oltapp/of:dp_id/uni_port_id", status_code=204) |
| 161 | |
| 162 | self.onu_device.pon_port.olt_device.dp_id = "of:dp_id" |
| 163 | |
| 164 | with patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 165 | olt_service_mock.return_value = self.volt_service |
| 166 | |
| 167 | self.sync_step().delete_record(self.o) |
| 168 | self.assertTrue(m.called) |
| 169 | self.assertEqual(m.call_count, 1) |
| 170 | |
Matteo Scandolo | f6337eb | 2018-04-05 15:58:37 -0700 | [diff] [blame] | 171 | if __name__ == "__main__": |
Luca Prete | ca974c8 | 2018-05-01 18:06:16 -0700 | [diff] [blame] | 172 | unittest.main() |