diff --git a/almanach/api/v1/routes.py b/almanach/api/v1/routes.py index 4a5073f..7f46e2c 100644 --- a/almanach/api/v1/routes.py +++ b/almanach/api/v1/routes.py @@ -59,7 +59,7 @@ def to_json(api_call): except exception.InvalidAttributeException as e: LOG.warning(e.get_error_message()) return send_response({"error": e.get_error_message()}, 400) - except (exception.AlmanachEntityNotFoundException, exception.VolumeTypeNotFoundException) as e: + except (exception.EntityNotFoundException, exception.VolumeTypeNotFoundException) as e: LOG.warning(e.message) return send_response({"error": e.message}, 404) except exception.AlmanachException as e: diff --git a/almanach/core/controllers/base_controller.py b/almanach/core/controllers/base_controller.py index 3d05c5c..a198c46 100644 --- a/almanach/core/controllers/base_controller.py +++ b/almanach/core/controllers/base_controller.py @@ -45,9 +45,9 @@ class BaseController(object): entity = self.database_adapter.get_active_entity(entity_id) if entity and entity.last_event > date: return True - except KeyError: + except exception.EntityNotFoundException: pass - except NotImplementedError: + except exception.EntityTypeNotSupportedException: pass return False diff --git a/almanach/core/controllers/entity_controller.py b/almanach/core/controllers/entity_controller.py index 062a7be..2b56665 100644 --- a/almanach/core/controllers/entity_controller.py +++ b/almanach/core/controllers/entity_controller.py @@ -32,34 +32,34 @@ class EntityController(base_controller.BaseController): self._update_instance_object(instance, **kwargs) self.database_adapter.update_active_entity(instance) return instance - except KeyError as e: + except exception.EntityNotFoundException as e: LOG.error("Instance %s is not in the database yet.", instance_id) raise e def update_inactive_entity(self, instance_id, start, end, **kwargs): - inactive_entities = self.database_adapter.list_entities_by_id(instance_id, start, end) + inactive_entities = self.database_adapter.get_all_entities_by_id_and_date(instance_id, start, end) if len(inactive_entities) > 1: raise exception.MultipleEntitiesMatchingQueryException() if len(inactive_entities) < 1: - raise exception.AlmanachEntityNotFoundException( + raise exception.EntityNotFoundException( "InstanceId: {0} Not Found with start".format(instance_id)) entity = inactive_entities[0] entity_update = self._transform_attribute_to_match_entity_attribute(**kwargs) self.database_adapter.update_closed_entity(entity=entity, data=entity_update) start = entity_update.get('start') or start end = entity_update.get('end') or end - return self.database_adapter.list_entities_by_id(instance_id, start, end)[0] + return self.database_adapter.get_all_entities_by_id_and_date(instance_id, start, end)[0] def entity_exists(self, entity_id): return self.database_adapter.count_entity_entries(entity_id=entity_id) >= 1 def get_all_entities_by_id(self, entity_id): if not self.entity_exists(entity_id=entity_id): - raise exception.AlmanachEntityNotFoundException("Entity not found") + raise exception.EntityNotFoundException("Entity not found") return self.database_adapter.get_all_entities_by_id(entity_id=entity_id) def list_entities(self, project_id, start, end): - return self.database_adapter.list_entities(project_id, start, end) + return self.database_adapter.get_all_entities_by_project(project_id, start, end) def _update_instance_object(self, instance, **kwargs): for key, value in self._transform_attribute_to_match_entity_attribute(**kwargs).items(): diff --git a/almanach/core/controllers/instance_controller.py b/almanach/core/controllers/instance_controller.py index 323ae14..f0c6d9b 100644 --- a/almanach/core/controllers/instance_controller.py +++ b/almanach/core/controllers/instance_controller.py @@ -46,7 +46,7 @@ class InstanceController(base_controller.BaseController): def delete_instance(self, instance_id, delete_date): if not self.database_adapter.has_active_entity(instance_id): - raise exception.AlmanachEntityNotFoundException( + raise exception.EntityNotFoundException( "InstanceId: {0} Not Found".format(instance_id)) delete_date = self._validate_and_parse_date(delete_date) @@ -65,7 +65,7 @@ class InstanceController(base_controller.BaseController): instance.end = None instance.last_event = resize_date self.database_adapter.insert_entity(instance) - except KeyError as e: + except exception.EntityNotFoundException as e: LOG.error("Trying to resize an instance with id '%s' not in the database yet.", instance_id) raise e @@ -87,7 +87,7 @@ class InstanceController(base_controller.BaseController): self.database_adapter.insert_entity(instance) def list_instances(self, project_id, start, end): - return self.database_adapter.list_entities(project_id, start, end, model.Instance.TYPE) + return self.database_adapter.get_all_entities_by_project(project_id, start, end, model.Instance.TYPE) def _filter_metadata_with_whitelist(self, metadata): return {key: value for key, value in metadata.items() if key in self.metadata_whitelist} diff --git a/almanach/core/controllers/volume_controller.py b/almanach/core/controllers/volume_controller.py index 7ff8bc9..c79be6a 100644 --- a/almanach/core/controllers/volume_controller.py +++ b/almanach/core/controllers/volume_controller.py @@ -16,6 +16,7 @@ from datetime import timedelta from oslo_log import log from almanach.core.controllers import base_controller +from almanach.core import exception from almanach.core import model LOG = log.getLogger(__name__) @@ -28,7 +29,7 @@ class VolumeController(base_controller.BaseController): self.volume_existence_threshold = timedelta(0, config.resources.volume_existence_threshold) def list_volumes(self, project_id, start, end): - return self.database_adapter.list_entities(project_id, start, end, model.Volume.TYPE) + return self.database_adapter.get_all_entities_by_project(project_id, start, end, model.Volume.TYPE) def create_volume(self, volume_id, project_id, start, volume_type, size, volume_name, attached_to=None): start = self._validate_and_parse_date(start) @@ -47,7 +48,7 @@ class VolumeController(base_controller.BaseController): LOG.info("volume %s detached on %s", volume_id, date) try: self._volume_detach_instance(volume_id, date, attachments) - except KeyError as e: + except exception.EntityNotFoundException as e: LOG.error("Trying to detach a volume with id '%s' not in the database yet.", volume_id) raise e @@ -56,7 +57,7 @@ class VolumeController(base_controller.BaseController): LOG.info("Volume %s attached to %s on %s", volume_id, attachments, date) try: self._volume_attach_instance(volume_id, date, attachments) - except KeyError as e: + except exception.EntityNotFoundException as e: LOG.error("Trying to attach a volume with id '%s' not in the database yet.", volume_id) raise e @@ -67,7 +68,7 @@ class VolumeController(base_controller.BaseController): LOG.info("volume %s renamed from %s to %s", volume_id, volume.name, volume_name) volume.name = volume_name self.database_adapter.update_active_entity(volume) - except KeyError: + except exception.EntityNotFoundException: LOG.error("Trying to update a volume with id '%s' not in the database yet.", volume_id) def resize_volume(self, volume_id, size, update_date): @@ -84,7 +85,7 @@ class VolumeController(base_controller.BaseController): volume.end = None volume.last_event = update_date self.database_adapter.insert_entity(volume) - except KeyError as e: + except exception.EntityNotFoundException as e: LOG.error("Trying to update a volume with id '%s' not in the database yet.", volume_id) raise e @@ -98,7 +99,7 @@ class VolumeController(base_controller.BaseController): self.database_adapter.delete_active_entity(volume_id) return self.database_adapter.close_active_entity(volume_id, delete_date) - except KeyError as e: + except exception.EntityNotFoundException as e: LOG.error("Trying to delete a volume with id '%s' not in the database yet.", volume_id) raise e diff --git a/almanach/core/exception.py b/almanach/core/exception.py index d8c44ae..b006297 100644 --- a/almanach/core/exception.py +++ b/almanach/core/exception.py @@ -18,12 +18,20 @@ class AlmanachException(Exception): self.message = message -class AlmanachEntityNotFoundException(AlmanachException): +class EntityNotFoundException(AlmanachException): def __init__(self, message=None): if not message: message = "Entity not found" - super(AlmanachEntityNotFoundException, self).__init__(message) + super(EntityNotFoundException, self).__init__(message) + + +class EntityTypeNotSupportedException(AlmanachException): + def __init__(self, message=None): + if not message: + message = "Entity type not supported" + + super(EntityTypeNotSupportedException, self).__init__(message) class AuthenticationFailureException(AlmanachException): @@ -60,9 +68,11 @@ class InvalidAttributeException(AlmanachException): class VolumeTypeNotFoundException(AlmanachException): - def __init__(self, volume_type_id, message=None): - if not message: - message = "Unable to find volume_type id '{volume_type_id}'".format(volume_type_id=volume_type_id) + def __init__(self, volume_type_id=None, message=None): + if not message and volume_type_id: + message = 'Unable to find volume_type_id "{}"'.format(volume_type_id) + elif not message: + message = 'Unable to find volume_type_id' super(VolumeTypeNotFoundException, self).__init__(message) diff --git a/almanach/core/model.py b/almanach/core/model.py index b876755..b0553bf 100644 --- a/almanach/core/model.py +++ b/almanach/core/model.py @@ -14,6 +14,8 @@ import six +from almanach.core import exception + class Entity(object): def __init__(self, entity_id, project_id, start, end, last_event, name, entity_type): @@ -120,7 +122,8 @@ def build_entity_from_dict(entity_dict): return Instance(**entity_dict) elif entity_dict.get("entity_type") == Volume.TYPE: return Volume(**entity_dict) - raise NotImplementedError("unsupported entity type: '%s'" % entity_dict.get("entity_type")) + raise exception.EntityTypeNotSupportedException( + 'Unsupported entity type: "{}"'.format(entity_dict.get("entity_type"))) def todict(obj): diff --git a/almanach/storage/drivers/base_driver.py b/almanach/storage/drivers/base_driver.py index 94a024b..60a6389 100644 --- a/almanach/storage/drivers/base_driver.py +++ b/almanach/storage/drivers/base_driver.py @@ -47,7 +47,7 @@ class BaseDriver(object): pass @abc.abstractmethod - def list_entities(self, project_id, start, end, entity_type=None): + def get_all_entities_by_project(self, project_id, start, end, entity_type=None): pass @abc.abstractmethod @@ -55,7 +55,7 @@ class BaseDriver(object): pass @abc.abstractmethod - def list_entities_by_id(self, entity_id, start, end): + def get_all_entities_by_id_and_date(self, entity_id, start, end): pass @abc.abstractmethod diff --git a/almanach/storage/drivers/mongodb_driver.py b/almanach/storage/drivers/mongodb_driver.py index 26b2ac9..321a8a1 100644 --- a/almanach/storage/drivers/mongodb_driver.py +++ b/almanach/storage/drivers/mongodb_driver.py @@ -24,6 +24,7 @@ LOG = log.getLogger(__name__) class MongoDbDriver(base_driver.BaseDriver): + def __init__(self, config, db=None): super(MongoDbDriver, self).__init__(config) self.db = db @@ -33,12 +34,6 @@ class MongoDbDriver(base_driver.BaseDriver): connection_options = pymongo.uri_parser.parse_uri(self.config.database.connection_url) self.db = connection[connection_options['database']] - def get_active_entity(self, entity_id): - entity = self._get_one_entity_from_db({"entity_id": entity_id, "end": None}) - if not entity: - raise KeyError("Unable to find entity id %s" % entity_id) - return build_entity_from_dict(entity) - def count_entities(self): return self.db.entity.count() @@ -51,37 +46,66 @@ class MongoDbDriver(base_driver.BaseDriver): def has_active_entity(self, entity_id): return self.db.entity.find({"entity_id": entity_id, "end": None}).count() == 1 - def list_entities(self, project_id, start, end, entity_type=None): - args = {"project_id": project_id, "start": {"$lte": end}, "$or": [{"end": None}, {"end": {"$gte": start}}]} + def get_active_entity(self, entity_id): + entity = self.db.entity.find_one({"entity_id": entity_id, "end": None}, {"_id": 0}) + if not entity: + raise exception.EntityNotFoundException("Entity {} not found".format(entity_id)) + return build_entity_from_dict(entity) + + def get_all_entities_by_project(self, project_id, start, end, entity_type=None): + args = { + "project_id": project_id, + "start": { + "$lte": end + }, + "$or": [{"end": None}, {"end": {"$gte": start}}] + } + if entity_type: args["entity_type"] = entity_type - entities = self._get_entities_from_db(args) + + entities = list(self.db.entity.find(args, {"_id": 0})) return [build_entity_from_dict(entity) for entity in entities] def get_all_entities_by_id(self, entity_id): entities = self.db.entity.find({"entity_id": entity_id}, {"_id": 0}) return [build_entity_from_dict(entity) for entity in entities] - def list_entities_by_id(self, entity_id, start, end): - entities = self.db.entity.find({"entity_id": entity_id, - "start": {"$gte": start}, - "$and": [ - {"end": {"$ne": None}}, - {"end": {"$lte": end}} - ] - }, {"_id": 0}) + def get_all_entities_by_id_and_date(self, entity_id, start, end): + entities = self.db.entity.find({ + "entity_id": entity_id, + "start": {"$gte": start}, + "$and": [ + {"end": {"$ne": None}}, + {"end": {"$lte": end}} + ] + }, {"_id": 0}) return [build_entity_from_dict(entity) for entity in entities] + def close_active_entity(self, entity_id, end): + self.db.entity.update({"entity_id": entity_id, "end": None}, {"$set": {"end": end, "last_event": end}}) + + def insert_entity(self, entity): + self.db.entity.insert(entity.as_dict()) + + def update_active_entity(self, entity): + self.db.entity.update({"entity_id": entity.entity_id, "end": None}, {"$set": entity.as_dict()}) + def update_closed_entity(self, entity, data): self.db.entity.update({"entity_id": entity.entity_id, "start": entity.start, "end": entity.end}, {"$set": data}) - def insert_entity(self, entity): - self._insert_entity(entity.as_dict()) + def delete_active_entity(self, entity_id): + self.db.entity.remove({"entity_id": entity_id, "end": None}) def insert_volume_type(self, volume_type): self.db.volume_type.insert(volume_type.__dict__) + def list_volume_types(self): + volume_types = self.db.volume_type.find() + return [model.VolumeType(volume_type_id=volume_type["volume_type_id"], + volume_type_name=volume_type["volume_type_name"]) for volume_type in volume_types] + def get_volume_type(self, volume_type_id): volume_type = self.db.volume_type.find_one({"volume_type_id": volume_type_id}) if not volume_type: @@ -94,31 +118,5 @@ class MongoDbDriver(base_driver.BaseDriver): if volume_type_id is None: raise exception.AlmanachException("Trying to delete all volume types which is not permitted.") returned_value = self.db.volume_type.remove({"volume_type_id": volume_type_id}) - if returned_value['n'] == 1: - LOG.info("Deleted volume type with id '%s' successfully.", volume_type_id) - else: - raise exception.AlmanachException( - "Volume type with id {} doesn't exist in the database.".format(volume_type_id)) - - def list_volume_types(self): - volume_types = self.db.volume_type.find() - return [model.VolumeType(volume_type_id=volume_type["volume_type_id"], - volume_type_name=volume_type["volume_type_name"]) for volume_type in volume_types] - - def close_active_entity(self, entity_id, end): - self.db.entity.update({"entity_id": entity_id, "end": None}, {"$set": {"end": end, "last_event": end}}) - - def update_active_entity(self, entity): - self.db.entity.update({"entity_id": entity.entity_id, "end": None}, {"$set": entity.as_dict()}) - - def delete_active_entity(self, entity_id): - self.db.entity.remove({"entity_id": entity_id, "end": None}) - - def _insert_entity(self, entity): - self.db.entity.insert(entity) - - def _get_entities_from_db(self, args): - return list(self.db.entity.find(args, {"_id": 0})) - - def _get_one_entity_from_db(self, args): - return self.db.entity.find_one(args, {"_id": 0}) + if returned_value['n'] != 1: + raise exception.VolumeTypeNotFoundException(volume_type_id) diff --git a/almanach/tests/unit/core/controllers/test_entity_controller.py b/almanach/tests/unit/core/controllers/test_entity_controller.py index 54f672a..e0e732d 100644 --- a/almanach/tests/unit/core/controllers/test_entity_controller.py +++ b/almanach/tests/unit/core/controllers/test_entity_controller.py @@ -114,7 +114,7 @@ class EntityControllerTest(base.BaseTestCase): fake_instance1 = a(instance().with_start(2016, 3, 1, 0, 0, 0).with_end(2016, 3, 2, 0, 0, 0)) (flexmock(self.database_adapter) - .should_receive("list_entities_by_id") + .should_receive("get_all_entities_by_id_and_date") .with_args(fake_instance1.entity_id, start, end) .and_return([fake_instance1]) .twice()) @@ -135,7 +135,7 @@ class EntityControllerTest(base.BaseTestCase): fake_instances = [a(instance()), a(instance())] (flexmock(self.database_adapter) - .should_receive("list_entities_by_id") + .should_receive("get_all_entities_by_id_and_date") .with_args(fake_instances[0].entity_id, fake_instances[0].start, fake_instances[0].end) .and_return(fake_instances) .once()) @@ -152,7 +152,7 @@ class EntityControllerTest(base.BaseTestCase): fake_instances = a(instance()) (flexmock(self.database_adapter) - .should_receive("list_entities_by_id") + .should_receive("get_all_entities_by_id_and_date") .with_args(fake_instances.entity_id, fake_instances.start, fake_instances.end) .and_return([]) .once()) @@ -162,12 +162,12 @@ class EntityControllerTest(base.BaseTestCase): start=fake_instances.start, end=fake_instances.end, flavor=fake_instances.flavor), - raises(exception.AlmanachEntityNotFoundException) + raises(exception.EntityNotFoundException) ) def test_list_entities(self): (flexmock(self.database_adapter) - .should_receive("list_entities") + .should_receive("get_all_entities_by_project") .with_args("project_id", "start", "end") .and_return(["volume2", "volume3", "instance1"])) @@ -219,7 +219,7 @@ class EntityControllerTest(base.BaseTestCase): assert_that( calling(self.controller.get_all_entities_by_id).with_args(entity_id), - raises(exception.AlmanachEntityNotFoundException) + raises(exception.EntityNotFoundException) ) def test_instance_updated_wrong_attributes_raises_exception(self): diff --git a/almanach/tests/unit/core/controllers/test_instance_controller.py b/almanach/tests/unit/core/controllers/test_instance_controller.py index 2b9e2da..19f16c4 100644 --- a/almanach/tests/unit/core/controllers/test_instance_controller.py +++ b/almanach/tests/unit/core/controllers/test_instance_controller.py @@ -39,7 +39,7 @@ class InstanceControllerTest(base.BaseTestCase): (flexmock(self.database_adapter) .should_receive("get_active_entity") .with_args(fake_instance.entity_id) - .and_raise(KeyError) + .and_raise(exception.EntityNotFoundException) .once()) (flexmock(self.database_adapter) @@ -96,7 +96,7 @@ class InstanceControllerTest(base.BaseTestCase): (flexmock(self.database_adapter) .should_receive("get_active_entity") .with_args(fake_instance.entity_id) - .and_raise(NotImplementedError) # The db adapter found garbage in the database, we will ignore this entry + .and_raise(exception.EntityTypeNotSupportedException) .once()) (flexmock(self.database_adapter) @@ -128,13 +128,13 @@ class InstanceControllerTest(base.BaseTestCase): .and_return(False) .once()) - self.assertRaises(exception.AlmanachEntityNotFoundException, + self.assertRaises(exception.EntityNotFoundException, self.controller.delete_instance, "id1", "2015-10-21T16:25:00.000000Z") def test_list_instances(self): (flexmock(self.database_adapter) - .should_receive("list_entities") + .should_receive("get_all_entities_by_project") .with_args("project_id", "start", "end", model.Instance.TYPE) .and_return(["instance1", "instance2"]) .once()) diff --git a/almanach/tests/unit/core/controllers/test_volume_controller.py b/almanach/tests/unit/core/controllers/test_volume_controller.py index dcbaa46..cd1652e 100644 --- a/almanach/tests/unit/core/controllers/test_volume_controller.py +++ b/almanach/tests/unit/core/controllers/test_volume_controller.py @@ -106,7 +106,7 @@ class VolumeControllerTest(base.BaseTestCase): def test_list_volumes(self): (flexmock(self.database_adapter) - .should_receive("list_entities") + .should_receive("get_all_entities_by_project") .with_args("project_id", "start", "end", model.Volume.TYPE) .and_return(["volume2", "volume3"])) @@ -193,7 +193,7 @@ class VolumeControllerTest(base.BaseTestCase): (flexmock(self.database_adapter) .should_receive("get_volume_type") .with_args(some_volume_type.volume_type_id) - .and_raise(KeyError) + .and_raise(exception.VolumeTypeNotFoundException) .once()) some_volume = a(volume() @@ -210,7 +210,7 @@ class VolumeControllerTest(base.BaseTestCase): .and_return(None) .once()) - self.assertRaises(KeyError, self.controller.create_volume, + self.assertRaises(exception.VolumeTypeNotFoundException, self.controller.create_volume, some_volume.entity_id, some_volume.project_id, some_volume.start, some_volume_type.volume_type_id, some_volume.size, some_volume.name, some_volume.attached_to) diff --git a/almanach/tests/unit/storage/drivers/test_mongodb_driver.py b/almanach/tests/unit/storage/drivers/test_mongodb_driver.py index b55e594..081378f 100644 --- a/almanach/tests/unit/storage/drivers/test_mongodb_driver.py +++ b/almanach/tests/unit/storage/drivers/test_mongodb_driver.py @@ -13,14 +13,10 @@ # limitations under the License. from datetime import datetime - -import mongomock -import pymongo -import pytz - -from flexmock import flexmock from hamcrest import assert_that from hamcrest import contains_inanyorder +import mongomock +import pytz from almanach.core import exception from almanach.core import model @@ -33,12 +29,12 @@ from almanach.tests.unit.builder import volume_type class MongoDbDriverTest(base.BaseTestCase): + def setUp(self): super(MongoDbDriverTest, self).setUp() mongo_connection = mongomock.Connection() self.db = mongo_connection['almanach'] self.adapter = mongodb_driver.MongoDbDriver(self.config, self.db) - flexmock(pymongo.MongoClient).new_instances(mongo_connection) def test_insert_instance(self): fake_instance = a(instance()) @@ -80,28 +76,40 @@ class MongoDbDriverTest(base.BaseTestCase): self.assert_entities_metadata_have_been_sanitize([entity]) def test_get_instance_entity_will_not_found(self): - self.assertRaises(KeyError, + self.assertRaises(exception.EntityNotFoundException, self.adapter.get_active_entity, "will_not_found") def test_get_instance_entity_with_unknown_type(self): fake_entity = a(instance()) - fake_entity.entity_type = "will_raise_excepion" + fake_entity.entity_type = "will_raise_exception" self.db.entity.insert(model.todict(fake_entity)) - self.assertRaises(NotImplementedError, + self.assertRaises(exception.EntityTypeNotSupportedException, self.adapter.get_active_entity, fake_entity.entity_id) def test_count_entities(self): fake_active_entities = [ - a(volume().with_id("id2").with_start(2014, 1, 1, 1, 0, 0).with_no_end()), - a(instance().with_id("id3").with_start(2014, 1, 1, 8, 0, 0).with_no_end()), + a(volume() + .with_id("id2") + .with_start(2014, 1, 1, 1, 0, 0) + .with_no_end()), + a(instance() + .with_id("id3") + .with_start(2014, 1, 1, 8, 0, 0) + .with_no_end()), ] fake_inactive_entities = [ - a(instance().with_id("id1").with_start(2014, 1, 1, 7, 0, 0).with_end(2014, 1, 1, 8, 0, 0)), - a(volume().with_id("id2").with_start(2014, 1, 1, 1, 0, 0).with_end(2014, 1, 1, 8, 0, 0)), + a(instance() + .with_id("id1") + .with_start(2014, 1, 1, 7, 0, 0) + .with_end(2014, 1, 1, 8, 0, 0)), + a(volume() + .with_id("id2") + .with_start(2014, 1, 1, 1, 0, 0) + .with_end(2014, 1, 1, 8, 0, 0)), ] all_entities = fake_active_entities + fake_inactive_entities @@ -125,23 +133,50 @@ class MongoDbDriverTest(base.BaseTestCase): def test_list_instances(self): fake_instances = [ - a(instance().with_id("id1").with_start(2014, 1, 1, 7, 0, 0).with_end( - 2014, 1, 1, 8, 0, 0).with_project_id("project_id").with_metadata({})), - a(instance().with_id("id2").with_start(2014, 1, 1, 1, 0, - 0).with_no_end().with_project_id("project_id").with_metadata({})), - a(instance().with_id("id3").with_start(2014, 1, 1, 8, 0, - 0).with_no_end().with_project_id("project_id").with_metadata({})), + a(instance() + .with_id("id1") + .with_start(2014, 1, 1, 7, 0, 0) + .with_end(2014, 1, 1, 8, 0, 0) + .with_project_id("project_id") + .with_metadata({})), + a(instance() + .with_id("id2") + .with_start(2014, 1, 1, 1, 0, 0) + .with_no_end() + .with_project_id("project_id") + .with_metadata({})), + a(instance() + .with_id("id3") + .with_start(2014, 1, 1, 8, 0, 0) + .with_no_end() + .with_project_id("project_id") + .with_metadata({})), ] + fake_volumes = [ - a(volume().with_id("id1").with_start(2014, 1, 1, 7, 0, 0).with_end( - 2014, 1, 1, 8, 0, 0).with_project_id("project_id")), - a(volume().with_id("id2").with_start(2014, 1, 1, 1, 0, 0).with_no_end().with_project_id("project_id")), - a(volume().with_id("id3").with_start(2014, 1, 1, 8, 0, 0).with_no_end().with_project_id("project_id")), + a(volume() + .with_id("id1") + .with_start(2014, 1, 1, 7, 0, 0) + .with_end(2014, 1, 1, 8, 0, 0) + .with_project_id("project_id")), + a(volume() + .with_id("id2") + .with_start(2014, 1, 1, 1, 0, 0) + .with_no_end() + .with_project_id("project_id")), + a(volume() + .with_id("id3") + .with_start(2014, 1, 1, 8, 0, 0) + .with_no_end() + .with_project_id("project_id")), ] + [self.db.entity.insert(model.todict(fake_entity)) for fake_entity in fake_instances + fake_volumes] - entities = self.adapter.list_entities("project_id", datetime( - 2014, 1, 1, 0, 0, 0, tzinfo=pytz.utc), datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.utc), "instance") + entities = self.adapter.get_all_entities_by_project("project_id", + datetime(2014, 1, 1, 0, 0, 0, tzinfo=pytz.utc), + datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.utc), + "instance") assert_that(entities, contains_inanyorder(*fake_instances)) def test_list_instances_with_decode_output(self): @@ -179,39 +214,57 @@ class MongoDbDriverTest(base.BaseTestCase): [self.db.entity.insert(model.todict(fake_entity)) for fake_entity in fake_instances] - entities = self.adapter.list_entities("project_id", datetime( + entities = self.adapter.get_all_entities_by_project("project_id", datetime( 2014, 1, 1, 0, 0, 0, tzinfo=pytz.utc), datetime(2014, 1, 1, 12, 0, 0, tzinfo=pytz.utc), "instance") assert_that(entities, contains_inanyorder(*expected_instances)) self.assert_entities_metadata_have_been_sanitize(entities) def test_list_entities_in_period(self): fake_entities_in_period = [ - a(instance().with_id("in_the_period").with_start(2014, 1, 1, 7, 0, - 0).with_end(2014, 1, 1, 8, 0, 0).with_project_id( - "project_id")), - a(instance().with_id("running_has_started_before").with_start( - 2014, 1, 1, 1, 0, 0).with_no_end().with_project_id("project_id")), - a(instance().with_id("running_has_started_during").with_start( - 2014, 1, 1, 8, 0, 0).with_no_end().with_project_id("project_id")), + a(instance() + .with_id("in_the_period") + .with_start(2014, 1, 1, 7, 0, 0) + .with_end(2014, 1, 1, 8, 0, 0) + .with_project_id("project_id")), + a(instance() + .with_id("running_has_started_before") + .with_start(2014, 1, 1, 1, 0, 0) + .with_no_end() + .with_project_id("project_id")), + a(instance() + .with_id("running_has_started_during") + .with_start(2014, 1, 1, 8, 0, 0) + .with_no_end() + .with_project_id("project_id")), ] fake_entities_out_period = [ - a(instance().with_id("before_the_period").with_start(2014, 1, 1, 0, - 0, 0).with_end(2014, 1, 1, 1, 0, 0).with_project_id( - "project_id")), - a(instance().with_id("after_the_period").with_start(2014, 1, 1, 10, - 0, 0).with_end(2014, 1, 1, 11, 0, 0).with_project_id( - "project_id")), - a(instance().with_id("running_has_started_after").with_start( - 2014, 1, 1, 10, 0, 0).with_no_end().with_project_id("project_id")), + a(instance() + .with_id("before_the_period") + .with_start(2014, 1, 1, 0, 0, 0) + .with_end(2014, 1, 1, 1, 0, 0) + .with_project_id("project_id")), + a(instance() + .with_id("after_the_period") + .with_start(2014, 1, 1, 10, 0, 0) + .with_end(2014, 1, 1, 11, 0, 0) + .with_project_id("project_id")), + a(instance() + .with_id("running_has_started_after") + .with_start(2014, 1, 1, 10, 0, 0) + .with_no_end() + .with_project_id("project_id")), ] + [self.db.entity.insert(model.todict(fake_entity)) for fake_entity in fake_entities_in_period + fake_entities_out_period] - entities = self.adapter.list_entities("project_id", datetime( - 2014, 1, 1, 6, 0, 0, tzinfo=pytz.utc), datetime(2014, 1, 1, 9, 0, 0, tzinfo=pytz.utc)) + entities = self.adapter.get_all_entities_by_project("project_id", + datetime(2014, 1, 1, 6, 0, 0, tzinfo=pytz.utc), + datetime(2014, 1, 1, 9, 0, 0, tzinfo=pytz.utc)) + assert_that(entities, contains_inanyorder(*fake_entities_in_period)) - def test_list_entities_by_id(self): + def test_get_all_entities_by_id_and_date(self): start = datetime(2016, 3, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2016, 3, 3, 0, 0, 0, 0, pytz.utc) proper_instance = a(instance().with_id("id1").with_start(2016, 3, 1, 0, 0, 0).with_end(2016, 3, 2, 0, 0, 0)) @@ -222,10 +275,10 @@ class MongoDbDriverTest(base.BaseTestCase): .with_start(2016, 3, 2, 0, 0, 0) .with_no_end()), ] + [self.db.entity.insert(model.todict(fake_instance)) for fake_instance in instances] - instance_list = self.adapter.list_entities_by_id("id1", start, end) - + instance_list = self.adapter.get_all_entities_by_id_and_date("id1", start, end) assert_that(instance_list, contains_inanyorder(*[proper_instance])) def test_update_active_entity(self): @@ -304,7 +357,7 @@ class MongoDbDriverTest(base.BaseTestCase): self.assertEqual(0, self.db.volume_type.count()) def test_delete_volume_type_not_in_database(self): - self.assertRaises(exception.AlmanachException, + self.assertRaises(exception.VolumeTypeNotFoundException, self.adapter.delete_volume_type, "not_in_database_id") @@ -329,4 +382,4 @@ class MongoDbDriverTest(base.BaseTestCase): for entity in entities: for key in entity.metadata: self.assertTrue(key.find("^") == -1, - "The metadata key %s contains carret" % (key)) + "The metadata key %s contains caret" % (key))