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 | |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 52 | from multistructlog import create_logger |
| 53 | self.log = create_logger(Config().get('logging')) |
| 54 | |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 55 | from synchronizers.new_base.mock_modelaccessor_build import build_mock_modelaccessor |
| 56 | |
| 57 | build_mock_modelaccessor(xos_dir, services_dir, [ |
| 58 | get_models_fn("att-workflow-driver", "att-workflow-driver.xproto"), |
| 59 | get_models_fn("olt-service", "volt.xproto"), |
| 60 | get_models_fn("../profiles/rcord", "rcord.xproto") |
| 61 | ]) |
| 62 | import synchronizers.new_base.modelaccessor |
| 63 | from helpers import AttHelpers, model_accessor |
| 64 | |
| 65 | # import all class names to globals |
| 66 | for (k, v) in model_accessor.all_model_classes.items(): |
| 67 | globals()[k] = v |
| 68 | |
| 69 | self.helpers = AttHelpers |
| 70 | |
| 71 | self._volt = VOLTService() |
| 72 | self._volt.id = 1 |
| 73 | |
| 74 | self.volt = Service() |
| 75 | self.volt.id = 1 |
| 76 | self.volt.name = "vOLT" |
| 77 | self.volt.leaf_model = self._volt |
| 78 | |
| 79 | self.pon_port = PONPort() |
| 80 | self.pon_port.port_no = 1234 |
| 81 | |
| 82 | self.onu = ONUDevice() |
| 83 | self.onu.pon_port = self.pon_port |
| 84 | self.onu.serial_number = "BRCM1234" |
| 85 | |
| 86 | self.att_si = AttWorkflowDriverServiceInstance( |
| 87 | serial_number="BRCM1234", |
| 88 | owner=self.volt, |
| 89 | owner_id=self.volt.id, |
| 90 | of_dpid="of:1234" |
| 91 | ) |
| 92 | |
| 93 | self.whitelist_entry = AttWorkflowDriverWhiteListEntry( |
| 94 | serial_number="BRCM1234", |
| 95 | owner=self.volt, |
| 96 | owner_id=self.volt.id, |
| 97 | pon_port_id=1234, |
| 98 | device_id="of:1234" |
| 99 | ) |
| 100 | |
| 101 | |
| 102 | def tearDown(self): |
| 103 | sys.path = self.sys_path_save |
| 104 | |
| 105 | def test_not_in_whitelist(self): |
| 106 | |
| 107 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock: |
| 108 | whitelist_mock.return_value = [] |
| 109 | |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 110 | [res, message] = self.helpers.validate_onu(self.log, self.att_si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 111 | |
| 112 | self.assertFalse(res) |
| 113 | self.assertEqual(message, "ONU not found in whitelist") |
| 114 | |
| 115 | def test_wrong_location_port(self): |
| 116 | self.pon_port.port_no = 666 |
| 117 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 118 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 119 | whitelist_mock.return_value = [self.whitelist_entry] |
| 120 | onu_mock.return_value = [self.onu] |
| 121 | |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 122 | [res, message] = self.helpers.validate_onu(self.log, self.att_si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 123 | |
| 124 | self.assertFalse(res) |
| 125 | self.assertEqual(message, "ONU activated in wrong location") |
| 126 | |
| 127 | def test_wrong_location_device(self): |
| 128 | self.att_si.of_dpid = 666 |
| 129 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 130 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 131 | whitelist_mock.return_value = [self.whitelist_entry] |
| 132 | onu_mock.return_value = [self.onu] |
| 133 | |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 134 | [res, message] = self.helpers.validate_onu(self.log, self.att_si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 135 | |
| 136 | self.assertFalse(res) |
| 137 | self.assertEqual(message, "ONU activated in wrong location") |
| 138 | |
| 139 | def test_deferred_validation(self): |
| 140 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 141 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 142 | whitelist_mock.return_value = [self.whitelist_entry] |
| 143 | onu_mock.return_value = [] |
| 144 | |
| 145 | with self.assertRaises(Exception) as e: |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 146 | self.helpers.validate_onu(self.log, self.att_si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 147 | |
| 148 | self.assertEqual(e.exception.message, "ONU device %s is not know to XOS yet" % self.att_si.serial_number) |
| 149 | |
| 150 | def test_validating_onu(self): |
| 151 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 152 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 153 | whitelist_mock.return_value = [self.whitelist_entry] |
| 154 | onu_mock.return_value = [self.onu] |
| 155 | |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 156 | [res, message] = self.helpers.validate_onu(self.log, self.att_si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 157 | |
| 158 | self.assertTrue(res) |
| 159 | self.assertEqual(message, "ONU has been validated") |
| 160 | |
| 161 | def test_validating_onu_lowercase(self): |
| 162 | self.whitelist_entry.serial_number = "brcm1234" |
| 163 | with patch.object(AttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 164 | patch.object(ONUDevice.objects, "get_items") as onu_mock: |
| 165 | whitelist_mock.return_value = [self.whitelist_entry] |
| 166 | onu_mock.return_value = [self.onu] |
| 167 | |
Scott Baker | 0b4ad93 | 2018-11-26 15:02:13 -0800 | [diff] [blame] | 168 | [res, message] = self.helpers.validate_onu(self.log, self.att_si) |
Matteo Scandolo | ea52909 | 2018-09-11 16:36:39 -0700 | [diff] [blame] | 169 | |
| 170 | self.assertTrue(res) |
| 171 | self.assertEqual(message, "ONU has been validated") |
| 172 | |
| 173 | if __name__ == '__main__': |
| 174 | unittest.main() |