diff --git a/almanach/tests/unit/api/v1/base_api.py b/almanach/tests/unit/api/v1/base_api.py index b8c59ad..19250bb 100644 --- a/almanach/tests/unit/api/v1/base_api.py +++ b/almanach/tests/unit/api/v1/base_api.py @@ -15,6 +15,7 @@ from datetime import datetime import flask from flexmock import flexmock +from mock import mock from oslo_serialization import jsonutils as json from almanach.api.v1 import routes @@ -30,11 +31,11 @@ class BaseApi(base.BaseTestCase): self.prepare_with_successful_authentication() def prepare(self): - self.instance_ctl = flexmock() - self.volume_ctl = flexmock() - self.volume_type_ctl = flexmock() - self.entity_ctl = flexmock() - self.app_ctl = flexmock() + self.instance_ctl = mock.Mock() + self.volume_ctl = mock.Mock() + self.volume_type_ctl = mock.Mock() + self.entity_ctl = mock.Mock() + self.app_ctl = mock.Mock() self.auth_adapter = flexmock() routes.instance_ctl = self.instance_ctl routes.volume_ctl = self.volume_ctl diff --git a/almanach/tests/unit/api/v1/test_api_authentication.py b/almanach/tests/unit/api/v1/test_api_authentication.py index 2f17a77..3bc4966 100644 --- a/almanach/tests/unit/api/v1/test_api_authentication.py +++ b/almanach/tests/unit/api/v1/test_api_authentication.py @@ -12,9 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from hamcrest import assert_that -from hamcrest import equal_to - from almanach.tests.unit.api.v1 import base_api @@ -26,11 +23,11 @@ class TestApiAuthentication(base_api.BaseApi): self.prepare_with_failed_authentication() def test_with_wrong_authentication(self): - self.entity_ctl.should_receive('list_entities').never() query_string = {'start': '2014-01-01 00:00:00.0000', 'end': '2014-02-01 00:00:00.0000'} code, result = self.api_get(url='/project/TENANT_ID/entities', query_string=query_string, headers={'X-Auth-Token': 'wrong token'}) - assert_that(code, equal_to(401)) + self.entity_ctl.list_entities.assert_not_called() + self.assertEqual(code, 401) diff --git a/almanach/tests/unit/api/v1/test_api_entity.py b/almanach/tests/unit/api/v1/test_api_entity.py index 7f43be4..57550c3 100644 --- a/almanach/tests/unit/api/v1/test_api_entity.py +++ b/almanach/tests/unit/api/v1/test_api_entity.py @@ -12,11 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from hamcrest import assert_that -from hamcrest import equal_to -from hamcrest import has_entries -from hamcrest import has_key -from hamcrest import is_ from voluptuous import Invalid from almanach.core import exception @@ -29,21 +24,16 @@ class TestApiEntity(base_api.BaseApi): def test_update_instance_flavor_for_terminated_instance(self): some_new_flavor = 'some_new_flavor' + an_instance = a(instance(). + with_id('INSTANCE_ID'). + with_start(2016, 3, 1, 0, 0, 0). + with_end(2016, 3, 3, 0, 0, 0). + with_flavor(some_new_flavor)) + self.entity_ctl.update_inactive_entity.return_value = an_instance data = dict(flavor=some_new_flavor) start = '2016-03-01 00:00:00.000000' end = '2016-03-03 00:00:00.000000' - self.entity_ctl.should_receive('update_inactive_entity').with_args( - instance_id="INSTANCE_ID", - start=base_api.a_date_matching(start), - end=base_api.a_date_matching(end), - flavor=some_new_flavor, - ).and_return(a(instance(). - with_id('INSTANCE_ID'). - with_start(2016, 3, 1, 0, 0, 0). - with_end(2016, 3, 3, 0, 0, 0). - with_flavor(some_new_flavor))) - code, result = self.api_put( '/entity/instance/INSTANCE_ID', headers={'X-Auth-Token': 'some token value'}, @@ -53,21 +43,24 @@ class TestApiEntity(base_api.BaseApi): }, data=data, ) - assert_that(code, equal_to(200)) - assert_that(result, has_key('entity_id')) - assert_that(result, has_key('flavor')) - assert_that(result['flavor'], is_(some_new_flavor)) + + self.entity_ctl.update_inactive_entity.assert_called_once_with( + instance_id="INSTANCE_ID", + start=base_api.a_date_matching(start), + end=base_api.a_date_matching(end), + flavor=some_new_flavor + ) + self.assertEqual(code, 200) + self.assertIn('entity_id', result) + self.assertIn('flavor', result) + self.assertEqual(result['flavor'], some_new_flavor) def test_update_instance_entity_with_a_new_start_date(self): data = { "start_date": "2014-01-01 00:00:00.0000", } - - self.entity_ctl.should_receive('update_active_instance_entity') \ - .with_args( - instance_id="INSTANCE_ID", - start_date=data["start_date"], - ).and_return(a(instance().with_id('INSTANCE_ID').with_start(2014, 1, 1, 0, 0, 0))) + an_instance = a(instance().with_id('INSTANCE_ID').with_start(2014, 1, 1, 0, 0, 0)) + self.entity_ctl.update_active_instance_entity.return_value = an_instance code, result = self.api_put( '/entity/instance/INSTANCE_ID', @@ -75,38 +68,42 @@ class TestApiEntity(base_api.BaseApi): data=data, ) - assert_that(code, equal_to(200)) - assert_that(result, has_key('entity_id')) - assert_that(result, has_key('start')) - assert_that(result, has_key('end')) - assert_that(result['start'], is_("2014-01-01 00:00:00+00:00")) + self.entity_ctl.update_active_instance_entity.assert_called_once_with( + instance_id="INSTANCE_ID", + start_date=data["start_date"] + ) + self.assertEqual(code, 200) + self.assertIn('entity_id', result) + self.assertIn('start', result) + self.assertIn('end', result) + self.assertEqual(result['start'], "2014-01-01 00:00:00+00:00") def test_update_active_instance_entity_with_bad_payload(self): + self.entity_ctl.update_active_instance_entity.side_effect = ValueError( + 'Expecting object: line 1 column 15 (char 14)' + ) instance_id = 'INSTANCE_ID' data = { 'flavor': 'A_FLAVOR', } - self.entity_ctl.should_receive('update_active_instance_entity') \ - .with_args(instance_id=instance_id, **data) \ - .once() \ - .and_raise(ValueError('Expecting object: line 1 column 15 (char 14)')) - code, result = self.api_put( '/entity/instance/INSTANCE_ID', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries({ - "error": 'Invalid parameter or payload' - })) - assert_that(code, equal_to(400)) + + self.entity_ctl.update_active_instance_entity.assert_called_once_with(instance_id=instance_id, **data) + self.assertIn("error", result) + self.assertEqual(result["error"], 'Invalid parameter or payload') + self.assertEqual(code, 400) def test_update_active_instance_entity_with_wrong_attribute_raise_exception(self): errors = [ Invalid(message="error message1", path=["my_attribute1"]), Invalid(message="error message2", path=["my_attribute2"]), ] + self.entity_ctl.update_active_instance_entity.side_effect = exception.InvalidAttributeException(errors) formatted_errors = { "my_attribute1": "error message1", @@ -118,20 +115,16 @@ class TestApiEntity(base_api.BaseApi): 'flavor': 'A_FLAVOR', } - self.entity_ctl.should_receive('update_active_instance_entity') \ - .with_args(instance_id=instance_id, **data) \ - .once() \ - .and_raise(exception.InvalidAttributeException(errors)) - code, result = self.api_put( '/entity/instance/INSTANCE_ID', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries({ - "error": formatted_errors - })) - assert_that(code, equal_to(400)) + + self.entity_ctl.update_active_instance_entity.assert_called_once_with(instance_id=instance_id, **data) + self.assertIn("error", result) + self.assertEqual(result['error'], formatted_errors) + self.assertEqual(code, 400) def test_entity_head_with_existing_entity(self): entity_id = "entity_id" @@ -140,13 +133,13 @@ class TestApiEntity(base_api.BaseApi): code, result = self.api_head('/entity/{id}'.format(id=entity_id), headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(200)) + self.assertEqual(code, 200) def test_entity_head_with_nonexistent_entity(self): + self.entity_ctl.entity_exists.return_value = False entity_id = "entity_id" - self.entity_ctl.should_receive('entity_exists') \ - .and_return(False) code, result = self.api_head('/entity/{id}'.format(id=entity_id), headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(404)) + self.entity_ctl.entity_exists.assert_called_once_with(entity_id=entity_id) + self.assertEqual(code, 404) diff --git a/almanach/tests/unit/api/v1/test_api_info.py b/almanach/tests/unit/api/v1/test_api_info.py index 7b25169..fd16a04 100644 --- a/almanach/tests/unit/api/v1/test_api_info.py +++ b/almanach/tests/unit/api/v1/test_api_info.py @@ -12,24 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -from hamcrest import assert_that -from hamcrest import equal_to -from hamcrest import has_key - from almanach.tests.unit.api.v1 import base_api class TestApiInfo(base_api.BaseApi): def test_info(self): - self.app_ctl.should_receive('get_application_info').and_return({ - 'info': {'version': '1.0'}, - 'database': {'all_entities': 10, - 'active_entities': 2} - }) + info = {'info': {'version': '1.0'}, 'database': {'all_entities': 10, 'active_entities': 2}} + self.app_ctl.get_application_info.return_value = info code, result = self.api_get('/info') - assert_that(code, equal_to(200)) - assert_that(result, has_key('info')) - assert_that(result['info']['version'], equal_to('1.0')) + self.app_ctl.get_application_info.assert_called_once() + self.assertEqual(code, 200) + self.assertIn('info', result) + self.assertEqual(result['info']['version'], '1.0') diff --git a/almanach/tests/unit/api/v1/test_api_instance.py b/almanach/tests/unit/api/v1/test_api_instance.py index 99d1f9f..66508a2 100644 --- a/almanach/tests/unit/api/v1/test_api_instance.py +++ b/almanach/tests/unit/api/v1/test_api_instance.py @@ -12,12 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from hamcrest import assert_that -from hamcrest import equal_to -from hamcrest import has_entries -from hamcrest import has_key -from hamcrest import has_length - from almanach.core import exception from almanach.tests.unit.api.v1 import base_api from almanach.tests.unit.builders.entity import a @@ -27,11 +21,7 @@ from almanach.tests.unit.builders.entity import instance class TestApiInstance(base_api.BaseApi): def test_get_instances(self): - self.instance_ctl.should_receive('list_instances') \ - .with_args('TENANT_ID', base_api.a_date_matching("2014-01-01 00:00:00.0000"), - base_api.a_date_matching("2014-02-01 00:00:00.0000")) \ - .and_return([a(instance().with_id('123'))]) - + self.instance_ctl.list_instances.return_value = [a(instance().with_id('123'))] code, result = self.api_get('/project/TENANT_ID/instances', query_string={ 'start': '2014-01-01 00:00:00.0000', @@ -39,10 +29,14 @@ class TestApiInstance(base_api.BaseApi): }, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(200)) - assert_that(result, has_length(1)) - assert_that(result[0], has_key('entity_id')) - assert_that(result[0]['entity_id'], equal_to('123')) + self.instance_ctl.list_instances.assert_called_once_with( + 'TENANT_ID', base_api.a_date_matching("2014-01-01 00:00:00.0000"), + base_api.a_date_matching("2014-02-01 00:00:00.0000") + ) + self.assertEqual(code, 200) + self.assertEqual(len(result), 1) + self.assertIn('entity_id', result[0]) + self.assertEqual(result[0]['entity_id'], '123') def test_successful_instance_create(self): data = dict(id="INSTANCE_ID", @@ -53,23 +47,23 @@ class TestApiInstance(base_api.BaseApi): os_distro="A_DISTRIBUTION", os_version="AN_OS_VERSION") - self.instance_ctl.should_receive('create_instance') \ - .with_args(tenant_id="PROJECT_ID", - instance_id=data["id"], - create_date=data["created_at"], - name=data['name'], - flavor=data['flavor'], - image_meta=dict(os_type=data['os_type'], - distro=data['os_distro'], - version=data['os_version']) - ).once() - code, result = self.api_post( '/project/PROJECT_ID/instance', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(code, equal_to(201)) + + self.instance_ctl.create_instance.assert_called_once_with( + tenant_id="PROJECT_ID", + instance_id=data["id"], + create_date=data["created_at"], + name=data['name'], + flavor=data['flavor'], + image_meta=dict(os_type=data['os_type'], + distro=data['os_distro'], + version=data['os_version']) + ) + self.assertEqual(code, 201) def test_instance_create_missing_a_param_returns_bad_request_code(self): data = dict(id="INSTANCE_ID", @@ -79,18 +73,18 @@ class TestApiInstance(base_api.BaseApi): os_type="AN_OS_TYPE", os_version="AN_OS_VERSION") - self.instance_ctl.should_receive('create_instance') \ - .never() - code, result = self.api_post( '/project/PROJECT_ID/instance', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries({"error": "The 'os_distro' param is mandatory for the request you have made."})) - assert_that(code, equal_to(400)) + + self.instance_ctl.create_instance.assert_not_called() + self.assertEqual(result["error"], "The 'os_distro' param is mandatory for the request you have made.") + self.assertEqual(code, 400) def test_instance_create_bad_date_format_returns_bad_request_code(self): + self.instance_ctl.create_instance.side_effect = exception.DateFormatException data = dict(id="INSTANCE_ID", created_at="A_BAD_DATE", name="INSTANCE_NAME", @@ -99,134 +93,135 @@ class TestApiInstance(base_api.BaseApi): os_distro="A_DISTRIBUTION", os_version="AN_OS_VERSION") - self.instance_ctl.should_receive('create_instance') \ - .with_args(tenant_id="PROJECT_ID", - instance_id=data["id"], - create_date=data["created_at"], - flavor=data['flavor'], - image_meta=dict(os_type=data['os_type'], - distro=data['os_distro'], - version=data['os_version']), - name=data['name']) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_post( '/project/PROJECT_ID/instance', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.instance_ctl.create_instance.assert_called_once_with( + tenant_id="PROJECT_ID", + instance_id=data["id"], + create_date=data["created_at"], + flavor=data['flavor'], + image_meta=dict(os_type=data['os_type'], + distro=data['os_distro'], + version=data['os_version']), + name=data['name'] + ) + + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.assertEqual(code, 400) def test_successful_instance_resize(self): data = dict(date="UPDATED_AT", flavor="A_FLAVOR") - self.instance_ctl.should_receive('resize_instance') \ - .with_args(instance_id="INSTANCE_ID", - flavor=data['flavor'], - resize_date=data['date']) \ - .once() - code, result = self.api_put( '/instance/INSTANCE_ID/resize', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(code, equal_to(200)) + + self.instance_ctl.resize_instance.assert_called_once_with( + instance_id="INSTANCE_ID", + flavor=data['flavor'], + resize_date=data['date'] + ) + self.assertEqual(code, 200) def test_successfull_instance_delete(self): data = dict(date="DELETE_DATE") - self.instance_ctl.should_receive('delete_instance') \ - .with_args(instance_id="INSTANCE_ID", - delete_date=data['date']) \ - .once() - code, result = self.api_delete('/instance/INSTANCE_ID', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(202)) + + self.instance_ctl.delete_instance.assert_called_once_with( + instance_id="INSTANCE_ID", + delete_date=data['date'] + ) + self.assertEqual(code, 202) def test_instance_delete_missing_a_param_returns_bad_request_code(self): - self.instance_ctl.should_receive('delete_instance') \ - .never() - code, result = self.api_delete( '/instance/INSTANCE_ID', data=dict(), headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries({"error": "The 'date' param is mandatory for the request you have made."})) - assert_that(code, equal_to(400)) + + self.instance_ctl.delete_instance.assert_not_called() + self.assertIn( + "The 'date' param is mandatory for the request you have made.", + result["error"] + ) + self.assertEqual(code, 400) def test_instance_delete_no_data_bad_request_code(self): - self.instance_ctl.should_receive('delete_instance') \ - .never() - code, result = self.api_delete('/instance/INSTANCE_ID', headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries({"error": "Invalid parameter or payload"})) - assert_that(code, equal_to(400)) + + self.instance_ctl.delete_instance.assert_not_called() + self.assertIn( + "Invalid parameter or payload", + result["error"] + ) + self.assertEqual(code, 400) def test_instance_delete_bad_date_format_returns_bad_request_code(self): data = dict(date="A_BAD_DATE") - - self.instance_ctl.should_receive('delete_instance') \ - .with_args(instance_id="INSTANCE_ID", - delete_date=data['date']) \ - .once() \ - .and_raise(exception.DateFormatException) + self.instance_ctl.delete_instance.side_effect = exception.DateFormatException code, result = self.api_delete('/instance/INSTANCE_ID', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.instance_ctl.delete_instance.assert_called_once_with( + instance_id="INSTANCE_ID", + delete_date=data['date'] + ) + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.assertEqual(code, 400) def test_instance_resize_missing_a_param_returns_bad_request_code(self): data = dict(date="UPDATED_AT") - - self.instance_ctl.should_receive('resize_instance') \ - .never() - code, result = self.api_put( '/instance/INSTANCE_ID/resize', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries({"error": "The 'flavor' param is mandatory for the request you have made."})) - assert_that(code, equal_to(400)) + + self.instance_ctl.resize_instance.assert_not_called() + self.assertIn( + "The 'flavor' param is mandatory for the request you have made.", + result["error"] + ) + self.assertEqual(code, 400) def test_instance_resize_bad_date_format_returns_bad_request_code(self): + self.instance_ctl.resize_instance.side_effect = exception.DateFormatException data = dict(date="A_BAD_DATE", flavor="A_FLAVOR") - - self.instance_ctl.should_receive('resize_instance') \ - .with_args(instance_id="INSTANCE_ID", - flavor=data['flavor'], - resize_date=data['date']) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_put( '/instance/INSTANCE_ID/resize', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.instance_ctl.resize_instance.assert_called_once_with( + instance_id="INSTANCE_ID", + flavor=data['flavor'], + resize_date=data['date'] + ) + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.assertEqual(code, 400) def test_rebuild_instance(self): instance_id = 'INSTANCE_ID' @@ -236,41 +231,41 @@ class TestApiInstance(base_api.BaseApi): 'os_type': 'AN_OS_TYPE', 'rebuild_date': 'UPDATE_DATE', } - self.instance_ctl.should_receive('rebuild_instance') \ - .with_args( - instance_id=instance_id, - rebuild_date=data.get('rebuild_date'), - image_meta=dict(distro=data.get('distro'), - version=data.get('version'), - os_type=data.get('os_type')) - ).once() - code, result = self.api_put( '/instance/INSTANCE_ID/rebuild', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(code, equal_to(200)) + self.instance_ctl.rebuild_instance.assert_called_once_with( + instance_id=instance_id, + rebuild_date=data.get('rebuild_date'), + image_meta=dict(distro=data.get('distro'), + version=data.get('version'), + os_type=data.get('os_type')) + ) + self.assertEqual(code, 200) def test_rebuild_instance_missing_a_param_returns_bad_request_code(self): data = { 'distro': 'A_DISTRIBUTION', 'rebuild_date': 'UPDATE_DATE', } - - self.instance_ctl.should_receive('rebuild_instance') \ - .never() - code, result = self.api_put( '/instance/INSTANCE_ID/rebuild', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries({"error": "The 'version' param is mandatory for the request you have made."})) - assert_that(code, equal_to(400)) + + self.instance_ctl.rebuild_instance.assert_not_called() + self.assertIn( + "The 'version' param is mandatory for the request you have made.", + result["error"] + ) + self.assertEqual(code, 400) def test_rebuild_instance_bad_date_format_returns_bad_request_code(self): + self.instance_ctl.rebuild_instance.side_effect = exception.DateFormatException instance_id = 'INSTANCE_ID' data = { 'distro': 'A_DISTRIBUTION', @@ -278,24 +273,22 @@ class TestApiInstance(base_api.BaseApi): 'os_type': 'AN_OS_TYPE', 'rebuild_date': 'A_BAD_UPDATE_DATE', } - - self.instance_ctl.should_receive('rebuild_instance') \ - .with_args(instance_id=instance_id, - rebuild_date=data.get('rebuild_date'), - image_meta=dict(distro=data.get('distro'), - version=data.get('version'), - os_type=data.get('os_type'))) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_put( '/instance/INSTANCE_ID/rebuild', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) + + self.instance_ctl.rebuild_instance.assert_called_once_with( + instance_id=instance_id, + rebuild_date=data.get('rebuild_date'), + image_meta=dict(distro=data.get('distro'), + version=data.get('version'), + os_type=data.get('os_type')) + ) + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.assertEqual(code, 400) diff --git a/almanach/tests/unit/api/v1/test_api_volume.py b/almanach/tests/unit/api/v1/test_api_volume.py index f6fe54c..202a047 100644 --- a/almanach/tests/unit/api/v1/test_api_volume.py +++ b/almanach/tests/unit/api/v1/test_api_volume.py @@ -12,9 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from hamcrest import assert_that -from hamcrest import equal_to -from hamcrest import has_entries from uuid import uuid4 from almanach.core import exception @@ -31,17 +28,17 @@ class TestApiVolume(base_api.BaseApi): volume_name="VOLUME_NAME", attached_to=["INSTANCE_ID"]) - self.volume_ctl.should_receive('create_volume') \ - .with_args(project_id="PROJECT_ID", - **data) \ - .once() - code, result = self.api_post( '/project/PROJECT_ID/volume', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(code, equal_to(201)) + + self.volume_ctl.create_volume.assert_called_once_with( + project_id="PROJECT_ID", + **data + ) + self.assertEqual(code, 201) def test_volume_create_missing_a_param_returns_bad_request_code(self): data = dict(volume_id="VOLUME_ID", @@ -50,21 +47,21 @@ class TestApiVolume(base_api.BaseApi): volume_name="VOLUME_NAME", attached_to=[]) - self.volume_ctl.should_receive('create_volume') \ - .never() - code, result = self.api_post( '/project/PROJECT_ID/volume', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that( - result, - has_entries({"error": "The 'volume_type' param is mandatory for the request you have made."}) + + self.assertIn( + "The 'volume_type' param is mandatory for the request you have made.", + result["error"] ) - assert_that(code, equal_to(400)) + self.volume_ctl.create_volume.assert_not_called() + self.assertEqual(code, 400) def test_volume_create_bad_date_format_returns_bad_request_code(self): + self.volume_ctl.create_volume.side_effect = exception.DateFormatException data = dict(volume_id="VOLUME_ID", start="A_BAD_DATE", volume_type="VOLUME_TYPE", @@ -72,205 +69,203 @@ class TestApiVolume(base_api.BaseApi): volume_name="VOLUME_NAME", attached_to=["INSTANCE_ID"]) - self.volume_ctl.should_receive('create_volume') \ - .with_args(project_id="PROJECT_ID", - **data) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_post( '/project/PROJECT_ID/volume', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) - def test_successfull_volume_delete(self): + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.volume_ctl.create_volume.assert_called_once_with( + project_id="PROJECT_ID", + **data + ) + self.assertEqual(code, 400) + + def test_successful_volume_delete(self): data = dict(date="DELETE_DATE") - self.volume_ctl.should_receive('delete_volume') \ - .with_args(volume_id="VOLUME_ID", - delete_date=data['date']) \ - .once() - code, result = self.api_delete('/volume/VOLUME_ID', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(202)) + + self.volume_ctl.delete_volume.assert_called_once_with( + volume_id="VOLUME_ID", + delete_date=data['date'] + ) + self.assertEqual(code, 202) def test_volume_delete_missing_a_param_returns_bad_request_code(self): - self.volume_ctl.should_receive('delete_volume') \ - .never() - code, result = self.api_delete('/volume/VOLUME_ID', data=dict(), headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries({"error": "The 'date' param is mandatory for the request you have made."})) - assert_that(code, equal_to(400)) + + self.assertIn( + "The 'date' param is mandatory for the request you have made.", + result["error"] + ) + self.volume_ctl.delete_volume.assert_not_called() + self.assertEqual(code, 400) def test_volume_delete_no_data_bad_request_code(self): - self.volume_ctl.should_receive('delete_volume') \ - .never() - code, result = self.api_delete('/volume/VOLUME_ID', headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries({"error": "Invalid parameter or payload"})) - assert_that(code, equal_to(400)) + + self.assertIn( + "Invalid parameter or payload", + result["error"] + ) + self.volume_ctl.delete_volume.assert_not_called() + self.assertEqual(code, 400) def test_volume_delete_bad_date_format_returns_bad_request_code(self): + self.volume_ctl.delete_volume.side_effect = exception.DateFormatException data = dict(date="A_BAD_DATE") - self.volume_ctl.should_receive('delete_volume') \ - .with_args(volume_id="VOLUME_ID", - delete_date=data['date']) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_delete('/volume/VOLUME_ID', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.volume_ctl.delete_volume.assert_called_once_with( + volume_id="VOLUME_ID", + delete_date=data['date'] + ) + self.assertEqual(code, 400) def test_successful_volume_resize(self): data = dict(date="UPDATED_AT", size="NEW_SIZE") - self.volume_ctl.should_receive('resize_volume') \ - .with_args(volume_id="VOLUME_ID", - size=data['size'], - update_date=data['date']) \ - .once() - code, result = self.api_put('/volume/VOLUME_ID/resize', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(200)) + + self.volume_ctl.resize_volume.assert_called_once_with( + volume_id="VOLUME_ID", + size=data['size'], + update_date=data['date'] + ) + self.assertEqual(code, 200) def test_volume_resize_missing_a_param_returns_bad_request_code(self): data = dict(date="A_DATE") - self.volume_ctl.should_receive('resize_volume') \ - .never() - code, result = self.api_put('/volume/VOLUME_ID/resize', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries({"error": "The 'size' param is mandatory for the request you have made."})) - assert_that(code, equal_to(400)) + + self.assertIn( + "The 'size' param is mandatory for the request you have made.", + result["error"] + ) + self.volume_ctl.resize_volume.assert_not_called() + self.assertEqual(code, 400) def test_volume_resize_bad_date_format_returns_bad_request_code(self): + self.volume_ctl.resize_volume.side_effect = exception.DateFormatException data = dict(date="BAD_DATE", size="NEW_SIZE") - self.volume_ctl.should_receive('resize_volume') \ - .with_args(volume_id="VOLUME_ID", - size=data['size'], - update_date=data['date']) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_put('/volume/VOLUME_ID/resize', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.volume_ctl.resize_volume.assert_called_once_with( + volume_id="VOLUME_ID", + size=data['size'], + update_date=data['date'] + ) + self.assertEqual(code, 400) def test_successful_volume_attach(self): data = dict(date="UPDATED_AT", attachments=[str(uuid4())]) - self.volume_ctl.should_receive('attach_volume') \ - .with_args(volume_id="VOLUME_ID", - attachments=data['attachments'], - date=data['date']) \ - .once() - code, result = self.api_put('/volume/VOLUME_ID/attach', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(200)) + + self.volume_ctl.attach_volume.assert_called_once_with( + volume_id="VOLUME_ID", + attachments=data['attachments'], + date=data['date'] + ) + self.assertEqual(code, 200) def test_volume_attach_missing_a_param_returns_bad_request_code(self): data = dict(date="A_DATE") - self.volume_ctl.should_receive('attach_volume') \ - .never() - code, result = self.api_put( '/volume/VOLUME_ID/attach', data=data, headers={'X-Auth-Token': 'some token value'} ) - assert_that( - result, - has_entries({"error": "The 'attachments' param is mandatory for the request you have made."}) + + self.assertIn( + "The 'attachments' param is mandatory for the request you have made.", + result["error"] ) - assert_that(code, equal_to(400)) + self.volume_ctl.attach_volume.assert_not_called() + self.assertEqual(code, 400) def test_volume_attach_bad_date_format_returns_bad_request_code(self): + self.volume_ctl.attach_volume.side_effect = exception.DateFormatException data = dict(date="A_BAD_DATE", attachments=[str(uuid4())]) - self.volume_ctl.should_receive('attach_volume') \ - .with_args(volume_id="VOLUME_ID", - attachments=data['attachments'], - date=data['date']) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_put('/volume/VOLUME_ID/attach', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.volume_ctl.attach_volume.assert_called_once_with( + volume_id="VOLUME_ID", + attachments=data['attachments'], + date=data['date'] + ) + self.assertEqual(code, 400) def test_successful_volume_detach(self): data = dict(date="UPDATED_AT", attachments=[str(uuid4())]) - self.volume_ctl.should_receive('detach_volume') \ - .with_args(volume_id="VOLUME_ID", - attachments=data['attachments'], - date=data['date']) \ - .once() - code, result = self.api_put('/volume/VOLUME_ID/detach', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(200)) + + self.volume_ctl.detach_volume.assert_called_once_with( + volume_id="VOLUME_ID", + attachments=data['attachments'], + date=data['date'] + ) + self.assertEqual(code, 200) def test_volume_detach_missing_a_param_returns_bad_request_code(self): data = dict(date="A_DATE") - self.volume_ctl.should_receive('detach_volume') \ - .never() - code, result = self.api_put('/volume/VOLUME_ID/detach', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that( - result, - has_entries({"error": "The 'attachments' param is mandatory for the request you have made."}) + + self.assertIn( + "The 'attachments' param is mandatory for the request you have made.", + result["error"] ) - assert_that(code, equal_to(400)) + self.volume_ctl.detach_volume.assert_not_called() + self.assertEqual(code, 400) def test_volume_detach_bad_date_format_returns_bad_request_code(self): + self.volume_ctl.detach_volume.side_effect = exception.DateFormatException data = dict(date="A_BAD_DATE", attachments=[str(uuid4())]) - self.volume_ctl.should_receive('detach_volume') \ - .with_args(volume_id="VOLUME_ID", - attachments=data['attachments'], - date=data['date']) \ - .once() \ - .and_raise(exception.DateFormatException) - code, result = self.api_put('/volume/VOLUME_ID/detach', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(result, has_entries( - { - "error": "The provided date has an invalid format. " - "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z" - } - )) - assert_that(code, equal_to(400)) + + self.assertIn( + "The provided date has an invalid format. " + "Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z", + result["error"] + ) + self.volume_ctl.detach_volume.assert_called_once_with( + volume_id="VOLUME_ID", + attachments=data['attachments'], + date=data['date'] + ) + self.assertEqual(code, 400) diff --git a/almanach/tests/unit/api/v1/test_api_volume_type.py b/almanach/tests/unit/api/v1/test_api_volume_type.py index 1157861..c98605c 100644 --- a/almanach/tests/unit/api/v1/test_api_volume_type.py +++ b/almanach/tests/unit/api/v1/test_api_volume_type.py @@ -12,13 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from hamcrest import assert_that -from hamcrest import equal_to -from hamcrest import has_entries -from hamcrest import has_entry -from hamcrest import has_key -from hamcrest import has_length - from almanach.core import exception from almanach.tests.unit.api.v1 import base_api from almanach.tests.unit.builders.entity import a @@ -28,16 +21,17 @@ from almanach.tests.unit.builders.entity import volume_type class TestApiVolumeType(base_api.BaseApi): def test_get_volume_types(self): - self.volume_type_ctl.should_receive('list_volume_types') \ - .and_return([a(volume_type().with_volume_type_name('some_volume_type_name'))]) \ - .once() + self.volume_type_ctl.list_volume_types.return_value = [ + a(volume_type().with_volume_type_name('some_volume_type_name')) + ] code, result = self.api_get('/volume_types', headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(200)) - assert_that(result, has_length(1)) - assert_that(result[0], has_key('volume_type_name')) - assert_that(result[0]['volume_type_name'], equal_to('some_volume_type_name')) + self.volume_type_ctl.list_volume_types.assert_called_once() + self.assertEqual(code, 200) + self.assertEqual(len(result), 1) + self.assertIn('volume_type_name', result[0]) + self.assertEqual(result[0]['volume_type_name'], 'some_volume_type_name') def test_successful_volume_type_create(self): data = dict( @@ -45,40 +39,34 @@ class TestApiVolumeType(base_api.BaseApi): type_name="A_VOLUME_TYPE_NAME" ) - self.volume_type_ctl.should_receive('create_volume_type') \ - .with_args( - volume_type_id=data['type_id'], - volume_type_name=data['type_name']) \ - .once() - code, result = self.api_post('/volume_type', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(201)) + + self.volume_type_ctl.create_volume_type.assert_called_once_with( + volume_type_id=data['type_id'], + volume_type_name=data['type_name'] + ) + self.assertEqual(code, 201) def test_volume_type_create_missing_a_param_returns_bad_request_code(self): data = dict(type_name="A_VOLUME_TYPE_NAME") - self.volume_type_ctl.should_receive('create_volume_type') \ - .never() - code, result = self.api_post('/volume_type', data=data, headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(400)) - assert_that(result, has_entries({"error": "The 'type_id' param is mandatory for the request you have made."})) + + self.volume_type_ctl.create_volume_type.assert_not_called() + self.assertEqual(result["error"], "The 'type_id' param is mandatory for the request you have made.") + self.assertEqual(code, 400) def test_volume_type_delete_with_authentication(self): - self.volume_type_ctl.should_receive('delete_volume_type') \ - .with_args('A_VOLUME_TYPE_ID') \ - .once() - code, result = self.api_delete('/volume_type/A_VOLUME_TYPE_ID', headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(202)) + + self.volume_type_ctl.delete_volume_type.assert_called_once_with('A_VOLUME_TYPE_ID') + self.assertEqual(code, 202) def test_volume_type_delete_not_in_database(self): - self.volume_type_ctl.should_receive('delete_volume_type') \ - .with_args('A_VOLUME_TYPE_ID') \ - .and_raise(exception.AlmanachException("An exception occurred")) \ - .once() + self.volume_type_ctl.delete_volume_type.side_effect = exception.AlmanachException("An exception occurred") code, result = self.api_delete('/volume_type/A_VOLUME_TYPE_ID', headers={'X-Auth-Token': 'some token value'}) - assert_that(code, equal_to(500)) - assert_that(result, has_entry("error", "An exception occurred")) + self.volume_type_ctl.delete_volume_type.assert_called_once_with('A_VOLUME_TYPE_ID') + self.assertIn("An exception occurred", result["error"]) + self.assertEqual(code, 500)