On 11/09/2010 04:46 PM, Lucas Meneghel Rodrigues wrote:
> Changes from v2:
> - Do not close the serial session on tests that require it
> - Instead of an infinite loop, have a thread that changes
> nic promisc mode in nic_promisc. It is more reliable
> and avoids garbage on the serial session.
> - On mac_change, open a remote session in the beginning
> of the test to check for responsiveness (the previous
> version was checking responsiveness of the serial session,
> which didn't make sense.
>
> Changes from v1:
> - Convert all net tests to use new function
> - Update debug info
>
> Signed-off-by: Amos Kong <[email protected]>
> Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>
> ---
> client/tests/kvm/kvm_test_utils.py | 15 +++++++---
> client/tests/kvm/tests/mac_change.py | 18 +++++-------
> client/tests/kvm/tests/nic_promisc.py | 41
> +++++++++++++++++++---------
> client/tests/kvm/tests/nicdriver_unload.py | 11 +++-----
> 4 files changed, 51 insertions(+), 34 deletions(-)
>
> diff --git a/client/tests/kvm/kvm_test_utils.py
> b/client/tests/kvm/kvm_test_utils.py
> index 014f265..11e3e58 100644
> --- a/client/tests/kvm/kvm_test_utils.py
> +++ b/client/tests/kvm/kvm_test_utils.py
> @@ -44,7 +44,7 @@ def get_living_vm(env, vm_name):
> return vm
>
>
> -def wait_for_login(vm, nic_index=0, timeout=240, start=0, step=2):
> +def wait_for_login(vm, nic_index=0, timeout=240, start=0, step=2,
> serial=None):
> """
> Try logging into a VM repeatedly. Stop on success or when timeout
> expires.
>
> @@ -53,9 +53,16 @@ def wait_for_login(vm, nic_index=0, timeout=240, start=0,
> step=2):
> @param timeout: Time to wait before giving up.
> @return: A shell session object.
> """
> - logging.info("Trying to log into guest '%s', timeout %ds", vm.name,
> timeout)
> - session = kvm_utils.wait_for(lambda:
> vm.remote_login(nic_index=nic_index),
> - timeout, start, step)
> + if serial:
> + logging.info("Trying to log into guest using serial connection,"
> + " timeout %ds", timeout)
> + session = kvm_utils.wait_for(lambda: vm.serial_login(), timeout,
> + start, step)
> + else:
> + logging.info("Trying to log into guest %s using remote connection,"
> + " timeout %ds", vm.name, timeout)
> + session = kvm_utils.wait_for(lambda: vm.remote_login(
> + nic_index=nic_index), timeout, start, step)
> if not session:
> raise error.TestFail("Could not log into guest '%s'" % vm.name)
> logging.info("Logged into guest '%s'" % vm.name)
> diff --git a/client/tests/kvm/tests/mac_change.py
> b/client/tests/kvm/tests/mac_change.py
> index c614e15..605d29d 100644
> --- a/client/tests/kvm/tests/mac_change.py
> +++ b/client/tests/kvm/tests/mac_change.py
> @@ -17,12 +17,10 @@ def run_mac_change(test, params, env):
> """
> timeout = int(params.get("login_timeout", 360))
> vm = kvm_test_utils.get_living_vm(env, params.get("main_vm"))
> - logging.info("Trying to log into guest '%s' by serial", vm.name)
> - session = kvm_utils.wait_for(lambda: vm.serial_login(),
> - timeout, 0, step=2)
> - if not session:
> - raise error.TestFail("Could not log into guest '%s'" % vm.name)
> -
> + session_serial = kvm_test_utils.wait_for_login(vm, 0, timeout, 0, 2,
> + serial=True)
> + # This session will be used to assess whether the IP change worked
> + session = kvm_test_utils.wait_for_login(vm, 0, timeout, 0, 2)
> old_mac = vm.get_mac_address(0)
> while True:
> vm.free_mac_address(0)
> @@ -30,23 +28,23 @@ def run_mac_change(test, params, env):
> if old_mac != new_mac:
> break
> logging.info("The initial MAC address is %s", old_mac)
> - interface = kvm_test_utils.get_linux_ifname(session, old_mac)
> + interface = kvm_test_utils.get_linux_ifname(session_serial, old_mac)
> # Start change MAC address
> logging.info("Changing MAC address to %s", new_mac)
> change_cmd = ("ifconfig %s down && ifconfig %s hw ether %s && "
> "ifconfig %s up" % (interface, interface, new_mac,
> interface))
> - if session.get_command_status(change_cmd) != 0:
> + if session_serial.get_command_status(change_cmd) != 0:
> raise error.TestFail("Fail to send mac_change command")
>
> # Verify whether MAC address was changed to the new one
> logging.info("Verifying the new mac address")
> - if session.get_command_status("ifconfig | grep -i %s" % new_mac) != 0:
> + if session_serial.get_command_status("ifconfig |grep -i %s" % new_mac)
> != 0:
> raise error.TestFail("Fail to change MAC address")
>
> # Restart `dhclient' to regain IP for new mac address
> logging.info("Restart the network to gain new IP")
> dhclient_cmd = "dhclient -r && dhclient %s" % interface
> - session.sendline(dhclient_cmd)
> + session_serial.sendline(dhclient_cmd)
>
> # Re-log into the guest after changing mac address
> if kvm_utils.wait_for(session.is_responsive, 120, 20, 3):
> diff --git a/client/tests/kvm/tests/nic_promisc.py
> b/client/tests/kvm/tests/nic_promisc.py
> index 99bbf8c..a8eaa39 100644
> --- a/client/tests/kvm/tests/nic_promisc.py
> +++ b/client/tests/kvm/tests/nic_promisc.py
> @@ -1,4 +1,4 @@
> -import logging
> +import logging, threading
> from autotest_lib.client.common_lib import error
> from autotest_lib.client.bin import utils
> import kvm_utils, kvm_test_utils
> @@ -21,12 +21,8 @@ def run_nic_promisc(test, params, env):
> timeout = int(params.get("login_timeout", 360))
> vm = kvm_test_utils.get_living_vm(env, params.get("main_vm"))
> session = kvm_test_utils.wait_for_login(vm, timeout=timeout)
> -
> - logging.info("Trying to log into guest '%s' by serial", vm.name)
> - session2 = kvm_utils.wait_for(lambda: vm.serial_login(),
> - timeout, 0, step=2)
> - if not session2:
> - raise error.TestFail("Could not log into guest '%s'" % vm.name)
> + session_serial = kvm_test_utils.wait_for_login(vm, 0, timeout, 0, 2,
> + serial=True)
>
> def compare(filename):
> cmd = "md5sum %s" % filename
> @@ -46,11 +42,28 @@ def run_nic_promisc(test, params, env):
> return True
>
> ethname = kvm_test_utils.get_linux_ifname(session, vm.get_mac_address(0))
> - set_promisc_cmd = ("ip link set %s promisc on; sleep 0.01;"
> - "ip link set %s promisc off; sleep 0.01" %
> - (ethname, ethname))
> - logging.info("Set promisc change repeatedly in guest")
> - session2.sendline("while true; do %s; done" % set_promisc_cmd)
> +
> + class ThreadPromiscCmd(threading.Thread):
> + def __init__(self, session, termination_event):
> + self.session = session
> + self.termination_event = termination_event
> + super(ThreadPromiscCmd, self).__init__()
> +
> +
> + def run(self):
> + set_promisc_cmd = ("ip link set %s promisc on; sleep 0.01;"
> + "ip link set %s promisc off; sleep 0.01" %
> + (ethname, ethname))
> + while True:
> + self.session.get_command_output(set_promisc_cmd)
> + if self.termination_event.isSet():
> + break
> +
> +
> + logging.info("Started thread to change promisc mode in guest")
> + termination_event = threading.Event()
> + promisc_thread = ThreadPromiscCmd(session_serial, termination_event)
> + promisc_thread.start()
A slightly shorter alternative:
def promisc_cmd_loop(session, termination_event):
set_promisc_cmd = ("ip link set %s promisc on; sleep 0.01;"
"ip link set %s promisc off; sleep 0.01" %
(ethname, ethname))
while True:
self.session.get_command_output(set_promisc_cmd)
if self.termination_event.isSet():
break
...
promisc_thread = threading.Thread(target=promisc_cmd_loop,
args=(session_serial, termination_event)
promisc_thread.start()
> dd_cmd = "dd if=/dev/urandom of=%s bs=%d count=1"
> filename = "/tmp/nic_promisc_file"
> @@ -93,8 +106,10 @@ def run_nic_promisc(test, params, env):
> session.get_command_status(cmd)
>
> finally:
> + logging.info("Stopping the promisc thread")
> + termination_event.set()
> + promisc_thread.join(10)
> logging.info("Restore the %s to the nonpromisc mode", ethname)
> - session2.close()
> session.get_command_status("ip link set %s promisc off" % ethname)
> session.close()
>
> diff --git a/client/tests/kvm/tests/nicdriver_unload.py
> b/client/tests/kvm/tests/nicdriver_unload.py
> index 47318ba..242b22f 100644
> --- a/client/tests/kvm/tests/nicdriver_unload.py
> +++ b/client/tests/kvm/tests/nicdriver_unload.py
> @@ -20,11 +20,8 @@ def run_nicdriver_unload(test, params, env):
> timeout = int(params.get("login_timeout", 360))
> vm = kvm_test_utils.get_living_vm(env, params.get("main_vm"))
> session = kvm_test_utils.wait_for_login(vm, timeout=timeout)
> - logging.info("Trying to log into guest '%s' by serial", vm.name)
> - session2 = kvm_utils.wait_for(lambda: vm.serial_login(),
> - timeout, 0, step=2)
> - if not session2:
> - raise error.TestFail("Could not log into guest '%s'" % vm.name)
> + session_serial = kvm_test_utils.wait_for_login(vm, 0, timeout, 0, 2,
> + serial=True)
>
> ethname = kvm_test_utils.get_linux_ifname(session, vm.get_mac_address(0))
> sys_path = "/sys/class/net/%s/device/driver" % (ethname)
> @@ -77,7 +74,8 @@ def run_nicdriver_unload(test, params, env):
> logging.info("Unload/load NIC driver repeatedly in guest...")
> while True:
> logging.debug("Try to unload/load nic drive once")
> - if session2.get_command_status(unload_load_cmd, timeout=120) !=
> 0:
> + if session_serial.get_command_status(unload_load_cmd,
> + timeout=120) != 0:
> session.get_command_output("rm -rf /tmp/Thread-*")
> raise error.TestFail("Unload/load nic driver failed")
> pid, s = os.waitpid(pid, os.WNOHANG)
> @@ -96,7 +94,6 @@ def run_nicdriver_unload(test, params, env):
> t.join(timeout = scp_timeout)
> os._exit(0)
>
> - session2.close()
>
> try:
> logging.info("Check MD5 hash for received files in multi-session")
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest