| 1 |
# Copyright (c) 2015 OpenStack 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 |
| 12 |
# implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
from oslo_db import exception as d_exc |
| 17 |
from oslo_utils import timeutils |
| 18 |
from oslo_utils import uuidutils |
| 19 |
|
| 20 |
from neutron.tests.unit import testlib_api |
| 21 |
from neutron_lib import context |
| 22 |
from neutron_lib.db import api as db_api |
| 23 |
|
| 24 |
from networking_l2gw.db.l2gateway.ovsdb import lib |
| 25 |
from networking_l2gw.db.l2gateway.ovsdb import models |
| 26 |
|
| 27 |
_uuid = uuidutils.generate_uuid |
| 28 |
|
| 29 |
|
| 30 |
class OvsdbLibTestCase(testlib_api.SqlTestCase): |
| 31 |
|
| 32 |
def setUp(self): |
| 33 |
super().setUp() |
| 34 |
self.ctx = context.get_admin_context() |
| 35 |
|
| 36 |
def _get_logical_switch_dict(self): |
| 37 |
uuid = _uuid() |
| 38 |
record_dict = {'uuid': uuid, |
| 39 |
'name': 'logical_switch1', |
| 40 |
'key': '100', |
| 41 |
'ovsdb_identifier': "host1"} |
| 42 |
return record_dict |
| 43 |
|
| 44 |
def _create_logical_switch(self, record_dict, name=None): |
| 45 |
if name: |
| 46 |
record_dict['name'] = name |
| 47 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 48 |
entry = models.LogicalSwitches( |
| 49 |
uuid=record_dict['uuid'], |
| 50 |
name=record_dict['name'], |
| 51 |
key=record_dict['key'], |
| 52 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 53 |
self.ctx.session.add(entry) |
| 54 |
return entry |
| 55 |
|
| 56 |
def _assert_model_equals(self, entry, result): |
| 57 |
for field, res in result.items(): |
| 58 |
self.assertEqual(str(entry[field]), str(res)) |
| 59 |
|
| 60 |
def test_get_logical_switch(self): |
| 61 |
record_dict = self._get_logical_switch_dict() |
| 62 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 63 |
entry = self._create_logical_switch(record_dict) |
| 64 |
result = lib.get_logical_switch(self.ctx, record_dict) |
| 65 |
self._assert_model_equals(entry, result) |
| 66 |
|
| 67 |
def test_get_logical_switch_return_none(self): |
| 68 |
record_dict = {'uuid': 'foo_uuid', 'ovsdb_identifier': 'foo_ovsdb_id'} |
| 69 |
result = lib.get_logical_switch(self.ctx, record_dict) |
| 70 |
self.assertIsNone(result) |
| 71 |
|
| 72 |
def test_add_logical_switch(self): |
| 73 |
record_dict = self._get_logical_switch_dict() |
| 74 |
self._create_logical_switch(record_dict) |
| 75 |
count = self.ctx.session.query(models.LogicalSwitches).count() |
| 76 |
self.assertEqual(1, count) |
| 77 |
|
| 78 |
def test_delete_logical_switch(self): |
| 79 |
record_dict = self._get_logical_switch_dict() |
| 80 |
self._create_logical_switch(record_dict) |
| 81 |
lib.delete_logical_switch(self.ctx, record_dict) |
| 82 |
count = self.ctx.session.query(models.LogicalSwitches).count() |
| 83 |
self.assertEqual(count, 0) |
| 84 |
|
| 85 |
def _get_physical_locator_dict(self): |
| 86 |
uuid = _uuid() |
| 87 |
record_dict = {'uuid': uuid, |
| 88 |
'dst_ip': '10.0.0.1', |
| 89 |
'ovsdb_identifier': 'host1'} |
| 90 |
return record_dict |
| 91 |
|
| 92 |
def _create_physical_locator(self, record_dict, dst_ip=None): |
| 93 |
if dst_ip: |
| 94 |
record_dict['dst_ip'] = dst_ip |
| 95 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 96 |
entry = models.PhysicalLocators( |
| 97 |
uuid=record_dict['uuid'], |
| 98 |
dst_ip=record_dict['dst_ip'], |
| 99 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 100 |
self.ctx.session.add(entry) |
| 101 |
return entry |
| 102 |
|
| 103 |
def test_get_physical_locator(self): |
| 104 |
record_dict = self._get_physical_locator_dict() |
| 105 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 106 |
entry = self._create_physical_locator(record_dict) |
| 107 |
result = lib.get_physical_locator(self.ctx, record_dict) |
| 108 |
self._assert_model_equals(entry, result) |
| 109 |
|
| 110 |
def test_add_physical_locator(self): |
| 111 |
record_dict = self._get_physical_locator_dict() |
| 112 |
self._create_physical_locator(record_dict) |
| 113 |
count = self.ctx.session.query(models.PhysicalLocators).count() |
| 114 |
self.assertEqual(1, count) |
| 115 |
|
| 116 |
def test_delete_physical_locator(self): |
| 117 |
record_dict = self._get_physical_locator_dict() |
| 118 |
self._create_physical_locator(record_dict) |
| 119 |
lib.delete_physical_locator(self.ctx, record_dict) |
| 120 |
count = self.ctx.session.query(models.PhysicalLocators).count() |
| 121 |
self.assertEqual(count, 0) |
| 122 |
|
| 123 |
def _get_physical_switch_dict(self): |
| 124 |
uuid = _uuid() |
| 125 |
record_dict = {'uuid': uuid, |
| 126 |
'name': 'physical_switch1', |
| 127 |
'tunnel_ip': '10.0.0.1', |
| 128 |
'ovsdb_identifier': 'host1'} |
| 129 |
return record_dict |
| 130 |
|
| 131 |
def _create_physical_switch(self, record_dict, name=None): |
| 132 |
if name: |
| 133 |
record_dict['name'] = name |
| 134 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 135 |
entry = models.PhysicalSwitches( |
| 136 |
uuid=record_dict['uuid'], |
| 137 |
name=record_dict['name'], |
| 138 |
tunnel_ip=record_dict['tunnel_ip'], |
| 139 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 140 |
self.ctx.session.add(entry) |
| 141 |
return entry |
| 142 |
|
| 143 |
def test_get_physical_switch(self): |
| 144 |
record_dict = self._get_physical_switch_dict() |
| 145 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 146 |
entry = self._create_physical_switch(record_dict) |
| 147 |
result = lib.get_physical_switch(self.ctx, record_dict) |
| 148 |
self._assert_model_equals(entry, result) |
| 149 |
|
| 150 |
def test_add_physical_switch(self): |
| 151 |
record_dict = self._get_physical_switch_dict() |
| 152 |
self._create_physical_switch(record_dict) |
| 153 |
count = self.ctx.session.query(models.PhysicalSwitches).count() |
| 154 |
self.assertEqual(1, count) |
| 155 |
|
| 156 |
def test_add_physical_switch_raise_on_duplicate_constraint(self): |
| 157 |
record_dict = self._get_physical_switch_dict() |
| 158 |
self._create_physical_switch(record_dict) |
| 159 |
# Call the method twice to trigger a db duplicate constraint error, |
| 160 |
# this time with a different switch name! |
| 161 |
self.assertRaises(d_exc.DBDuplicateEntry, |
| 162 |
self._create_physical_switch, |
| 163 |
record_dict, 'physical_switch2') |
| 164 |
|
| 165 |
def test_delete_physical_switch(self): |
| 166 |
record_dict = self._get_physical_switch_dict() |
| 167 |
self._create_physical_switch(record_dict) |
| 168 |
lib.delete_physical_switch(self.ctx, record_dict) |
| 169 |
count = self.ctx.session.query(models.PhysicalSwitches).count() |
| 170 |
self.assertEqual(count, 0) |
| 171 |
|
| 172 |
def _get_physical_port_dict(self): |
| 173 |
uuid = _uuid() |
| 174 |
ps_id = _uuid() |
| 175 |
record_dict = {'uuid': uuid, |
| 176 |
'name': 'physical_port1', |
| 177 |
'physical_switch_id': ps_id, |
| 178 |
'ovsdb_identifier': 'host1'} |
| 179 |
return record_dict |
| 180 |
|
| 181 |
def _create_physical_port(self, |
| 182 |
record_dict, |
| 183 |
name=None, |
| 184 |
physical_switch_id=None): |
| 185 |
if name and physical_switch_id: |
| 186 |
record_dict['name'] = name |
| 187 |
record_dict['physical_switch_id'] = physical_switch_id |
| 188 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 189 |
entry = models.PhysicalPorts( |
| 190 |
uuid=record_dict['uuid'], |
| 191 |
name=record_dict['name'], |
| 192 |
physical_switch_id=record_dict['physical_switch_id'], |
| 193 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 194 |
self.ctx.session.add(entry) |
| 195 |
return entry |
| 196 |
|
| 197 |
def test_get_physical_port(self): |
| 198 |
record_dict = self._get_physical_port_dict() |
| 199 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 200 |
entry = self._create_physical_port(record_dict) |
| 201 |
result = lib.get_physical_port(self.ctx, record_dict) |
| 202 |
self._assert_model_equals(entry, result) |
| 203 |
|
| 204 |
def test_add_physical_port(self): |
| 205 |
record_dict = self._get_physical_port_dict() |
| 206 |
self._create_physical_port(record_dict) |
| 207 |
count = self.ctx.session.query(models.PhysicalPorts).count() |
| 208 |
self.assertEqual(1, count) |
| 209 |
|
| 210 |
def test_add_physical_port_raise_on_duplicate_constraint(self): |
| 211 |
record_dict = self._get_physical_port_dict() |
| 212 |
self._create_physical_port(record_dict) |
| 213 |
# Call the method twice to trigger a db duplicate constraint error, |
| 214 |
# this time with a different switch name and physical switch id! |
| 215 |
self.assertRaises(d_exc.DBDuplicateEntry, |
| 216 |
self._create_physical_port, |
| 217 |
record_dict, 'physical_port2', _uuid()) |
| 218 |
|
| 219 |
def test_delete_physical_port(self): |
| 220 |
record_dict = self._get_physical_port_dict() |
| 221 |
self._create_physical_port(record_dict) |
| 222 |
lib.delete_physical_port(self.ctx, record_dict) |
| 223 |
count = self.ctx.session.query(models.PhysicalPorts).count() |
| 224 |
self.assertEqual(count, 0) |
| 225 |
|
| 226 |
def _get_ucast_mac_local_dict(self): |
| 227 |
uuid = _uuid() |
| 228 |
ls_id = _uuid() |
| 229 |
pl_id = _uuid() |
| 230 |
record_dict = {'uuid': uuid, |
| 231 |
'mac': '12:34:56:78:90:aa:bb', |
| 232 |
'logical_switch_id': ls_id, |
| 233 |
'physical_locator_id': pl_id, |
| 234 |
'ip_address': '10.0.0.1', |
| 235 |
'ovsdb_identifier': 'host1'} |
| 236 |
return record_dict |
| 237 |
|
| 238 |
def _create_ucast_mac_local(self, record_dict): |
| 239 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 240 |
entry = models.UcastMacsLocals( |
| 241 |
uuid=record_dict['uuid'], |
| 242 |
mac=record_dict['mac'], |
| 243 |
logical_switch_id=record_dict['logical_switch_id'], |
| 244 |
physical_locator_id=record_dict['physical_locator_id'], |
| 245 |
ip_address=record_dict['ip_address'], |
| 246 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 247 |
self.ctx.session.add(entry) |
| 248 |
return entry |
| 249 |
|
| 250 |
def test_get_ucast_mac_local(self): |
| 251 |
record_dict = self._get_ucast_mac_local_dict() |
| 252 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 253 |
entry = self._create_ucast_mac_local(record_dict) |
| 254 |
result = lib.get_ucast_mac_local(self.ctx, record_dict) |
| 255 |
self._assert_model_equals(entry, result) |
| 256 |
|
| 257 |
def test_add_ucast_mac_local(self): |
| 258 |
record_dict = self._get_ucast_mac_local_dict() |
| 259 |
self._create_ucast_mac_local(record_dict) |
| 260 |
count = self.ctx.session.query(models.UcastMacsLocals).count() |
| 261 |
self.assertEqual(1, count) |
| 262 |
|
| 263 |
def test_add_ucast_mac_local_raise_on_duplicate_constraint(self): |
| 264 |
record_dict = self._get_ucast_mac_local_dict() |
| 265 |
self._create_ucast_mac_local(record_dict) |
| 266 |
# Call the method twice to trigger a db duplicate constraint error, |
| 267 |
# this time with a different mac and logical switch id! |
| 268 |
record_dict['mac'] = '11:22:33:44:55:66:77' |
| 269 |
record_dict['logical_switch_id'] = _uuid() |
| 270 |
self.assertRaises(d_exc.DBDuplicateEntry, |
| 271 |
self._create_ucast_mac_local, |
| 272 |
record_dict) |
| 273 |
|
| 274 |
def test_delete_ucast_mac_local(self): |
| 275 |
record_dict = self._get_ucast_mac_local_dict() |
| 276 |
self._create_ucast_mac_local(record_dict) |
| 277 |
lib.delete_ucast_mac_local(self.ctx, record_dict) |
| 278 |
count = self.ctx.session.query(models.UcastMacsLocals).count() |
| 279 |
self.assertEqual(count, 0) |
| 280 |
|
| 281 |
def _get_ucast_mac_remote_dict(self): |
| 282 |
uuid = _uuid() |
| 283 |
ls_id = _uuid() |
| 284 |
pl_id = _uuid() |
| 285 |
record_dict = {'uuid': uuid, |
| 286 |
'mac': '12:34:56:78:90:aa:bb', |
| 287 |
'logical_switch_id': ls_id, |
| 288 |
'physical_locator_id': pl_id, |
| 289 |
'ip_address': '10.0.0.1', |
| 290 |
'ovsdb_identifier': 'host1'} |
| 291 |
return record_dict |
| 292 |
|
| 293 |
def _create_ucast_mac_remote(self, |
| 294 |
record_dict, |
| 295 |
mac=None, |
| 296 |
logical_switch_uuid=None): |
| 297 |
if mac and logical_switch_uuid: |
| 298 |
record_dict['mac'] = mac |
| 299 |
record_dict['logical_switch_id'] = logical_switch_uuid |
| 300 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 301 |
entry = models.UcastMacsRemotes( |
| 302 |
uuid=record_dict['uuid'], |
| 303 |
mac=record_dict['mac'], |
| 304 |
logical_switch_id=record_dict['logical_switch_id'], |
| 305 |
physical_locator_id=record_dict['physical_locator_id'], |
| 306 |
ip_address=record_dict['ip_address'], |
| 307 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 308 |
self.ctx.session.add(entry) |
| 309 |
return entry |
| 310 |
|
| 311 |
def test_get_ucast_mac_remote(self): |
| 312 |
record_dict = self._get_ucast_mac_remote_dict() |
| 313 |
entry = self._create_ucast_mac_remote(record_dict) |
| 314 |
result = lib.get_ucast_mac_remote(self.ctx, record_dict) |
| 315 |
for ent_key, ent_val in entry.items(): |
| 316 |
self.assertEqual(ent_val, result[ent_key]) |
| 317 |
|
| 318 |
def test_add_ucast_mac_remote(self): |
| 319 |
record_dict = self._get_ucast_mac_remote_dict() |
| 320 |
self._create_ucast_mac_remote(record_dict) |
| 321 |
count = self.ctx.session.query(models.UcastMacsRemotes).count() |
| 322 |
self.assertEqual(1, count) |
| 323 |
|
| 324 |
def test_add_ucast_mac_remote_raise_on_duplicate_constraint(self): |
| 325 |
record_dict = self._get_ucast_mac_remote_dict() |
| 326 |
self._create_ucast_mac_remote(record_dict) |
| 327 |
# Call the method twice to trigger a db duplicate constraint error, |
| 328 |
# this time with a different mac and logical switch id! |
| 329 |
self.assertRaises(d_exc.DBDuplicateEntry, |
| 330 |
self._create_ucast_mac_remote, |
| 331 |
record_dict, '11:22:33:44:55:66:77', _uuid()) |
| 332 |
|
| 333 |
def test_delete_ucast_mac_remote(self): |
| 334 |
record_dict = self._get_ucast_mac_remote_dict() |
| 335 |
self._create_ucast_mac_remote(record_dict) |
| 336 |
lib.delete_ucast_mac_remote(self.ctx, record_dict) |
| 337 |
count = self.ctx.session.query(models.UcastMacsRemotes).count() |
| 338 |
self.assertEqual(count, 0) |
| 339 |
|
| 340 |
def _get_vlan_binding_dict(self): |
| 341 |
port_uuid = _uuid() |
| 342 |
ls_uuid = _uuid() |
| 343 |
record_dict = {'port_uuid': port_uuid, |
| 344 |
'vlan': 200, |
| 345 |
'logical_switch_uuid': ls_uuid, |
| 346 |
'ovsdb_identifier': 'host1'} |
| 347 |
return record_dict |
| 348 |
|
| 349 |
def _get_vlan0_binding_dict(self): |
| 350 |
port_uuid = _uuid() |
| 351 |
ls_uuid = _uuid() |
| 352 |
record_dict = {'port_uuid': port_uuid, |
| 353 |
'vlan': 0, |
| 354 |
'logical_switch_uuid': ls_uuid, |
| 355 |
'ovsdb_identifier': 'host1'} |
| 356 |
return record_dict |
| 357 |
|
| 358 |
def _create_vlan_binding(self, record_dict, port_uuid=None): |
| 359 |
if port_uuid: |
| 360 |
record_dict['port_uuid'] = port_uuid |
| 361 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 362 |
entry = models.VlanBindings( |
| 363 |
port_uuid=record_dict['port_uuid'], |
| 364 |
vlan=record_dict['vlan'], |
| 365 |
logical_switch_uuid=record_dict['logical_switch_uuid'], |
| 366 |
ovsdb_identifier=record_dict['ovsdb_identifier']) |
| 367 |
self.ctx.session.add(entry) |
| 368 |
return entry |
| 369 |
|
| 370 |
def test_get_vlan_binding(self): |
| 371 |
record_dict = self._get_vlan_binding_dict() |
| 372 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 373 |
entry = self._create_vlan_binding(record_dict) |
| 374 |
result = lib.get_vlan_binding(self.ctx, record_dict) |
| 375 |
self._assert_model_equals(entry, result) |
| 376 |
|
| 377 |
def test_add_vlan_binding(self): |
| 378 |
record_dict = self._get_vlan_binding_dict() |
| 379 |
self._create_vlan_binding(record_dict) |
| 380 |
count = self.ctx.session.query(models.VlanBindings).count() |
| 381 |
self.assertEqual(1, count) |
| 382 |
|
| 383 |
def test_add_vlan_binding_raise_on_duplicate_constraint(self): |
| 384 |
record_dict = self._get_vlan_binding_dict() |
| 385 |
self._create_vlan_binding(record_dict) |
| 386 |
# Call the method twice to trigger a db duplicate constraint error, |
| 387 |
# this time with a same entries |
| 388 |
self.assertRaises(d_exc.DBDuplicateEntry, |
| 389 |
self._create_vlan_binding, |
| 390 |
record_dict) |
| 391 |
|
| 392 |
def test_delete_vlan_binding(self): |
| 393 |
record_dict = self._get_vlan_binding_dict() |
| 394 |
self._create_vlan_binding(record_dict) |
| 395 |
lib.delete_vlan_binding(self.ctx, record_dict) |
| 396 |
count = self.ctx.session.query(models.VlanBindings).count() |
| 397 |
self.assertEqual(count, 0) |
| 398 |
|
| 399 |
def test_delete_vlan0_binding(self): |
| 400 |
record_dict = self._get_vlan0_binding_dict() |
| 401 |
self._create_vlan_binding(record_dict) |
| 402 |
lib.delete_vlan_binding(self.ctx, record_dict) |
| 403 |
count = self.ctx.session.query(models.VlanBindings).count() |
| 404 |
self.assertEqual(count, 0) |
| 405 |
|
| 406 |
def test_get_logical_switch_by_name(self): |
| 407 |
record_dict = self._get_logical_switch_dict() |
| 408 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 409 |
entry = self._create_logical_switch(record_dict, |
| 410 |
'logical_switch2') |
| 411 |
record_dict['logical_switch_name'] = 'logical_switch2' |
| 412 |
result = lib.get_logical_switch_by_name(self.ctx, record_dict) |
| 413 |
self._assert_model_equals(entry, result) |
| 414 |
|
| 415 |
def test_get_all_logical_switch_by_name(self): |
| 416 |
record_dict1 = self._get_logical_switch_dict() |
| 417 |
self._create_logical_switch(record_dict1, 'logical_switch2') |
| 418 |
record_dict2 = self._get_logical_switch_dict() |
| 419 |
self._create_logical_switch(record_dict2, 'logical_switch2') |
| 420 |
ls_list = lib.get_all_logical_switches_by_name(self.ctx, |
| 421 |
'logical_switch2') |
| 422 |
self.assertEqual(2, len(ls_list)) |
| 423 |
|
| 424 |
def test_get_physical_locator_by_dst_ip(self): |
| 425 |
record_dict = self._get_physical_locator_dict() |
| 426 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 427 |
entry = self._create_physical_locator(record_dict, '20.0.0.1') |
| 428 |
record_dict['dst_ip'] = '20.0.0.1' |
| 429 |
record_dict.pop('uuid') |
| 430 |
result = lib.get_physical_locator_by_dst_ip(self.ctx, record_dict) |
| 431 |
self._assert_model_equals(entry, result) |
| 432 |
|
| 433 |
def test_get_physical_switch_by_name(self): |
| 434 |
record_dict = self._get_physical_switch_dict() |
| 435 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 436 |
entry = self._create_physical_switch(record_dict, |
| 437 |
'physical_switch2') |
| 438 |
result = lib.get_physical_switch_by_name(self.ctx, |
| 439 |
'physical_switch2') |
| 440 |
self._assert_model_equals(entry, result) |
| 441 |
|
| 442 |
def test_get_all_vlan_bindings_by_physical_port(self): |
| 443 |
record_dict1 = {'port_uuid': 'ps123', |
| 444 |
'vlan': 200, |
| 445 |
'logical_switch_uuid': 'ls123', |
| 446 |
'ovsdb_identifier': 'host1'} |
| 447 |
self._create_vlan_binding(record_dict1) |
| 448 |
record_dict1['vlan'] = 300 |
| 449 |
record_dict1['logical_switch_uuid'] = 'ls456' |
| 450 |
self._create_vlan_binding(record_dict1) |
| 451 |
record_dict1['uuid'] = record_dict1.get('port_uuid') |
| 452 |
vlan_list = lib.get_all_vlan_bindings_by_physical_port(self.ctx, |
| 453 |
record_dict1) |
| 454 |
self.assertEqual(2, len(vlan_list)) |
| 455 |
|
| 456 |
def test_get_ucast_mac_remote_by_mac_and_ls(self): |
| 457 |
record_dict = self._get_ucast_mac_remote_dict() |
| 458 |
entry = self._create_ucast_mac_remote(record_dict, |
| 459 |
'00:11:22:33:44:55:66', |
| 460 |
'ls123') |
| 461 |
record_dict['mac'] = '00:11:22:33:44:55:66' |
| 462 |
record_dict['logical_switch_uuid'] = 'ls123' |
| 463 |
result = lib.get_ucast_mac_remote_by_mac_and_ls(self.ctx, |
| 464 |
record_dict) |
| 465 |
for ent_key, ent_val in entry.items(): |
| 466 |
self.assertEqual(ent_val, result[ent_key]) |
| 467 |
|
| 468 |
def test_get_ucast_mac_remote_by_mac_and_ls_when_not_found(self): |
| 469 |
record_dict = self._get_ucast_mac_remote_dict() |
| 470 |
record_dict['mac'] = '00:11:22:33:44:55:66' |
| 471 |
record_dict['logical_switch_uuid'] = 'ls123' |
| 472 |
result = lib.get_ucast_mac_remote_by_mac_and_ls(self.ctx, |
| 473 |
record_dict) |
| 474 |
self.assertIsNone(result) |
| 475 |
|
| 476 |
def test_get_physical_port_by_name_and_ps(self): |
| 477 |
record_dict = self._get_physical_port_dict() |
| 478 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 479 |
entry = self._create_physical_port(record_dict, |
| 480 |
'port1', |
| 481 |
'ps123') |
| 482 |
record_dict['interface_name'] = 'port1' |
| 483 |
record_dict['physical_switch_id'] = 'ps123' |
| 484 |
result = lib.get_physical_port_by_name_and_ps(self.ctx, |
| 485 |
record_dict) |
| 486 |
self._assert_model_equals(entry, result) |
| 487 |
|
| 488 |
def _get_pending_mac_dict(self, timestamp): |
| 489 |
record_dict = {'uuid': _uuid(), |
| 490 |
'mac': 'aa:aa:aa:aa:aa:aa', |
| 491 |
'logical_switch_uuid': 'fake_ls_id', |
| 492 |
'locator_uuid': _uuid(), |
| 493 |
'dst_ip': '1.1.1.1', |
| 494 |
'vm_ip': '2.2.2.2', |
| 495 |
'ovsdb_identifier': 'ovsdb1', |
| 496 |
'operation': 'insert', |
| 497 |
'timestamp': timestamp} |
| 498 |
return record_dict |
| 499 |
|
| 500 |
def _create_pending_mac(self, record_dict): |
| 501 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 502 |
entry = models.PendingUcastMacsRemote( |
| 503 |
uuid=record_dict['uuid'], |
| 504 |
mac=record_dict['mac'], |
| 505 |
logical_switch_uuid=record_dict['logical_switch_uuid'], |
| 506 |
locator_uuid=record_dict['locator_uuid'], |
| 507 |
dst_ip=record_dict['dst_ip'], |
| 508 |
vm_ip=record_dict['vm_ip'], |
| 509 |
ovsdb_identifier=record_dict['ovsdb_identifier'], |
| 510 |
operation=record_dict['operation'], |
| 511 |
timestamp=record_dict['timestamp']) |
| 512 |
self.ctx.session.add(entry) |
| 513 |
return entry |
| 514 |
|
| 515 |
def test_add_pending_ucast_mac_remote(self): |
| 516 |
timestamp = timeutils.utcnow() |
| 517 |
record_dict = self._get_pending_mac_dict(timestamp) |
| 518 |
self._create_pending_mac(record_dict) |
| 519 |
count = self.ctx.session.query(models.PendingUcastMacsRemote).count() |
| 520 |
self.assertEqual(1, count) |
| 521 |
|
| 522 |
def test_get_pending_ucast_mac_remote(self): |
| 523 |
timestamp = timeutils.utcnow() |
| 524 |
record_dict = self._get_pending_mac_dict(timestamp) |
| 525 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 526 |
entry = self._create_pending_mac(record_dict) |
| 527 |
result = lib.get_pending_ucast_mac_remote( |
| 528 |
self.ctx, record_dict['ovsdb_identifier'], |
| 529 |
record_dict['mac'], record_dict['logical_switch_uuid']) |
| 530 |
self._assert_model_equals(entry, result) |
| 531 |
|
| 532 |
def test_get_all_pending_remote_macs_in_asc_order(self): |
| 533 |
timestamp1 = timeutils.utcnow() |
| 534 |
record_dict1 = self._get_pending_mac_dict(timestamp1) |
| 535 |
timestamp2 = timeutils.utcnow() |
| 536 |
record_dict2 = self._get_pending_mac_dict(timestamp2) |
| 537 |
entry = [] |
| 538 |
with db_api.CONTEXT_WRITER.using(self.ctx): |
| 539 |
entry.append(self._create_pending_mac(record_dict1)) |
| 540 |
entry.append(self._create_pending_mac(record_dict2)) |
| 541 |
result = lib.get_all_pending_remote_macs_in_asc_order( |
| 542 |
self.ctx, record_dict1['ovsdb_identifier']) |
| 543 |
for index, res in enumerate(result): |
| 544 |
for k, v in res.items(): |
| 545 |
self.assertEqual(res[k], entry[index][k]) |
| 546 |
|
| 547 |
def test_delete_pending_ucast_mac_remote(self): |
| 548 |
timestamp = timeutils.utcnow() |
| 549 |
record_dict = self._get_pending_mac_dict(timestamp) |
| 550 |
self._create_pending_mac(record_dict) |
| 551 |
lib.delete_pending_ucast_mac_remote(self.ctx, |
| 552 |
record_dict['operation'], |
| 553 |
record_dict['ovsdb_identifier'], |
| 554 |
record_dict['logical_switch_uuid'], |
| 555 |
record_dict['mac']) |
| 556 |
count = self.ctx.session.query(models.UcastMacsRemotes).count() |
| 557 |
self.assertEqual(count, 0) |
| 558 |
|
| 559 |
def test_get_all_ucast_mac_remote_by_ls(self): |
| 560 |
record_dict = self._get_ucast_mac_remote_dict() |
| 561 |
record_dict1 = self._create_ucast_mac_remote(record_dict) |
| 562 |
record_dict = self._get_ucast_mac_remote_dict() |
| 563 |
record_dict['mac'] = '00:11:22:33:44:55:66' |
| 564 |
record_dict['logical_switch_id'] = record_dict1.get( |
| 565 |
'logical_switch_id') |
| 566 |
self._create_ucast_mac_remote(record_dict) |
| 567 |
mac_list = lib.get_all_ucast_mac_remote_by_ls(self.ctx, record_dict) |
| 568 |
self.assertEqual(2, len(mac_list)) |