| 1 |
# Copyright (c) 2015 IBM Corp. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# 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, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import hashlib |
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
from oslo_utils import encodeutils |
| 19 |
from oslo_utils import excutils |
| 20 |
from oslo_utils import uuidutils |
| 21 |
|
| 22 |
from neutron_lib.api.definitions import portbindings_extended as pb_ext |
| 23 |
from neutron_lib import constants |
| 24 |
from neutron_lib import exceptions |
| 25 |
from neutron_lib.plugins import utils |
| 26 |
from neutron_lib.tests import _base as base |
| 27 |
|
| 28 |
|
| 29 |
LONG_NAME1 = "A_REALLY_LONG_INTERFACE_NAME1" |
| 30 |
LONG_NAME2 = "A_REALLY_LONG_INTERFACE_NAME2" |
| 31 |
SHORT_NAME = "SHORT" |
| 32 |
|
| 33 |
|
| 34 |
class TestUtils(base.BaseTestCase): |
| 35 |
|
| 36 |
def test_is_valid_vlan_tag(self): |
| 37 |
for v in [constants.MIN_VLAN_TAG, constants.MIN_VLAN_TAG + 2, |
| 38 |
constants.MAX_VLAN_TAG, constants.MAX_VLAN_TAG - 2]: |
| 39 |
self.assertTrue(utils.is_valid_vlan_tag(v)) |
| 40 |
|
| 41 |
def test_is_valid_vlan_tag_invalid_data(self): |
| 42 |
for v in [constants.MIN_VLAN_TAG - 1, constants.MIN_VLAN_TAG - 2, |
| 43 |
constants.MAX_VLAN_TAG + 1, constants.MAX_VLAN_TAG + 2]: |
| 44 |
self.assertFalse(utils.is_valid_vlan_tag(v)) |
| 45 |
|
| 46 |
def test_verify_vlan_range(self): |
| 47 |
for v in [(constants.MIN_VLAN_TAG, constants.MIN_VLAN_TAG + 2), |
| 48 |
(constants.MIN_VLAN_TAG + 2, constants.MAX_VLAN_TAG - 2)]: |
| 49 |
self.assertIsNone(utils.verify_vlan_range(v)) |
| 50 |
|
| 51 |
def test_verify_vlan_range_invalid_range(self): |
| 52 |
for v in [(constants.MIN_VLAN_TAG, constants.MAX_VLAN_TAG + 2), |
| 53 |
(constants.MIN_VLAN_TAG + 4, constants.MIN_VLAN_TAG + 1)]: |
| 54 |
self.assertRaises(exceptions.NetworkVlanRangeError, |
| 55 |
utils.verify_vlan_range, v) |
| 56 |
|
| 57 |
def test_parse_network_vlan_range(self): |
| 58 |
self.assertEqual( |
| 59 |
('n1', (1, 3)), |
| 60 |
utils.parse_network_vlan_range('n1:1:3')) |
| 61 |
self.assertEqual( |
| 62 |
('n1', (1, 1111)), |
| 63 |
utils.parse_network_vlan_range('n1:1:1111')) |
| 64 |
|
| 65 |
def test_parse_network_vlan_range_invalid_range(self): |
| 66 |
self.assertRaises(exceptions.NetworkVlanRangeError, |
| 67 |
utils.parse_network_vlan_range, |
| 68 |
'n1:1,4') |
| 69 |
|
| 70 |
def test_parse_network_vlan_range_missing_network(self): |
| 71 |
self.assertRaises(exceptions.PhysicalNetworkNameError, |
| 72 |
utils.parse_network_vlan_range, |
| 73 |
':1:4') |
| 74 |
|
| 75 |
def test_parse_network_vlan_range_invalid_min_type(self): |
| 76 |
self.assertRaises(exceptions.NetworkVlanRangeError, |
| 77 |
utils.parse_network_vlan_range, |
| 78 |
'n1:a:4') |
| 79 |
|
| 80 |
def test_parse_network_vlan_ranges(self): |
| 81 |
ranges = utils.parse_network_vlan_ranges( |
| 82 |
['n1:1:3', 'n2:2:4', 'n3', 'n4', 'n4:10:12']) |
| 83 |
self.assertEqual(4, len(ranges.keys())) |
| 84 |
self.assertIn('n1', ranges.keys()) |
| 85 |
self.assertIn('n2', ranges.keys()) |
| 86 |
self.assertEqual(2, len(ranges['n1'][0])) |
| 87 |
self.assertEqual(1, ranges['n1'][0][0]) |
| 88 |
self.assertEqual(3, ranges['n1'][0][1]) |
| 89 |
self.assertEqual(2, len(ranges['n2'][0])) |
| 90 |
self.assertEqual(2, ranges['n2'][0][0]) |
| 91 |
self.assertEqual(4, ranges['n2'][0][1]) |
| 92 |
self.assertEqual([constants.VLAN_VALID_RANGE], ranges['n3']) |
| 93 |
self.assertEqual([constants.VLAN_VALID_RANGE], ranges['n4']) |
| 94 |
|
| 95 |
def test_is_valid_gre_id(self): |
| 96 |
for v in [constants.MIN_GRE_ID, constants.MIN_GRE_ID + 2, |
| 97 |
constants.MAX_GRE_ID, constants.MAX_GRE_ID - 2]: |
| 98 |
self.assertTrue(utils.is_valid_gre_id(v)) |
| 99 |
|
| 100 |
def test_is_valid_gre_id_invalid_id(self): |
| 101 |
for v in [constants.MIN_GRE_ID - 1, constants.MIN_GRE_ID - 2, |
| 102 |
True, 'z', 99.999, []]: |
| 103 |
self.assertFalse(utils.is_valid_gre_id(v)) |
| 104 |
|
| 105 |
def test_is_valid_vxlan_vni(self): |
| 106 |
for v in [constants.MIN_VXLAN_VNI, constants.MAX_VXLAN_VNI, |
| 107 |
constants.MIN_VXLAN_VNI + 1, constants.MAX_VXLAN_VNI - 1]: |
| 108 |
self.assertTrue(utils.is_valid_vxlan_vni(v)) |
| 109 |
|
| 110 |
def test_is_valid_vxlan_vni_invalid_values(self): |
| 111 |
for v in [constants.MIN_VXLAN_VNI - 1, constants.MAX_VXLAN_VNI + 1, |
| 112 |
True, 'a', False, {}]: |
| 113 |
self.assertFalse(utils.is_valid_vxlan_vni(v)) |
| 114 |
|
| 115 |
def test_is_valid_geneve_vni(self): |
| 116 |
for v in [constants.MIN_GENEVE_VNI, constants.MAX_GENEVE_VNI, |
| 117 |
constants.MIN_GENEVE_VNI + 1, constants.MAX_GENEVE_VNI - 1]: |
| 118 |
self.assertTrue(utils.is_valid_geneve_vni(v)) |
| 119 |
|
| 120 |
def test_is_valid_geneve_vni_invalid_values(self): |
| 121 |
for v in [constants.MIN_GENEVE_VNI - 1, constants.MAX_GENEVE_VNI + 1, |
| 122 |
True, False, (), 'True']: |
| 123 |
self.assertFalse(utils.is_valid_geneve_vni(v)) |
| 124 |
|
| 125 |
def test_verify_tunnel_range_known_tunnel_type(self): |
| 126 |
mock_fns = [mock.Mock(return_value=False) for _ in range(3)] |
| 127 |
mock_map = { |
| 128 |
constants.TYPE_GRE: mock_fns[0], |
| 129 |
constants.TYPE_VXLAN: mock_fns[1], |
| 130 |
constants.TYPE_GENEVE: mock_fns[2] |
| 131 |
} |
| 132 |
|
| 133 |
with mock.patch.dict(utils._TUNNEL_MAPPINGS, mock_map): |
| 134 |
for t in [constants.TYPE_GRE, constants.TYPE_VXLAN, |
| 135 |
constants.TYPE_GENEVE]: |
| 136 |
self.assertRaises( |
| 137 |
exceptions.NetworkTunnelRangeError, |
| 138 |
utils.verify_tunnel_range, [0, 1], t) |
| 139 |
for f in mock_fns: |
| 140 |
f.assert_called_once_with(0) |
| 141 |
|
| 142 |
def test_verify_tunnel_range_invalid_range(self): |
| 143 |
for r in [[1, 0], [0, -1], [2, 1]]: |
| 144 |
self.assertRaises( |
| 145 |
exceptions.NetworkTunnelRangeError, |
| 146 |
utils.verify_tunnel_range, r, constants.TYPE_FLAT) |
| 147 |
|
| 148 |
def test_verify_tunnel_range(self): |
| 149 |
for r in [[0, 1], [-1, 0], [1, 2]]: |
| 150 |
self.assertIsNone( |
| 151 |
utils.verify_tunnel_range(r, constants.TYPE_FLAT)) |
| 152 |
|
| 153 |
def test_delete_port_on_error(self): |
| 154 |
core_plugin = mock.Mock() |
| 155 |
with mock.patch.object(excutils, 'save_and_reraise_exception'): |
| 156 |
with mock.patch.object(utils, 'LOG'): |
| 157 |
with utils.delete_port_on_error(core_plugin, 'ctx', '1'): |
| 158 |
raise exceptions.BadRequest() |
| 159 |
|
| 160 |
core_plugin.delete_port.assert_called_once_with( |
| 161 |
'ctx', '1', l3_port_check=False) |
| 162 |
|
| 163 |
def test_update_port_on_error(self): |
| 164 |
core_plugin = mock.Mock() |
| 165 |
with mock.patch.object(excutils, 'save_and_reraise_exception'): |
| 166 |
with mock.patch.object(utils, 'LOG'): |
| 167 |
with utils.update_port_on_error(core_plugin, 'ctx', '1', '2'): |
| 168 |
raise exceptions.BadRequest() |
| 169 |
|
| 170 |
core_plugin.update_port.assert_called_once_with( |
| 171 |
'ctx', '1', {'port': '2'}) |
| 172 |
|
| 173 |
@staticmethod |
| 174 |
def _hash_prefix(name): |
| 175 |
# nosec B324 |
| 176 |
hashed_name = hashlib.sha1(encodeutils.to_utf8(name)) |
| 177 |
return hashed_name.hexdigest()[0:utils.INTERFACE_HASH_LEN] |
| 178 |
|
| 179 |
def test_get_interface_name(self): |
| 180 |
prefix = "pre-" |
| 181 |
prefix_long = "long_prefix" |
| 182 |
prefix_exceeds_max_dev_len = "much_too_long_prefix" |
| 183 |
self.assertEqual("A_REALLY_" + self._hash_prefix(LONG_NAME1), |
| 184 |
utils.get_interface_name(LONG_NAME1)) |
| 185 |
self.assertEqual("SHORT", |
| 186 |
utils.get_interface_name(SHORT_NAME)) |
| 187 |
self.assertEqual("pre-A_REA" + self._hash_prefix(LONG_NAME1), |
| 188 |
utils.get_interface_name(LONG_NAME1, prefix=prefix)) |
| 189 |
self.assertEqual("pre-SHORT", |
| 190 |
utils.get_interface_name(SHORT_NAME, prefix=prefix)) |
| 191 |
# len(prefix) > max_device_len - len(hash_used) |
| 192 |
self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, |
| 193 |
prefix_long) |
| 194 |
# len(prefix) > max_device_len |
| 195 |
self.assertRaises(ValueError, utils.get_interface_name, SHORT_NAME, |
| 196 |
prefix=prefix_exceeds_max_dev_len) |
| 197 |
|
| 198 |
def test_get_interface_uniqueness(self): |
| 199 |
prefix = "prefix-" |
| 200 |
if_prefix1 = utils.get_interface_name(LONG_NAME1, prefix=prefix) |
| 201 |
if_prefix2 = utils.get_interface_name(LONG_NAME2, prefix=prefix) |
| 202 |
self.assertNotEqual(if_prefix1, if_prefix2) |
| 203 |
|
| 204 |
def test_get_interface_max_len(self): |
| 205 |
self.assertEqual(constants.DEVICE_NAME_MAX_LEN, |
| 206 |
len(utils.get_interface_name(LONG_NAME1))) |
| 207 |
self.assertEqual(10, len(utils.get_interface_name(LONG_NAME1, |
| 208 |
max_len=10))) |
| 209 |
self.assertEqual(12, len(utils.get_interface_name(LONG_NAME1, |
| 210 |
prefix="pre-", |
| 211 |
max_len=12))) |
| 212 |
|
| 213 |
def test_create_network(self): |
| 214 |
mock_plugin = mock.Mock() |
| 215 |
mock_plugin.create_network = lambda c, n: n |
| 216 |
mock_ctx = mock.Mock() |
| 217 |
mock_ctx.project_id = 'p1' |
| 218 |
net = utils.create_network( |
| 219 |
mock_plugin, mock_ctx, {'network': {'name': 'n1'}}) |
| 220 |
self.assertDictEqual( |
| 221 |
{'project_id': 'p1', 'admin_state_up': True, 'shared': False, |
| 222 |
'tenant_id': 'p1', 'name': 'n1'}, |
| 223 |
net['network']) |
| 224 |
|
| 225 |
def test_create_subnet(self): |
| 226 |
mock_plugin = mock.Mock() |
| 227 |
mock_plugin.create_subnet = lambda c, s: s |
| 228 |
mock_ctx = mock.Mock() |
| 229 |
mock_ctx.project_id = 'p1' |
| 230 |
net_id = uuidutils.generate_uuid() |
| 231 |
snet = utils.create_subnet( |
| 232 |
mock_plugin, mock_ctx, |
| 233 |
{'subnet': {'network_id': net_id, 'ip_version': 4}}) |
| 234 |
self.assertEqual('p1', snet['subnet']['tenant_id']) |
| 235 |
self.assertEqual('p1', snet['subnet']['project_id']) |
| 236 |
self.assertEqual(4, snet['subnet']['ip_version']) |
| 237 |
self.assertEqual(net_id, snet['subnet']['network_id']) |
| 238 |
|
| 239 |
def test_create_port(self): |
| 240 |
mock_plugin = mock.Mock() |
| 241 |
mock_plugin.create_port = lambda c, p: p |
| 242 |
mock_ctx = mock.Mock() |
| 243 |
mock_ctx.project_id = 'p1' |
| 244 |
net_id = uuidutils.generate_uuid() |
| 245 |
port = utils.create_port( |
| 246 |
mock_plugin, mock_ctx, |
| 247 |
{'port': {'network_id': net_id, 'name': 'aport'}}) |
| 248 |
self.assertEqual('p1', port['port']['tenant_id']) |
| 249 |
self.assertEqual('p1', port['port']['project_id']) |
| 250 |
self.assertEqual('aport', port['port']['name']) |
| 251 |
self.assertEqual(net_id, port['port']['network_id']) |
| 252 |
|
| 253 |
def test_get_port_binding_by_status_and_host(self): |
| 254 |
bindings = [] |
| 255 |
self.assertIsNone(utils.get_port_binding_by_status_and_host( |
| 256 |
bindings, constants.INACTIVE)) |
| 257 |
bindings.extend([{pb_ext.STATUS: constants.INACTIVE, |
| 258 |
pb_ext.HOST: 'host-1'}, |
| 259 |
{pb_ext.STATUS: constants.INACTIVE, |
| 260 |
pb_ext.HOST: 'host-2'}]) |
| 261 |
self.assertEqual( |
| 262 |
'host-1', utils.get_port_binding_by_status_and_host( |
| 263 |
bindings, |
| 264 |
constants.INACTIVE)[pb_ext.HOST]) |
| 265 |
self.assertEqual( |
| 266 |
'host-2', utils.get_port_binding_by_status_and_host( |
| 267 |
bindings, |
| 268 |
constants.INACTIVE, |
| 269 |
host='host-2')[pb_ext.HOST]) |
| 270 |
self.assertIsNone(utils.get_port_binding_by_status_and_host( |
| 271 |
bindings, constants.ACTIVE)) |
| 272 |
self.assertRaises(exceptions.PortBindingNotFound, |
| 273 |
utils.get_port_binding_by_status_and_host, bindings, |
| 274 |
constants.ACTIVE, 'host', True, 'port_id') |