diff --git a/almanach/tests/unit/core/controllers/test_instance_controller.py b/almanach/tests/unit/core/controllers/test_instance_controller.py index 7bab2d2..c6a373b 100644 --- a/almanach/tests/unit/core/controllers/test_instance_controller.py +++ b/almanach/tests/unit/core/controllers/test_instance_controller.py @@ -14,7 +14,7 @@ from datetime import datetime from dateutil.parser import parse -from flexmock import flexmock +import mock import pytz from almanach.core.controllers import instance_controller @@ -32,21 +32,12 @@ class TestInstanceController(base.BaseTestCase): def setUp(self): super(TestInstanceController, self).setUp() self.config.entities.instance_image_meta = ['distro', 'version', 'os_type'] - self.database_adapter = flexmock(base_driver.BaseDriver) + self.database_adapter = mock.Mock(spec=base_driver.BaseDriver) self.controller = instance_controller.InstanceController(self.config, self.database_adapter) def test_instance_created(self): fake_instance = a(instance().with_all_dates_in_string()) - - (flexmock(self.database_adapter) - .should_receive("get_active_entity") - .with_args(fake_instance.entity_id) - .and_raise(exception.EntityNotFoundException) - .once()) - - (flexmock(self.database_adapter) - .should_receive("insert_entity") - .once()) + self.database_adapter.get_active_entity.side_effect = exception.EntityNotFoundException self.controller.create_instance(fake_instance.entity_id, fake_instance.project_id, @@ -56,108 +47,75 @@ class TestInstanceController(base.BaseTestCase): fake_instance.image_meta, fake_instance.metadata) + self.database_adapter.get_active_entity.assert_called_once_with(fake_instance.entity_id) + self.database_adapter.insert_entity.assert_called_once() + def test_resize_instance(self): fake_instance = a(instance()) + self.database_adapter.get_active_entity.return_value = fake_instance dates_str = "2015-10-21T16:25:00.000000Z" fake_instance.start = parse(dates_str) fake_instance.end = None fake_instance.last_event = parse(dates_str) - (flexmock(self.database_adapter) - .should_receive("get_active_entity") - .with_args(fake_instance.entity_id) - .and_return(fake_instance) - .once()) - (flexmock(self.database_adapter) - .should_receive("close_active_entity") - .with_args(fake_instance.entity_id, parse(dates_str)) - .once()) - - (flexmock(self.database_adapter) - .should_receive("insert_entity") - .with_args(fake_instance) - .once()) - self.controller.resize_instance(fake_instance.entity_id, "newly_flavor", dates_str) + self.database_adapter.get_active_entity.assert_called_once_with(fake_instance.entity_id) + self.database_adapter.close_active_entity.assert_called_once_with(fake_instance.entity_id, parse(dates_str)) + self.database_adapter.insert_entity.assert_called_once_with(fake_instance) + def test_instance_created_but_its_an_old_event(self): fake_instance = a(instance() .with_last_event(pytz.utc.localize(datetime(2015, 10, 21, 16, 29, 0)))) - - (flexmock(self.database_adapter) - .should_receive("get_active_entity") - .with_args(fake_instance.entity_id) - .and_return(fake_instance) - .once()) + self.database_adapter.get_active_entity.return_value = fake_instance self.controller.create_instance(fake_instance.entity_id, fake_instance.project_id, '2015-10-21T16:25:00.000000Z', fake_instance.flavor, fake_instance.image_meta, fake_instance.metadata) + self.database_adapter.get_active_entity.assert_called_once_with(fake_instance.entity_id) + def test_instance_created_but_find_garbage(self): fake_instance = a(instance().with_all_dates_in_string()) - - (flexmock(self.database_adapter) - .should_receive("get_active_entity") - .with_args(fake_instance.entity_id) - .and_raise(exception.EntityTypeNotSupportedException) - .once()) - - (flexmock(self.database_adapter) - .should_receive("insert_entity") - .once()) + self.database_adapter.get_active_entity.side_effect = exception.EntityTypeNotSupportedException self.controller.create_instance(fake_instance.entity_id, fake_instance.project_id, fake_instance.start, fake_instance.flavor, fake_instance.image_meta, fake_instance.metadata) - def test_instance_deleted(self): - (flexmock(self.database_adapter) - .should_receive("has_active_entity") - .with_args("id1") - .and_return(True) - .once()) + self.database_adapter.get_active_entity.assert_called_once_with(fake_instance.entity_id) + self.database_adapter.insert_entity.assert_called_once() - (flexmock(self.database_adapter) - .should_receive("close_active_entity") - .with_args("id1", parse("2015-10-21T16:25:00.000000Z")) - .once()) + def test_instance_deleted(self): + self.database_adapter.has_active_entity.return_value = True self.controller.delete_instance("id1", "2015-10-21T16:25:00.000000Z") + self.database_adapter.has_active_entity.assert_called_once_with("id1") + self.database_adapter.close_active_entity.assert_called_once_with("id1", parse("2015-10-21T16:25:00.000000Z")) + def test_instance_deleted_when_entity_not_found(self): - (flexmock(self.database_adapter) - .should_receive("has_active_entity") - .with_args("id1") - .and_return(False) - .once()) + self.database_adapter.has_active_entity.return_value = False self.assertRaises(exception.EntityNotFoundException, self.controller.delete_instance, "id1", "2015-10-21T16:25:00.000000Z") + self.database_adapter.has_active_entity.assert_called_once_with("id1") + def test_list_instances(self): - (flexmock(self.database_adapter) - .should_receive("get_all_entities_by_project") - .with_args("project_id", "start", "end", model.Instance.TYPE) - .and_return(["instance1", "instance2"]) - .once()) + self.database_adapter.get_all_entities_by_project.return_value = ["instance1", "instance2"] self.assertEqual(self.controller.list_instances("project_id", "start", "end"), ["instance1", "instance2"]) + self.database_adapter.get_all_entities_by_project.assert_called_once_with( + "project_id", "start", "end", model.Instance.TYPE + ) + def test_instance_rebuilded(self): i = a(instance()) - - (flexmock(self.database_adapter) - .should_receive("get_active_entity") - .and_return(i) - .twice()) - (flexmock(self.database_adapter) - .should_receive("close_active_entity") - .once()) - (flexmock(self.database_adapter) - .should_receive("insert_entity") - .once()) + self.database_adapter.get_active_entity.side_effect = [i, i] + calls = [mock.call("an_instance_id"), mock.call("an_instance_id")] self.controller.rebuild_instance( "an_instance_id", @@ -169,3 +127,7 @@ class TestInstanceController(base.BaseTestCase): "2015-10-21T16:25:00.000000Z", dict(distro=i.image_meta['distro'], version=i.image_meta['version'], os_type=i.image_meta['os_type']) ) + + self.database_adapter.get_active_entity.assert_has_calls(calls) + self.database_adapter.close_active_entity.assert_called_once() + self.database_adapter.insert_entity.assert_called_once()