def get_balloon_n_memstatkeys(period): """ Set period and then get memballoon device's xml and output keys of virsh dommemstat. :param period: value of period :return: tuple of memballoon xml and output keys of virsh dommemstat """ virsh.dommemstat(vm_name, '--period %s' % period, **VIRSH_ARGS) vmxml_p = vm_xml.VMXML.new_from_dumpxml(vm_name) balloon_p = vmxml_p.get_devices('memballoon')[0] logging.debug('memballon xml of period %s is :\n%s', period, balloon_p) memstat_keys_p = get_dommemstat_keys( virsh.dommemstat(vm_name, **VIRSH_ARGS).stdout_text) return balloon_p, memstat_keys_p
def run_test_dommemstat(case): """ Test virsh command dommemstat related cases :param case: test case """ if case == 'disk_caches': # Verify dommemstat show right disk cache for RHEL8 guest # Update memballoon device balloon_dict = { k: v for k, v in params.items() if k.startswith('membal_') } libvirt.update_memballoon_xml(vmxml, balloon_dict) logging.debug(virsh.dumpxml(vm_name).stdout_text) vm.start() session = vm.wait_for_login() # Get info from virsh dommemstat command dommemstat_output = virsh.dommemstat( vm_name, debug=True).stdout_text.strip() dommemstat = {} for line in dommemstat_output.splitlines(): k, v = line.strip().split(' ') dommemstat[k] = v # Get info from vm meminfo_keys = ['Buffers', 'Cached', 'SwapCached'] meminfo = { k: utils_misc.get_mem_info(session, k) for k in meminfo_keys } # from kernel commit: Buffers + Cached + SwapCached = disk_caches tmp_sum = meminfo['Buffers'] + meminfo['Cached'] + meminfo[ 'SwapCached'] logging.info('Buffers %d + Cached %d + SwapCached %d = %d kb', meminfo['Buffers'], meminfo['Cached'], meminfo['SwapCached'], tmp_sum) # Compare and make sure error is within allowable range logging.info('disk_caches is %s', dommemstat['disk_caches']) allow_error = int(params.get('allow_error', 15)) actual_error = (tmp_sum - int(dommemstat['disk_caches'])) / tmp_sum * 100 logging.debug('Actual error: %.2f%%', actual_error) if actual_error > allow_error: test.fail('Buffers + Cached + SwapCached (%d) ' 'should be close to disk_caches (%s). ' 'Allowable error: %.2f%%' % (tmp_sum, dommemstat['disk_caches'], allow_error))
def execute_statistics_command(params): """ Execute statistics command :param params: dict, used to setup the connection """ vm_name = params.get("migrate_main_vm") disk_type = params.get("disk_type") vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) disks = vmxml.get_disk_all_by_expr('type==%s' % disk_type, 'device==disk') logging.debug("disks: %s", disks) debug_kargs = {'ignore_status': False, 'debug': True} for disk in list(disks.values()): disk_source = disk.find('source').get('dev') disk_target = disk.find('target').get('dev') logging.debug("disk_source: %s", disk_source) logging.debug("disk_target: %s", disk_target) virsh.domblkstat(vm_name, disk_target, "", **debug_kargs) virsh.domblkinfo(vm_name, disk_source, **debug_kargs) virsh.domstats(vm_name, **debug_kargs) virsh.dommemstat(vm_name, **debug_kargs)
def run(test, params, env): """ Test command: virsh dommemstat. The command gets memory statistics for a domain 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh dommemstat operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) domid = vm.get_id() domuuid = vm.get_uuid() status_error = params.get("status_error", "no") vm_ref = params.get("dommemstat_vm_ref", "name") libvirtd = params.get("libvirtd", "on") extra = params.get("dommemstat_extra", "") if libvirtd == "off": utils_libvirtd.libvirtd_stop() # run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.count("_invalid_"): # vm_ref names parameter to fetch vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s" % vm_name if libvirtd == "off": utils_libvirtd.libvirtd_stop() if vm_ref != "remote": status = virsh.dommemstat(vm_ref, extra, ignore_status=True, debug=True).exit_status else: remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): test.cancel("local/remote ip parameters not set.") status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s dommemstat %s %s" % (remote_uri, vm_name, extra) status = session.cmd_status(command, internal_timeout=5) session.close() except process.CmdError: status = 1 # recover libvirtd service start if libvirtd == "off": utils_libvirtd.libvirtd_start() # check status_error if status_error == "yes": if status == 0: test.fail("Run successfully with wrong command!") elif status_error == "no": if status != 0: test.fail("Run failed with right command")
def run_test_period(case): """ Present memballoon statistic period in live xml :param case: test case """ def get_dommemstat_keys(output): """ Get output keys of virsh dommemstat :param output: output of virsh dommemstat command :return: keys of the output """ return set( [line.split()[0] for line in output.strip().splitlines()]) def get_balloon_n_memstatkeys(period): """ Set period and then get memballoon device's xml and output keys of virsh dommemstat. :param period: value of period :return: tuple of memballoon xml and output keys of virsh dommemstat """ virsh.dommemstat(vm_name, '--period %s' % period, **VIRSH_ARGS) vmxml_p = vm_xml.VMXML.new_from_dumpxml(vm_name) balloon_p = vmxml_p.get_devices('memballoon')[0] logging.debug('memballon xml of period %s is :\n%s', period, balloon_p) memstat_keys_p = get_dommemstat_keys( virsh.dommemstat(vm_name, **VIRSH_ARGS).stdout_text) return balloon_p, memstat_keys_p if case == 'memstat': # Start a guest with memballoon element, it's automatically added. # Wait for vm to fully bootup to run test vm.wait_for_login().close() # Check the memballoon xml and dommemstat balloon_list = vmxml.get_devices('memballoon') if not balloon_list: test.error('There is no memballoon device in vmxml.') balloon = balloon_list[0] logging.debug('memballoon after vm started: \n%s', balloon) memstat_keys = get_dommemstat_keys( virsh.dommemstat(vm_name, **VIRSH_ARGS).stdout_text) # Set period to 2 p2 = 2 # Check the xml and the dommemstat balloon_p2, memstat_keys_p2 = get_balloon_n_memstatkeys(p2) if balloon_p2.xmltreefile.find('stats') is None \ or balloon_p2.stats_period != str(p2): test.fail('Period of memballoon check failed, should be %s' % p2) # Set period to 0 p0 = 0 # Check the xml and the dommemstat balloon_p0, memstat_keys_p0 = get_balloon_n_memstatkeys(p0) stats_elem = balloon_p0.xmltreefile.find('stats') if stats_elem is not None: if stats_elem.get('period') is not None: test.fail('Period of memballoon check failed, ' 'there should not be period in memballoon xml' ' after setting period to 0') # Fail test if output keys are not consistent if not memstat_keys == memstat_keys_p2 == memstat_keys_p0: test.fail('Output keys of dommemstat changed during test.')
def _check_mem(memory_to_eat): dommemstat_output = virsh.dommemstat(vm_name).stdout_text.strip() actual_mem = re.search("actual (\d*)", dommemstat_output).groups()[0] logging.debug("actual_mem is {}".format(actual_mem)) return int(actual_mem) > int(memory_to_eat)
def run_virsh_dommemstat(test, params, env): """ Test command: virsh dommemstat. The command gets memory statistics for a domain 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh dommemstat operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) domid = vm.get_id() domuuid = vm.get_uuid() status_error = params.get("status_error", "no") vm_ref = params.get("dommemstat_vm_ref", "name") libvirtd = params.get("libvirtd", "on") extra = params.get("dommemstat_extra", "") if libvirtd == "off": libvirt_vm.libvirtd_stop() #run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.count("_invalid_"): # vm_ref names parameter to fetch vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s" % vm_name if libvirtd == "off": libvirt_vm.libvirtd_stop() if vm_ref != "remote": status = virsh.dommemstat(vm_ref, extra, ignore_status=True, debug=True).exit_status else: remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) local_ip = params.get("local_ip", "LOCAL.EXAMPLE.COM") if remote_ip.count("EXAMPLE.COM") or local_ip.count("EXAMPLE.COM"): raise error.TestNAError("local/remote ip parameters not set.") status = 0 try: remote_uri = libvirt_vm.complete_uri(local_ip) session = remote.remote_login("ssh", remote_ip, "22", "root", remote_pwd, "#") session.cmd_output('LANG=C') command = "virsh -c %s dommemstat %s %s" % (remote_uri, vm_name, extra) status = session.cmd_status(command, internal_timeout=5) session.close() except error.CmdError: status = 1 #recover libvirtd service start if libvirtd == "off": libvirt_vm.libvirtd_start() #check status_error if status_error == "yes": if status == 0: raise error.TestFail("Run successfully with wrong command!") elif status_error == "no": if status != 0: raise error.TestFail("Run failed with right command")
def run(test, params, env): """ Test virtio/virtio-transitional/virtio-non-transitional model of memory balloon :param test: Test object :param params: Dictionary with the test parameters :param env: Dictionary with test environment Test steps: 1. Prepareguest domain and balloon xml use one of virtio/virtio-non-transitional/virtio-transitional model 2. Start domain and check the device exist in guest 3. Save/Restore and check if guest works well 4. Set balloon memory and see if it works in guest """ vm_name = params.get("main_vm", "avocado-vt-vm1") vm = env.get_vm(params["main_vm"]) vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) backup_xml = vmxml.copy() guest_src_url = params.get("guest_src_url") virtio_model = params['virtio_model'] os_variant = params.get("os_variant", "") params["disk_model"] = virtio_model if not libvirt_version.version_compare(5, 0, 0): test.cancel("This libvirt version doesn't support " "virtio-transitional model.") # Download and replace image when guest_src_url provided if guest_src_url: image_name = params['image_path'] target_path = utils_misc.get_path(data_dir.get_data_dir(), image_name) if not os.path.exists(target_path): download.get_file(guest_src_url, target_path) params["blk_source_name"] = target_path try: # Update disk and interface to correct model if (os_variant == 'rhel6' or 'rhel6' in params.get("shortname")): iface_params = {'model': 'virtio-transitional'} libvirt.modify_vm_iface(vm_name, "update_iface", iface_params) libvirt.set_vm_disk(vm, params) # The local variable "vmxml" will not be updated since set_vm_disk # sync with another dumped xml inside the function vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) # Update memory balloon device to correct model membal_dict = { 'membal_model': virtio_model, 'membal_stats_period': '10' } libvirt.update_memballoon_xml(vmxml, membal_dict) if not vm.is_alive(): vm.start() is_windows_guest = (params['os_type'] == 'Windows') session = vm.wait_for_login() # Check memory statistic if libvirt_version.version_compare(6, 6, 0): if (os_variant != 'rhel6' or 'rhel6' not in params.get("shortname")): rs = virsh.dommemstat(vm_name, ignore_status=True, debug=True).stdout_text if "available" not in rs: test.fail("Can't get memory stats in %s model" % virtio_model) # Finish test for Windows guest if is_windows_guest: return # Check if memory balloon device exists on guest status = session.cmd_status_output('lspci |grep balloon')[0] if status != 0: test.fail("Didn't detect memory balloon device on guest.") # Save and restore guest sn_path = os.path.join(data_dir.get_tmp_dir(), os_variant) session.close() virsh.save(vm_name, sn_path) virsh.restore(sn_path) session = vm.wait_for_login() # Get original memory for later balloon function check ori_outside_mem = vm.get_max_mem() ori_guest_mem = vm.get_current_memory_size() # balloon half of the memory ballooned_mem = ori_outside_mem // 2 # Set memory to test balloon function virsh.setmem(vm_name, ballooned_mem) # Check if memory is ballooned successfully logging.info("Check memory status") unusable_mem = ori_outside_mem - ori_guest_mem gcompare_threshold = int( params.get("guest_compare_threshold", unusable_mem)) after_mem = vm.get_current_memory_size() act_threshold = ballooned_mem - after_mem if (after_mem > ballooned_mem) or (abs(act_threshold) > gcompare_threshold): test.fail("Balloon test failed") finally: vm.destroy() backup_xml.sync()
def run(test, params, env): """ Test command: virsh dommemstat. The command gets memory statistics for a domain 1.Prepare test environment. 2.When the ibvirtd == "off", stop the libvirtd service. 3.Perform virsh dommemstat operation. 4.Recover test environment. 5.Confirm the test result. """ vm_name = params.get("main_vm") vm = env.get_vm(params["main_vm"]) domid = vm.get_id() domuuid = vm.get_uuid() status_error = params.get("status_error", "no") vm_ref = params.get("dommemstat_vm_ref", "name") libvirtd = params.get("libvirtd", "on") extra = params.get("dommemstat_extra", "") if libvirtd == "off": utils_libvirtd.libvirtd_stop() # run test case if vm_ref == "id": vm_ref = domid elif vm_ref == "hex_id": vm_ref = hex(int(domid)) elif vm_ref == "uuid": vm_ref = domuuid elif vm_ref.count("_invalid_"): # vm_ref names parameter to fetch vm_ref = params.get(vm_ref) elif vm_ref == "name": vm_ref = "%s" % vm_name if libvirtd == "off": utils_libvirtd.libvirtd_stop() try: if vm_ref != "remote": status = virsh.dommemstat(vm_ref, extra, ignore_status=True, debug=True).exit_status else: remote_ip = params.get("remote_ip", "REMOTE.EXAMPLE.COM") remote_pwd = params.get("remote_pwd", None) remote_user = params.get("remote_user", "root") if remote_ip.count("EXAMPLE.COM"): test.cancel("remote ip parameters not set.") ssh_key.setup_ssh_key(remote_ip, remote_user, remote_pwd) remote_uri = "qemu+ssh://%s/system" % remote_ip virsh.start(vm_name, ignore_status=False, debug=True, uri=remote_uri) status = virsh.dommemstat(vm_name, ignore_status=True, debug=True, uri=remote_uri).exit_status finally: if vm_ref == "remote": virsh.destroy(vm_name, ignore_status=False, debug=True, uri=remote_uri) # recover libvirtd service start if libvirtd == "off": utils_libvirtd.libvirtd_start() # check status_error if status_error == "yes": if status == 0: test.fail("Run successfully with wrong command!") elif status_error == "no": if status != 0: test.fail("Run failed with right command")