From 30843238dc79b51841bc33bc7b5639bcf7685d11 Mon Sep 17 00:00:00 2001 From: Felipe Monteiro Date: Thu, 22 Sep 2016 10:28:31 -0400 Subject: [PATCH] Increase unit test coverage for Static Actions API. Implements bp: murano-unit-test-coverage Co-Authored-By: David Purcell Co-Authored-By: Samantha Blanco Co-Authored-By: Julian Sy Change-Id: I5dc8e1907d45972adb7db90ad9330a2eae7ab9a9 --- .../tests/unit/api/v1/test_static_actions.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/murano/tests/unit/api/v1/test_static_actions.py b/murano/tests/unit/api/v1/test_static_actions.py index 722d1c7e..6a63ce7c 100644 --- a/murano/tests/unit/api/v1/test_static_actions.py +++ b/murano/tests/unit/api/v1/test_static_actions.py @@ -1,4 +1,5 @@ # Copyright (c) 2016 Mirantis, Inc. +# Copyright (c) 2016 AT&T Corp # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,7 +16,9 @@ import mock +from oslo_messaging.rpc import client from oslo_serialization import jsonutils +from webob import exc from murano.api.v1 import static_actions from murano.common import policy @@ -63,3 +66,69 @@ class TestStaticActionsApi(tb.ControllerTest, tb.MuranoApiTestCase): pass self.mock_engine_rpc.call_static_action.assert_called_once_with( rpc_task) + + def test_execute_static_action_handle_bad_data_exc(self, _): + request_data = { + "className": None, + "methodName": 'TestAction' + } + req = self._post('/actions', jsonutils.dump_as_bytes(request_data)) + self.assertRaises(exc.HTTPBadRequest, self.controller.execute, req, + request_data) + + request_data = { + "className": 'TestClass', + "methodName": None + } + req = self._post('/actions', jsonutils.dump_as_bytes(request_data)) + self.assertRaises(exc.HTTPBadRequest, self.controller.execute, req, + request_data) + + @mock.patch('murano.services.static_actions.StaticActionServices.execute') + def test_execute_static_action_handle_execute_excs(self, mock_execute, _): + """Test whether execute handles all exceptions thrown correctly.""" + request_data = { + "className": 'TestClass', + "methodName": 'TestAction', + "packageName": 'TestPackage', + "classVersion": '=0', + "parameters": {'name': 'John'} + } + + exc_types = ['NoClassFound', 'NoMethodFound', + 'NoPackageFound', 'NoPackageForClassFound', + 'MethodNotExposed', 'NoMatchingMethodException'] + for exc_type in exc_types: + mock_execute.side_effect = client.RemoteError(exc_type=exc_type) + req = self._post('/actions', jsonutils.dump_as_bytes(request_data)) + self.assertRaises(exc.HTTPNotFound, self.controller.execute, req, + request_data) + self.assertEqual(mock_execute.call_count, len(exc_types)) + + exc_type = 'ContractViolationException' + mock_execute.side_effect = client.RemoteError(exc_type=exc_type) + req = self._post('/actions', jsonutils.dump_as_bytes(request_data)) + self.assertRaises(exc.HTTPBadRequest, self.controller.execute, req, + request_data) + exc_types.append(exc_type) + self.assertEqual(mock_execute.call_count, len(exc_types)) + + exc_type = 'ThisIsARandomTestException' + mock_execute.side_effect = client.RemoteError(exc_type=exc_type) + req = self._post('/actions', jsonutils.dump_as_bytes(request_data)) + self.assertRaises(exc.HTTPServiceUnavailable, self.controller.execute, + req, request_data) + exc_types.append(exc_type) + self.assertEqual(mock_execute.call_count, len(exc_types)) + + try: + int('this will throw a value error') + except ValueError as e: + setattr(e, 'message', None) + exc_type = e + mock_execute.side_effect = exc_type + req = self._post('/actions', jsonutils.dump_as_bytes(request_data)) + self.assertRaises(exc.HTTPBadRequest, self.controller.execute, + req, request_data) + exc_types.append(exc_type) + self.assertEqual(mock_execute.call_count, len(exc_types))