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 | |
| 44 | ELINE_VLANIDS = "100" |
| 45 | ELINE_CONNECT_POINT_1_ID = "domain:10.90.1.30-cord-onos/1" |
| 46 | ELINE_CONNECT_POINT_2_ID = "domain:10.90.1.50-cord-onos/1" |
| 47 | ELINE_NAME = "testeline" |
| 48 | |
| 49 | UNI_TENANT = "onf" |
| 50 | UNI_NAME = "onf" |
| 51 | UNI_LATLNG = [37.773972, -122.431297] |
| 52 | UNI_CPE_ID = "netconf:192.168.56.20:830/0" |
| 53 | |
| 54 | |
| 55 | class TestSyncvNaaSEline(unittest.TestCase): |
| 56 | def setUp(self): |
| 57 | global SyncvNaaSEline, MockObjectList |
| 58 | |
| 59 | self.sys_path_save = sys.path |
| 60 | sys.path.append(xos_dir) |
| 61 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 62 | |
| 63 | config = os.path.join(test_path, "test_config.yaml") |
| 64 | from xosconfig import Config |
| 65 | Config.clear() |
| 66 | Config.init(config, 'synchronizer-config-schema.yaml') |
| 67 | |
| 68 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 69 | build_mock_modelaccessor(xos_dir, services_dir, ["vnaas/xos/vnaas.xproto"]) |
| 70 | |
| 71 | import synchronizers.new_base.modelaccessor |
| 72 | import synchronizers.new_base.model_policies.model_policy_tenantwithcontainer |
| 73 | import sync_vnaaseline |
| 74 | from sync_vnaaseline import SyncvNaaSEline, model_accessor |
| 75 | |
| 76 | from mock_modelaccessor import MockObjectList |
| 77 | |
| 78 | # import all class names to globals |
| 79 | for (k, v) in model_accessor.all_model_classes.items(): |
| 80 | globals()[k] = v |
| 81 | |
| 82 | # Some of the functions we call have side-effects. For example, creating a VSGServiceInstance may lead to creation of |
| 83 | # tags. Ideally, this wouldn't happen, but it does. So make sure we reset the world. |
| 84 | model_accessor.reset_all_object_stores() |
| 85 | |
| 86 | self.syncstep = SyncvNaaSEline() |
| 87 | |
| 88 | self.onosModel = OnosModel(name=ONOS_NAME, |
| 89 | onos_ip=ONOS_IP, |
| 90 | onos_port=ONOS_PORT, |
| 91 | onos_username=ONOS_USERNAME, |
| 92 | onos_password=ONOS_PASSWORD, |
| 93 | onos_type=ONOS_TYPE) |
| 94 | self.bandwidthProfile = BandwidthProfile(cbs=BWP_GOLD_CBS, |
| 95 | ebs=BWP_GOLD_EBS, |
| 96 | cir=BWP_GOLD_CIR, |
| 97 | eir=BWP_GOLD_EIR, |
| 98 | name=BWP_GOLD_NAME) |
| 99 | self.userNetworkInterface = UserNetworkInterface(tenant=UNI_TENANT, |
| 100 | name=UNI_NAME, |
| 101 | latlng=UNI_LATLNG, |
| 102 | cpe_id=UNI_CPE_ID) |
| 103 | |
| 104 | self.eline = ELine(name=ELINE_NAME, |
| 105 | connect_point_1_id=ELINE_CONNECT_POINT_1_ID, |
| 106 | connect_point_2_id=ELINE_CONNECT_POINT_2_ID, |
| 107 | vlanids=ELINE_VLANIDS, |
| 108 | cord_site_name=ONOS_NAME, |
| 109 | bwp=BWP_GOLD_NAME) |
| 110 | |
| 111 | def tearDown(self): |
| 112 | sys.path = self.sys_path_save |
| 113 | |
| 114 | def test_sync_record(self): |
| 115 | with patch.object(BandwidthProfile.objects, "get_items") as bwp_objects, \ |
| 116 | patch.object(OnosModel.objects, "get_items") as onos_objects, \ |
| 117 | patch("requests.post") as requests_post: |
| 118 | |
| 119 | bwp_objects.return_value = [self.bandwidthProfile] |
| 120 | onos_objects.return_value = [self.onosModel] |
| 121 | |
| 122 | requests_post.return_value = Mock(status_code=200) |
| 123 | |
| 124 | self.syncstep.sync_record(self.eline) |
| 125 | |
| 126 | requests_post.assert_called() |
| 127 | |
| 128 | attrs = requests_post.call_args[1]["data"] |
| 129 | attrs = eval(attrs) # convert POST string back into a dict |
| 130 | |
| 131 | desired_attrs = {"evcCfgId": ELINE_NAME, |
| 132 | "eir": BWP_GOLD_EIR, |
| 133 | "cir": BWP_GOLD_CIR, |
| 134 | "uniList": [ELINE_CONNECT_POINT_1_ID, ELINE_CONNECT_POINT_2_ID], |
| 135 | "ebs": BWP_GOLD_EBS, |
| 136 | "vlanId": int(ELINE_VLANIDS), |
| 137 | "cbs": BWP_GOLD_CBS, |
| 138 | "evcId": ELINE_NAME, |
| 139 | "evcType": "POINT_TO_POINT"} |
| 140 | |
| 141 | self.assertDictContainsSubset(desired_attrs, attrs) |
| 142 | |
| 143 | def test_delete_record(self): |
| 144 | with patch.object(BandwidthProfile.objects, "get_items") as bwp_objects, \ |
| 145 | patch.object(OnosModel.objects, "get_items") as onos_objects, \ |
| 146 | patch("requests.delete") as requests_delete: |
| 147 | |
| 148 | bwp_objects.return_value = [self.bandwidthProfile] |
| 149 | onos_objects.return_value = [self.onosModel] |
| 150 | |
| 151 | requests_delete.return_value = Mock(status_code=200) |
| 152 | |
| 153 | self.syncstep.delete_record(self.eline) |
| 154 | |
| 155 | requests_delete.assert_called() |
| 156 | |
| 157 | url = requests_delete.call_args[0][0] |
| 158 | self.assertEqual(url, "http://%s:%d/carrierethernet/evc/testeline" % (ONOS_IP, ONOS_PORT)) |
| 159 | |
| 160 | def test_get_onos_global_addr(self): |
| 161 | addr = self.syncstep.get_onos_global_addr(self.onosModel) |
| 162 | self.assertEqual(addr, 'http://%s:%d/carrierethernet/evc' % (ONOS_IP, ONOS_PORT)) |
| 163 | |
| 164 | def test_get_onos_global_auth(self): |
| 165 | auth = self.syncstep.get_onos_global_auth(self.onosModel) |
| 166 | self.assertEqual(auth.username, ONOS_USERNAME) |
| 167 | self.assertEqual(auth.password, ONOS_PASSWORD) |
| 168 | |
| 169 | if __name__ == '__main__': |
| 170 | unittest.main() |
| 171 | |
| 172 | |