Commit 11e60887 authored by Marco Mariani's avatar Marco Mariani

update tests conforming to requests API

parent 4da82657
...@@ -66,7 +66,8 @@ setup(name=name, ...@@ -66,7 +66,8 @@ setup(name=name,
'bpython_console': ('bpython',)}, 'bpython_console': ('bpython',)},
tests_require=[ tests_require=[
'pyflakes', 'pyflakes',
'mock' 'mock',
'httmock',
], ],
zip_safe=False, # proxy depends on Flask, which has issues with zip_safe=False, # proxy depends on Flask, which has issues with
# accessing templates # accessing templates
......
...@@ -495,7 +495,7 @@ def forwardRequestToExternalMaster(master_url, request_form): ...@@ -495,7 +495,7 @@ def forwardRequestToExternalMaster(master_url, request_form):
filter_kw['source_instance_id'] = partition_reference filter_kw['source_instance_id'] = partition_reference
new_request_form['filter_xml'] = dumps(filter_kw) new_request_form['filter_xml'] = dumps(filter_kw)
partition = loads(slap._connection_helper.POST('/requestComputerPartition', new_request_form)) partition = loads(slap._connection_helper.POST('/requestComputerPartition', data=new_request_form))
# XXX move to other end # XXX move to other end
partition._master_url = master_url partition._master_url = master_url
......
...@@ -338,7 +338,7 @@ class Computer(SlapDocument): ...@@ -338,7 +338,7 @@ class Computer(SlapDocument):
'message': message}) 'message': message})
def getStatus(self): def getStatus(self):
xml = self._connection_helper.GET('getComputerStatus', {'computer_id': self._computer_id}) xml = self._connection_helper.GET('getComputerStatus', params={'computer_id': self._computer_id})
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def revokeCertificate(self): def revokeCertificate(self):
...@@ -548,7 +548,7 @@ class ComputerPartition(SlapRequester): ...@@ -548,7 +548,7 @@ class ComputerPartition(SlapRequester):
def getCertificate(self): def getCertificate(self):
xml = self._connection_helper.GET('getComputerPartitionCertificate', xml = self._connection_helper.GET('getComputerPartitionCertificate',
{ params={
'computer_id': self._computer_id, 'computer_id': self._computer_id,
'computer_partition_id': self._partition_id, 'computer_partition_id': self._partition_id,
} }
...@@ -557,7 +557,7 @@ class ComputerPartition(SlapRequester): ...@@ -557,7 +557,7 @@ class ComputerPartition(SlapRequester):
def getStatus(self): def getStatus(self):
xml = self._connection_helper.GET('getComputerPartitionStatus', xml = self._connection_helper.GET('getComputerPartitionStatus',
{ params={
'computer_id': self._computer_id, 'computer_id': self._computer_id,
'computer_partition_id': self._partition_id, 'computer_partition_id': self._partition_id,
} }
...@@ -579,7 +579,7 @@ class ConnectionHelper: ...@@ -579,7 +579,7 @@ class ConnectionHelper:
self.timeout = timeout self.timeout = timeout
def getComputerInformation(self, computer_id): def getComputerInformation(self, computer_id):
xml = self.GET('getComputerInformation', {'computer_id': computer_id}) xml = self.GET('getComputerInformation', params={'computer_id': computer_id})
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def getFullComputerInformation(self, computer_id): def getFullComputerInformation(self, computer_id):
...@@ -591,20 +591,21 @@ class ConnectionHelper: ...@@ -591,20 +591,21 @@ class ConnectionHelper:
params = {'computer_id': computer_id} params = {'computer_id': computer_id}
if not computer_id: if not computer_id:
# XXX-Cedric: should raise something smarter than "NotFound". # XXX-Cedric: should raise something smarter than "NotFound".
raise NotFoundError('%r %r' (path, params)) raise NotFoundError('%r %r' % (path, params))
try: try:
xml = self.GET(path, params) xml = self.GET(path, params=params)
except NotFoundError: except NotFoundError:
# XXX: This is a ugly way to keep backward compatibility, # XXX: This is a ugly way to keep backward compatibility,
# We should stablise slap library soon. # We should stablise slap library soon.
xml = self.GET('getComputerInformation', {'computer_id': computer_id}) xml = self.GET('getComputerInformation', params=params)
return xml_marshaller.loads(xml) return xml_marshaller.loads(xml)
def do_request(self, method, path, params=None, data=None, headers=None): def do_request(self, method, path, params=None, data=None, headers=None):
url = urlparse.urljoin(self.slapgrid_uri, path) url = urlparse.urljoin(self.slapgrid_uri, path)
if path.startswith('/'): if path.startswith('/'):
raise ValueError('method path should be relative: %s' % path) path = path[1:]
# raise ValueError('method path should be relative: %s' % path)
try: try:
if url.startswith('https'): if url.startswith('https'):
...@@ -707,7 +708,7 @@ class slap: ...@@ -707,7 +708,7 @@ class slap:
raise NotFoundError raise NotFoundError
xml = self._connection_helper.GET('registerComputerPartition', xml = self._connection_helper.GET('registerComputerPartition',
{ params = {
'computer_reference': computer_guid, 'computer_reference': computer_guid,
'computer_partition_reference': partition_id, 'computer_partition_reference': partition_id,
} }
...@@ -726,18 +727,19 @@ class slap: ...@@ -726,18 +727,19 @@ class slap:
def getSoftwareReleaseListFromSoftwareProduct(self, def getSoftwareReleaseListFromSoftwareProduct(self,
software_product_reference=None, software_release_url=None): software_product_reference=None, software_release_url=None):
url = '/getSoftwareReleaseListFromSoftwareProduct?' url = 'getSoftwareReleaseListFromSoftwareProduct'
params = {}
if software_product_reference: if software_product_reference:
if software_release_url is not None: if software_release_url is not None:
raise AttributeError('Both software_product_reference and ' raise AttributeError('Both software_product_reference and '
'software_release_url parameters are specified.') 'software_release_url parameters are specified.')
url += 'software_product_reference=%s' % software_product_reference params['software_product_reference'] = software_product_reference
else: else:
if software_release_url is None: if software_release_url is None:
raise AttributeError('None of software_product_reference and ' raise AttributeError('None of software_product_reference and '
'software_release_url parameters are specified.') 'software_release_url parameters are specified.')
url += 'software_release_url=%s' % software_release_url params['software_release_url'] = software_release_url
result = xml_marshaller.loads(self._connection_helper.GET(url)) result = xml_marshaller.loads(self._connection_helper.GET(url, params=params))
assert(type(result) == list) assert(type(result) == list)
return result return result
...@@ -27,8 +27,6 @@ ...@@ -27,8 +27,6 @@
import logging import logging
import unittest import unittest
# XXX: BasicMixin should be in a separated module, not in slapgrid test module.
from slapos.tests.slapgrid import BasicMixin
import slapos.slap import slapos.slap
import slapos.client import slapos.client
......
...@@ -25,18 +25,16 @@ ...@@ -25,18 +25,16 @@
# #
############################################################################## ##############################################################################
import httplib
import logging import logging
import os import os
import unittest import unittest
import urlparse import urlparse
import httmock
import slapos.slap import slapos.slap
import xml_marshaller import xml_marshaller
ORIGINAL_HTTPLIB_HTTPCONNECTION = httplib.HTTPConnection
ORIGINAL_HTTPLIB_HTTPSCONNECTION = httplib.HTTPSConnection
ORIGINAL_HTTPLIB_HTTPRESPONSE = httplib.HTTPResponse
class UndefinedYetException(Exception): class UndefinedYetException(Exception):
"""To catch exceptions which are not yet defined""" """To catch exceptions which are not yet defined"""
...@@ -49,7 +47,6 @@ class SlapMixin(unittest.TestCase): ...@@ -49,7 +47,6 @@ class SlapMixin(unittest.TestCase):
def setUp(self): def setUp(self):
self._server_url = os.environ.get('TEST_SLAP_SERVER_URL', None) self._server_url = os.environ.get('TEST_SLAP_SERVER_URL', None)
if self._server_url is None: if self._server_url is None:
self._patchHttplib()
self.server_url = 'http://localhost/' self.server_url = 'http://localhost/'
else: else:
self.server_url = self._server_url self.server_url = self._server_url
...@@ -58,31 +55,7 @@ class SlapMixin(unittest.TestCase): ...@@ -58,31 +55,7 @@ class SlapMixin(unittest.TestCase):
self.partition_id = 'PARTITION_01' self.partition_id = 'PARTITION_01'
def tearDown(self): def tearDown(self):
self._unpatchHttplib() pass
def _patchHttplib(self):
"""Overrides httplib"""
import slapmock.httplib
self.saved_httplib = {}
for fake in vars(slapmock.httplib):
self.saved_httplib[fake] = getattr(httplib, fake, None)
setattr(httplib, fake, getattr(slapmock.httplib, fake))
def _unpatchHttplib(self):
"""Restores httplib overriding"""
import httplib
# XXX not reliable
for name, original_value in self.saved_httplib.items():
setattr(httplib, name, original_value)
del self.saved_httplib
# XXX this fixes upper code, to be sure it is reliable
httplib.HTTPConnection = ORIGINAL_HTTPLIB_HTTPCONNECTION
httplib.HTTPSConnection = ORIGINAL_HTTPLIB_HTTPSCONNECTION
httplib.HTTPResponse = ORIGINAL_HTTPLIB_HTTPRESPONSE
def _getTestComputerId(self): def _getTestComputerId(self):
""" """
...@@ -103,19 +76,7 @@ class TestSlap(SlapMixin): ...@@ -103,19 +76,7 @@ class TestSlap(SlapMixin):
""" """
slap_instance = slapos.slap.slap() slap_instance = slapos.slap.slap()
slap_instance.initializeConnection(self.server_url) slap_instance.initializeConnection(self.server_url)
self.assertIn(slap_instance._connection_helper.host, self.server_url) self.assertEquals(slap_instance._connection_helper.slapgrid_uri, self.server_url)
self.assertIn(slap_instance._connection_helper.path, self.server_url)
def test_slap_initialisation_wrong_url(self):
"""
Asserts that slap initialisation raises exception when passed url
is not correct
"""
server_url = 'https://user:pass@server/path/path?parameter=notAcceptable'
slap_instance = slapos.slap.slap()
self.assertRaises(AttributeError,
slap_instance.initializeConnection,
server_url)
def test_registerComputer_with_new_guid(self): def test_registerComputer_with_new_guid(self):
""" """
...@@ -179,19 +140,24 @@ class TestSlap(SlapMixin): ...@@ -179,19 +140,24 @@ class TestSlap(SlapMixin):
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
self.slap.registerComputer(computer_guid) self.slap.registerComputer(computer_guid)
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition'
if (parsed_url.path == 'registerComputerPartition' and qs == {
and parsed_qs['computer_reference'][0] == computer_guid 'computer_reference': [computer_guid],
and parsed_qs['computer_partition_reference'][0] == partition_id): 'computer_partition_reference': [partition_id]
partition = slapos.slap.ComputerPartition( }):
computer_guid, partition_id) partition = slapos.slap.ComputerPartition(computer_guid, partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(partition)) return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(partition)
}
else: else:
return (404, {}, '') return {'status_code': 400}
httplib.HTTPConnection._callback = server_response
self._handler = handler
with httmock.HTTMock(handler):
partition = self.slap.registerComputerPartition(computer_guid, partition_id) partition = self.slap.registerComputerPartition(computer_guid, partition_id)
self.assertIsInstance(partition, slapos.slap.ComputerPartition) self.assertIsInstance(partition, slapos.slap.ComputerPartition)
...@@ -201,6 +167,7 @@ class TestSlap(SlapMixin): ...@@ -201,6 +167,7 @@ class TestSlap(SlapMixin):
returns ComputerPartition object returns ComputerPartition object
""" """
self.test_registerComputerPartition_new_partition_id_known_computer_guid() self.test_registerComputerPartition_new_partition_id_known_computer_guid()
with httmock.HTTMock(self._handler):
partition = self.slap.registerComputerPartition(self._getTestComputerId(), partition = self.slap.registerComputerPartition(self._getTestComputerId(),
self.partition_id) self.partition_id)
self.assertIsInstance(partition, slapos.slap.ComputerPartition) self.assertIsInstance(partition, slapos.slap.ComputerPartition)
...@@ -214,22 +181,23 @@ class TestSlap(SlapMixin): ...@@ -214,22 +181,23 @@ class TestSlap(SlapMixin):
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
partition_id = 'PARTITION_01' partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition'
if (parsed_url.path == 'registerComputerPartition' and qs == {
and parsed_qs['computer_reference'][0] == computer_guid 'computer_reference': [computer_guid],
and parsed_qs['computer_partition_reference'][0] == partition_id): 'computer_partition_reference': [partition_id]
slapos.slap.ComputerPartition(computer_guid, partition_id) }):
return (404, {}, '') return {'status_code': 404}
else: else:
return (0, {}, '') return {'status_code': 0}
httplib.HTTPConnection._callback = server_response
with httmock.HTTMock(handler):
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
self.slap.registerComputerPartition, self.slap.registerComputerPartition,
computer_guid, partition_id) computer_guid, partition_id)
def test_getFullComputerInformation_empty_computer_guid(self): def test_getFullComputerInformation_empty_computer_guid(self):
""" """
Asserts that calling getFullComputerInformation with empty computer_id Asserts that calling getFullComputerInformation with empty computer_id
...@@ -237,12 +205,11 @@ class TestSlap(SlapMixin): ...@@ -237,12 +205,11 @@ class TestSlap(SlapMixin):
""" """
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
# Shouldn't even be called # Shouldn't even be called
self.assertFalse(True) self.assertFalse(True)
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
self.slap._connection_helper.getFullComputerInformation, self.slap._connection_helper.getFullComputerInformation,
None) None)
...@@ -254,12 +221,11 @@ class TestSlap(SlapMixin): ...@@ -254,12 +221,11 @@ class TestSlap(SlapMixin):
""" """
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
# Shouldn't even be called # Shouldn't even be called
self.assertFalse(True) self.assertFalse(True)
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
self.slap.registerComputerPartition, self.slap.registerComputerPartition,
None, 'PARTITION_01') None, 'PARTITION_01')
...@@ -271,12 +237,11 @@ class TestSlap(SlapMixin): ...@@ -271,12 +237,11 @@ class TestSlap(SlapMixin):
""" """
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
# Shouldn't even be called # Shouldn't even be called
self.assertFalse(True) self.assertFalse(True)
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
self.slap.registerComputerPartition, self.slap.registerComputerPartition,
self._getTestComputerId(), None) self._getTestComputerId(), None)
...@@ -288,12 +253,11 @@ class TestSlap(SlapMixin): ...@@ -288,12 +253,11 @@ class TestSlap(SlapMixin):
""" """
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
# Shouldn't even be called # Shouldn't even be called
self.assertFalse(True) self.assertFalse(True)
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
self.slap.registerComputerPartition, self.slap.registerComputerPartition,
None, None) None, None)
...@@ -309,14 +273,16 @@ class TestSlap(SlapMixin): ...@@ -309,14 +273,16 @@ class TestSlap(SlapMixin):
software_product_reference = 'random_reference' software_product_reference = 'random_reference'
software_release_url_list = ['1', '2'] software_release_url_list = ['1', '2']
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/getSoftwareReleaseListFromSoftwareProduct'
if parsed_url.path == 'getSoftwareReleaseListFromSoftwareProduct' \ and qs == {'software_product_reference': [software_product_reference]}):
and parsed_qs == {'software_product_reference': [software_product_reference]}: return {
return (200, {}, xml_marshaller.xml_marshaller.dumps(software_release_url_list)) 'status_code': 200,
httplib.HTTPConnection._callback = server_response 'content': xml_marshaller.xml_marshaller.dumps(software_release_url_list)
}
with httmock.HTTMock(handler):
self.assertEqual( self.assertEqual(
self.slap.getSoftwareReleaseListFromSoftwareProduct( self.slap.getSoftwareReleaseListFromSoftwareProduct(
software_product_reference=software_product_reference), software_product_reference=software_product_reference),
...@@ -333,14 +299,16 @@ class TestSlap(SlapMixin): ...@@ -333,14 +299,16 @@ class TestSlap(SlapMixin):
software_release_url = 'random_url' software_release_url = 'random_url'
software_release_url_list = ['1', '2'] software_release_url_list = ['1', '2']
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/getSoftwareReleaseListFromSoftwareProduct'
if parsed_url.path == 'getSoftwareReleaseListFromSoftwareProduct' \ and qs == {'software_release_url': [software_release_url]}):
and parsed_qs == {'software_release_url': [software_release_url]}: return {
return (200, {}, xml_marshaller.xml_marshaller.dumps(software_release_url_list)) 'status_code': 200,
httplib.HTTPConnection._callback = server_response 'content': xml_marshaller.xml_marshaller.dumps(software_release_url_list)
}
with httmock.HTTMock(handler):
self.assertEqual( self.assertEqual(
self.slap.getSoftwareReleaseListFromSoftwareProduct( self.slap.getSoftwareReleaseListFromSoftwareProduct(
software_release_url=software_release_url), software_release_url=software_release_url),
...@@ -381,28 +349,33 @@ class TestComputer(SlapMixin): ...@@ -381,28 +349,33 @@ class TestComputer(SlapMixin):
slap = self.slap slap = self.slap
slap.initializeConnection(self.server_url) slap.initializeConnection(self.server_url)
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition'
if (parsed_url.path == 'registerComputerPartition' and 'computer_reference' in qs
and 'computer_reference' in parsed_qs and 'computer_partition_reference' in qs):
and 'computer_partition_reference' in parsed_qs):
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_reference'][0], qs['computer_reference'][0],
parsed_qs['computer_partition_reference'][0]) qs['computer_partition_reference'][0])
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
elif (parsed_url.path == 'getFullComputerInformation' 'status_code': 200,
and 'computer_id' in parsed_qs): 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0]) }
elif (url.path == '/getFullComputerInformation'
and 'computer_id' in qs):
slap_computer = slapos.slap.Computer(qs['computer_id'][0])
slap_computer._software_release_list = [] slap_computer._software_release_list = []
slap_computer._computer_partition_list = [] slap_computer._computer_partition_list = []
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer)) return {
elif parsed_url.path == 'requestComputerPartition': 'status_code': 200,
return (408, {}, '') 'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
elif url.path == '/requestComputerPartition':
return {'status_code': 408}
else: else:
return (404, {}, '') return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
with httmock.HTTMock(handler):
computer = self.slap.registerComputer(computer_guid) computer = self.slap.registerComputer(computer_guid)
self.assertEqual(computer.getComputerPartitionList(), []) self.assertEqual(computer.getComputerPartitionList(), [])
...@@ -413,12 +386,11 @@ class TestComputer(SlapMixin): ...@@ -413,12 +386,11 @@ class TestComputer(SlapMixin):
""" """
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
def server_response(self_httpconnection, path, method, body, header): def handler(url, req):
# Shouldn't even be called # Shouldn't even be called
self.assertFalse(True) self.assertFalse(True)
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
computer = self.slap.registerComputer(None) computer = self.slap.registerComputer(None)
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
getattr(computer, computer_method)) getattr(computer, computer_method))
...@@ -446,6 +418,31 @@ class TestComputer(SlapMixin): ...@@ -446,6 +418,31 @@ class TestComputer(SlapMixin):
partition_id = 'PARTITION_01' partition_id = 'PARTITION_01'
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
def handler(url, req):
qs = urlparse.parse_qs(url.query)
if (url.path == '/registerComputerPartition'
and qs == {
'computer_reference': [self.computer_guid],
'computer_partition_reference': [partition_id]
}):
partition = slapos.slap.ComputerPartition(self.computer_guid, partition_id)
return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(partition)
}
elif (url.path == '/getFullComputerInformation'
and 'computer_id' in qs):
slap_computer = slapos.slap.Computer(qs['computer_id'][0])
slap_computer._computer_partition_list = []
return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
else:
return {'status_code': 400}
with httmock.HTTMock(handler):
self.computer = self.slap.registerComputer(self.computer_guid) self.computer = self.slap.registerComputer(self.computer_guid)
self.partition = self.slap.registerComputerPartition(self.computer_guid, self.partition = self.slap.registerComputerPartition(self.computer_guid,
partition_id) partition_id)
...@@ -504,31 +501,38 @@ class TestComputerPartition(SlapMixin): ...@@ -504,31 +501,38 @@ class TestComputerPartition(SlapMixin):
def test_request_sends_request(self): def test_request_sends_request(self):
partition_id = 'PARTITION_01' partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition'
if (parsed_url.path == 'registerComputerPartition' and 'computer_reference' in qs
and 'computer_reference' in parsed_qs and 'computer_partition_reference' in qs):
and 'computer_partition_reference' in parsed_qs):
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_reference'][0], qs['computer_reference'][0],
parsed_qs['computer_partition_reference'][0]) qs['computer_partition_reference'][0])
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
elif (parsed_url.path == 'getComputerInformation' 'status_code': 200,
and 'computer_id' in parsed_qs): 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0]) }
elif (url.path == '/getComputerInformation'
and 'computer_id' in qs):
slap_computer = slapos.slap.Computer(qs['computer_id'][0])
slap_computer._software_release_list = [] slap_computer._software_release_list = []
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_id'][0], qs['computer_id'][0],
partition_id) partition_id)
slap_computer._computer_partition_list = [slap_partition] slap_computer._computer_partition_list = [slap_partition]
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer)) return {
elif parsed_url.path == 'requestComputerPartition': 'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
elif url.path == '/requestComputerPartition':
raise RequestWasCalled raise RequestWasCalled
else: else:
return (404, {}, '') return {
'status_code': 404
}
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
self.computer_guid = self._getTestComputerId() self.computer_guid = self._getTestComputerId()
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
...@@ -542,34 +546,39 @@ class TestComputerPartition(SlapMixin): ...@@ -542,34 +546,39 @@ class TestComputerPartition(SlapMixin):
def test_request_not_raises(self): def test_request_not_raises(self):
partition_id = 'PARTITION_01' partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition'
if (parsed_url.path == 'registerComputerPartition' and 'computer_reference' in qs
and 'computer_reference' in parsed_qs and 'computer_partition_reference' in qs):
and 'computer_partition_reference' in parsed_qs):
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_reference'][0], qs['computer_reference'][0],
parsed_qs['computer_partition_reference'][0]) qs['computer_partition_reference'][0])
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
elif (parsed_url.path == 'getComputerInformation' 'status_code': 200,
and 'computer_id' in parsed_qs): 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0]) }
elif (url.path == '/getComputerInformation'
and 'computer_id' in qs):
slap_computer = slapos.slap.Computer(qs['computer_id'][0])
slap_computer._software_release_list = [] slap_computer._software_release_list = []
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_id'][0], qs['computer_id'][0],
partition_id) partition_id)
slap_computer._computer_partition_list = [slap_partition] slap_computer._computer_partition_list = [slap_partition]
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer)) return {
elif parsed_url.path == 'requestComputerPartition': 'status_code': 200,
return (408, {}, '') 'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
elif url.path == '/requestComputerPartition':
return {'status_code': 408}
else: else:
return (404, {}, '') return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
self.computer_guid = self._getTestComputerId() self.computer_guid = self._getTestComputerId()
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
with httmock.HTTMock(handler):
computer_partition = self.slap.registerComputerPartition( computer_partition = self.slap.registerComputerPartition(
self.computer_guid, partition_id) self.computer_guid, partition_id)
requested_partition = computer_partition.request( requested_partition = computer_partition.request(
...@@ -581,34 +590,39 @@ class TestComputerPartition(SlapMixin): ...@@ -581,34 +590,39 @@ class TestComputerPartition(SlapMixin):
def test_request_raises_later(self): def test_request_raises_later(self):
partition_id = 'PARTITION_01' partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition' and
if (parsed_url.path == 'registerComputerPartition' and 'computer_reference' in qs and
'computer_reference' in parsed_qs and 'computer_partition_reference' in qs):
'computer_partition_reference' in parsed_qs):
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_reference'][0], qs['computer_reference'][0],
parsed_qs['computer_partition_reference'][0]) qs['computer_partition_reference'][0])
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
elif (parsed_url.path == 'getComputerInformation' 'status_code': 200,
and 'computer_id' in parsed_qs): 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0]) }
elif (url.path == '/getComputerInformation'
and 'computer_id' in qs):
slap_computer = slapos.slap.Computer(qs['computer_id'][0])
slap_computer._software_release_list = [] slap_computer._software_release_list = []
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_id'][0], qs['computer_id'][0],
partition_id) partition_id)
slap_computer._computer_partition_list = [slap_partition] slap_computer._computer_partition_list = [slap_partition]
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer)) return {
elif parsed_url.path == 'requestComputerPartition': 'status_code': 200,
return (408, {}, '') 'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
elif url.path == '/requestComputerPartition':
return {'status_code': 408}
else: else:
return (404, {}, '') return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
self.computer_guid = self._getTestComputerId() self.computer_guid = self._getTestComputerId()
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
with httmock.HTTMock(handler):
computer_partition = self.slap.registerComputerPartition( computer_partition = self.slap.registerComputerPartition(
self.computer_guid, partition_id) self.computer_guid, partition_id)
requested_partition = computer_partition.request( requested_partition = computer_partition.request(
...@@ -625,36 +639,46 @@ class TestComputerPartition(SlapMixin): ...@@ -625,36 +639,46 @@ class TestComputerPartition(SlapMixin):
requested_partition_id = 'PARTITION_02' requested_partition_id = 'PARTITION_02'
computer_guid = self._getTestComputerId() computer_guid = self._getTestComputerId()
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition' and
if (parsed_url.path == 'registerComputerPartition' and 'computer_reference' in qs and
'computer_reference' in parsed_qs and 'computer_partition_reference' in qs):
'computer_partition_reference' in parsed_qs):
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_reference'][0], qs['computer_reference'][0],
parsed_qs['computer_partition_reference'][0]) qs['computer_partition_reference'][0])
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
elif (parsed_url.path == 'getComputerInformation' and 'computer_id' in parsed_qs): 'status_code': 200,
slap_computer = slapos.slap.Computer(parsed_qs['computer_id'][0]) 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
}
elif (url.path == '/getComputerInformation' and 'computer_id' in qs):
slap_computer = slapos.slap.Computer(qs['computer_id'][0])
slap_computer._software_release_list = [] slap_computer._software_release_list = []
slap_partition = slapos.slap.ComputerPartition( slap_partition = slapos.slap.ComputerPartition(
parsed_qs['computer_id'][0], qs['computer_id'][0],
partition_id) partition_id)
slap_computer._computer_partition_list = [slap_partition] slap_computer._computer_partition_list = [slap_partition]
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer)) return {
elif parsed_url.path == 'requestComputerPartition': 'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
elif url.path == '/requestComputerPartition':
from slapos.slap.slap import SoftwareInstance from slapos.slap.slap import SoftwareInstance
slap_partition = SoftwareInstance( slap_partition = SoftwareInstance(
slap_computer_id=computer_guid, slap_computer_id=computer_guid,
slap_computer_partition_id=requested_partition_id) slap_computer_partition_id=requested_partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
}
else: else:
return (404, {}, '') return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
with httmock.HTTMock(handler):
computer_partition = self.slap.registerComputerPartition( computer_partition = self.slap.registerComputerPartition(
computer_guid, partition_id) computer_guid, partition_id)
requested_partition = computer_partition.request( requested_partition = computer_partition.request(
...@@ -676,19 +700,22 @@ class TestComputerPartition(SlapMixin): ...@@ -676,19 +700,22 @@ class TestComputerPartition(SlapMixin):
slap = self.slap slap = self.slap
slap.initializeConnection(self.server_url) slap.initializeConnection(self.server_url)
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition' and
if (parsed_url.path == 'registerComputerPartition' and qs['computer_reference'][0] == computer_guid and
parsed_qs['computer_reference'][0] == computer_guid and qs['computer_partition_reference'][0] == partition_id):
parsed_qs['computer_partition_reference'][0] == partition_id):
partition = slapos.slap.ComputerPartition( partition = slapos.slap.ComputerPartition(
computer_guid, partition_id) computer_guid, partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(partition)) return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(partition)
}
else: else:
return (404, {}, '') return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
with httmock.HTTMock(handler):
computer_partition = self.slap.registerComputerPartition( computer_partition = self.slap.registerComputerPartition(
computer_guid, partition_id) computer_guid, partition_id)
self.assertRaises(slapos.slap.NotFoundError, self.assertRaises(slapos.slap.NotFoundError,
...@@ -731,28 +758,31 @@ class TestComputerPartition(SlapMixin): ...@@ -731,28 +758,31 @@ class TestComputerPartition(SlapMixin):
slap = self.slap slap = self.slap
slap.initializeConnection(self.server_url) slap.initializeConnection(self.server_url)
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(url.query)
parsed_qs = urlparse.parse_qs(parsed_url.query) if (url.path == '/registerComputerPartition' and
if (parsed_url.path == 'registerComputerPartition' and qs['computer_reference'][0] == computer_guid and
parsed_qs['computer_reference'][0] == computer_guid and qs['computer_partition_reference'][0] == partition_id):
parsed_qs['computer_partition_reference'][0] == partition_id):
partition = slapos.slap.ComputerPartition( partition = slapos.slap.ComputerPartition(
computer_guid, partition_id) computer_guid, partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(partition)) return {
elif parsed_url.path == 'softwareInstanceError': 'statu_code': 200,
parsed_qs_body = urlparse.parse_qs(body) 'content': xml_marshaller.xml_marshaller.dumps(partition)
}
elif url.path == '/softwareInstanceError':
parsed_qs_body = urlparse.parse_qs(req.body)
# XXX: why do we have computer_id and not computer_reference? # XXX: why do we have computer_id and not computer_reference?
# XXX: why do we have computer_partition_id and not # XXX: why do we have computer_partition_id and not
# computer_partition_reference? # computer_partition_reference?
if (parsed_qs_body['computer_id'][0] == computer_guid and if (parsed_qs_body['computer_id'][0] == computer_guid and
parsed_qs_body['computer_partition_id'][0] == partition_id and parsed_qs_body['computer_partition_id'][0] == partition_id and
parsed_qs_body['error_log'][0] == 'some error'): parsed_qs_body['error_log'][0] == 'some error'):
return (200, {}, '') return {'status_code': 200}
return (404, {}, '') return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
with httmock.HTTMock(handler):
computer_partition = slap.registerComputerPartition( computer_partition = slap.registerComputerPartition(
computer_guid, partition_id) computer_guid, partition_id)
# XXX: Interface does not define return value # XXX: Interface does not define return value
...@@ -800,18 +830,19 @@ class TestSoftwareRelease(SlapMixin): ...@@ -800,18 +830,19 @@ class TestSoftwareRelease(SlapMixin):
slap = self.slap slap = self.slap
slap.initializeConnection(self.server_url) slap.initializeConnection(self.server_url)
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) qs = urlparse.parse_qs(req.body)
parsed_qs = urlparse.parse_qs(body) if (url.path == '/softwareReleaseError' and
if (parsed_url.path == 'softwareReleaseError' and qs['computer_id'][0] == computer_guid and
parsed_qs['computer_id'][0] == computer_guid and qs['url'][0] == software_release_uri and
parsed_qs['url'][0] == software_release_uri and qs['error_log'][0] == 'some error'):
parsed_qs['error_log'][0] == 'some error'): return {
return (200, {}, '') 'status_code': 200
return (404, {}, '') }
return {'status_code': 404}
httplib.HTTPConnection._callback = server_response
with httmock.HTTMock(handler):
software_release = self.slap.registerSoftwareRelease(software_release_uri) software_release = self.slap.registerSoftwareRelease(software_release_uri)
software_release._computer_guid = computer_guid software_release._computer_guid = computer_guid
software_release.error('some error') software_release.error('some error')
...@@ -825,21 +856,28 @@ class TestOpenOrder(SlapMixin): ...@@ -825,21 +856,28 @@ class TestOpenOrder(SlapMixin):
# XXX: Interface lack registerOpenOrder method declaration # XXX: Interface lack registerOpenOrder method declaration
open_order = self.slap.registerOpenOrder() open_order = self.slap.registerOpenOrder()
def server_response(self, path, method, body, header): def handler(url, req):
parsed_url = urlparse.urlparse(path.lstrip('/')) if url.path == '/requestComputerPartition':
if parsed_url.path == 'requestComputerPartition':
raise RequestWasCalled raise RequestWasCalled
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
self.assertRaises(RequestWasCalled, self.assertRaises(RequestWasCalled,
open_order.request, open_order.request,
software_release_uri, 'myrefe') software_release_uri, 'myrefe')
@unittest.skip('unclear what should be returned')
def test_request_not_raises(self): def test_request_not_raises(self):
software_release_uri = 'http://server/new/' + self._getTestComputerId() software_release_uri = 'http://server/new/' + self._getTestComputerId()
self.slap = slapos.slap.slap() self.slap = slapos.slap.slap()
self.slap.initializeConnection(self.server_url) self.slap.initializeConnection(self.server_url)
# XXX: Interface lack registerOpenOrder method declaration # XXX: Interface lack registerOpenOrder method declaration
def handler(url, req):
if url.path == '/requestComputerPartition':
pass
# XXX what to do here?
with httmock.HTTMock(handler):
open_order = self.slap.registerOpenOrder() open_order = self.slap.registerOpenOrder()
computer_partition = open_order.request(software_release_uri, 'myrefe') computer_partition = open_order.request(software_release_uri, 'myrefe')
self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition) self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
...@@ -851,10 +889,10 @@ class TestOpenOrder(SlapMixin): ...@@ -851,10 +889,10 @@ class TestOpenOrder(SlapMixin):
# XXX: Interface lack registerOpenOrder method declaration # XXX: Interface lack registerOpenOrder method declaration
open_order = self.slap.registerOpenOrder() open_order = self.slap.registerOpenOrder()
def server_response(self, path, method, body, header): def handler(url, req):
return (408, {}, '') return {'status_code': 408}
httplib.HTTPConnection._callback = server_response with httmock.HTTMock(handler):
computer_partition = open_order.request(software_release_uri, 'myrefe') computer_partition = open_order.request(software_release_uri, 'myrefe')
self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition) self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
...@@ -870,15 +908,17 @@ class TestOpenOrder(SlapMixin): ...@@ -870,15 +908,17 @@ class TestOpenOrder(SlapMixin):
computer_guid = self._getTestComputerId() computer_guid = self._getTestComputerId()
requested_partition_id = 'PARTITION_01' requested_partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
from slapos.slap.slap import SoftwareInstance from slapos.slap.slap import SoftwareInstance
slap_partition = SoftwareInstance( slap_partition = SoftwareInstance(
slap_computer_id=computer_guid, slap_computer_id=computer_guid,
slap_computer_partition_id=requested_partition_id) slap_computer_partition_id=requested_partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
'status_code': 200,
httplib.HTTPConnection._callback = server_response 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
}
with httmock.HTTMock(handler):
computer_partition = open_order.request(software_release_uri, 'myrefe') computer_partition = open_order.request(software_release_uri, 'myrefe')
self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition) self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
self.assertEqual(requested_partition_id, computer_partition.getId()) self.assertEqual(requested_partition_id, computer_partition.getId())
...@@ -894,16 +934,19 @@ class TestOpenOrder(SlapMixin): ...@@ -894,16 +934,19 @@ class TestOpenOrder(SlapMixin):
computer_guid = self._getTestComputerId() computer_guid = self._getTestComputerId()
requested_partition_id = 'PARTITION_01' requested_partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
from slapos.slap.slap import SoftwareInstance from slapos.slap.slap import SoftwareInstance
slap_partition = SoftwareInstance( slap_partition = SoftwareInstance(
_connection_dict = {"url": 'URL_CONNECTION_PARAMETER'}, _connection_dict = {"url": 'URL_CONNECTION_PARAMETER'},
slap_computer_id=computer_guid, slap_computer_id=computer_guid,
slap_computer_partition_id=requested_partition_id) slap_computer_partition_id=requested_partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
}
httplib.HTTPConnection._callback = server_response
with httmock.HTTMock(handler):
computer_partition = open_order.request(software_release_uri, 'myrefe') computer_partition = open_order.request(software_release_uri, 'myrefe')
self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition) self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
self.assertEqual(requested_partition_id, computer_partition.getId()) self.assertEqual(requested_partition_id, computer_partition.getId())
...@@ -921,7 +964,7 @@ class TestOpenOrder(SlapMixin): ...@@ -921,7 +964,7 @@ class TestOpenOrder(SlapMixin):
computer_guid = self._getTestComputerId() computer_guid = self._getTestComputerId()
requested_partition_id = 'PARTITION_01' requested_partition_id = 'PARTITION_01'
def server_response(self, path, method, body, header): def handler(url, req):
from slapos.slap.slap import SoftwareInstance from slapos.slap.slap import SoftwareInstance
slap_partition = SoftwareInstance( slap_partition = SoftwareInstance(
connection_xml="""<?xml version='1.0' encoding='utf-8'?> connection_xml="""<?xml version='1.0' encoding='utf-8'?>
...@@ -930,10 +973,12 @@ class TestOpenOrder(SlapMixin): ...@@ -930,10 +973,12 @@ class TestOpenOrder(SlapMixin):
</instance>""", </instance>""",
slap_computer_id=computer_guid, slap_computer_id=computer_guid,
slap_computer_partition_id=requested_partition_id) slap_computer_partition_id=requested_partition_id)
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_partition)) return {
'status_code': 200,
httplib.HTTPConnection._callback = server_response 'content': xml_marshaller.xml_marshaller.dumps(slap_partition)
}
with httmock.HTTMock(handler):
computer_partition = open_order.request(software_release_uri, 'myrefe') computer_partition = open_order.request(software_release_uri, 'myrefe')
self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition) self.assertIsInstance(computer_partition, slapos.slap.ComputerPartition)
self.assertEqual(requested_partition_id, computer_partition.getId()) self.assertEqual(requested_partition_id, computer_partition.getId())
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
############################################################################## ##############################################################################
from __future__ import absolute_import from __future__ import absolute_import
import httplib
import logging import logging
import os import os
import random import random
...@@ -53,6 +52,8 @@ from slapos.grid import SlapObject ...@@ -53,6 +52,8 @@ from slapos.grid import SlapObject
from slapos.grid.SlapObject import WATCHDOG_MARK from slapos.grid.SlapObject import WATCHDOG_MARK
import slapos.grid.SlapObject import slapos.grid.SlapObject
import httmock
dummylogger = logging.getLogger() dummylogger = logging.getLogger()
...@@ -99,7 +100,8 @@ touch worked ...@@ -99,7 +100,8 @@ touch worked
""" """
class BasicMixin:
class BasicMixin(object):
def setUp(self): def setUp(self):
self._tempdir = tempfile.mkdtemp() self._tempdir = tempfile.mkdtemp()
self.software_root = os.path.join(self._tempdir, 'software') self.software_root = os.path.join(self._tempdir, 'software')
...@@ -227,6 +229,7 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase): ...@@ -227,6 +229,7 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase):
os.mkdir(self.software_root) os.mkdir(self.software_root)
self.assertRaises(OSError, self.grid.processComputerPartitionList) self.assertRaises(OSError, self.grid.processComputerPartitionList)
@unittest.skip('which request handler here?')
def test_no_master(self): def test_no_master(self):
os.mkdir(self.software_root) os.mkdir(self.software_root)
os.mkdir(self.instance_root) os.mkdir(self.instance_root)
...@@ -235,23 +238,6 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase): ...@@ -235,23 +238,6 @@ class TestBasicSlapgridCP(BasicMixin, unittest.TestCase):
class MasterMixin(BasicMixin): class MasterMixin(BasicMixin):
def _patchHttplib(self):
"""Overrides httplib"""
import slapos.tests.slapmock.httplib
self.saved_httplib = {}
for fake in vars(slapos.tests.slapmock.httplib):
self.saved_httplib[fake] = getattr(httplib, fake, None)
setattr(httplib, fake, getattr(slapos.tests.slapmock.httplib, fake))
def _unpatchHttplib(self):
"""Restores httplib overriding"""
import httplib
for name, original_value in self.saved_httplib.items():
setattr(httplib, name, original_value)
del self.saved_httplib
def _mock_sleep(self): def _mock_sleep(self):
self.fake_waiting_time = None self.fake_waiting_time = None
self.real_sleep = time.sleep self.real_sleep = time.sleep
...@@ -267,17 +253,15 @@ class MasterMixin(BasicMixin): ...@@ -267,17 +253,15 @@ class MasterMixin(BasicMixin):
time.sleep = self.real_sleep time.sleep = self.real_sleep
def setUp(self): def setUp(self):
self._patchHttplib()
self._mock_sleep() self._mock_sleep()
BasicMixin.setUp(self) BasicMixin.setUp(self)
def tearDown(self): def tearDown(self):
self._unpatchHttplib()
self._unmock_sleep() self._unmock_sleep()
BasicMixin.tearDown(self) BasicMixin.tearDown(self)
class ComputerForTest: class ComputerForTest(object):
""" """
Class to set up environment for tests setting instance, software Class to set up environment for tests setting instance, software
and server response and server response
...@@ -301,7 +285,76 @@ class ComputerForTest: ...@@ -301,7 +285,76 @@ class ComputerForTest:
os.mkdir(self.software_root) os.mkdir(self.software_root)
self.setSoftwares() self.setSoftwares()
self.setInstances() self.setInstances()
self.setServerResponse()
def request_handler(self, url, req):
"""
Define _callback.
Will register global sequence of message, sequence by partition
and error and error message by partition
"""
self.sequence.append(url.path)
if req.method == 'GET':
qs = urlparse.parse_qs(url.query)
else:
qs = urlparse.parse_qs(req.body)
if (url.path == '/getFullComputerInformation'
and 'computer_id' in qs):
slap_computer = self.getComputer(qs['computer_id'][0])
return {
'status_code': 200,
'content': xml_marshaller.xml_marshaller.dumps(slap_computer)
}
if req.method == 'POST' and 'computer_partition_id' in qs:
instance = self.instance_list[int(qs['computer_partition_id'][0])]
instance.sequence.append(url.path)
instance.header_list.append(req.headers)
if url.path == '/availableComputerPartition':
return {'status_code': 200}
if url.path == '/startedComputerPartition':
instance.state = 'started'
return {'status_code': 200}
if url.path == '/stoppedComputerPartition':
instance.state = 'stopped'
return {'status_code': 200}
if url.path == '/destroyedComputerPartition':
instance.state = 'destroyed'
return {'status_code': 200}
if url.path == '/softwareInstanceBang':
return {'status_code': 200}
if url.path == '/softwareInstanceError':
instance.error_log = '\n'.join(
[
line
for line in qs['error_log'][0].splitlines()
if 'dropPrivileges' not in line
]
)
instance.error = True
return {'status_code': 200}
elif req.method == 'POST' and 'url' in qs:
# XXX hardcoded to first software release!
software = self.software_list[0]
software.sequence.append(url.path)
if url.path == '/buildingSoftwareRelease':
return {'status_code': 200}
if url.path == '/softwareReleaseError':
software.error_log = '\n'.join(
[
line
for line in qs['error_log'][0].splitlines()
if 'dropPrivileges' not in line
]
)
software.error = True
return {'status_code': 200}
else:
return {'status_code': 500}
def setSoftwares(self): def setSoftwares(self):
""" """
...@@ -341,78 +394,9 @@ class ComputerForTest: ...@@ -341,78 +394,9 @@ class ComputerForTest:
] ]
return slap_computer return slap_computer
def setServerResponse(self):
httplib.HTTPConnection._callback = self.getServerResponse()
httplib.HTTPSConnection._callback = self.getServerResponse()
def getServerResponse(self):
"""
Define _callback.
Will register global sequence of message, sequence by partition
and error and error message by partition
"""
def server_response(self_httplib, path, method, body, header):
parsed_url = urlparse.urlparse(path.lstrip('/'))
self.sequence.append(parsed_url.path)
if method == 'GET':
parsed_qs = urlparse.parse_qs(parsed_url.query)
else:
parsed_qs = urlparse.parse_qs(body)
if (parsed_url.path == 'getFullComputerInformation'
and 'computer_id' in parsed_qs):
slap_computer = self.getComputer(parsed_qs['computer_id'][0])
return (200, {}, xml_marshaller.xml_marshaller.dumps(slap_computer))
if method == 'POST' and 'computer_partition_id' in parsed_qs:
instance = self.instance_list[int(parsed_qs['computer_partition_id'][0])]
instance.sequence.append(parsed_url.path)
instance.header_list.append(header)
if parsed_url.path == 'availableComputerPartition':
return (200, {}, '')
if parsed_url.path == 'startedComputerPartition':
instance.state = 'started'
return (200, {}, '')
if parsed_url.path == 'stoppedComputerPartition':
instance.state = 'stopped'
return (200, {}, '')
if parsed_url.path == 'destroyedComputerPartition':
instance.state = 'destroyed'
return (200, {}, '')
if parsed_url.path == 'softwareInstanceBang':
return (200, {}, '')
if parsed_url.path == 'softwareInstanceError':
instance.error_log = '\n'.join(
[
line
for line in parsed_qs['error_log'][0].splitlines()
if 'dropPrivileges' not in line
]
)
instance.error = True
return (200, {}, '')
elif method == 'POST' and 'url' in parsed_qs: class InstanceForTest(object):
# XXX hardcoded to first software release!
software = self.software_list[0]
software.sequence.append(parsed_url.path)
if parsed_url.path == 'buildingSoftwareRelease':
return (200, {}, '')
if parsed_url.path == 'softwareReleaseError':
software.error_log = '\n'.join(
[
line
for line in parsed_qs['error_log'][0].splitlines()
if 'dropPrivileges' not in line
]
)
software.error = True
return (200, {}, '')
else:
return (500, {}, '')
return server_response
class InstanceForTest:
""" """
Class containing all needed paramaters and function to simulate instances Class containing all needed paramaters and function to simulate instances
""" """
...@@ -479,7 +463,7 @@ class InstanceForTest: ...@@ -479,7 +463,7 @@ class InstanceForTest:
open(self.key_file, 'w').write(self.key) open(self.key_file, 'w').write(self.key)
class SoftwareForTest: class SoftwareForTest(object):
""" """
Class to prepare and simulate software. Class to prepare and simulate software.
each instance has a sotfware attributed each instance has a sotfware attributed
...@@ -533,9 +517,8 @@ touch worked"""): ...@@ -533,9 +517,8 @@ touch worked"""):
class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
def test_nothing_to_do(self): def test_nothing_to_do(self):
computer = ComputerForTest(self.software_root, self.instance_root, 0, 0)
ComputerForTest(self.software_root, self.instance_root, 0, 0) with httmock.HTTMock(computer.request_handler):
self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS)
self.assertItemsEqual(os.listdir(self.instance_root), ['etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['etc', 'var'])
self.assertItemsEqual(os.listdir(self.software_root), []) self.assertItemsEqual(os.listdir(self.software_root), [])
...@@ -544,6 +527,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -544,6 +527,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
def test_one_partition(self): def test_one_partition(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS)
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
...@@ -552,8 +536,8 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -552,8 +536,8 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
'software_release', 'worked', '.slapos-retention-lock-delay']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'stoppedComputerPartition']) '/stoppedComputerPartition'])
def test_one_partition_instance_cfg(self): def test_one_partition_instance_cfg(self):
""" """
...@@ -561,6 +545,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -561,6 +545,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
"template.cfg" but "instance.cfg". "template.cfg" but "instance.cfg".
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS)
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
...@@ -569,16 +554,17 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -569,16 +554,17 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
'software_release', 'worked', '.slapos-retention-lock-delay']) 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'stoppedComputerPartition']) '/stoppedComputerPartition'])
def test_one_free_partition(self): def test_one_free_partition(self):
""" """
Test if slapgrid cp don't process "free" partition Test if slapgrid cp does not process "free" partition
""" """
computer = ComputerForTest(self.software_root, computer = ComputerForTest(self.software_root,
self.instance_root, self.instance_root,
software_amount=0) software_amount=0)
with httmock.HTTMock(computer.request_handler):
partition = computer.instance_list[0] partition = computer.instance_list[0]
partition.requested_state = 'destroyed' partition.requested_state = 'destroyed'
self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS)
...@@ -589,6 +575,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -589,6 +575,7 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
def test_one_partition_started(self): def test_one_partition_started(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
partition = computer.instance_list[0] partition = computer.instance_list[0]
partition.requested_state = 'started' partition.requested_state = 'started'
partition.software.setBuildout(WRAPPER_CONTENT) partition.software.setBuildout(WRAPPER_CONTENT)
...@@ -601,12 +588,13 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase): ...@@ -601,12 +588,13 @@ class TestSlapgridCPWithMaster(MasterMixin, unittest.TestCase):
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [partition.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [partition.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual(partition.state, 'started') self.assertEqual(partition.state, 'started')
def test_one_partition_started_stopped(self): def test_one_partition_started_stopped(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
...@@ -637,8 +625,8 @@ chmod 755 etc/run/wrapper ...@@ -637,8 +625,8 @@ chmod 755 etc/run/wrapper
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual(instance.state, 'started') self.assertEqual(instance.state, 'started')
computer.sequence = [] computer.sequence = []
...@@ -650,8 +638,8 @@ chmod 755 etc/run/wrapper ...@@ -650,8 +638,8 @@ chmod 755 etc/run/wrapper
'etc', 'software_release', 'worked', '.slapos-retention-lock-delay']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15') self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'stoppedComputerPartition']) '/stoppedComputerPartition'])
self.assertEqual(instance.state, 'stopped') self.assertEqual(instance.state, 'stopped')
def test_one_broken_partition_stopped(self): def test_one_broken_partition_stopped(self):
...@@ -661,6 +649,7 @@ chmod 755 etc/run/wrapper ...@@ -661,6 +649,7 @@ chmod 755 etc/run/wrapper
to run) but status is still started. to run) but status is still started.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
...@@ -692,8 +681,8 @@ chmod 755 etc/run/wrapper ...@@ -692,8 +681,8 @@ chmod 755 etc/run/wrapper
self.assertItemsEqual(os.listdir(self.software_root), self.assertItemsEqual(os.listdir(self.software_root),
[instance.software.software_hash]) [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual(instance.state, 'started') self.assertEqual(instance.state, 'started')
computer.sequence = [] computer.sequence = []
...@@ -709,12 +698,13 @@ exit 1 ...@@ -709,12 +698,13 @@ exit 1
'etc', 'software_release', 'worked', '.slapos-retention-lock-delay']) 'etc', 'software_release', 'worked', '.slapos-retention-lock-delay'])
self.assertLogContent(wrapper_log, 'Signal handler called with signal 15') self.assertLogContent(wrapper_log, 'Signal handler called with signal 15')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', ['/getFullComputerInformation',
'softwareInstanceError']) '/softwareInstanceError'])
self.assertEqual(instance.state, 'started') self.assertEqual(instance.state, 'started')
def test_one_partition_stopped_started(self): def test_one_partition_stopped_started(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'stopped' instance.requested_state = 'stopped'
instance.software.setBuildout(WRAPPER_CONTENT) instance.software.setBuildout(WRAPPER_CONTENT)
...@@ -727,8 +717,8 @@ exit 1 ...@@ -727,8 +717,8 @@ exit 1
self.assertItemsEqual(os.listdir(self.software_root), self.assertItemsEqual(os.listdir(self.software_root),
[instance.software.software_hash]) [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'stoppedComputerPartition']) '/stoppedComputerPartition'])
self.assertEqual('stopped', instance.state) self.assertEqual('stopped', instance.state)
instance.requested_state = 'started' instance.requested_state = 'started'
...@@ -744,8 +734,8 @@ exit 1 ...@@ -744,8 +734,8 @@ exit 1
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual('started', instance.state) self.assertEqual('started', instance.state)
def test_one_partition_destroyed(self): def test_one_partition_destroyed(self):
...@@ -754,6 +744,7 @@ exit 1 ...@@ -754,6 +744,7 @@ exit 1
stopped by slapgrid-cp, not processed stopped by slapgrid-cp, not processed
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'destroyed' instance.requested_state = 'destroyed'
...@@ -768,8 +759,8 @@ exit 1 ...@@ -768,8 +759,8 @@ exit 1
self.assertItemsEqual(os.listdir(partition), ['.slapgrid', dummy_file_name]) self.assertItemsEqual(os.listdir(partition), ['.slapgrid', dummy_file_name])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', ['/getFullComputerInformation',
'stoppedComputerPartition']) '/stoppedComputerPartition'])
self.assertEqual('stopped', instance.state) self.assertEqual('stopped', instance.state)
...@@ -802,6 +793,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -802,6 +793,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
5.Wait for file generated by monkeypacthed bang to appear 5.Wait for file generated by monkeypacthed bang to appear
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
partition = computer.instance_list[0] partition = computer.instance_list[0]
partition.requested_state = 'started' partition.requested_state = 'started'
partition.software.setBuildout(DAEMON_CONTENT) partition.software.setBuildout(DAEMON_CONTENT)
...@@ -829,6 +821,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -829,6 +821,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
5.Check that file generated by monkeypacthed bang do not appear 5.Check that file generated by monkeypacthed bang do not appear
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
partition = computer.instance_list[0] partition = computer.instance_list[0]
partition.requested_state = 'started' partition.requested_state = 'started'
...@@ -868,6 +861,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -868,6 +861,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
(ie: watchdog id in process name) (ie: watchdog id in process name)
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
certificate_repository_path = os.path.join(self._tempdir, 'partition_pki') certificate_repository_path = os.path.join(self._tempdir, 'partition_pki')
instance.setCertificate(certificate_repository_path) instance.setCertificate(certificate_repository_path)
...@@ -884,9 +878,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -884,9 +878,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
payload = 'processname:%s groupname:%s from_state:RUNNING' % ( payload = 'processname:%s groupname:%s from_state:RUNNING' % (
'daemon' + WATCHDOG_MARK, instance.name) 'daemon' + WATCHDOG_MARK, instance.name)
watchdog.handle_event(headers, payload) watchdog.handle_event(headers, payload)
self.assertEqual(instance.sequence, ['softwareInstanceBang']) self.assertEqual(instance.sequence, ['/softwareInstanceBang'])
self.assertEqual(instance.header_list[0]['key'], instance.key)
self.assertEqual(instance.header_list[0]['certificate'], instance.certificate)
def test_unwanted_events_will_not_bang(self): def test_unwanted_events_will_not_bang(self):
""" """
...@@ -939,6 +931,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -939,6 +931,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
.timestamp file. .timestamp file.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
certificate_repository_path = os.path.join(self._tempdir, 'partition_pki') certificate_repository_path = os.path.join(self._tempdir, 'partition_pki')
instance.setCertificate(certificate_repository_path) instance.setCertificate(certificate_repository_path)
...@@ -961,9 +954,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -961,9 +954,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
payload = 'processname:%s groupname:%s from_state:RUNNING' % ( payload = 'processname:%s groupname:%s from_state:RUNNING' % (
'daemon' + WATCHDOG_MARK, instance.name) 'daemon' + WATCHDOG_MARK, instance.name)
watchdog.handle_event(headers, payload) watchdog.handle_event(headers, payload)
self.assertEqual(instance.sequence, ['softwareInstanceBang']) self.assertEqual(instance.sequence, ['/softwareInstanceBang'])
self.assertEqual(instance.header_list[0]['key'], instance.key)
self.assertEqual(instance.header_list[0]['certificate'], instance.certificate)
self.assertEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content) self.assertEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content)
...@@ -976,6 +967,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -976,6 +967,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
Practically speaking, .timestamp file in the partition does not exsit. Practically speaking, .timestamp file in the partition does not exsit.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
certificate_repository_path = os.path.join(self._tempdir, 'partition_pki') certificate_repository_path = os.path.join(self._tempdir, 'partition_pki')
instance.setCertificate(certificate_repository_path) instance.setCertificate(certificate_repository_path)
...@@ -995,9 +987,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -995,9 +987,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
payload = 'processname:%s groupname:%s from_state:RUNNING' % ( payload = 'processname:%s groupname:%s from_state:RUNNING' % (
'daemon' + WATCHDOG_MARK, instance.name) 'daemon' + WATCHDOG_MARK, instance.name)
watchdog.handle_event(headers, payload) watchdog.handle_event(headers, payload)
self.assertEqual(instance.sequence, ['softwareInstanceBang']) self.assertEqual(instance.sequence, ['/softwareInstanceBang'])
self.assertEqual(instance.header_list[0]['key'], instance.key)
self.assertEqual(instance.header_list[0]['certificate'], instance.certificate)
self.assertNotEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content) self.assertNotEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content)
...@@ -1010,6 +1000,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -1010,6 +1000,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
* subsequent bangs are ignored until a deployment is successful. * subsequent bangs are ignored until a deployment is successful.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
certificate_repository_path = os.path.join(self._tempdir, 'partition_pki') certificate_repository_path = os.path.join(self._tempdir, 'partition_pki')
instance.setCertificate(certificate_repository_path) instance.setCertificate(certificate_repository_path)
...@@ -1029,9 +1020,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -1029,9 +1020,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
payload = 'processname:%s groupname:%s from_state:RUNNING' % ( payload = 'processname:%s groupname:%s from_state:RUNNING' % (
'daemon' + WATCHDOG_MARK, instance.name) 'daemon' + WATCHDOG_MARK, instance.name)
watchdog.handle_event(headers, payload) watchdog.handle_event(headers, payload)
self.assertEqual(instance.sequence, ['softwareInstanceBang']) self.assertEqual(instance.sequence, ['/softwareInstanceBang'])
self.assertEqual(instance.header_list[0]['key'], instance.key)
self.assertEqual(instance.header_list[0]['certificate'], instance.certificate)
# Second bang # Second bang
event = watchdog.process_state_events[0] event = watchdog.process_state_events[0]
...@@ -1061,6 +1050,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -1061,6 +1050,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
* The process crashes again, watchdog ignroes it * The process crashes again, watchdog ignroes it
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
certificate_repository_path = os.path.join(self._tempdir, 'partition_pki') certificate_repository_path = os.path.join(self._tempdir, 'partition_pki')
instance.setCertificate(certificate_repository_path) instance.setCertificate(certificate_repository_path)
...@@ -1084,9 +1074,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -1084,9 +1074,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
payload = 'processname:%s groupname:%s from_state:RUNNING' % ( payload = 'processname:%s groupname:%s from_state:RUNNING' % (
'daemon' + WATCHDOG_MARK, instance.name) 'daemon' + WATCHDOG_MARK, instance.name)
watchdog.handle_event(headers, payload) watchdog.handle_event(headers, payload)
self.assertEqual(instance.sequence, ['softwareInstanceBang']) self.assertEqual(instance.sequence, ['/softwareInstanceBang'])
self.assertEqual(instance.header_list[0]['key'], instance.key)
self.assertEqual(instance.header_list[0]['certificate'], instance.certificate)
self.assertEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content) self.assertEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content)
...@@ -1114,9 +1102,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase): ...@@ -1114,9 +1102,7 @@ class TestSlapgridCPWithMasterWatchdog(MasterMixin, unittest.TestCase):
payload = 'processname:%s groupname:%s from_state:RUNNING' % ( payload = 'processname:%s groupname:%s from_state:RUNNING' % (
'daemon' + WATCHDOG_MARK, instance.name) 'daemon' + WATCHDOG_MARK, instance.name)
watchdog.handle_event(headers, payload) watchdog.handle_event(headers, payload)
self.assertEqual(instance.sequence, ['softwareInstanceBang']) self.assertEqual(instance.sequence, ['/softwareInstanceBang'])
self.assertEqual(instance.header_list[0]['key'], instance.key)
self.assertEqual(instance.header_list[0]['certificate'], instance.certificate)
self.assertEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content) self.assertEqual(open(os.path.join(partition, slapos.grid.slapgrid.COMPUTER_PARTITION_LATEST_BANG_TIMESTAMP_FILENAME)).read(), timestamp_content)
...@@ -1134,6 +1120,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1134,6 +1120,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
def test_partition_timestamp(self): def test_partition_timestamp(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1149,10 +1136,11 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1149,10 +1136,11 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.grid.processComputerPartitionList(), slapgrid.SLAPGRID_SUCCESS)
self.assertIn(timestamp, open(timestamp_path).read()) self.assertIn(timestamp, open(timestamp_path).read())
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
def test_partition_timestamp_develop(self): def test_partition_timestamp_develop(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1170,11 +1158,12 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1170,11 +1158,12 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition', ['/availableComputerPartition', '/stoppedComputerPartition',
'availableComputerPartition', 'stoppedComputerPartition']) '/availableComputerPartition', '/stoppedComputerPartition'])
def test_partition_old_timestamp(self): def test_partition_old_timestamp(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1188,10 +1177,11 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1188,10 +1177,11 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
instance.timestamp = str(int(timestamp) - 1) instance.timestamp = str(int(timestamp) - 1)
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
def test_partition_timestamp_new_timestamp(self): def test_partition_timestamp_new_timestamp(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1206,13 +1196,14 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1206,13 +1196,14 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS) self.assertEqual(self.launchSlapgrid(), slapgrid.SLAPGRID_SUCCESS)
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'stoppedComputerPartition', 'getFullComputerInformation', '/stoppedComputerPartition', '/getFullComputerInformation',
'availableComputerPartition', 'stoppedComputerPartition', '/availableComputerPartition', '/stoppedComputerPartition',
'getFullComputerInformation']) '/getFullComputerInformation'])
def test_partition_timestamp_no_timestamp(self): def test_partition_timestamp_no_timestamp(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1227,9 +1218,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1227,9 +1218,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
instance.timestamp = None instance.timestamp = None
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', 'availableComputerPartition', ['/getFullComputerInformation', '/availableComputerPartition',
'stoppedComputerPartition', 'getFullComputerInformation', '/stoppedComputerPartition', '/getFullComputerInformation',
'availableComputerPartition', 'stoppedComputerPartition']) '/availableComputerPartition', '/stoppedComputerPartition'])
def test_partition_periodicity_remove_timestamp(self): def test_partition_periodicity_remove_timestamp(self):
""" """
...@@ -1237,6 +1228,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1237,6 +1228,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
removes the .timestamp file. removes the .timestamp file.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
timestamp = str(int(time.time())) timestamp = str(int(time.time()))
...@@ -1274,6 +1266,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1274,6 +1266,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
5. We check that modification time of .timestamp was modified 5. We check that modification time of .timestamp was modified
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 20, 20) computer = ComputerForTest(self.software_root, self.instance_root, 20, 20)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
timestamp = str(int(time.time() - 5)) timestamp = str(int(time.time() - 5))
instance0.timestamp = timestamp instance0.timestamp = timestamp
...@@ -1293,16 +1286,16 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1293,16 +1286,16 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
time.sleep(wanted_periodicity + 1) time.sleep(wanted_periodicity + 1)
for instance in computer.instance_list[1:]: for instance in computer.instance_list[1:]:
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
time.sleep(1) time.sleep(1)
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['availableComputerPartition', 'startedComputerPartition', ['/availableComputerPartition', '/startedComputerPartition',
'availableComputerPartition', 'startedComputerPartition', '/availableComputerPartition', '/startedComputerPartition',
]) ])
for instance in computer.instance_list[1:]: for instance in computer.instance_list[1:]:
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
self.assertGreater( self.assertGreater(
os.path.getmtime(os.path.join(instance0.partition_path, '.timestamp')), os.path.getmtime(os.path.join(instance0.partition_path, '.timestamp')),
last_runtime) last_runtime)
...@@ -1314,6 +1307,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1314,6 +1307,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
started. started.
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 20, 20) computer = ComputerForTest(self.software_root, self.instance_root, 20, 20)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
timestamp = str(int(time.time() - 5)) timestamp = str(int(time.time() - 5))
instance0.timestamp = timestamp instance0.timestamp = timestamp
...@@ -1332,15 +1326,15 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1332,15 +1326,15 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
time.sleep(wanted_periodicity + 1) time.sleep(wanted_periodicity + 1)
for instance in computer.instance_list[1:]: for instance in computer.instance_list[1:]:
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
time.sleep(1) time.sleep(1)
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['availableComputerPartition', 'stoppedComputerPartition', ['/availableComputerPartition', '/stoppedComputerPartition',
'availableComputerPartition', 'stoppedComputerPartition']) '/availableComputerPartition', '/stoppedComputerPartition'])
for instance in computer.instance_list[1:]: for instance in computer.instance_list[1:]:
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
self.assertNotEqual(os.path.getmtime(os.path.join(instance0.partition_path, self.assertNotEqual(os.path.getmtime(os.path.join(instance0.partition_path,
'.timestamp')), '.timestamp')),
last_runtime) last_runtime)
...@@ -1352,6 +1346,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1352,6 +1346,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
started. started.
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 20, 20) computer = ComputerForTest(self.software_root, self.instance_root, 20, 20)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
timestamp = str(int(time.time() - 5)) timestamp = str(int(time.time() - 5))
instance0.timestamp = timestamp instance0.timestamp = timestamp
...@@ -1371,16 +1366,16 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1371,16 +1366,16 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
time.sleep(wanted_periodicity + 1) time.sleep(wanted_periodicity + 1)
for instance in computer.instance_list[1:]: for instance in computer.instance_list[1:]:
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
time.sleep(1) time.sleep(1)
instance0.requested_state = 'destroyed' instance0.requested_state = 'destroyed'
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['availableComputerPartition', 'stoppedComputerPartition', ['/availableComputerPartition', '/stoppedComputerPartition',
'stoppedComputerPartition']) '/stoppedComputerPartition'])
for instance in computer.instance_list[1:]: for instance in computer.instance_list[1:]:
self.assertEqual(instance.sequence, self.assertEqual(instance.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
self.assertNotEqual(os.path.getmtime(os.path.join(instance0.partition_path, self.assertNotEqual(os.path.getmtime(os.path.join(instance0.partition_path,
'.timestamp')), '.timestamp')),
last_runtime) last_runtime)
...@@ -1397,8 +1392,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1397,8 +1392,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
4. We launch slapgrid anew and check that install as not been called again 4. We launch slapgrid anew and check that install as not been called again
""" """
timestamp = str(int(time.time()))
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1) computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
timestamp = str(int(time.time()))
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.software.setPeriodicity(-1) instance.software.setPeriodicity(-1)
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1419,8 +1415,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1419,8 +1415,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
new setup and one time because of periodicity = 0) new setup and one time because of periodicity = 0)
""" """
timestamp = str(int(time.time()))
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1) computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
timestamp = str(int(time.time()))
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.software.setPeriodicity(0) instance.software.setPeriodicity(0)
instance.timestamp = timestamp instance.timestamp = timestamp
...@@ -1435,6 +1432,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1435,6 +1432,7 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
2. One will fail but the other one will be processed correctly 2. One will fail but the other one will be processed correctly
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 2, 2) computer = ComputerForTest(self.software_root, self.instance_root, 2, 2)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
instance1 = computer.instance_list[1] instance1 = computer.instance_list[1]
instance1.software = computer.software_list[1] instance1.software = computer.software_list[1]
...@@ -1442,9 +1440,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase): ...@@ -1442,9 +1440,9 @@ class TestSlapgridCPPartitionProcessing(MasterMixin, unittest.TestCase):
exit 42""") exit 42""")
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['softwareInstanceError']) ['/softwareInstanceError'])
self.assertEqual(instance1.sequence, self.assertEqual(instance1.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
def test_one_partition_lacking_software_path_does_not_disturb_others(self): def test_one_partition_lacking_software_path_does_not_disturb_others(self):
""" """
...@@ -1452,15 +1450,16 @@ exit 42""") ...@@ -1452,15 +1450,16 @@ exit 42""")
2. One will fail but the other one will be processed correctly 2. One will fail but the other one will be processed correctly
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 2, 2) computer = ComputerForTest(self.software_root, self.instance_root, 2, 2)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
instance1 = computer.instance_list[1] instance1 = computer.instance_list[1]
instance1.software = computer.software_list[1] instance1.software = computer.software_list[1]
shutil.rmtree(instance0.software.srdir) shutil.rmtree(instance0.software.srdir)
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['softwareInstanceError']) ['/softwareInstanceError'])
self.assertEqual(instance1.sequence, self.assertEqual(instance1.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
def test_one_partition_lacking_software_bin_path_does_not_disturb_others(self): def test_one_partition_lacking_software_bin_path_does_not_disturb_others(self):
""" """
...@@ -1468,15 +1467,16 @@ exit 42""") ...@@ -1468,15 +1467,16 @@ exit 42""")
2. One will fail but the other one will be processed correctly 2. One will fail but the other one will be processed correctly
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 2, 2) computer = ComputerForTest(self.software_root, self.instance_root, 2, 2)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
instance1 = computer.instance_list[1] instance1 = computer.instance_list[1]
instance1.software = computer.software_list[1] instance1.software = computer.software_list[1]
shutil.rmtree(instance0.software.srbindir) shutil.rmtree(instance0.software.srbindir)
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['softwareInstanceError']) ['/softwareInstanceError'])
self.assertEqual(instance1.sequence, self.assertEqual(instance1.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
def test_one_partition_lacking_path_does_not_disturb_others(self): def test_one_partition_lacking_path_does_not_disturb_others(self):
""" """
...@@ -1484,15 +1484,16 @@ exit 42""") ...@@ -1484,15 +1484,16 @@ exit 42""")
2. One will fail but the other one will be processed correctly 2. One will fail but the other one will be processed correctly
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 2, 2) computer = ComputerForTest(self.software_root, self.instance_root, 2, 2)
with httmock.HTTMock(computer.request_handler):
instance0 = computer.instance_list[0] instance0 = computer.instance_list[0]
instance1 = computer.instance_list[1] instance1 = computer.instance_list[1]
instance1.software = computer.software_list[1] instance1.software = computer.software_list[1]
shutil.rmtree(instance0.partition_path) shutil.rmtree(instance0.partition_path)
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance0.sequence, self.assertEqual(instance0.sequence,
['softwareInstanceError']) ['/softwareInstanceError'])
self.assertEqual(instance1.sequence, self.assertEqual(instance1.sequence,
['availableComputerPartition', 'stoppedComputerPartition']) ['/availableComputerPartition', '/stoppedComputerPartition'])
def test_one_partition_buildout_fail_is_correctly_logged(self): def test_one_partition_buildout_fail_is_correctly_logged(self):
""" """
...@@ -1500,14 +1501,15 @@ exit 42""") ...@@ -1500,14 +1501,15 @@ exit 42""")
2. It will fail, make sure that whole log is sent to master 2. It will fail, make sure that whole log is sent to master
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1) computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
line1 = "Nerdy kitten: Can I has a process crash?" line1 = "Nerdy kitten: Can I haz a process crash?"
line2 = "Cedric: Sure, here it is." line2 = "Cedric: Sure, here it is."
instance.software.setBuildout("""#!/bin/sh instance.software.setBuildout("""#!/bin/sh
echo %s; echo %s; exit 42""" % (line1, line2)) echo %s; echo %s; exit 42""" % (line1, line2))
self.launchSlapgrid() self.launchSlapgrid()
self.assertEqual(instance.sequence, ['softwareInstanceError']) self.assertEqual(instance.sequence, ['/softwareInstanceError'])
# We don't care of actual formatting, we just want to have full log # We don't care of actual formatting, we just want to have full log
self.assertIn(line1, instance.error_log) self.assertIn(line1, instance.error_log)
self.assertIn(line2, instance.error_log) self.assertIn(line2, instance.error_log)
...@@ -1524,6 +1526,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1524,6 +1526,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
Test than an instance in "destroyed" state is correctly destroyed Test than an instance in "destroyed" state is correctly destroyed
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
instance.software.setBuildout(WRAPPER_CONTENT) instance.software.setBuildout(WRAPPER_CONTENT)
...@@ -1536,9 +1539,9 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1536,9 +1539,9 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', ['/getFullComputerInformation',
'availableComputerPartition', '/availableComputerPartition',
'startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual(instance.state, 'started') self.assertEqual(instance.state, 'started')
# Then destroy the instance # Then destroy the instance
...@@ -1555,9 +1558,9 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1555,9 +1558,9 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertIsNotCreated(wrapper_log) self.assertIsNotCreated(wrapper_log)
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', ['/getFullComputerInformation',
'stoppedComputerPartition', '/stoppedComputerPartition',
'destroyedComputerPartition']) '/destroyedComputerPartition'])
self.assertEqual(instance.state, 'destroyed') self.assertEqual(instance.state, 'destroyed')
def test_partition_list_is_complete_if_empty_destroyed_partition(self): def test_partition_list_is_complete_if_empty_destroyed_partition(self):
...@@ -1570,6 +1573,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1570,6 +1573,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
2. See if it destroyed 2. See if it destroyed
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
computer.sequence = [] computer.sequence = []
...@@ -1586,13 +1590,14 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1586,13 +1590,14 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertEqual( self.assertEqual(
computer.sequence, computer.sequence,
['getFullComputerInformation', 'stoppedComputerPartition', 'destroyedComputerPartition']) ['/getFullComputerInformation', '/stoppedComputerPartition', '/destroyedComputerPartition'])
def test_slapgrid_not_destroy_bad_instance(self): def test_slapgrid_not_destroy_bad_instance(self):
""" """
Checks that slapgrid-ur don't destroy instance not to be destroyed. Checks that slapgrid-ur don't destroy instance not to be destroyed.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
instance.software.setBuildout(WRAPPER_CONTENT) instance.software.setBuildout(WRAPPER_CONTENT)
...@@ -1605,9 +1610,9 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1605,9 +1610,9 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation', ['/getFullComputerInformation',
'availableComputerPartition', '/availableComputerPartition',
'startedComputerPartition']) '/startedComputerPartition'])
self.assertEqual('started', instance.state) self.assertEqual('started', instance.state)
# Then run usage report and see if it is still working # Then run usage report and see if it is still working
...@@ -1626,7 +1631,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1626,7 +1631,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log') wrapper_log = os.path.join(instance.partition_path, '.0_wrapper.log')
self.assertLogContent(wrapper_log, 'Working') self.assertLogContent(wrapper_log, 'Working')
self.assertEqual(computer.sequence, self.assertEqual(computer.sequence,
['getFullComputerInformation']) ['/getFullComputerInformation'])
self.assertEqual('started', instance.state) self.assertEqual('started', instance.state)
def test_slapgrid_instance_ignore_free_instance(self): def test_slapgrid_instance_ignore_free_instance(self):
...@@ -1635,6 +1640,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1635,6 +1640,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
software_release URI) is ignored by slapgrid-cp. software_release URI) is ignored by slapgrid-cp.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.software.name = None instance.software.name = None
...@@ -1645,7 +1651,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1645,7 +1651,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), []) self.assertItemsEqual(os.listdir(instance.partition_path), [])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, ['getFullComputerInformation']) self.assertEqual(computer.sequence, ['/getFullComputerInformation'])
def test_slapgrid_report_ignore_free_instance(self): def test_slapgrid_report_ignore_free_instance(self):
""" """
...@@ -1653,6 +1659,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1653,6 +1659,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
software_release URI) is ignored by slapgrid-ur. software_release URI) is ignored by slapgrid-ur.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.software.name = None instance.software.name = None
...@@ -1663,7 +1670,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase): ...@@ -1663,7 +1670,7 @@ class TestSlapgridUsageReport(MasterMixin, unittest.TestCase):
self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var']) self.assertItemsEqual(os.listdir(self.instance_root), ['0', 'etc', 'var'])
self.assertItemsEqual(os.listdir(instance.partition_path), []) self.assertItemsEqual(os.listdir(instance.partition_path), [])
self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash]) self.assertItemsEqual(os.listdir(self.software_root), [instance.software.software_hash])
self.assertEqual(computer.sequence, ['getFullComputerInformation']) self.assertEqual(computer.sequence, ['/getFullComputerInformation'])
class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase): class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase):
...@@ -1673,15 +1680,16 @@ class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase): ...@@ -1673,15 +1680,16 @@ class TestSlapgridSoftwareRelease(MasterMixin, unittest.TestCase):
2. It will fail, make sure that whole log is sent to master 2. It will fail, make sure that whole log is sent to master
""" """
computer = ComputerForTest(self.software_root, self.instance_root, 1, 1) computer = ComputerForTest(self.software_root, self.instance_root, 1, 1)
with httmock.HTTMock(computer.request_handler):
software = computer.software_list[0] software = computer.software_list[0]
line1 = "Nerdy kitten: Can I has a process crash?" line1 = "Nerdy kitten: Can I haz a process crash?"
line2 = "Cedric: Sure, here it is." line2 = "Cedric: Sure, here it is."
software.setBuildout("""#!/bin/sh software.setBuildout("""#!/bin/sh
echo %s; echo %s; exit 42""" % (line1, line2)) echo %s; echo %s; exit 42""" % (line1, line2))
self.launchSlapgridSoftware() self.launchSlapgridSoftware()
self.assertEqual(software.sequence, self.assertEqual(software.sequence,
['buildingSoftwareRelease', 'softwareReleaseError']) ['/buildingSoftwareRelease', '/softwareReleaseError'])
# We don't care of actual formatting, we just want to have full log # We don't care of actual formatting, we just want to have full log
self.assertIn(line1, software.error_log) self.assertIn(line1, software.error_log)
self.assertIn(line2, software.error_log) self.assertIn(line2, software.error_log)
...@@ -1733,6 +1741,7 @@ buildout = /path/to/buildout/binary ...@@ -1733,6 +1741,7 @@ buildout = /path/to/buildout/binary
class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_one_failing_promise(self): def test_one_failing_promise(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
worked_file = os.path.join(instance.partition_path, 'fail_worked') worked_file = os.path.join(instance.partition_path, 'fail_worked')
...@@ -1749,6 +1758,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1749,6 +1758,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_one_succeeding_promise(self): def test_one_succeeding_promise(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
self.fake_waiting_time = 0.1 self.fake_waiting_time = 0.1
...@@ -1766,8 +1776,8 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1766,8 +1776,8 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_stderr_has_been_sent(self): def test_stderr_has_been_sent(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
httplib.HTTPConnection._callback = computer.getServerResponse()
instance.requested_state = 'started' instance.requested_state = 'started'
self.fake_waiting_time = 0.5 self.fake_waiting_time = 0.5
...@@ -1793,6 +1803,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1793,6 +1803,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_timeout_works(self): def test_timeout_works(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
...@@ -1818,6 +1829,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1818,6 +1829,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_two_succeeding_promises(self): def test_two_succeeding_promises(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
...@@ -1840,6 +1852,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1840,6 +1852,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_one_succeeding_one_failing_promises(self): def test_one_succeeding_one_failing_promises(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
self.fake_waiting_time = 0.1 self.fake_waiting_time = 0.1
...@@ -1868,6 +1881,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase): ...@@ -1868,6 +1881,7 @@ class TestSlapgridCPWithMasterPromise(MasterMixin, unittest.TestCase):
def test_one_succeeding_one_timing_out_promises(self): def test_one_succeeding_one_timing_out_promises(self):
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
self.fake_waiting_time = 0.1 self.fake_waiting_time = 0.1
...@@ -1902,6 +1916,7 @@ class TestSlapgridDestructionLock(MasterMixin, unittest.TestCase): ...@@ -1902,6 +1916,7 @@ class TestSlapgridDestructionLock(MasterMixin, unittest.TestCase):
if specifying a retention lock delay. if specifying a retention lock delay.
""" """
computer = ComputerForTest(self.software_root, self.instance_root) computer = ComputerForTest(self.software_root, self.instance_root)
with httmock.HTTMock(computer.request_handler):
instance = computer.instance_list[0] instance = computer.instance_list[0]
instance.requested_state = 'started' instance.requested_state = 'started'
instance.filter_dict = {'retention_delay': 1.0 / (3600 * 24)} instance.filter_dict = {'retention_delay': 1.0 / (3600 * 24)}
...@@ -1929,3 +1944,4 @@ class TestSlapgridDestructionLock(MasterMixin, unittest.TestCase): ...@@ -1929,3 +1944,4 @@ class TestSlapgridDestructionLock(MasterMixin, unittest.TestCase):
time.sleep(1) time.sleep(1)
self.grid.agregateAndSendUsage() self.grid.agregateAndSendUsage()
self.assertFalse(os.path.exists(dummy_instance_file_path)) self.assertFalse(os.path.exists(dummy_instance_file_path))
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""Mocked httplib
"""
__all__ = []
def log(message):
"""Need to be overridden to get a proper logger
"""
pass
class HTTPConnection(object):
scheme = 'http'
def _callback(self, path, method, body, headers):
"""To get it works properly, you need to override
HTTPConnection._callback. This method received the instance, the path,
method and request body as parameter, and it has to return a tuple with
headers dictionary and body response string.
@param self object instance reference
@param URL the parsed URL
@param method the http method
@param body the request body
@param headers the request headers
@return tuple containing status integer, headers dictionary and body
response"""
return (0, {}, '', )
def __init__(self, host, port=None, strict=None,
timeout=None, source_address=None):
self.host = host
self.port = port
self.strict = strict
self.timeout = timeout
self.source_address = source_address
self.__response = None
def request(self, method, url, body=None, headers=None):
status, headers, body = self._callback(url, method, body, headers)
self.__response = HTTPResponse('HTTP/1.1', status, 'OK', body, headers)
def getresponse(self):
response = self.__response
self.__response = None
return response
def set_debuglevel(self, level):
pass
def set_tunnel(self, host, port=None, headers=None):
pass
def connect(self):
pass
def close(self):
pass
def putrequest(self, request, selector, skip_host=None,
skip_accept_encoding=None):
pass
def putheader(self, *args):
pass
def endheaders(self):
pass
def send(self, data):
pass
class HTTPSConnection(HTTPConnection):
def __init__(self, host, port=None, key_file=None,
cert_file=None, strict=None, timeout=None,
source_address=None):
super(HTTPSConnection, self).__init__(host, port, strict, timeout,
source_address)
self.certificate = open(cert_file, 'r').read()
self.key = open(key_file, 'r').read()
def request(self, method, url, body=None, headers=None):
headers['certificate'] = self.certificate
headers['key'] = self.key
status, headers, body = self._callback(url, method, body, headers)
self.__response = HTTPResponse('HTTP/1.1', status, 'OK', body, headers)
def getresponse(self):
response = self.__response
self.__response = None
return response
class HTTPResponse(object):
def __init__(self, version, status, reason, content, headers=()):
self.version = version
self.status = status
self.reason = reason
self.__headers = headers
self.__content = content
def read(self, amt=None):
result = None
if amt is None:
result = self.__content
self.__content = ''
else:
end = max(amt, len(self.__content))
result = self.__content[:end]
del self.__content[:end]
return result
def getheader(self, name, default=None):
pass
def getheaders(self):
pass
# -*- coding: utf-8 -*-
def response_ok(url, request):
return {
'status_code': 200,
'content': ''
}
...@@ -968,7 +968,7 @@ database_uri = %(tempdir)s/lib/external_proxy.db ...@@ -968,7 +968,7 @@ database_uri = %(tempdir)s/lib/external_proxy.db
'xml': xml_marshaller.xml_marshaller.dumps(computer_dict), 'xml': xml_marshaller.xml_marshaller.dumps(computer_dict),
} }
self.external_proxy_slap._connection_helper.POST('/loadComputerConfigurationFromXML', self.external_proxy_slap._connection_helper.POST('/loadComputerConfigurationFromXML',
parameter_dict=request_dict) data=request_dict)
def _checkInstanceIsFowarded(self, name, partition_parameter_kw, software_release): def _checkInstanceIsFowarded(self, name, partition_parameter_kw, software_release):
""" """
......
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