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 | |
| 46 | class TestSyncOLTDevice(unittest.TestCase): |
| 47 | |
| 48 | def setUp(self): |
| 49 | global DeferredException |
| 50 | |
| 51 | self.sys_path_save = sys.path |
| 52 | sys.path.append(xos_dir) |
| 53 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 54 | |
| 55 | # Setting up the config module |
| 56 | from xosconfig import Config |
| 57 | config = os.path.join(test_path, "../model_policies/test_config.yaml") |
| 58 | Config.clear() |
| 59 | Config.init(config, "synchronizer-config-schema.yaml") |
| 60 | # END Setting up the config module |
| 61 | |
| 62 | from synchronizers.new_base.syncstep import DeferredException |
| 63 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 64 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("olt-service", "volt.xproto")]) |
| 65 | import synchronizers.new_base.modelaccessor |
| 66 | from sync_volt_service_instance import SyncVOLTServiceInstance, model_accessor |
| 67 | |
| 68 | # import all class names to globals |
| 69 | for (k, v) in model_accessor.all_model_classes.items(): |
| 70 | globals()[k] = v |
| 71 | |
| 72 | self.sync_step = SyncVOLTServiceInstance |
| 73 | |
| 74 | # create a mock service instance |
| 75 | o = Mock() |
| 76 | o.id = 1 |
| 77 | o.owner_id = "volt_service" |
| 78 | o.tologdict.return_value = {} |
| 79 | |
| 80 | volt_service = Mock() |
| 81 | volt_service.p_onos_url = "p_onos_url" |
| 82 | volt_service.p_onos_user = "p_onos_user" |
| 83 | volt_service.p_onos_pass = "p_onos_pass" |
| 84 | |
| 85 | si = Mock() |
| 86 | si.get_westbound_service_instance_properties = mock_get_westbound_service_instance_properties |
| 87 | |
| 88 | olt_device = Mock() |
| 89 | olt_device.name = "Test OLT Device" |
| 90 | |
| 91 | self.o = o |
| 92 | self.si = si |
| 93 | self.olt_device = olt_device |
| 94 | self.volt_service = volt_service |
| 95 | |
| 96 | def tearDown(self): |
| 97 | self.o = None |
| 98 | sys.path = self.sys_path_save |
| 99 | |
| 100 | |
| 101 | |
| 102 | @requests_mock.Mocker() |
| 103 | def test_do_not_sync(self, m): |
| 104 | |
| 105 | self.olt_device.dp_id = None |
| 106 | |
| 107 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
| 108 | patch.object(OLTDevice.objects, "get") as olt_device_mock, \ |
| 109 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 110 | service_instance_mock.return_value = self.si |
| 111 | olt_device_mock.return_value = self.olt_device |
| 112 | olt_service_mock.return_value = self.volt_service |
| 113 | |
| 114 | with self.assertRaises(DeferredException) as e: |
| 115 | self.sync_step().sync_record(self.o) |
| 116 | |
| 117 | self.assertFalse(m.called) |
| 118 | self.assertEqual(e.exception.message, "Waiting for OLTDevice Test OLT Device to be synchronized") |
| 119 | |
| 120 | @requests_mock.Mocker() |
| 121 | def test_do_sync(self, m): |
| 122 | |
| 123 | m.post("http://p_onos_url/onos/olt/oltapp/of:dp_id/uni_port_id/c_tag", status_code=200, json={}) |
| 124 | |
| 125 | self.olt_device.dp_id = "of:dp_id" |
| 126 | |
| 127 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
| 128 | patch.object(OLTDevice.objects, "get") as olt_device_mock, \ |
| 129 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 130 | service_instance_mock.return_value = self.si |
| 131 | olt_device_mock.return_value = self.olt_device |
| 132 | olt_service_mock.return_value = self.volt_service |
| 133 | |
| 134 | self.sync_step().sync_record(self.o) |
| 135 | self.assertTrue(m.called) |
| 136 | |
| 137 | @requests_mock.Mocker() |
| 138 | def test_do_sync_fail(self, m): |
| 139 | m.post("http://p_onos_url/onos/olt/oltapp/of:dp_id/uni_port_id/c_tag", status_code=500, text="Mock Error") |
| 140 | |
| 141 | self.olt_device.dp_id = "of:dp_id" |
| 142 | |
| 143 | with patch.object(ServiceInstance.objects, "get") as service_instance_mock, \ |
| 144 | patch.object(OLTDevice.objects, "get") as olt_device_mock, \ |
| 145 | patch.object(VOLTService.objects, "get") as olt_service_mock: |
| 146 | service_instance_mock.return_value = self.si |
| 147 | olt_device_mock.return_value = self.olt_device |
| 148 | olt_service_mock.return_value = self.volt_service |
| 149 | |
| 150 | with self.assertRaises(Exception) as e: |
| 151 | self.sync_step().sync_record(self.o) |
| 152 | |
| 153 | self.assertTrue(m.called) |
| 154 | self.assertEqual(e.exception.message, "Failed to add subscriber in P_ONOS: Mock Error") |
| 155 | |
| 156 | |
| 157 | if __name__ == "__main__": |
| 158 | unittest.main() |