Exemplo n.º 1
0
def run(test, params, env):
    """
    Test command: virsh iface-edit <interface>

    Edit interface start mode in this case.
    """
    iface_name = params.get("iface_name", "lo")
    status_error = "yes" == params.get("status_error", "no")
    if not libvirt.check_iface(iface_name, "exists", "--all"):
        raise error.TestError("Interface '%s' not exists" % iface_name)
    # get the interface script independent of distro
    iface_script = utils_net.get_network_cfg_file(iface_name)
    iface_script_bk = os.path.join(test.tmpdir, "iface-%s.bk" % iface_name)
    net_iface = utils_net.Interface(name=iface_name)
    iface_is_up = net_iface.is_up()
    old_ifstart_mode = get_ifstart_mode(iface_name)
    if not old_ifstart_mode:
        raise error.TestError("Get start mode fail")
    if old_ifstart_mode == "onboot":
        new_ifstart_mode = "none"
    else:
        new_ifstart_mode = "onboot"
    try:
        # Backup interface script
        process.run("cp %s %s" % (iface_script, iface_script_bk), shell=True)

        # Edit interface
        edit_ifstart_mode(iface_name, old_ifstart_mode, new_ifstart_mode)

        # Restart interface
        if iface_is_up:
            net_iface.down()
        process.run("ifup %s" % iface_name, shell=True)

        after_edit_mode = get_ifstart_mode(iface_name)
        if not after_edit_mode:
            raise error.TestError("Get start mode fail")
        if new_ifstart_mode == after_edit_mode:
            logging.debug("Interface start mode change to %s",
                          new_ifstart_mode)
        else:
            raise error.TestFail("Unexpect interface start mode: %s" %
                                 after_edit_mode)
    finally:
        net_iface.down()
        process.run("mv %s %s" % (iface_script_bk, iface_script), shell=True)
        if iface_is_up:
            process.run("ifup %s" % iface_name, shell=True)
Exemplo n.º 2
0
def run(test, params, env):
    """
    Test command: virsh iface-edit <interface>

    Edit interface start mode in this case.
    """
    iface_name = params.get("iface_name", "lo")
    status_error = "yes" == params.get("status_error", "no")
    if not libvirt.check_iface(iface_name, "exists", "--all"):
        raise error.TestError("Interface '%s' not exists" % iface_name)
    # get the interface script independent of distro
    iface_script = utils_net.get_network_cfg_file(iface_name)
    iface_script_bk = os.path.join(test.tmpdir, "iface-%s.bk" % iface_name)
    net_iface = utils_net.Interface(name=iface_name)
    iface_is_up = net_iface.is_up()
    old_ifstart_mode = get_ifstart_mode(iface_name)
    if not old_ifstart_mode:
        raise error.TestError("Get start mode fail")
    if old_ifstart_mode == "onboot":
        new_ifstart_mode = "none"
    else:
        new_ifstart_mode = "onboot"
    try:
        # Backup interface script
        process.run("cp %s %s" % (iface_script, iface_script_bk), shell=True)

        # Edit interface
        edit_ifstart_mode(iface_name, old_ifstart_mode, new_ifstart_mode)

        # Restart interface
        if iface_is_up:
            net_iface.down()
        process.run("ifup %s" % iface_name, shell=True)

        after_edit_mode = get_ifstart_mode(iface_name)
        if not after_edit_mode:
            raise error.TestError("Get start mode fail")
        if new_ifstart_mode == after_edit_mode:
            logging.debug("Interface start mode change to %s", new_ifstart_mode)
        else:
            raise error.TestFail("Unexpect interface start mode: %s"
                                 % after_edit_mode)
    finally:
        net_iface.down()
        process.run("mv %s %s" % (iface_script_bk, iface_script), shell=True)
        if iface_is_up:
            process.run("ifup %s" % iface_name, shell=True)
Exemplo n.º 3
0
    def add_device(pci_num):
        global iface_scripts
        reference_cmd = params["reference_cmd"]
        info_pci_ref = vm.monitor.info("pci")
        session = vm.wait_for_serial_login(timeout=timeout)
        reference = session.cmd_output(reference_cmd)
        active_nics = get_active_network_device(session, nic_filter)
        logging.debug("Active nics before hotplug - %s", active_nics)

        # Stop the VM monitor and try hot adding SRIOV dev
        if params.get("vm_stop", "no") == "yes":
            logging.debug("stop the monitor of the VM before hotplug")
            vm.pause()
        try:
            # get function for adding device.
            add_function = local_functions["%s_iov" % cmd_type]
        except Exception:
            test.error("No function for adding sr-iov dev with '%s'" %
                       cmd_type)
        after_add = None
        if add_function:
            # Do add pci device.
            after_add = add_function(pci_num)

        try:
            # Define a helper function to compare the output
            def _new_shown():
                output = session.cmd_output(reference_cmd)
                return output != reference

            # Define a helper function to make sure new nic could get ip.
            def _check_ip():
                post_nics = get_active_network_device(session, nic_filter)
                logging.debug("Active nics after hotplug - %s", post_nics)
                return (len(active_nics) <= len(post_nics)
                        and active_nics != post_nics)

            # Define a helper function to catch PCI device string
            def _find_pci():
                output = session.cmd_output("lspci -nn")
                if re.search(vf_filter, output, re.IGNORECASE):
                    return True
                else:
                    return False

            # Resume the VM
            if params.get("vm_resume", "no") == "yes":
                logging.debug("resuming the VM after hotplug")
                vm.resume()

            # Reboot the VM
            if params.get("vm_reboot", "no") == "yes":
                logging.debug("Rebooting the VM after hotplug")
                vm.reboot()
            session = vm.wait_for_serial_login(timeout=timeout)

            error_context.context("Start checking new added device")
            # Compare the output of 'info pci'
            if after_add == info_pci_ref:
                test.fail("No new PCI device shown after executing "
                          "monitor command: 'info pci'")

            secs = int(params["wait_secs_for_hook_up"])
            if not utils_misc.wait_for(_new_shown, test_timeout, secs, 3):
                test.fail("No new device shown in output of command "
                          "executed inside the guest: %s" % reference_cmd)

            if not utils_misc.wait_for(_find_pci, test_timeout, 3, 3):
                test.fail("New add device not found in guest. "
                          "Command was: lspci -nn")

            # Assign static IP to the hotplugged interface
            if params.get("assign_static_ip", "no") == "yes":
                cmd = "service networking restart"
                static_ip = next(ip_gen)
                net_mask = params.get("static_net_mask", "255.255.255.0")
                broadcast = params.get("static_broadcast", "10.10.10.255")
                pci_id = utils_misc.get_pci_id_using_filter(vf_filter, session)
                logging.debug("PCIs associated with %s - %s", vf_filter,
                              ', '.join(map(str, pci_id)))
                for each_pci in pci_id:
                    iface_name = utils_misc.get_interface_from_pci_id(
                        each_pci, session)
                    logging.debug("Interface associated with PCI %s - %s",
                                  each_pci, iface_name)
                    mac = session.cmd_output("ethtool -P %s" % iface_name)
                    mac = mac.split("Permanent address:")[-1].strip()
                    logging.debug("mac address of %s: %s", iface_name, mac)
                    # backup the network script for other distros
                    if "ubuntu" not in vm.get_distro().lower():
                        cmd = "service network restart"
                        iface_scripts.append(
                            utils_net.get_network_cfg_file(iface_name))
                    if not check_interface(str(iface_name), nic_filter):
                        utils_net.create_network_script(iface_name,
                                                        mac,
                                                        boot_proto="static",
                                                        net_mask=net_mask,
                                                        vm=vm,
                                                        ip_addr=static_ip)
                        status, output = session.cmd_status_output(cmd)
                        if status:
                            test.error("Failed to set static ip in guest: "
                                       "%s" % output)
            # Test the newly added device
            if not utils_misc.wait_for(_check_ip, 120, 3, 3):
                ifconfig = session.cmd_output("ifconfig -a")
                test.fail("New hotpluged device could not get ip "
                          "after 120s in guest. guest ifconfig "
                          "output: \n%s" % ifconfig)
            try:
                session.cmd(params["pci_test_cmd"] % (pci_num + 1))
            except aexpect.ShellError as e:
                test.fail("Check device failed after PCI "
                          "hotplug. Output: %r" % e.output)

        except Exception:
            pci_del(pci_num, ignore_failure=True)
            raise
Exemplo n.º 4
0
    def add_device(pci_num):
        global iface_scripts
        reference_cmd = params["reference_cmd"]
        info_pci_ref = vm.monitor.info("pci")
        session = vm.wait_for_serial_login(timeout=timeout)
        reference = session.cmd_output(reference_cmd)
        active_nics = get_active_network_device(session, nic_filter)
        logging.debug("Active nics before hotplug - %s", active_nics)

        # Stop the VM monitor and try hot adding SRIOV dev
        if params.get("vm_stop", "no") == "yes":
            logging.debug("stop the monitor of the VM before hotplug")
            vm.pause()
        try:
            # get function for adding device.
            add_function = local_functions["%s_iov" % cmd_type]
        except Exception:
            test.error("No function for adding sr-iov dev with '%s'" %
                       cmd_type)
        after_add = None
        if add_function:
            # Do add pci device.
            after_add = add_function(pci_num)

        try:
            # Define a helper function to compare the output
            def _new_shown():
                output = session.cmd_output(reference_cmd)
                return output != reference

            # Define a helper function to make sure new nic could get ip.
            def _check_ip():
                post_nics = get_active_network_device(session, nic_filter)
                logging.debug("Active nics after hotplug - %s", post_nics)
                return (len(active_nics) <= len(post_nics) and
                        active_nics != post_nics)

            # Define a helper function to catch PCI device string
            def _find_pci():
                output = session.cmd_output("lspci -nn")
                if re.search(vf_filter, output, re.IGNORECASE):
                    return True
                else:
                    return False

            # Resume the VM
            if params.get("vm_resume", "no") == "yes":
                logging.debug("resuming the VM after hotplug")
                vm.resume()

            # Reboot the VM
            if params.get("vm_reboot", "no") == "yes":
                logging.debug("Rebooting the VM after hotplug")
                vm.reboot()
            session = vm.wait_for_serial_login(timeout=timeout)

            error_context.context("Start checking new added device")
            # Compare the output of 'info pci'
            if after_add == info_pci_ref:
                test.fail("No new PCI device shown after executing "
                          "monitor command: 'info pci'")

            secs = int(params["wait_secs_for_hook_up"])
            if not utils_misc.wait_for(_new_shown, test_timeout, secs, 3):
                test.fail("No new device shown in output of command "
                          "executed inside the guest: %s" % reference_cmd)

            if not utils_misc.wait_for(_find_pci, test_timeout, 3, 3):
                test.fail("New add device not found in guest. "
                          "Command was: lspci -nn")

            # Assign static IP to the hotplugged interface
            if params.get("assign_static_ip", "no") == "yes":
                cmd = "service networking restart"
                static_ip = next(ip_gen)
                net_mask = params.get("static_net_mask", "255.255.255.0")
                broadcast = params.get("static_broadcast", "10.10.10.255")
                pci_id = utils_misc.get_pci_id_using_filter(vf_filter,
                                                            session)
                logging.debug("PCIs associated with %s - %s", vf_filter,
                              ', '.join(map(str, pci_id)))
                for each_pci in pci_id:
                    iface_name = utils_misc.get_interface_from_pci_id(each_pci,
                                                                      session)
                    logging.debug("Interface associated with PCI %s - %s",
                                  each_pci, iface_name)
                    mac = session.cmd_output("ethtool -P %s" % iface_name)
                    mac = mac.split("Permanent address:")[-1].strip()
                    logging.debug("mac address of %s: %s", iface_name, mac)
                    # backup the network script for other distros
                    if "ubuntu" not in vm.get_distro().lower():
                        cmd = "service network restart"
                        iface_scripts.append(utils_net.get_network_cfg_file(iface_name))
                    if not check_interface(str(iface_name), nic_filter):
                        utils_net.create_network_script(iface_name, mac,
                                                        boot_proto="static",
                                                        net_mask=net_mask,
                                                        vm=vm,
                                                        ip_addr=static_ip)
                        status, output = session.cmd_status_output(cmd)
                        if status:
                            test.error("Failed to set static ip in guest: "
                                       "%s" % output)
            # Test the newly added device
            if not utils_misc.wait_for(_check_ip, 120, 3, 3):
                ifconfig = session.cmd_output("ifconfig -a")
                test.fail("New hotpluged device could not get ip "
                          "after 120s in guest. guest ifconfig "
                          "output: \n%s" % ifconfig)
            try:
                session.cmd(params["pci_test_cmd"] % (pci_num + 1))
            except aexpect.ShellError as e:
                test.fail("Check device failed after PCI "
                          "hotplug. Output: %r" % e.output)

        except Exception:
            pci_del(pci_num, ignore_failure=True)
            raise