Commit 9a79de52 authored by Romain Courteaud's avatar Romain Courteaud

slapos_json_rpc_api: test bang + use jIOWebSection_getObjectFromData

parent eedac28e
......@@ -56,7 +56,7 @@
"title": "Success Message",\n
"type": "string",\n
"description": "Success Message"\n
},\n
}\n
}\n
}\n
</string> </value>
......
compute_node = context.jIOWebSection_getObjectFromData(data_dict)
def compareAndUpdateAddressList(partition, partition_ip_list):
to_delete_ip_id_list = []
to_add_ip_dict_list = partition_ip_list[:]
......@@ -43,28 +45,28 @@ def compareAndUpdateAddressList(partition, partition_ip_list):
partition.deleteContent(to_delete_ip_id_list)
if "bang_status_message" in data_dict:
context.reportComputeNodeBang(comment=data_dict["bang_status_message"])
compute_node.reportComputeNodeBang(comment=data_dict["bang_status_message"])
# Getting existing partitions
existing_partition_dict = {}
for c in context.contentValues(portal_type="Compute Partition"):
for c in compute_node.contentValues(portal_type="Compute Partition"):
existing_partition_dict[c.getReference()] = c
# update compute_node data
edit_kw = {}
quantity = len(data_dict.get('compute_partition_list', []))
if context.getQuantity() != quantity:
if compute_node.getQuantity() != quantity:
edit_kw['quantity'] = quantity
if edit_kw:
context.edit(**edit_kw)
compute_node.edit(**edit_kw)
expected_partition_dict = {}
for send_partition in data_dict.get('compute_partition_list', []):
partition = existing_partition_dict.get(send_partition['partition_id'], None)
expected_partition_dict[send_partition['partition_id']] = True
if partition is None:
partition = context.newContent(portal_type='Compute Partition')
partition = compute_node.newContent(portal_type='Compute Partition')
partition.validate()
partition.markFree()
elif partition.getSlapState() == 'inactive':
......@@ -85,11 +87,10 @@ for key, value in existing_partition_dict.items():
if value.getValidationState() == "validated":
value.invalidate(comment="Desactivated by slapformat")
import json
return json.dumps({
return {
"$schema": json_form.absolute_url().strip() + "/getOutputJSONSchema",
"compute_node_id": context.getReference(),
"compute_node_id": compute_node.getReference(),
"date": str(DateTime()),
"portal_type": "Compute Node",
"success": "Done"
}, indent=2)
}
from erp5.component.document.JsonRpcAPIService import JsonRpcAPIError
class ComputeNodeNotFoundError(JsonRpcAPIError):
type = "COMPUTE-NODE-NOT-FOUND"
status = 403
class RequestFailsError(JsonRpcAPIError):
type = "SUPPLY-FAILED"
status = 403
compute_node = context.getPortalObject().portal_catalog.getResultValue(
portal_type='Compute Node',
validation_state="validated",
reference=data_dict["compute_node_id"],
)
if not compute_node:
raise ComputeNodeNotFoundError("Compute Node '%s' not found" % data_dict["compute_node_id"])
compute_node = context.jIOWebSection_getObjectFromData(data_dict)
compute_node.requestSoftwareRelease(
software_release_url=data_dict["software_release_uri"],
......
from erp5.component.document.JsonRpcAPIService import JsonRpcAPIError
portal_type = data_dict["portal_type"]
import urllib
......@@ -11,6 +13,11 @@ if portal_type == "Software Installation":
return compute_node.getSoftwareInstallationFromUrl(urllib.unquote(data_dict["software_release_uri"]))
elif portal_type == "Compute Node":
class ComputeNodeNotFoundError(JsonRpcAPIError):
type = "COMPUTE-NODE-NOT-FOUND"
status = 403
if "compute_node_id" in data_dict:
compute_node_id = data_dict["compute_node_id"]
user = portal.portal_membership.getAuthenticatedMember().getUserName()
......@@ -21,6 +28,7 @@ elif portal_type == "Compute Node":
compute_node = portal.portal_catalog.getComputeNodeObject(compute_node_id)
if compute_node:
return compute_node
raise ComputeNodeNotFoundError("Compute Node '%s' not found" % data_dict["compute_node_id"])
elif portal_type == "Software Instance":
if "reference" in data_dict:
......@@ -50,5 +58,7 @@ elif portal_type == "Software Instance Certificate Record":
portal_type="Software Instance Certificate Record",
)
return None
class ObjectNotFoundError(JsonRpcAPIError):
type = "OBJECT-NODE-NOT-FOUND"
status = 403
raise ObjectNotFoundError("Object not found")
......@@ -1253,30 +1253,47 @@ class TestSlapOSSlapToolPersonAccess(TestSlapOSJsonRpcMixin):
self.person_user_id = self.person.getUserId()
def test_30_computerBang(self):
self.called_banged_kw = ""
def calledBang(*args, **kw):
self.called_banged_kw = kw
start_date = DateTime()
try:
reportComputeNodeBang = self.compute_node.__class__.reportComputeNodeBang
self.compute_node.__class__.reportComputeNodeBang = calledBang
self.login(self.person_user_id)
error_log = 'Please bang me'
response = self.callJsonRpcWebService("slapos.put.slapos_jio_api_put_compute_node", {
# XXX XXX XXX TODO pin datetime to check the result
error_log = 'Please force slapos node rerun'
response = self.callJsonRpcWebService(
"slapos.put.slapos_jio_api_put_compute_node",
{
"compute_node_id": self.compute_node_id,
"portal_type": "Compute Node",
"bang_status_message": error_log,
})
response_dict = json.loads(response.getBody())
self.assertEqual(self.called_banged_kw, {"comment": error_log})
self.assertEqual(response_dict["compute_node_id"], self.compute_node.getReference())
self.assertEqual(response_dict["success"], "Done")
self.assertEqual(response_dict["portal_type"], "Compute Node")
self.assertTrue(response_dict["$schema"].endswith("ComputeNode_updateFromJSON/getOutputJSONSchema"))
self.assertTrue(DateTime(response_dict["date"]) >= start_date)
finally:
self.compute_node.__class__.reportComputeNodeBang = reportComputeNodeBang
},
self.person_user_id
)
response_dict = json.loads(response.getBody())
self.assertEqual('application/json', response.headers.get('content-type'))
"""
self.assertEqual(
response.getBody(),
json.dumps(
{
'type': 'success-type',
'title': "query completed",
'status': 200
}).encode())"""
self.assertEqual(response.getStatus(), 200)
self.assertEqual(response_dict["compute_node_id"], self.compute_node.getReference().decode('UTF-8'))
self.assertEqual(response_dict["success"], "Done")
self.assertEqual(response_dict["portal_type"], "Compute Node")
self.assertTrue(response_dict["$schema"].endswith("ComputeNode_updateFromJSON/getOutputJSONSchema"))
self.assertTrue(DateTime(response_dict["date"]) >= start_date)
portal_workflow = self.portal.portal_workflow
comment = portal_workflow.getInfoFor(ob=self.compute_node,
name='comment',
wf_id='compute_node_slap_interface_workflow')
self.assertEqual(comment, error_log)
action_id = portal_workflow.getInfoFor(ob=self.compute_node,
name='action',
wf_id='compute_node_slap_interface_workflow')
self.assertEqual(action_id, 'report_compute_node_bang')
def test_31_getInstanceWithSharedInstance(self, with_slave=True):
self._makeComplexComputeNode(self.project, person=self.person, with_slave=with_slave)
......
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