def test_write_uses_alternative_root_tag(): """ Checks that the generic `write` method uses the alternative root tag if provided. """ vm = types.Vm() result = Writer.write(vm, root='list') assert_equals(result, '<list/>')
def test_write_does_not_require_xml_writer(): """ Checks that the generic `write` method doesn't require an XML writer paramater. """ vm = types.Vm() result = Writer.write(vm) assert_equals(result, '<vm/>')
def test_remove_vm2_lease(api_v4): engine = api_v4.system_service() vm2_service = test_utils.get_vm_service(engine, VM2_NAME) vm2_service.update( vm=types.Vm(high_availability=types.HighAvailability(enabled=False, ), lease=types.StorageDomainLease(storage_domain=None))) testlib.assert_true_within_short(lambda: vm2_service.get().lease is None)
def boot_device(self, vmname, bootdevice): if bootdevice == "network": vm = self.find_vm(vmname) vm_service = self.vms_service.vm_service(vm.id) vm_service.update(vm=types.Vm(os=types.OperatingSystem( boot=types.Boot(devices=[types.BootDevice.NETWORK])))) if bootdevice == "hd": vm = self.find_vm(vmname) vm_service = self.vms_service.vm_service(vm.id) vm_service.update(vm=types.Vm(os=types.OperatingSystem( boot=types.Boot(devices=[types.BootDevice.HD])))) if bootdevice == "optical": vm = self.find_vm(vmname) vm_service = self.vms_service.vm_service(vm.id) vm_service.update(vm=types.Vm(os=types.OperatingSystem( boot=types.Boot(devices=[types.BootDevice.CDROM])))) return
def test_add_vm_from_scratch_with_clone_parameter(self): """ Test when adding clone vm from scratch the query with this parameter is sent. """ self.server.set_xml_response("vms", 201, "<vm/>") self.vms_service.add_from_scratch(types.Vm(), clone=True) assert_equal(self.server.last_request_query, 'clone=true')
def hotplug_cpu(api): engine = api.system_service() vms_service = engine.vms_service() vm_service = test_utils.get_vm_service(engine, VM0_NAME) new_cpu = vm_service.get().cpu new_cpu.topology.sockets = 2 vm_service.update(vm=types.Vm(cpu=new_cpu)) nt.assert_true(vm_service.get().cpu.topology.sockets == 2)
def editVMMEM(self, vmname, memsize): vms_service = self.connection.system_service().vms_service() vm = vms_service.list(search='name=%s' % vmname)[0] vm_service = vms_service.vm_service(vm.id) vm_service.update( types.Vm( memory=memsize * 2**30, # 更新虚拟机的内存大小 ), )
def add_vm(pname, pcluster, ptemplate): print('Adding VM : ' + pname + '...') vms_service.add( types.Vm( name=pname, cluster=types.Cluster(name=pcluster, ), template=types.Template(name=ptemplate, ), ), )
def start_vm_with_pxe(api, options): """Add PXE Boot option to vm.""" search_name = "name=" + options['vm_name'] vms_service = api.system_service().vms_service() vm = vms_service.list(search=search_name)[0] vm_service = vms_service.vm_service(vm.id) vm_service.start(vm=types.Vm(os=types.OperatingSystem(boot=types.Boot( devices=[types.BootDevice.HD, types.BootDevice.NETWORK]))))
def test_write_accepts_list_with_one_element(): """ Checks that the generic `write` method accets lists with one element. """ vm = types.Vm() result = Writer.write([vm], root='list') assert_equals(result, '<list><vm/></list>')
def event(self, vm, severity, description): self.events_service.add(event=types.Event( vm=types.Vm(id=vm.id, ), origin=self.config["application_name"], severity=severity, custom_id=self.event_id, description=description, ), ) self.event_id += 1
def test_write_accepts_elements_of_different_types(): """ Checks that the generic `write` method accets lists containing elements of different types. """ vm = types.Vm() disk = types.Disk() result = Writer.write([vm, disk], root='list') assert_equals(result, '<list><vm/><disk/></list>')
def test_hotplug_memory(prefix): api_v4 = prefix.virt_env.engine_vm().get_api_v4() engine = api_v4.system_service() vm_service = test_utils.get_vm_service(engine, VM0_NAME) new_memory = vm_service.get().memory + 256 * MB with test_utils.TestEvent(engine, 2039): # HOT_SET_MEMORY(2,039) vm_service.update(vm=types.Vm(memory=new_memory)) assert vm_service.get().memory == new_memory assert_vm0_is_alive(prefix)
def rename_vm(self, vm: types.Vm, new_name: str): """ Изменение имени ВМ :param vm: ВМ :param new_name: Новое имя """ vm_service = self._connection.system_service().vms_service( ).vm_service(vm.id) vm_service.update(types.Vm(name=new_name))
def rename_vm(self, vm_name, new_vm_name): vm_service = self._get_vm_service(vm_name) try: vm_service.update(types.Vm(name=new_vm_name)) except Exception as e: self.logger.exception(e) return vm_name else: return new_vm_name
def add_vm_blank(api): # Get the vms service vms_service=api.system_service().vms_service() #Create VM from blank template vm_memory=256*MB vm=types.Vm( name=VM0_NAME, memory=vm_memory, type=types.VmType.SERVER, os=types.OperatingSystem( type='other_linux', boot=types.Boot( devices=[types.BootDevice.HD, types.BootDevice.NETWORK] ), ), high_availability=types.HighAvailability( enabled=False ), cluster=types.Cluster( name=TEST_CLUSTER ), template=types.Template( name=TEMPLATE_BLANK ), display=types.Display( smartcard_enabled=True, keyboard_layout='en-us', file_transfer_enabled=True, copy_paste_enabled=True ), memory_policy=types.MemoryPolicy( guaranteed=vm_memory//2 ) ) #Add this VM vm=vms_service.add(vm) #Check that VM was added vm_service=vms_service.vm_service(vm.id) testlib.assert_true_within_short( lambda: vm_service.get().status == types.VmStatus.DOWN ) #Add another VM vm.id=None vm.name=VM1_NAME vm.initialization=None vm=vms_service.add(vm) #Check that the second VM was added vm_service=vms_service.vm_service(vm.id) testlib.assert_true_within_short( lambda: vm_service.get().status == types.VmStatus.DOWN )
def test_add_vm_with_global_header(self): """ Test that adding a VM with global header a request is sent with that header. """ connection = self.server.connection(headers={'my': 'value'}) vms_service = connection.system_service().vms_service() self.server.set_xml_response("vms", 201, "<vm/>") vms_service.add(types.Vm()) assert_equal(self.server.last_request_headers.get('my'), 'value')
def test_add_vm_with_clone_and_clone_permissions_parameters(self): """ Test when adding vm clone and clone_permissions the query with those parameters is sent. """ self.server.set_xml_response("vms", 201, "<vm/>") self.vms_service.add(types.Vm(), clone=True, clone_permissions=True) assert_equal(self.server.last_request_query, 'clone=true&clone_permissions=true')
def build_entity(self): return otypes.Template( id=self._module.params['id'], name=self._module.params['name'], cluster=otypes.Cluster( name=self._module.params['cluster'] ) if self._module.params['cluster'] else None, vm=otypes.Vm( name=self._module.params['vm'] ) if self._module.params['vm'] else None, description=self._module.params['description'], cpu_profile=otypes.CpuProfile( id=search_by_name( self._connection.system_service().cpu_profiles_service(), self._module.params['cpu_profile'], ).id ) if self._module.params['cpu_profile'] else None, display=otypes.Display( smartcard_enabled=self.param('smartcard_enabled') ) if self.param('smartcard_enabled') is not None else None, os=otypes.OperatingSystem( type=self.param('operating_system'), ) if self.param('operating_system') else None, memory=convert_to_bytes( self.param('memory') ) if self.param('memory') else None, soundcard_enabled=self.param('soundcard_enabled'), usb=( otypes.Usb(enabled=self.param('usb_support')) ) if self.param('usb_support') is not None else None, sso=( otypes.Sso( methods=[otypes.Method(id=otypes.SsoMethod.GUEST_AGENT)] if self.param('sso') else [] ) ) if self.param('sso') is not None else None, time_zone=otypes.TimeZone( name=self.param('timezone'), ) if self.param('timezone') else None, version=otypes.TemplateVersion( base_template=self._get_base_template(), version_name=self.param('version').get('name'), ) if self.param('version') else None, memory_policy=otypes.MemoryPolicy( guaranteed=convert_to_bytes(self.param('memory_guaranteed')), ballooning=self.param('ballooning_enabled'), max=convert_to_bytes(self.param('memory_max')), ) if any(( self.param('memory_guaranteed'), self.param('ballooning_enabled'), self.param('memory_max') )) else None, io=otypes.Io( threads=self.param('io_threads'), ) if self.param('io_threads') is not None else None, initialization=self.get_initialization(), )
def add_serial_console_vm2(api): engine = api.system_service() # Find the virtual machine. Note the use of the `all_content` parameter, it is # required in order to obtain additional information that isn't retrieved by # default, like the configuration of the serial console. vm = engine.vms_service().list(search='name={}'.format(VM2_NAME), all_content=True)[0] if not vm.console.enabled: vm_service = test_utils.get_vm_service(engine, VM2_NAME) vm_service.update(types.Vm(console=types.Console(enabled=True)))
def test_add_vm_with_global_header_overridden(self): """ Test that adding a VM with global header set and a request header set, the header is overridden by request header. """ connection = self.server.connection(headers={'my': 'value'}) vms_service = connection.system_service().vms_service() self.server.set_xml_response("vms", 201, "<vm/>") vms_service.add(types.Vm(), headers={'my': 'overridden'}) assert_equal(self.server.last_request_headers.get('my'), 'overridden')
def start_vm(self, vm_service, cloud_init=True, scontent=None): vm_service.start( use_cloud_init=cloud_init, vm=types.Vm( initialization=types.Initialization(custom_script=scontent, ))) #if vm no start down while True: break
def clone_snapshot_to_vm(vms_service, vm, snap): counter = 0 snapshots_service = vms_service.vm_service(vm.id).snapshots_service() new_vm_name = '{}_{}_{}'.format(vm.name, VM_MIDDLE, datetime.now().strftime('%Y%m%d_%H%M%S')) # It can happen, that the snapshot state gives ok, # but the snapshot is still not free for cloning/deleting. # For this case we have to wait a bit more. sleep(120) check_snapshot(snap, snapshots_service, True) logger.info("[{}] Create VM clone from snapshot...".format(vm.name)) try: vm_clone = vms_service.add( vm=types.Vm(name=new_vm_name, snapshots=[types.Snapshot(id=snap.id)], cluster=types.Cluster(name=CLUSTER))) except sdk.Error as oerr: logger.error(oerr) send_mail(oerr) return None cloned_vm_service = vms_service.vm_service(vm_clone.id) created = True # check if cloned vm is down, # what means that the cloning process has been completed while vm_clone.status != types.VmStatus.DOWN: if counter >= MAX_OPERATION_TIME: logger.error("[{}] Creating VM clone from snapshot failed".format( vm.name)) send_mail("Creating VM clone from snapshot failed!\n" + "No backup for: <b>{}</b> at: <b>{}</b>".format( vm.name, datetime.now().strftime('%H:%M:%S'))) created = None break sleep(5) vm_clone = cloned_vm_service.get() counter + 5 if created: logger.info("[{}] Creating VM clone from snapshot completed!".format( vm.name)) # is allways good to sleep a bit :) sleep(2) return vm_clone else: return None
def next_run_unplug_cpu(api): vm_service = test_utils.get_vm_service(api.system_service(), VM0_NAME) new_cpu = vm_service.get().cpu new_cpu.topology.sockets = 1 vm_service.update(vm=types.Vm(cpu=new_cpu, ), next_run=True) nt.assert_true(vm_service.get().cpu.topology.sockets == 2) nt.assert_true(vm_service.get(next_run=True).cpu.topology.sockets == 1) vm_service.reboot() testlib.assert_true_within_long( lambda: vm_service.get().status == types.VmStatus.UP) nt.assert_true(vm_service.get().cpu.topology.sockets == 1)
def test_add_vm_with_two_custom_parameters(self): """ Test that adding a VM with two parameters a request is sent with that two parameters. """ self.server.set_xml_response("vms", 201, "<vm/>") self.vms_service.add(types.Vm(), query={ 'my': 'value', 'your': 'value' }) assert_equal(self.server.last_request_query, 'my=value&your=value')
def set_vm_cpu_shares(self, vm: types.Vm, cpu_sockets: int): """ Установить количество ядер у ВМ :param vm: ВМ :param cpu_sockets: Количество ядер """ vm_service = self._connection.system_service().vms_service( ).vm_service(vm.id) vm_service.update( types.Vm(cpu=types.Cpu(topology=types.CpuTopology( sockets=cpu_sockets))))
def editVMCPU(self, vmname, core=2, socket=1): # 虚拟机的CPU在虚拟机看到的数量lscpu由 core 和 socket 组成 vms_service = self.connection.system_service().vms_service() vm = vms_service.list(search='name=%s' % vmname)[0] vm_service = vms_service.vm_service(vm.id) vm_service.update( types.Vm(cpu=types.Cpu( topology=types.CpuTopology( cores=core, # 更新 虚拟机的 cpu core sockets=socket, ), # 更新 虚拟机的cpu socket )))
def test_write_accepts_xml_writer(): """ Checks that the generic `write` method accepts an XML writer as parameter. """ vm = types.Vm() writer = XmlWriter(None) result = Writer.write(vm, target=writer) text = writer.string() writer.close() assert_is_none(result) assert_equals(text, '<vm/>')
def test_add_vm_with_two_custom_headers(self): """ Test that adding a VM with two headers a request is sent with that two headers. """ self.server.set_xml_response("vms", 201, "<vm/>") self.vms_service.add(types.Vm(), headers={ 'my': 'value', 'your': 'value' }) assert_equal(self.server.last_request_headers.get('my'), 'value') assert_equal(self.server.last_request_headers.get('your'), 'value')
def build_entity(self): return otypes.Template( name=self._module.params['name'], cluster=otypes.Cluster(name=self._module.params['cluster']) if self._module.params['cluster'] else None, vm=otypes.Vm(name=self._module.params['vm']) if self._module.params['vm'] else None, description=self._module.params['description'], cpu_profile=otypes.CpuProfile(id=search_by_name( self._connection.system_service().cpu_profiles_service(), self._module.params['cpu_profile'], ).id) if self._module.params['cpu_profile'] else None, )