Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -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, Mock, MagicMock |
| 17 | |
| 18 | # mocking XOS exception, as they're based in Django |
| 19 | class Exceptions: |
| 20 | XOSValidationError = Exception |
Matteo Scandolo | e73cd44 | 2019-06-20 21:37:22 -0700 | [diff] [blame] | 21 | XOSNotFound = Exception |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 22 | |
| 23 | class XOS: |
| 24 | exceptions = Exceptions |
| 25 | |
| 26 | class TestOLTDeviceModel(unittest.TestCase): |
| 27 | def setUp(self): |
| 28 | self.xos = XOS |
| 29 | |
| 30 | self.models_decl = Mock() |
| 31 | self.models_decl.OLTDevice_decl = MagicMock |
| 32 | self.models_decl.OLTDevice_decl.delete = Mock() |
| 33 | |
| 34 | modules = { |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 35 | 'xos': MagicMock(), |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 36 | 'xos.exceptions': self.xos.exceptions, |
| 37 | 'models_decl': self.models_decl, |
| 38 | } |
| 39 | |
| 40 | self.module_patcher = patch.dict('sys.modules', modules) |
| 41 | self.module_patcher.start() |
| 42 | |
| 43 | from models import OLTDevice |
| 44 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 45 | self.olt_device = OLTDevice() |
| 46 | self.olt_device.id = None # this is a new model |
| 47 | self.olt_device.is_new = True |
| 48 | self.olt_device.device_id = 1234 |
| 49 | |
Matteo Scandolo | 33fb162 | 2018-06-19 18:54:35 -0700 | [diff] [blame] | 50 | def tearDown(self): |
| 51 | self.module_patcher.stop() |
| 52 | |
Matteo Scandolo | 2ed64b9 | 2018-06-18 10:32:56 -0700 | [diff] [blame] | 53 | def test_create_mac_address(self): |
| 54 | from models import OLTDevice |
| 55 | olt = OLTDevice() |
| 56 | |
| 57 | olt.host = "1.1.1.1" |
| 58 | olt.port = "9101" |
| 59 | olt.mac_address = "00:0c:d5:00:05:40" |
| 60 | |
| 61 | with self.assertRaises(Exception) as e: |
| 62 | olt.save() |
| 63 | |
| 64 | self.assertEqual(e.exception.message, |
| 65 | "You can't specify both host/port and mac_address for OLTDevice [host=%s, port=%s, mac_address=%s]" % (olt.host, olt.port, olt.mac_address)) |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 66 | |
| 67 | def test_delete(self): |
| 68 | self.olt_device.delete() |
| 69 | self.models_decl.OLTDevice_decl.delete.assert_called() |
| 70 | |
| 71 | def test_prevent_delete(self): |
| 72 | |
| 73 | onu1 = Mock() |
| 74 | onu1.id = 1 |
| 75 | |
| 76 | pon1 = Mock() |
| 77 | pon1.onu_devices.all.return_value = [onu1] |
| 78 | |
| 79 | self.olt_device.pon_ports.all.return_value = [pon1] |
| 80 | |
| 81 | volt_si_1 = Mock() |
| 82 | volt_si_1.onu_device_id = onu1.id |
| 83 | |
| 84 | with patch.object(self.olt_device, "get_volt_si")as volt_si_get: |
| 85 | volt_si_get.return_value = [volt_si_1] |
| 86 | with self.assertRaises(Exception) as e: |
| 87 | self.olt_device.delete() |
| 88 | |
| 89 | self.assertEqual(e.exception.message, 'OLT "1234" can\'t be deleted as it has subscribers associated with its ONUs') |
| 90 | self.models_decl.OLTDevice_decl.delete.assert_not_called() |
| 91 | |
Matteo Scandolo | 33fb162 | 2018-06-19 18:54:35 -0700 | [diff] [blame] | 92 | class TestONUDeviceModel(unittest.TestCase): |
| 93 | |
| 94 | def setUp(self): |
| 95 | self.xos = XOS |
| 96 | |
| 97 | self.models_decl = Mock() |
| 98 | self.models_decl.ONUDevice_decl = MagicMock |
| 99 | self.models_decl.ONUDevice_decl.delete = Mock() |
| 100 | |
| 101 | modules = { |
Scott Baker | 47b4730 | 2019-01-30 16:55:07 -0800 | [diff] [blame] | 102 | 'xos': MagicMock(), |
Matteo Scandolo | 33fb162 | 2018-06-19 18:54:35 -0700 | [diff] [blame] | 103 | 'xos.exceptions': self.xos.exceptions, |
| 104 | 'models_decl': self.models_decl, |
| 105 | } |
| 106 | |
| 107 | self.module_patcher = patch.dict('sys.modules', modules) |
| 108 | self.module_patcher.start() |
| 109 | |
| 110 | from models import ONUDevice |
| 111 | |
| 112 | self.onu_device = ONUDevice() |
| 113 | self.onu_device.id = None # this is a new model |
| 114 | self.onu_device.is_new = True |
| 115 | self.onu_device.serial_number = 1234 |
| 116 | |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 117 | def tearDown(self): |
| 118 | self.module_patcher.stop() |
| 119 | |
Matteo Scandolo | 33fb162 | 2018-06-19 18:54:35 -0700 | [diff] [blame] | 120 | def test_delete(self): |
| 121 | self.onu_device.delete() |
| 122 | self.models_decl.ONUDevice_decl.delete.assert_called() |
| 123 | |
| 124 | def test_prevent_delete(self): |
| 125 | volt_si_1 = Mock() |
| 126 | volt_si_1.onu_device_id = self.onu_device.id |
| 127 | self.onu_device.volt_service_instances.all.return_value = [volt_si_1] |
| 128 | |
| 129 | with self.assertRaises(Exception) as e: |
| 130 | self.onu_device.delete() |
| 131 | |
| 132 | self.assertEqual(e.exception.message, |
| 133 | 'ONU "1234" can\'t be deleted as it has subscribers associated with it') |
| 134 | self.models_decl.OLTDevice_decl.delete.assert_not_called() |
| 135 | |
Matteo Scandolo | 045b04d | 2019-06-27 18:28:42 -0700 | [diff] [blame^] | 136 | |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 137 | class TestTechnologyProfileModel(unittest.TestCase): |
| 138 | |
| 139 | def setUp(self): |
| 140 | self.xos = XOS |
| 141 | |
| 142 | self.models_decl = Mock() |
| 143 | self.models_decl.TechnologyProfile_decl = MagicMock |
| 144 | self.models_decl.TechnologyProfile_decl.save = Mock() |
| 145 | self.models_decl.TechnologyProfile_decl.objects = Mock() |
| 146 | self.models_decl.TechnologyProfile_decl.objects.filter.return_value = [] |
| 147 | |
| 148 | modules = { |
| 149 | 'xos': MagicMock(), |
| 150 | 'xos.exceptions': self.xos.exceptions, |
| 151 | 'models_decl': self.models_decl |
| 152 | } |
| 153 | |
| 154 | self.module_patcher = patch.dict('sys.modules', modules) |
| 155 | self.module_patcher.start() |
| 156 | |
| 157 | from models import TechnologyProfile |
| 158 | |
| 159 | self.technology_profile = TechnologyProfile() |
| 160 | self.technology_profile.deleted = False |
| 161 | self.technology_profile.id = None # this is a new model |
| 162 | self.technology_profile.is_new = True |
| 163 | self.technology_profile.technology = 'xgspon' |
| 164 | self.technology_profile.profile_id = 64 |
| 165 | self.technology_profile.profile_value = '{ "name": "4QueueHybridProfileMap1", "profile_type": "XPON", "version": 1, "num_gem_ports": 4, "instance_control": { "onu": "multi-instance", "uni": "single-instance", "max_gem_payload_size": "auto" }, "us_scheduler": { "additional_bw": "auto", "direction": "UPSTREAM", "priority": 0, "weight": 0, "q_sched_policy": "hybrid" }, "ds_scheduler": { "additional_bw": "auto", "direction": "DOWNSTREAM", "priority": 0, "weight": 0, "q_sched_policy": "hybrid" }, "upstream_gem_port_attribute_list": [ { "pbit_map": "0b00000101", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 4, "weight": 25, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "max_threshold": 0, "min_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00011010", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 3, "weight": 75, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00100000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 2, "weight": 0, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b11000000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 1, "weight": 25, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } } ], "downstream_gem_port_attribute_list": [ { "pbit_map": "0b00000101", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 4, "weight": 10, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00011010", "aes_encryption": "True", "scheduling_policy": "WRR", "priority_q": 3, "weight": 90, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b00100000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 2, "weight": 0, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } }, { "pbit_map": "0b11000000", "aes_encryption": "True", "scheduling_policy": "StrictPriority", "priority_q": 1, "weight": 25, "discard_policy": "TailDrop", "max_q_size": "auto", "discard_config": { "min_threshold": 0, "max_threshold": 0, "max_probability": 0 } } ]}' |
| 166 | |
| 167 | def tearDown(self): |
| 168 | self.module_patcher.stop() |
| 169 | |
| 170 | def test_save(self): |
| 171 | self.technology_profile.save() |
| 172 | self.models_decl.TechnologyProfile_decl.save.assert_called() |
| 173 | |
Matteo Scandolo | 045b04d | 2019-06-27 18:28:42 -0700 | [diff] [blame^] | 174 | def test_allow_save_if_nohing_changed(self): |
| 175 | self.technology_profile.is_new = False |
| 176 | self.technology_profile.id = 1 |
| 177 | self.technology_profile.profile_value = '{"name": "someValue", "profile_type": "someValue"}' |
| 178 | self.technology_profile.diff.keys.return_value = [] |
| 179 | |
| 180 | self.technology_profile.save() |
| 181 | self.models_decl.TechnologyProfile_decl.save.assert_called() |
| 182 | |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 183 | def test_prevent_modify(self): |
| 184 | self.technology_profile.is_new = False |
| 185 | self.technology_profile.id = 1 |
| 186 | self.technology_profile.profile_value = '{"name": "someValue", "profile_type": "someValue"}' |
Matteo Scandolo | 045b04d | 2019-06-27 18:28:42 -0700 | [diff] [blame^] | 187 | self.technology_profile.diff.keys.return_value = ["some"] |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 188 | |
| 189 | self.models_decl.TechnologyProfile_decl.objects.filter.return_value = [self.technology_profile] |
| 190 | |
| 191 | with self.assertRaises(Exception) as e: |
| 192 | self.technology_profile.save() |
| 193 | |
| 194 | self.assertEqual(e.exception.message, |
| 195 | 'Modification operation is not allowed on Technology Profile [/xgspon/64]. Delete it and add again') |
| 196 | self.models_decl.TechnologyProfile_decl.save.assert_not_called() |
| 197 | |
| 198 | def test_invalid_tech_profile_value_format(self): |
| 199 | self.technology_profile.profile_value = 'someTechProfileValue' |
| 200 | |
| 201 | with self.assertRaises(Exception) as e: |
| 202 | self.technology_profile.save() |
| 203 | |
| 204 | self.assertEqual(e.exception.message, |
| 205 | 'Technology Profile value not in valid JSON format') |
| 206 | self.models_decl.TechnologyProfile_decl.save.assert_not_called() |
| 207 | |
| 208 | |
Matteo Scandolo | 2360fd9 | 2018-05-29 17:27:51 -0700 | [diff] [blame] | 209 | if __name__ == '__main__': |
| 210 | unittest.main() |