Scott Baker | a6c687c | 2018-07-16 15:08:49 -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 | |
| 17 | import functools |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 18 | from mock import patch, call, Mock, PropertyMock, MagicMock |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 19 | import requests_mock |
| 20 | import multistructlog |
| 21 | from multistructlog import create_logger |
| 22 | |
| 23 | import os, sys |
| 24 | |
| 25 | # Hack to load synchronizer framework |
| 26 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 27 | xos_dir=os.path.join(test_path, "../../..") |
| 28 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 29 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 30 | services_dir = os.path.join(xos_dir, "../../xos_services") |
| 31 | sys.path.append(xos_dir) |
| 32 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 33 | # END Hack to load synchronizer framework |
| 34 | |
| 35 | # generate model from xproto |
| 36 | def get_models_fn(service_name, xproto_name): |
| 37 | name = os.path.join(service_name, "xos", xproto_name) |
| 38 | if os.path.exists(os.path.join(services_dir, name)): |
| 39 | return name |
| 40 | else: |
| 41 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 42 | if os.path.exists(os.path.join(services_dir, name)): |
| 43 | return name |
| 44 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 45 | # END generate model from xproto |
| 46 | |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 47 | def mock_get_westbound_service_instance_properties(props, prop): |
| 48 | return props[prop] |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 49 | |
| 50 | def match_json(desired, req): |
| 51 | if desired!=req.json(): |
| 52 | raise Exception("Got request %s, but body is not matching" % req.url) |
| 53 | return False |
| 54 | return True |
| 55 | |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 56 | class TestSyncFabricCrossconnectServiceInstance(unittest.TestCase): |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 57 | |
| 58 | def setUp(self): |
| 59 | global DeferredException |
| 60 | |
| 61 | self.sys_path_save = sys.path |
| 62 | sys.path.append(xos_dir) |
| 63 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 64 | |
| 65 | # Setting up the config module |
| 66 | from xosconfig import Config |
| 67 | config = os.path.join(test_path, "../test_fabric_crossconnect_config.yaml") |
| 68 | Config.clear() |
| 69 | Config.init(config, "synchronizer-config-schema.yaml") |
| 70 | # END Setting up the config module |
| 71 | |
| 72 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 73 | build_mock_modelaccessor(xos_dir, services_dir, [get_models_fn("fabric-crossconnect", "fabric-crossconnect.xproto")]) |
| 74 | import synchronizers.new_base.modelaccessor |
| 75 | |
| 76 | from sync_fabric_crossconnect_service_instance import SyncFabricCrossconnectServiceInstance, model_accessor, DeferredException |
| 77 | |
| 78 | # import all class names to globals |
| 79 | for (k, v) in model_accessor.all_model_classes.items(): |
| 80 | globals()[k] = v |
| 81 | |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 82 | self.sync_step = SyncFabricCrossconnectServiceInstance |
| 83 | self.sync_step.log = Mock() |
| 84 | |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 85 | # mock onos-fabric |
| 86 | self.onos_fabric = Service(name = "onos-fabric", |
| 87 | rest_hostname = "onos-fabric", |
| 88 | rest_port = "8181", |
| 89 | rest_username = "onos", |
| 90 | rest_password = "rocks") |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 91 | |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 92 | self.service = FabricCrossconnectService(name = "fcservice", |
| 93 | provider_services = [self.onos_fabric]) |
| 94 | |
| 95 | def mock_westbound(self, fsi, s_tag, switch_datapath_id, switch_port): |
| 96 | # Mock out a ServiceInstance so the syncstep can call get_westbound_service_instance_properties on it |
| 97 | si = ServiceInstance(id=fsi.id) |
| 98 | si.get_westbound_service_instance_properties = functools.partial( |
| 99 | mock_get_westbound_service_instance_properties, |
| 100 | {"s_tag": s_tag, |
| 101 | "switch_datapath_id": switch_datapath_id, |
| 102 | "switch_port": switch_port}) |
| 103 | return si |
| 104 | |
| 105 | def test_format_url(self): |
| 106 | url = self.sync_step().format_url("foo.com/bar") |
| 107 | self.assertEqual(url, "http://foo.com/bar") |
| 108 | |
| 109 | url = self.sync_step().format_url("http://foo.com/bar") |
| 110 | self.assertEqual(url, "http://foo.com/bar") |
| 111 | |
| 112 | def test_make_handle_extract_handle(self): |
| 113 | h = self.sync_step().make_handle(222, "of:0000000000000201") |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 114 | (s_tag, switch_datapath_id) = self.sync_step().extract_handle(h) |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 115 | |
| 116 | self.assertEqual(s_tag, 222) |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 117 | self.assertEqual(switch_datapath_id, "of:0000000000000201") |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 118 | |
| 119 | def test_get_fabric_onos_init(self): |
| 120 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service) |
| 121 | |
| 122 | d = self.sync_step().get_fabric_onos_info(fsi) |
| 123 | |
| 124 | self.assertEqual(d["url"], "http://onos-fabric:8181") |
| 125 | self.assertEqual(d["user"], "onos") |
| 126 | self.assertEqual(d["pass"], "rocks") |
| 127 | |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 128 | def test_range_matches_single(self): |
| 129 | self.assertTrue(self.sync_step().range_matches(123, "123")) |
| 130 | |
| 131 | def test_range_matches_single_incorrect(self): |
| 132 | self.assertFalse(self.sync_step().range_matches(123, "456")) |
| 133 | |
| 134 | def test_range_matches_range(self): |
| 135 | self.assertTrue(self.sync_step().range_matches(123, "122-124")) |
| 136 | |
| 137 | def test_range_matches_range_incorrect(self): |
| 138 | self.assertFalse(self.sync_step().range_matches(123, "110-113")) |
| 139 | |
| 140 | def test_range_matches_any(self): |
| 141 | self.assertTrue(self.sync_step().range_matches(123, "ANY")) |
| 142 | self.assertTrue(self.sync_step().range_matches(123, "any")) |
| 143 | |
| 144 | def test_find_bng_single(self): |
| 145 | with patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \ |
| 146 | patch.object(self.sync_step, "range_matches") as range_matches: |
| 147 | bngmapping = BNGPortMapping(s_tag="111", switch_port=4) |
| 148 | bng_objects.return_value = [bngmapping] |
| 149 | |
| 150 | # this should not be called |
| 151 | range_matches.return_value = False |
| 152 | |
| 153 | found_bng = self.sync_step().find_bng(111) |
| 154 | self.assertTrue(found_bng) |
| 155 | self.assertEqual(found_bng.switch_port, 4) |
| 156 | |
| 157 | range_matches.assert_not_called() |
| 158 | |
| 159 | def test_find_bng_any(self): |
| 160 | with patch.object(BNGPortMapping.objects, "get_items") as bng_objects: |
| 161 | bngmapping = BNGPortMapping(s_tag="ANY", switch_port=4) |
| 162 | bng_objects.return_value = [bngmapping] |
| 163 | |
| 164 | found_bng = self.sync_step().find_bng(111) |
| 165 | self.assertTrue(found_bng) |
| 166 | self.assertEqual(found_bng.switch_port, 4) |
| 167 | |
| 168 | def test_find_bng_range(self): |
| 169 | with patch.object(BNGPortMapping.objects, "get_items") as bng_objects: |
| 170 | bngmapping = BNGPortMapping(s_tag="100-200", switch_port=4) |
| 171 | bng_objects.return_value = [bngmapping] |
| 172 | |
| 173 | found_bng = self.sync_step().find_bng(111) |
| 174 | self.assertTrue(found_bng) |
| 175 | self.assertEqual(found_bng.switch_port, 4) |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 176 | |
| 177 | @requests_mock.Mocker() |
| 178 | def test_sync(self, m): |
| 179 | with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \ |
| 180 | patch.object(BNGPortMapping.objects, "get_items") as bng_objects, \ |
| 181 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 182 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 183 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, source_port=3, |
| 184 | switch_datapath_id="of:0000000000000201", updated=1, policed=2) |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 185 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 186 | serviceinstance_objects.return_value = [fsi] |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 187 | |
Scott Baker | d443ea7 | 2018-08-07 13:50:06 -0700 | [diff] [blame] | 188 | bngmapping = BNGPortMapping(s_tag="111", switch_port=4) |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 189 | bng_objects.return_value = [bngmapping] |
| 190 | |
| 191 | desired_data = {"deviceId": "of:0000000000000201", |
| 192 | "vlanId": 111, |
| 193 | "ports": [3, 4]} |
| 194 | |
| 195 | m.post("http://onos-fabric:8181/onos/segmentrouting/xconnect", |
| 196 | status_code=200, |
| 197 | additional_matcher=functools.partial(match_json, desired_data)) |
| 198 | |
| 199 | self.sync_step().sync_record(fsi) |
| 200 | self.assertTrue(m.called) |
| 201 | |
| 202 | self.assertEqual(fsi.backend_handle, "111/of:0000000000000201") |
| 203 | fcsi_save.assert_called() |
| 204 | |
| 205 | def test_sync_no_bng_mapping(self): |
| 206 | with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \ |
| 207 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 208 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 209 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, source_port=3, |
| 210 | switch_datapath_id="of:0000000000000201", updated=1, policed=2) |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 211 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 212 | serviceinstance_objects.return_value = [fsi] |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 213 | |
| 214 | with self.assertRaises(Exception) as e: |
| 215 | self.sync_step().sync_record(fsi) |
| 216 | |
| 217 | self.assertEqual(e.exception.message, "Unable to determine BNG port for s_tag 111") |
| 218 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 219 | def test_sync_not_policed(self): |
| 220 | with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \ |
| 221 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 222 | |
| 223 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3, |
| 224 | switch_datapath_id="of:0000000000000201", updated=1, policed=0) |
| 225 | |
| 226 | serviceinstance_objects.return_value = [fsi] |
| 227 | |
| 228 | with self.assertRaises(Exception) as e: |
| 229 | self.sync_step().sync_record(fsi) |
| 230 | |
| 231 | self.assertEqual(e.exception.message, "Waiting for model_policy to run on fcsi 7777") |
| 232 | |
| 233 | def test_sync_no_s_tag(self): |
| 234 | with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \ |
| 235 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 236 | |
| 237 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3, |
| 238 | switch_datapath_id="of:0000000000000201", updated=1, policed=2) |
| 239 | |
| 240 | serviceinstance_objects.return_value = [fsi] |
| 241 | |
| 242 | with self.assertRaises(Exception) as e: |
| 243 | self.sync_step().sync_record(fsi) |
| 244 | |
| 245 | self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if s_tag is None on fcsi 7777") |
| 246 | |
| 247 | def test_sync_no_switch_datapath_id(self): |
| 248 | with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \ |
| 249 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 250 | |
| 251 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, source_port=3, s_tag=111, |
| 252 | updated=1, policed=2) |
| 253 | |
| 254 | serviceinstance_objects.return_value = [fsi] |
| 255 | |
| 256 | with self.assertRaises(Exception) as e: |
| 257 | self.sync_step().sync_record(fsi) |
| 258 | |
| 259 | self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if switch_datapath_id is unset on fcsi 7777") |
| 260 | |
| 261 | def test_sync_no_source_port(self): |
| 262 | with patch.object(ServiceInstance.objects, "get_items") as serviceinstance_objects, \ |
| 263 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 264 | |
| 265 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, s_tag=111, |
| 266 | switch_datapath_id="of:0000000000000201", updated=1, policed=2) |
| 267 | |
| 268 | serviceinstance_objects.return_value = [fsi] |
| 269 | |
| 270 | with self.assertRaises(Exception) as e: |
| 271 | self.sync_step().sync_record(fsi) |
| 272 | |
| 273 | self.assertEqual(e.exception.message, "Cannot sync FabricCrossconnectServiceInstance if source_port is None on fcsi 7777") |
| 274 | |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 275 | @requests_mock.Mocker() |
| 276 | def test_delete(self, m): |
| 277 | with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects, \ |
| 278 | patch.object(FabricCrossconnectServiceInstance, "save") as fcsi_save: |
| 279 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, |
| 280 | backend_handle="111/of:0000000000000201", |
| 281 | enacted=True) |
| 282 | |
| 283 | fcsi_objects.return_value=[fsi] |
| 284 | |
| 285 | desired_data = {"deviceId": "of:0000000000000201", |
| 286 | "vlanId": 111} |
| 287 | |
| 288 | m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect", |
| 289 | status_code=204, |
| 290 | additional_matcher=functools.partial(match_json, desired_data)) |
| 291 | |
| 292 | self.sync_step().delete_record(fsi) |
| 293 | self.assertTrue(m.called) |
| 294 | |
| 295 | @requests_mock.Mocker() |
| 296 | def test_delete_in_use(self, m): |
| 297 | with patch.object(FabricCrossconnectServiceInstance.objects, "get_items") as fcsi_objects: |
| 298 | # The subscriber we want to delete |
| 299 | fsi = FabricCrossconnectServiceInstance(id=7777, owner=self.service, |
| 300 | backend_handle="111/of:0000000000000201", |
| 301 | enacted=True) |
| 302 | |
Scott Baker | 8256547 | 2018-08-20 11:40:03 -0700 | [diff] [blame^] | 303 | # Another subscriber using the same (s_tag, switch_datapath_id) pair |
Scott Baker | 547dea0 | 2018-07-18 15:24:26 -0700 | [diff] [blame] | 304 | fsi2 = FabricCrossconnectServiceInstance(id=7778, owner=self.service, |
| 305 | backend_handle="111/of:0000000000000201", |
| 306 | enacted=True) |
| 307 | |
| 308 | fcsi_objects.return_value=[fsi, fsi2] |
| 309 | |
| 310 | desired_data = {"deviceId": "of:0000000000000201", |
| 311 | "vlanId": 111} |
| 312 | |
| 313 | m.delete("http://onos-fabric:8181/onos/segmentrouting/xconnect", |
| 314 | status_code=204, |
| 315 | additional_matcher=functools.partial(match_json, desired_data)) |
| 316 | |
| 317 | self.sync_step().delete_record(fsi) |
| 318 | self.assertFalse(m.called) |
Scott Baker | a6c687c | 2018-07-16 15:08:49 -0700 | [diff] [blame] | 319 | |
| 320 | def tearDown(self): |
| 321 | self.o = None |
| 322 | sys.path = self.sys_path_save |
| 323 | |
| 324 | if __name__ == '__main__': |
| 325 | unittest.main() |