KVM-test: Add a subtest: nic_hotplug
PATCH 1/2 KVM-test: Add a subtest: nic_hotplug
From: Amos Kong <[email protected]>
Original nic hotplug test only adds a netdev to guest, but it doesn't add a nic device to guest and check the network status of new interface.
This test only works with latest qemu-kvm.
Changes from v1:
* Style fixes
* Don't sleep before erroring out the test
* Don't close serial session
Signed-off-by Jason Wang <[email protected]>
Signed-off-by Amos Kong <[email protected]>
--- client/tests/kvm/tests/nic_hotplug.py | 144 ++++++++++++++++++++++++++++++++ client/tests/kvm/tests_base.cfg.sample | 7 ++- 2 files changed, 150 insertions(+), 1 deletions(-) create mode 100644 client/tests/kvm/tests/nic_hotplug.py
client/tests/kvm/tests/nic_hotplug.py new file +144 -0
@@ -0,0 +1,144 @@ +import logging, os, commands, re, time +from autotest_lib.client.common_lib import error +import kvm_subprocess, kvm_test_utils, kvm_utils, kvm_vm + + +def run_nic_hotplug(test, params, env): + """ + Test hotplug of NIC devices + + 1) Boot up guest with one nic + 2) Add a host network device through monitor cmd and check if it's added + 3) Add nic device through monitor cmd and check if it's added + 4) Check if new interface gets ip address + 5) Disable primary link of guest + 6) Ping guest new ip from host + 7) Delete nic device and netdev + 8) Re-enable primary link of guest + + @param test: KVM test object. + @param params: Dictionary with the test parameters. + @param env: Dictionary with test environment. + """ + vm = kvm_test_utils.get_living_vm(env, params.get("main_vm")) + timeout = int(params.get("login_timeout", 360)) + guest_delay = int(params.get("guest_delay", 20)) + session_serial = kvm_test_utils.wait_for_login(vm, timeout=timeout, + serial=True) + + # Modprobe the module if specified in config file + module = params.get("modprobe_module") + if module: + session_serial.get_command_output("modprobe %s" % module) + + def netdev_add(vm): + netdev_id = kvm_utils.generate_random_id() + attach_cmd = ("netdev_add tap,id=%s,script=%s" % + (netdev_id, kvm_utils.get_path(vm.root_dir, + params.get("nic_script")))) + netdev_extra_params = params.get("netdev_extra_params") + if netdev_extra_params: + attach_cmd += ",%s" % netdev_extra_params + logging.info("Adding netdev through %s" % attach_cmd) + vm.monitor.cmd(attach_cmd) + + network = vm.monitor.info("network") + if netdev_id not in network: + logging.error(network) + raise error.TestError("Fail to add netdev: %s" % netdev_id) + else: + return netdev_id + + def netdev_del(vm, id): + vm.monitor.cmd("netdev_del %s" % id) + + network = vm.monitor.info("network") + if id in network: + logging.error(network) + raise error.TestError("Fail to remove netdev %s" % id) + + def nic_add(vm, model, netdev_id, mac): + """ + Add a nic to virtual machine + + @vm: VM object + @model: nic model + @netdev_id: id of netdev + @mac: Mac address of new nic + """ + id = kvm_utils.generate_random_id() + if model=="virtio": model="virtio-net-pci" + device_add_cmd = "device_add %s,netdev=%s,mac=%s,id=%s" % (model, + netdev_id, + mac, id) + logging.info("Adding nic through %s" % device_add_cmd) + vm.monitor.cmd(device_add_cmd) + + qdev = vm.monitor.info("qtree") + if id not in qdev: + logging.error(qdev) + raise error.TestFail("Device %s was not plugged into qdev" + "tree" % id) + else: + return id + + def nic_del(vm, id, wait=True): + """ + Remove the nic from pci tree. + + @vm: VM object + @id: the nic id + @wait: Whether need to wait for the guest to unplug the device + """ + nic_del_cmd = "device_del %s" % id + vm.monitor.cmd(nic_del_cmd) + if wait: + logging.info("waiting for the guest to finish the unplug") + if not kvm_utils.wait_for(lambda: id not in + vm.monitor.info("qtree"), + guest_delay, 5 ,1): + logging.error(vm.monitor.info("qtree")) + raise error.TestError("Device is not unplugged by " + "guest, please check whether the " + "hotplug module was loaded in guest"); + + logging.info("Attach a virtio nic to vm") + mac = kvm_utils.generate_mac_address(vm.instance, 1) + if not mac: + mac = "00:00:02:00:00:02" + netdev_id = netdev_add(vm) + device_id = nic_add(vm, "virtio", netdev_id, mac) + + if "Win" not in params.get("guest_name", ""): + session_serial.sendline("dhclient %s &" % + kvm_test_utils.get_linux_ifname(session_serial, mac)) + + logging.info("Shutting down the primary link") + vm.monitor.cmd("set_link %s down" % vm.netdev_id[0]) + + try: + logging.info("Waiting for new nic's ip address acquisition...") + if not kvm_utils.wait_for(lambda: (vm.address_cache.get(mac) is + not None), 10, 1): + raise error.TestFail("Could not get ip address of new nic") + ip = vm.address_cache.get(mac) + if not kvm_utils.verify_ip_address_ownership(ip, mac): + raise error.TestFail("Could not verify the ip address of new nic") + else: + logging.info("Got the ip address of new nic: %s" % ip) + + logging.info("Ping test the new nic ...") + s, o = kvm_test_utils.ping(ip, 100) + if s != 0: + logging.error(o) + raise error.TestFail("New nic failed ping test") + + logging.info("Detaching a virtio nic from vm") + nic_del(vm, device_id) + netdev_del(vm,netdev_id) + + finally: + vm.free_mac_address(1) + logging.info("Re-enabling the primary link") + vm.monitor.cmd("set_link %s up" % vm.netdev_id[0]) +
client/tests/kvm/tests_base.cfg.sample +6 -1
@@ -528,7 +528,6 @@ variants: relogin_timeout = 240 - nic_hotplug: install setup unattended_install.cdrom - type = pci_hotplug pci_type = nic reference_cmd = lspci find_pci_cmd = 'lspci | tail -n1'
@@ -544,6 +543,12 @@ variants: - nic_e1000: pci_model = e1000 match_string = "Gigabit Ethernet Controller" + variants: + - default: + type = pci_hotplug + - additional: + type = nic_hotplug + - block_hotplug: install setup unattended_install.cdrom type = pci_hotplug
Signature · 1 line
1.7.2.3
PATCH 2/2 KVM-test: Add set_link subtest
From: Amos Kong <[email protected]>
This test is used to check if packages are transmit into guest while put down/up guest link in monitor. The ping test can only pass when link is up. do file transfer test at the end of this test.
Changes from v1:
* Style corrections
Signed-off-by Amos Kong <[email protected]>
--- client/tests/kvm/tests/set_link.py | 60 ++++++++++++++++++++++++++++++++ client/tests/kvm/tests_base.cfg.sample | 8 ++++ 2 files changed, 68 insertions(+), 0 deletions(-) create mode 100644 client/tests/kvm/tests/set_link.py
client/tests/kvm/tests/set_link.py new file +60 -0
@@ -0,0 +1,60 @@ +import logging +from autotest_lib.client.common_lib import error +from tests import file_transfer +import kvm_test_utils + + +def run_set_link(test, params, env): + """ + KVM guest link test: + 1) Boot up guest with one nic + 2) Ping guest from host + 3) Disable guest link and ping guest from host + 4) Re-enable guest link and ping guest from host + 5) Do file transfer test + + @param test: kvm test object + @param params: Dictionary with the test parameters + @param env: Dictionary with test environment. + """ + vm = kvm_test_utils.get_living_vm(env, params.get("main_vm")) + timeout = float(params.get("login_timeout", 360)) + session = kvm_test_utils.wait_for_login(vm, 0, timeout, 0, 2) + + ip = vm.get_address(0) + linkname = vm.netdev_id[0] + + logging.info("Pinging guest from host") + s, o = kvm_test_utils.ping(ip, count=10, timeout=20) + if s != 0: + raise error.TestFail("Ping failed, status: %s, output: %s" % (s, o)) + ratio = kvm_test_utils.get_loss_ratio(o) + if ratio != 0: + raise error.TestFail("Loss ratio is %s, output: %s" % (ratio, o)) + + logging.info("Executing 'set link %s off'" % linkname) + vm.monitor.cmd("set_link %s off" % linkname) + logging.info(vm.monitor.info("network")) + logging.info("Pinging guest from host") + s, o = kvm_test_utils.ping(ip, count=10, timeout=20) + if s == 0: + raise error.TestFail("Ping unexpectedly succeeded, status: %s," + "output: %s" % (s, o)) + ratio = kvm_test_utils.get_loss_ratio(o) + if ratio != 100: + raise error.TestFail("Loss ratio is not 100%," + "Loss ratio is %s" % ratio) + + logging.info("Executing 'set link %s on'" % linkname) + vm.monitor.cmd("set_link %s on" % linkname) + logging.info(vm.monitor.info("network")) + logging.info("Pinging guest from host") + s, o = kvm_test_utils.ping(ip, count=10, timeout=20) + if s != 0: + raise error.TestFail("Ping failed, status: %s, output: %s" % (s, o)) + ratio = kvm_test_utils.get_loss_ratio(o) + if ratio != 0: + raise error.TestFail("Loss ratio is %s, output: %s" % (ratio, o)) + + file_transfer.run_file_transfer(test, params, env) + session.close()
client/tests/kvm/tests_base.cfg.sample +8 -0
@@ -678,6 +678,14 @@ variants: transfer_type = remote kill_vm = yes + - set_link: + type = set_link + test_timeout = 1000 + filesize = 4000 + transfer_timeout = 1000 + transfer_type = remote + kill_vm =yes + - physical_resources_check: install setup unattended_install.cdrom type = physical_resources_check catch_uuid_cmd = dmidecode | awk -F: '/UUID/ {print $2}'
Signature · 1 line
1.7.2.3