From 8d70873949b39e979e560d3107e39764cf3121dd Mon Sep 17 00:00:00 2001 From: nanli Date: Wed, 20 Sep 2023 14:06:18 +0800 Subject: [PATCH] add case for mem balloon autodeflate VIRT-299026: Memory balloon autodeflate config Signed-off-by: nanli --- .../mem_balloon_autodeflate.cfg | 30 +++++ .../memory_balloon/mem_balloon_autodeflate.py | 111 ++++++++++++++++++ spell.ignore | 1 + 3 files changed, 142 insertions(+) create mode 100644 libvirt/tests/cfg/memory/memory_balloon/mem_balloon_autodeflate.cfg create mode 100644 libvirt/tests/src/memory/memory_balloon/mem_balloon_autodeflate.py diff --git a/libvirt/tests/cfg/memory/memory_balloon/mem_balloon_autodeflate.cfg b/libvirt/tests/cfg/memory/memory_balloon/mem_balloon_autodeflate.cfg new file mode 100644 index 0000000000..aa29005715 --- /dev/null +++ b/libvirt/tests/cfg/memory/memory_balloon/mem_balloon_autodeflate.cfg @@ -0,0 +1,30 @@ +- memory.balloon.autodeflate: + type = mem_balloon_autodeflate + start_vm = no + set_mem = 1945600 + mem_unit = "KiB" + current_mem_unit = "KiB" + current_mem = "2097152" + mem_value = "2097152" + dominfo_check = "Max memory:(\s+)${mem_value} KiB\nUsed memory:(\s+)${set_mem} KiB" + variants: + - virtio_model: + memballoon_model = "virtio" + - virtio_trans_model: + memballoon_model = "virtio-transitional" + - virtio_non_trans_model: + memballoon_model = "virtio-non-transitional" + variants: + - autodeflate_undefined: + autodeflate = '' + auto_attr = "" + - autodeflate_off: + autodeflate = "off" + auto_attr = ",'autodeflate':'${autodeflate}'" + - autodeflate_on: + autodeflate = "on" + auto_attr = ",'autodeflate':'${autodeflate}'" + device_dict = "{'model':'${memballoon_model}' ${auto_attr}}" + variants: + - memory_allocation: + mem_attrs = {'memory_unit':'${mem_unit}','memory':${mem_value},'current_mem':${current_mem},'current_mem_unit':'${current_mem_unit}'} diff --git a/libvirt/tests/src/memory/memory_balloon/mem_balloon_autodeflate.py b/libvirt/tests/src/memory/memory_balloon/mem_balloon_autodeflate.py new file mode 100644 index 0000000000..22687afa9d --- /dev/null +++ b/libvirt/tests/src/memory/memory_balloon/mem_balloon_autodeflate.py @@ -0,0 +1,111 @@ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Copyright Redhat +# +# SPDX-License-Identifier: GPL-2.0 + +# Author: Nannan Li +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +from virttest import virsh + +from virttest.libvirt_xml import vm_xml +from virttest.utils_test import libvirt +from virttest.libvirt_xml.devices import memballoon +from virttest.staging import utils_memory + + +def run(test, params, env): + """ + Verify the autodeflate takes effect with various memory balloon models. + + Scenario: + 1.memory balloon models: virtio, virtio-transitional, virtio-non-transitional. + 2.autodeflate: on, off, default. + 3.mem config: mem, current mem. + """ + def get_memtotal_vm_internal(): + """ + Get memTotal value + + :return: mem_total, the value of 'cat /proc/meminfo | grep MemTotal' + """ + session = vm.wait_for_login() + mem_total = utils_memory.memtotal(session) + session.close() + + return mem_total + + def run_test(): + """ + Define and start guest + Check memTotal and memory allocation + """ + test.log.info("TEST_STEP1: Define guest") + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) + + vmxml.del_device('memballoon', by_tag=True) + mem_balloon = memballoon.Memballoon() + mem_balloon.setup_attrs(**device_dict) + vmxml.devices = vmxml.devices.append(mem_balloon) + + vmxml.setup_attrs(**mem_attrs) + vmxml.sync() + + test.log.info("TEST_STEP2: Start guest ") + if not vm.is_alive(): + vm.start() + vmxml = vm_xml.VMXML.new_from_dumpxml(vm_name) + test.log.debug("After define vm, get vmxml is:\n%s", vmxml) + + test.log.info("TEST_STEP3: Login guest and get the total guest memory") + init_mem_total = get_memtotal_vm_internal() + + test.log.info("TEST_STEP4: Change guest current memory allocation") + result = virsh.setmem(domain=vm_name, size=set_mem, debug=True) + libvirt.check_exit_status(result) + + test.log.info("TEST_STEP5: Login guest and get the total guest memory") + second_mem_total = get_memtotal_vm_internal() + + if autodeflate == "on": + if second_mem_total != init_mem_total: + test.fail("Total guest memory should not changed from '%d' to " + "'%d'" % (init_mem_total, second_mem_total)) + else: + if init_mem_total - second_mem_total != mem_value - set_mem: + test.fail("The difference between two memTotal values: '%d-%d'" + " should be '%d'" % (init_mem_total, second_mem_total, + mem_value - set_mem)) + test.log.debug("Get twice correct memTotal:'%d' and '%d'", + init_mem_total, second_mem_total) + + test.log.info("TEST_STEP6: Check the memory allocation") + dominfo = virsh.dominfo(vm_name, ignore_status=True, debug=True) + libvirt.check_result(dominfo, expected_match=dominfo_check) + + def teardown_test(): + """ + Clean data. + """ + test.log.info("TEST_TEARDOWN: Clean up env.") + bkxml.sync() + + vm_name = params.get("main_vm") + vm = env.get_vm(vm_name) + vmxml = vm_xml.VMXML.new_from_inactive_dumpxml(vm_name) + bkxml = vmxml.copy() + + mem_attrs = eval(params.get("mem_attrs", "{}")) + device_dict = eval(params.get("device_dict", "{}")) + autodeflate = params.get("autodeflate", '') + mem_value = int(params.get("mem_value")) + set_mem = int(params.get("set_mem")) + dominfo_check = params.get("dominfo_check") + + try: + run_test() + + finally: + teardown_test() diff --git a/spell.ignore b/spell.ignore index 3a9a7eee8e..91ec5bb1a8 100644 --- a/spell.ignore +++ b/spell.ignore @@ -31,6 +31,7 @@ augpath ausearch auth Auth +autodeflate autodestroy autogenerated autologin