Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-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 | |
| 16 | |
| 17 | import unittest |
| 18 | from mock import patch, call, Mock, MagicMock, PropertyMock |
| 19 | import mock |
| 20 | |
| 21 | import os, sys |
| 22 | |
| 23 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 24 | service_dir=os.path.join(test_path, "../../../..") |
| 25 | xos_dir=os.path.join(test_path, "../../..") |
| 26 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 27 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 28 | services_dir=os.path.join(xos_dir, "../../xos_services") |
| 29 | |
| 30 | # chosen to match https://github.com/opencord/ecord/blob/master/examples/vnaasglobal-service-reference.yaml |
| 31 | ONOS_NAME = "onfGlobalONOS" |
| 32 | ONOS_IP = "onos-cord" |
| 33 | ONOS_PORT = 8182 |
| 34 | ONOS_USERNAME = "onos" |
| 35 | ONOS_PASSWORD = "rocks" |
| 36 | ONOS_TYPE = "global" |
| 37 | |
| 38 | BWP_GOLD_CBS = 2000 |
| 39 | BWP_GOLD_EBS = 2700 |
| 40 | BWP_GOLD_CIR = 20000 |
| 41 | BWP_GOLD_EIR = 5000 |
| 42 | BWP_GOLD_NAME = "gold" |
| 43 | |
Scott Baker | d76123f | 2018-01-23 14:26:54 -0800 | [diff] [blame^] | 44 | CONNECT_POINT_1_TENANT = "onf" |
| 45 | CONNECT_POINT_1_NAME = "uni1" |
| 46 | CONNECT_POINT_1_LATLNG = "[37.973535, -122.531087]" |
| 47 | CONNECT_POINT_1_CPE_ID = "domain:10.90.1.30-cord-onos/1" |
| 48 | |
| 49 | CONNECT_POINT_2_TENANT = "onf" |
| 50 | CONNECT_POINT_2_NAME = "uni2" |
| 51 | CONNECT_POINT_2_LATLNG = "[37.773972, -122.431297]" |
| 52 | CONNECT_POINT_2_CPE_ID = "domain:10.90.1.30-cord-onos/1" |
| 53 | |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 54 | ELINE_VLANIDS = "100" |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 55 | ELINE_NAME = "testeline" |
| 56 | |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 57 | class TestSyncvNaaSEline(unittest.TestCase): |
| 58 | def setUp(self): |
| 59 | global SyncvNaaSEline, MockObjectList |
| 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 | config = os.path.join(test_path, "test_config.yaml") |
| 66 | from xosconfig import Config |
| 67 | Config.clear() |
| 68 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 69 | |
| 70 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 71 | build_mock_modelaccessor(xos_dir, services_dir, ["vnaas/xos/vnaas.xproto"]) |
| 72 | |
| 73 | import synchronizers.new_base.modelaccessor |
| 74 | import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer |
| 75 | import sync_vnaaseline |
| 76 | from sync_vnaaseline import SyncvNaaSEline, model_accessor |
| 77 | |
| 78 | from mock_modelaccessor import MockObjectList |
| 79 | |
| 80 | # import all class names to globals |
| 81 | for (k, v) in model_accessor.all_model_classes.items(): |
| 82 | globals()[k] = v |
| 83 | |
| 84 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 85 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 86 | model_accessor.reset_all_object_stores() |
| 87 | |
| 88 | self.syncstep = SyncvNaaSEline() |
| 89 | |
| 90 | self.onosModel = OnosModel(name=ONOS_NAME, |
| 91 | onos_ip=ONOS_IP, |
| 92 | onos_port=ONOS_PORT, |
| 93 | onos_username=ONOS_USERNAME, |
| 94 | onos_password=ONOS_PASSWORD, |
| 95 | onos_type=ONOS_TYPE) |
| 96 | self.bandwidthProfile = BandwidthProfile(cbs=BWP_GOLD_CBS, |
| 97 | ebs=BWP_GOLD_EBS, |
| 98 | cir=BWP_GOLD_CIR, |
| 99 | eir=BWP_GOLD_EIR, |
| 100 | name=BWP_GOLD_NAME) |
Scott Baker | d76123f | 2018-01-23 14:26:54 -0800 | [diff] [blame^] | 101 | self.connect_point_1 = UserNetworkInterface(tenant=CONNECT_POINT_1_TENANT, |
| 102 | name=CONNECT_POINT_1_NAME, |
| 103 | latlng=CONNECT_POINT_1_LATLNG, |
| 104 | cpe_id=CONNECT_POINT_1_CPE_ID) |
| 105 | self.connect_point_2 = UserNetworkInterface(tenant=CONNECT_POINT_2_TENANT, |
| 106 | name=CONNECT_POINT_2_NAME, |
| 107 | latlng=CONNECT_POINT_2_LATLNG, |
| 108 | cpe_id=CONNECT_POINT_2_CPE_ID) |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 109 | |
| 110 | self.eline = ELine(name=ELINE_NAME, |
Scott Baker | d76123f | 2018-01-23 14:26:54 -0800 | [diff] [blame^] | 111 | connect_point_1=self.connect_point_1, |
| 112 | connect_point_2=self.connect_point_2, |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 113 | vlanids=ELINE_VLANIDS, |
| 114 | cord_site_name=ONOS_NAME, |
Scott Baker | d76123f | 2018-01-23 14:26:54 -0800 | [diff] [blame^] | 115 | bwp=self.bandwidthProfile) |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 116 | |
| 117 | def tearDown(self): |
| 118 | sys.path = self.sys_path_save |
| 119 | |
| 120 | def test_sync_record(self): |
| 121 | with patch.object(BandwidthProfile.objects, "get_items") as bwp_objects, \ |
| 122 | patch.object(OnosModel.objects, "get_items") as onos_objects, \ |
| 123 | patch("requests.post") as requests_post: |
| 124 | |
| 125 | bwp_objects.return_value = [self.bandwidthProfile] |
| 126 | onos_objects.return_value = [self.onosModel] |
| 127 | |
| 128 | requests_post.return_value = Mock(status_code=200) |
| 129 | |
| 130 | self.syncstep.sync_record(self.eline) |
| 131 | |
| 132 | requests_post.assert_called() |
| 133 | |
| 134 | attrs = requests_post.call_args[1]["data"] |
| 135 | attrs = eval(attrs) # convert POST string back into a dict |
| 136 | |
| 137 | desired_attrs = {"evcCfgId": ELINE_NAME, |
| 138 | "eir": BWP_GOLD_EIR, |
| 139 | "cir": BWP_GOLD_CIR, |
Scott Baker | d76123f | 2018-01-23 14:26:54 -0800 | [diff] [blame^] | 140 | "uniList": [CONNECT_POINT_1_CPE_ID, CONNECT_POINT_2_CPE_ID], |
Scott Baker | b146a85 | 2017-11-29 16:36:05 -0800 | [diff] [blame] | 141 | "ebs": BWP_GOLD_EBS, |
| 142 | "vlanId": int(ELINE_VLANIDS), |
| 143 | "cbs": BWP_GOLD_CBS, |
| 144 | "evcId": ELINE_NAME, |
| 145 | "evcType": "POINT_TO_POINT"} |
| 146 | |
| 147 | self.assertDictContainsSubset(desired_attrs, attrs) |
| 148 | |
| 149 | def test_delete_record(self): |
| 150 | with patch.object(BandwidthProfile.objects, "get_items") as bwp_objects, \ |
| 151 | patch.object(OnosModel.objects, "get_items") as onos_objects, \ |
| 152 | patch("requests.delete") as requests_delete: |
| 153 | |
| 154 | bwp_objects.return_value = [self.bandwidthProfile] |
| 155 | onos_objects.return_value = [self.onosModel] |
| 156 | |
| 157 | requests_delete.return_value = Mock(status_code=200) |
| 158 | |
| 159 | self.syncstep.delete_record(self.eline) |
| 160 | |
| 161 | requests_delete.assert_called() |
| 162 | |
| 163 | url = requests_delete.call_args[0][0] |
| 164 | self.assertEqual(url, "http://%s:%d/carrierethernet/evc/testeline" % (ONOS_IP, ONOS_PORT)) |
| 165 | |
| 166 | def test_get_onos_global_addr(self): |
| 167 | addr = self.syncstep.get_onos_global_addr(self.onosModel) |
| 168 | self.assertEqual(addr, 'http://%s:%d/carrierethernet/evc' % (ONOS_IP, ONOS_PORT)) |
| 169 | |
| 170 | def test_get_onos_global_auth(self): |
| 171 | auth = self.syncstep.get_onos_global_auth(self.onosModel) |
| 172 | self.assertEqual(auth.username, ONOS_USERNAME) |
| 173 | self.assertEqual(auth.password, ONOS_PASSWORD) |
| 174 | |
| 175 | if __name__ == '__main__': |
| 176 | unittest.main() |
| 177 | |
| 178 | |