diff options
author | Dileep Ranganathan <dileep.ranganathan@intel.com> | 2018-02-13 03:32:12 -0800 |
---|---|---|
committer | Dileep Ranganathan <dileep.ranganathan@intel.com> | 2018-02-13 08:08:41 -0800 |
commit | 1396b1237f9b5997b96d5b0cf1a1907e2163e5a4 (patch) | |
tree | e4a90bf6c79676d1b5f9117faee32e6c044a614e /conductor | |
parent | e761b422db53e8d81c875a3f0e7806fe942463cc (diff) |
Added more Unit tests for conductor/api module
Fixed the MUSIC rpc mock for plans api
Issue-ID: OPTFRA-68
Change-Id: Ic0075c1816ed84133b2248e1b91e5e39ff4866d8
Signed-off-by: Dileep Ranganathan <dileep.ranganathan@intel.com>
Diffstat (limited to 'conductor')
3 files changed, 103 insertions, 9 deletions
diff --git a/conductor/conductor/tests/unit/api/base_api.py b/conductor/conductor/tests/unit/api/base_api.py index 6adc410..4c96bf6 100644 --- a/conductor/conductor/tests/unit/api/base_api.py +++ b/conductor/conductor/tests/unit/api/base_api.py @@ -21,6 +21,8 @@ import os import eventlet +import mock + eventlet.monkey_patch(os=False) import pecan @@ -30,9 +32,6 @@ from oslo_config import fixture as config_fixture from oslo_serialization import jsonutils from oslotest import base as oslo_test_base -from conductor import service -from conductor import controller - class BaseApiTest(oslo_test_base.BaseTestCase): """Pecan controller functional testing class. @@ -56,7 +55,6 @@ class BaseApiTest(oslo_test_base.BaseTestCase): def _make_app(self): # Determine where we are so we can set up paths in the config - root_dir = self.path_get() self.app_config = { 'app': { @@ -90,4 +88,4 @@ class BaseApiTest(oslo_test_base.BaseTestCase): def assertJsonEqual(self, expected, observed): """Asserts that 2 complex data structures are json equivalent.""" self.assertEqual(jsonutils.dumps(expected, sort_keys=True), - jsonutils.dumps(observed, sort_keys=True))
\ No newline at end of file + jsonutils.dumps(observed, sort_keys=True)) diff --git a/conductor/conductor/tests/unit/api/controller/test_root.py b/conductor/conductor/tests/unit/api/controller/test_root.py index 5821b4d..7a2d233 100644 --- a/conductor/conductor/tests/unit/api/controller/test_root.py +++ b/conductor/conductor/tests/unit/api/controller/test_root.py @@ -30,6 +30,6 @@ class TestRoot(base_api.BaseApiTest): req_json_file = './conductor/tests/unit/api/controller/versions.json' expected_response = json.loads(open(req_json_file).read()) # print('GOT:%s' % actual_response) - self.assertJsonEqual(actual_response.status_int, 200) + self.assertEqual(200, actual_response.status_int) self.assertJsonEqual(expected_response, json.loads(actual_response.body)) diff --git a/conductor/conductor/tests/unit/api/controller/v1/test_plans.py b/conductor/conductor/tests/unit/api/controller/v1/test_plans.py index 07de14f..8ae1c8e 100644 --- a/conductor/conductor/tests/unit/api/controller/v1/test_plans.py +++ b/conductor/conductor/tests/unit/api/controller/v1/test_plans.py @@ -18,7 +18,9 @@ # """Test case for PlansController""" +import copy import json +import uuid import mock from conductor.api.controllers.v1 import plans @@ -37,7 +39,14 @@ class TestPlansController(base_api.BaseApiTest): @mock.patch.object(plans.LOG, 'debug') @mock.patch.object(plans.LOG, 'warning') @mock.patch.object(plans.LOG, 'info') - def test_index_get(self, info_mock, warning_mock, debug_mock, error_mock): + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_get(self, rpc_mock, info_mock, warning_mock, debug_mock, + error_mock): + req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' + params = json.loads(open(req_json_file).read()) + plan_id = str(uuid.uuid4()) + params['id'] = plan_id + rpc_mock.return_value = {'plans': [params]} actual_response = self.app.get('/v1/plans') self.assertEqual(200, actual_response.status_int) @@ -45,16 +54,103 @@ class TestPlansController(base_api.BaseApiTest): @mock.patch.object(plans.LOG, 'debug') @mock.patch.object(plans.LOG, 'warning') @mock.patch.object(plans.LOG, 'info') - def test_index_post(self, info_mock, warning_mock, debug_mock, error_mock): + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_post_error(self, rpc_mock, info_mock, warning_mock, + debug_mock, + error_mock): req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' params = jsonutils.dumps(json.loads(open(req_json_file).read())) - print(params) + rpc_mock.return_value = {} response = self.app.post('/v1/plans', params=params, expect_errors=True) self.assertEqual(500, response.status_int) + @mock.patch.object(plans.LOG, 'error') + @mock.patch.object(plans.LOG, 'debug') + @mock.patch.object(plans.LOG, 'warning') + @mock.patch.object(plans.LOG, 'info') + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_post_success(self, rpc_mock, info_mock, warning_mock, + debug_mock, + error_mock): + req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' + params = json.loads(open(req_json_file).read()) + mock_params = copy.deepcopy(params) + plan_id = str(uuid.uuid4()) + + mock_params['id'] = plan_id + rpc_mock.return_value = {'plan': mock_params} + params = json.dumps(params) + response = self.app.post('/v1/plans', params=params, + expect_errors=True) + self.assertEqual(201, response.status_int) + def test_index_httpmethod_notallowed(self): actual_response = self.app.put('/v1/plans', expect_errors=True) self.assertEqual(405, actual_response.status_int) actual_response = self.app.patch('/v1/plans', expect_errors=True) self.assertEqual(405, actual_response.status_int) + + +class TestPlansItemController(base_api.BaseApiTest): + + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_options(self, rpc_mock): + req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' + params = json.loads(open(req_json_file).read()) + plan_id = str(uuid.uuid4()) + params['id'] = plan_id + rpc_mock.return_value = {'plans': [params]} + url = '/v1/plans/' + plan_id + print(url) + actual_response = self.app.options(url=url, expect_errors=True) + self.assertEqual(204, actual_response.status_int) + self.assertEqual("GET,DELETE", actual_response.headers['Allow']) + + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_httpmethod_notallowed(self, rpc_mock): + req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' + params = json.loads(open(req_json_file).read()) + plan_id = str(uuid.uuid4()) + params['id'] = plan_id + rpc_mock.return_value = {'plans': [params]} + url = '/v1/plans/' + plan_id + actual_response = self.app.put(url=url, expect_errors=True) + self.assertEqual(405, actual_response.status_int) + actual_response = self.app.patch(url=url, expect_errors=True) + self.assertEqual(405, actual_response.status_int) + actual_response = self.app.post(url=url, expect_errors=True) + self.assertEqual(405, actual_response.status_int) + + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_get(self, rpc_mock): + req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' + params = json.loads(open(req_json_file).read()) + plan_id = str(uuid.uuid4()) + params['id'] = plan_id + expected_response = {'plans': [params]} + rpc_mock.return_value = {'plans': [params]} + url = '/v1/plans/' + plan_id + actual_response = self.app.get(url=url, expect_errors=True) + self.assertEqual(200, actual_response.status_int) + self.assertJsonEqual(expected_response, + json.loads(actual_response.body)) + + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_get_non_exist(self, rpc_mock): + rpc_mock.return_value = {'plans': []} + plan_id = str(uuid.uuid4()) + url = '/v1/plans/' + plan_id + actual_response = self.app.get(url=url, expect_errors=True) + self.assertEqual(404, actual_response.status_int) + + @mock.patch('conductor.common.music.messaging.component.RPCClient.call') + def test_index_delete(self, rpc_mock): + req_json_file = './conductor/tests/unit/api/controller/v1/plans.json' + params = json.loads(open(req_json_file).read()) + plan_id = str(uuid.uuid4()) + params['id'] = plan_id + rpc_mock.return_value = {'plans': [params]} + url = '/v1/plans/' + plan_id + actual_response = self.app.delete(url=url, expect_errors=True) + self.assertEqual(204, actual_response.status_int) |