From 086496bfc45e01cd2905a074d526a7d513bf4ec2 Mon Sep 17 00:00:00 2001 From: Kevin Benton Date: Sat, 20 Sep 2014 10:48:22 -0700 Subject: [PATCH] Mock out all RPC calls with a fixture Mock out the rpc proxy calls used by various agents to prevent unit tests from blocking for 10+ seconds while waiting for a timeout. This happened with the OVS agent unit tests recently in Change-ID Idd770a85a9eabff112d9613e75d8bb524020234a. This change results in a reduction from 330.8 seconds to 2.7 seconds for the neutron.tests.unit.openvswitch.test_ovs_neutron_agent test module. Closes-Bug: #1372076 Change-Id: I5e6794dc33c64c8fe309d8e72a8af3385c7d4442 --- neutron/tests/base.py | 20 +++++++++++++------ .../openvswitch/test_ovs_neutron_agent.py | 3 +++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/neutron/tests/base.py b/neutron/tests/base.py index adda2a0bd..dae1320de 100644 --- a/neutron/tests/base.py +++ b/neutron/tests/base.py @@ -125,11 +125,25 @@ class BaseTestCase(testtools.TestCase): 'neutron.common.exceptions.NeutronException.use_fatal_exceptions', fake_use_fatal_exceptions)) + self.setup_rpc_mocks() + + if sys.version_info < (2, 7) and getattr(self, 'fmt', '') == 'xml': + raise self.skipException('XML Testing Skipped in Py26') + + self.setup_config() + self.addOnException(self.check_for_systemexit) + + def setup_rpc_mocks(self): # don't actually start RPC listeners when testing self.useFixture(fixtures.MonkeyPatch( 'neutron.common.rpc.Connection.consume_in_threads', fake_consume_in_threads)) + # immediately return RPC calls + self.useFixture(fixtures.MonkeyPatch( + 'neutron.common.rpc.RpcProxy._RpcProxy__call_rpc_method', + mock.MagicMock())) + self.useFixture(fixtures.MonkeyPatch( 'oslo.messaging.Notifier', fake_notifier.FakeNotifier)) @@ -144,12 +158,6 @@ class BaseTestCase(testtools.TestCase): self.addCleanup(n_rpc.cleanup) n_rpc.init(CONF) - if sys.version_info < (2, 7) and getattr(self, 'fmt', '') == 'xml': - raise self.skipException('XML Testing Skipped in Py26') - - self.setup_config() - self.addOnException(self.check_for_systemexit) - def check_for_systemexit(self, exc_info): if isinstance(exc_info[1], SystemExit): self.fail("A SystemExit was raised during the test. %s" diff --git a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py index 3289b3496..6a214dd6f 100644 --- a/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py +++ b/neutron/tests/unit/openvswitch/test_ovs_neutron_agent.py @@ -136,6 +136,9 @@ class TestOvsNeutronAgent(base.BaseTestCase): 'FixedIntervalLoopingCall', new=MockFixedIntervalLoopingCall)): self.agent = ovs_neutron_agent.OVSNeutronAgent(**kwargs) + # set back to true because initial report state will succeed due + # to mocked out RPC calls + self.agent.use_call = True self.agent.tun_br = mock.Mock() self.agent.sg_agent = mock.Mock()