Beispiel #1
0
        def before_migration(self, mig_data):
            def do_reboot(vm):
                reboot_method = mig_data.params.get("reboot_method",
                                                    "system_reset")
                reboot_timeout = float(mig_data.params.get("reboot_timeout", 30))
                if self.is_src:
                    logging.info("Do '%s' before migraion..." % reboot_method)

                    end_time = time.time() + reboot_timeout
                    while time.time() < end_time:
                        vm.monitor.clear_event("RESET")
                        vm.monitor.cmd(reboot_method)
                        reseted = utils_misc.wait_for(lambda:
                                                      vm.monitor.get_event("RESET"),
                                                      timeout=self.login_timeout)
                        if not reseted:
                            raise error.TestFail("Not found RESET event after "
                                                 "execute 'system_reset'")
                        vm.monitor.clear_event("RESET")

                        time.sleep(self.random_timeout)

            error.context("Do reboot before migraion.", logging.info)
            vm = env.get_vm(params["main_vm"])
            bg = utils.InterruptedThread(do_reboot, (vm,))
            bg.start()
            time.sleep(self.random_timeout)
Beispiel #2
0
        def post_migration_capability_load_host_io(self, vm, cancel_delay,
                                                   mig_offline, dsthost,
                                                   vm_ports,
                                                   not_wait_for_migration, fd,
                                                   mig_data):
            """
            set auto-converge off/on during migration

            :param vm: vm object
            :param cancel_delay: If provided, specifies a time duration
                   after which migration will be canceled.  Used for
                   testing migrate_cancel.
            :param mig_offline: If True, pause the source VM before migration
            :param dsthost: Destination host
            :param vm_ports: vm migration ports
            :param not_wait_for_migration: If True migration start but not
                   wait till the end of migration.
            :param fd: File descriptor for migration
            :param mig_data: Data for migration
            """

            mig_thread = utils.InterruptedThread(self.get_sar_output)
            mig_thread.start()
            try:
                vm.wait_for_migration(self.migration_timeout)
                logging.info("Migration completed with set auto-converge: "
                             "%s" % set_auto_converge)
            except virt_vm.VMMigrateTimeoutError:
                raise error.TestFail("Migration failed with set auto-converge"
                                     ": %s" % set_auto_converge)
            finally:
                if self.session:
                    self.session.close()
                vm.destroy(gracefully=False)
                mig_thread.join()
Beispiel #3
0
    def migration_when_wdt_timeout():
        """
        Migration when WDT timeout
        Test Step:
        1. Boot guest with watchdog device
        2. Check watchdog device have been initialized successfully in guest
        3. Start VM with watchdog device, action reset|pause
        4. Inside RHEL guest, trigger watchdog
        5. Before WDT timeout, do vm migration
        6. After migration, check the watchdog action take effect
        """

        mig_timeout = float(params.get("mig_timeout", "3600"))
        mig_protocol = params.get("migration_protocol", "tcp")
        mig_cancel_delay = int(params.get("mig_cancel") == "yes") * 2

        _watchdog_device_check(session, watchdog_device_type)
        _trigger_watchdog(session, trigger_cmd)

        error.context(
            "Do migration(protocol:%s),Watchdog have been triggered." %
            mig_protocol, logging.info)
        args = (mig_timeout, mig_protocol, mig_cancel_delay)
        migrate_thread = utils.InterruptedThread(vm.migrate, args)
        migrate_thread.start()
        _action_check(session, watchdog_action)
        migrate_thread.join(timeout=mig_timeout)
Beispiel #4
0
def run_drive_mirror_cancel(test, params, env):
    """
    Test block mirroring functionality

    1). boot vm then mirror $source_image to nfs/iscsi target
    2). block nfs/iscsi serivce port via iptables rules
    3). cancel block job and check it not cancel immedicatly
    4). flush iptables chain then check job canceled in 10s

    """
    tag = params.get("source_images", "image1")
    mirror_test = drive_mirror.DriveMirror(test, params, env, tag)
    try:
        mirror_test.start()
        error.context("Block network connection with iptables", logging.info)
        utils.run(params["start_firewall_cmd"])
        bg = utils.InterruptedThread(mirror_test.cancel, )
        bg.start()
        job = mirror_test.get_status()
        if job["type"] != "mirror":
            raise error.TestFail("Job cancel immediacatly")
        error.context("Cleanup rules in iptables", logging.info)
        utils.run(params["stop_firewall_cmd"])
        bg.join(timeout=int(params["cancel_timeout"]))
    finally:
        mirror_test.vm.destroy()
        mirror_test.clean()
Beispiel #5
0
            def start_worker(mig_data):
                logging.info("Try to login guest before migration test.")
                vm = env.get_vm(params["main_vm"])
                bg_stress_test = self.params.get("bg_stress_test")
                session = vm.wait_for_login(timeout=self.login_timeout,
                                            nic_index=self.nic_index)

                error.context("Do stress test before migration.", logging.info)
                check_running_cmd = params.get("check_running_cmd")

                self.bg = utils.InterruptedThread(
                    utils_test.run_virt_sub_test,
                    args=(
                        test,
                        params,
                        env,
                    ),
                    kwargs={"sub_type": bg_stress_test})

                self.bg.start()

                def check_running():
                    return session.cmd_status(check_running_cmd) == 0

                if check_running_cmd:
                    if not utils_misc.wait_for(check_running, timeout=360):
                        raise error.TestFail("Failed to start %s in guest." %
                                             bg_stress_test)
Beispiel #6
0
    def run_bg_stress_test(bg_stress_test):
        """
        Run backgroud test.

        :param bg_stress_test: Background test.
        :return: return the background case thread if it's successful;
                 else raise error.
        """
        if bg_stress_test:
            error_context.context("Run test %s background" % bg_stress_test,
                                  logging.info)
            stress_thread = None
            wait_time = float(params.get("wait_bg_time", 60))
            target_process = params.get("target_process", "")
            bg_stress_run_flag = params.get("bg_stress_run_flag")
            env[bg_stress_run_flag] = False
            stress_thread = utils.InterruptedThread(
                utils_test.run_virt_sub_test, (test, params, env),
                {"sub_type": bg_stress_test})
            stress_thread.start()
            if not utils_misc.wait_for(
                    lambda: check_bg_running(session, target_process), 120, 0,
                    5):
                raise exceptions.TestFail("Backgroud test %s is not "
                                          "alive!" % bg_stress_test)
            env["bg_status"] = 1
            logging.debug("Wait %s test start" % bg_stress_test)
            if not utils_misc.wait_for(lambda: env.get(bg_stress_run_flag),
                                       wait_time, 0, 0.5):
                err = "Fail to start %s test" % bg_stress_test
                raise exceptions.TestError(err)
            return stress_thread
        def migration_scenario(self):
            sync = SyncData(self.master_id(), self.hostid, self.hosts, self.id,
                            self.sync_server)
            self.vm = params.get("vms").split()[0]
            address_cache = env.get("address_cache")

            if (self.hostid == self.master_id()):
                utils.run("dd if=/dev/urandom of=%s bs=1M"
                          " count=%s" % (host_path, file_size))

                self.vm_addr = self._prepare_vm(self.vm).get_address()

                end_event = threading.Event()
                bg = utils.InterruptedThread(self._copy_until_end,
                                             (end_event, ))

                self._hosts_barrier(self.hosts, self.id, "befor_mig", 120)
                sync.sync(address_cache, timeout=120)
                error.context(
                    "ping-pong between host and guest while"
                    " migrating", logging.info)
                self._run_and_migrate(bg, end_event, sync, migrate_count)

                # Check if guest lives.
                virt_remote.wait_for_login(shell_client, self.vm_addr,
                                           shell_port, guest_root, guest_pass,
                                           shell_prompt)
                self._hosts_barrier(self.hosts, self.id, "After_check", 120)

                error.context("comparing hashes", logging.info)
                orig_hash = client_utils.hash_file(host_path)
                returned_hash = client_utils.hash_file(host_path_returned)

                #Check all check sum
                wrong_check_sum = False
                for i in range(len(self.file_check_sums)):
                    check_sum = self.file_check_sums[i]
                    if check_sum != orig_hash:
                        wrong_check_sum = True
                        logging.error("Checksum in transfer number"
                                      " %d if wrong." % (i))
                if wrong_check_sum:
                    raise error.TestFail("Returned file hash (%s) differs from"
                                         " original one (%s)" %
                                         (returned_hash, orig_hash))
                else:
                    #clean temp
                    utils.run("rm -rf %s" % (host_path))
                    utils.run("rm -rf %s" % (returned_hash))

                error.context()
            else:
                self._hosts_barrier(self.hosts, self.id, "befor_mig", 260)
                address_cache.update(sync.sync(timeout=120)[self.master_id()])
                logging.debug("Address cache updated to %s" % address_cache)
                self._slave_migrate(sync)

                #Wait for check if guest lives.
                self._hosts_barrier(self.hosts, self.id, "After_check", 120)
Beispiel #8
0
def run_rv_audio(test, params, env):

    guest_vm = env.get_vm(params["guest_vm"])
    guest_vm.verify_alive()
    guest_session = guest_vm.wait_for_login(
        timeout=int(params.get("login_timeout", 360)))

    client_vm = env.get_vm(params["client_vm"])
    client_vm.verify_alive()
    client_session = client_vm.wait_for_login(
        timeout=int(params.get("login_timeout", 360)))

    if (guest_session.cmd_status("ls %s" % params.get("audio_tgt"))):
        print params.get("audio_src")
        print params.get("audio_tgt")
        guest_vm.copy_files_to(params.get("audio_src"),
                               params.get("audio_tgt"))
    if (client_session.cmd_status("ls %s" % params.get("audio_tgt"))):
        client_vm.copy_files_to(params.get("audio_src"),
                                params.get("audio_tgt"))

    if params.get("rv_record") == "yes":
        logging.info("rv_record set; Testing recording")
        player = client_vm.wait_for_login(
            timeout=int(params.get("login_timeout", 360)))
        recorder_session = guest_vm.wait_for_login(
            timeout=int(params.get("login_timeout", 360)))
        recorder_session_vm = guest_vm
    else:
        logging.info("rv_record not set; Testing playback")
        player = guest_vm.wait_for_login(
            timeout=int(params.get("login_timeout", 360)))
        recorder_session = client_vm.wait_for_login(
            timeout=int(params.get("login_timeout", 360)))
        recorder_session_vm = client_vm

    player.cmd(
        "aplay %s &> /dev/null &" %  # starts playback
        params.get("audio_tgt"),
        timeout=30)

    if params.get("config_test", "no") == "migration":
        bg = utils.InterruptedThread(guest_vm.migrate, kwargs={})
        bg.start()

    recorder_session.cmd(
        "arecord -d %s -f cd -D hw:0,1 %s" % (  # records
            params.get("audio_time", "200"),  # duration
            params.get("audio_rec")),  # target
        timeout=500)

    if params.get("config_test", "no") == "migration":
        bg.join()

    recorder_session_vm.copy_files_from(params.get("audio_rec"),
                                        "./recorded.wav")
    if not verify_recording("./recorded.wav", params):
        raise error.TestFail("Test failed")
Beispiel #9
0
 def action_when_start(self):
     """
     start pre-action in new threads;
     """
     for test in self.params.get("when_start").split():
         if hasattr(self, test):
             fun = getattr(self, test)
             bg = utils.InterruptedThread(fun)
             bg.start()
             if bg.isAlive():
                 self.processes.append(bg)
Beispiel #10
0
 def action_before_start(self):
     """
     start pre-action in new threads;
     """
     params = self.parser_test_args()
     for param in params.get("before_start").split():
         if hasattr(self, param):
             fun = getattr(self, param)
             bg = utils.InterruptedThread(fun)
             bg.start()
             if bg.isAlive():
                 self.process.append(bg)
Beispiel #11
0
 def installation_test():
     args = (test, params, env)
     bg = utils.InterruptedThread(utils_test.run_virt_sub_test, args,
                                  {"sub_type": "unattended_install"})
     bg.start()
     if bg.is_alive():
         sleep_time = int(params.get("sleep_time", 60))
         time.sleep(sleep_time)
         create_snapshot(vm)
         try:
             bg.join()
         except Exception:
             raise
Beispiel #12
0
 def action_when_start(self):
     """
     start pre-action in new threads;
     do live snapshot during pre-action.
     """
     tag = self.params.get("source_image", "image1")
     for test in self.params.get("when_start").split():
         if hasattr(self, test):
             fun = getattr(self, test)
             bg = utils.InterruptedThread(fun)
             bg.start()
             if bg.isAlive():
                 self.create_snapshot()
                 bg.join()
Beispiel #13
0
    def _start_server(self, address, port):
        signal.signal(signal.SIGTERM, self)
        self.server_thread = utils.InterruptedThread(self._server,
                                                     (address, port))
        self.server_thread.start()

        while not self.exit_event.isSet():
            signal.pause()

        self.server_thread.join(2 * _DEFAULT_TIMEOUT)
        logging.debug("Server thread finished.")
        for session in self.sessions.itervalues():
            session.close()
        self.listen_server.close()
        logging.debug("ListenServer closed finished.")
Beispiel #14
0
    def stress_test_in_guest(self, timeout=60):

        self.bg = utils.InterruptedThread(
            utils_test.run_virt_sub_test,
            args=(
                self.test,
                self.params,
                self.env,
            ),
            kwargs={"sub_type": self.guest_stress_test})
        self.bg.start()
        logging.info("sleep %ds waiting guest stress test start.", timeout)
        time.sleep(timeout)
        if not self.bg.is_alive():
            raise error.TestFail("Failed to start guest stress test!")
Beispiel #15
0
        def post_migration_capability(
                self, vm, cancel_delay, mig_offline, dsthost,
                vm_ports, not_wait_for_migration, fd, mig_data):

            """
            set auto-converge off/on during migration
            set/get parameter cpu-throttle-initial 30
            set/get parameter cpu-throttle-increment 20

            :param vm: vm object
            :param cancel_delay: If provided, specifies a time duration
                   after which migration will be canceled.  Used for
                   testing migrate_cancel.
            :param mig_offline: If True, pause the source VM before migration
            :param dsthost: Destination host
            :param vm_ports: vm migration ports
            :param not_wait_for_migration: If True migration start but not
                   wait till the end of migration.
            :param fd: File descriptor for migration
            :param mig_data: Data for migration
            """

            if set_auto_converge == "yes":
                mig_thread = utils.InterruptedThread(
                    self.thread_check_mig_cpu_throttling_percentage)
                mig_thread.start()
            try:
                vm.wait_for_migration(self.migration_timeout)
                logging.info("Migration completed with auto-converge on")
            except virt_vm.VMMigrateTimeoutError:
                if set_auto_converge == "yes":
                    raise error.TestFail("Migration failed with "
                                         "auto-converge on")
                else:
                    logging.info("migration would never finish with "
                                 "auto-converge off")
                    if self.need_cleanup:
                        self.clean_up(self.kill_bg_stress_cmd, vm)
                    try:
                        vm.wait_for_migration(self.migration_timeout)
                    except virt_vm.VMMigrateTimeoutError:
                        raise error.TestFail("After kill stessapptest, "
                                             "migration failed with "
                                             "auto-converge off")
            finally:
                if self.session:
                    self.session.close()
                vm.destroy(gracefully=False)
Beispiel #16
0
def run(test, params, env):
    """
    KVM migration test:
    1) Get a live VM and clone it.
    2) Verify that the source VM supports migration.  If it does, proceed with
            the test.
    3) Reboot the VM
    4) Send a migration command to the source VM and wait until it's finished.
    5) Kill off the source VM.
    6) Log into the destination VM after the migration is finished.

    :param test: kvm test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    login_timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)

    mig_timeout = float(params.get("mig_timeout", "3600"))
    mig_protocol = params.get("migration_protocol", "tcp")
    mig_cancel_delay = int(params.get("mig_cancel") == "yes") * 2

    try:
        # Reboot the VM in the background
        bg = utils.InterruptedThread(vm.reboot,
                                     kwargs={
                                         'session': session,
                                         'timeout': login_timeout
                                     })
        bg.start()
        try:
            while bg.isAlive():
                vm.migrate(mig_timeout,
                           mig_protocol,
                           mig_cancel_delay,
                           env=env)
        except Exception:
            # If something bad happened in the main thread, ignore exceptions
            # raised in the background thread
            bg.join(suppress_exception=True)
            raise
        else:
            session = bg.join()
    finally:
        session.close()
Beispiel #17
0
    def _convert(cmd,
                 output_fmt,
                 img_name,
                 output_filename,
                 fmt=None,
                 compressed="no",
                 encrypted="no"):
        """
        Simple wrapper of 'qemu-img convert' function.

        :param cmd: qemu-img base command.
        :param output_fmt: the output format of converted image
        :param img_name: image name that to be converted
        :param output_filename: output image name that converted
        :param fmt: output image format
        :param compressed: whether output image is compressed
        :param encrypted: whether output image is encrypted
        """
        cmd += " convert"
        if compressed == "yes":
            cmd += " -c"
        if encrypted == "yes":
            cmd += " -e"
        show_progress = params.get("show_progress", "")
        if show_progress == "on":
            cmd += " -p"
        if fmt:
            cmd += " -f %s" % fmt
        cmd += " -O %s" % output_fmt
        options = params.get("qemu_img_options")
        if options:
            options = options.split()
            cmd += " -o "
            for option in options:
                value = params.get(option)
                cmd += "%s=%s," % (option, value)
            cmd = cmd.rstrip(",")
        cmd += " %s %s" % (img_name, output_filename)
        msg = "Converting '%s' from format '%s'" % (img_name, fmt)
        msg += " to '%s'" % output_fmt
        error.context(msg, logging.info)
        if show_progress == "off":
            bg = utils.InterruptedThread(send_signal)
            bg.start()
        check_command_output(utils.run(cmd, ignore_status=True))
Beispiel #18
0
def run_pxe_query_cpus(test, params, env):
    """
    Qemu guest pxe boot test:
    1). check npt/ept function enable, then boot vm
    2). execute query/info cpus in loop
    3). verify vm not paused during pxe booting

    params:
    @param test: QEMU test object
    @param params: Dictionary with the test parameters
    @param env: Dictionary with test environment.
    """
    pxe_test = PxeTest(test, params, env)
    error.context("Enable %s on host" % pxe_test.mmu, logging.info)
    pxe_test.enable_mmu()

    error.context("Bootup vm from network", logging.info)
    vm = pxe_test.get_vm()
    params["start_vm"] = "yes"
    params["restart_vm"] = "no"
    bg = utils.InterruptedThread(utils_test.run_virt_sub_test,
                                 args=(
                                     test,
                                     params,
                                     env,
                                 ),
                                 kwargs={"sub_type": "pxe"})
    bg.start()
    count = 0
    try:
        error.context("Query cpus in loop", logging.info)
        while True:
            vm.monitor.info("cpus")
            count += 1
            vm.verify_status("running")
            if not bg.isAlive():
                break
        logging.info("Execute info/query cpus %d times", count)
    finally:
        pxe_test.cleanup()
Beispiel #19
0
    def run_bg_stress_test(bg_stress_test):
        """
        Run backgroud test.

        :param bg_stress_test: Background test.
        :return: return the background case thread if it's successful;
                 else raise error.
        """
        error_context.context("Run test %s background" % bg_stress_test,
                              logging.info)
        stress_thread = None
        wait_time = float(params.get("wait_bg_time", 60))
        target_process = params.get("target_process", "")
        bg_stress_run_flag = params.get("bg_stress_run_flag")
        # Need to set bg_stress_run_flag in some cases to make sure all
        # necessary steps are active
        env[bg_stress_run_flag] = False
        if params.get("bg_stress_test_is_cmd", "no") == "yes":
            session = vm.wait_for_login()
            bg_stress_test = utils_misc.set_winutils_letter(
                session, bg_stress_test)
            session.sendline(bg_stress_test)
        else:
            stress_thread = utils.InterruptedThread(
                utils_test.run_virt_sub_test, (test, params, env),
                {"sub_type": bg_stress_test})
            stress_thread.start()
        if not utils_misc.wait_for(lambda: check_bg_running(target_process),
                                   120, 0, 1):
            raise exceptions.TestFail("Backgroud test %s is not "
                                      "alive!" % bg_stress_test)
        if params.get("set_bg_stress_flag", "no") == "yes":
            logging.info("Wait %s test start" % bg_stress_test)
            if not utils_misc.wait_for(lambda: env.get(bg_stress_run_flag),
                                       wait_time, 0, 0.5):
                err = "Fail to start %s test" % bg_stress_test
                raise exceptions.TestError(err)
        env["bg_status"] = 1
        return stress_thread
Beispiel #20
0
    def _server(self, address, port):
        self.listen_server = barrier.listen_server(address, port)
        logging.debug("Wait for clients")
        self.listen_server.socket.settimeout(_DEFAULT_TIMEOUT)
        while not self.exit_event.isSet():
            try:
                connection = self.listen_server.socket.accept()
                logging.debug("Client %s connected.", connection[1][0])
                session_id, hosts, timeout = net_recv_object(
                    connection[0], _DEFAULT_TIMEOUT)
                self._clean_sessions()
                if session_id not in self.sessions:
                    logging.debug("Add new session")
                    self.sessions[session_id] = SessionData(hosts, timeout)

                logging.debug("Start recv thread.")
                utils.InterruptedThread(
                    self._recv_data,
                    (connection, self.sessions[session_id])).start()

            except (socket.timeout, error.NetCommunicationError):
                self._clean_sessions()
        logging.debug("SyncListenServer on closed.")
            def start_worker(mig_data):
                error.context("Load stress in guest.", logging.info)
                vm = env.get_vm(params["main_vm"])
                session = vm.wait_for_login(timeout=self.login_timeout)
                bg_stress_test = params.get("bg_stress_test")
                check_running_cmd = params.get("check_running_cmd")

                bg = utils.InterruptedThread(
                    utils_test.run_virt_sub_test,
                    args=(
                        test,
                        params,
                        env,
                    ),
                    kwargs={"sub_type": bg_stress_test})
                bg.start()

                def is_stress_running():
                    return session.cmd_status(check_running_cmd) == 0

                if not utils_misc.wait_for(is_stress_running, timeout=360):
                    raise error.TestFail("Failed to start %s in guest." %
                                         bg_stress_test)
Beispiel #22
0
    def _rebase(cmd, img_name, base_img, backing_fmt, mode="unsafe"):
        """
        Simple wrapper of 'qemu-img rebase'.

        :param cmd: qemu-img base command.
        :param img_name: image name to be rebased
        :param base_img: indicates the base image
        :param backing_fmt: the format of base image
        :param mode: rebase mode: safe mode, unsafe mode
        """
        cmd += " rebase"
        show_progress = params.get("show_progress", "")
        if mode == "unsafe":
            cmd += " -u"
        if show_progress == "on":
            cmd += " -p"
        cmd += " -b %s -F %s %s" % (base_img, backing_fmt, img_name)
        msg = "Trying to rebase '%s' to '%s' by command %s" % (img_name,
                                                               base_img, cmd)
        error.context(msg, logging.info)
        if show_progress == "off":
            bg = utils.InterruptedThread(send_signal)
            bg.start()
        check_command_output(utils.run(cmd))
Beispiel #23
0
def run_ntttcp(test, params, env):
    """
    Run NTttcp on Windows guest

    1) Install NTttcp in server/client side by Autoit
    2) Start NTttcp in server/client side
    3) Get test results

    :param test: kvm test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    login_timeout = int(params.get("login_timeout", 360))
    timeout = int(params.get("timeout"))
    results_path = os.path.join(test.resultsdir,
                                'raw_output_%s' % test.iteration)
    platform = "x86"
    if "64" in params["vm_arch_name"]:
        platform = "x64"
    buffers = params.get("buffers").split()
    buf_num = params.get("buf_num", 200000)
    session_num = params.get("session_num")

    vm_sender = env.get_vm(params["main_vm"])
    vm_sender.verify_alive()
    vm_receiver = None
    receiver_addr = params.get("receiver_address")

    logging.debug(utils.system("numactl --hardware", ignore_status=True))
    logging.debug(utils.system("numactl --show", ignore_status=True))
    # pin guest vcpus/memory/vhost threads to last numa node of host by default
    if params.get('numa_node'):
        numa_node = int(params.get('numa_node'))
        node = utils_misc.NumaNode(numa_node)
        utils_test.qemu.pin_vm_threads(vm_sender, node)

    if not receiver_addr:
        vm_receiver = env.get_vm("vm2")
        vm_receiver.verify_alive()
        try:
            sess = None
            sess = vm_receiver.wait_for_login(timeout=login_timeout)
            receiver_addr = vm_receiver.get_address()
            if not receiver_addr:
                raise error.TestError("Can't get receiver(%s) ip address" %
                                      vm_sender.name)
            if params.get('numa_node'):
                utils_test.qemu.pin_vm_threads(vm_receiver, node)
        finally:
            if sess:
                sess.close()

    @error.context_aware
    def install_ntttcp(session):
        """ Install ntttcp through a remote session """
        logging.info("Installing NTttcp ...")
        try:
            # Don't install ntttcp if it's already installed
            error.context("NTttcp directory already exists")
            session.cmd(params.get("check_ntttcp_cmd"))
        except aexpect.ShellCmdError:
            ntttcp_install_cmd = params.get("ntttcp_install_cmd")
            error.context("Installing NTttcp on guest")
            session.cmd(ntttcp_install_cmd % (platform, platform), timeout=200)

    def receiver():
        """ Receive side """
        logging.info("Starting receiver process on %s", receiver_addr)
        if vm_receiver:
            session = vm_receiver.wait_for_login(timeout=login_timeout)
        else:
            username = params.get("username", "")
            password = params.get("password", "")
            prompt = params.get("shell_prompt", "[\#\$]")
            linesep = eval("'%s'" % params.get("shell_linesep", r"\n"))
            client = params.get("shell_client")
            port = int(params.get("shell_port"))
            log_filename = (
                "session-%s-%s.log" %
                (receiver_addr, utils_misc.generate_random_string(4)))
            session = remote.remote_login(client, receiver_addr, port,
                                          username, password, prompt, linesep,
                                          log_filename, timeout)
            session.set_status_test_command("echo %errorlevel%")
        install_ntttcp(session)
        ntttcp_receiver_cmd = params.get("ntttcp_receiver_cmd")
        global _receiver_ready
        f = open(results_path + ".receiver", 'a')
        for b in buffers:
            utils_misc.wait_for(lambda: not _wait(), timeout)
            _receiver_ready = True
            rbuf = params.get("fixed_rbuf", b)
            cmd = ntttcp_receiver_cmd % (session_num, receiver_addr, rbuf,
                                         buf_num)
            r = session.cmd_output(cmd,
                                   timeout=timeout,
                                   print_func=logging.debug)
            f.write("Send buffer size: %s\n%s\n%s" % (b, cmd, r))
        f.close()
        session.close()

    def _wait():
        """ Check if receiver is ready """
        global _receiver_ready
        if _receiver_ready:
            return _receiver_ready
        return False

    def sender():
        """ Send side """
        logging.info("Sarting sender process ...")
        session = vm_sender.wait_for_login(timeout=login_timeout)
        install_ntttcp(session)
        ntttcp_sender_cmd = params.get("ntttcp_sender_cmd")
        f = open(results_path + ".sender", 'a')
        try:
            global _receiver_ready
            for b in buffers:
                cmd = ntttcp_sender_cmd % (session_num, receiver_addr, b,
                                           buf_num)
                # Wait until receiver ready
                utils_misc.wait_for(_wait, timeout)
                r = session.cmd_output(cmd,
                                       timeout=timeout,
                                       print_func=logging.debug)
                _receiver_ready = False
                f.write("Send buffer size: %s\n%s\n%s" % (b, cmd, r))
        finally:
            f.close()
            session.close()

    def parse_file(resultfile):
        """ Parse raw result files and generate files with standard format """
        fileobj = open(resultfile, "r")
        lst = []
        found = False
        for line in fileobj.readlines():
            o = re.findall("Send buffer size: (\d+)", line)
            if o:
                bfr = o[0]
            if "Total Throughput(Mbit/s)" in line:
                found = True
            if found:
                fields = line.split()
                if len(fields) == 0:
                    continue
                try:
                    [float(i) for i in fields]
                    lst.append([bfr, fields[-1]])
                except ValueError:
                    continue
                found = False
        return lst

    try:
        bg = utils.InterruptedThread(receiver, ())
        bg.start()
        if bg.isAlive():
            sender()
            bg.join(suppress_exception=True)
        else:
            raise error.TestError("Can't start backgroud receiver thread")
    finally:
        for i in glob.glob("%s.receiver" % results_path):
            f = open("%s.RHS" % results_path, "w")
            raw = "  buf(k)| throughput(Mbit/s)"
            logging.info(raw)
            f.write("#ver# %s\n#ver# host kernel: %s\n" %
                    (commands.getoutput("rpm -q qemu-kvm"), os.uname()[2]))
            desc = """#desc# The tests are sessions of "NTttcp", send buf number is %s. 'throughput' was taken from ntttcp's report.
#desc# How to read the results:
#desc# - The Throughput is measured in Mbit/sec.
#desc#
""" % (buf_num)
            f.write(desc)
            f.write(raw + "\n")
            for j in parse_file(i):
                raw = "%8s| %8s" % (j[0], j[1])
                logging.info(raw)
                f.write(raw + "\n")
            f.close()
Beispiel #24
0
def run(test, params, env):
    """
    Qemu guest pxe boot test:
    1). check npt/ept function enable, then boot vm
    2). execute query/info cpus in loop
    3). verify vm not paused during pxe booting

    params:
    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def stopVMS(params, env):
        """
        Kill all VMS for relaod kvm_intel/kvm_amd module;
        """
        for vm in env.get_all_vms():
            if vm:
                vm.destroy()
                env.unregister_vm(vm.name)
        qemu_bin = os.path.basename(params["qemu_binary"])
        utils.run("killall -g %s" % qemu_bin, ignore_status=True)
        time.sleep(5)

    error.context("Enable hardware MMU", logging.info)
    enable_mmu_cmd = check_mmu_cmd = restore_mmu_cmd = None
    try:
        flag = filter(lambda x: x in utils_misc.get_cpu_flags(),
                      ['ept', 'npt'])[0]
    except IndexError:
        logging.warn("Host doesn't support Hareware MMU")
    else:
        enable_mmu_cmd = params["enable_mmu_cmd_%s" % flag]
        check_mmu_cmd = params["check_mmu_cmd_%s" % flag]
        status = utils.system(check_mmu_cmd, timeout=120, ignore_status=True)
        if status != 0:
            stopVMS(params, env)
            utils.run(enable_mmu_cmd)
            restore_mmu_cmd = params["restore_mmu_cmd_%s" % flag]

    params["start_vm"] = "yes"
    params["kvm_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params["main_vm"])
    vm = env.get_vm(params["main_vm"])
    bg = utils.InterruptedThread(utils_test.run_virt_sub_test,
                                 args=(
                                     test,
                                     params,
                                     env,
                                 ),
                                 kwargs={"sub_type": "pxe"})
    bg.start()
    count = 0
    try:
        error.context("Query cpus in loop", logging.info)
        while True:
            vm.monitor.info("cpus")
            count += 1
            vm.verify_status("running")
            if not bg.isAlive():
                break
        logging.info("Execute info/query cpus %d times", count)
    finally:
        if restore_mmu_cmd:
            stopVMS(params, env)
            utils.run(restore_mmu_cmd)
Beispiel #25
0
def run(test, params, env):
    """
    Qemu host nic bonding test:
    1) Load bonding module with mode 802.3ad
    2) Bring up bond interface
    3) Add nics to bond interface
    4) Add a new bridge and add bond interface to it
    5) Get ip address for bridge
    6) Boot up guest with the bridge
    7) Checking guest netowrk via ping
    8) Start file transfer between guest and host
    9) Disable and enable physical interfaces during file transfer

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    bond_iface = params.get("bond_iface", "bond0")
    bond_br_name = params.get("bond_br_name", "br_bond0")
    timeout = int(params.get("login_timeout", 240))
    remote_host = params.get("dsthost")
    ping_timeout = int(params.get("ping_timeout", 240))
    bonding_timeout = int(params.get("bonding_timeout", 1))
    bonding_mode = params.get("bonding_mode")
    if not bonding_mode:
        bonding_mode = "1"
    params['netdst'] = bond_br_name
    host_bridges = utils_net.Bridge()

    error.context("Load bonding module with mode 802.3ad", logging.info)
    if not utils.system("lsmod|grep bonding", ignore_status=True):
        utils.system("modprobe -r bonding")

    utils.system("modprobe bonding mode=%s" % bonding_mode)

    error.context("Bring up %s" % bond_iface, logging.info)
    host_ifaces = utils_net.get_host_iface()

    if bond_iface not in host_ifaces:
        raise error.TestError("Can not find bond0 in host")

    bond_iface = utils_net.Interface(bond_iface)
    bond_iface.up()
    bond_mac = bond_iface.get_mac()

    host_ph_iface_pre = params.get("host_ph_iface_prefix", "en")
    host_iface_bonding = int(params.get("host_iface_bonding", 2))

    host_ph_ifaces = [_ for _ in host_ifaces if re.match(host_ph_iface_pre, _)]

    ifaces_in_use = host_bridges.list_iface()
    host_ph_ifaces_un = list(set(host_ph_ifaces) - set(ifaces_in_use))

    if (len(host_ph_ifaces_un) < 2
            or len(host_ph_ifaces_un) < host_iface_bonding):
        raise error.TestNAError("Host need %s nics"
                                " at least." % host_iface_bonding)

    error.context("Add nics to %s" % bond_iface.name, logging.info)
    host_ifaces_bonding = host_ph_ifaces_un[:host_iface_bonding]
    ifenslave_cmd = "ifenslave %s" % bond_iface.name
    op_ifaces = []
    for host_iface_bonding in host_ifaces_bonding:
        op_ifaces.append(utils_net.Interface(host_iface_bonding))
        ifenslave_cmd += " %s" % host_iface_bonding
    utils.system(ifenslave_cmd)

    error.context("Add a new bridge and add %s to it." % bond_iface.name,
                  logging.info)
    if bond_br_name not in host_bridges.list_br():
        host_bridges.add_bridge(bond_br_name)
    host_bridges.add_port(bond_br_name, bond_iface.name)

    error.context("Get ip address for bridge", logging.info)
    utils.system("pkill dhclient; dhclient %s" % bond_br_name)

    error.context("Boot up guest with bridge %s" % bond_br_name, logging.info)
    params["start_vm"] = "yes"
    vm_name = params.get("main_vm")
    env_process.preprocess_vm(test, params, env, vm_name)
    vm = env.get_vm(vm_name)
    session = vm.wait_for_login(timeout=timeout)

    error.context("Checking guest netowrk via ping.", logging.info)
    ping_cmd = params.get("ping_cmd")
    ping_cmd = re.sub("REMOTE_HOST", remote_host, ping_cmd)
    session.cmd(ping_cmd, timeout=ping_timeout)

    error.context("Start file transfer", logging.info)
    f_transfer = utils.InterruptedThread(utils_test.run_virt_sub_test,
                                         args=(
                                             test,
                                             params,
                                             env,
                                         ),
                                         kwargs={"sub_type": "file_transfer"})
    f_transfer.start()
    utils_misc.wait_for(
        lambda: utils.system_output("pidof scp", ignore_status=True), 30)

    error.context(
        "Disable and enable physical "
        "interfaces in %s" % bond_br_name, logging.info)
    while True:
        for op_iface in op_ifaces:
            logging.debug("Turn down %s" % op_iface.name)
            op_iface.down()
            time.sleep(bonding_timeout)
            logging.debug("Bring up %s" % op_iface.name)
            op_iface.up()
            time.sleep(bonding_timeout)
        if not f_transfer.is_alive():
            break
    f_transfer.join()
Beispiel #26
0
def run_nic_bonding(test, params, env):
    """
    Nic bonding test in guest.

    1) Start guest with four nic models.
    2) Setup bond0 in guest by script nic_bonding_guest.py.
    3) Execute file transfer test between guest and host.
    4) Repeatedly put down/up interfaces by set_link
    5) Execute file transfer test between guest and host.

    @param test: Kvm test object.
    @param params: Dictionary with the test parameters.
    @param env: Dictionary with test environment.
    """
    timeout = int(params.get("login_timeout", 1200))
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    session_serial = vm.wait_for_serial_login(timeout=timeout)

    # get params of bonding
    modprobe_cmd = "modprobe bonding"
    bonding_params = params.get("bonding_params")
    if bonding_params:
        modprobe_cmd += " %s" % bonding_params
    session_serial.cmd(modprobe_cmd)

    session_serial.cmd("ifconfig bond0 up")
    ifnames = [
        utils_net.get_linux_ifname(session_serial, vm.get_mac_address(vlan))
        for vlan, nic in enumerate(vm.virtnet)
    ]
    setup_cmd = "ifenslave bond0 " + " ".join(ifnames)
    session_serial.cmd(setup_cmd)
    #do a pgrep to check if dhclient has already been running
    pgrep_cmd = "pgrep dhclient"
    try:
        session_serial.cmd(pgrep_cmd)
    #if dhclient is there, killl it
    except aexpect.ShellCmdError:
        logging.info("it's safe to run dhclient now")
    else:
        logging.info("dhclient already is running,kill it")
        session_serial.cmd("killall -9 dhclient")
        time.sleep(1)
    session_serial.cmd("dhclient bond0")

    try:
        logging.info("Test file transfering:")
        utils_test.run_file_transfer(test, params, env)

        logging.info("Failover test with file transfer")
        transfer_thread = utils.InterruptedThread(utils_test.run_file_transfer,
                                                  (test, params, env))
        try:
            transfer_thread.start()
            while transfer_thread.isAlive():
                for vlan, nic in enumerate(vm.virtnet):
                    device_id = nic.device_id
                    if not device_id:
                        raise error.TestError("Could not find peer device for"
                                              " nic device %s" % nic)
                    vm.set_link(device_id, up=False)
                    time.sleep(1)
                    vm.set_link(device_id, up=True)
        except Exception:
            transfer_thread.join(suppress_exception=True)
            raise
        else:
            transfer_thread.join()
    finally:
        session_serial.sendline("ifenslave -d bond0 " + " ".join(ifnames))
        session_serial.sendline("kill -9 `pgrep dhclient`")
Beispiel #27
0
def run(test, params, env):
    """
    KVM migration test:
    1) Get a live VM and clone it.
    2) Verify that the source VM supports migration.  If it does, proceed with
            the test.
    3) Transfer file from host to guest.
    4) Repeatedly migrate VM and wait until transfer's finished.
    5) Transfer file from guest back to host.
    6) Repeatedly migrate VM and wait until transfer's finished.

    :param test: QEMU test object.
    :param params: Dictionary with test parameters.
    :param env: Dictionary with the test environment.
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    login_timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)

    mig_timeout = float(params.get("mig_timeout", "3600"))
    mig_protocol = params.get("migration_protocol", "tcp")
    mig_cancel_delay = int(params.get("mig_cancel") == "yes") * 2

    host_path = "/tmp/file-%s" % utils_misc.generate_random_string(6)
    host_path_returned = "%s-returned" % host_path
    guest_path = params.get("guest_path", "/tmp/file")
    file_size = params.get("file_size", "500")
    transfer_timeout = int(params.get("transfer_timeout", "240"))
    migrate_between_vhost_novhost = params.get("migrate_between_vhost_novhost")

    try:
        utils.run("dd if=/dev/urandom of=%s bs=1M count=%s" %
                  (host_path, file_size))

        def run_and_migrate(bg):
            bg.start()
            try:
                while bg.isAlive():
                    logging.info(
                        "File transfer not ended, starting a round of "
                        "migration...")
                    if migrate_between_vhost_novhost == "yes":
                        vhost_status = vm.params.get("vhost")
                        if vhost_status == "vhost=on":
                            vm.params["vhost"] = "vhost=off"
                        elif vhost_status == "vhost=off":
                            vm.params["vhost"] = "vhost=on"
                    vm.migrate(mig_timeout, mig_protocol, mig_cancel_delay)
            except Exception:
                # If something bad happened in the main thread, ignore
                # exceptions raised in the background thread
                bg.join(suppress_exception=True)
                raise
            else:
                bg.join()

        error.context("transferring file to guest while migrating",
                      logging.info)
        bg = utils.InterruptedThread(
            vm.copy_files_to, (host_path, guest_path),
            dict(verbose=True, timeout=transfer_timeout))
        run_and_migrate(bg)

        error.context("transferring file back to host while migrating",
                      logging.info)
        bg = utils.InterruptedThread(
            vm.copy_files_from, (guest_path, host_path_returned),
            dict(verbose=True, timeout=transfer_timeout))
        run_and_migrate(bg)

        # Make sure the returned file is identical to the original one
        error.context("comparing hashes", logging.info)
        orig_hash = client_utils.hash_file(host_path)
        returned_hash = client_utils.hash_file(host_path_returned)
        if orig_hash != returned_hash:
            raise error.TestFail("Returned file hash (%s) differs from "
                                 "original one (%s)" %
                                 (returned_hash, orig_hash))
        error.context()

    finally:
        session.close()
        if os.path.isfile(host_path):
            os.remove(host_path)
        if os.path.isfile(host_path_returned):
            os.remove(host_path_returned)
Beispiel #28
0
def run(test, params, env):
    """
    Test failover by team driver

    1) Boot a vm with 4 nics.
    2) inside guest, configure the team driver.
    3) inside guest, ping host
    4) inside guest, repeated down the slaves one by one.
    5) check ping_result.

    :param test: Kvm test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def team_port_add(ifnames, team_if):
        """Team0 add ports and return the ip link result for debuging"""
        for port in ifnames:
            session_serial.cmd_output_safe(params["clearip_cmd"] % port)
            session_serial.cmd_output_safe(params["setdown_cmd"] % port)
            session_serial.cmd_output_safe(params["addport_cmd"] % port)
        output_teamnl = session_serial.cmd_output_safe(params["portchk_cmd"])
        ports = re.findall(r"%s" % params["ptn_teamnl"], output_teamnl)
        for port in ifnames:
            if port not in ports:
                raise error.TestFail("Add %s to %s failed." % (port, team_if))
        session_serial.cmd_output_safe(params["killdhclient_cmd"])
        output = session_serial.cmd_output_safe(params["getip_cmd"],
                                                timeout=300)
        team_ip = re.search(r"%s" % params["ptn_ipv4"], output).group()
        if not team_ip:
            raise error.TestFail("Failed to get ip address of %s" % team_if)
        return ports, team_ip

    def failover(ifnames, timeout):
        """func for failover"""
        time.sleep(3)
        starttime = time.time()
        while True:
            pid_ping = session_serial.cmd_output_safe("pidof ping")
            pid = re.findall(r"(\d+)", pid_ping)
            if not pid:
                break
                # if ping finished, will break the loop.
            for port in ifnames:
                session_serial.cmd_output_safe(params["setdown_cmd"] % port)
                time.sleep(random.randint(5, 30))
                session_serial.cmd_output_safe(params["setup_cmd"] % port)
            endtime = time.time()
            timegap = endtime - starttime
            if timegap > timeout:
                break

    def check_ping(status, output):
        """ ratio <5% is acceptance."""
        if status != 0:
            raise error.TestFail("Ping failed, staus:%s, output:%s"
                                 % (status, output))
        # if status != 0 the ping process seams hit issue.
        ratio = utils_test.get_loss_ratio(output)
        if ratio == -1:
            raise error.TestFail('''The ratio is %s, and status is %s,
                                    output is %s''' % (ratio, status, output))
        elif ratio > int(params["failed_ratio"]):
            raise error.TestFail("The loss raito is %s, test failed" % ratio)
        logging.info("ping pass with loss raito:%s, that less than %s" %
                     (ratio, params["failed_ratio"]))

    def team_if_exist():
        """ judge if team is alive well."""
        team_exists_cmd = params.get("team_if_exists_cmd")
        return session_serial.cmd_status(team_exists_cmd) == 0

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 1200))
    session_serial = vm.wait_for_serial_login(timeout=timeout)
    ifnames = [utils_net.get_linux_ifname(session_serial,
                                          vm.get_mac_address(vlan))
               for vlan, nic in enumerate(vm.virtnet)]
    session_serial.cmd_output_safe(params["nm_stop_cmd"])
    team_if = params.get("team_if")
    # initial

    error.context("Step1: Configure the team environment", logging.info)
    # steps of building the teaming environment starts
    modprobe_cmd = "modprobe team"
    session_serial.cmd_output_safe(modprobe_cmd)
    session_serial.cmd_output_safe(params["createteam_cmd"])
    # this cmd is to create the team0 and correspoding userspace daemon
    if not team_if_exist():
        raise error.TestFail("Interface %s is not created." % team_if)
    # check if team0 is created successfully
    ports, team_ip = team_port_add(ifnames, team_if)
    logging.debug("The list of the ports that added to %s : %s"
                  % (team_if, ports))
    logging.debug("The ip address of %s : %s" % (team_if, team_ip))
    output = session_serial.cmd_output_safe(params["team_debug_cmd"])
    logging.debug("team interface configuration: %s" % output)
    route_cmd = session_serial.cmd_output_safe(params["route_cmd"])
    logging.debug("The route table of guest: %s" % route_cmd)
    # this is not this case checkpoint, just to check if route works fine
    # steps of building finished

    try:
        error.context("Login in guest via ssh", logging.info)
        # steps of testing this case starts
        session = vm.wait_for_login(timeout=timeout)
        dest = utils_net.get_ip_address_by_interface(params["netdst"])
        count = params.get("count")
        timeout = float(count) * 2
        error.context("Step2: Check if guest can ping out:", logging.info)
        status, output = utils_test.ping(dest=dest, count=10,
                                         interface=team_if,
                                         timeout=30,
                                         session=session)
        check_ping(status, output)
        # small ping check if the team0 works w/o failover
        error.context("Step3: Start failover testing until ping finished",
                      logging.info)
        failover_thread = utils.InterruptedThread(failover, (ifnames, timeout))
        failover_thread.start()
        # start failover loop until ping finished
        error.context("Step4: Start ping host for %s counts"
                      % count, logging.info)
        if failover_thread.is_alive():
            status, output = utils_test.ping(dest=dest, count=count,
                                             interface=team_if,
                                             timeout=float(count) * 1.5,
                                             session=session)
            error.context("Step5: Check if ping succeeded", logging.info)
            check_ping(status, output)
        else:
            raise error.TestWarn("The failover thread is not alive")
        time.sleep(3)
        try:
            timeout = timeout * 1.5
            failover_thread.join(timeout)
        except Exception:
            raise error.TestWarn("Failed to join the failover thread")
        # finish the main steps and check the result
        session_serial.cmd_output_safe(params["killteam_cmd"])
        if team_if_exist():
            raise error.TestFail("Remove %s failed" % team_if)
        logging.info("%s removed" % team_if)
        # remove the team0 and the daemon, check if succeed
    finally:
        if session:
            session.close()
def run_sr_iov_irqbalance(test, params, env):
    """
    Qemu guest irqbalance inactive/active test:
    1) Setup host for sr-iov test.
    2) Boot VM with sr-iov vf/pf assigned and multi vcpu.
    3) Update irqbalance service status in guest. stop/start this server
       according to request.
    4) Get available network interface name in guest.
    5) Start background network stress in guest.
    6) Get irq number assigned to attached vfs/pfs.
    7) Get the cpu number the irq running.
    8) Check specified IRQ count grow on specified cpu.
    9) Repeat step 7 for every 10s.
    10) Balance IRQs generated by vfs/pfs to different vcpus (optional)
       e.g.
       echo 4 > /proc/irq/num/smp_affinity
    11) Repeat step 6, 7
    12) Check that specified IRQ count grow on every cpu. (optional)

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=timeout)
    irqbalance_check_count = int(params.get("irqbalance_check_count", 36))
    nic_interface_filter = params["nic_interface_filter"]

    error.context("Make sure that guest have at least 2 vCPUs.", logging.info)
    cpu_count = vm.get_cpu_count()
    if cpu_count < 2:
        raise error.TestNAError("Test requires at least 2 vCPUs.")

    msg = "Update irqbalance service status in guest if not match request."
    error.context(msg, logging.info)
    irqbalance_status = params.get("irqbalance_status", "active")
    status = get_guest_service_status(session=session, service="irqbalance")
    service_cmd = ""
    if status == "active" and irqbalance_status == "inactive":
        service_cmd = "service irqbalance stop"
    elif status == "inactive" and irqbalance_status == "active":
        service_cmd = "service irqbalance start"
    if service_cmd:
        status, output = session.cmd_status_output(service_cmd)
        if status:
            msg = "Fail to update irqbalance service status in guest."
            msg += " Command output in guest: %s" % output
            raise error.TestError(msg)

    error.context("Get first network interface name in guest.", logging.info)
    devname = get_first_network_devname(session, nic_interface_filter)

    error.context("Start background network stress in guest.", logging.info)
    host_ip = utils_net.get_ip_address_by_interface(params.get('netdst'))
    ping_cmd = "ping %s  -f -q" % host_ip
    ping_timeout = irqbalance_check_count * 10 + 100
    ping_session = vm.wait_for_login(timeout=timeout)
    bg_stress = utils.InterruptedThread(utils_test.raw_ping,
                                        kwargs={
                                            'command': ping_cmd,
                                            'timeout': ping_timeout,
                                            'session': ping_session,
                                            'output_func': None
                                        })
    bg_stress.start()
    try:
        error.context("Get irq number assigned to attached VF/PF in guest",
                      logging.info)
        irq_nums_dict = get_guest_irq_info(session, devname, cpu_count)
        if irq_nums_dict:
            irqs = irq_nums_dict.keys()

        msg = "Check specified IRQ count grow on specified cpu."
        error.context(msg, logging.info)
        check_irqbalance(session, devname, cpu_count, irqs)
        irq_cpus_dict = {}
        for irq in irqs:
            cpus = get_irq_smp_affinity(session, irq)
            irq_cpus_dict[irq] = cpus

        if irqbalance_status == "inactive":
            msg = "balance IRQs generated by vfs/pfs to different vcpus."
            error.context(msg, logging.info)
            post_irq_cpus_dict = {}
            for irq in irq_cpus_dict:
                balance_cpu_count = 1
                cpus = []
                for cpu in xrange(cpu_count):
                    if cpu not in irq_cpus_dict[irq]:
                        cpus.append(cpu)
                        if len(cpus) == balance_cpu_count:
                            break
                set_irq_smp_affinity(session, irq, cpus)
                post_irq_cpus_dict[irq] = cpus

            for irq in irqs:
                cpus = get_irq_smp_affinity(session, irq)
                msg = "Fail to balance IRQs generated by vf/pf to different cpu"
                if cpus != post_irq_cpus_dict[irq]:
                    raise error.TestFail(msg)

        msg = "Check specified IRQ count grow on specified cpu."
        error.context(msg, logging.info)
        check_irqbalance(session,
                         devname,
                         cpu_count,
                         irqs,
                         count=irqbalance_check_count)

        if irqbalance_status == "active":
            msg = "Check that specified IRQ count grow on every cpu."
            error.context(msg, logging.info)
            post_irq_nums_dict = get_guest_irq_info(session, devname,
                                                    cpu_count)

            for irq in irqs:
                if irq not in post_irq_nums_dict.keys():
                    post_irqs = post_irq_nums_dict.keys()
                    msg = "Different irq detected: '%s' and '%s'." % (
                        irqs, post_irqs)
                    raise error.TestError(msg)
                for cpu in xrange(cpu_count):
                    if (int(irq_nums_dict[irq][cpu]) >= int(
                            post_irq_nums_dict[irq][cpu])):
                        msg = "'Cpu%s' did not handle more interrupt" % cpu
                        msg += "for irq '%s'." % irq
                        msg += "IRQ balance information for IRQ '%s'\n" % irq
                        msg += "First time: %s\n" % irq_nums_dict
                        msg += "Just now: %s" % post_irq_nums_dict
                        raise error.TestFail(msg)
    finally:
        if bg_stress.isAlive():
            bg_stress.join(suppress_exception=True)
        else:
            logging.warn("Background stress test already finished")
def run_timedrift_monotonicity(test, params, env):
    """
    Check guest time monotonicity during migration:

    1) Log into a guest.
    2) Take time from guest.
    3) Migrate the guest.
    4) Keep guest running for a period after migration,
       and record the time log.
    5) Analyse log if it is exist.

    @param test: QEMU test object.
    @param params: Dictionary with test parameters.
    @param env: Dictionary with the test environment.
    """
    def get_time(cmd, test_time, session):
        if os.path.isfile(host_path):
            os.remove(host_path)
        lasttv = "0"
        cmd_timeout=int(params.get("cmd_timeout"))
        start_time = time.time()
        while (time.time() - start_time) < test_time :
            tv = session.cmd_output(cmd, timeout=cmd_timeout)
            if params.get("os_type") == 'windows':
                list = re.split('[:]',tv)
                tv = str(int(list[0])*3600 + int(list[1])*60 + float(list[2]))
            if float(tv) < float(lasttv):
                p_tv = "time value = " + tv + "\n"
                p_lasttv = "last time value = " + lasttv + "\n"
                time_log = file(host_path, 'a')
                time_log.write("time went backwards:\n" + p_tv + p_lasttv)
                time_log.close()
            lasttv = tv
            time.sleep(0.1)

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()

    boot_option_added = params.get("boot_option_added")
    boot_option_removed = params.get("boot_option_removed")
    if boot_option_added or boot_option_removed:
        utils_test.update_boot_option(vm,
                                      args_removed=boot_option_removed,
                                      args_added=boot_option_added)

    timeout = int(params.get("login_timeout", 360))
    session1 = vm.wait_for_login(timeout=timeout)

    host_path = params.get("host_path")
    cmd = params.get("cmd_get_time")
    test_time = int(params.get("time_linger","60"))

    try:
        # take time
        logging.info("Start take guest time")
        bg = utils.InterruptedThread(get_time,(cmd,test_time,session1))
        bg.start()

        # migration
        logging.info("Start migration")
        vm.migrate()

        # log in
        logging.info("Logging in after migration...")
        session2 = vm.wait_for_login(timeout=timeout)
        if not session2:
            raise error.TestFail("Could not log in after migration")
        logging.info("Logged in after migration")

        # linger a while
        time.sleep(test_time)

        # analyse the result
        if os.path.isfile(host_path):
            log_dir = os.path.join(test.outputdir,
                                   "timedrift-monotonicity-result.txt")
            shutil.copyfile(host_path, log_dir)
            myfile = file(host_path, 'r')
            for line in myfile:
                if "time went backwards" in line:
                    myfile.close()
                    raise error.TestFail("Failed Time Monotonicity testing, "
                                         "Please check log %s" % host_path)
    finally:
        session1.close()
        # remove flags add for this test.
        if boot_option_added or boot_option_removed:
            utils_test.update_boot_option(vm,
                                          args_removed=boot_option_added,
                                          args_added=boot_option_removed)