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