def test_vnc_port(self): vmm = Fvm(MagicMock()) vm_id = str(uuid.uuid4()) flavor = Flavor("vm", [ QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.GB) ]) spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.create_vm(vm_id, spec) port = vmm.get_vnc_port(vm_id) assert_that(port, none()) expected = set(range(5900, 5905)) for p in expected: vm_id = str(uuid.uuid4()) spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.set_vnc_port(spec, p) vmm.create_vm(vm_id, spec) port = vmm.get_vnc_port(vm_id) assert_that(port, equal_to(p)) ports = vmm.get_occupied_vnc_ports() # The following 2 asserts test equality of ports and expected assert_that(len(ports), equal_to(len(expected))) assert_that(len(ports), equal_to(len(expected.union(ports))))
def _create_vm_spec(self, metadata, env): flavor = Flavor("default", [ QuotaLineItem("vm.memory", "256", Unit.MB), QuotaLineItem("vm.cpu", "1", Unit.COUNT), ]) spec = self.vm_manager.create_vm_spec("vm_id", "ds1", flavor, metadata, env) return spec
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 _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 from_thrift(thrift_object): flavor = Flavor.from_thrift(thrift_object.flavor_info) image = DiskImage.from_thrift(thrift_object.image) constraints = Disk.from_thrift_constraints(thrift_object) instance = Disk(thrift_object.id, flavor, thrift_object.persistent, thrift_object.new_disk, thrift_object.capacity_gb, image, None, constraints) if thrift_object.datastore: instance.datastore = thrift_object.datastore.id return instance
def _test_vminfo(self, vminfo): vm_id = self._vm_id() flavor = Flavor("vm", [QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.MB)]) datastore = self.vim_client.get_datastore().name spec = self.vm_manager.create_vm_spec(vm_id, datastore, flavor) self.vm_manager.set_vminfo(spec, vminfo) try: self.vm_manager.create_vm(vm_id, spec) got_metadata = self.vm_manager.get_vminfo(vm_id) assert_that(got_metadata, equal_to(vminfo)) finally: self.vm_manager.delete_vm(vm_id)
def test_mks_ticket(self, _exists): vm_id = self._vm_id() flavor = Flavor("vm", [QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.MB)]) datastore = self.vim_client.get_datastore().name spec = self.vm_manager.create_vm_spec(vm_id, datastore, flavor) try: self.vm_manager.create_vm(vm_id, spec) self.vm_manager.power_on_vm(vm_id) ticket = self.vm_manager.get_mks_ticket(vm_id) assert_that(ticket.cfg_file, not_none()) assert_that(ticket.ticket, not_none()) finally: self.vm_manager.power_off_vm(vm_id) self.vm_manager.delete_vm(vm_id)
def test_set_vnc_port(self): flavor = Flavor("default", [ QuotaLineItem("vm.memory", "256", Unit.MB), QuotaLineItem("vm.cpu", "1", Unit.COUNT), ]) spec = self.vm_manager.create_vm_spec("vm_id", "ds1", flavor) self.vm_manager.set_vnc_port(spec, 5901) options = [ o for o in spec.extraConfig if o.key == 'RemoteDisplay.vnc.enabled' ] assert_that(options[0].value, equal_to('True')) options = [ o for o in spec.extraConfig if o.key == 'RemoteDisplay.vnc.port' ] assert_that(options[0].value, equal_to(5901))
def from_thrift(thrift_object): instance = Vm( vm_id=thrift_object.id, flavor=Flavor.from_thrift(thrift_object.flavor_info), state=State(thrift_object.state), environment=thrift_object.environment, ) if thrift_object.disks: instance.disks = [Disk.from_thrift(d) for d in thrift_object.disks] if thrift_object.datastore: instance.datastore = thrift_object.datastore.id if thrift_object.resource_constraints: instance.resource_constraints = thrift_object.resource_constraints return instance
def test_vminfo(self): vmm = Fvm(MagicMock()) vm_id = str(uuid.uuid4()) flavor = Flavor("vm", [ QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.GB) ]) vm_metadata = { "project": "p1", "vendor": "v1", } spec = vmm.create_vm_spec(vm_id, "ds-1", flavor, image_id="image_id") vmm.set_vminfo(spec, vm_metadata) vmm.create_vm(vm_id, spec) got_metadata = vmm.get_vminfo(vm_id) assert_that(got_metadata, equal_to(vm_metadata))
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 from_thrift(thrift_object): instance = Vm( vm_id=thrift_object.id, flavor=Flavor.from_thrift(thrift_object.flavor_info), state=State(thrift_object.state), environment=thrift_object.environment, ) if thrift_object.disks: instance.disks = [Disk.from_thrift(d) for d in thrift_object.disks] if thrift_object.datastore: instance.datastore = thrift_object.datastore.id if thrift_object.resource_constraints: instance.resource_constraints = \ thrift_object.resource_constraints return instance
def test_vnc_ports(self, _exists): vm_id = self._vm_id() port = self._test_port() flavor = Flavor("vm", [QuotaLineItem("vm.cpu", 1, Unit.COUNT), QuotaLineItem("vm.memory", 8, Unit.MB)]) datastore = self.vim_client.get_datastore().name spec = self.vm_manager.create_vm_spec(vm_id, datastore, flavor) self.vm_manager.set_vnc_port(spec, port) try: self.vm_manager.create_vm(vm_id, spec) expected = self.vm_manager.get_vnc_port(vm_id) assert_that(expected, equal_to(port)) ports = self.vm_manager.get_occupied_vnc_ports() assert_that(ports, contains(port)) finally: self.vm_manager.delete_vm(vm_id)
def _create_vm_spec(self, metadata, env): """Test VM spec creation""" flavor = Flavor("default", [ QuotaLineItem("vm.memory", "256", Unit.MB), QuotaLineItem("vm.cpu", "1", Unit.COUNT), ]) create_spec_mock = MagicMock( wraps=self.vm_manager.vm_config.create_spec) self.vm_manager.vm_config.create_spec = create_spec_mock spec = self.vm_manager.create_vm_spec("vm_id", "ds1", flavor, metadata, env) create_spec_mock.assert_called_once_with("vm_id", "ds1", 256, 1, metadata, env) return spec
def from_thrift(thrift_object): flavor = Flavor.from_thrift(thrift_object.flavor_info) image = DiskImage.from_thrift(thrift_object.image) constraints = Disk.from_thrift_constraints(thrift_object) instance = Disk( thrift_object.id, flavor, thrift_object.persistent, thrift_object.new_disk, thrift_object.capacity_gb, image, None, constraints, ) if thrift_object.datastore: instance.datastore = thrift_object.datastore.id return instance
def _vm_flavor_mb(self, memory_mb, cpu=1): return Flavor("default", [ QuotaLineItem("vm.memory", str(memory_mb), Unit.MB), QuotaLineItem("vm.cpu", str(cpu), Unit.COUNT), ])
from gen.resource.ttypes import ResourceConstraintType from gen.resource.ttypes import ResourceConstraint from host.placement.placement_manager import AgentPlacementScore from host.placement.placement_manager import PlacementManager from host.placement.placement_manager import PlacementOption from host.placement.placement_manager import NoSuchResourceException from host.placement.placement_manager import NotEnoughCpuResourceException from host.placement.placement_manager import NotEnoughDatastoreCapacityException from host.placement.placement_manager import NotEnoughMemoryResourceException from host.hypervisor.resources import Disk from host.hypervisor.resources import DiskImage from host.hypervisor.resources import VmPowerState from host.hypervisor.resources import Vm from host.hypervisor.system import DatastoreInfo DISK_FLAVOR = Flavor("default", []) VM_FLAVOR = Flavor("default", [ QuotaLineItem("vm.memory", "2", Unit.GB), QuotaLineItem("vm.cpu", "1", Unit.COUNT), ]) class TestPlacementManager(unittest.TestCase): @parameterized.expand([ # d1, d2, oc, (score) (512, 512, 1.0, (87, 100)), # disk score dominates 1-(0.5k+0.5k)/8k (1, 1, 1.0, (97, 100)), # memory score dominates 1-2k/64k (512, 512, 2.0, (87, 100)), # disk score dominates 1-(0.5k+0.5k)/8k (1, 1, 2.0, (98, 100)), # memory score dominates 1-2k/(64k*2) ]) def test_place_new(self, disk_capacity_1, disk_capacity_2, overcommit,