Commit 7ce173e1 authored by Nicolas Wavrant's avatar Nicolas Wavrant

test_runner: adds tests for the isSoftwareReleaseReady function

parent 76f2188a
...@@ -644,7 +644,7 @@ def getSoftwareReleaseName(config): ...@@ -644,7 +644,7 @@ def getSoftwareReleaseName(config):
project = open(sr_profile, "r").read().split("/") project = open(sr_profile, "r").read().split("/")
software = project[-2] software = project[-2]
return software.replace(' ', '_') return software.replace(' ', '_')
return "No_name" return None
def removeSoftwareRootDirectory(config, md5, folder_name): def removeSoftwareRootDirectory(config, md5, folder_name):
""" """
...@@ -835,27 +835,29 @@ def readParameters(path): ...@@ -835,27 +835,29 @@ def readParameters(path):
else: else:
return "No such file or directory: %s" % path return "No such file or directory: %s" % path
def isSoftwareReleaseCompleted(config):
software_name = getSoftwareReleaseName(config)
if software_name is None:
return False
elif os.path.exists(os.path.join(config['runner_workdir'],
'softwareLink', software_name, '.completed')):
return True
else:
return False
def isSoftwareReleaseReady(config): def isSoftwareReleaseReady(config):
"""Return 1 if the Software Release has """Return 1 if the Software Release has
correctly been deployed, 0 if not, correctly been deployed, 0 if not,
and 2 if it is currently deploying""" and 2 if it is currently deploying"""
slapos_software = (False if config.get('slapos-software', None) is None else True) slapos_software = config.get('slapos-software', None) is not None
# auto_deploy and auto_run are True only if slapos_software has been declared # auto_deploy and auto_run are True only if slapos_software has been declared
auto_deploy = (config['auto_deploy'] in TRUE_VALUES) and slapos_software auto_deploy = (config['auto_deploy'] in TRUE_VALUES) and slapos_software
auto_run = (config['autorun'] in TRUE_VALUES) and slapos_software auto_run = (config['autorun'] in TRUE_VALUES) and slapos_software
project = os.path.join(config['etc_dir'], '.project') project = os.path.join(config['etc_dir'], '.project')
if not ( os.path.exists(project) and (auto_run or auto_deploy) ): if not ( os.path.exists(project) and (auto_run or auto_deploy) ):
return "0" return "0"
path = open(project, 'r').readline().strip()
software_name = path
if software_name[-1] == '/':
software_name = software_name[:-1]
software_name = software_name.split('/')[-1]
updateInstanceParameter(config) updateInstanceParameter(config)
config_SR_folder(config) if isSoftwareReleaseCompleted(config):
if os.path.exists(os.path.join(config['runner_workdir'],
'softwareLink', software_name, '.completed')):
if auto_run: if auto_run:
runSlapgridUntilSuccess(config, 'instance') runSlapgridUntilSuccess(config, 'instance')
return "1" return "1"
...@@ -864,8 +866,6 @@ def isSoftwareReleaseReady(config): ...@@ -864,8 +866,6 @@ def isSoftwareReleaseReady(config):
return "2" return "2"
elif auto_deploy: elif auto_deploy:
runSoftwareWithLock(config) runSoftwareWithLock(config)
config_SR_folder(config)
time.sleep(15)
if auto_run: if auto_run:
runSlapgridUntilSuccess(config, 'instance') runSlapgridUntilSuccess(config, 'instance')
return "2" return "2"
......
...@@ -92,11 +92,13 @@ class TestRunnerBackEnd(unittest.TestCase): ...@@ -92,11 +92,13 @@ class TestRunnerBackEnd(unittest.TestCase):
@mock.patch('os.mkdir') @mock.patch('os.mkdir')
@mock.patch('slapos.runner.utils.updateProxy') @mock.patch('slapos.runner.utils.updateProxy')
@mock.patch('slapos.runner.utils.requestInstance')
@mock.patch('slapos.runner.utils.config_SR_folder') @mock.patch('slapos.runner.utils.config_SR_folder')
def _runSlapgridWithLockMakesCorrectCallsToSupervisord(self, def _runSlapgridWithLockMakesCorrectCallsToSupervisord(self,
run_slapgrid_function, run_slapgrid_function,
process_name, process_name,
mock_configSRFolder, mock_configSRFolder,
mock_requestInstance,
mock_updateProxy, mock_updateProxy,
mock_mkdir): mock_mkdir):
""" """
...@@ -105,7 +107,8 @@ class TestRunnerBackEnd(unittest.TestCase): ...@@ -105,7 +107,8 @@ class TestRunnerBackEnd(unittest.TestCase):
""" """
mock_updateProxy.return_value = True mock_updateProxy.return_value = True
cwd = os.getcwd() cwd = os.getcwd()
config = {'software_root': os.path.join(cwd, 'software'), config = {'etc_dir': cwd,
'software_root': os.path.join(cwd, 'software'),
'software_log': os.path.join(cwd, 'software.log'), 'software_log': os.path.join(cwd, 'software.log'),
'instance_root': os.path.join(cwd, 'software'), 'instance_root': os.path.join(cwd, 'software'),
'instance_log': os.path.join(cwd, 'software.log')} 'instance_log': os.path.join(cwd, 'software.log')}
...@@ -296,6 +299,67 @@ class TestRunnerBackEnd(unittest.TestCase): ...@@ -296,6 +299,67 @@ class TestRunnerBackEnd(unittest.TestCase):
# if running software fails, then no need to try to deploy instances # if running software fails, then no need to try to deploy instances
self.assertEqual(mock_runInstanceWithLock.call_count, 0) self.assertEqual(mock_runInstanceWithLock.call_count, 0)
@mock.patch('os.path.exists')
@mock.patch('slapos.runner.utils.updateInstanceParameter')
@mock.patch('slapos.runner.utils.isSoftwareReleaseCompleted')
@mock.patch('slapos.runner.utils.runSlapgridUntilSuccess')
@mock.patch('slapos.runner.utils.isSoftwareRunning')
@mock.patch('slapos.runner.utils.runSoftwareWithLock')
def test_isSoftwareReleaseReady(self,
mock_runSoftwareWithLock,
mock_isSoftwareRunning,
mock_runSlapgridUntilSuccess,
mock_isSoftwareReleaseCompleted,
mock_updateInstanceParameter,
mock_path_exists):
cwd = os.getcwd()
config = {
'etc_dir': cwd,
'runner_workdir': cwd,
'slapos-software': 'slapos/dummy',
'auto_deploy': False,
'autorun': False,
}
# Every parameter is False, so do nothing
self.assertEqual(runner_utils.isSoftwareReleaseReady(config), '0')
# We define a software, so from now we expect to build
mock_path_exists.return_value = True
# auto_deploy is True, so Software Release should build
config.update({
'auto_deploy': True,
})
# slapgrid-sr is running
mock_isSoftwareRunning.return_value = True
mock_isSoftwareReleaseCompleted.return_value = False
self.assertEqual(runner_utils.isSoftwareReleaseReady(config), '2')
# SR is not built, and slapgrid-sr is not running, so it should be started
mock_isSoftwareRunning.return_value = False
mock_isSoftwareReleaseCompleted.return_value = False
self.assertEqual(runner_utils.isSoftwareReleaseReady(config), '2')
self.assertTrue(mock_runSoftwareWithLock.called)
mock_runSoftwareWithLock.reset_mock()
# SR is built
mock_isSoftwareReleaseCompleted.return_value = True
self.assertEqual(runner_utils.isSoftwareReleaseReady(config), '1')
# If autorun is True, Instance is expected to build too
config.update({
'autorun': True,
})
mock_isSoftwareRunning.return_value = False
mock_isSoftwareReleaseCompleted.return_value = True
self.assertEqual(runner_utils.isSoftwareReleaseReady(config), '1')
mock_runSlapgridUntilSuccess.assert_called_with(config, 'instance')
@unittest.skip('No scenario defined') @unittest.skip('No scenario defined')
def test_autoDeployWontEraseExistingInstances(self): def test_autoDeployWontEraseExistingInstances(self):
raise NotImplementedError raise NotImplementedError
......
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