From 327f5d3af50a95d6f57b4fe24c0a2ad9f4daa3b3 Mon Sep 17 00:00:00 2001 From: Gary Kotton Date: Mon, 25 Jul 2016 02:03:34 -0700 Subject: [PATCH] Python 3: add support The patche does the following: 1. remove nested with calls from unit tests 2. updates tox.ini to enable us to use the \ at the end of a line 3. All integers in python 3 are long. 4. Python 3 does not support __builtin__. Removed it from the tests. Now the unit tests pass when running python 3. This also adds support to tox. Change-Id: Iacbf0c2c05248819af817109e7dd637f71c82404 --- .../agent/ovsdb/test_base_connection.py | 93 +- .../l2gateway/agent/ovsdb/test_manager.py | 194 ++-- .../agent/ovsdb/test_ovsdb_monitor.py | 137 ++- .../agent/ovsdb/test_ovsdb_writer.py | 310 +++---- .../l2gateway/agent/test_l2gw_agent.py | 17 +- .../l2gateway/common/test_tunnel_calls.py | 14 +- .../services/l2gateway/ovsdb/test_data.py | 451 +++++----- .../service_drivers/test_rpc_l2gw.py | 835 ++++++++---------- .../l2gateway/test_agent_scheduler.py | 85 +- .../unit/services/l2gateway/test_plugin.py | 189 ++-- tox.ini | 4 +- 11 files changed, 1046 insertions(+), 1283 deletions(-) diff --git a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_base_connection.py b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_base_connection.py index ee5462f2..6023d276 100755 --- a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_base_connection.py +++ b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_base_connection.py @@ -15,7 +15,6 @@ import eventlet -import contextlib import os.path import socket import ssl @@ -104,10 +103,9 @@ class TestBaseConnection(base.BaseTestCase): """Test case to test __init__.""" fakesocket = SocketClass() - with contextlib.nested( - mock.patch.object(base_connection.LOG, 'debug'), - mock.patch.object(socket, 'socket', return_value=fakesocket) - ) as(logger_call, mock_sock): + with mock.patch.object(base_connection.LOG, 'debug') as logger_call, \ + mock.patch.object(socket, 'socket', + return_value=fakesocket): self.l2gw_ovsdb.__init__(mock.Mock(), self.conf) self.assertTrue(self.l2gw_ovsdb.connected) self.assertTrue(logger_call.called) @@ -117,13 +115,10 @@ class TestBaseConnection(base.BaseTestCase): """Test case to test __init__ with socket error exception.""" fakesocket = SocketClass(socket.error) - with contextlib.nested( - mock.patch.object(base_connection.LOG, 'exception'), - mock.patch.object(base_connection.LOG, 'warning'), - mock.patch.object(socket, 'socket', return_value=fakesocket), - mock.patch.object(time, 'sleep') - ) as(logger_exc, logger_warn, - sock_connect, mock_sleep): + with mock.patch.object(base_connection.LOG, 'exception') as logger_exc, \ + mock.patch.object(base_connection.LOG, 'warning') as logger_warn, \ + mock.patch.object(socket, 'socket', return_value=fakesocket) as sock_connect, \ + mock.patch.object(time, 'sleep'): ovsdb_conf = FakeConf() self.assertRaises(socket.error, base_connection.BaseConnection, cfg.CONF.ovsdb, ovsdb_conf) @@ -135,13 +130,10 @@ class TestBaseConnection(base.BaseTestCase): """Test case to test __init__ with socket timeout exception.""" fakesocket = SocketClass(socket.timeout) - with contextlib.nested( - mock.patch.object(base_connection.LOG, 'exception'), - mock.patch.object(base_connection.LOG, 'warning'), - mock.patch.object(socket, 'socket', return_value=fakesocket), - mock.patch.object(time, 'sleep') - ) as(logger_exc, logger_warn, - sock_connect, mock_sleep): + with mock.patch.object(base_connection.LOG, 'exception') as logger_exc, \ + mock.patch.object(base_connection.LOG, 'warning') as logger_warn, \ + mock.patch.object(socket, 'socket', return_value=fakesocket) as sock_connect, \ + mock.patch.object(time, 'sleep'): ovsdb_conf = FakeConf() self.assertRaises(socket.timeout, base_connection.BaseConnection, cfg.CONF.ovsdb, ovsdb_conf) @@ -225,12 +217,11 @@ class TestBaseConnection_with_enable_manager(base.BaseTestCase): self.l2gw_ovsdb_conn.ovsdb_fd_states = {fake_ip: 'fake_status'} self.l2gw_ovsdb_conn.mgr.l2gw_agent_type = n_const.MONITOR self.l2gw_ovsdb_conn.ovsdb_conn_list = [fake_ip] - with contextlib.nested( - mock.patch.object(eventlet.greenthread, 'spawn_n', - side_effect=Exception), - mock.patch.object(base_connection.LOG, 'warning'), - mock.patch.object(self.l2gw_ovsdb_conn, 'disconnect')) as ( - mock_thread, mock_warning, mock_disconnect): + with mock.patch.object(eventlet.greenthread, 'spawn_n', + side_effect=Exception) as mock_thread, \ + mock.patch.object(base_connection.LOG, 'warning') as mock_warning, \ + mock.patch.object(self.l2gw_ovsdb_conn, + 'disconnect') as mock_disconnect: self.l2gw_ovsdb_conn._send_monitor_msg_to_ovsdb_connection( fake_ip) self.assertTrue(mock_thread.called) @@ -242,18 +233,13 @@ class TestBaseConnection_with_enable_manager(base.BaseTestCase): "params": "fake_params", "id": "fake_id", } - with contextlib.nested( - mock.patch.object(eventlet.greenthread, 'sleep'), - mock.patch.object(jsonutils, 'loads', return_value=fake_resp), - mock.patch.object(self.fakesocket, - 'recv', - return_value=fake_resp), - mock.patch.object(self.fakesocket, - 'send') - ) as ( - fake_thread, mock_loads, - mock_sock_rcv, - mock_sock_send): + with mock.patch.object(eventlet.greenthread, 'sleep') as fake_thread, \ + mock.patch.object(jsonutils, 'loads', return_value=fake_resp) as mock_loads, \ + mock.patch.object(self.fakesocket, + 'recv', + return_value=fake_resp) as mock_sock_rcv, \ + mock.patch.object(self.fakesocket, + 'send') as mock_sock_send: self.l2gw_ovsdb_conn._echo_response(self.fake_ip) self.assertTrue(fake_thread.called) self.assertTrue(mock_sock_rcv.called) @@ -262,16 +248,13 @@ class TestBaseConnection_with_enable_manager(base.BaseTestCase): self.assertTrue(mock_sock_send.called) def test_common_sock_rcv_thread_none(self): - with contextlib.nested( - mock.patch.object(base_connection.BaseConnection, - '_echo_response'), - mock.patch.object(eventlet.greenthread, 'sleep'), - mock.patch.object(self.fakesocket, - 'recv', return_value=None), - mock.patch.object(base_connection.BaseConnection, - 'disconnect')) as ( - mock_resp, green_thrd_sleep, - mock_rcv, mock_disconnect): + with mock.patch.object(base_connection.BaseConnection, + '_echo_response') as mock_resp, \ + mock.patch.object(eventlet.greenthread, 'sleep') as green_thrd_sleep, \ + mock.patch.object(self.fakesocket, + 'recv', return_value=None) as mock_rcv, \ + mock.patch.object(base_connection.BaseConnection, + 'disconnect') as mock_disconnect: self.l2gw_ovsdb_conn.check_c_sock = True self.l2gw_ovsdb_conn.read_on = True self.l2gw_ovsdb_conn._common_sock_rcv_thread(self.fake_ip) @@ -312,11 +295,9 @@ class TestBaseConnection_with_enable_manager(base.BaseTestCase): cfg.CONF.set_override('l2_gw_agent_ca_cert_base_path', '/home', 'ovsdb') - with contextlib.nested( - mock.patch.object(os.path, 'isfile', return_value=True), - mock.patch.object(base_connection.LOG, 'error'), - mock.patch.object(ssl, 'wrap_socket') - ) as (mock_isfile, mock_error, mock_wrap_sock): + with mock.patch.object(os.path, 'isfile', return_value=True) as mock_isfile, \ + mock.patch.object(base_connection.LOG, 'error') as mock_error, \ + mock.patch.object(ssl, 'wrap_socket') as mock_wrap_sock: self.l2gw_ovsdb_conn._is_ssl_configured('10.10.10.10', self.mock_sock) self.assertTrue(mock_isfile.called) @@ -341,11 +322,9 @@ class TestBaseConnection_with_enable_manager(base.BaseTestCase): cfg.CONF.set_override('l2_gw_agent_ca_cert_base_path', '/home/', 'ovsdb') - with contextlib.nested( - mock.patch.object(os.path, 'isfile', return_value=False), - mock.patch.object(base_connection.LOG, 'error'), - mock.patch.object(ssl, 'wrap_socket') - ) as (mock_isfile, mock_error, mock_wrap_sock): + with mock.patch.object(os.path, 'isfile', return_value=False) as mock_isfile, \ + mock.patch.object(base_connection.LOG, 'error') as mock_error, \ + mock.patch.object(ssl, 'wrap_socket') as mock_wrap_sock: self.l2gw_ovsdb_conn._is_ssl_configured('10.10.10.10', self.mock_sock) self.assertTrue(mock_isfile.called) diff --git a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_manager.py b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_manager.py index 80f04e53..1832faf3 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_manager.py +++ b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_manager.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import contextlib import socket import eventlet @@ -117,16 +116,14 @@ class TestManager(base.BaseTestCase): gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) ovsdb_ident = self.fake_config_json.get(n_const.OVSDB_IDENTIFIER) self.l2gw_agent_manager.gateways[ovsdb_ident] = gateway - with contextlib.nested( - mock.patch.object(ovsdb_monitor, - 'OVSDBMonitor'), - mock.patch.object(eventlet.greenthread, - 'spawn_n'), - mock.patch.object(manager.OVSDBManager, - 'agent_to_plugin_rpc'), - mock.patch.object(self.plugin_rpc, - 'notify_ovsdb_states') - ) as (ovsdb_connection, event_spawn, call_back, notify): + with mock.patch.object(ovsdb_monitor, + 'OVSDBMonitor') as ovsdb_connection, \ + mock.patch.object(eventlet.greenthread, + 'spawn_n') as event_spawn, \ + mock.patch.object(manager.OVSDBManager, + 'agent_to_plugin_rpc') as call_back, \ + mock.patch.object(self.plugin_rpc, + 'notify_ovsdb_states') as notify: self.l2gw_agent_manager._connect_to_ovsdb_server() self.assertTrue(event_spawn.called) self.assertTrue(ovsdb_connection.called) @@ -140,33 +137,28 @@ class TestManager(base.BaseTestCase): gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) ovsdb_ident = self.fake_config_json.get(n_const.OVSDB_IDENTIFIER) self.l2gw_agent_manager.gateways[ovsdb_ident] = gateway - with contextlib.nested( - mock.patch.object(ovsdb_monitor.OVSDBMonitor, - '__init__', - side_effect=socket.error - ), - mock.patch.object(eventlet.greenthread, - 'spawn_n'), - mock.patch.object(manager.LOG, 'error') - ) as (ovsdb_connection, event_spawn, mock_warn): - self.l2gw_agent_manager._connect_to_ovsdb_server() - event_spawn.assert_not_called() + with mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '__init__', + side_effect=socket.error + ), \ + mock.patch.object(eventlet.greenthread, + 'spawn_n') as event_spawn, \ + mock.patch.object(manager.LOG, 'error'): + self.l2gw_agent_manager._connect_to_ovsdb_server() + event_spawn.assert_not_called() def test_handle_report_state_failure(self): self.l2gw_agent_manager.l2gw_agent_type = n_const.MONITOR - with contextlib.nested( - mock.patch.object(self.l2gw_agent_manager, - '_disconnect_all_ovsdb_servers' - ), - mock.patch.object(agent_rpc.PluginReportStateAPI, - 'report_state', - side_effect=Exception), - mock.patch.object(base_agent_manager.LOG, - 'exception'), - mock.patch.object(self.l2gw_agent_manager, - '_stop_looping_task') - ) as (mock_disconnect_ovsdb_servers, mock_report_state, - mock_log, mock_stop_looping): + with mock.patch.object(self.l2gw_agent_manager, + '_disconnect_all_ovsdb_servers' + ) as mock_disconnect_ovsdb_servers, \ + mock.patch.object(agent_rpc.PluginReportStateAPI, + 'report_state', + side_effect=Exception), \ + mock.patch.object(base_agent_manager.LOG, + 'exception'), \ + mock.patch.object(self.l2gw_agent_manager, + '_stop_looping_task') as mock_stop_looping: self.l2gw_agent_manager._report_state() self.assertEqual(self.l2gw_agent_manager.l2gw_agent_type, '') @@ -210,26 +202,23 @@ class TestManager(base.BaseTestCase): self.l2gw_agent_manager.gateways = {} gateway = l2gateway_config.L2GatewayConfig(self.fake_config_json) self.l2gw_agent_manager.gateways['fake_ovsdb_identifier'] = gateway - with contextlib.nested( - mock.patch.object(manager.LOG, 'warning'), - mock.patch.object(socket.socket, 'connect'), - mock.patch.object(ovsdb_writer.OVSDBWriter, - 'delete_logical_switch') - ) as (logger_call, mock_connect, mock_del_ls): - mock_connect.side_effect = socket.error - self.l2gw_agent_manager.delete_network(self.context, - mock.Mock(), - mock.Mock()) - self.assertEqual(1, logger_call.call_count) - self.assertFalse(mock_del_ls.called) + with mock.patch.object(manager.LOG, 'warning') as logger_call, \ + mock.patch.object(socket.socket, 'connect') as mock_connect, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + 'delete_logical_switch') as mock_del_ls: + mock_connect.side_effect = socket.error + self.l2gw_agent_manager.delete_network(self.context, + mock.Mock(), + mock.Mock()) + self.assertEqual(1, logger_call.call_count) + self.assertFalse(mock_del_ls.called) def test_init_with_enable_manager(self): cfg.CONF.set_override('enable_manager', True, 'ovsdb') - with contextlib.nested( - mock.patch.object(manager.OVSDBManager, - '_sock_open_connection'), - mock.patch.object(loopingcall, 'FixedIntervalLoopingCall')) as ( - mock_sock_open_conn, mock_loop): + with mock.patch.object(manager.OVSDBManager, + '_sock_open_connection') as mock_sock_open_conn, \ + mock.patch.object(loopingcall, + 'FixedIntervalLoopingCall') as mock_loop: self.l2gw_agent_manager.__init__() self.assertTrue(mock_sock_open_conn.called) self.assertTrue(mock_loop.called) @@ -259,14 +248,13 @@ class TestManager(base.BaseTestCase): self.l2gw_agent_manager.conf.host = 'fake_host' self.l2gw_agent_manager.__init__() self.l2gw_agent_manager.l2gw_agent_type = n_const.MONITOR - with contextlib.nested( - mock.patch.object(ovsdb_common_class, - 'OVSDB_commom_class'), - mock.patch.object(eventlet.greenthread, - 'spawn_n'), - mock.patch.object(self.l2gw_agent_manager, - '_start_looping_task_ovsdb_states')) as ( - mock_ovsdb_common, mock_thread, mock_looping): + with mock.patch.object(ovsdb_common_class, + 'OVSDB_commom_class') as mock_ovsdb_common, \ + mock.patch.object(eventlet.greenthread, + 'spawn_n') as mock_thread, \ + mock.patch.object( + self.l2gw_agent_manager, + '_start_looping_task_ovsdb_states') as mock_looping: self.l2gw_agent_manager.ovsdb_fd = mock_ovsdb_common.return_value self.l2gw_agent_manager.ovsdb_fd.check_monitor_table_thread = False self.l2gw_agent_manager.ovsdb_fd.check_sock_rcv = True @@ -306,11 +294,9 @@ class TestManager(base.BaseTestCase): self.l2gw_agent_manager.__init__() self.l2gw_agent_manager.l2gw_agent_type = '' fake_op_method = 'CREATE' - with contextlib.nested( - mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class'), - mock.patch.object(manager.OVSDBManager, - '_sock_open_connection')) as ( - mock_ovsdb_common, mock_open_conn): + with mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class') as mock_ovsdb_common, \ + mock.patch.object(manager.OVSDBManager, + '_sock_open_connection') as mock_open_conn: self.l2gw_agent_manager.ovsdb_fd = mock_ovsdb_common.return_value self.l2gw_agent_manager.ovsdb_fd.ovsdb_conn_list = ['fake_ip'] self.l2gw_agent_manager.update_connection_to_gateway( @@ -329,11 +315,10 @@ class TestManager(base.BaseTestCase): cfg.CONF.set_override('enable_manager', False, 'ovsdb') self.l2gw_agent_manager.__init__() fake_op_method = 'CREATE' - with contextlib.nested( - mock.patch.object(self.l2gw_agent_manager, - '_is_valid_request', return_value=True), - mock.patch.object(ovsdb_writer, 'OVSDBWriter' - )) as (mock_valid_req, mock_ovsdb_fd): + with mock.patch.object(self.l2gw_agent_manager, + '_is_valid_request', return_value=True) as mock_valid_req, \ + mock.patch.object(ovsdb_writer, 'OVSDBWriter' + ) as mock_ovsdb_fd: self.l2gw_agent_manager.update_connection_to_gateway( self.context, 'fake_ovsdb_id', "fake_logical_switch_dict", "fake_locator_dicts", "fake_mac_dicts", "fake_port_dicts", @@ -364,11 +349,10 @@ class TestManager(base.BaseTestCase): """Test case to test delete_network with enable_manager=False.""" cfg.CONF.set_override('enable_manager', False, 'ovsdb') self.l2gw_agent_manager.__init__() - with contextlib.nested( - mock.patch.object(self.l2gw_agent_manager, - '_is_valid_request', return_value=True), - mock.patch.object(ovsdb_writer, 'OVSDBWriter' - )) as (mock_valid_req, mock_ovsdb_fd): + with mock.patch.object(self.l2gw_agent_manager, + '_is_valid_request', return_value=True) as mock_valid_req, \ + mock.patch.object(ovsdb_writer, 'OVSDBWriter' + ) as mock_ovsdb_fd: self.l2gw_agent_manager.delete_network( self.context, 'fake_ovsdb_id', "fake_logical_switch_uuid") ovsdb_sock_fd = mock_ovsdb_fd.return_value @@ -381,11 +365,9 @@ class TestManager(base.BaseTestCase): cfg.CONF.set_override('enable_manager', True, 'ovsdb') self.l2gw_agent_manager.__init__() self.l2gw_agent_manager.l2gw_agent_type = '' - with contextlib.nested( - mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class'), - mock.patch.object(manager.OVSDBManager, - '_sock_open_connection')) as ( - mock_ovsdb_common, mock_open_conn): + with mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class') as mock_ovsdb_common, \ + mock.patch.object(manager.OVSDBManager, + '_sock_open_connection') as mock_open_conn: self.l2gw_agent_manager.ovsdb_fd = mock_ovsdb_common.return_value self.l2gw_agent_manager.ovsdb_fd.ovsdb_conn_list = ['fake_ip'] self.l2gw_agent_manager.delete_network( @@ -415,11 +397,9 @@ class TestManager(base.BaseTestCase): cfg.CONF.set_override('enable_manager', True, 'ovsdb') self.l2gw_agent_manager.__init__() self.l2gw_agent_manager.l2gw_agent_type = '' - with contextlib.nested( - mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class'), - mock.patch.object(manager.OVSDBManager, - '_sock_open_connection')) as ( - mock_ovsdb_common, mock_open_conn): + with mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class') as mock_ovsdb_common, \ + mock.patch.object(manager.OVSDBManager, + '_sock_open_connection') as mock_open_conn: self.l2gw_agent_manager.ovsdb_fd = mock_ovsdb_common.return_value self.l2gw_agent_manager.ovsdb_fd.check_c_sock = True self.l2gw_agent_manager.ovsdb_fd.ovsdb_conn_list = ['fake_ip'] @@ -439,11 +419,11 @@ class TestManager(base.BaseTestCase): """ cfg.CONF.set_override('enable_manager', False, 'ovsdb') self.l2gw_agent_manager.__init__() - with contextlib.nested( - mock.patch.object(self.l2gw_agent_manager, - '_is_valid_request', return_value=True), - mock.patch.object(ovsdb_writer, 'OVSDBWriter' - )) as (mock_valid_req, mock_ovsdb_fd): + with mock.patch.object(self.l2gw_agent_manager, + '_is_valid_request', + return_value=True) as mock_valid_req, \ + mock.patch.object(ovsdb_writer, 'OVSDBWriter' + ) as mock_ovsdb_fd: self.l2gw_agent_manager.add_vif_to_gateway( self.context, 'fake_ovsdb_id', "fake_logical_switch_dict", "fake_locator_dict", "fake_mac_dict") @@ -475,11 +455,9 @@ class TestManager(base.BaseTestCase): cfg.CONF.set_override('enable_manager', True, 'ovsdb') self.l2gw_agent_manager.__init__() self.l2gw_agent_manager.l2gw_agent_type = '' - with contextlib.nested( - mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class'), - mock.patch.object(manager.OVSDBManager, - '_sock_open_connection')) as ( - mock_ovsdb_common, mock_open_conn): + with mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class') as mock_ovsdb_common, \ + mock.patch.object(manager.OVSDBManager, + '_sock_open_connection') as mock_open_conn: self.l2gw_agent_manager.ovsdb_fd = mock_ovsdb_common.return_value self.l2gw_agent_manager.ovsdb_fd.ovsdb_conn_list = ['fake_ip'] self.l2gw_agent_manager.delete_vif_from_gateway( @@ -497,11 +475,10 @@ class TestManager(base.BaseTestCase): """ cfg.CONF.set_override('enable_manager', False, 'ovsdb') self.l2gw_agent_manager.__init__() - with contextlib.nested( - mock.patch.object(self.l2gw_agent_manager, - '_is_valid_request', return_value=True), - mock.patch.object(ovsdb_writer, 'OVSDBWriter' - )) as (mock_valid_req, mock_ovsdb_fd): + with mock.patch.object(self.l2gw_agent_manager, + '_is_valid_request', return_value=True) as mock_valid_req, \ + mock.patch.object(ovsdb_writer, 'OVSDBWriter' + ) as mock_ovsdb_fd: self.l2gw_agent_manager.delete_vif_from_gateway( self.context, 'fake_ovsdb_id', "fake_logical_switch_uuid", "fake_mac") @@ -535,11 +512,9 @@ class TestManager(base.BaseTestCase): cfg.CONF.set_override('enable_manager', True, 'ovsdb') self.l2gw_agent_manager.__init__() self.l2gw_agent_manager.l2gw_agent_type = '' - with contextlib.nested( - mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class'), - mock.patch.object(manager.OVSDBManager, - '_sock_open_connection')) as ( - mock_ovsdb_common, mock_open_conn): + with mock.patch.object(ovsdb_common_class, 'OVSDB_commom_class') as mock_ovsdb_common, \ + mock.patch.object(manager.OVSDBManager, + '_sock_open_connection') as mock_open_conn: self.l2gw_agent_manager.ovsdb_fd = mock_ovsdb_common.return_value self.l2gw_agent_manager.ovsdb_fd.ovsdb_conn_list = ['fake_ip'] self.l2gw_agent_manager.update_vif_to_gateway( @@ -557,11 +532,10 @@ class TestManager(base.BaseTestCase): """ cfg.CONF.set_override('enable_manager', False, 'ovsdb') self.l2gw_agent_manager.__init__() - with contextlib.nested( - mock.patch.object(self.l2gw_agent_manager, - '_is_valid_request', return_value=True), - mock.patch.object(ovsdb_writer, 'OVSDBWriter' - )) as (mock_valid_req, mock_ovsdb_fd): + with mock.patch.object(self.l2gw_agent_manager, + '_is_valid_request', return_value=True) as mock_valid_req, \ + mock.patch.object(ovsdb_writer, 'OVSDBWriter' + ) as mock_ovsdb_fd: self.l2gw_agent_manager.update_vif_to_gateway( self.context, 'fake_ovsdb_id', "fake_locator_dict", "fake_mac_dict") diff --git a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_monitor.py b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_monitor.py index f6c2aa18..34a35359 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_monitor.py +++ b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_monitor.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import contextlib import socket import ssl @@ -91,11 +90,10 @@ class TestOVSDBMonitor(base.BaseTestCase): """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): + with mock.patch.object(ovsdb_monitor.LOG, 'debug'), \ + mock.patch.object(eventlet.greenthread, 'spawn') as gt, \ + mock.patch.object(socket, 'socket', + return_value=fakesocket): self.l2gw_ovsdb.__init__(mock.Mock(), self.conf, self.callback) self.assertTrue(self.l2gw_ovsdb.connected) @@ -122,19 +120,16 @@ class TestOVSDBMonitor(base.BaseTestCase): 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): + with mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_set_handler') as set_handler, \ + mock.patch.object(ovsdb_monitor.OVSDBMonitor, + 'send', return_value=True) as send, \ + mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_process_response', + return_value=(mock.ANY, False)) as process_resp, \ + mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_process_monitor_msg') as process_monitor_msg, \ + mock.patch.object(ovsdb_monitor.LOG, 'warning'): self.l2gw_ovsdb.set_monitor_response_handler() self.assertTrue(set_handler.called) self.assertTrue(send.called) @@ -144,19 +139,17 @@ class TestOVSDBMonitor(base.BaseTestCase): 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): + with mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_set_handler') as set_handler, \ + mock.patch.object(ovsdb_monitor.OVSDBMonitor, + 'send', return_value=False) as send, \ + mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_process_response', + return_value=(mock.ANY, True)) as process_resp, \ + mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_process_monitor_msg') as process_monitor_msg, \ + mock.patch.object(ovsdb_monitor.LOG, + 'warning'): self.l2gw_ovsdb.set_monitor_response_handler() self.assertTrue(set_handler.called) self.assertTrue(send.called) @@ -173,42 +166,34 @@ class TestOVSDBMonitor(base.BaseTestCase): 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'), + with mock.patch.object(ovsdb_monitor.OVSDBMonitor, + '_process_physical_port') as proc_phy_port, \ mock.patch.object(ovsdb_monitor.OVSDBMonitor, - '_process_logical_switch'), + '_process_physical_switch') as proc_phy_switch, \ mock.patch.object(ovsdb_monitor.OVSDBMonitor, - '_process_ucast_macs_local'), + '_process_logical_switch') as proc_logic_switch, \ mock.patch.object(ovsdb_monitor.OVSDBMonitor, - '_process_physical_locator'), + '_process_ucast_macs_local') as proc_ucast_mac, \ mock.patch.object(ovsdb_monitor.OVSDBMonitor, - '_process_ucast_macs_remote'), + '_process_physical_locator') as proc_phy_loc, \ mock.patch.object(ovsdb_monitor.OVSDBMonitor, - '_process_mcast_macs_local'), + '_process_ucast_macs_remote') as proc_ucast_mac_remote, \ 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) + '_process_mcast_macs_local') as proc_mcast_mac_local, \ + mock.patch.object( + ovsdb_monitor.OVSDBMonitor, + '_process_physical_locator_set') as 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.""" @@ -273,21 +258,19 @@ class TestOVSDBMonitor(base.BaseTestCase): 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) + with mock.patch.object(self.l2gw_ovsdb.socket, 'recv', + side_effect=Exception, + return_value=None) as sock_recv, \ + mock.patch.object(self.l2gw_ovsdb.socket, + 'close') as sock_close, \ + mock.patch.object(ovsdb_monitor.LOG, + 'exception') as 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() diff --git a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_writer.py b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_writer.py index 3a0866ea..27341c86 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_writer.py +++ b/networking_l2gw/tests/unit/services/l2gateway/agent/ovsdb/test_ovsdb_writer.py @@ -13,7 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import contextlib import random import socket import ssl @@ -112,17 +111,16 @@ class TestOVSDBWriter(base.BaseTestCase): """Test case to test _get_reply.""" ret_value = jsonutils.dumps({self.op_id: 'foo_value'}) - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_recv_data', - return_value=jsonutils.dumps({self.op_id: - 'foo_value'})), + with mock.patch.object( + ovsdb_writer.OVSDBWriter, + '_recv_data', + return_value=jsonutils.dumps({self.op_id: + 'foo_value'})) as recv_data, \ mock.patch.object(ovsdb_writer.OVSDBWriter, '_process_response', - return_value=(ret_value, None)), + return_value=(ret_value, None)) as proc_response, \ mock.patch.object(ovsdb_writer.LOG, - 'debug') - ) as (recv_data, proc_response, debug): + 'debug'): self.l2gw_ovsdb._get_reply(self.op_id, mock.ANY) self.assertTrue(recv_data.called) self.assertTrue(proc_response.called) @@ -180,69 +178,53 @@ class TestOVSDBWriter(base.BaseTestCase): def test_insert_ucast_macs_remote(self): """Test case to test insert ucast_macs_remote.""" - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_ucast_macs_remote_dict'), - mock.patch.object(random, - 'getrandbits', - return_value=self.op_id - ), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_send_and_receive' - ), - mock.patch.object(ovsdb_writer.LOG, - 'debug'), - mock.patch.object(ovsdb_schema, 'LogicalSwitch'), - mock.patch.object(ovsdb_schema, 'PhysicalLocator'), - mock.patch.object(ovsdb_schema, 'UcastMacsRemote'), - ) as (get_ucast_mac_remote, - get_rand, - send_n_receive, - mock_log, - mock_ls, - mock_pl, - mock_ucmr): - self.l2gw_ovsdb.insert_ucast_macs_remote(mock.MagicMock(), - mock.MagicMock(), - mock.MagicMock(), - mock.ANY) - get_rand.assert_called_with(128) - send_n_receive.assert_called_with(mock.ANY, - self.op_id, mock.ANY, True) + with mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_ucast_macs_remote_dict') as get_ucast_mac_remote, \ + mock.patch.object(random, + 'getrandbits', + return_value=self.op_id + ) as get_rand, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_send_and_receive' + ) as send_n_receive, \ + mock.patch.object(ovsdb_writer.LOG, + 'debug'), \ + mock.patch.object(ovsdb_schema, 'LogicalSwitch'), \ + mock.patch.object(ovsdb_schema, 'PhysicalLocator'), \ + mock.patch.object(ovsdb_schema, 'UcastMacsRemote'): + self.l2gw_ovsdb.insert_ucast_macs_remote(mock.MagicMock(), + mock.MagicMock(), + mock.MagicMock(), + mock.ANY) + get_rand.assert_called_with(128) + send_n_receive.assert_called_with(mock.ANY, + self.op_id, mock.ANY, True) - self.assertTrue(get_ucast_mac_remote.called) + self.assertTrue(get_ucast_mac_remote.called) def test_insert_ucast_macs_remote_with_no_locator_id(self): """Test case to test insert ucast_macs_remote without locator_id and logical_switch_id. """ - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_ucast_macs_remote_dict'), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_physical_locator_dict'), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_logical_switch_dict'), - mock.patch.object(random, - 'getrandbits', - return_value=self.op_id - ), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_send_and_receive' - ), - mock.patch.object(ovsdb_writer.LOG, - 'debug'), - mock.patch.object(ovsdb_schema, 'LogicalSwitch'), - mock.patch.object(ovsdb_schema, 'PhysicalLocator'), - mock.patch.object(ovsdb_schema, 'UcastMacsRemote'), - ) as (get_ucast_mac_remote, - get_physical_locator_dict, - get_logical_switch_dict, - get_rand, - send_n_receive, - mock_log, - mock_ls, mock_pl, mock_ucmr): + with mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_ucast_macs_remote_dict') as get_ucast_mac_remote, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_physical_locator_dict') as get_physical_locator_dict, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_logical_switch_dict') as get_logical_switch_dict, \ + mock.patch.object(random, + 'getrandbits', + return_value=self.op_id + ), \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_send_and_receive' + ), \ + mock.patch.object(ovsdb_writer.LOG, + 'debug'), \ + mock.patch.object(ovsdb_schema, 'LogicalSwitch') as mock_ls, \ + mock.patch.object(ovsdb_schema, 'PhysicalLocator') as mock_pl, \ + mock.patch.object(ovsdb_schema, 'UcastMacsRemote'): locator = mock_pl.return_value locator.uuid = None ls = mock_ls.return_value @@ -258,62 +240,48 @@ class TestOVSDBWriter(base.BaseTestCase): def test_update_ucast_macs_remote(self): """Test case to test update ucast_macs_remote.""" - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_dict_for_update_ucast_mac_remote'), - mock.patch.object(random, - 'getrandbits', - return_value=self.op_id - ), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_send_and_receive' - ), - mock.patch.object(ovsdb_writer.LOG, - 'debug'), - mock.patch.object(ovsdb_schema, 'PhysicalLocator'), - mock.patch.object(ovsdb_schema, 'UcastMacsRemote'), - ) as (get_update_ucast_mac_remote, - get_rand, - send_n_receive, - mock_log, - mock_pl, - mock_ucmr): - self.l2gw_ovsdb.update_ucast_macs_remote(mock.MagicMock(), - mock.MagicMock(), - mock.ANY) - get_rand.assert_called_with(128) - send_n_receive.assert_called_with(mock.ANY, - self.op_id, mock.ANY, True) + with mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_dict_for_update_ucast_mac_remote') as get_update_ucast_mac_remote, \ + mock.patch.object(random, + 'getrandbits', + return_value=self.op_id + ) as get_rand, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_send_and_receive' + ) as send_n_receive, \ + mock.patch.object(ovsdb_writer.LOG, + 'debug'), \ + mock.patch.object(ovsdb_schema, 'PhysicalLocator'), \ + mock.patch.object(ovsdb_schema, 'UcastMacsRemote'): + self.l2gw_ovsdb.update_ucast_macs_remote(mock.MagicMock(), + mock.MagicMock(), + mock.ANY) + get_rand.assert_called_with(128) + send_n_receive.assert_called_with(mock.ANY, + self.op_id, mock.ANY, True) - self.assertTrue(get_update_ucast_mac_remote.called) + self.assertTrue(get_update_ucast_mac_remote.called) def test_update_ucast_macs_remote_with_no_locator_id(self): """Test case to test update ucast_macs_remote without locator_id and logical_switch_id. """ - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_dict_for_update_ucast_mac_remote'), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_physical_locator_dict'), - mock.patch.object(random, - 'getrandbits', - return_value=self.op_id - ), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_send_and_receive' - ), - mock.patch.object(ovsdb_writer.LOG, - 'debug'), - mock.patch.object(ovsdb_schema, 'PhysicalLocator'), - mock.patch.object(ovsdb_schema, 'UcastMacsRemote'), - ) as (get_update_ucast_mac_remote, - get_physical_locator_dict, - get_rand, - send_n_receive, - mock_log, - mock_pl, mock_ucmr): + with mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_dict_for_update_ucast_mac_remote') as get_update_ucast_mac_remote, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_physical_locator_dict') as get_physical_locator_dict, \ + mock.patch.object(random, + 'getrandbits', + return_value=self.op_id + ), \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_send_and_receive' + ), \ + mock.patch.object(ovsdb_writer.LOG, + 'debug'), \ + mock.patch.object(ovsdb_schema, 'PhysicalLocator') as mock_pl, \ + mock.patch.object(ovsdb_schema, 'UcastMacsRemote'): locator = mock_pl.return_value locator.uuid = None self.l2gw_ovsdb.update_ucast_macs_remote(mock.MagicMock(), @@ -324,68 +292,56 @@ class TestOVSDBWriter(base.BaseTestCase): def test_delete_ucast_macs_remote(self): """Test case to test delete_ucast_macs_remote.""" - with contextlib.nested( - mock.patch.object(random, - 'getrandbits', - return_value=self.op_id - ), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_send_and_receive' - ), - mock.patch.object(ovsdb_writer.LOG, - 'debug') - ) as (get_rand, - send_n_receive, - mock_log): - self.l2gw_ovsdb.delete_ucast_macs_remote(mock.Mock(), - mock.MagicMock(), - mock.ANY) - get_rand.assert_called_with(128) - send_n_receive.assert_called_with(mock.ANY, - self.op_id, mock.ANY, True) + with mock.patch.object(random, + 'getrandbits', + return_value=self.op_id + ) as get_rand, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_send_and_receive' + ) as send_n_receive, \ + mock.patch.object(ovsdb_writer.LOG, + 'debug'): + self.l2gw_ovsdb.delete_ucast_macs_remote(mock.Mock(), + mock.MagicMock(), + mock.ANY) + get_rand.assert_called_with(128) + send_n_receive.assert_called_with(mock.ANY, + self.op_id, mock.ANY, True) def test_update_connection_to_gateway(self): """Test case to test update_connection_to_gateway.""" - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_get_bindings_to_update'), - mock.patch.object(random, - 'getrandbits', - return_value=self.op_id - ), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_send_and_receive' - ), - mock.patch.object(ovsdb_writer.LOG, - 'debug') - ) as (get_bindings, - get_rand, - send_n_receive, - mock_log): - self.l2gw_ovsdb.update_connection_to_gateway( - mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock(), - mock.ANY, mock.ANY) - get_rand.assert_called_with(128) - send_n_receive.assert_called_with(mock.ANY, - self.op_id, mock.ANY, True) - self.assertTrue(get_bindings.called) + with mock.patch.object(ovsdb_writer.OVSDBWriter, + '_get_bindings_to_update') as get_bindings, \ + mock.patch.object(random, + 'getrandbits', + return_value=self.op_id + ) as get_rand, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_send_and_receive' + ) as send_n_receive, \ + mock.patch.object(ovsdb_writer.LOG, + 'debug'): + self.l2gw_ovsdb.update_connection_to_gateway( + mock.Mock(), mock.Mock(), mock.Mock(), mock.Mock(), + mock.ANY, mock.ANY) + get_rand.assert_called_with(128) + send_n_receive.assert_called_with(mock.ANY, + self.op_id, mock.ANY, True) + self.assertTrue(get_bindings.called) def test_get_bindings_to_update1(self): """Test case to test _get_bindings_to_update.""" fake_op_method = 'CREATE' - with contextlib.nested( - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_form_logical_switch'), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_form_physical_locators'), - mock.patch.object(ovsdb_writer.OVSDBWriter, - '_form_ports'), - mock.patch.object(ovsdb_schema, 'LogicalSwitch'), - mock.patch.object(ovsdb_schema, 'PhysicalLocator'), - mock.patch.object(ovsdb_schema, 'UcastMacsRemote'), - mock.patch.object(ovsdb_schema, 'PhysicalPort') - ) as (form_ls, form_pl, form_pp, - mock_ls, mock_pl, mock_ucmr, mock_pp): + with mock.patch.object(ovsdb_writer.OVSDBWriter, + '_form_logical_switch') as form_ls, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_form_physical_locators') as form_pl, \ + mock.patch.object(ovsdb_writer.OVSDBWriter, + '_form_ports') as form_pp, \ + mock.patch.object(ovsdb_schema, 'LogicalSwitch') as mock_ls, \ + mock.patch.object(ovsdb_schema, 'PhysicalLocator') as mock_pl, \ + mock.patch.object(ovsdb_schema, 'UcastMacsRemote') as mock_ucmr, \ + mock.patch.object(ovsdb_schema, 'PhysicalPort') as mock_pp: ls = mock_ls.return_value = ovsdb_schema.LogicalSwitch( 'ls_uuid', 'ls_name', 'ls_key', 'ls_desc') pl = mock_pl.return_value = ovsdb_schema.PhysicalLocator( @@ -492,11 +448,9 @@ class TestOVSDBWriter(base.BaseTestCase): None, None, '') - with contextlib.nested( - mock.patch.object(socket, 'socket', return_value=fake_socket - ), - mock.patch.object(ovsdb_writer.LOG, 'warning') - ) as (fake_sock, fake_warn): + with mock.patch.object(socket, 'socket', + return_value=fake_socket): + with mock.patch.object(ovsdb_writer.LOG, 'warning'): ovsdb_conf = base_test.FakeConf() l2gw_obj = ovsdb_writer.OVSDBWriter( cfg.CONF.ovsdb, ovsdb_conf) @@ -509,11 +463,9 @@ class TestOVSDBWriter(base.BaseTestCase): fake_socket = base_test.SocketClass(None, None, socket.error) - with contextlib.nested( - mock.patch.object(socket, 'socket', return_value=fake_socket - ), - mock.patch.object(ovsdb_writer.LOG, 'warning') - ) as (fake_sock, fake_warn): + with mock.patch.object(socket, 'socket', return_value=fake_socket): + with mock.patch.object(ovsdb_writer.LOG, + 'warning') as fake_warn: ovsdb_conf = base_test.FakeConf() l2gw_obj = ovsdb_writer.OVSDBWriter( cfg.CONF.ovsdb, ovsdb_conf) diff --git a/networking_l2gw/tests/unit/services/l2gateway/agent/test_l2gw_agent.py b/networking_l2gw/tests/unit/services/l2gateway/agent/test_l2gw_agent.py index b7d8f7fb..e1f6640d 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/agent/test_l2gw_agent.py +++ b/networking_l2gw/tests/unit/services/l2gateway/agent/test_l2gw_agent.py @@ -13,8 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import contextlib - import mock from neutron.common import config as common_config @@ -45,14 +43,11 @@ class TestL2gwAgent(base.BaseTestCase): def test_main_l2gw_agent(self): logging_str = 'neutron.agent.common.config.setup_logging' common_config_str = mock.patch.object(common_config, 'init').start() - with contextlib.nested( - mock.patch.object(common_config_str, 'init'), - mock.patch(logging_str), - mock.patch.object(agent.service, 'launch'), - mock.patch('sys.argv'), - mock.patch.object(agent.manager, 'OVSDBManager'), - mock.patch.object(cfg.CONF, 'register_opts') - ) as (mock_config, mock_logging, mock_launch, sys_argv, - mgr_cls, ro): + with mock.patch.object(common_config_str, 'init'), \ + mock.patch(logging_str), \ + mock.patch.object(agent.service, 'launch') as mock_launch, \ + mock.patch('sys.argv'), \ + mock.patch.object(agent.manager, 'OVSDBManager'), \ + mock.patch.object(cfg.CONF, 'register_opts'): agent.main() mock_launch.assert_called_once_with(cfg.CONF, mock.ANY) diff --git a/networking_l2gw/tests/unit/services/l2gateway/common/test_tunnel_calls.py b/networking_l2gw/tests/unit/services/l2gateway/common/test_tunnel_calls.py index 756630f6..67c51f52 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/common/test_tunnel_calls.py +++ b/networking_l2gw/tests/unit/services/l2gateway/common/test_tunnel_calls.py @@ -15,7 +15,6 @@ import mock -import contextlib from neutron.plugins.ml2.drivers.l2pop import rpc as l2pop_rpc from neutron.plugins.ml2.drivers import type_tunnel from neutron.plugins.ml2 import managers @@ -34,13 +33,12 @@ class TestTunnelCalls(base.BaseTestCase): self.context = mock.MagicMock() def test_trigger_tunnel_sync(self): - with contextlib.nested( - mock.patch.object(rpc, 'RpcCallbacks'), - mock.patch.object(type_tunnel.TunnelRpcCallbackMixin, - 'tunnel_sync')) as (mock_rpc, mock_tunnel_sync): - self.tunnel_call.trigger_tunnel_sync(self.context, 'fake_ip') - mock_tunnel_sync.assert_called_with( - self.context, tunnel_ip='fake_ip', tunnel_type='vxlan') + with mock.patch.object(rpc, 'RpcCallbacks'): + with mock.patch.object(type_tunnel.TunnelRpcCallbackMixin, + 'tunnel_sync') as mock_tunnel_sync: + self.tunnel_call.trigger_tunnel_sync(self.context, 'fake_ip') + mock_tunnel_sync.assert_called_with( + self.context, tunnel_ip='fake_ip', tunnel_type='vxlan') def test_trigger_l2pop_sync(self): fake_fdb_entry = "fake_fdb_entry" diff --git a/networking_l2gw/tests/unit/services/l2gateway/ovsdb/test_data.py b/networking_l2gw/tests/unit/services/l2gateway/ovsdb/test_data.py index b502851c..6b670f00 100755 --- a/networking_l2gw/tests/unit/services/l2gateway/ovsdb/test_data.py +++ b/networking_l2gw/tests/unit/services/l2gateway/ovsdb/test_data.py @@ -15,7 +15,6 @@ import mock -import contextlib from neutron import context from neutron import manager from neutron.plugins.ml2 import managers @@ -123,52 +122,36 @@ class TestOVSDBData(base.BaseTestCase): 'deleted_physical_locators': fake_deleted_physical_locators, 'deleted_local_macs': fake_deleted_local_macs, 'deleted_remote_macs': fake_deleted_remote_macs} - with contextlib.nested( + with mock.patch.object(self.ovsdb_data, + '_process_new_logical_switches') as process_new_logical_switches, \ mock.patch.object(self.ovsdb_data, - '_process_new_logical_switches'), + '_process_new_physical_ports') as process_new_physical_ports, \ mock.patch.object(self.ovsdb_data, - '_process_new_physical_ports'), + '_process_new_physical_switches') as process_new_physical_switches, \ mock.patch.object(self.ovsdb_data, - '_process_new_physical_switches'), + '_process_new_physical_locators') as process_new_physical_locators, \ mock.patch.object(self.ovsdb_data, - '_process_new_physical_locators'), + '_process_new_local_macs') as process_new_local_macs, \ mock.patch.object(self.ovsdb_data, - '_process_new_local_macs'), + '_process_new_remote_macs') as process_new_remote_macs, \ mock.patch.object(self.ovsdb_data, - '_process_new_remote_macs'), + '_process_modified_remote_macs') as process_modified_remote_macs, \ mock.patch.object(self.ovsdb_data, - '_process_modified_remote_macs'), + '_process_modified_physical_ports') as process_modified_physical_ports, \ mock.patch.object(self.ovsdb_data, - '_process_modified_physical_ports'), + '_process_deleted_logical_switches') as process_deleted_logical_switches, \ mock.patch.object(self.ovsdb_data, - '_process_deleted_logical_switches'), + '_process_deleted_physical_switches') as process_deleted_physical_switches, \ mock.patch.object(self.ovsdb_data, - '_process_deleted_physical_switches'), + '_process_deleted_physical_ports') as process_deleted_physical_ports, \ mock.patch.object(self.ovsdb_data, - '_process_deleted_physical_ports'), + '_process_deleted_physical_locators') as process_deleted_physical_locators, \ mock.patch.object(self.ovsdb_data, - '_process_deleted_physical_locators'), + '_process_deleted_local_macs') as process_deleted_local_macs, \ mock.patch.object(self.ovsdb_data, - '_process_deleted_local_macs'), + '_process_deleted_remote_macs') as process_deleted_remote_macs, \ mock.patch.object(self.ovsdb_data, - '_process_deleted_remote_macs'), - mock.patch.object(self.ovsdb_data, - '_handle_l2pop') - ) as (process_new_logical_switches, - process_new_physical_ports, - process_new_physical_switches, - process_new_physical_locators, - process_new_local_macs, - process_new_remote_macs, - process_modified_remote_macs, - process_modified_physical_ports, - process_deleted_logical_switches, - process_deleted_physical_switches, - process_deleted_physical_ports, - process_deleted_physical_locators, - process_deleted_local_macs, - process_deleted_remote_macs, - mock_handle_l2pop): + '_handle_l2pop') as mock_handle_l2pop: self.ovsdb_data.entry_table = { 'new_logical_switches': process_new_logical_switches, 'new_physical_ports': process_new_physical_ports, @@ -217,7 +200,18 @@ class TestOVSDBData(base.BaseTestCase): self.context, fake_deleted_remote_macs) self.assertTrue(mock_handle_l2pop.called) - def test_notify_ovsdb_states(self): + @mock.patch.object(lib, 'get_all_pending_remote_macs_in_asc_order') + @mock.patch.object(lib, 'delete_pending_ucast_mac_remote') + @mock.patch.object(ovsdb_schema, 'LogicalSwitch') + @mock.patch.object(ovsdb_schema, 'PhysicalLocator') + @mock.patch.object(ovsdb_schema, 'UcastMacsRemote') + @mock.patch.object(agent_api.L2gatewayAgentApi, 'add_vif_to_gateway') + @mock.patch.object(agent_api.L2gatewayAgentApi, 'update_vif_to_gateway') + @mock.patch.object(agent_api.L2gatewayAgentApi, 'delete_vif_from_gateway') + def test_notify_ovsdb_states(self, mock_del_vif, mock_upd_vif, + mock_add_vif, mock_ucmr, mock_pl, + mock_ls, mock_del_pend_recs, + mock_get_pend_recs): fake_ovsdb_states = {'ovsdb1': 'connected'} fake_dict = {'logical_switch_uuid': 'fake_ls_id', 'mac': 'fake_mac', @@ -230,35 +224,18 @@ class TestOVSDBData(base.BaseTestCase): fake_update_dict.update(fake_dict) fake_delete_dict = {'operation': 'delete'} fake_delete_dict.update(fake_dict) - with contextlib.nested( - mock.patch.object( - lib, 'get_all_pending_remote_macs_in_asc_order'), - mock.patch.object(lib, - 'delete_pending_ucast_mac_remote'), - mock.patch.object(ovsdb_schema, 'LogicalSwitch'), - mock.patch.object(ovsdb_schema, 'PhysicalLocator'), - mock.patch.object(ovsdb_schema, 'UcastMacsRemote'), - mock.patch.object(agent_api.L2gatewayAgentApi, - 'add_vif_to_gateway'), - mock.patch.object(agent_api.L2gatewayAgentApi, - 'update_vif_to_gateway'), - mock.patch.object(agent_api.L2gatewayAgentApi, - 'delete_vif_from_gateway') - ) as (mock_get_pend_recs, mock_del_pend_recs, - mock_ls, mock_pl, mock_ucmr, mock_add_vif, - mock_upd_vif, mock_del_vif): - mock_get_pend_recs.return_value = [fake_insert_dict] - self.ovsdb_data.notify_ovsdb_states( - self.context, fake_ovsdb_states) - self.assertTrue(mock_add_vif.called) - mock_get_pend_recs.return_value = [fake_update_dict] - self.ovsdb_data.notify_ovsdb_states( - self.context, fake_ovsdb_states) - self.assertTrue(mock_upd_vif.called) - mock_get_pend_recs.return_value = [fake_delete_dict] - self.ovsdb_data.notify_ovsdb_states( - self.context, fake_ovsdb_states) - self.assertTrue(mock_del_vif.called) + mock_get_pend_recs.return_value = [fake_insert_dict] + self.ovsdb_data.notify_ovsdb_states( + self.context, fake_ovsdb_states) + self.assertTrue(mock_add_vif.called) + mock_get_pend_recs.return_value = [fake_update_dict] + self.ovsdb_data.notify_ovsdb_states( + self.context, fake_ovsdb_states) + self.assertTrue(mock_upd_vif.called) + mock_get_pend_recs.return_value = [fake_delete_dict] + self.ovsdb_data.notify_ovsdb_states( + self.context, fake_ovsdb_states) + self.assertTrue(mock_del_vif.called) def test_process_new_logical_switches(self): fake_dict = {} @@ -291,31 +268,26 @@ class TestOVSDBData(base.BaseTestCase): get_ps.assert_called_with(self.context, fake_dict) add_ps.assert_called_with(self.context, fake_dict) - def test_process_new_physical_ports(self): + @mock.patch.object(lib, 'get_physical_port', return_value=None) + @mock.patch.object(lib, 'add_physical_port') + @mock.patch.object(lib, 'get_vlan_binding', return_value=None) + @mock.patch.object(lib, 'add_vlan_binding') + def test_process_new_physical_ports(self, add_vlan, get_vlan, + add_pp, get_pp): fake_dict1 = {} fake_dict2 = {'vlan_bindings': [fake_dict1]} fake_new_physical_ports = [fake_dict2] - with contextlib.nested( - mock.patch.object(lib, 'get_physical_port', - return_value=None), - mock.patch.object(lib, - 'add_physical_port'), - mock.patch.object(lib, - 'get_vlan_binding', return_value=None), - mock.patch.object(lib, - 'add_vlan_binding')) as ( - get_pp, add_pp, get_vlan, add_vlan): - self.ovsdb_data._process_new_physical_ports( - self.context, fake_new_physical_ports) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict2) - self.assertEqual('fake_ovsdb_id', - fake_dict2[n_const.OVSDB_IDENTIFIER]) - get_pp.assert_called_with(self.context, fake_dict2) - add_pp.assert_called_with(self.context, fake_dict2) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) - self.assertIn('port_uuid', fake_dict1) - get_vlan.assert_called_with(self.context, fake_dict1) - add_vlan.assert_called_with(self.context, fake_dict1) + self.ovsdb_data._process_new_physical_ports( + self.context, fake_new_physical_ports) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict2) + self.assertEqual('fake_ovsdb_id', + fake_dict2[n_const.OVSDB_IDENTIFIER]) + get_pp.assert_called_with(self.context, fake_dict2) + add_pp.assert_called_with(self.context, fake_dict2) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) + self.assertIn('port_uuid', fake_dict1) + get_vlan.assert_called_with(self.context, fake_dict1) + add_vlan.assert_called_with(self.context, fake_dict1) def test_process_new_physical_locators(self): fake_dict = {} @@ -332,23 +304,21 @@ class TestOVSDBData(base.BaseTestCase): get_pl.assert_called_with(self.context, fake_dict) add_pl.assert_called_with(self.context, fake_dict) - def test_process_new_local_macs(self): + @mock.patch.object(lib, 'get_ucast_mac_local', return_value=None) + @mock.patch.object(lib, 'add_ucast_mac_local') + def test_process_new_local_macs(self, add_lm, get_lm): fake_dict = {'uuid': '123456', 'mac': 'mac123', 'ovsdb_identifier': 'host1', 'logical_switch_id': 'ls123'} fake_new_local_macs = [fake_dict] - with contextlib.nested( - mock.patch.object(lib, 'get_ucast_mac_local', return_value=None), - mock.patch.object(lib, 'add_ucast_mac_local')) as ( - get_lm, add_lm): - self.ovsdb_data._process_new_local_macs( - self.context, fake_new_local_macs) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict) - self.assertEqual('fake_ovsdb_id', - fake_dict[n_const.OVSDB_IDENTIFIER]) - get_lm.assert_called_with(self.context, fake_dict) - add_lm.assert_called_with(self.context, fake_dict) + self.ovsdb_data._process_new_local_macs( + self.context, fake_new_local_macs) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict) + self.assertEqual('fake_ovsdb_id', + fake_dict[n_const.OVSDB_IDENTIFIER]) + get_lm.assert_called_with(self.context, fake_dict) + add_lm.assert_called_with(self.context, fake_dict) def test_process_new_remote_macs(self): fake_dict = {'logical_switch_id': 'ls123'} @@ -391,12 +361,11 @@ class TestOVSDBData(base.BaseTestCase): def test_get_agent_by_mac(self): fake_mac = {'mac': 'fake_mac_1'} fake_port = [{'binding:host_id': 'fake_host'}] - with contextlib.nested( - mock.patch.object(self.ovsdb_data, '_get_port_by_mac', - return_value=fake_port), - mock.patch.object(self.ovsdb_data, - '_get_agent_details_by_host')) as ( - mock_get_port_mac, mock_get_agent_detail): + with mock.patch.object(self.ovsdb_data, '_get_port_by_mac', + return_value=fake_port) as mock_get_port_mac, \ + mock.patch.object( + self.ovsdb_data, + '_get_agent_details_by_host') as mock_get_agent_detail: self.ovsdb_data._get_agent_by_mac(self.context, fake_mac) mock_get_port_mac.assert_called_with(self.context, 'fake_mac_1') mock_get_agent_detail.assert_called_with(self.context, 'fake_host') @@ -405,10 +374,9 @@ class TestOVSDBData(base.BaseTestCase): fake_agent = {'configurations': {'tunnel_types': ["vxlan"], 'l2_population': True}} fake_agents = [fake_agent] - with contextlib.nested( - mock.patch.object(self.ovsdb_data.core_plugin, - 'get_agents', - return_value=fake_agents)): + with mock.patch.object(self.ovsdb_data.core_plugin, + 'get_agents', + return_value=fake_agents): l2pop_enabled = self.ovsdb_data._get_agent_details_by_host( self.context, 'fake_host') self.assertTrue(l2pop_enabled) @@ -418,15 +386,13 @@ class TestOVSDBData(base.BaseTestCase): fake_deleted_physical_switches = [fake_dict] fake_ls_dict = {'uuid': 'ls-uuid'} fake_ls_list = [fake_ls_dict] - with contextlib.nested( - mock.patch.object(lib, 'delete_physical_switch'), + with mock.patch.object(lib, 'delete_physical_switch') as delete_ps, \ mock.patch.object(lib, 'get_all_physical_switches_by_ovsdb_id', - return_value=False), + return_value=False) as get_ps, \ mock.patch.object(lib, 'get_all_logical_switches_by_ovsdb_id', - return_value=fake_ls_list), + return_value=fake_ls_list) as get_ls, \ mock.patch.object(agent_api.L2gatewayAgentApi, - 'delete_network')) as ( - delete_ps, get_ps, get_ls, del_network): + 'delete_network') as del_network: self.ovsdb_data._process_deleted_physical_switches( self.context, fake_deleted_physical_switches) self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict) @@ -450,32 +416,46 @@ class TestOVSDBData(base.BaseTestCase): 'vlan': 'fake_vlan', 'logical_switch_uuid': 'fake_switch_uuid', 'ovsdb_identifier': 'fake_ovsdb_id'} - with contextlib.nested( - mock.patch.object(lib, - 'delete_physical_port'), + with mock.patch.object(lib, + 'delete_physical_port'), \ mock.patch.object(lib, 'get_physical_port', - return_value=fake_physical_port), + return_value=fake_physical_port), \ mock.patch.object(lib, 'get_physical_switch', - return_vaue=fake_physical_switch), + return_vaue=fake_physical_switch), \ mock.patch.object(lib, 'get_all_vlan_bindings_by_physical_port', - return_vaue=fake_vlan_binding), + return_vaue=fake_vlan_binding), \ mock.patch.object(l2gateway_db.L2GatewayMixin, '_get_l2gw_ids_by_interface_switch', - return_value=['fake_uuid']), - mock.patch.object(l2gateway_db.L2GatewayMixin, - '_delete_connection_by_l2gw_id') - ) as (delete_pp, get_pp, get_ps, get_vb, l2gw_del, l2gw_conn_del): + return_value=['fake_uuid']), \ + mock.patch.object( + l2gateway_db.L2GatewayMixin, + '_delete_connection_by_l2gw_id') as l2gw_conn_del: self.ovsdb_data._process_deleted_physical_ports( self.context, fake_deleted_physical_ports) self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict) self.assertEqual('fake_ovsdb_id', fake_dict[n_const.OVSDB_IDENTIFIER]) l2gw_conn_del.assert_called_with(self.context, 'fake_uuid') - delete_pp.assert_called_with(self.context, fake_dict) - def test_process_deleted_physical_ports_with_delete_macs(self): + @mock.patch.object(lib, 'delete_physical_port') + @mock.patch.object(lib, 'get_physical_port') + @mock.patch.object(lib, 'get_physical_switch') + @mock.patch.object(l2gateway_db.L2GatewayMixin, + '_get_l2gw_ids_by_interface_switch', + return_value=['fake_uuid']) + @mock.patch.object(l2gateway_db.L2GatewayMixin, + '_delete_connection_by_l2gw_id') + @mock.patch.object(lib, + 'get_all_vlan_bindings_by_physical_port') + @mock.patch.object(lib, + 'get_all_vlan_bindings_by_logical_switch') + @mock.patch.object(data.OVSDBData, '_delete_macs_from_ovsdb') + @mock.patch.object(lib, 'delete_vlan_binding') + def test_process_deleted_physical_ports_with_delete_macs( + self, del_vlan, del_macs, get_vlan_by_ls, get_vlan_by_pp, + l2gw_conn_del, get_l2gw, get_ps, get_pp, delete_pp): fake_dict = {'uuid': 'fake_uuid', 'name': 'fake_name', 'logical_switch_id': 'fake_ls_id', 'ovsdb_identifier': 'fake_ovsdb_id'} @@ -493,44 +473,41 @@ class TestOVSDBData(base.BaseTestCase): 'logical_switch_id': 'fake_ls_id'} fake_vlan_binding_list = [vlan_binding_dict] fake_binding_list = [vlan_binding_dict] - with contextlib.nested( - mock.patch.object(lib, - 'delete_physical_port'), - mock.patch.object(lib, - 'get_physical_port', - return_value=fake_physical_port), - mock.patch.object(lib, 'get_physical_switch', - return_vaue=fake_physical_switch), - mock.patch.object(l2gateway_db.L2GatewayMixin, - '_get_l2gw_ids_by_interface_switch', - return_value=['fake_uuid']), - mock.patch.object(l2gateway_db.L2GatewayMixin, - '_delete_connection_by_l2gw_id'), - mock.patch.object(lib, - 'get_all_vlan_bindings_by_physical_port', - return_value=fake_vlan_binding_list), - mock.patch.object(lib, - 'get_all_vlan_bindings_by_logical_switch', - return_value=fake_binding_list), - mock.patch.object(data.OVSDBData, '_delete_macs_from_ovsdb'), - mock.patch.object(lib, 'delete_vlan_binding') - ) as (delete_pp, get_pp, get_ps, get_l2gw, l2gw_conn_del, - get_vlan_by_pp, get_vlan_by_ls, del_macs, del_vlan): - self.ovsdb_data._process_deleted_physical_ports( - self.context, fake_deleted_physical_ports) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict) - self.assertEqual('fake_ovsdb_id', - fake_dict[n_const.OVSDB_IDENTIFIER]) - l2gw_conn_del.assert_called_with(self.context, 'fake_uuid') - get_vlan_by_pp.assert_called_with(self.context, fake_dict) - del_vlan.assert_called_with(self.context, vlan_binding_dict) - get_vlan_by_ls.assert_called_with(self.context, vlan_binding_dict) - del_macs.assert_called_with(self.context, - 'fake_ls_id', 'fake_ovsdb_id') - del_vlan.assert_called_with(self.context, vlan_binding_dict) - delete_pp.assert_called_with(self.context, fake_dict) - - def test_process_deleted_physical_locators(self): + get_pp.return_value = fake_physical_port + get_ps.return_vaue = fake_physical_switch + get_vlan_by_pp.return_value = fake_vlan_binding_list + get_vlan_by_ls.return_value = fake_binding_list + self.ovsdb_data._process_deleted_physical_ports( + self.context, fake_deleted_physical_ports) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict) + self.assertEqual('fake_ovsdb_id', + fake_dict[n_const.OVSDB_IDENTIFIER]) + l2gw_conn_del.assert_called_with(self.context, 'fake_uuid') + get_vlan_by_pp.assert_called_with(self.context, fake_dict) + del_vlan.assert_called_with(self.context, vlan_binding_dict) + get_vlan_by_ls.assert_called_with(self.context, vlan_binding_dict) + del_macs.assert_called_with(self.context, + 'fake_ls_id', 'fake_ovsdb_id') + del_vlan.assert_called_with(self.context, vlan_binding_dict) + delete_pp.assert_called_with(self.context, fake_dict) + + @mock.patch.object(data.OVSDBData, + '_get_logical_switch_ids', + return_value=['1']) + @mock.patch.object(lib, + 'get_all_physical_switches_by_ovsdb_id', + return_value=[{'tunnel_ip': '3.3.3.3'}]) + @mock.patch.object(data.OVSDBData, + '_get_fdb_entries') + @mock.patch.object(lib, + 'delete_physical_locator') + @mock.patch.object(data.OVSDBData, '_get_agent_ips', + return_value={'1.1.1.1': 'hostname'}) + @mock.patch.object(tunnel_calls.Tunnel_Calls, + 'trigger_l2pop_delete') + def test_process_deleted_physical_locators( + self, trig_l2pop, get_agent_ips, delete_pl, get_fdb, get_all_ps, + get_ls): """Test case to test _process_deleted_physical_locators. for unicast rpc to the L2 agent @@ -539,38 +516,37 @@ class TestOVSDBData(base.BaseTestCase): fake_dict2 = {'dst_ip': '2.2.2.2'} fake_deleted_physical_locators = [fake_dict2, fake_dict1] mock.patch.object(manager, 'NeutronManager').start() - with contextlib.nested( - mock.patch.object(data.OVSDBData, - '_get_logical_switch_ids', - return_value=['1']), - mock.patch.object(lib, - 'get_all_physical_switches_by_ovsdb_id', - return_value=[{'tunnel_ip': '3.3.3.3'}]), - mock.patch.object(data.OVSDBData, - '_get_fdb_entries'), - mock.patch.object(lib, - 'delete_physical_locator'), - mock.patch.object(data.OVSDBData, '_get_agent_ips', - return_value={'1.1.1.1': 'hostname'}), - mock.patch.object(tunnel_calls.Tunnel_Calls, - 'trigger_l2pop_delete') - ) as (get_ls, get_all_ps, get_fdb, delete_pl, get_agent_ips, - trig_l2pop): - self.ovsdb_data._process_deleted_physical_locators( - self.context, fake_deleted_physical_locators) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) - self.assertTrue(get_ls.called) - self.assertTrue(get_all_ps.called) - self.assertTrue(get_fdb.called) - self.assertEqual('fake_ovsdb_id', - fake_dict1[n_const.OVSDB_IDENTIFIER]) - delete_pl.assert_called_with(self.context, fake_dict1) - self.assertTrue(get_agent_ips.called) - trig_l2pop.assert_called_with(self.context, - mock.ANY, - 'hostname') - - def test_process_deleted_physical_locators1(self): + self.ovsdb_data._process_deleted_physical_locators( + self.context, fake_deleted_physical_locators) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) + self.assertTrue(get_ls.called) + self.assertTrue(get_all_ps.called) + self.assertTrue(get_fdb.called) + self.assertEqual('fake_ovsdb_id', + fake_dict1[n_const.OVSDB_IDENTIFIER]) + delete_pl.assert_called_with(self.context, fake_dict1) + self.assertTrue(get_agent_ips.called) + trig_l2pop.assert_called_with(self.context, + mock.ANY, + 'hostname') + + @mock.patch.object(data.OVSDBData, + '_get_logical_switch_ids', + return_value=['1']) + @mock.patch.object(lib, + 'get_all_physical_switches_by_ovsdb_id', + return_value=[{'tunnel_ip': '3.3.3.3'}]) + @mock.patch.object(data.OVSDBData, + '_get_fdb_entries') + @mock.patch.object(lib, + 'delete_physical_locator') + @mock.patch.object(data.OVSDBData, '_get_agent_ips', + return_value={'2.2.2.2': 'hostname'}) + @mock.patch.object(tunnel_calls.Tunnel_Calls, + 'trigger_l2pop_delete') + def test_process_deleted_physical_locators1( + self, trig_l2pop, get_agent_ips, delete_pl, get_fdb, + get_all_ps, get_ls): """Test case to test _process_deleted_physical_locators. for broadcast rpc to the L2 agents @@ -578,35 +554,18 @@ class TestOVSDBData(base.BaseTestCase): fake_dict1 = {'dst_ip': '1.1.1.1'} fake_deleted_physical_locators = [fake_dict1] mock.patch.object(manager, 'NeutronManager').start() - with contextlib.nested( - mock.patch.object(data.OVSDBData, - '_get_logical_switch_ids', - return_value=['1']), - mock.patch.object(lib, - 'get_all_physical_switches_by_ovsdb_id', - return_value=[{'tunnel_ip': '3.3.3.3'}]), - mock.patch.object(data.OVSDBData, - '_get_fdb_entries'), - mock.patch.object(lib, - 'delete_physical_locator'), - mock.patch.object(data.OVSDBData, '_get_agent_ips', - return_value={'2.2.2.2': 'hostname'}), - mock.patch.object(tunnel_calls.Tunnel_Calls, - 'trigger_l2pop_delete') - ) as (get_ls, get_all_ps, get_fdb, delete_pl, get_agent_ips, - trig_l2pop): - self.ovsdb_data._process_deleted_physical_locators( - self.context, fake_deleted_physical_locators) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) - self.assertTrue(get_ls.called) - self.assertTrue(get_all_ps.called) - self.assertTrue(get_fdb.called) - self.assertEqual('fake_ovsdb_id', - fake_dict1[n_const.OVSDB_IDENTIFIER]) - delete_pl.assert_called_once_with(self.context, fake_dict1) - self.assertTrue(get_agent_ips.called) - trig_l2pop.assert_called_with(self.context, - mock.ANY) + self.ovsdb_data._process_deleted_physical_locators( + self.context, fake_deleted_physical_locators) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) + self.assertTrue(get_ls.called) + self.assertTrue(get_all_ps.called) + self.assertTrue(get_fdb.called) + self.assertEqual('fake_ovsdb_id', + fake_dict1[n_const.OVSDB_IDENTIFIER]) + delete_pl.assert_called_once_with(self.context, fake_dict1) + self.assertTrue(get_agent_ips.called) + trig_l2pop.assert_called_with(self.context, + mock.ANY) def test_process_deleted_local_macs(self): fake_dict = {'uuid': '123456', @@ -636,32 +595,26 @@ class TestOVSDBData(base.BaseTestCase): fake_dict[n_const.OVSDB_IDENTIFIER]) delete_mr.assert_called_with(self.context, fake_dict) - def test_process_modified_physical_ports(self): + @mock.patch.object(lib, 'get_physical_port') + @mock.patch.object(lib, 'add_physical_port') + @mock.patch.object(lib, 'get_all_vlan_bindings_by_physical_port') + @mock.patch.object(lib, 'add_vlan_binding') + @mock.patch.object(lib, 'update_physical_ports_status') + def test_process_modified_physical_ports(self, update_pp_status, add_vlan, + get_vlan, add_pp, get_pp): fake_dict1 = {} fake_dict2 = {'vlan_bindings': [fake_dict1], 'uuid': 'fake_uuid'} fake_modified_physical_ports = [fake_dict2] - with contextlib.nested( - mock.patch.object(lib, 'get_physical_port'), - mock.patch.object(lib, - 'add_physical_port'), - mock.patch.object(lib, - 'get_all_vlan_bindings_by_physical_port'), - mock.patch.object(lib, - 'add_vlan_binding'), - mock.patch.object(lib, - 'update_physical_ports_status') - ) as ( - get_pp, add_pp, get_vlan, add_vlan, update_pp_status): - self.ovsdb_data._process_modified_physical_ports( - self.context, fake_modified_physical_ports) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict2) - self.assertEqual('fake_ovsdb_id', - fake_dict2[n_const.OVSDB_IDENTIFIER]) - get_pp.assert_called_with(self.context, fake_dict2) - update_pp_status.assert_called_with(self.context, fake_dict2) - self.assertFalse(add_pp.called) - get_vlan.assert_called_with(self.context, fake_dict2) - self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) - self.assertIn('port_uuid', fake_dict1) - add_vlan.assert_called_with(self.context, fake_dict1) + self.ovsdb_data._process_modified_physical_ports( + self.context, fake_modified_physical_ports) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict2) + self.assertEqual('fake_ovsdb_id', + fake_dict2[n_const.OVSDB_IDENTIFIER]) + get_pp.assert_called_with(self.context, fake_dict2) + update_pp_status.assert_called_with(self.context, fake_dict2) + self.assertFalse(add_pp.called) + get_vlan.assert_called_with(self.context, fake_dict2) + self.assertIn(n_const.OVSDB_IDENTIFIER, fake_dict1) + self.assertIn('port_uuid', fake_dict1) + add_vlan.assert_called_with(self.context, fake_dict1) diff --git a/networking_l2gw/tests/unit/services/l2gateway/service_drivers/test_rpc_l2gw.py b/networking_l2gw/tests/unit/services/l2gateway/service_drivers/test_rpc_l2gw.py index 97e431fd..2cafa28d 100755 --- a/networking_l2gw/tests/unit/services/l2gateway/service_drivers/test_rpc_l2gw.py +++ b/networking_l2gw/tests/unit/services/l2gateway/service_drivers/test_rpc_l2gw.py @@ -14,7 +14,6 @@ # limitations under the License. import mock -import contextlib from neutron.common import rpc as n_rpc from neutron import context as ctx from neutron.db import agents_db @@ -44,86 +43,77 @@ class TestL2gwRpcDriver(base.BaseTestCase): self.ovsdb_identifier = 'fake_ovsdb_id' self.context = mock.ANY - def test_l2rpcdriver_init(self): - with contextlib.nested( - mock.patch.object(importutils, - 'import_object'), - mock.patch.object(agents_db, - 'AgentExtRpcCallback'), - mock.patch.object(n_rpc, - 'create_connection'), - mock.patch.object(n_rpc.Connection, - 'consume_in_threads'), - mock.patch.object(rpc_l2gw.LOG, - 'debug'), - mock.patch.object(rpc_l2gw.L2gwRpcDriver, - 'start_l2gateway_agent_scheduler'), - ) as (import_obj, - agent_calback, - create_conn, - consume_in_thread, - debug, - scheduler): - rpc_l2gw.L2gwRpcDriver(self.service_plugin) - rpc_conn = create_conn.return_value - rpc_conn.create_consumer.assert_called_with( - mock.ANY, [mock.ANY, mock.ANY], fanout=mock.ANY) - rpc_conn.consume_in_threads.assert_called_with() - self.assertTrue(import_obj.called) - self.assertTrue(agent_calback.called) - self.assertTrue(create_conn.called) - self.assertTrue(debug.called) - self.assertTrue(scheduler.called) + @mock.patch.object(importutils, + 'import_object') + @mock.patch.object(agents_db, + 'AgentExtRpcCallback') + @mock.patch.object(n_rpc, + 'create_connection') + @mock.patch.object(n_rpc.Connection, + 'consume_in_threads') + @mock.patch.object(rpc_l2gw.LOG, + 'debug') + @mock.patch.object(rpc_l2gw.L2gwRpcDriver, + 'start_l2gateway_agent_scheduler') + def test_l2rpcdriver_init(self, scheduler, debug, consume_in_thread, + create_conn, agent_calback, import_obj): + rpc_l2gw.L2gwRpcDriver(self.service_plugin) + rpc_conn = create_conn.return_value + rpc_conn.create_consumer.assert_called_with( + mock.ANY, [mock.ANY, mock.ANY], fanout=mock.ANY) + rpc_conn.consume_in_threads.assert_called_with() + self.assertTrue(import_obj.called) + self.assertTrue(agent_calback.called) + self.assertTrue(create_conn.called) + self.assertTrue(debug.called) + self.assertTrue(scheduler.called) def test_validate_connection(self): fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_net_seg_list = [{'id': 'fake_id', 'network_type': 'fake_vxlan', 'physical_network': 'fake_phy_net', - 'segmentation_id': 100L}] + 'segmentation_id': 100}] fake_l2_gw = {'id': 'fake_l2gw_id'} fake_tenant_id = 'fake_tenant_id' fake_filters = {'network_id': ['fake_network_id'], 'tenant_id': [fake_tenant_id], 'l2_gateway_id': ['fake_l2gw_id']} - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_is_vlan_configured_on_any_interface_for_l2gw', - return_value=False), + with mock.patch.object(self.service_plugin, + '_is_vlan_configured_on_any_interface_for_l2gw', + return_value=False) as is_vlan, \ mock.patch.object(l2gw_validators, 'validate_network_mapping_list', - return_value='fake_network_id'), + return_value='fake_network_id') as val_ntwk, \ mock.patch.object(self.service_plugin, '_get_network_segments', - return_value=fake_net_seg_list), + return_value=fake_net_seg_list), \ mock.patch.object(self.service_plugin, '_get_network', - return_value=True), + return_value=True) as get_network, \ mock.patch.object(self.service_plugin, '_get_l2_gateway', - return_value=fake_l2_gw), + return_value=fake_l2_gw) as get_l2gw, \ mock.patch.object(self.service_plugin, '_retrieve_gateway_connections', - return_value=False), + return_value=False) as ret_gw_conn, \ mock.patch.object(self.service_plugin, '_get_tenant_id_for_create', - return_value=fake_tenant_id), + return_value=fake_tenant_id) as get_ten_id, \ mock.patch.object(self.service_plugin, 'get_l2_gateway_connections', - return_value=False), + return_value=False) as get_l2_gw_conn, \ mock.patch.object( self.plugin, - '_check_port_fault_status_and_switch_fault_status') - ) as (is_vlan, val_ntwk, get_net_seg, get_network, get_l2gw, - ret_gw_conn, get_ten_id, get_l2_gw_conn, check_pf_sf): + '_check_port_fault_status_and_switch_fault_status') as pf_sf: self.plugin._validate_connection(self.context, fake_connection) is_vlan.assert_called_with(self.context, 'fake_l2gw_id') val_ntwk.assert_called_with(fake_connection, False) get_network.assert_called_with(self.context, 'fake_network_id') get_l2gw.assert_called_with(self.context, 'fake_l2gw_id') - check_pf_sf.assert_called_with(self.context, 'fake_l2gw_id') + pf_sf.assert_called_with(self.context, 'fake_l2gw_id') ret_gw_conn.assert_called_with(self.context, 'fake_l2gw_id', fake_connection) @@ -131,10 +121,16 @@ class TestL2gwRpcDriver(base.BaseTestCase): get_l2_gw_conn.assert_called_with(self.context, filters=fake_filters) - def test_process_port_list(self): + @mock.patch.object(db, + 'get_physical_switch_by_name') + @mock.patch.object(db, + 'get_logical_switch_by_name') + @mock.patch.object(db, + 'get_physical_port_by_name_and_ps') + def test_process_port_list(self, get_pp, get_ls, get_ps): fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_device = {'id': 'fake_device_id', 'device_name': 'fake_device_name'} fake_method = 'CREATE' @@ -152,22 +148,15 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'name': 'fake_network_id'} fake_physical_port = {'uuid': 'fake_uuid', 'name': 'fake_name'} - with contextlib.nested( - mock.patch.object(self.service_plugin, - 'get_l2gateway_interfaces_by_device_id', - return_value=fake_interface_list), - mock.patch.object(db, - 'get_physical_switch_by_name', - return_value=fake_physical_switch), - mock.patch.object(db, - 'get_logical_switch_by_name', - return_value=fake_logical_switch), - mock.patch.object(db, - 'get_physical_port_by_name_and_ps', - return_value=fake_physical_port), - mock.patch.object(self.plugin, - '_generate_port_list')) as ( - get_intf, get_ps, get_ls, get_pp, gen_port_list): + get_ps.return_value = fake_physical_switch + get_ls.return_value = fake_logical_switch + get_pp.return_value = fake_physical_port + with mock.patch.object( + self.service_plugin, + 'get_l2gateway_interfaces_by_device_id', + return_value=fake_interface_list) as get_intf, \ + mock.patch.object(self.plugin, + '_generate_port_list') as gen_port_list: self.plugin._process_port_list(self.context, fake_device, fake_connection, fake_method) @@ -176,7 +165,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): get_pp.assert_called_with(self.context, fake_pp_dict) get_ls.assert_called_with(self.context, fake_pp_dict) gen_port_list.assert_called_with( - self.context, fake_method, 100L, fake_interface, + self.context, fake_method, 100, fake_interface, fake_pp_dict, 'fake_uuid', fake_connection) def test_generate_port_list_for_create(self): @@ -188,7 +177,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'logical_switch_name': 'fake_network_id', 'uuid': 'fake_uuid', 'name': 'fake_name'} - fake_vlan_binding = {'vlan': 100L, + fake_vlan_binding = {'vlan': 100, 'logical_switch_uuid': 'fake_uuid'} fake_vlan_binding_list = [fake_vlan_binding] with mock.patch.object( @@ -196,7 +185,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): return_value=fake_vlan_binding_list) as ( get_vlan): self.plugin._generate_port_list( - self.context, fake_method, 101L, fake_interface, + self.context, fake_method, 101, fake_interface, fake_pp_dict, 'fake_uuid') get_vlan.assert_called_with(self.context, fake_pp_dict) @@ -209,7 +198,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'logical_switch_name': 'fake_network_id', 'uuid': 'fake_uuid', 'name': 'fake_name'} - fake_vlan_binding = {'vlan': 100L, + fake_vlan_binding = {'vlan': 100, 'logical_switch_uuid': 'fake_uuid'} fake_vlan_binding_list = [fake_vlan_binding] with mock.patch.object( @@ -218,7 +207,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): get_vlan): self.assertRaises(l2gw_exc.L2GatewayDuplicateSegmentationID, self.plugin._generate_port_list, - self.context, fake_method, 100L, + self.context, fake_method, 100, fake_interface, fake_pp_dict, 'fake_uuid') get_vlan.assert_called_with(self.context, fake_pp_dict) @@ -253,7 +242,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): return_value=fake_vlan_binding_list) as ( get_vlan): port = self.plugin._generate_port_list( - self.context, fake_method, 100L, fake_interface, + self.context, fake_method, 100, fake_interface, fake_pp_dict, 'fake_uuid', fake_connection) get_vlan.assert_called_with(self.context, fake_pp_dict) self.assertEqual(phys_port_dict, port) @@ -311,7 +300,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_ls = None fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_network = {'id': 'fake_network_id', 'name': 'fake_network_name', 'provider:network_type': 'vxlan', @@ -341,7 +330,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_ls = None fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_network = {'id': 'fake_network_id', 'name': 'fake_network_name', 'segments': [{"provider:network_type": "vxlan", @@ -372,7 +361,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'name': 'fake_network_id'} fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_network = {'id': 'fake_network_id', 'name': 'fake_network_name', 'segments': [{"provider:network_type": "vlan", @@ -393,7 +382,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'name': 'fake_network_id'} fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_network = {'id': 'fake_network_id', 'name': 'fake_network_name', 'segments': [{"provider:network_type": "vxlan", @@ -409,7 +398,9 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_connection) get_network.assert_called_with(self.context, 'fake_network_id') - def test_get_locator_list(self): + @mock.patch.object(db, + 'get_physical_locator_by_dst_ip') + def test_get_locator_list(self, get_pl_by_dst_ip): fake_dst_ip = 'fake_tun_ip' fake_ovsdb_id = 'fake_ovsdb_id' fake_mac_list = [] @@ -421,14 +412,11 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_pl = {'uuid': 'fake_uuid', 'dst_ip': 'fake_tun_ip'} fale_pl_list = [fake_locator_dict] - with contextlib.nested( - mock.patch.object(db, - 'get_physical_locator_by_dst_ip', - return_value=fake_pl), - mock.patch.object(self.plugin, - '_get_physical_locator_dict', - return_value=fake_locator_dict)) as ( - get_pl_by_dst_ip, get_pl_dict): + get_pl_by_dst_ip.return_value = fake_pl + with mock.patch.object( + self.plugin, + '_get_physical_locator_dict', + return_value=fake_locator_dict) as get_pl_dict: ret_pl_list = self.plugin._get_locator_list( self.context, fake_dst_ip, fake_ovsdb_id, fake_mac_list, fake_locator_list) @@ -438,24 +426,22 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'fake_uuid', fake_mac_list) self.assertEqual(fale_pl_list, ret_pl_list) - def test_get_identifer_list(self): + @mock.patch.object(db, + 'get_physical_switch_by_name') + def test_get_identifer_list(self, get_ps): fake_connection = {'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_device = {'id': 'fake_device_id', 'device_name': 'fake_device_name'} fake_device_list = [fake_device] fake_physical_switch = {'uuid': 'fake_uuid', 'ovsdb_identifier': 'fake_ovsdb_id'} fake_identifier_set = set(['fake_ovsdb_id']) - with contextlib.nested( - mock.patch.object(self.service_plugin, - 'get_l2gateway_devices_by_gateway_id', - return_value=fake_device_list), - mock.patch.object(db, - 'get_physical_switch_by_name', - return_value=fake_physical_switch)) as ( - get_l2_gw, get_ps): + get_ps.return_value = fake_physical_switch + with mock.patch.object(self.service_plugin, + 'get_l2gateway_devices_by_gateway_id', + return_value=fake_device_list): ret_value = self.plugin._get_identifer_list(self.context, fake_connection) (self.service_plugin.get_l2gateway_devices_by_gateway_id. @@ -468,7 +454,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_connection = {'id': 'fake_id', 'l2_gateway_id': 'fake_l2gw_id', 'network_id': 'fake_network_id', - 'segmentation_id': 100L} + 'segmentation_id': 100} fake_gw_conn_ovsdb_set = set(['fake_ovsdb_id']) fake_connection_list = [fake_connection] with mock.patch.object(self.service_plugin, @@ -488,13 +474,11 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'fixed_ips': [{'ip_address': 'fake_ip'}], 'mac_address': 'fake_mac_add'} fake_port_list = [fake_port] - with contextlib.nested( - mock.patch.object(self.plugin, - '_get_port_details', - return_value=fake_port_list), - mock.patch.object(self.plugin, - 'delete_port_mac')) as ( - get_port, delete_mac): + with mock.patch.object(self.plugin, + '_get_port_details', + return_value=fake_port_list) as get_port, \ + mock.patch.object(self.plugin, + 'delete_port_mac') as delete_mac: self.plugin._remove_vm_macs(self.context, fake_network_id, fake_ovsdb_id_set) @@ -505,7 +489,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): def test_add_port_mac(self): fake_ip1 = "fake_ip1" fake_ip2 = "fake_ip2" - fake_network_dict = {'provider:segmentation_id': 100L, + fake_network_dict = {'provider:segmentation_id': 100, 'name': 'fake_name', 'id': 'fake_network_id'} fake_conn_dict = {'id': 'fake_conn_id', @@ -533,38 +517,34 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'network_id': 'fake_network_id', 'mac_address': 'fake_mac'} ovsdb_identifier = 'fake_ovsdb_id' - with contextlib.nested( - mock.patch.object(self.plugin, - '_get_ip_details', - return_value=(fake_ip1, - fake_ip2)), + with mock.patch.object(self.plugin, + '_get_ip_details', + return_value=(fake_ip1, + fake_ip2)),\ mock.patch.object(self.plugin, '_get_network_details', - return_value=fake_network_dict), + return_value=fake_network_dict) as get_network,\ mock.patch.object(self.service_plugin, 'get_l2_gateway_connections', - return_value=fake_conn_list), + return_value=fake_conn_list) as get_l2gw_conn,\ mock.patch.object(self.plugin, '_form_physical_locator_schema', - return_value=fake_locator_dict), + return_value=fake_locator_dict) as get_pl,\ mock.patch.object(ovsdb_schema, - 'UcastMacsRemote'), + 'UcastMacsRemote') as mock_ucmr,\ mock.patch.object(self.plugin, '_get_dict', - return_value=fake_dict), + return_value=fake_dict) as get_dict,\ mock.patch.object(db, 'get_ucast_mac_remote_by_mac_and_ls', - return_value=False), + return_value=False) as get_ucast_mac,\ mock.patch.object(self.plugin.agent_rpc, - 'add_vif_to_gateway'), + 'add_vif_to_gateway') as add_rpc,\ mock.patch.object(self.plugin.agent_rpc, - 'update_vif_to_gateway'), + 'update_vif_to_gateway') as update_rpc,\ mock.patch.object(db, 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list)) as ( - get_ip, get_network, get_l2gw_conn, - get_pl, mock_ucmr, get_dict, get_ucast_mac, add_rpc, - update_rpc, get_all_ls): + return_value=fake_logical_switch_list): remote_mac = ovsdb_schema.UcastMacsRemote( None, fake_dict['mac'], fake_logical_switch['uuid'], fake_locator_dict['uuid'], @@ -588,7 +568,20 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_logical_switch, fake_locator_dict, fake_dict) self.assertFalse(update_rpc.called) - def test_delete_port_mac_for_multiple_vlan_bindings(self): + @mock.patch.object(db, + 'get_all_logical_switches_by_name') + @mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=True) + @mock.patch.object(agent_api.L2gatewayAgentApi, + 'delete_vif_from_gateway') + @mock.patch.object(db, + 'get_logical_switch_by_name') + @mock.patch.object(db, + 'get_all_vlan_bindings_by_logical_switch', + return_value=[1, 2]) + def test_delete_port_mac_for_multiple_vlan_bindings( + self, get_vlan_binding, get_ls, delete_rpc, get_mac, get_all_ls): fake_port_list = [{'network_id': 'fake_network_id', 'device_owner': 'fake_owner', 'mac_address': 'fake_mac', @@ -600,26 +593,11 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'ovsdb_identifier': 'fake_ovsdb_id'} fake_rec_dict = {'logical_switch_id': 'fake_uuid', 'ovsdb_identifier': 'fake_ovsdb_id'} - with contextlib.nested( - mock.patch.object(db, - 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=True), - mock.patch.object(agent_api.L2gatewayAgentApi, - 'delete_vif_from_gateway'), - mock.patch.object(db, - 'get_logical_switch_by_name', - return_value=fake_logical_switch_dict), - mock.patch.object(db, - 'get_all_vlan_bindings_by_logical_switch', - return_value=[1, 2]), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connections', - return_value=[1, 2])) as ( - get_all_ls, get_mac, delete_rpc, get_ls, get_vlan_binding, - get_l2gw_conn): + get_all_ls.return_value = fake_logical_switch_list + get_ls.return_value = fake_logical_switch_dict + with mock.patch.object(self.service_plugin, + 'get_l2_gateway_connections', + return_value=[1, 2]): self.plugin.delete_port_mac(self.context, fake_port_list) self.assertFalse(get_all_ls.called) get_ls.assert_called_with(self.context, lg_dict) @@ -631,7 +609,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): "Test case to test add_port_mac when the OVSDB server is down." fake_ip1 = "fake_ip1" fake_ip2 = "fake_ip2" - fake_network_dict = {'provider:segmentation_id': 100L, + fake_network_dict = {'provider:segmentation_id': 100, 'name': 'fake_name', 'id': 'fake_network_id'} fake_conn_dict = {'id': 'fake_conn_id', @@ -659,39 +637,35 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'network_id': 'fake_network_id', 'mac_address': 'fake_mac'} ovsdb_identifier = 'fake_ovsdb_id' - with contextlib.nested( - mock.patch.object(self.plugin, - '_get_ip_details', - return_value=(fake_ip1, - fake_ip2)), - mock.patch.object(self.plugin, - '_get_network_details', - return_value=fake_network_dict), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connections', - return_value=fake_conn_list), - mock.patch.object(self.plugin, - '_form_physical_locator_schema', - return_value=fake_locator_dict), - mock.patch.object(ovsdb_schema, - 'UcastMacsRemote'), - mock.patch.object(self.plugin, - '_get_dict', - return_value=fake_dict), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=False), - mock.patch.object(self.plugin.agent_rpc, - 'add_vif_to_gateway', - side_effect=RuntimeError), - mock.patch.object(self.plugin.agent_rpc, - 'update_vif_to_gateway'), - mock.patch.object(db, 'add_pending_ucast_mac_remote'), - mock.patch.object(db, 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list) - ) as (get_ip, get_network, get_l2gw_conn, - get_pl, mock_ucmr, get_dict, get_ucast_mac, add_rpc, - update_rpc, add_pending_mac, get_all_ls): + with mock.patch.object(self.plugin, + '_get_ip_details', + return_value=(fake_ip1, + fake_ip2)), \ + mock.patch.object(self.plugin, + '_get_network_details', + return_value=fake_network_dict) as get_network, \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway_connections', + return_value=fake_conn_list), \ + mock.patch.object(self.plugin, + '_form_physical_locator_schema', + return_value=fake_locator_dict) as get_pl, \ + mock.patch.object(ovsdb_schema, + 'UcastMacsRemote') as mock_ucmr, \ + mock.patch.object(self.plugin, + '_get_dict', + return_value=fake_dict) as get_dict, \ + mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=False) as get_ucast_mac, \ + mock.patch.object(self.plugin.agent_rpc, + 'add_vif_to_gateway', + side_effect=RuntimeError) as add_rpc, \ + mock.patch.object(self.plugin.agent_rpc, + 'update_vif_to_gateway') as update_rpc, \ + mock.patch.object(db, 'add_pending_ucast_mac_remote') as add_pending_mac, \ + mock.patch.object(db, 'get_all_logical_switches_by_name', + return_value=fake_logical_switch_list): remote_mac = ovsdb_schema.UcastMacsRemote( None, fake_dict['mac'], fake_logical_switch['uuid'], fake_locator_dict['uuid'], @@ -713,7 +687,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): def test_add_port_mac_vm_migrate(self): fake_ip1 = "fake_ip1" fake_ip2 = "fake_ip2" - fake_network_dict = {'provider:segmentation_id': 100L, + fake_network_dict = {'provider:segmentation_id': 100, 'name': 'fake_name', 'id': 'fake_network_id'} fake_conn_dict = {'id': 'fake_conn_id', @@ -743,36 +717,32 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'network_id': 'fake_network_id', 'mac_address': 'fake_mac'} ovsdb_identifier = 'fake_ovsdb_id' - with contextlib.nested( - mock.patch.object(self.plugin, - '_get_ip_details', - return_value=(fake_ip1, - fake_ip2)), - mock.patch.object(self.plugin, - '_get_network_details', - return_value=fake_network_dict), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connections', - return_value=fake_conn_list), - mock.patch.object(self.plugin, - '_form_physical_locator_schema', - return_value=fake_locator_dict), - mock.patch.object(self.plugin, - '_get_dict', - return_value=fake_dict), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=fake_mac_dict), - mock.patch.object(self.plugin.agent_rpc, - 'add_vif_to_gateway'), - mock.patch.object(self.plugin.agent_rpc, - 'update_vif_to_gateway'), - mock.patch.object(db, - 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list)) as ( - get_ip, get_network, get_l2gw_conn, - get_pl, get_dict, get_ucast_mac, add_rpc, update_rpc, - get_all_ls): + with mock.patch.object(self.plugin, + '_get_ip_details', + return_value=(fake_ip1, + fake_ip2)), \ + mock.patch.object(self.plugin, + '_get_network_details', + return_value=fake_network_dict) as get_network, \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway_connections', + return_value=fake_conn_list) as get_l2gw_conn, \ + mock.patch.object(self.plugin, + '_form_physical_locator_schema', + return_value=fake_locator_dict) as get_pl, \ + mock.patch.object(self.plugin, + '_get_dict', + return_value=fake_dict), \ + mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=fake_mac_dict) as get_ucast_mac, \ + mock.patch.object(self.plugin.agent_rpc, + 'add_vif_to_gateway') as add_rpc, \ + mock.patch.object(self.plugin.agent_rpc, + 'update_vif_to_gateway') as update_rpc, \ + mock.patch.object(db, + 'get_all_logical_switches_by_name', + return_value=fake_logical_switch_list): self.plugin.add_port_mac(self.context, fake_dict) get_network.assert_called_with(self.context, 'fake_network_id') get_l2gw_conn.assert_called_with( @@ -788,7 +758,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): "Test case to test update_port_mac when the OVSDB server is down." fake_ip1 = "fake_ip1" fake_ip2 = "fake_ip2" - fake_network_dict = {'provider:segmentation_id': 100L, + fake_network_dict = {'provider:segmentation_id': 100, 'name': 'fake_name', 'id': 'fake_network_id'} fake_conn_dict = {'id': 'fake_conn_id', @@ -817,37 +787,33 @@ class TestL2gwRpcDriver(base.BaseTestCase): return_value) = {'device_owner': 'fake_owner', 'network_id': 'fake_network_id', 'mac_address': 'fake_mac'} - with contextlib.nested( - mock.patch.object(self.plugin, - '_get_ip_details', - return_value=(fake_ip1, - fake_ip2)), - mock.patch.object(self.plugin, - '_get_network_details', - return_value=fake_network_dict), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connections', - return_value=fake_conn_list), - mock.patch.object(self.plugin, - '_form_physical_locator_schema', - return_value=fake_locator_dict), - mock.patch.object(self.plugin, - '_get_dict', - return_value=fake_dict), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=fake_mac_dict), - mock.patch.object(self.plugin.agent_rpc, - 'add_vif_to_gateway'), - mock.patch.object(self.plugin.agent_rpc, - 'update_vif_to_gateway', - side_effect=RuntimeError), - mock.patch.object(db, 'add_pending_ucast_mac_remote'), - mock.patch.object(db, 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list) - ) as (get_ip, get_network, get_l2gw_conn, - get_pl, get_dict, get_ucast_mac, add_rpc, update_rpc, - add_pending_mac, get_all_ls): + with mock.patch.object(self.plugin, + '_get_ip_details', + return_value=(fake_ip1, + fake_ip2)), \ + mock.patch.object(self.plugin, + '_get_network_details', + return_value=fake_network_dict) as get_network, \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway_connections', + return_value=fake_conn_list) as get_l2gw_conn, \ + mock.patch.object(self.plugin, + '_form_physical_locator_schema', + return_value=fake_locator_dict) as get_pl, \ + mock.patch.object(self.plugin, + '_get_dict', + return_value=fake_dict), \ + mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=fake_mac_dict) as get_ucast_mac, \ + mock.patch.object(self.plugin.agent_rpc, + 'add_vif_to_gateway') as add_rpc, \ + mock.patch.object(self.plugin.agent_rpc, + 'update_vif_to_gateway', + side_effect=RuntimeError) as update_rpc, \ + mock.patch.object(db, 'add_pending_ucast_mac_remote') as add_pending_mac, \ + mock.patch.object(db, 'get_all_logical_switches_by_name', + return_value=fake_logical_switch_list): self.plugin.add_port_mac(self.context, fake_dict) get_network.assert_called_with(self.context, 'fake_network_id') get_l2gw_conn.assert_called_with( @@ -863,7 +829,7 @@ class TestL2gwRpcDriver(base.BaseTestCase): "when the openvswitch agent is restarted." fake_ip1 = "fake_ip1" fake_ip2 = "fake_ip2" - fake_network_dict = {'provider:segmentation_id': 100L, + fake_network_dict = {'provider:segmentation_id': 100, 'name': 'fake_name', 'id': 'fake_network_id'} fake_conn_dict = {'id': 'fake_conn_id', @@ -892,36 +858,32 @@ class TestL2gwRpcDriver(base.BaseTestCase): return_value) = {'device_owner': 'fake_owner', 'network_id': 'fake_network_id', 'mac_address': 'fake_mac'} - with contextlib.nested( - mock.patch.object(self.plugin, - '_get_ip_details', - return_value=(fake_ip1, - fake_ip2)), - mock.patch.object(self.plugin, - '_get_network_details', - return_value=fake_network_dict), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connections', - return_value=fake_conn_list), - mock.patch.object(self.plugin, - '_form_physical_locator_schema', - return_value=fake_locator_dict), - mock.patch.object(self.plugin, - '_get_dict', - return_value=fake_dict), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=fake_mac_dict), - mock.patch.object(agent_api.L2gatewayAgentApi, - 'add_vif_to_gateway'), - mock.patch.object(data.L2GatewayOVSDBCallbacks, - 'get_ovsdbdata_object'), - mock.patch.object(db, - 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list)) as ( - get_ip, get_network, get_l2gw_conn, - get_pl, get_dict, get_ucast_mac, add_rpc, - get_ovsdbdata_obj, get_all_ls): + with mock.patch.object(self.plugin, + '_get_ip_details', + return_value=(fake_ip1, + fake_ip2)), \ + mock.patch.object(self.plugin, + '_get_network_details', + return_value=fake_network_dict) as get_network, \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway_connections', + return_value=fake_conn_list) as get_l2gw_conn, \ + mock.patch.object(self.plugin, + '_form_physical_locator_schema', + return_value=fake_locator_dict) as get_pl, \ + mock.patch.object(self.plugin, + '_get_dict', + return_value=fake_dict), \ + mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=fake_mac_dict) as get_ucast_mac, \ + mock.patch.object(agent_api.L2gatewayAgentApi, + 'add_vif_to_gateway') as add_rpc, \ + mock.patch.object(data.L2GatewayOVSDBCallbacks, + 'get_ovsdbdata_object') as get_ovsdbdata_obj, \ + mock.patch.object(db, + 'get_all_logical_switches_by_name', + return_value=fake_logical_switch_list): self.plugin.add_port_mac(self.context, fake_dict) get_network.assert_called_with(self.context, 'fake_network_id') get_l2gw_conn.assert_called_with( @@ -931,7 +893,16 @@ class TestL2gwRpcDriver(base.BaseTestCase): self.assertFalse(add_rpc.called) self.assertTrue(get_ovsdbdata_obj.called) - def test_delete_port_mac_with_list(self): + @mock.patch.object(db, + 'get_all_logical_switches_by_name') + @mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=True) + @mock.patch.object(db, + 'get_all_vlan_bindings_by_logical_switch', + return_value=[1]) + def test_delete_port_mac_with_list(self, get_vlan_binding, + get_mac, get_ls): network_id = 'fake_network_id' fake_port_dict = {'network_id': 'fake_network_id', 'device_owner': 'fake_owner', @@ -945,22 +916,12 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'ovsdb_identifier': 'fake_ovsdb_id'} fake_rec_dict = {'logical_switch_id': 'fake_uuid', 'ovsdb_identifier': 'fake_ovsdb_id'} - with contextlib.nested( - mock.patch.object(db, - 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_list), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=True), - mock.patch.object(self.plugin.agent_rpc, - 'delete_vif_from_gateway'), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connections', - return_value=True), - mock.patch.object(db, - 'get_all_vlan_bindings_by_logical_switch', - return_value=[1])) as ( - get_ls, get_mac, delete_rpc, get_l2gw_conn, get_vlan_binding): + get_ls.return_value = fake_logical_switch_list + with mock.patch.object(self.plugin.agent_rpc, + 'delete_vif_from_gateway') as delete_rpc, \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway_connections', + return_value=True): self.plugin.delete_port_mac(self.context, fake_port_dict) get_ls.assert_called_with(self.context, network_id) get_mac.assert_called_with(self.context, fake_dict) @@ -968,7 +929,18 @@ class TestL2gwRpcDriver(base.BaseTestCase): delete_rpc.assert_called_with( self.context, 'fake_ovsdb_id', 'fake_uuid', ['fake_mac']) - def test_delete_port_mac(self): + @mock.patch.object(db, + 'get_all_logical_switches_by_name') + @mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=True) + @mock.patch.object(db, + 'get_all_vlan_bindings_by_logical_switch', + return_value=[1]) + @mock.patch.object(db, + 'get_logical_switch_by_name') + def test_delete_port_mac(self, get_ls, get_vlan_binding, get_mac, + get_all_ls): fake_port_list = [{'network_id': 'fake_network_id', 'device_owner': 'fake_owner', 'mac_address': 'fake_mac', @@ -982,22 +954,10 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'ovsdb_identifier': 'fake_ovsdb_id'} fake_rec_dict = {'logical_switch_id': 'fake_uuid', 'ovsdb_identifier': 'fake_ovsdb_id'} - with contextlib.nested( - mock.patch.object(db, - 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_dict), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=True), - mock.patch.object(self.plugin.agent_rpc, - 'delete_vif_from_gateway'), - mock.patch.object(db, - 'get_all_vlan_bindings_by_logical_switch', - return_value=[1]), - mock.patch.object(db, - 'get_logical_switch_by_name', - return_value=fake_logical_switch_dict)) as ( - get_all_ls, get_mac, delete_rpc, get_vlan_binding, get_ls): + get_all_ls.return_value = fake_logical_switch_dict + get_ls.return_value = fake_logical_switch_dict + with mock.patch.object(self.plugin.agent_rpc, + 'delete_vif_from_gateway') as delete_rpc: self.plugin.delete_port_mac(self.context, fake_port_list) self.assertFalse(get_all_ls.called) get_ls.assert_called_with(self.context, lg_dict) @@ -1006,7 +966,18 @@ class TestL2gwRpcDriver(base.BaseTestCase): delete_rpc.assert_called_with( self.context, 'fake_ovsdb_id', 'fake_uuid', ['fake_mac']) - def test_delete_port_mac_with_ovsdb_server_down(self): + @mock.patch.object(db, + 'get_all_logical_switches_by_name') + @mock.patch.object(db, + 'get_ucast_mac_remote_by_mac_and_ls', + return_value=True) + @mock.patch.object(db, 'add_pending_ucast_mac_remote') + @mock.patch.object(db, 'get_logical_switch_by_name') + @mock.patch.object(db, + 'get_all_vlan_bindings_by_logical_switch') + def test_delete_port_mac_with_ovsdb_server_down(self, get_vlan_binding, + get_ls, add_pending_mac, + get_mac, get_all_ls): "Test case to test delete_port_mac when the OVSDB server is down." fake_port_list = [{'network_id': 'fake_network_id', 'device_owner': 'fake_owner', @@ -1019,23 +990,11 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_dict = {'mac': 'fake_mac', 'logical_switch_uuid': 'fake_uuid', 'ovsdb_identifier': 'fake_ovsdb_id'} - with contextlib.nested( - mock.patch.object(db, - 'get_all_logical_switches_by_name', - return_value=fake_logical_switch_dict), - mock.patch.object(db, - 'get_ucast_mac_remote_by_mac_and_ls', - return_value=True), - mock.patch.object(self.plugin.agent_rpc, - 'delete_vif_from_gateway', - side_effect=RuntimeError), - mock.patch.object(db, 'add_pending_ucast_mac_remote'), - mock.patch.object(db, 'get_logical_switch_by_name', - return_value=fake_logical_switch_dict), - mock.patch.object(db, - 'get_all_vlan_bindings_by_logical_switch') - ) as (get_all_ls, get_mac, delete_rpc, add_pending_mac, get_ls, - get_vlan_binding): + get_all_ls.return_value = fake_logical_switch_dict + get_ls.return_value = fake_logical_switch_dict + with mock.patch.object(self.plugin.agent_rpc, + 'delete_vif_from_gateway', + side_effect=RuntimeError) as delete_rpc: self.plugin.delete_port_mac(self.context, fake_port_list) self.assertFalse(get_all_ls.called) get_ls.assert_called_with(self.context, lg_dict) @@ -1062,32 +1021,29 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'mac_address': 'fake_mac', 'ovsdb_identifier': 'fake_ovsdb_id'} DELETE = "DELETE" - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_admin_check', - return_value=True), - mock.patch.object(self.service_plugin, - 'get_l2_gateway_connection', - return_value=fake_conn_dict), - mock.patch.object(self.plugin, - '_get_identifer_list', - return_value=fake_identifier_list), - mock.patch.object(self.plugin, - '_get_set_of_ovsdb_ids', - return_value=fake_ovsdb_list), - mock.patch.object(self.service_plugin, - 'get_l2gateway_devices_by_gateway_id', - return_value=fake_device_list), - mock.patch.object(self.plugin, - '_process_port_list', - return_value=(ovsdb_id, logical_switch, - fake_port_dict)), - mock.patch.object(self.plugin.agent_rpc, - 'update_connection_to_gateway'), - mock.patch.object(self.plugin, - '_remove_vm_macs'), - ) as (admin_check, get_con, get_id_list, get_ovsdb_id, - get_devices, port_list, update_rpc, remove_vm_mac): + with mock.patch.object(self.service_plugin, + '_admin_check', + return_value=True) as admin_check, \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway_connection', + return_value=fake_conn_dict) as get_con, \ + mock.patch.object(self.plugin, + '_get_identifer_list', + return_value=fake_identifier_list) as get_id_list, \ + mock.patch.object(self.plugin, + '_get_set_of_ovsdb_ids', + return_value=fake_ovsdb_list) as get_ovsdb_id, \ + mock.patch.object(self.service_plugin, + 'get_l2gateway_devices_by_gateway_id', + return_value=fake_device_list) as get_devices, \ + mock.patch.object(self.plugin, + '_process_port_list', + return_value=(ovsdb_id, logical_switch, + fake_port_dict)) as port_list, \ + mock.patch.object(self.plugin.agent_rpc, + 'update_connection_to_gateway') as update_rpc, \ + mock.patch.object(self.plugin, + '_remove_vm_macs') as remove_vm_mac: self.plugin.delete_l2_gateway_connection(self.context, fake_conn_dict) admin_check.assert_called_with(self.context, 'DELETE') @@ -1120,24 +1076,21 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'tunnel_ip': 'fake_tunnel_ip', 'ovsdb_identifier': 'fake_ovsdb_id', 'switch_fault_status': 'DOWN'} - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_admin_check', - return_value=True), - mock.patch.object(self.service_plugin, - 'get_l2_gateway', return_value=fake_device), - mock.patch.object(db, 'get_physical_port_by_name_and_ps', - return_value=fake_physical_port), - mock.patch.object(db, 'get_physical_switch_by_name', - return_value=fake_physical_switch), - mock.patch.object(self.service_plugin, - '_get_network', - return_value=True), - mock.patch.object(self.service_plugin, - '_get_l2_gateway', - return_value=True) - ) as (get_l2gw, admin_check, phy_port, phy_switch, get_network, - get_l2gateway): + with mock.patch.object(self.service_plugin, + '_admin_check', + return_value=True), \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway', return_value=fake_device), \ + mock.patch.object(db, 'get_physical_port_by_name_and_ps', + return_value=fake_physical_port), \ + mock.patch.object(db, 'get_physical_switch_by_name', + return_value=fake_physical_switch), \ + mock.patch.object(self.service_plugin, + '_get_network', + return_value=True), \ + mock.patch.object(self.service_plugin, + '_get_l2_gateway', + return_value=True): self.assertRaises(l2gw_exc.L2GatewayPhysicalSwitchFaultStatus, self.plugin.create_l2_gateway_connection, self.db_context, @@ -1159,29 +1112,29 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'tunnel_ip': 'fake_tunnel_ip', 'ovsdb_identifier': 'fake_ovsdb_id', 'switch_fault_status': 'UP'} - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_admin_check', - return_value=True), - mock.patch.object(self.service_plugin, - 'get_l2_gateway', return_value=fake_device), - mock.patch.object(db, 'get_physical_port_by_name_and_ps', - return_value=fake_physical_port), - mock.patch.object(db, 'get_physical_switch_by_name', - return_value=fake_physical_switch), - mock.patch.object(self.service_plugin, - '_get_network', - return_value=True), - mock.patch.object(self.service_plugin, - '_get_l2_gateway', - return_value=True) - ) as (get_l2gw, admin_check, phy_port, phy_switch, get_network, - get_l2gateway): + with mock.patch.object(self.service_plugin, + '_admin_check', + return_value=True), \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway', return_value=fake_device), \ + mock.patch.object(db, 'get_physical_port_by_name_and_ps', + return_value=fake_physical_port), \ + mock.patch.object(db, 'get_physical_switch_by_name', + return_value=fake_physical_switch), \ + mock.patch.object(self.service_plugin, + '_get_network', + return_value=True), \ + mock.patch.object(self.service_plugin, + '_get_l2_gateway', + return_value=True): self.assertRaises(l2gw_exc.L2GatewayPhysicalPortFaultStatus, self.plugin.create_l2_gateway_connection, self.db_context, fake_l2gw_conn_dict) - def test_check_port_fault_status_and_switch_fault_status(self): + @mock.patch.object(db, 'get_physical_port_by_name_and_ps') + @mock.patch.object(db, 'get_physical_switch_by_name') + def test_check_port_fault_status_and_switch_fault_status( + self, phy_switch, phy_port): fake_device = {'devices': [{'device_name': 'fake_device', 'interfaces': [{'name': 'fake_interface'}]}]} self.db_context = ctx.get_admin_context() @@ -1196,15 +1149,11 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'tunnel_ip': 'fake_tunnel_ip', 'ovsdb_identifier': 'fake_ovsdb_id', 'switch_fault_status': None} - with contextlib.nested( - mock.patch.object(self.service_plugin, - 'get_l2_gateway', - return_value=fake_device), - mock.patch.object(db, 'get_physical_port_by_name_and_ps', - return_value=fake_physical_port), - mock.patch.object(db, 'get_physical_switch_by_name', - return_value=fake_physical_switch) - ) as (get_l2gw, phy_port, phy_switch): + phy_port.return_value = fake_physical_port + phy_switch.return_value = fake_physical_switch + with mock.patch.object(self.service_plugin, + 'get_l2_gateway', + return_value=fake_device) as get_l2gw: self.plugin._check_port_fault_status_and_switch_fault_status( mock.Mock(), mock.Mock()) self.assertTrue(get_l2gw.called) @@ -1233,38 +1182,35 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'ovsdb_identifier': 'fake_ovsdb_id', 'macs': [fake_port]} fake_pl_list = [fake_pl_dict] - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_admin_check', - return_value=True), + with mock.patch.object(self.service_plugin, + '_admin_check', + return_value=True) as admin_check, \ mock.patch.object(self.plugin, - '_validate_connection'), + '_validate_connection'), \ mock.patch.object(self.service_plugin, 'get_l2gateway_devices_by_gateway_id', - return_value=fake_device_list), + return_value=fake_device_list) as get_devices, \ mock.patch.object(self.plugin, '_process_port_list', return_value=(ovsdb_id, logical_switch, - fake_port)), + fake_port)) as port_list, \ mock.patch.object(self.plugin, '_get_logical_switch_dict', - return_value=fake_ls_dict), + return_value=fake_ls_dict) as get_ls, \ mock.patch.object(self.plugin, '_get_port_details', - return_value=fake_port_list), + return_value=fake_port_list) as get_port, \ mock.patch.object(self.plugin, '_get_ip_details', - return_value=('fake_ip1', 'fake_ip2')), - mock.patch.object(self.plugin, '_get_dict', return_value=mock.ANY), - mock.patch.object(db, 'get_ucast_mac_remote_by_mac_and_ls'), + return_value=('fake_ip1', 'fake_ip2')) as get_ip, \ + mock.patch.object(self.plugin, '_get_dict', return_value=mock.ANY) as get_dict, \ + mock.patch.object(db, 'get_ucast_mac_remote_by_mac_and_ls') as get_ucast_mac, \ mock.patch.object(self.plugin, '_get_locator_list', - return_value=fake_pl_list), + return_value=fake_pl_list) as get_pl, \ mock.patch.object(self.plugin.agent_rpc, - 'update_connection_to_gateway')) as ( - admin_check, validate, get_devices, port_list, get_ls, - get_port, get_ip, get_ucast_mac, get_dict, get_pl, update_rpc): + 'update_connection_to_gateway') as update_rpc: self.plugin.create_l2_gateway_connection(self.db_context, fake_l2gw_conn_dict) admin_check.assert_called_with(self.db_context, 'CREATE') @@ -1282,7 +1228,9 @@ class TestL2gwRpcDriver(base.BaseTestCase): self.assertTrue(get_pl.called) self.assertTrue(update_rpc.called) - def test_create_l2gateway_connection_with_invalid_device(self): + @mock.patch.object(db, 'get_physical_switch_by_name') + def test_create_l2gateway_connection_with_invalid_device(self, + phy_switch): self.db_context = ctx.get_admin_context() fake_l2gw_conn_dict = {'l2_gateway_connection': { 'id': 'fake_id', 'network_id': 'fake_network_id', @@ -1290,26 +1238,27 @@ class TestL2gwRpcDriver(base.BaseTestCase): fake_device = {'devices': [{'device_name': 'fake_device', 'interfaces': [{'name': 'fake_interface'}]}]} fake_physical_switch = None - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_admin_check', - return_value=True), - mock.patch.object(self.service_plugin, - 'get_l2_gateway', return_value=fake_device), - mock.patch.object(db, 'get_physical_switch_by_name', - return_value=fake_physical_switch), - mock.patch.object(self.service_plugin, - '_get_network', - return_value=True), - mock.patch.object(self.service_plugin, - '_get_l2_gateway', - return_value=True) - ) as (get_l2gw, admin_check, phy_switch, get_network, get_l2gateway): + phy_switch.return_value = fake_physical_switch + with mock.patch.object(self.service_plugin, + '_admin_check', + return_value=True), \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway', + return_value=fake_device), \ + mock.patch.object(self.service_plugin, + '_get_network', + return_value=True), \ + mock.patch.object(self.service_plugin, + '_get_l2_gateway', + return_value=True): self.assertRaises(l2gw_exc.L2GatewayDeviceNotFound, self.plugin.create_l2_gateway_connection, self.db_context, fake_l2gw_conn_dict) - def test_create_l2gateway_connection_with_invalid_interface(self): + @mock.patch.object(db, 'get_physical_switch_by_name') + @mock.patch.object(db, 'get_physical_port_by_name_and_ps') + def test_create_l2gateway_connection_with_invalid_interface( + self, phy_port, phy_switch): self.db_context = ctx.get_admin_context() fake_l2gw_conn_dict = {'l2_gateway_connection': { 'id': 'fake_id', 'network_id': 'fake_network_id', @@ -1322,24 +1271,20 @@ class TestL2gwRpcDriver(base.BaseTestCase): 'tunnel_ip': 'fake_tunnel_ip', 'ovsdb_identifier': 'fake_ovsdb_id', 'switch_fault_status': None} - with contextlib.nested( - mock.patch.object(self.service_plugin, - '_admin_check', - return_value=True), - mock.patch.object(self.service_plugin, - 'get_l2_gateway', return_value=fake_device), - mock.patch.object(db, 'get_physical_switch_by_name', - return_value=fake_physical_switch), - mock.patch.object(db, 'get_physical_port_by_name_and_ps', - return_value=fake_physical_port), - mock.patch.object(self.service_plugin, - '_get_network', - return_value=True), - mock.patch.object(self.service_plugin, - '_get_l2_gateway', - return_value=True) - ) as (get_l2gw, admin_check, phy_port, phy_switch, get_network, - get_l2gateway): + phy_switch.return_value = fake_physical_switch + phy_port.return_value = fake_physical_port + with mock.patch.object(self.service_plugin, + '_admin_check', + return_value=True), \ + mock.patch.object(self.service_plugin, + 'get_l2_gateway', + return_value=fake_device), \ + mock.patch.object(self.service_plugin, + '_get_network', + return_value=True), \ + mock.patch.object(self.service_plugin, + '_get_l2_gateway', + return_value=True): self.assertRaises(l2gw_exc.L2GatewayInterfaceNotFound, self.plugin.create_l2_gateway_connection, self.db_context, fake_l2gw_conn_dict) diff --git a/networking_l2gw/tests/unit/services/l2gateway/test_agent_scheduler.py b/networking_l2gw/tests/unit/services/l2gateway/test_agent_scheduler.py index e94d353c..4e7fa8cd 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/test_agent_scheduler.py +++ b/networking_l2gw/tests/unit/services/l2gateway/test_agent_scheduler.py @@ -14,7 +14,6 @@ # under the License. # -import contextlib import copy import datetime @@ -93,53 +92,44 @@ class TestAgentScheduler(base.BaseTestCase): self.fake_i_agent_list.append(make_inactive_agent( '2000', srv_const.AGENT_TYPE_L2GATEWAY, 52, config)) - def test_initialize_thread(self): - with contextlib.nested( - mock.patch.object(loopingcall, 'FixedIntervalLoopingCall'), - mock.patch.object(self.LOG, 'debug'), - mock.patch.object(self.LOG, 'error') - ) as (loop_call, debug, err): - self.agentsch.initialize_thread() - self.assertTrue(loop_call.called) - self.assertTrue(debug.called) - self.assertFalse(err.called) - - def test_initialize_thread_loop_call_exception(self): - with contextlib.nested( - mock.patch.object(loopingcall, 'FixedIntervalLoopingCall', - side_effect=RuntimeError), - mock.patch.object(self.LOG, 'error') - ) as (loop_call, log_err): + @mock.patch.object(loopingcall, 'FixedIntervalLoopingCall') + @mock.patch.object(agent_scheduler.LOG, 'debug') + @mock.patch.object(agent_scheduler.LOG, 'error') + def test_initialize_thread(self, err, debug, loop_call): + self.agentsch.initialize_thread() + self.assertTrue(loop_call.called) + self.assertTrue(debug.called) + self.assertFalse(err.called) + + @mock.patch.object(loopingcall, 'FixedIntervalLoopingCall', + side_effect=RuntimeError) + def test_initialize_thread_loop_call_exception(self, loop_call): + with mock.patch.object(self.LOG, 'error') as log_err: self.agentsch.initialize_thread() self.assertTrue(loop_call.called) self.assertTrue(log_err.called) - def test_select_agent_type_one_active(self): + @mock.patch.object(manager, 'NeutronManager') + def test_select_agent_type_one_active(self, mgr): config = {srv_const.L2GW_AGENT_TYPE: ''} self.populate_agent_lists(config) - with contextlib.nested( - mock.patch('__builtin__.sorted'), - mock.patch.object(manager, 'NeutronManager'), - mock.patch.object(self.LOG, 'exception') - ) as (mock_sorted, mgr, logger_call): + with mock.patch.object(self.LOG, 'exception'): self.agentsch._l2gwplugin = mock.Mock() self.agentsch._select_agent_type(self.context, self.fake_a_agent_list) self.agentsch.l2gwplugin.agent_rpc.set_monitor_agent_called_with( self.context, self.fake_a_agent_list[0]['host']) - def test_select_agent_type_multiple_active(self): + @mock.patch.object(manager, 'NeutronManager') + def test_select_agent_type_multiple_active(self, mgr): config = {srv_const.L2GW_AGENT_TYPE: ''} self.populate_agent_lists(config) self.fake_a_agent_list.append(make_active_agent( '1001', srv_const.AGENT_TYPE_L2GATEWAY, config)) self.agentsch._l2gwplugin = mock.Mock() - with contextlib.nested( - mock.patch.object(manager, 'NeutronManager'), - mock.patch.object(self.LOG, 'exception') - ) as (mgr, logger_call): + with mock.patch.object(self.LOG, 'exception'): self.agentsch._select_agent_type(self.context, self.fake_a_agent_list) self.agentsch.l2gwplugin.agent_rpc.set_monitor_agent_called_with( @@ -153,24 +143,23 @@ class TestAgentScheduler(base.BaseTestCase): self.fake_a_agent_list.append(make_active_agent( '1001', srv_const.AGENT_TYPE_L2GATEWAY, config)) - with contextlib.nested( - mock.patch.object(self.agentsch, '_select_agent_type'), - mock.patch.object(self.plugin, 'get_agents', - return_value=fake_all_agent_list), - mock.patch.object(self.agentsch, 'is_agent_down', - return_value=False) - ) as (select_agent, get_agent_list, is_agt): - self.agentsch.monitor_agent_state() - self.assertTrue(get_agent_list.called) - self.assertTrue(select_agent.called) - self.assertTrue(is_agt.called) + with mock.patch.object(self.agentsch, + '_select_agent_type') as select_agent: + with mock.patch.object( + self.plugin, 'get_agents', + return_value=fake_all_agent_list) as get_agent_list: + with mock.patch.object(self.agentsch, 'is_agent_down', + return_value=False) as is_agt: + self.agentsch.monitor_agent_state() + self.assertTrue(get_agent_list.called) + self.assertTrue(select_agent.called) + self.assertTrue(is_agt.called) def test_monitor_agent_state_exception_get_agents(self): - with contextlib.nested( - mock.patch.object(self.plugin, 'get_agents', - side_effect=Exception), - mock.patch.object(self.LOG, 'exception') - ) as (get_agent_list, exception_log): - self.agentsch.monitor_agent_state() - self.assertTrue(get_agent_list.called) - self.assertTrue(exception_log.called) + with mock.patch.object( + self.plugin, 'get_agents', + side_effect=Exception) as get_agent_list: + with mock.patch.object(self.LOG, 'exception') as exception_log: + self.agentsch.monitor_agent_state() + self.assertTrue(get_agent_list.called) + self.assertTrue(exception_log.called) diff --git a/networking_l2gw/tests/unit/services/l2gateway/test_plugin.py b/networking_l2gw/tests/unit/services/l2gateway/test_plugin.py index d83df1b1..1c220dff 100644 --- a/networking_l2gw/tests/unit/services/l2gateway/test_plugin.py +++ b/networking_l2gw/tests/unit/services/l2gateway/test_plugin.py @@ -14,7 +14,6 @@ # limitations under the License. import mock -import contextlib from neutron.tests import base from networking_l2gw.db.l2gateway import l2gateway_db @@ -82,110 +81,106 @@ class TestL2GatewayPlugin(base.BaseTestCase): self.plugin.delete_port_mac(self.context, {}) self.driver.delete_port_mac.assert_called_once_with(self.context, {}) - def test_create_l2_gateway(self): + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'validate_l2_gateway_for_create') + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'create_l2_gateway') + def test_create_l2_gateway(self, mock_create_l2gw_db, + mock_validate_for_create): fake_l2gw_id, fake_l2gw = self._get_fake_l2_gateway() - with contextlib.nested( - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'validate_l2_gateway_for_create'), - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'create_l2_gateway', - return_value=fake_l2gw) - ) as (mock_validate_for_create, mock_create_l2gw_db): - self.plugin.create_l2_gateway(self.context, fake_l2gw) - mock_validate_for_create.assert_called_with(self.context, - fake_l2gw) - mock_create_l2gw_db.assert_called_with(self.context, fake_l2gw) - self.driver.create_l2_gateway.assert_called_once_with(self.context, - fake_l2gw) - self.driver.create_l2_gateway_precommit.assert_called_once_with( - self.context, fake_l2gw) - self.driver.create_l2_gateway_postcommit.assert_called_once_with( - self.context, fake_l2gw) + mock_create_l2gw_db.return_value = fake_l2gw + self.plugin.create_l2_gateway(self.context, fake_l2gw) + mock_validate_for_create.assert_called_with(self.context, + fake_l2gw) + mock_create_l2gw_db.assert_called_with(self.context, fake_l2gw) + self.driver.create_l2_gateway.assert_called_once_with(self.context, + fake_l2gw) + self.driver.create_l2_gateway_precommit.assert_called_once_with( + self.context, fake_l2gw) + self.driver.create_l2_gateway_postcommit.assert_called_once_with( + self.context, fake_l2gw) - def test_delete_l2_gateway(self): + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'validate_l2_gateway_for_delete') + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'delete_l2_gateway') + def test_delete_l2_gateway(self, mock_delete_l2gw_db, + mock_validate_for_delete): fake_l2gw_id, fake_l2gw = self._get_fake_l2_gateway() - with contextlib.nested( - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'validate_l2_gateway_for_delete'), - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'delete_l2_gateway') - ) as (mock_validate_for_delete, mock_delete_l2gw_db): - self.plugin.delete_l2_gateway(self.context, fake_l2gw_id) - mock_validate_for_delete.assert_called_with(self.context, - fake_l2gw_id) - mock_delete_l2gw_db.assert_called_with(self.context, fake_l2gw_id) - self.driver.delete_l2_gateway.assert_called_once_with(self.context, - fake_l2gw_id) - self.driver.delete_l2_gateway_precommit.assert_called_once_with( - self.context, fake_l2gw_id) - self.driver.delete_l2_gateway_postcommit.assert_called_once_with( - self.context, fake_l2gw_id) + self.plugin.delete_l2_gateway(self.context, fake_l2gw_id) + mock_validate_for_delete.assert_called_with(self.context, + fake_l2gw_id) + mock_delete_l2gw_db.assert_called_with(self.context, fake_l2gw_id) + self.driver.delete_l2_gateway.assert_called_once_with(self.context, + fake_l2gw_id) + self.driver.delete_l2_gateway_precommit.assert_called_once_with( + self.context, fake_l2gw_id) + self.driver.delete_l2_gateway_postcommit.assert_called_once_with( + self.context, fake_l2gw_id) - def test_update_l2_gateway(self): + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'validate_l2_gateway_for_update') + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'update_l2_gateway') + def test_update_l2_gateway(self, mock_update_l2gw_db, + mock_validate_for_update): fake_l2gw_id, fake_l2gw = self._get_fake_l2_gateway() - with contextlib.nested( - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'validate_l2_gateway_for_update'), - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'update_l2_gateway', return_value=fake_l2gw) - ) as (mock_validate_for_update, mock_update_l2gw_db): - self.plugin.update_l2_gateway(self.context, - fake_l2gw_id, - fake_l2gw) - mock_validate_for_update.assert_called_with(self.context, - fake_l2gw_id, - fake_l2gw) - mock_update_l2gw_db.assert_called_with(self.context, fake_l2gw_id, - fake_l2gw) - self.driver.update_l2_gateway.assert_called_once_with(self.context, - fake_l2gw_id, - fake_l2gw) - self.driver.update_l2_gateway_precommit.assert_called_once_with( - self.context, fake_l2gw) - self.driver.update_l2_gateway_postcommit.assert_called_once_with( - self.context, fake_l2gw) + mock_update_l2gw_db.return_value = fake_l2gw + self.plugin.update_l2_gateway(self.context, + fake_l2gw_id, + fake_l2gw) + mock_validate_for_update.assert_called_with(self.context, + fake_l2gw_id, + fake_l2gw) + mock_update_l2gw_db.assert_called_with(self.context, fake_l2gw_id, + fake_l2gw) + self.driver.update_l2_gateway.assert_called_once_with(self.context, + fake_l2gw_id, + fake_l2gw) + self.driver.update_l2_gateway_precommit.assert_called_once_with( + self.context, fake_l2gw) + self.driver.update_l2_gateway_postcommit.assert_called_once_with( + self.context, fake_l2gw) - def test_create_l2_gateway_connection(self): + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'validate_l2_gateway_connection_for_create') + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'create_l2_gateway_connection') + def test_create_l2_gateway_connection(self, mock_conn_create_l2gw_db, + mock_validate_for_conn_create): fake_l2gw_conn_id, fake_l2gw_conn = ( self._get_fake_l2_gateway_connection()) - with contextlib.nested( - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'validate_l2_gateway_connection_for_create'), - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'create_l2_gateway_connection', - return_value=fake_l2gw_conn) - ) as (mock_validate_for_conn_create, mock_conn_create_l2gw_db): - self.plugin.create_l2_gateway_connection(self.context, - fake_l2gw_conn) - mock_validate_for_conn_create.assert_called_with(self.context, - fake_l2gw_conn) - mock_conn_create_l2gw_db.assert_called_with(self.context, - fake_l2gw_conn) - self.driver.create_l2_gateway_connection.assert_called_once_with( - self.context, fake_l2gw_conn) - (self.driver.create_l2_gateway_connection_precommit. - assert_called_once_with(self.context, fake_l2gw_conn)) - (self.driver.create_l2_gateway_connection_postcommit. - assert_called_once_with(self.context, fake_l2gw_conn)) + mock_conn_create_l2gw_db.return_value = fake_l2gw_conn + self.plugin.create_l2_gateway_connection(self.context, + fake_l2gw_conn) + mock_validate_for_conn_create.assert_called_with(self.context, + fake_l2gw_conn) + mock_conn_create_l2gw_db.assert_called_with(self.context, + fake_l2gw_conn) + self.driver.create_l2_gateway_connection.assert_called_once_with( + self.context, fake_l2gw_conn) + (self.driver.create_l2_gateway_connection_precommit. + assert_called_once_with(self.context, fake_l2gw_conn)) + (self.driver.create_l2_gateway_connection_postcommit. + assert_called_once_with(self.context, fake_l2gw_conn)) - def test_delete_l2_gateway_connection(self): + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'validate_l2_gateway_connection_for_delete') + @mock.patch.object(l2gateway_db.L2GatewayMixin, + 'delete_l2_gateway_connection') + def test_delete_l2_gateway_connection(self, mock_conn_delete_l2gw_db, + mock_validate_for_conn_delete): fake_l2gw_conn_id, fake_l2gw_conn = ( self._get_fake_l2_gateway_connection()) - with contextlib.nested( - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'validate_l2_gateway_connection_for_delete'), - mock.patch.object(l2gateway_db.L2GatewayMixin, - 'delete_l2_gateway_connection') - ) as (mock_validate_for_conn_delete, mock_conn_delete_l2gw_db): - self.plugin.delete_l2_gateway_connection(self.context, - fake_l2gw_conn_id) - mock_validate_for_conn_delete.assert_called_with(self.context, - fake_l2gw_conn_id) - mock_conn_delete_l2gw_db.assert_called_with(self.context, - fake_l2gw_conn_id) - self.driver.delete_l2_gateway_connection.assert_called_once_with( - self.context, fake_l2gw_conn_id) - (self.driver.delete_l2_gateway_connection_precommit. - assert_called_once_with(self.context, fake_l2gw_conn_id)) - (self.driver.delete_l2_gateway_connection_postcommit. - assert_called_once_with(self.context, fake_l2gw_conn_id)) + self.plugin.delete_l2_gateway_connection(self.context, + fake_l2gw_conn_id) + mock_validate_for_conn_delete.assert_called_with(self.context, + fake_l2gw_conn_id) + mock_conn_delete_l2gw_db.assert_called_with(self.context, + fake_l2gw_conn_id) + self.driver.delete_l2_gateway_connection.assert_called_once_with( + self.context, fake_l2gw_conn_id) + (self.driver.delete_l2_gateway_connection_precommit. + assert_called_once_with(self.context, fake_l2gw_conn_id)) + (self.driver.delete_l2_gateway_connection_postcommit. + assert_called_once_with(self.context, fake_l2gw_conn_id)) diff --git a/tox.ini b/tox.ini index b4170dcb..b1ed19bb 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27,pep8 +envlist = py35,py34,py27,pep8 minversion = 1.6 skipsdist = True @@ -37,7 +37,7 @@ commands = python setup.py build_sphinx # E123, E125 skipped as they are invalid PEP-8. show-source = True -ignore = E123,H803,H302 +ignore = E123,H803,H302,H904 builtins = _ exclude=.venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build