Commit f4ab205f authored by Cédric Le Ninivin's avatar Cédric Le Ninivin Committed by Titouan Soulard

request: Update Transaction file when using jIOAPI

parent 77733217
......@@ -28,9 +28,10 @@ import logging
from zc.buildout import UserError
from slapos.recipe.librecipe import wrap, JSON_SERIALISED_MAGIC_KEY
import json
import os
from slapos import slap as slapmodule
from slapos.slap import SoftwareProductCollection
from slapos.slap.slap import json_loads_byteified
from slapos.slap.slap import json_loads_byteified, COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
import slapos.recipe.librecipe.generic as librecipe
import traceback
......@@ -178,6 +179,7 @@ class Recipe(object):
if requested_state:
request_dict["state"] = requested_state
self._updateTransactionFile(options['partition-id'], name)
partition_dict = slap.jio_api_connector.post(request_dict)
if "$schema" in partition_dict and "error-response-schema.json" in partition_dict["$schema"]:
self.logger.warning(
......@@ -307,6 +309,29 @@ class Recipe(object):
update = install
def _updateTransactionFile(self, partition_id, name):
"""
Store reference to all Instances requested by this Computer Parition
"""
# Environ variable set by Slapgrid while processing this partition
instance_root = os.environ.get('SLAPGRID_INSTANCE_ROOT', '')
if not instance_root or not partition_id:
return
transaction_file_name = COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME % partition_id
transaction_file_path = os.path.join(instance_root, partition_id,
transaction_file_name)
try:
if os.access(os.path.join(instance_root, partition_id), os.W_OK):
if not os.path.exists(transaction_file_path):
transac_file = open(transaction_file_path, 'w')
transac_file.close()
with open(transaction_file_path, 'a') as transac_file:
transac_file.write('%s\n' % name)
except OSError:
return
class RequestOptional(Recipe):
"""
......
import httmock
import json
import mock
import os
import unittest
import tempfile
from collections import defaultdict
from slapos.recipe import request
from slapos.slap.slap import COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME
from test_slaposconfiguration import APIRequestHandler
from testfixtures import LogCapture
......@@ -195,6 +198,13 @@ class RecipejIOTestMixin:
"partition-id": self.instance_data["compute_partition_id"],
"software-url": self.instance_data["software_release_uri"],
}
instance_root = tempfile.mkdtemp()
partition_root = os.path.join(instance_root, self.instance_data["compute_partition_id"])
os.mkdir(partition_root)
os.environ['SLAPGRID_INSTANCE_ROOT'] = instance_root
transaction_file_name = COMPUTER_PARTITION_REQUEST_LIST_TEMPLATE_FILENAME % self.instance_data["compute_partition_id"]
self.transaction_file_path = os.path.join(partition_root,
transaction_file_name)
def test_no_return_in_options_logs(self):
api_handler = APIRequestHandler([
......@@ -220,6 +230,11 @@ class RecipejIOTestMixin:
expected_request_body["parameters"] = json.dumps(self.called_partition_parameter_kw)
self.assertEqual(
api_handler.request_payload_list[0], json.dumps(expected_request_body))
self.assertEqual(api_handler.sequence_list, ["/api/post/"])
self.assertTrue(os.path.exists(self.transaction_file_path))
with open(self.transaction_file_path, 'r') as f:
content_list = f.read().splitlines()
self.assertEqual(sorted(content_list), ['MyInstance'])
def test_return_in_options_logs(self):
api_handler = APIRequestHandler([
......@@ -244,6 +259,12 @@ class RecipejIOTestMixin:
self.assertEqual(
api_handler.request_payload_list[0], json.dumps(expected_request_body))
self.assertEqual(self.options["connection-anything"], "done")
self.assertEqual(api_handler.sequence_list, ["/api/post/"])
self.assertTrue(os.path.exists(self.transaction_file_path))
with open(self.transaction_file_path, 'r') as f:
content_list = f.read().splitlines()
self.assertEqual(sorted(content_list), ['MyInstance'])
def test_return_not_ready(self):
self.instance_data["connection_parameters"] = self.connection_parameter_dict_empty
......@@ -272,6 +293,11 @@ class RecipejIOTestMixin:
self.assertEqual(
api_handler.request_payload_list[0], json.dumps(expected_request_body))
self.assertEqual(self.options["connection-anything"], "")
self.assertEqual(api_handler.sequence_list, ["/api/post/"])
self.assertTrue(os.path.exists(self.transaction_file_path))
with open(self.transaction_file_path, 'r') as f:
content_list = f.read().splitlines()
self.assertEqual(sorted(content_list), ['MyInstance'])
def test_return_ready(self):
api_handler = APIRequestHandler([
......@@ -297,6 +323,35 @@ class RecipejIOTestMixin:
api_handler.request_payload_list[0], json.dumps(expected_request_body))
self.assertEqual(self.options["connection-anything"], "done")
self.assertIsInstance(self.options['connection-anything'], str)
self.assertEqual(api_handler.sequence_list, ["/api/post/"])
self.assertTrue(os.path.exists(self.transaction_file_path))
with open(self.transaction_file_path, 'r') as f:
content_list = f.read().splitlines()
self.assertEqual(sorted(content_list), ['MyInstance'])
def test_two_requests_return_ready(self):
# Request first instance
api_handler = APIRequestHandler([
("/api/get", json.dumps(self.instance_data)),
])
self.options['return'] = 'anything'
with httmock.HTTMock(api_handler.request_handler):
recipe = self.recipe(self.buildout, "request", self.options)
result = recipe.install()
# Request Second Instance
self.options["name"] = self.instance_data["title"] = 'MyInstance2'
self.instance_data["reference"] = "SOFTINST-13"
api_handler = APIRequestHandler([
("/api/get", json.dumps(self.instance_data)),
])
self.options['return'] = 'anything'
with httmock.HTTMock(api_handler.request_handler):
recipe = self.recipe(self.buildout, "request", self.options)
result = recipe.install()
self.assertTrue(os.path.exists(self.transaction_file_path))
with open(self.transaction_file_path, 'r') as f:
content_list = f.read().splitlines()
self.assertEqual(sorted(content_list), ['MyInstance', 'MyInstance2'])
class RequestjIOTest(RecipejIOTestMixin, unittest.TestCase):
recipe = request.Recipe
......
......@@ -18,12 +18,13 @@ class APIRequestHandler(object):
self.sequence_list = []
def request_handler(self, url, req):
self.sequence_list.append(url.path)
if url.path == "/getHateoasUrl":
return ""
elif url.path == "/getJIOAPIUrl":
return "https://127.0.0.1/api/"
self.sequence_list.append(url.path)
if not self.response_list and self.response_list[0][0] != url.path:
raise ValueError("Unexcpected call: %s %s" % (url.path, req.body))
......
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