def test_constraint_place_engine_no_constraints(self): """ constraint place engine should not handle placement with no constraints """ # Create constraint place engine image_datastore = "ds1" image_datastores = [{"name": image_datastore, "used_for_vms": True}] option = PlacementOption(1, 1, image_datastores) ds_map = { image_datastore: DatastoreInfo(10, 0), "ds2": DatastoreInfo(20, 0), "ds3": DatastoreInfo(30, 0) } ds_mgr = self.create_datastore_manager(ds_map, image_datastore) engine = ConstraintDiskPlaceEngine(ds_mgr, option) ds = engine.placeable_datastores() selector = DatastoreSelector.init_datastore_selector(ds_mgr, ds) # Try place disks = [ Disk(disk_id="disk1", capacity_gb=1), Disk(disk_id="disk2", capacity_gb=1), Disk(capacity_gb=1), ] disks_placement = DisksPlacement(disks, selector) place_result = engine.place(disks_placement, []) # Verify place result assert_that(place_result.result, equal_to(PlaceResultCode.OK)) # Verify unplaced list disks_placement = place_result.disks_placement assert_that(disks_placement.placement_list, has_length(0)) assert_that(disks_placement.disks, has_length(3))
def test_place_large_disks_image_datastore(self): ds_map = {"datastore_id_1": (DatastoreInfo(3 * 1024, 0), set([]))} ds_with_image = ["datastore_id_1", "datastore_id_2"] total_storage = sum(t[0].total for t in ds_map.values()) manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image).build() # Disable freespace based admission control. manager.FREESPACE_THRESHOLD = 0 disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 1024) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, 1024) disk_list = [disk1, disk2] used_storage = sum(disk.capacity_gb for disk in disk_list) vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED, None, None, disk_list) disks = None score, placement_list = manager.place(vm, disks) expected_score = 100 * (total_storage - used_storage) / total_storage assert_that(score, is_(AgentPlacementScore(expected_score, 100))) base_index = 0 assert_that(placement_list[base_index].resource_id, is_(vm.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_1")) base_index += 1 assert_that(placement_list[base_index].resource_id, is_(disk1.id)) assert_that(placement_list[base_index + 1].resource_id, is_(disk2.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_1")) assert_that(placement_list[base_index + 1].container_id, is_("datastore_id_1"))
def test_place_vm_existing_image_two_matching_datastores(self): ds_map = { "datastore_id_1": (DatastoreInfo(8 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(16 * 1024, 0), set([])), "datastore_id_3": (DatastoreInfo(15 * 1024, 0), set([])) } ds_with_image = ["datastore_id_1", "datastore_id_2"] manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image).build() total_storage = sum(t[0].total for t in ds_map.values()) image = DiskImage("disk_image", DiskImage.COPY_ON_WRITE) # disk1 and disk2 both take 1/100 disk1 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image) vm = Vm(new_id(), self._vm_flavor_mb(1), VmPowerState.STOPPED, None, None, [disk1, disk2]) score, placement_list = manager.place(vm, None) # There are 2 disk of approx size 1/100 of avail space # the score should be 98 assert_that(score, is_(AgentPlacementScore(98, 100))) base_index = 0 assert_that(placement_list[base_index].resource_id, is_(vm.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_2")) base_index += 1 assert_that(placement_list[base_index].resource_id, is_(disk1.id)) assert_that(placement_list[base_index + 1].resource_id, is_(disk2.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_2")) assert_that(placement_list[base_index + 1].container_id, is_("datastore_id_2"))
def test_place_image_disk_best_effort(self): ds_map = { "datastore_id_1": (DatastoreInfo(1 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(2 * 1024, 0), set([])) } manager = PMBuilder(ds_map=ds_map).build() image = DiskImage("disk_image1", DiskImage.COPY_ON_WRITE) # Place a image disk that is linked cloned from image. The size # should not be included in calculation. disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 512, image) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, 2048) disk3 = Disk(new_id(), DISK_FLAVOR, False, True, 512) vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED, None, None, [disk1, disk2, disk3]) score, placement_list = manager.place(vm, None) # storage score is 1. 17/10. divided by 10 which is not optimal penalty assert_that(score.utilization, equal_to(1)) assert_that(score.transfer, equal_to(100)) assert_that(placement_list, has_length(4)) assert_that(placement_list[0].container_id, equal_to("datastore_id_2")) assert_that(placement_list[0].resource_id, equal_to(vm.id)) assert_that(placement_list[1].container_id, equal_to("datastore_id_2")) assert_that(placement_list[1].resource_id, equal_to(disk2.id)) assert_that(placement_list[2].container_id, equal_to("datastore_id_1")) assert_that(placement_list[2].resource_id, equal_to(disk3.id)) assert_that(placement_list[3].container_id, equal_to("datastore_id_1")) assert_that(placement_list[3].resource_id, equal_to(disk1.id))
def test_place_cpu_constraint(self): disk1 = Disk(new_id(), DISK_FLAVOR, True, True, 1024) disk2 = Disk(new_id(), DISK_FLAVOR, True, True, 1024) vm = Vm(new_id(), self._vm_flavor_gb(memory_gb=1, cpu=100), VmPowerState.STARTED, None) vm.disks = [disk1, disk2] manager = PMBuilder(cpu_overcommit=1).build() manager.place(vm, None)
def test_constraint_place_engine(self): # Create constraint place engine image_datastore = "ds1" image_datastores = [{"name": image_datastore, "used_for_vms": True}] option = PlacementOption(1, 1, image_datastores) ds_map = { image_datastore: DatastoreInfo(10, 0), "ds2": DatastoreInfo(20, 0), "ds3": DatastoreInfo(30, 0) } ds_mgr = self.create_datastore_manager(ds_map, image_datastore) engine = ConstraintDiskPlaceEngine(ds_mgr, option) ds = engine.placeable_datastores() selector = DatastoreSelector.init_datastore_selector(ds_mgr, ds) # Try place disks = [ Disk(disk_id="disk", capacity_gb=5, constraints=[]), Disk(disk_id="disk-in-ds1", capacity_gb=5, constraints=[ ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds1"]) ]), Disk(disk_id="disk-in-ds2", capacity_gb=5, constraints=[ ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds2"]) ]), Disk(disk_id="disk-in-ds3", capacity_gb=5, constraints=[ ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds3"]) ]), Disk(capacity_gb=5, constraints=[]), ] disks_placement = DisksPlacement(disks, selector) place_result = engine.place(disks_placement) # Verify place result assert_that(place_result.result, equal_to(PlaceResultCode.OK)) # Verify placement list and unplaced list disks_placement = place_result.disks_placement assert_that(disks_placement.placement_list, has_length(3)) assert_that(disks_placement.disks, has_length(2)) assert_that([d.resource_id for d in disks_placement.placement_list], contains_inanyorder("disk-in-ds1", "disk-in-ds2", "disk-in-ds3")) for placement in disks_placement.placement_list: assert_that(placement.type, equal_to(AgentResourcePlacement.DISK)) assert_that("disk-in-" + placement.container_id, equal_to(placement.resource_id))
def test_placement_vm_on_image_datastore(self, use_image_ds, use_vm): ds_map = { "datastore_id_1": (DatastoreInfo(7 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(8 * 1024, 0), set([])), "image_datastore": (DatastoreInfo(16 * 1024, 0), set([])) } manager = PMBuilder(im_ds_for_vm=use_image_ds, image_ds="image_datastore", ds_map=ds_map).build() image = DiskImage("disk_image", DiskImage.FULL_COPY) disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 7 * 1024, image) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, 7 * 1024, image) disk3 = Disk(new_id(), DISK_FLAVOR, False, True, 7 * 1024, image) disk4 = Disk(new_id(), DISK_FLAVOR, False, True, 7 * 1024, image) disk_list = [disk1, disk2, disk3, disk4] if use_vm: vm = Vm(new_id(), VM_FLAVOR, State.STOPPED, None, None, disk_list) disks = None else: vm = None disks = disk_list if not use_image_ds: self.assertRaises(NotEnoughDatastoreCapacityException, manager.place, vm, disks) else: (score, placement_list) = manager.place(vm, disks) container_ids = [item.container_id for item in placement_list] resource_ids = [item.resource_id for item in placement_list] if use_vm: assert_that( container_ids, contains( "image_datastore", # vm "image_datastore", # 16T, 8T, 7T "image_datastore", # 9T, 8T, 7T "datastore_id_2", # 2T, 8T, 7T "datastore_id_1")) # 2T, 1T, 7T assert_that( resource_ids, contains(vm.id, disk1.id, disk2.id, disk3.id, disk4.id)) else: assert_that( container_ids, contains( "image_datastore", # 16T, 8T, 7T "image_datastore", # 9T, 8T, 7T "datastore_id_2", # 2T, 8T, 7T "datastore_id_1")) # 2T, 1T, 7T assert_that(resource_ids, contains(disk1.id, disk2.id, disk3.id, disk4.id))
def test_place_memory_constraint(self): # Try to deploy a VM with 64GB memory on a system with 64GB memory # will fail because the memory score is 0. # Actually if memory score is less than 5 (1 - max_usage) will be # rejected. In this case, 60GB is also rejected. disk1 = Disk(new_id(), DISK_FLAVOR, True, True, 1024) disk2 = Disk(new_id(), DISK_FLAVOR, True, True, 1024) vm = Vm(new_id(), self._vm_flavor_gb(64), VmPowerState.STARTED, None) vm.disks = [disk1, disk2] manager = PMBuilder().build() manager.place(vm, None)
def test_disks_capacity_gb(self): """Linked-cloned image disks are excluded from capacity calculation.""" image1 = DiskImage("disk_image1", DiskImage.COPY_ON_WRITE) image2 = DiskImage("disk_image2", DiskImage.FULL_COPY) disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 1, image1) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, 2, image2) disk3 = Disk(new_id(), DISK_FLAVOR, False, True, 3) disk4 = Disk(new_id(), DISK_FLAVOR, False, True, 4) disk5 = Disk(new_id(), DISK_FLAVOR, False, True, 5) disks = [disk1, disk2, disk3, disk4, disk5] assert_that(DiskUtil().disks_capacity_gb(disks), equal_to(14))
def test_place_new_with_reservations(self, disk1_capacity, disk2_capacity, overcommit, expected): disk1 = Disk(new_id(), DISK_FLAVOR, True, True, disk1_capacity) disk2 = Disk(new_id(), DISK_FLAVOR, True, True, disk2_capacity) vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED, None) vm.disks = [disk1, disk2] manager = PMBuilder(mem_overcommit=overcommit).build() # Disable freespace based admission control. manager.FREESPACE_THRESHOLD = 0 manager.reserve(vm, None) score, placement_list = manager.place(vm, None) assert_that(score, is_(AgentPlacementScore(*expected)))
def test_place_large_disks(self, use_vm): ds_map = { "datastore_id_1": (DatastoreInfo(1 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(2 * 1024, 0), set([])), "datastore_id_3": (DatastoreInfo(3 * 1024, 0), set([])) } ds_with_image = ["datastore_id_3"] total_storage = sum(t[0].total for t in ds_map.values()) manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image).build() image = DiskImage("disk_image", DiskImage.FULL_COPY) disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 1024, image) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, 1024, image) disk3 = Disk(new_id(), DISK_FLAVOR, False, True, 1024, image) disk4 = Disk(new_id(), DISK_FLAVOR, False, True, 1024, image) disk_list = [disk1, disk2, disk3, disk4] used_storage = sum(disk.capacity_gb for disk in disk_list) if use_vm: vm = Vm(new_id(), VM_FLAVOR, State.STOPPED, None, None, disk_list) disks = None else: vm = None disks = disk_list score, placement_list = manager.place(vm, disks) # optimal placement cannot be achieved, # divide the expected score by NOT_OPTIMAL_DIVIDE_FACTOR expected_score = 100 * (total_storage - used_storage) / \ total_storage / PlacementManager.NOT_OPTIMAL_DIVIDE_FACTOR assert_that(score, is_(AgentPlacementScore(expected_score, 100))) base_index = 0 if vm: assert_that(placement_list[base_index].resource_id, is_(vm.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_3")) base_index += 1 assert_that(placement_list[base_index].resource_id, is_(disk1.id)) assert_that(placement_list[base_index + 1].resource_id, is_(disk2.id)) assert_that(placement_list[base_index + 2].resource_id, is_(disk3.id)) assert_that(placement_list[base_index + 3].resource_id, is_(disk4.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_3")) assert_that(placement_list[base_index + 1].container_id, is_("datastore_id_3")) assert_that(placement_list[base_index + 2].container_id, is_("datastore_id_3")) assert_that(placement_list[base_index + 3].container_id, is_("datastore_id_2"))
def test_place_with_disks_constraints(self): ds_map = { "datastore_id_1": (DatastoreInfo(8 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(16 * 1024, 0), set([])), "datastore_id_3": (DatastoreInfo(16 * 1024, 0), set([])) } ds_name_id_map = { "ds1": "datastore_id_1", "ds2": "datastore_id_2", "ds3": "datastore_id_3" } ds_with_image = ["datastore_id_1", "datastore_id_2"] total_storage = sum(t[0].total for t in ds_map.values()) manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image, ds_name_id_map=ds_name_id_map).build() # Disable freespace based admission control. manager.FREESPACE_THRESHOLD = 0 image = DiskImage("disk_image", DiskImage.COPY_ON_WRITE) constraint1 = ResourceConstraint(ResourceConstraintType.DATASTORE, ["datastore_id_1"]) constraint2 = ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds2"]) disk1 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image, constraints=[constraint1]) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image, constraints=[constraint2]) disks = [disk1, disk2] score, placement_list = manager.place(None, disks) # Image disks doesn't count in utilization score assert_that(score, is_(AgentPlacementScore(100, 100))) base_index = 0 assert_that(placement_list[base_index].resource_id, is_(disk1.id)) assert_that(placement_list[base_index + 1].resource_id, is_(disk2.id)) assert_that(placement_list[base_index].container_id, is_("datastore_id_1")) assert_that(placement_list[base_index + 1].container_id, is_("datastore_id_2"))
def test_place_new(self, disk_capacity_1, disk_capacity_2, overcommit, expected): created_disk1 = Disk(new_id(), DISK_FLAVOR, True, True, disk_capacity_1) created_disk2 = Disk(new_id(), DISK_FLAVOR, True, True, disk_capacity_2) vm = Vm(new_id(), VM_FLAVOR, State.STOPPED, None) vm.disks = [created_disk1, created_disk2] manager = PMBuilder(mem_overcommit=overcommit).build() # Disable freespace based admission control. manager.FREESPACE_THRESHOLD = 0 score, placement_list = manager.place(vm, None) assert_that(score, is_(AgentPlacementScore(*expected)))
def test_place_with_conflicting_constraints(self): ds_map = { "datastore_id_1": (DatastoreInfo(8 * 1024, 0), set(["tag1", "tag2"])), "datastore_id_2": (DatastoreInfo(16 * 1024, 0), set(["tag3", "tag2"])), "datastore_id_3": (DatastoreInfo(16 * 1024, 0), set([])) } ds_with_image = ["datastore_id_1", "datastore_id_2"] total_storage = sum(t[0].total for t in ds_map.values()) manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image).build() image = DiskImage("disk_image", DiskImage.COPY_ON_WRITE) # The following 2 constraints conflict constraint1 = ResourceConstraint(ResourceConstraintType.DATASTORE_TAG, ["tag1"]) constraint2 = ResourceConstraint(ResourceConstraintType.DATASTORE, ["datastore_id_2"]) disk1 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image, constraints=[constraint1, constraint2]) disks = [disk1] manager.place(None, disks)
def test_constraint_place_engine_constraint_violated(self): # Create constraint place engine image_datastore = "ds1" image_datastores = [{"name": image_datastore, "used_for_vms": True}] option = PlacementOption(1, 1, image_datastores) ds_map = {image_datastore: DatastoreInfo(5, 0)} ds_mgr = self.create_datastore_manager(ds_map, image_datastores) engine = ConstraintDiskPlaceEngine(ds_mgr, option) ds = engine.placeable_datastores() selector = DatastoreSelector.init_datastore_selector(ds_mgr, ds) # Try place disks = [ Disk(disk_id="ds1", capacity_gb=1, constraints=[ ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds2"]) ]), ] place_result = engine.place(DisksPlacement(disks, selector)) # Verify place result assert_that(place_result.result, equal_to(PlaceResultCode.NO_SUCH_RESOURCE))
def test_constraint_place_engine_conflicting_constraints(self): """ constraint place engine should fail if multiple constraints conflict """ # Create constraint place engine image_datastore = "ds1" image_datastores = [{"name": image_datastore, "used_for_vms": True}] option = PlacementOption(1, 1, image_datastores) ds_map = { image_datastore: DatastoreInfo(10, 0), "ds2": DatastoreInfo(20, 0) } ds_mgr = self.create_datastore_manager(ds_map, image_datastores) engine = ConstraintDiskPlaceEngine(ds_mgr, option) ds = engine.placeable_datastores() selector = DatastoreSelector.init_datastore_selector(ds_mgr, ds) # Try place constraints = [ ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds1"]), ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds2"]) ] disk = Disk(disk_id="ds1", capacity_gb=1) place_result = engine.place(DisksPlacement([disk], selector), constraints) # Verify place result assert_that(place_result.result, equal_to(PlaceResultCode.NO_SUCH_RESOURCE))
def test_place_with_disks_tagging_constraints(self, tags, expected): ds_map = { "datastore_id_1": (DatastoreInfo(8 * 1024, 0), set(["tag1", "tag2"])), "datastore_id_2": (DatastoreInfo(16 * 1024, 0), set(["tag3", "tag2"])), "datastore_id_3": (DatastoreInfo(16 * 1024, 0), set([])) } ds_with_image = ["datastore_id_1", "datastore_id_2"] total_storage = sum(t[0].total for t in ds_map.values()) manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image).build() # Disable freespace based admission control. manager.FREESPACE_THRESHOLD = 0 image = DiskImage("disk_image", DiskImage.COPY_ON_WRITE) constraints = [ ResourceConstraint(ResourceConstraintType.DATASTORE_TAG, [t]) for t in tags ] disk = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image, constraints=constraints) score, placement_list = manager.place(None, [disk]) base_index = 0 assert_that(placement_list[base_index].resource_id, is_(disk.id)) assert_that(placement_list[base_index].container_id, is_(expected))
def test_reserved_resources(self): manager = PMBuilder().build() disk = Disk(new_id(), DISK_FLAVOR, True, True, 1024) vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STARTED) reservations = [] for _ in range(3): reservations.append(manager.reserve(vm, [disk])) assert_that(manager._storage_reserved(), equal_to(disk.capacity_gb * len(reservations))) assert_that( manager._memory_reserved(), equal_to(manager._vm_memory_mb(vm) * len(reservations))) for rid in reservations: manager.remove_disk_reservation(rid) manager.remove_vm_reservation(rid) assert_that(manager._storage_reserved(), equal_to(0)) assert_that(manager._memory_reserved(), equal_to(0)) # test that reserve sums as expected if vm or disks is None rid = manager.reserve(vm, None) assert_that(manager._storage_reserved(), equal_to(0)) assert_that(manager._memory_reserved(), equal_to(manager._vm_memory_mb(vm))) manager.remove_vm_reservation(rid) rid = manager.reserve(None, [disk]) assert_that(manager._storage_reserved(), equal_to(disk.capacity_gb)) assert_that(manager._memory_reserved(), equal_to(0)) manager.remove_disk_reservation(rid)
def test_place_vm_fail_disk_too_large(self): ds_map = { "datastore_id_1": (DatastoreInfo(1 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(2 * 1024, 0), set([])), "datastore_id_3": (DatastoreInfo(3 * 1024, 0), set([])) } ds_with_image = ["datastore_id_1", "datastore_id_2"] manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image).build() disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 2 * 1024) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, 2 * 1024) disk3 = Disk(new_id(), DISK_FLAVOR, False, True, 2 * 1024) disk4 = Disk(new_id(), DISK_FLAVOR, False, True, 2 * 1024) disk_list = [disk1, disk2, disk3, disk4] vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED, None, None, disk_list) disks = None manager.place(vm, disks)
def test_place_vm_non_existing_image(self, image_size, expected): manager = PMBuilder(ds_with_image=[], image_size=image_size).build() image = DiskImage("disk_image", DiskImage.COPY_ON_WRITE) disk = Disk(new_id(), DISK_FLAVOR, False, True, 1024, image) vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED, None, None, [disk]) score, placement_list = manager.place(vm, None) # Image size is 2GB, 50% of 1GB, so transfer score is assert_that(score.transfer, is_(expected)) assert_that(placement_list, has_length(2)) # vm and disk
def test_place_storage_constraint(self): disk = Disk(new_id(), DISK_FLAVOR, True, True, 1024) vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STARTED, None) vm.disks = [disk] ds_map = { "datastore_id_1": (DatastoreInfo(8 * 1024, 7 * 1024), set([])) } manager = PMBuilder(ds_map=ds_map).build() manager.place(vm, None)
def get_resource(self, disk_id): datastore = self.get_datastore(disk_id) if datastore is None: raise DiskNotFoundException(disk_id) resource = Disk(disk_id) resource.flavor = Flavor("default") # TODO resource.persistent = False # TODO resource.new_disk = False resource.capacity_gb = -1 # TODO resource.image = None resource.datastore = datastore return resource
def test_place_image_disk_not_included(self): ds_map = {"datastore_id_1": (DatastoreInfo(1 * 1024, 0), set([]))} manager = PMBuilder(ds_map=ds_map).build() image = DiskImage("disk_image1", DiskImage.COPY_ON_WRITE) # Place a image disk that is linked cloned from image. The size # should not be included in calculation. disk = Disk(new_id(), DISK_FLAVOR, False, True, 2048, image) vm = Vm(new_id(), VM_FLAVOR, State.STOPPED, None, None, [disk]) score, placement_list = manager.place(vm, None) assert_that(score.utilization, equal_to(97)) assert_that(score.transfer, equal_to(100)) assert_that(placement_list, has_length(2)) assert_that(placement_list[0].container_id, equal_to("datastore_id_1")) assert_that(placement_list[0].resource_id, equal_to(vm.id)) assert_that(placement_list[1].container_id, equal_to("datastore_id_1")) assert_that(placement_list[1].resource_id, equal_to(disk.id)) # Try the same place with constraints constraint = ResourceConstraint(ResourceConstraintType.DATASTORE, ["datastore_id_1"]) disk = Disk(new_id(), DISK_FLAVOR, False, True, 2048, image, constraints=[constraint]) vm = Vm(new_id(), VM_FLAVOR, State.STOPPED, None, None, [disk]) score, placement_list = manager.place(vm, None) assert_that(score.utilization, equal_to(97)) assert_that(score.transfer, equal_to(100)) assert_that(placement_list, has_length(2)) assert_that(placement_list[0].container_id, equal_to("datastore_id_1")) assert_that(placement_list[0].resource_id, equal_to(vm.id)) assert_that(placement_list[1].container_id, equal_to("datastore_id_1")) assert_that(placement_list[1].resource_id, equal_to(disk.id))
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]
def get_resource(self, disk_id): datastore = self.get_datastore(disk_id) if datastore is None: raise DiskNotFoundException(disk_id) size = os.path.getsize(self._disk_path(datastore, disk_id)) resource = Disk(disk_id) resource.flavor = Flavor("default") resource.persistent = False resource.new_disk = False # Every one byte in fake world equals to one G byte in real world resource.capacity_gb = size resource.image = None resource.datastore = datastore return resource
def test_place_vm_in_no_image_datastore(self): ds_map = { "datastore_id_1": (DatastoreInfo(8 * 1024, 0), set([])), "datastore_id_2": (DatastoreInfo(8 * 1024, 0), set([])), "datastore_id_3": (DatastoreInfo(16 * 1024, 0), set([])) } ds_with_image = ["datastore_id_1", "datastore_id_2"] manager = PMBuilder(ds_map=ds_map, ds_with_image=ds_with_image, image_size=100 * 1024 * 1024).build() total_storage = sum(t[0].total for t in ds_map.values()) image = DiskImage("disk_image", DiskImage.COPY_ON_WRITE) # disk1 and disk2 both take 1/100 disk1 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100, image) disk2 = Disk(new_id(), DISK_FLAVOR, False, True, total_storage / 100) vm = Vm(new_id(), self._vm_flavor_mb(1), VmPowerState.STOPPED, None, None, [disk1, disk2]) score, placement_list = manager.place(vm, None) # disk1 and disk2 both take 1% space, so utilization score is 98 # image takes 100MB, which is 10% of 1GB, so transfer score is 90 assert_that(score, is_(AgentPlacementScore(98, 90))) assert_that(placement_list, has_length(3)) for placement in placement_list: assert_that(placement.container_id, is_("datastore_id_3")) # no other datastores except only image datastore available, # and use_image_datastore_for_vms:False, thus should fail. ds_map = {"image_datastore": (DatastoreInfo(8 * 1024, 0), set())} manager = PMBuilder(ds_map=ds_map, im_ds_for_vm=False).build() # vm with disks self.assertRaises(NoSuchResourceException, manager.place, vm, None) # vm without disks vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED) self.assertRaises(NoSuchResourceException, manager.place, vm, None)
def test_place_disks_with_threshold(self, threshold, expected): ds_map = {"datastore_id_1": (DatastoreInfo(1 * 1024, 0), set([]))} manager = PMBuilder(ds_map=ds_map).build() manager.FREESPACE_THRESHOLD = threshold disk1 = Disk(new_id(), DISK_FLAVOR, False, True, 512, None) disk_list = [disk1] vm = Vm(new_id(), VM_FLAVOR, VmPowerState.STOPPED, None, None, disk_list) disks = None score, placement_list = manager.place(vm, disks) # optimal placement cannot be achieved, # divide the expected score by NOT_OPTIMAL_DIVIDE_FACTOR assert_that(score, is_(AgentPlacementScore(expected, 100)))
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]
def test_constraint_place_engine_cannot_fit(self): # Create constraint place engine image_datastore = "ds1" image_datastores = [{"name": image_datastore, "used_for_vms": True}] option = PlacementOption(1, 1, image_datastores) ds_map = {image_datastore: DatastoreInfo(5, 0)} ds_mgr = self.create_datastore_manager(ds_map, image_datastore) engine = ConstraintDiskPlaceEngine(ds_mgr, option) ds = engine.placeable_datastores() selector = DatastoreSelector.init_datastore_selector(ds_mgr, ds) # Try place constraint = ResourceConstraint(ResourceConstraintType.DATASTORE, ["ds1"]) disk = Disk(disk_id="ds1", capacity_gb=6) place_result = engine.place(DisksPlacement([disk], selector), [constraint]) # Verify place result assert_that(place_result.result, equal_to(PlaceResultCode.NOT_ENOUGH_DATASTORE_CAPACITY))
def _get_resource_from_vmcache(self, vmcache): """Translate to vm resource from vm cache """ vm_resource = Vm(vmcache.name) vm_resource.flavor = Flavor("default") # TODO vm_resource.disks = [] for disk in vmcache.disks: disk_id = os.path.splitext(os.path.basename(disk))[0] datastore_name = self._get_datastore_name_from_ds_path(disk) datastore_uuid = self._get_datastore_uuid(datastore_name) if datastore_uuid: disk_resource = Disk(disk_id, Flavor("default"), False, False, -1, None, datastore_uuid) vm_resource.disks.append(disk_resource) vm_resource.state = vmcache.power_state datastore_name = self._get_datastore_name_from_ds_path(vmcache.path) vm_resource.datastore = self._get_datastore_uuid(datastore_name) return vm_resource
def create_disks(self, disk_sizes): return [Disk(capacity_gb=disk_size) for disk_size in disk_sizes]