Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -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 json |
| 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 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 24 | xos_dir=os.path.join(test_path, "../../../../../orchestration/xos/xos") |
| 25 | services_dir=os.path.join(xos_dir, "../../xos_services") |
| 26 | # END Hack to load synchronizer framework |
| 27 | |
| 28 | |
| 29 | # generate model from xproto |
| 30 | def get_models_fn(service_name, xproto_name): |
| 31 | name = os.path.join(service_name, "xos", "synchronizer", "models", xproto_name) |
| 32 | if os.path.exists(os.path.join(services_dir, name)): |
| 33 | return name |
| 34 | raise Exception("Unable to find service=%s xproto=%s" % (service_name, xproto_name)) |
| 35 | # END generate model from xproto |
| 36 | |
| 37 | class TestAttHelpers(unittest.TestCase): |
| 38 | |
| 39 | def setUp(self): |
| 40 | |
| 41 | self.sys_path_save = sys.path |
| 42 | sys.path.append(xos_dir) |
| 43 | sys.path.append(os.path.join(xos_dir, 'synchronizers', 'new_base')) |
| 44 | |
| 45 | # Setting up the config module |
| 46 | from xosconfig import Config |
| 47 | config = os.path.join(test_path, "test_config.yaml") |
| 48 | Config.clear() |
| 49 | Config.init(config, "synchronizer-config-schema.yaml") |
| 50 | # END Setting up the config module |
| 51 | |
| 52 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 53 | |
| 54 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 55 | get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"), |
| 56 | get_models_fn("olt-service", "volt.xproto"), |
| 57 | get_models_fn("../profiles/rcord", "rcord.xproto") |
| 58 | ]) |
| 59 | import synchronizers.new_base.modelaccessor |
| 60 | from helpers import AttHelpers, model_accessor |
| 61 | |
| 62 | # import all class names to globals |
| 63 | for (k, v) in model_accessor.all_model_classes.items(): |
| 64 | globals()[k] = v |
| 65 | |
| 66 | self.helpers = AttHelpers |
| 67 | |
| 68 | self._volt = VOLTService() |
| 69 | self._volt.id = 1 |
| 70 | |
| 71 | self.volt = Service() |
| 72 | self.volt.id = 1 |
| 73 | self.volt.name = "vOLT" |
| 74 | self.volt.leaf_model = self._volt |
| 75 | |
| 76 | self.pon_port = PONPort() |
| 77 | self.pon_port.port_no = 1234 |
| 78 | |
| 79 | self.onu = ONUDevice() |
| 80 | self.onu.pon_port = self.pon_port |
| 81 | self.onu.serial_number = "BRCM1234" |
| 82 | |
| 83 | self.att_si = AttWorkflowDriverServiceInstance( |
| 84 | serial_number="BRCM1234", |
| 85 | owner=self.volt, |
| 86 | owner_id=self.volt.id, |
| 87 | of_dpid="of:1234" |
| 88 | ) |
| 89 | |
| 90 | self.whitelist_entry = AttWorkflowDriverWhiteListEntry( |
| 91 | serial_number="BRCM1234", |
| 92 | owner=self.volt, |
| 93 | owner_id=self.volt.id, |
| 94 | pon_port_id=1234, |
| 95 | device_id="of:1234" |
| 96 | ) |
| 97 | |
| 98 | |
| 99 | def tearDown(self): |
| 100 | sys.path = self.sys_path_save |
| 101 | |
| 102 | def test_not_in_whitelist(self): |
| 103 | |
| 104 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock: |
| 105 | whitelist_mock.return_value = [] |
| 106 | |
| 107 | [res, message] = self.helpers.validate_onu(self.att_si) |
| 108 | |
| 109 | self.assertFalse(res) |
| 110 | self.assertEqual(message, "ONU not found in whitelist") |
| 111 | |
| 112 | def test_wrong_location_port(self): |
| 113 | self.pon_port.port_no = 666 |
| 114 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 115 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 116 | whitelist_mock.return_value = [self.whitelist_entry] |
| 117 | onu_mock.return_value = [self.onu] |
| 118 | |
| 119 | [res, message] = self.helpers.validate_onu(self.att_si) |
| 120 | |
| 121 | self.assertFalse(res) |
| 122 | self.assertEqual(message, "ONU activated in wrong location") |
| 123 | |
| 124 | def test_wrong_location_device(self): |
| 125 | self.att_si.of_dpid = 666 |
| 126 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 127 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 128 | whitelist_mock.return_value = [self.whitelist_entry] |
| 129 | onu_mock.return_value = [self.onu] |
| 130 | |
| 131 | [res, message] = self.helpers.validate_onu(self.att_si) |
| 132 | |
| 133 | self.assertFalse(res) |
| 134 | self.assertEqual(message, "ONU activated in wrong location") |
| 135 | |
| 136 | def test_deferred_validation(self): |
| 137 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 138 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 139 | whitelist_mock.return_value = [self.whitelist_entry] |
| 140 | onu_mock.return_value = [] |
| 141 | |
| 142 | with self.assertRaises(Exception) as e: |
| 143 | self.helpers.validate_onu(self.att_si) |
| 144 | |
| 145 | self.assertEqual(e.exception.message, "ONU device %s is not know to XOS yet" % self.att_si.serial_number) |
| 146 | |
| 147 | def test_validating_onu(self): |
| 148 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 149 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 150 | whitelist_mock.return_value = [self.whitelist_entry] |
| 151 | onu_mock.return_value = [self.onu] |
| 152 | |
| 153 | [res, message] = self.helpers.validate_onu(self.att_si) |
| 154 | |
| 155 | self.assertTrue(res) |
| 156 | self.assertEqual(message, "ONU has been validated") |
| 157 | |
| 158 | def test_validating_onu_lowercase(self): |
| 159 | self.whitelist_entry.serial_number = "brcm1234" |
| 160 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 161 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 162 | whitelist_mock.return_value = [self.whitelist_entry] |
| 163 | onu_mock.return_value = [self.onu] |
| 164 | |
| 165 | [res, message] = self.helpers.validate_onu(self.att_si) |
| 166 | |
| 167 | self.assertTrue(res) |
| 168 | self.assertEqual(message, "ONU has been validated") |
| 169 | |
| 170 | if __name__ == '__main__': |
| 171 | unittest.main() |