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() |
| 53 | self.rcord_subscriber.onu_device = "BRCM1234" |
| 54 | self.rcord_subscriber.c_tag = "111" |
| 55 | self.rcord_subscriber.ip_address = "1.1.1.1" |
| 56 | self.rcord_subscriber.mac_address = "00:AA:00:00:00:01" |
| 57 | self.rcord_subscriber.owner.leaf_model.access = "voltha" |
| 58 | self.rcord_subscriber.owner.provider_services = [self.volt] |
| 59 | |
| 60 | |
| 61 | def test_save(self): |
| 62 | self.rcord_subscriber.save() |
| 63 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 64 | |
| 65 | def test_validate_ip_address(self): |
| 66 | self.rcord_subscriber.ip_address = "invalid" |
| 67 | with self.assertRaises(Exception) as e: |
| 68 | self.rcord_subscriber.save() |
| 69 | |
| 70 | self.assertEqual(e.exception.message, "The ip_address you specified (invalid) is not valid") |
| 71 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 72 | |
| 73 | def test_validate_mac_address(self): |
| 74 | self.rcord_subscriber.mac_address = "invalid" |
| 75 | with self.assertRaises(Exception) as e: |
| 76 | self.rcord_subscriber.save() |
| 77 | |
| 78 | self.assertEqual(e.exception.message, "The mac_address you specified (invalid) is not valid") |
| 79 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 80 | |
| 81 | def test_valid_onu_device(self): |
| 82 | self.rcord_subscriber.save() |
| 83 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 84 | |
| 85 | def test_invalid_onu_device(self): |
| 86 | self.volt.leaf_model.has_access_device.return_value = False |
| 87 | with self.assertRaises(Exception) as e: |
| 88 | self.rcord_subscriber.save() |
| 89 | |
| 90 | self.assertEqual(e.exception.message, "The onu_device you specified (BRCM1234) does not exists") |
| 91 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 92 | |
| 93 | def test_validate_c_tag(self): |
| 94 | """ |
| 95 | check that other subscriber attached to the same ONU don't have the same c_tag |
| 96 | """ |
| 97 | |
| 98 | s = Mock() |
| 99 | s.c_tag = "111" |
| 100 | s.onu_device = "BRCM1234" |
| 101 | |
| 102 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 103 | |
| 104 | with self.assertRaises(Exception) as e: |
| 105 | self.rcord_subscriber.save() |
| 106 | |
| 107 | self.assertEqual(e.exception.message, "The c_tag you specified (111) has already been used on device BRCM1234") |
| 108 | self.models_decl.RCORDSubscriber_decl.save.assert_not_called() |
| 109 | |
| 110 | |
| 111 | def test_generate_c_tag(self): |
| 112 | s = Mock() |
| 113 | s.c_tag = "111" |
| 114 | s.onu_device = "BRCM1234" |
| 115 | |
| 116 | self.models_decl.RCORDSubscriber_decl.objects.filter.return_value = [s] |
| 117 | self.rcord_subscriber.c_tag = None |
| 118 | |
| 119 | self.rcord_subscriber.save() |
| 120 | |
| 121 | self.models_decl.RCORDSubscriber_decl.save.assert_called() |
| 122 | self.assertNotEquals(self.rcord_subscriber.c_tag, "111") |
| 123 | self.assertGreater(self.rcord_subscriber.c_tag, 16) |
| 124 | self.assertLess(self.rcord_subscriber.c_tag, 4097) |
| 125 | |
| 126 | |
| 127 | if __name__ == '__main__': |
| 128 | unittest.main() |