Takahiro Suzuki | 2b66b94 | 2020-12-17 11:58:14 +0900 | [diff] [blame] | 1 | # Copyright 2020-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 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 22 | |
| 23 | |
| 24 | class TestNttHelpers(unittest.TestCase): |
| 25 | |
| 26 | def setUp(self): |
| 27 | |
| 28 | self.sys_path_save = sys.path |
| 29 | |
| 30 | # Setting up the config module |
| 31 | from xosconfig import Config |
| 32 | config = os.path.join(test_path, "test_config.yaml") |
| 33 | Config.clear() |
| 34 | Config.init(config, "synchronizer-config-schema.yaml") |
| 35 | # END Setting up the config module |
| 36 | |
| 37 | from multistructlog import create_logger |
| 38 | self.log = create_logger(Config().get('logging')) |
| 39 | |
| 40 | from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config |
| 41 | mock_modelaccessor_config(test_path, [("ntt-workflow-driver", "ntt-workflow-driver.xproto"), |
| 42 | ("olt-service", "volt.xproto"), |
| 43 | ("rcord", "rcord.xproto")]) |
| 44 | |
| 45 | import xossynchronizer.modelaccessor |
| 46 | import mock_modelaccessor |
| 47 | reload(mock_modelaccessor) # in case nose2 loaded it in a previous test |
| 48 | reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test |
| 49 | |
| 50 | from xossynchronizer.modelaccessor import model_accessor |
| 51 | from helpers import NttHelpers |
| 52 | |
| 53 | # import all class names to globals |
| 54 | for (k, v) in model_accessor.all_model_classes.items(): |
| 55 | globals()[k] = v |
| 56 | |
| 57 | self.helpers = NttHelpers |
| 58 | self.model_accessor = model_accessor |
| 59 | |
| 60 | self._volt = VOLTService() |
| 61 | self._volt.id = 1 |
| 62 | |
| 63 | self.volt = Service() |
| 64 | self.volt.id = 1 |
| 65 | self.volt.name = "vOLT" |
| 66 | self.volt.leaf_model = self._volt |
| 67 | |
| 68 | self.pon_port = PONPort() |
| 69 | self.pon_port.port_no = 1234 |
| 70 | |
| 71 | self.onu = ONUDevice() |
| 72 | self.onu.pon_port = self.pon_port |
| 73 | self.onu.serial_number = "BRCM1234" |
| 74 | |
| 75 | self.technologyProfile = TechnologyProfile() |
| 76 | self.technologyProfile.profile_id = 64 |
| 77 | self.technologyProfile.profile_value = '{"profile_type": "EPON","epon_attribute": {"package_type": "A"}}' |
| 78 | |
| 79 | self.ntt_si = NttWorkflowDriverServiceInstance( |
| 80 | serial_number="BRCM1234", |
| 81 | owner=self.volt, |
| 82 | owner_id=self.volt.id, |
| 83 | mac_address="0a0a0a", |
| 84 | of_dpid="of:1234" |
| 85 | ) |
| 86 | |
| 87 | self.whitelist_entry = NttWorkflowDriverWhiteListEntry( |
| 88 | mac_address="0a0a0a", |
| 89 | owner=self.volt, |
| 90 | owner_id=self.volt.id, |
| 91 | pon_port_from=1234, |
| 92 | pon_port_to=1235, |
| 93 | ) |
| 94 | |
| 95 | |
| 96 | def tearDown(self): |
| 97 | sys.path = self.sys_path_save |
| 98 | |
| 99 | def test_not_in_whitelist(self): |
| 100 | |
| 101 | with patch.object(NttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 102 | patch.object(TechnologyProfile.objects, "get_items") as technologyProfile_mock: |
| 103 | whitelist_mock.return_value = [] |
| 104 | technologyProfile_mock.return_value = [self.technologyProfile] |
| 105 | |
| 106 | [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.ntt_si) |
| 107 | |
| 108 | self.assertFalse(res) |
| 109 | self.assertEqual(message, "ONU not found in whitelist") |
| 110 | |
| 111 | def test_wrong_location_port(self): |
| 112 | self.pon_port.port_no = 666 |
| 113 | with patch.object(NttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 114 | patch.object(ONUDevice.objects, "get_items") as onu_mock, \ |
| 115 | patch.object(TechnologyProfile.objects, "get_items") as technologyProfile_mock: |
| 116 | whitelist_mock.return_value = [self.whitelist_entry] |
| 117 | onu_mock.return_value = [self.onu] |
| 118 | technologyProfile_mock.return_value = [self.technologyProfile] |
| 119 | |
| 120 | [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.ntt_si) |
| 121 | |
| 122 | self.assertFalse(res) |
| 123 | self.assertEqual(message, "PON port is not approved.") |
| 124 | |
| 125 | def test_deferred_validation(self): |
| 126 | with patch.object(NttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 127 | patch.object(ONUDevice.objects, "get_items") as onu_mock, \ |
| 128 | patch.object(TechnologyProfile.objects, "get_items") as technologyProfile_mock: |
| 129 | whitelist_mock.return_value = [self.whitelist_entry] |
| 130 | onu_mock.return_value = [] |
| 131 | technologyProfile_mock.return_value = [self.technologyProfile] |
| 132 | |
| 133 | with self.assertRaises(Exception) as e: |
| 134 | self.helpers.validate_onu(self.model_accessor, self.log, self.ntt_si) |
| 135 | |
| 136 | self.assertEqual(e.exception.message, "ONU device %s is not know to XOS yet" % self.ntt_si.serial_number) |
| 137 | |
| 138 | def test_validating_onu(self): |
| 139 | with patch.object(NttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 140 | patch.object(ONUDevice.objects, "get_items") as onu_mock, \ |
| 141 | patch.object(TechnologyProfile.objects, "get_items") as technologyProfile_mock: |
| 142 | whitelist_mock.return_value = [self.whitelist_entry] |
| 143 | onu_mock.return_value = [self.onu] |
| 144 | technologyProfile_mock.return_value = [self.technologyProfile] |
| 145 | |
| 146 | [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.ntt_si) |
| 147 | |
| 148 | self.assertTrue(res) |
| 149 | self.assertEqual(message, "ONU has been validated") |
| 150 | |
| 151 | def test_validating_onu_uppercase(self): |
| 152 | self.whitelist_entry.mac_address = "0A0A0A" |
| 153 | with patch.object(NttWorkflowDriverWhiteListEntry.objects, "get_items") as whitelist_mock, \ |
| 154 | patch.object(ONUDevice.objects, "get_items") as onu_mock, \ |
| 155 | patch.object(TechnologyProfile.objects, "get_items") as technologyProfile_mock: |
| 156 | whitelist_mock.return_value = [self.whitelist_entry] |
| 157 | onu_mock.return_value = [self.onu] |
| 158 | technologyProfile_mock.return_value = [self.technologyProfile] |
| 159 | |
| 160 | [res, message] = self.helpers.validate_onu(self.model_accessor, self.log, self.ntt_si) |
| 161 | |
| 162 | self.assertTrue(res) |
| 163 | self.assertEqual(message, "ONU has been validated") |
| 164 | |
| 165 | if __name__ == '__main__': |
| 166 | unittest.main() |