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. Change-Id: I97aeb8d1d5773ef3d59e8f908aea34ccceb38378 Related-Bug: 1561046
This commit is contained in:
parent
fc1db4690f
commit
2690eed19a
|
@ -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
|
||||
|
||||
|
@ -56,3 +58,30 @@ class AdminDirFixture(fixtures.Fixture):
|
|||
delete_cmd = ['rm', '-r', self.directory]
|
||||
utils.execute(create_cmd, run_as_root=True)
|
||||
self.addCleanup(utils.execute, delete_cmd, run_as_root=True)
|
||||
|
||||
|
||||
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
|
||||
|
|
|
@ -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 neutron._i18n import _
|
||||
|
@ -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
|
||||
|
||||
|
||||
|
@ -86,7 +83,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:
|
||||
|
@ -110,23 +107,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
|
||||
|
|
Loading…
Reference in New Issue