def to_thrift(self): disks = [disk.to_thrift() for disk in self.disks] resource_constraints = self.resource_constraints thrift_state = getattr(ThriftState, self.state.name) thrift_vm = ThriftVm( self.id, self.flavor.name, thrift_state, None, self.environment, disks, self.flavor.to_thrift(), resource_constraints, self.tenant_id, self.project_id, ) if self.datastore: thrift_vm.datastore = ThriftDatastore(id=self.datastore, name=self.datastore_name) return thrift_vm
def createVmResource(image, network): disk = Disk() disk.flavor = "some-disk-flavor" disk.id = str(uuid.uuid4()) disk.persistent = False disk.new_disk = True disk.capacity_gb = 0 disk.image = DiskImage() disk.image.id = image disk.image.clone_type = CloneType.COPY_ON_WRITE disk.flavor_info = Flavor() disk.flavor_info.name = "some-disk-flavor" disk.flavor_info.cost = [] vm = Vm() vm.id = str(uuid.uuid4()) vm.flavor = "some-vm-flavor" vm.state = VmPowerState.STOPPED vm.flavor_info = Flavor() vm.flavor_info.name = "some-vm-flavor" vm.flavor_info.cost = [ QuotaLineItem("vm.cpu", "1", QuotaUnit.COUNT), QuotaLineItem("vm.memory", "0.5", QuotaUnit.GB) ] vm.disks = [disk] if network: vm.resource_constraints = [ ResourceConstraint(ResourceConstraintType.NETWORK, [network]) ] return vm
def _sample_vm(self): flavor = Flavor(name="flavor", cost=[QuotaLineItem("a", "b", 1)]) disks = [ Disk(stable_uuid("%s-1" % id), flavor, False, False, 1, datastore=Datastore("ds1")), Disk(stable_uuid("%s-2" % id), flavor, False, False, 1, datastore=Datastore("ds2")), ] vm = Vm() vm.id = id vm.flavor = "flavor" vm.flavor_info = flavor vm.disks = disks vm.state = State().STARTED vm.datastore = Datastore("ds1") return vm
def _place_request_with_params(self, ratio, min_fanout, max_fanout): place_params = PlaceParams() place_params.fanoutRatio = ratio place_params.minFanoutCount = min_fanout place_params.maxFanoutCount = max_fanout place_request = PlaceRequest(resource=Resource(Vm(), [Disk()]), leafSchedulerParams=place_params) return place_request
def vm_resource(): cost = [ QuotaLineItem("vm.cpu", "1", QuotaUnit.COUNT), QuotaLineItem("vm.memory", "32", 2)] return Vm(id=new_id(), flavor="default", flavor_info=Flavor(name="default", cost=cost), state=VmPowerState.STOPPED)
def create_resource(self, id): flavor = Flavor(name="flavor", cost=[QuotaLineItem("a", "b", 1)]) disks = [ Disk(stable_uuid("%s-1" % id), flavor, False, False, 1, datastore=Datastore("ds1")), Disk(stable_uuid("%s-2" % id), flavor, False, False, 1, datastore=Datastore("ds2")), ] vm = Vm() vm.id = id vm.flavor = "flavor" vm.flavor_info = flavor vm.state = State().STARTED vm.datastore = Datastore("ds1") resource = Resource(vm, disks) return resource
def createVmResource(image, network): disk = Disk() disk.flavor = "some-disk-flavor" disk.id = str(uuid.uuid4()) disk.persistent = False disk.new_disk = True disk.capacity_gb = 0 disk.image = DiskImage() disk.image.id = image disk.image.clone_type = CloneType.COPY_ON_WRITE disk.flavor_info = Flavor() disk.flavor_info.name = "some-disk-flavor" disk.flavor_info.cost = [] vm = Vm() vm.id = str(uuid.uuid4()) vm.flavor = "some-vm-flavor" vm.state = VmPowerState.STOPPED vm.flavor_info = Flavor() vm.flavor_info.name = "some-vm-flavor" vm.flavor_info.cost = [ QuotaLineItem("vm.cpu", "1", QuotaUnit.COUNT), QuotaLineItem("vm.memory", "0.5", QuotaUnit.GB), ] vm.disks = [disk] if network: vm.resource_constraints = [ResourceConstraint(ResourceConstraintType.NETWORK, [network])] return vm
def createVmResource(image): disk = Disk() disk.flavor = "some-disk-flavor" disk.id = str(uuid.uuid1()) disk.persistent = False disk.new_disk = True disk.capacity_gb = 0 disk.image = DiskImage() disk.image.id = image disk.image.clone_type = CloneType.COPY_ON_WRITE disk.flavor_info = Flavor() disk.flavor_info.name = "some-disk-flavor" disk.flavor_info.cost = [] vm = Vm() vm.id = str(uuid.uuid1()) vm.flavor = "some-vm-flavor" vm.state = State.STOPPED vm.flavor_info = Flavor() vm.flavor_info.name = "some-vm-flavor" vm.flavor_info.cost = [ QuotaLineItem("vm.cpu", "1", QuotaUnit.COUNT), QuotaLineItem("vm.memory", "0.5", QuotaUnit.GB) ] vm.disks = [disk] return vm
def vm_create(): cost = [ QuotaLineItem("vm.cpu", "1", QuotaUnit.COUNT), QuotaLineItem("vm.memory", "32", 2)] return Vm(id=new_id(), flavor="default", flavor_info=Flavor(name="default", cost=cost), state=VmPowerState.STOPPED, tenant_id="t1", project_id="p1")
def to_thrift(self): disks = [ disk.to_thrift() for disk in self.disks ] resource_constraints = self.resource_constraints thrift_vm = ThriftVm( self.id, self.flavor.name, self.state, None, self.environment, disks, self.flavor.to_thrift(), resource_constraints, self.tenant_id, self.project_id, self.location_id ) if self.datastore: thrift_vm.datastore = ThriftDatastore(id=self.datastore, name=self.datastore_name) return thrift_vm
def createVmResource(image): disk = Disk() disk.flavor = "some-disk-flavor" disk.id = str(uuid.uuid1()) disk.persistent = False disk.new_disk = True disk.capacity_gb = 0 disk.image = DiskImage() disk.image.id = image disk.image.clone_type = CloneType.COPY_ON_WRITE disk.flavor_info = Flavor() disk.flavor_info.name = "some-disk-flavor" disk.flavor_info.cost = [] vm = Vm() vm.id = str(uuid.uuid1()) vm.flavor = "some-vm-flavor" vm.state = State.STOPPED vm.flavor_info = Flavor() vm.flavor_info.name = "some-vm-flavor" vm.flavor_info.cost = [ QuotaLineItem("vm.cpu", "1", QuotaUnit.COUNT), QuotaLineItem("vm.memory", "0.5", QuotaUnit.GB)] vm.disks = [disk] return vm
def to_thrift(self): disks = [disk.to_thrift() for disk in self.disks] resource_constraints = self.resource_constraints thrift_state = getattr(ThriftState, self.state.name) thrift_vm = ThriftVm( self.id, self.flavor.name, thrift_state, None, self.environment, disks, self.flavor.to_thrift(), resource_constraints, ) if self.datastore: thrift_vm.datastore = ThriftDatastore(id=self.datastore, name=self.datastore_name) return thrift_vm
def test_disk_ops_vm_suspended(self): """ Attach/detach a disk from a vm when vm is suspended """ req = VmDisksDetachRequest(vm_id="vm.id", disk_ids=["disk.id"]) handler = HostHandler(MagicMock()) handler.hypervisor = MagicMock() vm = Vm(id="vm.id", state=VmState.SUSPENDED) handler.hypervisor.vm_manager.get_resource.return_value = vm response = handler.attach_disks(req) assert_that(response.result, equal_to(VmDiskOpResultCode.INVALID_VM_POWER_STATE)) response = handler.detach_disks(req) assert_that(response.result, equal_to(VmDiskOpResultCode.INVALID_VM_POWER_STATE))
def _sample_vm(self): flavor = Flavor(name="flavor", cost=[QuotaLineItem("a", "b", 1)]) disks = [ Disk(stable_uuid("%s-1" % id), flavor, False, False, 1, datastore=Datastore("ds1")), Disk(stable_uuid("%s-2" % id), flavor, False, False, 1, datastore=Datastore("ds2")), ] vm = Vm() vm.id = id vm.flavor = "flavor" vm.flavor_info = flavor vm.disks = disks vm.state = VmPowerState().STARTED vm.datastore = Datastore("ds1") return vm
def _place_request(self): return PlaceRequest(resource=Resource(Vm(), [Disk()]))
def __init__(self, requests_file): self.requests = [] content = open(Universe.get_path(requests_file), 'r').read() requests = yaml.load(content) request_id = 1 disk_id = 1 if 'auto' in requests: requests = self.generate_requests(requests['auto']) for request in requests: place_request = PlaceRequest() resource = Resource() resource.disks = [] env_info = {} if 'vm' in request: resource.vm = Vm() # Make the vm id look like a uuid by zero-padding. Otherwise # reference counting doesn't work. resource.vm.id = "{0:032d}".format(request_id) resource.vm.state = State.STARTED flavor = Universe.vm_flavors[request['vm']['flavor']] resource.vm.flavor = flavor.name resource.vm.flavor_info = flavor.to_thrift() resource.vm.disks = [] if 'constraints' in request: constraints = [] for c in request['constraints']: constraint = ResourceConstraint() constraint.type = RCT._NAMES_TO_VALUES[c['type']] constraint.values = c['values'] if 'negative' in c: constraint.negative = c['negative'] else: constraint.negative = False constraints.append(constraint) if constraints: resource.vm.resource_constraints = constraints if 'load' in request['vm']: env_info['mem_load'] = request['vm']['load']['mem'] if 'disks' in request: for d in request['disks']: disk = Disk() flavor = Universe.ephemeral_disk_flavors[d['flavor']] disk.flavor = flavor.name disk.flavor_info = flavor.to_thrift() disk.id = str(disk_id) disk.persistent = False disk.new_disk = True disk.capacity_gb = 1024 # hard coded in FakeVmManager disk_id += 1 resource.vm.disks.append(disk) place_request.resource = resource tracing_info = TracingInfo() tracing_info.request_id = request_id place_request.tracing_info = tracing_info request_id += 1 self.requests.append(PsimVmRequest(place_request, env_info))
def test_reserve_vm(self): disk_ids = ["disk_id_1", "disk_id_2", "disk_id_3"] datastore_ids = ["datastore_1", "datastore_2", "datastore_3"] disk_flavor = "disk_flavor_1" networks = ["net_1", "net_2"] vm_flavor = "vm_flavor_1" vm_id = "vm_id_1" def reserve_vm_validate(vm, disks): assert_that(vm) assert_that(not disks) assert_that(vm.id, equal_to(vm_id)) # Check VM placement vm_placement = vm.placement assert_that(vm_placement is not None) assert_that(vm_placement.type is ResourcePlacementType.VM) assert_that(vm_placement.resource_id, equal_to(vm_id)) assert_that(vm_placement.container_id, equal_to(datastore_ids[0])) # Check VM networks vm_networks = vm.networks assert_that(vm_networks is not None) assert_that(set(networks), equal_to(set(vm_networks))) disks = vm.disks assert_that(len(disks) is 3) disk_index = 0 for vm_disk in disks: assert_that(vm_disk.id, equal_to(disk_ids[disk_index])) assert_that(vm_disk.placement is not None) disk_placement = vm_disk.placement assert_that(disk_placement.type is ResourcePlacementType.DISK) assert_that(disk_placement.resource_id, equal_to(vm_disk.id)) assert_that(disk_placement.container_id, equal_to(datastore_ids[disk_index])) disk_index += 1 return "reservation_id" handler = HostHandler(MagicMock()) mocked_reserve = MagicMock() mocked_reserve.side_effect = reserve_vm_validate handler.hypervisor.placement_manager = MagicMock() handler.hypervisor.placement_manager.reserve = mocked_reserve placements = [] # Add VM placement info placement = ResourcePlacement() placement.type = ResourcePlacementType.VM placement.resource_id = vm_id placement.container_id = datastore_ids[0] placements.append(placement) # Add Network placement info : net_1 placement = ResourcePlacement() placement.type = ResourcePlacementType.NETWORK placement.resource_id = vm_id placement.container_id = networks[0] placements.append(placement) # Add Network placement info : net_2 placement = ResourcePlacement() placement.type = ResourcePlacementType.NETWORK placement.resource_id = vm_id placement.container_id = networks[1] placements.append(placement) # Add disks placement info index = 0 for disk_id in disk_ids: placement = ResourcePlacement() placement.type = ResourcePlacementType.DISK placement.container_id = datastore_ids[index] index += 1 placement.resource_id = disk_id placements.append(placement) placement_list = ResourcePlacementList(placements) disk_flavor_info = Flavor(name=disk_flavor, cost=[QuotaLineItem("size", "1", 1)]) disks = [] for disk_id in disk_ids: disk = Disk(id=disk_id, flavor=disk_flavor, persistent=True, new_disk=True, capacity_gb=2, flavor_info=disk_flavor_info) disks.append(disk) vm_flavor_info = Flavor(name=vm_flavor, cost=[QuotaLineItem("cpu", "1", 5)]) vm = Vm(vm_id, vm_flavor, State.STOPPED, None, None, disks, vm_flavor_info) request = ReserveRequest() request.generation = 1 request.resource = Resource(vm=vm, disks=None, placement_list=placement_list) response = handler.reserve(request) assert_that(response.result, equal_to(ReserveResultCode.OK)) # test reserve under entering-maintenance-mode and maintenance mode state = common.services.get(ServiceName.MODE) state.set_mode(MODE.ENTERING_MAINTENANCE) request = MagicMock() response = handler.reserve(request) assert_that(response.result, equal_to(ReserveResultCode.OPERATION_NOT_ALLOWED)) state.set_mode(MODE.MAINTENANCE) response = handler.reserve(request) assert_that(response.result, equal_to(ReserveResultCode.OPERATION_NOT_ALLOWED))