Commit 9665b9c6 authored by Romain Courteaud's avatar Romain Courteaud

tests/test_slapgrid: request_handler with json rpc

parent 3b7416ea
......@@ -397,6 +397,7 @@ class ComputerForTest(object):
Will set up instances, software and sequence
"""
self.sequence = []
self.body_sequence = []
self.instance_amount = instance_amount
self.software_amount = software_amount
self.software_root = software_root
......@@ -422,6 +423,127 @@ class ComputerForTest(object):
and error and error message by partition
"""
self.sequence.append(url.path)
if req.method == 'POST':
content = json.loads(req.body)
self.body_sequence.append(content)
if (url.path == '/slapos.allDocs.instance'):
if "compute_node_id" in content:
"""
+ if "compute_partition_id" in content:
+ return json.dumps({
+ "current_page_full": False,
+ "next_page_request": {
+ "portal_type": "Software Instance",
+ "compute_partition_id": content["compute_partition_id"],
+ },
+ "result_list": [{
+ "software_release_uri": x.software.name if x.software else None,
+ "reference": x.name,
+ "title": x.name,
+ "portal_type": "Software Instance",
+ "compute_partition_id": x.name,
+ "state": x.requested_state,
+ "api_revision": "12132",
+ "get_parameters": {
+ "portal_type": "Software Instance",
+ "reference": x.name,
+ }
+ } for x in self.instance_list if x.name == content["compute_partition_id"]]
+ })
+ else:
"""
return json.dumps({
"current_page_full": False,
"next_page_request": {
"portal_type": "Software Instance",
"compute_node_id": content["compute_node_id"],
},
"result_list": [{
"software_release_uri": x.software.name if x.software else None,
"reference": x.name,
"title": x.name,
"portal_type": "Software Instance",
"compute_partition_id": x.name,
"state": x.requested_state,
"get_parameters": {
"portal_type": "Software Instance",
"reference": x.name,
}
} for x in self.instance_list]
})
elif (url.path == '/slapos.get.software_instance_certificate'):
reference = content["reference"]
requested_instance = None
# raise NotImplementedError(self.instance_list)
for instance in self.instance_list:
if instance.name == reference:
requested_instance = instance
break
if requested_instance:
# We don't need to check certificates are being retrieved
return json.dumps({
"reference": requested_instance.name,
"certificate": getattr(requested_instance, 'certificate', ''),
"key": getattr(requested_instance, 'key', ''),
"portal_type": "Software Instance Certificate Record",
})
else:
return {'status_code': 500, 'reason': 'reference not found'}
if (url.path == '/slapos.put.software_instance'):
slap_computer = self.getComputer(content.pop('computer_id')[0])
instance = self.instance_list[int(content.pop('computer_partition_id')[0])]
assert content.pop('portal_type') == 'Software Instance'
if 'reported_state' in content:
assert content['reported_state'] in ["started", "destroyed", "stopped", "error", "bang"]
instance.state = content.pop('reported_state')
if 'status_message' in content:
instance.error_log = content.pop('status_message')
if content:
# Check that there is no other input parameters
return {'status_code': 500, 'reason': 'Unhandled body content: %s' % str(content)}
return json.dumps({'foo': 'bar'})
"""
+ elif "root_instance_title" in content:
+ return json.dumps({
+ "current_page_full": False,
+ "next_page_request": {
+ "portal_type": "Software Instance",
+ "root_instance_title": content["root_instance_title"],
+ },
+ "result_list": [{
+ "software_release_uri": x.software.name if x.software else None,
+ "reference": x.name,
+ "title": x.name,
+ "portal_type": "Software Instance",
+ "compute_partition_id": x.name,
+ "state": x.requested_state,
+ "get_parameters": {
+ "portal_type": "Software Instance",
+ "reference": x.name,
+ },
+ } for x in self.instance_list] + [
+ {
+ "software_release_uri": "foo.cfg",
+ "reference": "related_instance",
+ "title": "related_instance",
+ "portal_type": "Software Instance",
+ "compute_partition_id": "related_instance",
+ "state": "stopped",
+ "get_parameters": {
+ "portal_type": "Software Instance",
+ "reference": "related_instance",
+ }
+ }
+ ]
+ })
"""
"""
if req.method == 'GET':
qs = parse.parse_qs(url.query)
else:
......@@ -448,29 +570,13 @@ class ComputerForTest(object):
instance = self.instance_list[int(qs['computer_partition_id'][0])]
instance.sequence.append(url.path)
instance.header_list.append(req.headers)
if url.path == '/startedComputerPartition':
instance.state = 'started'
return {'status_code': self.status_code}
if url.path == '/stoppedComputerPartition':
instance.state = 'stopped'
return {'status_code': self.status_code}
if url.path == '/destroyedComputerPartition':
instance.state = 'destroyed'
return {'status_code': self.status_code}
if url.path == '/softwareInstanceBang':
return {'status_code': self.status_code}
if url.path == "/updateComputerPartitionRelatedInstanceList":
return {'status_code': self.status_code}
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': self.status_code}
elif req.method == 'POST' and 'url' in qs:
# XXX hardcoded to first software release!
......@@ -494,7 +600,8 @@ class ComputerForTest(object):
return {'status_code': self.status_code}
else:
return {'status_code': 404, 'reason': 'test_slapgrid does not handle'}
"""
return {'status_code': 404, 'reason': 'test_slapgrid does not handle'}
def getTestSoftwareClass(self):
return SoftwareForTest
......@@ -554,6 +661,7 @@ class InstanceForTest(object):
self.error = False
self.error_log = None
self.sequence = []
self.body_sequence = []
self.header_list = []
self.name = name
self.partition_path = os.path.join(self.instance_root, self.name)
......@@ -650,6 +758,7 @@ class SoftwareForTest(object):
self.software_root = software_root
self.name = 'http://sr%s/' % name
self.sequence = []
self.body_sequence = []
self.software_hash = md5digest(self.name)
self.srdir = os.path.join(self.software_root, self.software_hash)
self.requested_state = 'available'
......@@ -695,6 +804,7 @@ touch worked"""):
class DummyManager(object):
def __init__(self):
self.sequence = []
self.body_sequence = []
def format(self, computer):
self.sequence.append('format')
......
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