Commit 8267d5c4 by Simon János

calling deploy method from api; mocking celery in api tests

parent 09e73a8e
Pipeline #296 passed with stage
in 3 minutes 57 seconds
...@@ -36,8 +36,11 @@ class Stack(object): ...@@ -36,8 +36,11 @@ class Stack(object):
if request.content_length: if request.content_length:
req = request.stream.read().decode('utf8') req = request.stream.read().decode('utf8')
stack = ResourceGroup(json.loads(req)) stack = ResourceGroup(json.loads(req))
flat_stack = stack.flatten(flat_leaves=False)
self.__deploy_resources(flat_stack)
self.stacks[stack.id] = stack self.stacks[stack.id] = stack
response.body = str(stack) response.body = str(stack)
response.status = falcon.HTTP_CREATED
else: else:
raise falcon.HTTPBadRequest('Empty request received') raise falcon.HTTPBadRequest('Empty request received')
...@@ -46,14 +49,28 @@ class Stack(object): ...@@ -46,14 +49,28 @@ class Stack(object):
update_body = request.stream.read().decode('utf8') update_body = request.stream.read().decode('utf8')
updated_stack = json.loads(update_body) updated_stack = json.loads(update_body)
diff = self.stacks[stack_id].diff(ResourceGroup(updated_stack), flat_leaves=False) diff = self.stacks[stack_id].diff(ResourceGroup(updated_stack), flat_leaves=False)
self.__deploy_resources(diff['added'])
response.body = json.dumps(diff, default=Resource.json_encoder) response.body = json.dumps(diff, default=Resource.json_encoder)
self.stacks[stack_id] = updated_stack self.stacks[stack_id] = updated_stack
else: else:
raise falcon.HTTPNotFound() raise falcon.HTTPNotFound()
def on_delete(self, _request, _response, stack_id): def __deploy_resources(self, flat_stack):
results = {}
for fqn, resource in flat_stack.items():
results[fqn] = resource.deploy(fqn)
self.logger.info('Deployment started: %s', fqn)
for fqn, result in results.items():
if result is not None:
result.wait(timeout=cfg.CONF.rpc.operation_timeout)
self.logger.info('Deployment finished: %s (%s)', fqn, result.status)
else:
self.logger.info('Deployment status N/A of %s', fqn)
def on_delete(self, _request, response, stack_id):
if stack_id in self.stacks.keys(): if stack_id in self.stacks.keys():
del self.stacks[stack_id] del self.stacks[stack_id]
response.status = falcon.HTTP_NO_CONTENT
else: else:
raise falcon.HTTPNotFound() raise falcon.HTTPNotFound()
......
import json import json
from falcon import HTTP_OK, HTTP_METHOD_NOT_ALLOWED, HTTP_BAD_REQUEST, HTTP_NOT_FOUND from celery import Celery
from falcon.testing import TestCase from falcon import HTTP_OK, HTTP_METHOD_NOT_ALLOWED, HTTP_BAD_REQUEST, HTTP_NOT_FOUND, HTTP_CREATED, HTTP_NO_CONTENT
from falcon import testing
from mockito import mock, when, any # pylint: disable=redefined-builtin
from orchestrator.api import stacks from orchestrator.api import stacks
...@@ -10,10 +12,21 @@ TEST_INSTANCE_ARGS = { ...@@ -10,10 +12,21 @@ TEST_INSTANCE_ARGS = {
'memory': 256 * 1000 * 1000 'memory': 256 * 1000 * 1000
} }
class StackTest(TestCase):
class StackTest(testing.TestCase):
def setUp(self): def setUp(self):
super(StackTest, self).setUp() super(StackTest, self).setUp()
self.app = stacks.create_app() self.app = stacks.create_app()
self.__mock_celery()
def __mock_celery(self):
self.async_result_mock = mock()
self.async_result_mock.status = 'TEST_STATUS'
when(Celery).send_task(any(), args=any(), queue=any()).thenReturn(self.async_result_mock)
when(self.async_result_mock).wait(timeout=any()).thenReturn()
def tearDown(self):
super(StackTest, self).tearDown()
def test_stacks_api_allowed_methods_on_endpoints(self): def test_stacks_api_allowed_methods_on_endpoints(self):
self.assertEqual(HTTP_OK, self.simulate_get('/stacks').status) self.assertEqual(HTTP_OK, self.simulate_get('/stacks').status)
...@@ -72,6 +85,7 @@ class StackTest(TestCase): ...@@ -72,6 +85,7 @@ class StackTest(TestCase):
# then # then
self.assertEqual(sort_resources(stack_request_body), sort_resources(result.json)) self.assertEqual(sort_resources(stack_request_body), sort_resources(result.json))
self.assertEqual(HTTP_CREATED, result.status)
def test_stacks_api_create_with_empty_json(self): def test_stacks_api_create_with_empty_json(self):
# given # given
...@@ -146,7 +160,7 @@ class StackTest(TestCase): ...@@ -146,7 +160,7 @@ class StackTest(TestCase):
# then # then
self.assertEqual([resource], list_result_before_delete.json) self.assertEqual([resource], list_result_before_delete.json)
self.assertEqual(HTTP_OK, delete_result.status) self.assertEqual(HTTP_NO_CONTENT, delete_result.status)
self.assertEqual([], list_result_after_delete.json) self.assertEqual([], list_result_after_delete.json)
def test_stacks_api_get_list(self): def test_stacks_api_get_list(self):
......
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 sign in to comment