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_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_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_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_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_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_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_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_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 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)