Merge "Volume controller unit test use built-in mock"

This commit is contained in:
Jenkins 2017-01-06 16:18:56 +00:00 committed by Gerrit Code Review
commit 6593961855
1 changed files with 79 additions and 195 deletions

View File

@ -15,7 +15,7 @@
from datetime import datetime
from datetime import timedelta
from dateutil.parser import parse
from flexmock import flexmock
import mock
import pytz
from almanach.core.controllers import volume_controller
@ -33,93 +33,67 @@ class TestVolumeController(base.BaseTestCase):
def setUp(self):
super(TestVolumeController, self).setUp()
self.database_adapter = flexmock(base_driver.BaseDriver)
self.database_adapter = mock.Mock(spec=base_driver.BaseDriver)
self.controller = volume_controller.VolumeController(self.config, self.database_adapter)
def test_volume_deleted(self):
def test_volume_closed(self):
fake_volume = a(volume())
self.database_adapter.count_entity_entries.return_value = 2
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
date = date + timedelta(1)
expected_date = pytz.utc.localize(date)
(flexmock(self.database_adapter)
.should_receive("count_entity_entries")
.with_args(fake_volume.entity_id)
.and_return(1))
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume))
(flexmock(self.database_adapter)
.should_receive("close_active_entity")
.with_args(fake_volume.entity_id, expected_date)
.once())
self.controller.delete_volume(fake_volume.entity_id, date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"))
self.database_adapter.count_entity_entries.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.close_active_entity.assert_called_once_with(fake_volume.entity_id, expected_date)
def test_volume_deleted_within_volume_existance_threshold(self):
fake_volume = a(volume())
self.database_adapter.count_entity_entries.return_value = 2
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
date = date + timedelta(0, 5)
(flexmock(self.database_adapter)
.should_receive("count_entity_entries")
.with_args(fake_volume.entity_id)
.and_return(2))
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume))
(flexmock(self.database_adapter)
.should_receive("delete_active_entity")
.with_args(fake_volume.entity_id)
.once())
self.controller.delete_volume(fake_volume.entity_id, date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"))
self.database_adapter.count_entity_entries.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.delete_active_entity.assert_called_once_with(fake_volume.entity_id)
def test_volume_deleted_within_volume_existance_threshold_but_with_only_one_entry(self):
fake_volume = a(volume())
self.database_adapter.count_entity_entries.return_value = 1
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
date = date + timedelta(0, 5)
expected_date = pytz.utc.localize(date)
(flexmock(self.database_adapter)
.should_receive("count_entity_entries")
.with_args(fake_volume.entity_id)
.and_return(1))
(flexmock(self.database_adapter)
.should_receive("close_active_entity")
.with_args(fake_volume.entity_id, expected_date)
.once())
self.controller.delete_volume(fake_volume.entity_id, date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"))
self.database_adapter.count_entity_entries.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.close_active_entity.assert_called_once_with(fake_volume.entity_id, expected_date)
def test_list_volumes(self):
(flexmock(self.database_adapter)
.should_receive("get_all_entities_by_project")
.with_args("project_id", "start", "end", model.Volume.TYPE)
.and_return(["volume2", "volume3"]))
self.database_adapter.get_all_entities_by_project.return_value = ["volume2", "volume3"]
self.assertEqual(self.controller.list_volumes("project_id", "start", "end"), ["volume2", "volume3"])
self.database_adapter.get_all_entities_by_project.assert_called_once_with(
"project_id", "start", "end", model.Volume.TYPE
)
def test_create_volume(self):
some_volume_type = a(volume_type().with_volume_type_name("some_volume_type_name"))
(flexmock(self.database_adapter)
.should_receive("get_volume_type")
.with_args(some_volume_type.volume_type_id)
.and_return(some_volume_type)
.once())
self.database_adapter.get_volume_type.return_value = some_volume_type
self.database_adapter.get_active_entity.return_value = None
some_volume = a(volume()
.with_volume_type(some_volume_type.volume_type_name)
@ -130,20 +104,14 @@ class TestVolumeController(base.BaseTestCase):
.with_project_id(some_volume.project_id)
.with_id(some_volume.entity_id))
(flexmock(self.database_adapter)
.should_receive("insert_entity")
.with_args(expected_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(some_volume.entity_id)
.and_return(None)
.once())
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)
self.database_adapter.get_volume_type.assert_called_once_with(some_volume_type.volume_type_id)
self.database_adapter.insert_entity.assert_called_once_with(expected_volume)
self.database_adapter.get_active_entity.assert_called_once_with(some_volume.entity_id)
def test_create_volume_raises_bad_date_format(self):
some_volume = a(volume())
@ -161,9 +129,7 @@ class TestVolumeController(base.BaseTestCase):
def test_create_volume_insert_none_volume_type_as_type(self):
some_volume_type = a(volume_type().with_volume_type_id(None).with_volume_type_name(None))
(flexmock(self.database_adapter)
.should_receive("get_volume_type")
.never())
self.database_adapter.get_active_entity.return_value = None
some_volume = a(volume()
.with_volume_type(some_volume_type.volume_type_name)
@ -174,60 +140,42 @@ class TestVolumeController(base.BaseTestCase):
.with_project_id(some_volume.project_id)
.with_id(some_volume.entity_id))
(flexmock(self.database_adapter)
.should_receive("insert_entity")
.with_args(expected_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(some_volume.entity_id)
.and_return(None)
.once())
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)
self.database_adapter.get_volume_type.assert_not_called()
self.database_adapter.insert_entity.assert_called_once_with(expected_volume)
self.database_adapter.get_active_entity.assert_called_once_with(some_volume.entity_id)
def test_create_volume_with_invalid_volume_type(self):
some_volume_type = a(volume_type())
(flexmock(self.database_adapter)
.should_receive("get_volume_type")
.with_args(some_volume_type.volume_type_id)
.and_raise(exception.VolumeTypeNotFoundException)
.once())
self.database_adapter.get_volume_type.side_effect = exception.VolumeTypeNotFoundException
self.database_adapter.get_active_entity.return_value = None
some_volume = a(volume()
.with_volume_type(some_volume_type.volume_type_name)
.with_all_dates_in_string())
(flexmock(self.database_adapter)
.should_receive("insert_entity")
.never())
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(some_volume.entity_id)
.and_return(None)
.once())
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)
self.database_adapter.get_volume_type.assert_called_once_with(some_volume_type.volume_type_id)
self.database_adapter.insert_entity.assert_not_called()
self.database_adapter.get_active_entity.assert_called_once_with(some_volume.entity_id)
def test_create_volume_but_its_an_old_event(self):
some_volume = a(volume().with_last_event(pytz.utc.localize(datetime(2015, 10, 21, 16, 29, 0))))
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(some_volume.entity_id)
.and_return(some_volume)
.once())
self.database_adapter.get_active_entity.return_value = some_volume
self.controller.create_volume(some_volume.entity_id, some_volume.project_id, '2015-10-21T16:25:00.000000Z',
some_volume.volume_type, some_volume.size, some_volume.name,
some_volume.attached_to)
self.database_adapter.get_active_entity.assert_called_once_with(some_volume.entity_id)
def test_volume_updated(self):
fake_volume = a(volume())
dates_str = "2015-10-21T16:25:00.000000Z"
@ -235,91 +183,59 @@ class TestVolumeController(base.BaseTestCase):
fake_volume.start = parse(dates_str)
fake_volume.end = None
fake_volume.last_event = parse(dates_str)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("close_active_entity")
.with_args(fake_volume.entity_id, parse(dates_str))
.once())
(flexmock(self.database_adapter)
.should_receive("insert_entity")
.with_args(fake_volume)
.once())
self.database_adapter.get_active_entity.return_value = fake_volume
self.controller.resize_volume(fake_volume.entity_id, "new_size", dates_str)
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.close_active_entity.assert_called_once_with(fake_volume.entity_id, parse(dates_str))
self.database_adapter.insert_entity.assert_called_once_with(fake_volume)
def test_volume_attach_with_no_existing_attachment(self):
fake_volume = a(volume()
.with_no_attachment())
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("update_active_entity")
.with_args(fake_volume))
self.controller.attach_volume(
fake_volume.entity_id,
date.strftime("%Y-%m-%dT%H:%M:%S.%f"),
["new_attached_to"]
)
self.assertEqual(fake_volume.attached_to, ["new_attached_to"])
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.update_active_entity.assert_called_once_with(fake_volume)
def test_volume_attach_with_existing_attachments(self):
fake_volume = a(volume()
.with_attached_to(["existing_attached_to"]))
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("update_active_entity")
.with_args(fake_volume))
self.controller.attach_volume(
fake_volume.entity_id,
date.strftime("%Y-%m-%dT%H:%M:%S.%f"),
["existing_attached_to", "new_attached_to"]
)
self.assertEqual(fake_volume.attached_to, ["existing_attached_to", "new_attached_to"])
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.update_active_entity.assert_called_once_with(fake_volume)
def test_volume_attach_after_threshold(self):
fake_volume = a(volume())
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
date = date + timedelta(0, 120)
expected_date = pytz.utc.localize(date)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("close_active_entity")
.with_args(fake_volume.entity_id, expected_date)
.once())
new_volume = a(volume()
.build_from(fake_volume)
.with_datetime_start(expected_date)
@ -327,74 +243,51 @@ class TestVolumeController(base.BaseTestCase):
.with_last_event(expected_date)
.with_attached_to(["new_attached_to"]))
(flexmock(self.database_adapter)
.should_receive("insert_entity")
.with_args(new_volume)
.once())
self.controller.attach_volume(
fake_volume.entity_id,
date.strftime("%Y-%m-%dT%H:%M:%S.%f"),
["new_attached_to"]
)
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.close_active_entity.assert_called_once_with(fake_volume.entity_id, expected_date)
self.database_adapter.insert_entity.assert_called_once_with(new_volume)
def test_volume_detach_with_two_attachments(self):
fake_volume = a(volume().with_attached_to(["I1", "I2"]))
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("update_active_entity")
.with_args(fake_volume))
self.controller.detach_volume(fake_volume.entity_id, date.strftime("%Y-%m-%dT%H:%M:%S.%fZ"), ["I2"])
self.assertEqual(fake_volume.attached_to, ["I2"])
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.update_active_entity.assert_called_once_with(fake_volume)
def test_volume_detach_with_one_attachments(self):
fake_volume = a(volume().with_attached_to(["I1"]))
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("update_active_entity")
.with_args(fake_volume))
self.controller.detach_volume(fake_volume.entity_id, date.strftime("%Y-%m-%dT%H:%M:%S.%f"), [])
self.assertEqual(fake_volume.attached_to, [])
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.update_active_entity.assert_called_once_with(fake_volume)
def test_volume_detach_last_attachment_after_threshold(self):
fake_volume = a(volume().with_attached_to(["I1"]))
self.database_adapter.get_active_entity.return_value = fake_volume
date = datetime(fake_volume.start.year, fake_volume.start.month, fake_volume.start.day, fake_volume.start.hour,
fake_volume.start.minute, fake_volume.start.second, fake_volume.start.microsecond)
date = date + timedelta(0, 120)
expected_date = pytz.utc.localize(date)
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
(flexmock(self.database_adapter)
.should_receive("close_active_entity")
.with_args(fake_volume.entity_id, expected_date)
.once())
new_volume = a(volume()
.build_from(fake_volume)
.with_datetime_start(expected_date)
@ -402,30 +295,21 @@ class TestVolumeController(base.BaseTestCase):
.with_last_event(expected_date)
.with_no_attachment())
(flexmock(self.database_adapter)
.should_receive("insert_entity")
.with_args(new_volume)
.once())
self.controller.detach_volume(fake_volume.entity_id, date.strftime("%Y-%m-%dT%H:%M:%S.%f"), [])
self.assertEqual(fake_volume.attached_to, [])
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.close_active_entity.assert_called_once_with(fake_volume.entity_id, expected_date)
self.database_adapter.insert_entity.assert_called_once_with(new_volume)
def test_rename_volume(self):
fake_volume = a(volume().with_display_name('old_volume_name'))
volume_name = 'new_volume_name'
(flexmock(self.database_adapter)
.should_receive("get_active_entity")
.with_args(fake_volume.entity_id)
.and_return(fake_volume)
.once())
self.database_adapter.get_active_entity.return_value = fake_volume
new_volume = a(volume().build_from(fake_volume).with_display_name(volume_name))
(flexmock(self.database_adapter)
.should_receive("update_active_entity")
.with_args(new_volume)
.once())
self.controller.rename_volume(fake_volume.entity_id, volume_name)
self.database_adapter.get_active_entity.assert_called_once_with(fake_volume.entity_id)
self.database_adapter.update_active_entity.assert_called_once_with(new_volume)