/usr/lib/python2.7/dist-packages/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_monitor.py is in python-networking-l2gw 1:1.0.0-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 | # Copyright (c) 2015 OpenStack Foundation.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import contextlib
import socket
import ssl
import eventlet
import mock
from neutron.tests import base
from networking_l2gw.services.l2gateway.agent import l2gateway_config as conf
from networking_l2gw.services.l2gateway.agent.ovsdb import ovsdb_monitor
from networking_l2gw.services.l2gateway.common import config
from networking_l2gw.services.l2gateway.common import constants as n_const
from networking_l2gw.services.l2gateway.common import ovsdb_schema
from networking_l2gw.services.l2gateway import exceptions
from networking_l2gw.tests.unit.services.l2gateway.agent.ovsdb import (
test_base_connection as base_test)
from oslo_config import cfg
from oslo_log import log as logging
from oslo_serialization import jsonutils
LOG = logging.getLogger(__name__)
class TestOVSDBMonitor(base.BaseTestCase):
def setUp(self):
super(TestOVSDBMonitor, self).setUp()
self.conf = mock.patch.object(conf, 'L2GatewayConfig').start()
config.register_ovsdb_opts_helper(cfg.CONF)
self.callback = mock.Mock()
cfg.CONF.set_override('max_connection_retries', 0, 'ovsdb')
self.sock = mock.patch('socket.socket').start()
self.ssl_sock = mock.patch.object(ssl, 'wrap_socket').start()
self.greenthread = mock.patch.object(eventlet.greenthread,
'spawn_n').start()
self.l2gw_ovsdb = ovsdb_monitor.OVSDBMonitor(mock.Mock(),
self.conf,
self.callback)
self.op_id = 'abcd'
props = {'select': {'initial': True,
'insert': True,
'delete': True,
'modify': True}}
self.monitor_message = {'id': self.op_id,
'method': 'monitor',
'params': [n_const.OVSDB_SCHEMA_NAME,
None,
{'Logical_Switch': [props],
'Physical_Switch': [props],
'Physical_Port': [props],
'Ucast_Macs_Local': [props],
'Ucast_Macs_Remote': [props],
'Physical_Locator': [props],
'Mcast_Macs_Local': [props],
'Physical_Locator_Set': [props]}]}
fake_message = {'Logical_Switch': props,
'Physical_Switch': props,
'Physical_Port': props,
'Ucast_Macs_Local': props,
'Ucast_Macs_Remote': props,
'Physical_Locator': props,
'Mcast_Macs_Local': props,
'Physical_Locator_Set': props}
self.msg = self.monitor_message
self.msg1 = {'result': fake_message}
self.msg2 = {'method': 'update',
'params': ['', fake_message]}
self.l2gw_ovsdb.responses = [self.monitor_message]
def test_init(self):
"""Test case to test __init__."""
fakesocket = base_test.SocketClass()
with contextlib.nested(
mock.patch.object(ovsdb_monitor.LOG, 'debug'),
mock.patch.object(eventlet.greenthread, 'spawn'),
mock.patch.object(socket, 'socket', return_value=fakesocket)
) as(logger_call, gt, mock_sock):
self.l2gw_ovsdb.__init__(mock.Mock(), self.conf,
self.callback)
self.assertTrue(self.l2gw_ovsdb.connected)
self.assertTrue(gt.called)
self.assertTrue(self.sock.called)
def test_setup_dispatch_table(self):
expected_dict = {'Logical_Switch':
self.l2gw_ovsdb._process_logical_switch,
'Ucast_Macs_Local':
self.l2gw_ovsdb._process_ucast_macs_local,
'Physical_Locator':
self.l2gw_ovsdb._process_physical_locator,
'Ucast_Macs_Remote':
self.l2gw_ovsdb._process_ucast_macs_remote,
'Mcast_Macs_Local':
self.l2gw_ovsdb._process_mcast_macs_local,
'Physical_Locator_Set':
self.l2gw_ovsdb._process_physical_locator_set
}
self.l2gw_ovsdb._setup_dispatch_table()
self.assertEqual(expected_dict, self.l2gw_ovsdb.dispatch_table)
def test_set_monitor_response_handler(self):
"""Test case to test _set_monitor_response_handler with error_msg."""
self.l2gw_ovsdb.connected = True
with contextlib.nested(
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_set_handler'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'send', return_value=True),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_response',
return_value=(mock.ANY, False)),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_monitor_msg'),
mock.patch.object(ovsdb_monitor.LOG, 'warning')) as (
set_handler, send, process_resp,
process_monitor_msg, logger_call):
self.l2gw_ovsdb.set_monitor_response_handler()
self.assertTrue(set_handler.called)
self.assertTrue(send.called)
self.assertTrue(process_monitor_msg.called)
self.assertTrue(process_resp.called)
def test_set_monitor_response_handler_with_error_in_send(self):
"""Test case to test _set_monitor_response_handler."""
self.l2gw_ovsdb.connected = True
with contextlib.nested(
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_set_handler'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'send', return_value=False),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_response',
return_value=(mock.ANY, True)),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_monitor_msg'),
mock.patch.object(ovsdb_monitor.LOG, 'warning')) as (
set_handler, send,
process_resp, process_monitor_msg, logger_call):
self.l2gw_ovsdb.set_monitor_response_handler()
self.assertTrue(set_handler.called)
self.assertTrue(send.called)
self.assertFalse(process_resp.called)
self.assertFalse(process_monitor_msg.called)
def test_update_event_handler(self):
"""Test case to test _update_event_handler."""
with mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_update_event'
) as process_update_event:
self.l2gw_ovsdb._update_event_handler(self.msg, mock.ANY)
process_update_event.assert_called_once_with(self.msg, mock.ANY)
def test_process_update_event(self):
"""Test case to test _process_update_event."""
with contextlib.nested(
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_physical_port'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_physical_switch'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_logical_switch'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_ucast_macs_local'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_physical_locator'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_ucast_macs_remote'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_mcast_macs_local'),
mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_process_physical_locator_set')
) as (proc_phy_port,
proc_phy_switch,
proc_logic_switch,
proc_ucast_mac,
proc_phy_loc,
proc_ucast_mac_remote,
proc_mcast_mac_local,
proc_phys_loc_set):
self.l2gw_ovsdb._setup_dispatch_table()
self.l2gw_ovsdb._process_update_event(self.msg2, mock.ANY)
self.assertTrue(proc_phy_port.called)
self.assertTrue(proc_phy_switch.called)
self.assertTrue(proc_logic_switch.called)
self.assertTrue(proc_ucast_mac.called)
self.assertTrue(proc_phy_loc.called)
self.assertTrue(proc_ucast_mac_remote.called)
self.assertTrue(proc_mcast_mac_local.called)
self.assertTrue(proc_phys_loc_set.called)
self.assertTrue(self.callback.called)
def test_process_response_raise_exception(self):
"""Test case to test _process_response with exception."""
with mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_response') as resp:
with mock.patch.object(ovsdb_monitor.LOG, 'debug'):
self.assertRaises(exceptions.OVSDBError,
self.l2gw_ovsdb._process_response,
self.op_id)
resp.assert_called_once_with(self.op_id)
def test_process_response(self):
"""Test case to test _process_response."""
with mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_response',
return_value={'key':
'some_value'
}) as resp:
with mock.patch.object(ovsdb_monitor.LOG, 'debug'):
self.l2gw_ovsdb._process_response(self.op_id)
resp.assert_called_once_with(self.op_id)
def test_default_echo_handler(self):
"""Test case to test _default_echo_handler."""
dummy_msg = {'params': 'fake_params',
'id': 'fake_id'}
with mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'send') as send:
self.l2gw_ovsdb._default_echo_handler(dummy_msg, mock.ANY)
self.assertTrue(send.called)
def test_set_handler(self):
"""Test case to test _set_handler."""
dummy_method = "dummy"
dummy_handler = "handler"
self.l2gw_ovsdb._set_handler(dummy_method, dummy_handler)
self.assertEqual(self.l2gw_ovsdb.handlers[dummy_method],
dummy_handler)
def test_on_remote_message(self):
"""Test case to test _on_remote_message."""
self.l2gw_ovsdb.handlers = mock.Mock()
with mock.patch.object(ovsdb_monitor.LOG, 'debug'):
with mock.patch.object(jsonutils, 'loads') as json_load:
self.l2gw_ovsdb._on_remote_message(self.msg)
self.assertTrue(json_load.called)
handler_method = json_load.return_value
handler_method.get.assert_called_once_with(mock.ANY, mock.ANY)
def test_rcv_thread_none(self):
"""Test case to test _rcv_thread receives None from socket."""
self.assertTrue(self.l2gw_ovsdb.read_on)
with mock.patch.object(self.l2gw_ovsdb.socket,
'recv', return_value=None) as sock_recv:
with mock.patch.object(self.l2gw_ovsdb.socket,
'close') as sock_close:
self.l2gw_ovsdb._rcv_thread()
self.assertTrue(sock_recv.called)
self.assertFalse(self.l2gw_ovsdb.connected)
self.assertFalse(self.l2gw_ovsdb.read_on)
self.assertTrue(sock_close.called)
def test_rcv_thread_exception(self):
"""Test case to test _rcv_thread with exception."""
with contextlib.nested(
mock.patch.object(self.l2gw_ovsdb.socket, 'recv',
side_effect=Exception,
return_value=None),
mock.patch.object(self.l2gw_ovsdb.socket,
'close'),
mock.patch.object(ovsdb_monitor.LOG,
'exception')
) as (sock_recv, sock_close, logger_call):
self.l2gw_ovsdb._rcv_thread()
self.assertTrue(logger_call.called)
self.assertTrue(sock_recv.called)
self.assertFalse(self.l2gw_ovsdb.connected)
self.assertFalse(self.l2gw_ovsdb.read_on)
self.assertTrue(sock_close.called)
def test_form_ovsdb_data(self):
some_value = mock.Mock()
expect = {n_const.OVSDB_IDENTIFIER: self.conf.ovsdb_identifier,
'new_logical_switches': some_value,
'new_physical_switches': some_value,
'new_physical_ports': some_value,
'new_physical_locators': some_value,
'new_local_macs': some_value,
'new_remote_macs': some_value,
'new_mlocal_macs': some_value,
'new_locator_sets': some_value,
'deleted_logical_switches': some_value,
'deleted_physical_switches': some_value,
'deleted_physical_ports': some_value,
'deleted_physical_locators': some_value,
'deleted_local_macs': some_value,
'deleted_remote_macs': some_value,
'deleted_mlocal_macs': some_value,
'deleted_locator_sets': some_value,
'modified_logical_switches': some_value,
'modified_physical_switches': some_value,
'modified_physical_ports': some_value,
'modified_physical_locators': some_value,
'modified_local_macs': some_value,
'modified_remote_macs': some_value,
'modified_mlocal_macs': some_value,
'modified_locator_sets': some_value}
with mock.patch.object(ovsdb_monitor.OVSDBMonitor,
'_get_list',
return_value=some_value
):
result = self.l2gw_ovsdb._form_ovsdb_data(mock.Mock(), mock.ANY)
self.assertEqual(expect, result)
def test_process_physical_port(self):
"""Test case to process new physical_port."""
fake_id = 'fake_id'
add = {'new': {'uuid': 'fake_id',
'name': 'fake_name',
'physical_switch_id': 'fake_switch_id',
'port_fault_status': 'fake_status',
'vlan_bindings': [["some"], []]}}
delete = {'old': {'uuid': 'fake_id_old',
'name': 'fake_name_old',
'physical_switch_id': 'fake_switch_id_old'}}
modify = {}
modify.update(add)
modify.update(delete)
port_map = {'fake_id': 'fake_switch_id'}
data_dict = {'new_physical_ports': [],
'modified_physical_ports': [],
'deleted_physical_ports': []}
with mock.patch.object(ovsdb_schema, 'PhysicalPort') as phy_port:
with mock.patch.object(ovsdb_schema, 'VlanBinding'):
# test add
self.l2gw_ovsdb._process_physical_port(fake_id,
add,
port_map,
data_dict)
phy_port.assert_called_once_with(
fake_id, 'fake_name', None, None, 'fake_status')
self.assertIn(phy_port.return_value,
data_dict.get('new_physical_ports'))
# test modify
self.l2gw_ovsdb._process_physical_port(fake_id,
modify,
port_map,
data_dict)
self.assertIn(phy_port.return_value,
data_dict.get('modified_physical_ports'))
# test delete
self.l2gw_ovsdb._process_physical_port(fake_id,
delete,
port_map,
data_dict)
self.assertIn(phy_port.return_value,
data_dict.get('deleted_physical_ports'))
def test_process_physical_port_with_empty_fault_status(self):
"""Test case to process new physical_port with empty fault status."""
fake_id = 'fake_id'
add = {'new': {'uuid': 'fake_id',
'name': 'fake_name',
'physical_switch_id': 'fake_switch_id',
'port_fault_status': ['set', []],
'vlan_bindings': [["some"], []]}}
port_map = {'fake_id': 'fake_switch_id'}
data_dict = {'new_physical_ports': [],
'modified_physical_ports': [],
'deleted_physical_ports': []}
with mock.patch.object(ovsdb_schema, 'PhysicalPort') as phy_port:
with mock.patch.object(ovsdb_schema, 'VlanBinding'):
# test add
self.l2gw_ovsdb._process_physical_port(fake_id,
add,
port_map,
data_dict)
phy_port.assert_called_once_with(
fake_id, 'fake_name', None, None, None)
self.assertIn(phy_port.return_value,
data_dict.get('new_physical_ports'))
def test_process_physical_switch(self):
"""Test case to process new physical_switch."""
port_map = {'fake_id': 'fake_switch_id'}
with mock.patch.object(ovsdb_schema, 'PhysicalPort') as phy_port:
with mock.patch.object(ovsdb_schema,
'PhysicalSwitch') as phy_switch:
physical_port = phy_port.return_value
fake_id = 'fake_id'
add = {'new': {'uuid': 'fake_id',
'name': 'fake_name',
'tunnel_ips': 'fake_tunnel_ip',
'switch_fault_status': 'fake_status',
'ports': ['set', 'set',
physical_port]}}
delete = {'old': {'uuid': 'fake_id_old',
'name': 'fake_name_old',
'tunnel_ips': 'fake_tunnel_ip_old',
'ports': ['', physical_port]}}
modify = {}
modify.update(add)
modify.update(delete)
data_dict = {'new_physical_switches': [],
'modified_physical_switches': [],
'deleted_physical_switches': [],
'new_physical_ports': []}
# test add
self.l2gw_ovsdb._process_physical_switch(fake_id,
add,
port_map,
data_dict)
self.assertIn(phy_switch.return_value,
data_dict['new_physical_switches'])
phy_switch.assert_called_once_with(
'fake_id', 'fake_name',
'fake_tunnel_ip', 'fake_status')
# test modify
self.l2gw_ovsdb._process_physical_switch(fake_id,
modify,
port_map,
data_dict)
self.assertIn(phy_switch.return_value,
data_dict['modified_physical_switches'])
# test delete
self.l2gw_ovsdb._process_physical_switch(fake_id,
delete,
port_map,
data_dict)
self.assertIn(phy_switch.return_value,
data_dict['deleted_physical_switches'])
def test_process_physical_switch_with_empty_fault_status(self):
"""Test case to process new physical_switch with empty fault status."""
port_map = {'fake_id': 'fake_switch_id'}
with mock.patch.object(ovsdb_schema, 'PhysicalPort') as phy_port:
with mock.patch.object(ovsdb_schema,
'PhysicalSwitch') as phy_switch:
physical_port = phy_port.return_value
fake_id = 'fake_id'
add = {'new': {'uuid': 'fake_id',
'name': 'fake_name',
'tunnel_ips': 'fake_tunnel_ip',
'switch_fault_status': ['set', []],
'ports': ['set', 'set',
physical_port]}}
data_dict = {'new_physical_switches': [],
'modified_physical_switches': [],
'deleted_physical_switches': [],
'new_physical_ports': []}
# test add
self.l2gw_ovsdb._process_physical_switch(fake_id,
add,
port_map,
data_dict)
self.assertIn(phy_switch.return_value,
data_dict['new_physical_switches'])
phy_switch.assert_called_once_with('fake_id', 'fake_name',
'fake_tunnel_ip', None)
def test_process_logical_switch(self):
"""Test case to process new logical_switch."""
fake_id = 'fake_id'
fake_name = 'fake_name'
fake_tunnel_key = 'fake_tunnel_key'
add = {'new': {'name': 'fake_name',
'tunnel_key': 'fake_tunnel_key'}}
delete = {'old': {'name': 'fake_name_old',
'tunnel_key': 'fake_tunnel_key_old'}}
modify = {}
modify.update(add)
modify.update(delete)
data_dict = {'new_logical_switches': [],
'modified_logical_switches': [],
'deleted_logical_switches': []}
with mock.patch.object(ovsdb_schema,
'LogicalSwitch') as logical_switch:
# test add
self.l2gw_ovsdb._process_logical_switch(fake_id, add,
data_dict)
logical_switch.assert_called_once_with(
fake_id, fake_name, fake_tunnel_key, None)
self.assertIn(logical_switch.return_value,
data_dict['new_logical_switches'])
# test modify
self.l2gw_ovsdb._process_logical_switch(fake_id, modify,
data_dict)
self.assertIn(logical_switch.return_value,
data_dict['modified_logical_switches'])
# test delete
self.l2gw_ovsdb._process_logical_switch(fake_id, delete,
data_dict)
self.assertIn(logical_switch.return_value,
data_dict['deleted_logical_switches'])
def test_process_ucast_macs_local(self):
"""Test case to process new ucast_macs_local."""
fake_id = 'fake_id'
add = {'new': {'locator': ["uuid", "fake_locator_id"],
'MAC': 'fake_mac',
'logical_switch':
["uuid",
"fake_logical_switch_id"],
'ipaddr': 'fake_ip_address'}}
delete = {'old': {'MAC': 'fake_mac_old',
'logical_switch':
["uuid",
"fake_logical_switch_id_old"]}}
modify = {}
modify.update(add)
modify.update(delete)
fake_mac = 'fake_mac'
fake_logical_switch_id = 'fake_logical_switch_id'
fake_locator_id = 'fake_locator_id'
fake_ip_address = 'fake_ip_address'
data_dict = {'new_local_macs': [],
'modified_local_macs': [],
'deleted_local_macs': []}
with mock.patch.object(ovsdb_schema,
'UcastMacsLocal') as ucast_mac_local:
# test add
self.l2gw_ovsdb._process_ucast_macs_local(fake_id, add,
data_dict)
ucast_mac_local.assert_called_once_with(fake_id, fake_mac,
fake_logical_switch_id,
fake_locator_id,
fake_ip_address)
self.assertIn(ucast_mac_local.return_value,
data_dict['new_local_macs'])
# test modify
self.l2gw_ovsdb._process_ucast_macs_local(fake_id, modify,
data_dict)
self.assertIn(ucast_mac_local.return_value,
data_dict['modified_local_macs'])
# test delete
self.l2gw_ovsdb._process_ucast_macs_local(fake_id, delete,
data_dict)
self.assertIn(ucast_mac_local.return_value,
data_dict['deleted_local_macs'])
def test_process_ucast_macs_remote(self):
"""Test case to process new ucast_macs_remote."""
fake_id = 'fake_id'
add = {'new': {'locator': ["uuid", "fake_locator_id"],
'MAC': 'fake_mac',
'logical_switch':
["uuid",
"fake_logical_switch_id"],
'ipaddr': 'fake_ip_address'}}
delete = {'old': {'MAC': 'fake_mac_old',
'logical_switch':
["uuid", "fake_logical_switch_id_old"]}}
modify = {}
modify.update(add)
modify.update(delete)
fake_mac = 'fake_mac'
fake_logical_switch_id = 'fake_logical_switch_id'
fake_locator_id = 'fake_locator_id'
fake_ip_address = 'fake_ip_address'
data_dict = {'new_remote_macs': [],
'modified_remote_macs': [],
'deleted_remote_macs': []}
with mock.patch.object(ovsdb_schema,
'UcastMacsRemote') as ucast_mac_remote:
# test add
self.l2gw_ovsdb._process_ucast_macs_remote(fake_id,
add,
data_dict)
ucast_mac_remote.assert_called_once_with(fake_id, fake_mac,
fake_logical_switch_id,
fake_locator_id,
fake_ip_address)
self.assertIn(ucast_mac_remote.return_value,
data_dict['new_remote_macs'])
# test modify
self.l2gw_ovsdb._process_ucast_macs_remote(fake_id,
modify,
data_dict)
self.assertIn(ucast_mac_remote.return_value,
data_dict['modified_remote_macs'])
# test delete
self.l2gw_ovsdb._process_ucast_macs_remote(fake_id,
delete,
data_dict)
self.assertIn(ucast_mac_remote.return_value,
data_dict['deleted_remote_macs'])
def test_process_physical_locator(self):
"""Test case to process new physical locator."""
fake_id = 'fake_id'
add = {'new': {"dst_ip": "fake_dst_ip"}}
delete = {'old': {"dst_ip": "fake_dst_ip_old"}}
modify = {}
modify.update(add)
modify.update(delete)
data_dict = {'new_physical_locators': [],
'modified_physical_locators': [],
'deleted_physical_locators': []}
fake_dst_ip = 'fake_dst_ip'
with mock.patch.object(ovsdb_schema, 'PhysicalLocator') as phy_locator:
# test add
self.l2gw_ovsdb._process_physical_locator(fake_id, add,
data_dict)
phy_locator.assert_called_once_with(fake_id, fake_dst_ip)
self.assertIn(phy_locator.return_value,
data_dict['new_physical_locators'])
# test delete
self.l2gw_ovsdb._process_physical_locator(fake_id, delete,
data_dict)
self.assertIn(phy_locator.return_value,
data_dict['deleted_physical_locators'])
# test modify
self.l2gw_ovsdb._process_physical_locator(fake_id, modify,
data_dict)
self.assertIn(phy_locator.return_value,
data_dict['modified_physical_locators'])
def test_process_mcast_macs_local(self):
"""Test case to process new mcast_macs_local."""
fake_id = 'fake_id'
add = {'new': {'locator_set': ["uuid", "fake_locator_id"],
'MAC': 'fake_mac',
'logical_switch': ["uuid",
"fake_logical_switch_id"],
'ipaddr': 'fake_ip_address'}}
delete = {'old': {'locator': ["uuid", "fake_locator_id"],
'MAC': 'fake_mac',
'logical_switch': ["uuid",
"fake_logical_switch_id"],
'ipaddr': 'fake_ip_address'}}
fake_mac = 'fake_mac'
fake_logical_switch_id = 'fake_logical_switch_id'
fake_locator_id = 'fake_locator_id'
fake_ip_address = 'fake_ip_address'
data_dict = {'new_mlocal_macs': [],
'deleted_mlocal_macs': []}
with mock.patch.object(ovsdb_schema,
'McastMacsLocal') as mcast_mac_local:
# test add
self.l2gw_ovsdb._process_mcast_macs_local(fake_id,
add, data_dict)
mcast_mac_local.assert_called_once_with(fake_id, fake_mac,
fake_logical_switch_id,
fake_locator_id,
fake_ip_address)
self.assertIn(mcast_mac_local.return_value,
data_dict['new_mlocal_macs'])
# test delete
self.l2gw_ovsdb._process_mcast_macs_local(fake_id,
delete,
data_dict)
self.assertTrue(mcast_mac_local.called)
self.assertIn(mcast_mac_local.return_value,
data_dict['deleted_mlocal_macs'])
def test_process_physical_locator_set(self):
"""Test case to process new physical_locaor_set."""
fake_id = 'fake_id'
add = {'new': {'locators': ["set", [["uuid", "fake_id1"],
["uuid", "fake_id2"]]]
}}
delete = {'old': {'locators': ["set", [["uuid", "fake_id1"],
["uuid", "fake_id2"]]]
}}
fake_locators = ["fake_id1", "fake_id2"]
data_dict = {'new_locator_sets': [],
'deleted_locator_sets': []}
with mock.patch.object(ovsdb_schema,
'PhysicalLocatorSet') as phys_loc_set:
self.l2gw_ovsdb._process_physical_locator_set(fake_id,
add,
data_dict)
phys_loc_set.assert_called_once_with(fake_id, fake_locators)
PhysLocatorSet = phys_loc_set.return_value
self.assertIn(PhysLocatorSet,
data_dict['new_locator_sets'])
self.l2gw_ovsdb._process_physical_locator_set(fake_id,
delete,
data_dict)
self.assertTrue(phys_loc_set.called)
PhysLocatorSet = phys_loc_set.return_value
self.assertIn(PhysLocatorSet,
data_dict['deleted_locator_sets'])
class SocketClass(object):
def __init__(self,
connect_error=None,
send_error=None,
recv_error=None,
rcv_data=None,
sock=None,
ip_addr=None):
self.connect_error = connect_error
self.rcv_data = rcv_data
self.send_error = send_error
self.recv_error = recv_error
self.sock = sock
self.ip_addr = ip_addr
def connect(self, ip_port):
if self.connect_error:
raise self.connect_error
def send(self, data):
if self.send_error:
raise self.send_error
return len(data)
def recv(self, length):
if self.recv_error:
raise self.recv_error
return self.rcv_data
def bind(self, host_port):
pass
def listen(self, conn):
pass
def accept(self):
return self.sock, self.ip_addr
def setsockopt(self, sock_opt, sock_reuse, mode):
pass
class TestOVSDBMonitor_with_enable_manager(base.BaseTestCase):
def setUp(self):
super(TestOVSDBMonitor_with_enable_manager, self).setUp()
config.register_ovsdb_opts_helper(cfg.CONF)
cfg.CONF.set_override('enable_manager', True, 'ovsdb')
self.conf = mock.Mock()
self.callback = mock.Mock()
self.l2gw_ovsdb = ovsdb_monitor.OVSDBMonitor(mock.Mock(),
self.conf,
self.callback)
def test_init_with_enable_manager(self):
self.l2gw_ovsdb.__init__(mock.Mock(), self.conf,
self.callback)
self.assertTrue(self.l2gw_ovsdb.enable_manager)
self.assertFalse(self.l2gw_ovsdb.check_monitor_table_thread)
|