Scott Baker | d443ea7 | 2018-08-07 13:50:06 -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 | import os, sys |
| 17 | from mock import patch, Mock, MagicMock |
| 18 | |
| 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 | |
| 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 TestFabricCrossconnectModels(unittest.TestCase): |
| 36 | def setUp(self): |
| 37 | |
| 38 | self.sys_path_save = sys.path |
| 39 | sys.path.append(xos_dir) |
| 40 | |
| 41 | self.xos = XOS |
| 42 | |
| 43 | self.models_decl = Mock() |
| 44 | self.models_decl.BNGPortMapping_decl = MagicMock |
| 45 | self.models_decl.BNGPortMapping_decl.save = Mock() |
| 46 | self.models_decl.BNGPortMapping_decl.objects = Mock() |
| 47 | self.models_decl.BNGPortMapping_decl.objects.filter.return_value = [] |
| 48 | |
| 49 | |
| 50 | modules = { |
| 51 | 'xos.exceptions': self.xos.exceptions, |
| 52 | 'models_decl': self.models_decl |
| 53 | } |
| 54 | |
| 55 | self.module_patcher = patch.dict('sys.modules', modules) |
| 56 | self.module_patcher.start() |
| 57 | |
| 58 | self.volt = Mock() |
| 59 | |
| 60 | from models import BNGPortMapping |
| 61 | |
| 62 | self.BNGPortMapping = BNGPortMapping() |
| 63 | |
| 64 | def tearDown(self): |
| 65 | sys.path = self.sys_path_save |
| 66 | |
| 67 | def test_validate_range_single(self): |
| 68 | bpm = self.BNGPortMapping() |
| 69 | bpm.validate_range("123") |
| 70 | |
| 71 | def test_validate_range_commas(self): |
| 72 | bpm = self.BNGPortMapping() |
| 73 | bpm.validate_range("123, 456") |
| 74 | |
| 75 | def test_validate_range_ANY(self): |
| 76 | bpm = self.BNGPortMapping() |
| 77 | bpm.validate_range("ANY") |
| 78 | bpm.validate_range("any") |
| 79 | |
| 80 | def test_validate_range_dash(self): |
| 81 | bpm = self.BNGPortMapping() |
| 82 | bpm.validate_range("123-456") |
| 83 | |
| 84 | def test_validate_dash_commas(self): |
| 85 | bpm = self.BNGPortMapping() |
| 86 | bpm.validate_range("123-456, 789 - 1000") |
| 87 | |
| 88 | def test_validate_range_empty(self): |
| 89 | bpm = self.BNGPortMapping() |
| 90 | with self.assertRaises(Exception) as e: |
| 91 | bpm.validate_range("") |
| 92 | |
| 93 | self.assertEqual(e.exception.message, 'Malformed range ') |
| 94 | |
| 95 | def test_validate_range_none(self): |
| 96 | bpm = self.BNGPortMapping() |
| 97 | with self.assertRaises(Exception) as e: |
| 98 | bpm.validate_range("") |
| 99 | |
| 100 | self.assertEqual(e.exception.message, 'Malformed range ') |
| 101 | |
| 102 | def test_validate_range_all(self): |
| 103 | bpm = self.BNGPortMapping() |
| 104 | with self.assertRaises(Exception) as e: |
| 105 | bpm.validate_range("badstring") |
| 106 | |
| 107 | self.assertEqual(e.exception.message, 'Malformed range badstring') |
| 108 | |
| 109 | def test_validate_half_range(self): |
| 110 | bpm = self.BNGPortMapping() |
| 111 | with self.assertRaises(Exception) as e: |
| 112 | bpm.validate_range("123-") |
| 113 | |
| 114 | self.assertEqual(e.exception.message, 'Malformed range 123-') |
| 115 | |
| 116 | def test_validate_half_comma(self): |
| 117 | bpm = self.BNGPortMapping() |
| 118 | with self.assertRaises(Exception) as e: |
| 119 | bpm.validate_range("123,") |
| 120 | |
| 121 | self.assertEqual(e.exception.message, 'Malformed range 123,') |
| 122 | |
| 123 | if __name__ == '__main__': |
| 124 | unittest.main() |