blob: 5c62143d33f10c420969e67b6938dc72e6d10b69 [file] [log] [blame]
Matteo Scandolo2360fd92018-05-29 17:27:51 -07001# 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
15import unittest
16from mock import patch, Mock, MagicMock
17
18# mocking XOS exception, as they're based in Django
19class Exceptions:
20 XOSValidationError = Exception
Matteo Scandoloe73cd442019-06-20 21:37:22 -070021 XOSNotFound = Exception
Matteo Scandolo2360fd92018-05-29 17:27:51 -070022
23class XOS:
24 exceptions = Exceptions
25
26class 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 Baker47b47302019-01-30 16:55:07 -080035 'xos': MagicMock(),
Matteo Scandolo2360fd92018-05-29 17:27:51 -070036 '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 Scandolo2360fd92018-05-29 17:27:51 -070045 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 Scandolo33fb1622018-06-19 18:54:35 -070050 def tearDown(self):
51 self.module_patcher.stop()
52
Matteo Scandolo2ed64b92018-06-18 10:32:56 -070053 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 Scandolo2360fd92018-05-29 17:27:51 -070066
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 Scandolo33fb1622018-06-19 18:54:35 -070092class 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 Baker47b47302019-01-30 16:55:07 -0800102 'xos': MagicMock(),
Matteo Scandolo33fb1622018-06-19 18:54:35 -0700103 '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 Windlassda0d6c42019-05-13 16:00:01 +0000117 def tearDown(self):
118 self.module_patcher.stop()
119
Matteo Scandolo33fb1622018-06-19 18:54:35 -0700120 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
Hardik Windlassda0d6c42019-05-13 16:00:01 +0000136class TestTechnologyProfileModel(unittest.TestCase):
137
138 def setUp(self):
139 self.xos = XOS
140
141 self.models_decl = Mock()
142 self.models_decl.TechnologyProfile_decl = MagicMock
143 self.models_decl.TechnologyProfile_decl.save = Mock()
144 self.models_decl.TechnologyProfile_decl.objects = Mock()
145 self.models_decl.TechnologyProfile_decl.objects.filter.return_value = []
146
147 modules = {
148 'xos': MagicMock(),
149 'xos.exceptions': self.xos.exceptions,
150 'models_decl': self.models_decl
151 }
152
153 self.module_patcher = patch.dict('sys.modules', modules)
154 self.module_patcher.start()
155
156 from models import TechnologyProfile
157
158 self.technology_profile = TechnologyProfile()
159 self.technology_profile.deleted = False
160 self.technology_profile.id = None # this is a new model
161 self.technology_profile.is_new = True
162 self.technology_profile.technology = 'xgspon'
163 self.technology_profile.profile_id = 64
164 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 } } ]}'
165
166 def tearDown(self):
167 self.module_patcher.stop()
168
169 def test_save(self):
170 self.technology_profile.save()
171 self.models_decl.TechnologyProfile_decl.save.assert_called()
172
173 def test_prevent_modify(self):
174 self.technology_profile.is_new = False
175 self.technology_profile.id = 1
176 self.technology_profile.profile_value = '{"name": "someValue", "profile_type": "someValue"}'
177
178 self.models_decl.TechnologyProfile_decl.objects.filter.return_value = [self.technology_profile]
179
180 with self.assertRaises(Exception) as e:
181 self.technology_profile.save()
182
183 self.assertEqual(e.exception.message,
184 'Modification operation is not allowed on Technology Profile [/xgspon/64]. Delete it and add again')
185 self.models_decl.TechnologyProfile_decl.save.assert_not_called()
186
187 def test_invalid_tech_profile_value_format(self):
188 self.technology_profile.profile_value = 'someTechProfileValue'
189
190 with self.assertRaises(Exception) as e:
191 self.technology_profile.save()
192
193 self.assertEqual(e.exception.message,
194 'Technology Profile value not in valid JSON format')
195 self.models_decl.TechnologyProfile_decl.save.assert_not_called()
196
197
Matteo Scandolo2360fd92018-05-29 17:27:51 -0700198if __name__ == '__main__':
199 unittest.main()