From 2106494d412c9f5ff2b57e83748d157f848e69f9 Mon Sep 17 00:00:00 2001 From: Andrew Pashkin Date: Fri, 6 Mar 2015 19:07:50 +0300 Subject: [PATCH] Fixed 500 error in get_result API handler If there is no task in DB with environemnt id and task id passed in handler, a server error was returned by the API. This patch fixes this bug. Closes-Bug: #162206 Change-Id: I9fd84a963a1eacf8ab2aa428cfb4365f80fff02b --- murano/api/v1/actions.py | 14 +++++- murano/services/actions.py | 6 ++- murano/tests/unit/api/v1/test_actions.py | 57 ++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/murano/api/v1/actions.py b/murano/api/v1/actions.py index 8d11ef24..b9971a73 100644 --- a/murano/api/v1/actions.py +++ b/murano/api/v1/actions.py @@ -20,7 +20,7 @@ from murano.db import models from murano.db.services import environments as envs from murano.db.services import sessions from murano.db import session as db_session -from murano.common.i18n import _LI, _LE +from murano.common.i18n import _LI, _LE, _ from murano.openstack.common import log as logging from murano.services import actions from murano.services import states @@ -79,7 +79,17 @@ class Controller(object): unit = db_session.get_session() self._validate_environment(unit, request, environment_id) - return actions.ActionServices.get_result(environment_id, task_id, unit) + result = actions.ActionServices.get_result(environment_id, task_id, + unit) + + if result is not None: + return result + msg = (_('Result for task with environment_id: {} and ' + 'task_id: {} was not found.') + .format(environment_id, task_id)) + + LOG.error(msg) + raise exc.HTTPNotFound(msg) def create_resource(): diff --git a/murano/services/actions.py b/murano/services/actions.py index 1871138e..92a2f027 100644 --- a/murano/services/actions.py +++ b/murano/services/actions.py @@ -111,4 +111,8 @@ class ActionServices(object): def get_result(environment_id, task_id, unit): task = unit.query(models.Task).filter_by( id=task_id, environment_id=environment_id).first() - return task.result + + if task is not None: + return task.result + + return None diff --git a/murano/tests/unit/api/v1/test_actions.py b/murano/tests/unit/api/v1/test_actions.py index 044823af..8a8109d7 100644 --- a/murano/tests/unit/api/v1/test_actions.py +++ b/murano/tests/unit/api/v1/test_actions.py @@ -95,3 +95,60 @@ class TestActionsApi(tb.ControllerTest, tb.MuranoApiTestCase): self.mock_engine_rpc.handle_task.assert_called_once_with(rpc_task) self.assertIn('task_id', result) + + def test_get_result(self, _): + """Result of task with given id and environment id is returned.""" + now = timeutils.utcnow() + expected_environment_id = 'test_environment' + expected_task_id = 'test_task' + expected_result = {'test_result': 'test_result'} + + environment = models.Environment( + id=expected_environment_id, + name='test_environment', created=now, updated=now, + tenant_id=self.tenant + ) + + task = models.Task( + id=expected_task_id, + started=now, + finished=now, + result=expected_result, + environment_id=expected_environment_id + ) + + test_utils.save_models(environment, task) + + request = self._get( + '/environments/{environment_id}/actions/{task_id}' + .format(environment_id=expected_environment_id, + task_id=expected_task_id), + ) + + response = request.get_response(self.api) + + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json, expected_result) + + def test_get_result_not_found(self, _): + """If task does not exists, it should be handled correctly + and API should return 404. + """ + expected_environment_id = 'test_environment' + + environment = models.Environment( + id=expected_environment_id, + name='test_environment', + tenant_id=self.tenant + ) + test_utils.save_models(environment) + + request = self._get( + '/environments/{environment_id}/actions/{task_id}' + .format(environment_id=expected_environment_id, + task_id='not_existent_task_id'), + ) + + response = request.get_response(self.api) + + self.assertEqual(response.status_code, 404)