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 |
| 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 | XOSProgrammingError = Exception |
| 22 | XOSPermissionDenied = Exception |
| 23 | |
| 24 | class XOS: |
| 25 | exceptions = Exceptions |
| 26 | |
| 27 | class TestRCORDModels(unittest.TestCase): |
| 28 | def setUp(self): |
| 29 | self.xos = XOS |
| 30 | |
| 31 | self.models_decl = Mock() |
| 32 | self.models_decl.RCORDSubscriber_decl = MagicMock |
| 33 | self.models_decl.RCORDSubscriber_decl.save = Mock() |
| 34 | self.models_decl.RCORDSubscriber_decl.objects = Mock() |
| 35 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [] |
| 36 | |
| 37 | |
| 38 | modules = { |
| 39 | 'xos.exceptions': self.xos.exceptions, |
| 40 | 'models_decl': self.models_decl |
| 41 | } |
| 42 | |
| 43 | self.module_patcher = patch.dict('sys.modules', modules) |
| 44 | self.module_patcher.start() |
| 45 | |
| 46 | self.volt = Mock() |
| 47 | |
| 48 | from models import RCORDSubscriber |
| 49 | |
| 50 | self.rcord_subscriber_class = RCORDSubscriber |
| 51 | |
| 52 | self.rcord_subscriber = RCORDSubscriber() |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 53 | self.rcord_subscriber.id = None # this is a new model |
| 54 | self.rcord_subscriber.is_new = True |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 55 | self.rcord_subscriber.onu_device = "BRCM1234" |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 56 | self.rcord_subscriber.c_tag = 111 |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 57 | self.rcord_subscriber.ip_address = "1.1.1.1" |
| 58 | self.rcord_subscriber.mac_address = "00:AA:00:00:00:01" |
| 59 | self.rcord_subscriber.owner.leaf_model.access = "voltha" |
| 60 | self.rcord_subscriber.owner.provider_services = [self.volt] |
| 61 | |
| 62 | |
| 63 | def test_save(self): |
| 64 | self.rcord_subscriber.save() |
| 65 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 66 | |
| 67 | def test_validate_ip_address(self): |
| 68 | self.rcord_subscriber.ip_address = "invalid" |
| 69 | with self.assertRaises(Exception) as e: |
| 70 | self.rcord_subscriber.save() |
| 71 | |
| 72 | self.assertEqual(e.exception.message, "The ip_address you specified (invalid) is not valid") |
| 73 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 74 | |
| 75 | def test_validate_mac_address(self): |
| 76 | self.rcord_subscriber.mac_address = "invalid" |
| 77 | with self.assertRaises(Exception) as e: |
| 78 | self.rcord_subscriber.save() |
| 79 | |
| 80 | self.assertEqual(e.exception.message, "The mac_address you specified (invalid) is not valid") |
| 81 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 82 | |
| 83 | def test_valid_onu_device(self): |
| 84 | self.rcord_subscriber.save() |
| 85 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 86 | |
| 87 | def test_invalid_onu_device(self): |
| 88 | self.volt.leaf_model.has_access_device.return_value = False |
| 89 | with self.assertRaises(Exception) as e: |
| 90 | self.rcord_subscriber.save() |
| 91 | |
| 92 | self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists") |
| 93 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 94 | |
| 95 | def test_validate_c_tag(self): |
| 96 | """ |
| 97 | check that other subscriber attached to the same ONU don't have the same c_tag |
| 98 | """ |
| 99 | |
| 100 | s = Mock() |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 101 | s.c_tag = 111 |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 102 | s.onu_device = "BRCM1234" |
| 103 | |
| 104 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 105 | |
| 106 | with self.assertRaises(Exception) as e: |
| 107 | self.rcord_subscriber.save() |
| 108 | |
| 109 | self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234") |
| 110 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 111 | |
Matteo Scandolo | aa37b42 | 2018-05-23 15:36:56 -0700 | [diff] [blame] | 112 | def test_validate_c_tag_on_update(self): |
| 113 | s = Mock() |
| 114 | s.c_tag = 111 |
| 115 | s.onu_device = "BRCM1234" |
| 116 | s.id = 1 |
| 117 | |
| 118 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 119 | |
| 120 | self.rcord_subscriber.is_new = False |
| 121 | self.rcord_subscriber.id = 1 |
| 122 | self.rcord_subscriber.save() |
| 123 | |
| 124 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 125 | |
| 126 | def test_validate_c_tag_on_update_fail(self): |
| 127 | s = Mock() |
| 128 | s.c_tag = 222 |
| 129 | s.onu_device = "BRCM1234" |
| 130 | s.id = 2 |
| 131 | |
| 132 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 133 | |
| 134 | self.rcord_subscriber.id = 1 |
| 135 | self.rcord_subscriber.is_new = False |
| 136 | self.rcord_subscriber.c_tag = 222 |
| 137 | with self.assertRaises(Exception) as e: |
| 138 | self.rcord_subscriber.save() |
| 139 | |
| 140 | self.assertEqual(e.exception.message, "The c_tag you specified (222) has already been used on device BRCM1234") |
| 141 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
Matteo Scandolo | cc94e90 | 2018-05-22 15:25:25 -0700 | [diff] [blame] | 142 | |
| 143 | def test_generate_c_tag(self): |
| 144 | s = Mock() |
| 145 | s.c_tag = "111" |
| 146 | s.onu_device = "BRCM1234" |
| 147 | |
| 148 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 149 | self.rcord_subscriber.c_tag = None |
| 150 | |
| 151 | self.rcord_subscriber.save() |
| 152 | |
| 153 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 154 | self.assertNotEquals(self.rcord_subscriber.c_tag, "111") |
| 155 | self.assertGreater(self.rcord_subscriber.c_tag, 16) |
| 156 | self.assertLess(self.rcord_subscriber.c_tag, 4097) |
| 157 | |
| 158 | |
| 159 | if __name__ == '__main__': |
| 160 | unittest.main() |