| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 |
# not use this file except in compliance with the License. You may obtain |
| 3 |
# a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 |
# License for the specific language governing permissions and limitations |
| 11 |
# under the License. |
| 12 |
|
| 13 |
from unittest import mock |
| 14 |
|
| 15 |
from oslo_config import cfg |
| 16 |
from webob import exc |
| 17 |
|
| 18 |
from neutron_lib.api.validators import allowedaddresspairs as validator |
| 19 |
from neutron_lib.exceptions import allowedaddresspairs as addr_exc |
| 20 |
from neutron_lib.tests import _base as base |
| 21 |
|
| 22 |
|
| 23 |
class TestAllowedAddressPairs(base.BaseTestCase): |
| 24 |
|
| 25 |
def test__validate_allowed_address_pairs_not_a_list(self): |
| 26 |
for d in [{}, set(), 'abc', True, 1]: |
| 27 |
self.assertRaisesRegex( |
| 28 |
exc.HTTPBadRequest, 'must be a list', |
| 29 |
validator._validate_allowed_address_pairs, d) |
| 30 |
|
| 31 |
@mock.patch.object(cfg, 'CONF') |
| 32 |
def test__validate_allowed_address_pairs_exhausted(self, mock_conf): |
| 33 |
mock_conf.max_allowed_address_pair = 1 |
| 34 |
self.assertRaises( |
| 35 |
addr_exc.AllowedAddressPairExhausted, |
| 36 |
validator._validate_allowed_address_pairs, |
| 37 |
[{}, {}]) |
| 38 |
|
| 39 |
@mock.patch.object(cfg, 'CONF') |
| 40 |
def test__validate_allowed_address_pairs_invalid_mac(self, mock_conf): |
| 41 |
mock_conf.max_allowed_address_pair = 3 |
| 42 |
self.assertRaisesRegex( |
| 43 |
exc.HTTPBadRequest, 'is not a valid MAC address', |
| 44 |
validator._validate_allowed_address_pairs, |
| 45 |
[{'mac_address': 1}]) |
| 46 |
|
| 47 |
@mock.patch.object(cfg, 'CONF') |
| 48 |
def test__validate_allowed_address_pairs_missing_ip(self, mock_conf): |
| 49 |
mock_conf.max_allowed_address_pair = 3 |
| 50 |
self.assertRaises( |
| 51 |
addr_exc.AllowedAddressPairsMissingIP, |
| 52 |
validator._validate_allowed_address_pairs, |
| 53 |
[{'ip_adress': '192.168.1.11'}]) |
| 54 |
|
| 55 |
@mock.patch.object(cfg, 'CONF') |
| 56 |
def test__validate_allowed_address_pairs_duplicate(self, mock_conf): |
| 57 |
mock_conf.max_allowed_address_pair = 3 |
| 58 |
self.assertRaises( |
| 59 |
addr_exc.DuplicateAddressPairInRequest, |
| 60 |
validator._validate_allowed_address_pairs, |
| 61 |
[{'ip_address': '192.168.1.11'}, |
| 62 |
{'ip_address': '192.168.1.11'}]) |
| 63 |
|
| 64 |
@mock.patch.object(cfg, 'CONF') |
| 65 |
def test__validate_allowed_address_pairs_invalid_attrs(self, mock_conf): |
| 66 |
mock_conf.max_allowed_address_pair = 3 |
| 67 |
self.assertRaisesRegex( |
| 68 |
exc.HTTPBadRequest, 'Unrecognized attribute', |
| 69 |
validator._validate_allowed_address_pairs, |
| 70 |
[{'ip_address': '192.168.1.11'}, |
| 71 |
{'ip_address': '192.168.1.12', 'idk': True}]) |
| 72 |
|
| 73 |
@mock.patch.object(cfg, 'CONF') |
| 74 |
def test__validate_allowed_address_pairs_invalid_subnet(self, mock_conf): |
| 75 |
mock_conf.max_allowed_address_pair = 3 |
| 76 |
self.assertRaisesRegex( |
| 77 |
exc.HTTPBadRequest, 'is not a valid IP subnet', |
| 78 |
validator._validate_allowed_address_pairs, |
| 79 |
[{'ip_address': '192.168.1.11'}, |
| 80 |
{'ip_address': '192.168.1.0/a'}]) |
| 81 |
|
| 82 |
@mock.patch.object(cfg, 'CONF') |
| 83 |
def test__validate_allowed_address_pairs_invalid_ip_address( |
| 84 |
self, mock_conf): |
| 85 |
mock_conf.max_allowed_address_pair = 3 |
| 86 |
self.assertRaisesRegex( |
| 87 |
exc.HTTPBadRequest, 'is not a valid IP address', |
| 88 |
validator._validate_allowed_address_pairs, |
| 89 |
[{'ip_address': '192.168.1.a'}, |
| 90 |
{'ip_address': '192.168.1.2'}]) |
| 91 |
|
| 92 |
@mock.patch.object(cfg, 'CONF') |
| 93 |
def test__validate_allowed_address_pairs_good_data(self, mock_conf): |
| 94 |
mock_conf.max_allowed_address_pair = 3 |
| 95 |
self.assertIsNone(validator._validate_allowed_address_pairs( |
| 96 |
[{'ip_address': '192.1.1.11'}, {'ip_address': '19.1.1.11'}])) |