def test_create_disk_placement(self, placement, expected_id, expected_name): def local_consume_disk_reservation(reservation_id): assert_that(reservation_id is "reservation_id_1") host_disk = HostDisk() host_disk.id = "disk_id_1" host_disk.flavor = HostFlavor("disk_flavor_1") host_disk.persistent = True host_disk.new_disk = True host_disk.capacity_gb = 2 if placement: host_disk.placement = AgentResourcePlacement( AgentResourcePlacement.DISK, host_disk.id, placement ) return [host_disk] handler = HostHandler(MagicMock()) # set the list of datastores handler.hypervisor.datastore_manager.vm_datastores.return_value = \ ["datastore_1", "datastore_2", "datastore_3"] handler.hypervisor.datastore_manager.datastore_name.return_value = \ expected_name handler.hypervisor.placement_manager = MagicMock() mocked_consume_disk_reservation = MagicMock() mocked_consume_disk_reservation.side_effect = \ local_consume_disk_reservation handler.hypervisor.placement_manager.consume_disk_reservation = \ mocked_consume_disk_reservation request = CreateDisksRequest() request.generation = 1 request.reservation = "reservation_id_1" response = handler.create_disks(request) assert_that(response.result is CreateDisksResultCode.OK) response_disks = response.disks assert_that(len(response_disks) is 1) response_disk = response_disks[0] assert_that(response_disk.id, equal_to("disk_id_1")) assert_that(response_disk.datastore.id, equal_to(expected_id)) assert_that(response_disk.datastore.name, equal_to(expected_name))
def test_create_disk_placement_failure( self, placement, expected): def local_consume_disk_reservation(reservation_id): assert_that(reservation_id is "reservation_id_1") host_disk = HostDisk() host_disk.id = "disk_id_1" host_disk.flavor = HostFlavor("disk_flavor_1") host_disk.persistent = True host_disk.new_disk = True host_disk.capacity_gb = 2 if placement: host_disk.placement = AgentResourcePlacement( AgentResourcePlacement.DISK, host_disk.id, placement ) return [host_disk] handler = HostHandler(MagicMock()) # set the list of datastores handler.hypervisor.vm_datastores = \ ["datastore_1", "datastore_2", "datastore_3"] handler.hypervisor.placement_manager = MagicMock() mocked_consume_disk_reservation = MagicMock() mocked_consume_disk_reservation.side_effect = \ local_consume_disk_reservation pm = handler.hypervisor.placement_manager pm.consume_disk_reservation = mocked_consume_disk_reservation request = CreateDisksRequest() request.generation = 1 request.reservation = "reservation_id_1" response = handler.create_disks(request) pm.remove_disk_reservation.assert_called_once_with(request.reservation) assert_that(response.result is CreateDisksResultCode.OK) disk_errors = response.disk_errors assert_that(disk_errors is not None) assert_that(len(disk_errors) is 1) disk_error = disk_errors["disk_id_1"] assert_that(disk_error is not None) assert_that(disk_error.result is expected)
def test_create_disk_placement(self, placement, expected_id, expected_name): def local_consume_disk_reservation(reservation_id): assert_that(reservation_id is "reservation_id_1") host_disk = HostDisk() host_disk.id = "disk_id_1" host_disk.flavor = HostFlavor("disk_flavor_1") host_disk.persistent = True host_disk.new_disk = True host_disk.capacity_gb = 2 if placement: host_disk.placement = AgentResourcePlacement( AgentResourcePlacement.DISK, host_disk.id, placement) return [host_disk] handler = HostHandler(MagicMock()) # set the list of datastores handler.hypervisor.datastore_manager.vm_datastores.return_value = \ ["datastore_1", "datastore_2", "datastore_3"] handler.hypervisor.datastore_manager.datastore_name.return_value = \ expected_name handler.hypervisor.placement_manager = MagicMock() mocked_consume_disk_reservation = MagicMock() mocked_consume_disk_reservation.side_effect = \ local_consume_disk_reservation handler.hypervisor.placement_manager.consume_disk_reservation = \ mocked_consume_disk_reservation request = CreateDisksRequest() request.generation = 1 request.reservation = "reservation_id_1" response = handler.create_disks(request) assert_that(response.result is CreateDisksResultCode.OK) response_disks = response.disks assert_that(len(response_disks) is 1) response_disk = response_disks[0] assert_that(response_disk.id, equal_to("disk_id_1")) assert_that(response_disk.datastore.id, equal_to(expected_id)) assert_that(response_disk.datastore.name, equal_to(expected_name))
def test_create_disk_placement_failure(self, placement, expected): def local_consume_disk_reservation(reservation_id): assert_that(reservation_id is "reservation_id_1") host_disk = HostDisk() host_disk.id = "disk_id_1" host_disk.flavor = HostFlavor("disk_flavor_1") host_disk.persistent = True host_disk.new_disk = True host_disk.capacity_gb = 2 if placement: host_disk.placement = AgentResourcePlacement( AgentResourcePlacement.DISK, host_disk.id, placement) return [host_disk] handler = HostHandler(MagicMock()) # set the list of datastores handler.hypervisor.vm_datastores = \ ["datastore_1", "datastore_2", "datastore_3"] handler.hypervisor.placement_manager = MagicMock() mocked_consume_disk_reservation = MagicMock() mocked_consume_disk_reservation.side_effect = \ local_consume_disk_reservation pm = handler.hypervisor.placement_manager pm.consume_disk_reservation = mocked_consume_disk_reservation request = CreateDisksRequest() request.generation = 1 request.reservation = "reservation_id_1" response = handler.create_disks(request) pm.remove_disk_reservation.assert_called_once_with(request.reservation) assert_that(response.result is CreateDisksResultCode.OK) disk_errors = response.disk_errors assert_that(disk_errors is not None) assert_that(len(disk_errors) is 1) disk_error = disk_errors["disk_id_1"] assert_that(disk_error is not None) assert_that(disk_error.result is expected)