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 _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 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 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 test_load_ephemeral_disk_flavors(self): file = os.path.join(self.test_dir, 'ephemeral-disk.yml') cmd = LoadFlavorCmd(file) cmd.run() assert_that(len(Universe.ephemeral_disk_flavors), equal_to(2)) assert_that(Universe.ephemeral_disk_flavors['core-100'], not_none()) assert_that(Universe.ephemeral_disk_flavors['core-200'], not_none()) flavor = Universe.ephemeral_disk_flavors['core-100'] assert_that(len(flavor.cost), equal_to(3)) assert_that(flavor.cost['ephemeral-disk'], equal_to(QuotaLineItem('ephemeral-disk', "1.0", Unit.COUNT))) assert_that(flavor.cost['ephemeral-disk.flavor.core-100'], equal_to(QuotaLineItem('ephemeral-disk.flavor.core-100', "1.0", Unit.COUNT))) assert_that(flavor.cost['ephemeral-disk.cost'], equal_to(QuotaLineItem('ephemeral-disk.cost', "1.0", Unit.COUNT)))
def test_load_vm_flavors(self): file = os.path.join(self.test_dir, 'vm.yml') cmd = LoadFlavorCmd(file) cmd.run() assert_that(len(Universe.vm_flavors), equal_to(2)) assert_that(Universe.vm_flavors['core-10'], not_none()) assert_that(Universe.vm_flavors['core-100'], not_none()) flavor = Universe.vm_flavors['core-10'] assert_that(len(flavor.cost), equal_to(5)) assert_that(flavor.cost['vm'], equal_to(QuotaLineItem('vm', "1.0", Unit.COUNT))) assert_that(flavor.cost['vm.flavor.core-10'], equal_to(QuotaLineItem('vm.flavor.core-10', "1.0", Unit.COUNT))) assert_that(flavor.cost['vm.cpu'], equal_to(QuotaLineItem('vm.cpu', "1.0", Unit.COUNT))) assert_that(flavor.cost['vm.memory'], equal_to(QuotaLineItem('vm.memory', "32.0", Unit.MB))) assert_that(flavor.cost['vm.cost'], equal_to(QuotaLineItem('vm.cost', "0.025", Unit.COUNT)))
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 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, expected): created_disk1 = Disk(new_id(), DISK_FLAVOR, True, True,
def test_convert(self): item = QuotaLineItem("foo", "1024", Unit.MB) assert_that(int(item.convert(Unit.B)), is_(1024 * 1024 * 1024)) assert_that(int(item.convert(Unit.KB)), is_(1024 * 1024)) assert_that(int(item.convert(Unit.MB)), is_(1024)) assert_that(int(item.convert(Unit.GB)), is_(1))