Commit fdd15b4c authored by Jérome Perrin's avatar Jérome Perrin

testnode: refuse to erase an instance directory if supervisor is already runnning there

parent 8c33379a
import unittest
import mock
from unittest import TestCase
from contextlib import contextmanager
......@@ -1138,3 +1139,36 @@ shared = true
RunnerClass.updateDictionaryFile = original_updateDictionaryFile
RunnerClass._createInstance = original__createInstance
RunnerClass._waitInstanceCreation = original__waitInstanceCreation
def test_cleanup_supervisord_from_previous_run(self):
test_node = self.getTestNode()
runner = test_type_registry['UnitTest'](test_node)
node_test_suite = test_node.getNodeTestSuite('foo')
# XXX this have to be cleaned up
host, port = '127.0.0.1', 1234 # XXX
test_node.config['ipv4_address'] = host
test_node.config['ipv6_address'] = '::1' # TODO
test_node.config['proxy_host'] = host
test_node.config['proxy_port'] = port
test_node.config['master_url'] = "http://{proxy_host}:{proxy_port}".format(**test_node.config)
test_node.config['slapos_binary'] = '/usr/bin/slapos' # XXX must be in path (and not be the slaprunner wrapper !)
test_node.config['partition_reference'] = self.id()
test_node.config['environment'] = os.environ
test_node.config["software_list"] = []
self.addCleanup(test_node.process_manager.killPreviousRun)
with mock.patch(
'erp5.util.testnode.UnitTestRunner.SlapOSControler.runSoftwareRelease',
return_value={'status_code': 0}):
# start slapos node's supervisord
runner.prepareSlapOSForTestSuite(node_test_suite)
# Here we are in a state where supervisor is started, maybe by human
# logged in on test node, maybe an unexpected error in previous run.
# preparing slapos again should report this as an error and refuse starting.
# XXX or maybe just terminate the previous supervisor and start ?
with self.assertRaisesRegexp(RuntimeError, 'Supervisord is already running'):
runner.prepareSlapOSForTestSuite(node_test_suite)
import os
import stat
import shutil
import psutil
import logging
def rmtree(path):
"""Delete a path recursively.
......@@ -30,6 +32,19 @@ def createFolder(folder, clean=False):
if os.path.exists(folder):
if not clean:
return
# before removing, check that we don't have a supervisord left here,
# and refuse to erase this directory in that case.
supervisord_pid_file = os.path.join(folder, 'var/run/supervisord.pid')
if os.path.exists(supervisord_pid_file):
with open(supervisord_pid_file, 'r') as f:
pid = int(f.read().strip())
supervisord = psutil.Process(pid)
if supervisord.cmdline()[-1].endswith("supervisor.supervisord.main()"):
logger = logging.getLogger()
logger.error("Supervisord is already running in this directory ( pid: %s )", pid)
# XXX terminate ?
# supervisord.terminate()
raise RuntimeError("Supervisord is already running")
rmtree(folder)
os.mkdir(folder)
......
......@@ -81,6 +81,7 @@ setup(name=name,
test_suite='erp5.tests',
tests_require=[
'slapos.core',
'mock',
'xml_marshaller',
'psutil >= 0.5.0',
],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment