]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Stop any spawned ProcessMonitor at test cleanup
authorMiguel Angel Ajo <mangelajo@redhat.com>
Tue, 24 Mar 2015 13:07:37 +0000 (13:07 +0000)
committerMiguel Angel Ajo <mangelajo@redhat.com>
Thu, 26 Mar 2015 18:17:00 +0000 (18:17 +0000)
Base test class adds a cleanup fixture to stop any
spawned process monitor via unit or functional tests,
which otherwise would keep running after the tests already
finished, and execution functions go unpatched.

Without this patch unit tests will randomly fail
when we enable process monitor by default at change:
I85fe31bee30714148168a293eea29fa0a37f9701

Co-Authored-By: Maru Newby <marun@redhat.com>
Change-Id: Ide799a52391b14ff921de25788e8b0f0463fb8f8

neutron/agent/linux/external_process.py
neutron/tests/base.py
neutron/tests/functional/agent/linux/test_keepalived.py
neutron/tests/functional/agent/linux/test_process_monitor.py

index 95beddd99df52401451ce20eceb53edc15984515..0dff4efa88a87a69d5d7a626ced93145ad593b17 100644 (file)
@@ -199,7 +199,11 @@ class ProcessMonitor(object):
         self._monitored_processes.pop(service_id, None)
 
     def stop(self):
-        """Stop the process monitoring. """
+        """Stop the process monitoring.
+
+        This method will stop the monitoring thread, but no monitored
+        process will be stopped.
+        """
         self._monitor_processes = False
 
     def _spawn_checking_thread(self):
index 6886af9e3cf5f2c95c8c9a2842b141c596b15027..d9e61f0613cf80ac53e9055c5cad1e4a1c40511d 100644 (file)
@@ -27,10 +27,12 @@ import logging as std_logging
 import os.path
 
 import fixtures
+import mock
 from oslo_concurrency.fixture import lockutils
 from oslo_config import cfg
 from oslo_messaging import conffixture as messaging_conffixture
 
+from neutron.agent.linux import external_process
 from neutron.common import config
 from neutron.common import rpc as n_rpc
 from neutron import policy
@@ -61,6 +63,28 @@ def fake_consume_in_threads(self):
 bool_from_env = sub_base.bool_from_env
 
 
+class ProcessMonitorFixture(fixtures.Fixture):
+    """Test fixture to capture and cleanup any spawn process monitor."""
+    def setUp(self):
+        super(ProcessMonitorFixture, self).setUp()
+        self.old_callable = (
+            external_process.ProcessMonitor._spawn_checking_thread)
+        p = mock.patch("neutron.agent.linux.external_process.ProcessMonitor."
+                       "_spawn_checking_thread",
+                       new=lambda x: self.record_calls(x))
+        p.start()
+        self.instances = []
+        self.addCleanup(self.stop)
+
+    def stop(self):
+        for instance in self.instances:
+            instance.stop()
+
+    def record_calls(self, instance):
+        self.old_callable(instance)
+        self.instances.append(instance)
+
+
 class BaseTestCase(sub_base.SubBaseTestCase):
 
     @staticmethod
@@ -97,6 +121,7 @@ class BaseTestCase(sub_base.SubBaseTestCase):
         cfg.CONF.set_override('state_path', self.get_default_temp_dir().path)
 
         self.addCleanup(CONF.reset)
+        self.useFixture(ProcessMonitorFixture())
 
         self.useFixture(fixtures.MonkeyPatch(
             'neutron.common.exceptions.NeutronException.use_fatal_exceptions',
index 9e1060ea07ee374df49946e20c558d151adc868f..f0fe113e61f365742efabe8357eb5f3f09c34f17 100644 (file)
@@ -36,7 +36,6 @@ class KeepalivedManagerTestCase(base.BaseTestCase,
             'router1', self.expected_config, conf_path=cfg.CONF.state_path,
             process_monitor=self.process_monitor)
         self.addCleanup(self.manager.get_process().disable)
-        self.addCleanup(self.process_monitor.stop)
 
     def test_keepalived_spawn(self):
         self.manager.spawn()
index d202d2e0e3cf187c9a78ec4c3901ca5a46893447..1bf50803fc6e567d99eca5f65e84edb61524dc24 100644 (file)
@@ -34,7 +34,6 @@ class BaseTestProcessMonitor(base.BaseTestCase):
         self._process_monitor = None
         self.create_child_processes_manager('respawn')
         self.addCleanup(self.cleanup_spawned_children)
-        self.addCleanup(self._process_monitor.stop)
 
     def create_child_processes_manager(self, action):
         cfg.CONF.set_override('check_child_processes_action', action, 'AGENT')