| 1 |
# Copyright 2014 NEC Corporation |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
import testtools |
| 17 |
|
| 18 |
from neutronclient.common import exceptions |
| 19 |
from neutronclient.common import validators |
| 20 |
|
| 21 |
|
| 22 |
class FakeParsedArgs(object): |
| 23 |
pass |
| 24 |
|
| 25 |
|
| 26 |
class ValidatorTest(testtools.TestCase): |
| 27 |
|
| 28 |
def _test_validate_int(self, attr_val, attr_name='attr1', |
| 29 |
min_value=1, max_value=10): |
| 30 |
obj = FakeParsedArgs() |
| 31 |
setattr(obj, attr_name, attr_val) |
| 32 |
ret = validators.validate_int_range(obj, attr_name, |
| 33 |
min_value, max_value) |
| 34 |
# Come here only if there is no exception. |
| 35 |
self.assertIsNone(ret) |
| 36 |
|
| 37 |
def _test_validate_int_error(self, attr_val, expected_msg, |
| 38 |
attr_name='attr1', expected_exc=None, |
| 39 |
min_value=1, max_value=10): |
| 40 |
if expected_exc is None: |
| 41 |
expected_exc = exceptions.CommandError |
| 42 |
e = self.assertRaises(expected_exc, |
| 43 |
self._test_validate_int, |
| 44 |
attr_val, attr_name, min_value, max_value) |
| 45 |
self.assertEqual(expected_msg, str(e)) |
| 46 |
|
| 47 |
def test_validate_int_min_max(self): |
| 48 |
self._test_validate_int(1) |
| 49 |
self._test_validate_int(10) |
| 50 |
self._test_validate_int('1') |
| 51 |
self._test_validate_int('10') |
| 52 |
self._test_validate_int('0x0a') |
| 53 |
|
| 54 |
self._test_validate_int_error( |
| 55 |
0, 'attr1 "0" should be an integer [1:10].') |
| 56 |
self._test_validate_int_error( |
| 57 |
11, 'attr1 "11" should be an integer [1:10].') |
| 58 |
self._test_validate_int_error( |
| 59 |
'0x10', 'attr1 "0x10" should be an integer [1:10].') |
| 60 |
|
| 61 |
def test_validate_int_min_only(self): |
| 62 |
self._test_validate_int(1, max_value=None) |
| 63 |
self._test_validate_int(10, max_value=None) |
| 64 |
self._test_validate_int(11, max_value=None) |
| 65 |
self._test_validate_int_error( |
| 66 |
0, 'attr1 "0" should be an integer greater than or equal to 1.', |
| 67 |
max_value=None) |
| 68 |
|
| 69 |
def test_validate_int_max_only(self): |
| 70 |
self._test_validate_int(0, min_value=None) |
| 71 |
self._test_validate_int(1, min_value=None) |
| 72 |
self._test_validate_int(10, min_value=None) |
| 73 |
self._test_validate_int_error( |
| 74 |
11, 'attr1 "11" should be an integer smaller than or equal to 10.', |
| 75 |
min_value=None) |
| 76 |
|
| 77 |
def test_validate_int_no_limit(self): |
| 78 |
self._test_validate_int(0, min_value=None, max_value=None) |
| 79 |
self._test_validate_int(1, min_value=None, max_value=None) |
| 80 |
self._test_validate_int(10, min_value=None, max_value=None) |
| 81 |
self._test_validate_int(11, min_value=None, max_value=None) |
| 82 |
self._test_validate_int_error( |
| 83 |
'abc', 'attr1 "abc" should be an integer.', |
| 84 |
min_value=None, max_value=None) |
| 85 |
|
| 86 |
def _test_validate_subnet(self, attr_val, attr_name='attr1'): |
| 87 |
obj = FakeParsedArgs() |
| 88 |
setattr(obj, attr_name, attr_val) |
| 89 |
ret = validators.validate_ip_subnet(obj, attr_name) |
| 90 |
# Come here only if there is no exception. |
| 91 |
self.assertIsNone(ret) |
| 92 |
|
| 93 |
def test_validate_ip_subnet(self): |
| 94 |
self._test_validate_subnet('192.168.2.0/24') |
| 95 |
self._test_validate_subnet('192.168.2.3/20') |
| 96 |
self._test_validate_subnet('192.168.2.1') |
| 97 |
|
| 98 |
e = self.assertRaises(exceptions.CommandError, |
| 99 |
self._test_validate_subnet, |
| 100 |
'192.168.2.256') |
| 101 |
self.assertEqual('attr1 "192.168.2.256" is not a valid CIDR.', str(e)) |