Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -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 |
Matteo Scandolo | 0813b88 | 2018-07-16 14:38:01 -0400 | [diff] [blame] | 16 | import os, sys |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 17 | from mock import patch, Mock, MagicMock |
| 18 | |
Matteo Scandolo | 0813b88 | 2018-07-16 14:38:01 -0400 | [diff] [blame] | 19 | test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__))) |
| 20 | service_dir=os.path.join(test_path, "../../../..") |
| 21 | xos_dir=os.path.join(test_path, "../../..") |
| 22 | if not os.path.exists(os.path.join(test_path, "new_base")): |
| 23 | xos_dir=os.path.join(test_path, "../../../../../../orchestration/xos/xos") |
| 24 | services_dir=os.path.join(xos_dir, "../../xos_services") |
| 25 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 26 | # mocking XOS exception, as they're based in Django |
| 27 | class Exceptions: |
| 28 | XOSValidationError = Exception |
| 29 | XOSProgrammingError = Exception |
| 30 | XOSPermissionDenied = Exception |
| 31 | |
| 32 | class XOS: |
| 33 | exceptions = Exceptions |
| 34 | |
| 35 | class TestRCORDModels(unittest.TestCase): |
| 36 | def setUp(self): |
Matteo Scandolo | 0813b88 | 2018-07-16 14:38:01 -0400 | [diff] [blame] | 37 | |
| 38 | self.sys_path_save = sys.path |
| 39 | sys.path.append(xos_dir) |
| 40 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 41 | self.xos = XOS |
| 42 | |
| 43 | self.models_decl = Mock() |
| 44 | self.models_decl.RCORDSubscriber_decl = MagicMock |
| 45 | self.models_decl.RCORDSubscriber_decl.save = Mock() |
| 46 | self.models_decl.RCORDSubscriber_decl.objects = Mock() |
| 47 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [] |
| 48 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 49 | self.models_decl.RCORDIpAddress_decl = MagicMock |
| 50 | self.models_decl.RCORDIpAddress_decl.save = Mock() |
| 51 | self.models_decl.RCORDIpAddress_decl.objects = Mock() |
| 52 | self.models_decl.RCORDIpAddress_decl.objects.filter.return_value = [] |
| 53 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 54 | |
| 55 | modules = { |
| 56 | 'xos.exceptions': self.xos.exceptions, |
| 57 | 'models_decl': self.models_decl |
| 58 | } |
| 59 | |
| 60 | self.module_patcher = patch.dict('sys.modules', modules) |
| 61 | self.module_patcher.start() |
| 62 | |
| 63 | self.volt = Mock() |
| 64 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 65 | from models import RCORDSubscriber, RCORDIpAddress |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 66 | |
| 67 | self.rcord_subscriber_class = RCORDSubscriber |
| 68 | |
| 69 | self.rcord_subscriber = RCORDSubscriber() |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 70 | self.rcord_subscriber.id = None # this is a new model |
| 71 | self.rcord_subscriber.is_new = True |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 72 | self.rcord_subscriber.onu_device = "BRCM1234" |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 73 | self.rcord_subscriber.c_tag = 111 |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 74 | self.rcord_subscriber.s_tag = 222 |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 75 | self.rcord_subscriber.ips = Mock() |
| 76 | self.rcord_subscriber.ips.all.return_value = [] |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 77 | self.rcord_subscriber.mac_address = "00:AA:00:00:00:01" |
| 78 | self.rcord_subscriber.owner.leaf_model.access = "voltha" |
| 79 | self.rcord_subscriber.owner.provider_services = [self.volt] |
| 80 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 81 | self.rcord_ip = RCORDIpAddress() |
| 82 | self.rcord_ip.subscriber = 1; |
| 83 | |
Matteo Scandolo | 0813b88 | 2018-07-16 14:38:01 -0400 | [diff] [blame] | 84 | def tearDown(self): |
| 85 | sys.path = self.sys_path_save |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 86 | |
| 87 | def test_save(self): |
| 88 | self.rcord_subscriber.save() |
| 89 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 90 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 91 | def _test_validate_ipv4_address(self): |
| 92 | self.rcord_ip.ip = "192.168.0." |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 93 | with self.assertRaises(Exception) as e: |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 94 | self.rcord_ip.save() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 95 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 96 | self.assertEqual(e.exception.message, "The IP specified is not valid: 192.168.0.") |
| 97 | self.models_decl.RCORDIpAddress.save.assert_not_called() |
| 98 | |
| 99 | def test_validate_ipv6_address(self): |
| 100 | self.rcord_ip.ip = "2001:0db8:85a3:0000:0000:8a2e:03" |
| 101 | with self.assertRaises(Exception) as e: |
| 102 | self.rcord_ip.save() |
| 103 | |
| 104 | self.assertEqual(e.exception.message, "The IP specified is not valid: 2001:0db8:85a3:0000:0000:8a2e:03") |
| 105 | self.models_decl.RCORDIpAddress.save.assert_not_called() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 106 | |
| 107 | def test_validate_mac_address(self): |
| 108 | self.rcord_subscriber.mac_address = "invalid" |
| 109 | with self.assertRaises(Exception) as e: |
| 110 | self.rcord_subscriber.save() |
| 111 | |
Luca Prete | d6700ba | 2018-09-12 16:40:49 -0700 | [diff] [blame^] | 112 | self.assertEqual(e.exception.message, "The MAC address specified is not valid: invalid") |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 113 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 114 | |
| 115 | def test_valid_onu_device(self): |
| 116 | self.rcord_subscriber.save() |
| 117 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 118 | |
| 119 | def test_invalid_onu_device(self): |
| 120 | self.volt.leaf_model.has_access_device.return_value = False |
| 121 | with self.assertRaises(Exception) as e: |
| 122 | self.rcord_subscriber.save() |
| 123 | |
| 124 | self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists") |
| 125 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 126 | |
Matteo Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 127 | def test_validate_c_tag_pass(self): |
| 128 | """ |
| 129 | check that other subscriber attached to the same ONU don't have the same c_tag |
| 130 | """ |
| 131 | |
| 132 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [self.rcord_subscriber] |
| 133 | |
| 134 | |
| 135 | self.rcord_subscriber.save() |
| 136 | |
| 137 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 138 | |
| 139 | def test_validate_c_tag_fail(self): |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 140 | """ |
| 141 | check that other subscriber attached to the same ONU don't have the same c_tag |
| 142 | """ |
| 143 | |
| 144 | s = Mock() |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 145 | s.c_tag = 111 |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 146 | s.onu_device = "BRCM1234" |
| 147 | |
Matteo Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 148 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s, self.rcord_subscriber] |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 149 | |
| 150 | with self.assertRaises(Exception) as e: |
| 151 | self.rcord_subscriber.save() |
| 152 | |
| 153 | self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234") |
| 154 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 155 | |
Matteo Scandolo | a187560 | 2018-10-16 07:35:04 -0700 | [diff] [blame] | 156 | def _test_validate_c_tag_on_same_s_tag(self): |
| 157 | """ |
| 158 | check that other subscriber using the same s_tag don't have the same c_tag |
| 159 | """ |
| 160 | s = Mock() |
| 161 | s.id = 123 |
| 162 | s.c_tag = 111 |
| 163 | s.s_tag = 222 |
| 164 | s.onu_device = "BRCM1234" |
| 165 | |
| 166 | with self.assertRaises(Exception) as e: |
| 167 | self.rcord_subscriber.save() |
| 168 | |
| 169 | self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used by Subscriber with id 123 and the same s_tag: 222") |
| 170 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 171 | |
| 172 | |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 173 | def test_validate_c_tag_on_update(self): |
| 174 | s = Mock() |
| 175 | s.c_tag = 111 |
| 176 | s.onu_device = "BRCM1234" |
| 177 | s.id = 1 |
| 178 | |
| 179 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 180 | |
| 181 | self.rcord_subscriber.is_new = False |
| 182 | self.rcord_subscriber.id = 1 |
| 183 | self.rcord_subscriber.save() |
| 184 | |
| 185 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 186 | |
| 187 | def test_validate_c_tag_on_update_fail(self): |
| 188 | s = Mock() |
| 189 | s.c_tag = 222 |
| 190 | s.onu_device = "BRCM1234" |
| 191 | s.id = 2 |
| 192 | |
| 193 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 194 | |
| 195 | self.rcord_subscriber.id = 1 |
| 196 | self.rcord_subscriber.is_new = False |
| 197 | self.rcord_subscriber.c_tag = 222 |
| 198 | with self.assertRaises(Exception) as e: |
| 199 | self.rcord_subscriber.save() |
| 200 | |
| 201 | self.assertEqual(e.exception.message, "The c_tag you specified (222) has already been used on device BRCM1234") |
| 202 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 203 | |
| 204 | def test_generate_c_tag(self): |
| 205 | s = Mock() |
| 206 | s.c_tag = "111" |
| 207 | s.onu_device = "BRCM1234" |
| 208 | |
| 209 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 210 | self.rcord_subscriber.c_tag = None |
| 211 | |
| 212 | self.rcord_subscriber.save() |
| 213 | |
| 214 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 215 | self.assertNotEquals(self.rcord_subscriber.c_tag, "111") |
| 216 | self.assertGreater(self.rcord_subscriber.c_tag, 16) |
| 217 | self.assertLess(self.rcord_subscriber.c_tag, 4097) |
| 218 | |
Matteo Scandolo | c348b3f | 2018-07-29 09:35:11 -0700 | [diff] [blame] | 219 | def test_generate_s_tag(self): |
| 220 | self.rcord_subscriber.c_tag = None |
| 221 | |
| 222 | self.rcord_subscriber.save() |
| 223 | |
| 224 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 225 | self.assertNotEqual(self.rcord_subscriber.s_tag, None) |
| 226 | |
| 227 | def test_provisioned_s_stag(self): |
| 228 | self.rcord_subscriber.save() |
| 229 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 230 | self.assertEqual(self.rcord_subscriber.s_tag, 222) |
| 231 | |
| 232 | |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 233 | |
| 234 | if __name__ == '__main__': |
| 235 | unittest.main() |