From 7175fdef97d8d2d18c0f71f3c089fa5341b73d9e Mon Sep 17 00:00:00 2001 From: Hynek Mlnarik Date: Wed, 30 Mar 2016 10:44:09 +0200 Subject: [PATCH] Refactor and fix dummy process fixture Extracting the test fixture that creates a new process and leaves it running for a given amount of time into helpers where other fixtures for functional tests live. This both keeps the fixtures at one place and increases visibility of the fixture so that it can be reused in other tests. At the same time, the fixture is fixed as the original code omitted starting the process. Conflicts: neutron/tests/functional/agent/linux/helpers.py Change-Id: I97aeb8d1d5773ef3d59e8f908aea34ccceb38378 Related-Bug: 1561046 (cherry picked from commit 2690eed19a749fb1b50bb38f3d01fce0f1497f39) --- .../tests/functional/agent/linux/helpers.py | 29 +++++++++++++++++++ .../functional/agent/linux/test_keepalived.py | 27 ++--------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/neutron/tests/functional/agent/linux/helpers.py b/neutron/tests/functional/agent/linux/helpers.py index 9980293aa75..246bb23ada4 100644 --- a/neutron/tests/functional/agent/linux/helpers.py +++ b/neutron/tests/functional/agent/linux/helpers.py @@ -12,7 +12,9 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import multiprocessing import os +import time import fixtures @@ -34,3 +36,30 @@ class RecursivePermDirFixture(fixtures.Fixture): os.chmod(current_directory, perms | self.least_perms) previous_directory = current_directory current_directory = os.path.dirname(current_directory) + + +class SleepyProcessFixture(fixtures.Fixture): + """ + Process fixture that performs time.sleep for the given number of seconds. + """ + + def __init__(self, timeout=60): + super(SleepyProcessFixture, self).__init__() + self.timeout = timeout + + @staticmethod + def yawn(seconds): + time.sleep(seconds) + + def _setUp(self): + self.process = multiprocessing.Process(target=self.yawn, + args=[self.timeout]) + self.process.start() + self.addCleanup(self.destroy) + + def destroy(self): + self.process.terminate() + + @property + def pid(self): + return self.process.pid diff --git a/neutron/tests/functional/agent/linux/test_keepalived.py b/neutron/tests/functional/agent/linux/test_keepalived.py index 85977932832..aec696f406b 100644 --- a/neutron/tests/functional/agent/linux/test_keepalived.py +++ b/neutron/tests/functional/agent/linux/test_keepalived.py @@ -13,10 +13,6 @@ # License for the specific language governing permissions and limitations # under the License. -import fixtures -from multiprocessing import Process -import time - from oslo_config import cfg from oslo_log import log as logging @@ -24,6 +20,7 @@ from neutron.agent.linux import external_process from neutron.agent.linux import keepalived from neutron.agent.linux import utils from neutron.tests import base +from neutron.tests.functional.agent.linux import helpers from neutron.tests.unit.agent.linux import test_keepalived @@ -89,7 +86,7 @@ class KeepalivedManagerTestCase(base.BaseTestCase, # existing non-keepalived process. This situation can happen e.g. # after hard node reset. - spawn_process = SleepyProcessFixture() + spawn_process = helpers.SleepyProcessFixture() self.useFixture(spawn_process) with open(pid_file, "w") as f_pid_file: @@ -113,23 +110,3 @@ class KeepalivedManagerTestCase(base.BaseTestCase, self._test_keepalived_spawns_conflicting_pid( process, self.manager.get_vrrp_pid_file_name(pid_file)) - - -class SleepyProcessFixture(fixtures.Fixture): - - def __init__(self): - super(SleepyProcessFixture, self).__init__() - - @staticmethod - def yawn(): - time.sleep(60) - - def _setUp(self): - self.process = Process(target=self.yawn) - - def destroy(self): - self.process.terminate() - - @property - def pid(self): - return self.process.pid