Merge "Increase unit test coverage for Static Actions API."

This commit is contained in:
Jenkins 2016-09-28 05:34:09 +00:00 committed by Gerrit Code Review
commit a8800d9b14

View File

@ -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))