Commit 5b0a1273 authored by Cédric Le Ninivin's avatar Cédric Le Ninivin

wip one test passing

slapos/tests/test_slapgrid.py::
TestSlapgridSoftwareRelease::
test_one_software_buildout_fail_is_correctly_logged
parent 124b4537
......@@ -670,7 +670,7 @@ stderr_logfile_backups=1
"portal_type": "Software Installation",
"compute_node_id": self.computer_id,
"software_release_uri": software_release_uri,
"error_status": exc,
"error_status": str(exc),
})
else:
software_release["compatibility_software_release"].error(exc, logger=self.logger)
......
......@@ -169,7 +169,7 @@ class BasicMixin(object):
logger=logging.getLogger(),
shared_part_list=self.shared_parts_root,
force_stop=force_stop,
#slapgrid_jio_uri=self.master_url + "api/",
slapgrid_jio_uri=self.master_url + "api/",
)
self.grid._manager_list = self.manager_list
# monkey patch buildout bootstrap
......@@ -355,6 +355,28 @@ class MasterMixin(BasicMixin):
self._unmock_sleep()
BasicMixin.tearDown(self)
class SlapToolMasterMixin(MasterMixin):
def setSlapgrid(self, develop=False, force_stop=False):
if getattr(self, 'master_url', None) is None:
self.master_url = 'http://127.0.0.1:80/'
self.computer_id = 'computer'
self.supervisord_socket = os.path.join(self._tempdir, 'sv.sock')
self.supervisord_configuration_path = os.path.join(self._tempdir,
'supervisord')
self.usage_report_periodicity = 1
self.buildout = None
self.grid = slapgrid.Slapgrid(self.software_root,
self.instance_root,
self.master_url,
self.computer_id,
self.buildout,
develop=develop,
logger=logging.getLogger(),
shared_part_list=self.shared_parts_root,
force_stop=force_stop,
)
self.grid._manager_list = self.manager_list
# monkey patch buildout bootstrap
class ComputerForTest(object):
"""
......@@ -400,8 +422,30 @@ class ComputerForTest(object):
qs = parse.parse_qs(req.body)
# Catch API calls
#if (url.path.startwith("/api")):
# pass
if url.path.startswith('/api/'):
content = json.loads(req.body)
#import pdb; pdb.set_trace()
if (url.path == '/api/allDocs/'):
if content["portal_type"] == "Software Installation":
return json.dumps({
"result_list": [{
"software_release_uri": x.name,
"portal_type": "Software Installation",
"compute_node_id": content["compute_node_id"],
"state": x.requested_state
} for x in self.software_list]
})
elif (url.path == '/api/put/'):
if content["portal_type"] == "Software Installation":
software = self.software_list[0]
software.sequence.append((url.path, content))
if "error_status" in content:
software.error_log = content['error_status']
software.error = True
return json.dumps({"id": content["software_release_uri"]})
raise ValueError("Unexcepted call to API. URL:%s Content:%s" % (url.path, req.body))
if (url.path == '/getFullComputerInformation'
and 'computer_id' in qs):
......@@ -1981,7 +2025,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertEqual(computer.sequence, ['/getFullComputerInformation'])
class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase):
class TestSlapgridSoftwareReleaseSlapTool(SlapToolMasterMixin, unittest.TestCase):
fake_waiting_time = 0.05
def test_one_software_buildout_fail_is_correctly_logged(self):
......@@ -2050,6 +2094,95 @@ chmod a-rxw directory
self.launchSlapgridSoftware()
self.assertEqual(os.listdir(self.software_root), [])
class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase):
fake_waiting_time = 0.05
def test_one_software_buildout_fail_is_correctly_logged(self):
"""
1. We set up a software using a corrupted buildout
2. It will fail, make sure that whole log is sent to master
"""
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
software = computer.software_list[0]
line1 = "Nerdy kitten: Can I haz a process crash?"
line2 = "Cedric: Sure, here it is."
software.setBuildout("""#!/bin/sh
echo %s; echo %s; exit 42""" % (line1, line2))
self.launchSlapgridSoftware()
self.assertEqual(
software.sequence,
[
(
'/api/put/',
{
'compute_node_id': self.computer_id,
'portal_type': 'Software Installation',
'reported_state': 'building',
'software_release_uri': software.name,
}
),
(
'/api/put/',
{
'compute_node_id': self.computer_id,
'portal_type': 'Software Installation',
'error_status': software.error_log,
'software_release_uri': software.name,
}
)
]
)
# We don't care of actual formatting, we just want to have full log
self.assertIn(line1, software.error_log)
self.assertIn(line2, software.error_log)
self.assertIn('Failed to run buildout', software.error_log)
def test_software_install_generate_buildout_cfg_with_shared_part_list(self):
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
software = computer.software_list[0]
# examine the genrated buildout
software.setBuildout("""#!/bin/sh
cat buildout.cfg; exit 1""")
self.launchSlapgridSoftware()
self.assertIn('shared-part-list = %s' % self.shared_parts_root, software.error_log)
def test_remove_software(self):
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
software = computer.software_list[0]
software.setBuildout("""#!/bin/sh
mkdir directory
touch directory/file
""")
self.launchSlapgridSoftware()
self.assertIn('directory', os.listdir(os.path.join(self.software_root, software.software_hash)))
software.requested_state = 'destroyed'
self.launchSlapgridSoftware()
self.assertEqual(os.listdir(self.software_root), [])
def test_remove_software_chmod(self):
# This software is "hard" to remove, as permissions have been changed
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
software = computer.software_list[0]
software.setBuildout("""#!/bin/sh
mkdir directory
touch directory/file
chmod a-rxw directory/file
chmod a-rxw directory
""")
self.launchSlapgridSoftware()
self.assertIn('directory', os.listdir(os.path.join(self.software_root, software.software_hash)))
software.requested_state = 'destroyed'
self.launchSlapgridSoftware()
self.assertEqual(os.listdir(self.software_root), [])
class SlapgridInitialization(unittest.TestCase):
"""
......
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