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 2690eed19a)
This commit is contained in:
Hynek Mlnarik 2016-03-30 10:44:09 +02:00
parent 6dea586906
commit 7175fdef97
2 changed files with 31 additions and 25 deletions

View File

@ -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

View File

@ -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