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)
        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.
                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)
Exemplo n.º 3
0
def run(test, params, env):
    """
    live_snapshot_base test:
    1). Boot up guest
    2). Create a file on host and record md5
    3). Copy the file to guest
    3). Create live snapshot
    4). Copy the file from guest,then check md5

    :param test: Kvm 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", 3600))
    session = vm.wait_for_login(timeout=timeout)

    dd_timeout = params.get("dd_timeoout", 600)
    copy_timeout = params.get("copy_timeoout", 600)
    base_file = storage.get_image_filename(params, data_dir.get_data_dir())
    device = vm.get_block({"file": base_file})
    snapshot_file = "images/%s" % params.get("snapshot_name")
    snapshot_file = utils_misc.get_path(data_dir.get_data_dir(), snapshot_file)
    snapshot_format = params.get("snapshot_format", "qcow2")
    tmp_name = utils_misc.generate_random_string(5)
    src = dst = "/tmp/%s" % tmp_name
    if params.get("os_type") != "linux":
        dst = "c:\\users\\public\\%s" % tmp_name

    try:
        error.context("create file on host, copy it to guest", logging.info)
        cmd = params.get("dd_cmd") % src
        utils.system(cmd, timeout=dd_timeout)
        md5 = utils.hash_file(src, method="md5")
        vm.copy_files_to(src, dst, timeout=copy_timeout)
        utils.system("rm -f %s" % src)
        error.context("create live snapshot", logging.info)
        if vm.live_snapshot(base_file, snapshot_file,
                            snapshot_format) != device:
            raise error.TestFail("Fail to create snapshot")
        backing_file = vm.monitor.get_backingfile(device)
        if backing_file != base_file:
            logging.error("backing file: %s, base file: %s", backing_file,
                          base_file)
            raise error.TestFail("Got incorrect backing file")
        error.context("copy file to host, check content not changed",
                      logging.info)
        vm.copy_files_from(dst, src, timeout=copy_timeout)
        if md5 and (md5 != utils.hash_file(src, method="md5")):
            raise error.TestFail("diff md5 before/after create snapshot")
        session.cmd(params.get("alive_check_cmd", "dir"))
    finally:
        if session:
            session.close()
        utils.system("rm -f %s %s" % (snapshot_file, src))
Exemplo n.º 4
0
def run(test, params, env):
    """
    live_snapshot_base test:
    1). Boot up guest
    2). Create a file on host and record md5
    3). Copy the file to guest
    3). Create live snapshot
    4). Copy the file from guest,then check md5

    :param test: Kvm 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", 3600))
    session = vm.wait_for_login(timeout=timeout)

    dd_timeout = params.get("dd_timeoout", 600)
    copy_timeout = params.get("copy_timeoout", 600)
    base_file = storage.get_image_filename(params, data_dir.get_data_dir())
    device = vm.get_block({"file": base_file})
    snapshot_file = "images/%s" % params.get("snapshot_name")
    snapshot_file = utils_misc.get_path(data_dir.get_data_dir(), snapshot_file)
    snapshot_format = params.get("snapshot_format", "qcow2")
    tmp_name = utils_misc.generate_random_string(5)
    src = dst = "/tmp/%s" % tmp_name
    if params.get("os_type") != "linux":
        dst = "c:\\users\\public\\%s" % tmp_name

    try:
        error.context("create file on host, copy it to guest", logging.info)
        cmd = params.get("dd_cmd") % src
        utils.system(cmd, timeout=dd_timeout)
        md5 = utils.hash_file(src, method="md5")
        vm.copy_files_to(src, dst, timeout=copy_timeout)
        utils.system("rm -f %s" % src)
        error.context("create live snapshot", logging.info)
        if vm.live_snapshot(base_file, snapshot_file,
                            snapshot_format) != device:
            raise error.TestFail("Fail to create snapshot")
        backing_file = vm.monitor.get_backingfile(device)
        if backing_file != base_file:
            logging.error(
                "backing file: %s, base file: %s", backing_file, base_file)
            raise error.TestFail("Got incorrect backing file")
        error.context("copy file to host, check content not changed",
                      logging.info)
        vm.copy_files_from(dst, src, timeout=copy_timeout)
        if md5 and (md5 != utils.hash_file(src, method="md5")):
            raise error.TestFail("diff md5 before/after create snapshot")
        session.cmd(params.get("alive_check_cmd", "dir"))
    finally:
        if session:
            session.close()
        utils.system("rm -f %s %s" % (snapshot_file, src))
Exemplo n.º 5
0
    def do_file_copy(src_file, guest_file, dst_file, transfer_timeout):
        error.context("Transferring file host -> guest,"
                      " timeout: %ss" % transfer_timeout, logging.info)
        vm.copy_files_to(src_file, guest_file, timeout=transfer_timeout)
        error.context("Transferring file guest -> host,"
                      " timeout: %ss" % transfer_timeout, logging.info)
        vm.copy_files_from(guest_file, dst_file, timeout=transfer_timeout)
        error.context("Compare md5sum between original file and"
                      " transferred file", logging.info)

        if (utils.hash_file(src_file, method="md5") !=
                utils.hash_file(dst_file, method="md5")):
            raise error.TestFail("File changed after transfer host -> guest "
                                 "and guest -> host")
Exemplo n.º 6
0
    def do_file_copy(src_file, guest_file, dst_file, transfer_timeout):
        error.context("Transferring file host -> guest,"
                      " timeout: %ss" % transfer_timeout, logging.info)
        vm.copy_files_to(src_file, guest_file, timeout=transfer_timeout)
        error.context("Transferring file guest -> host,"
                      " timeout: %ss" % transfer_timeout, logging.info)
        vm.copy_files_from(guest_file, dst_file, timeout=transfer_timeout)
        error.context("Compare md5sum between original file and"
                      " transferred file", logging.info)

        if (utils.hash_file(src_file, method="md5") !=
                utils.hash_file(dst_file, method="md5")):
            raise error.TestFail("File changed after transfer host -> guest "
                                 "and guest -> host")
        def _copy_until_end(self, end_event):
            #Copy until migration not end.
            while not end_event.isSet():
                logging.info("Copy file to guest %s.", self.vm_addr)
                remote.copy_files_to(self.vm_addr,
                                     "scp",
                                     guest_root,
                                     guest_pass,
                                     22,
                                     host_path,
                                     guest_path,
                                     limit=transfer_speed,
                                     verbose=True,
                                     timeout=transfer_timeout)
                logging.info("Copy file to guests %s done.", self.vm_addr)

                logging.info("Copy file from guest %s.", self.vm_addr)
                remote.copy_files_from(self.vm_addr,
                                       "scp",
                                       guest_root,
                                       guest_pass,
                                       22,
                                       guest_path,
                                       host_path_returned,
                                       limit=transfer_speed,
                                       verbose=True,
                                       timeout=transfer_timeout)
                logging.info("Copy file from guests %s done.", self.vm_addr)
                check_sum = client_utils.hash_file(host_path_returned)
                #store checksum for later check.
                self.file_check_sums.append(check_sum)
Exemplo n.º 8
0
    def copy_if_hash_differs(vm, local_path, remote_path):
        """
        Copy a file to a guest if it doesn't exist or if its MD5sum differs.

        :param vm: VM object.
        :param local_path: Local path.
        :param remote_path: Remote path.

        :return: Whether the hash differs (True) or not (False).
        """
        hash_differs = False
        local_hash = utils.hash_file(local_path)
        basename = os.path.basename(local_path)
        output = session.cmd_output("md5sum %s" % remote_path)
        if "such file" in output:
            remote_hash = "0"
        elif output:
            remote_hash = output.split()[0]
        else:
            logging.warning("MD5 check for remote path %s did not return.",
                            remote_path)
            # Let's be a little more lenient here and see if it wasn't a
            # temporary problem
            remote_hash = "0"
        if remote_hash != local_hash:
            hash_differs = True
            logging.debug(
                "Copying %s to guest "
                "(remote hash: %s, local hash:%s)", basename, remote_hash,
                local_hash)
            vm.copy_files_to(local_path, remote_path)
        return hash_differs
Exemplo n.º 9
0
    def copy_if_hash_differs(vm, local_path, remote_path):
        """
        Copy a file to a guest if it doesn't exist or if its MD5sum differs.

        :param vm: VM object.
        :param local_path: Local path.
        :param remote_path: Remote path.

        :return: Whether the hash differs (True) or not (False).
        """
        hash_differs = False
        local_hash = utils.hash_file(local_path)
        basename = os.path.basename(local_path)
        output = session.cmd_output("md5sum %s" % remote_path)
        if "such file" in output:
            remote_hash = "0"
        elif output:
            remote_hash = output.split()[0]
        else:
            logging.warning("MD5 check for remote path %s did not return.",
                            remote_path)
            # Let's be a little more lenient here and see if it wasn't a
            # temporary problem
            remote_hash = "0"
        if remote_hash != local_hash:
            hash_differs = True
            logging.debug("Copying %s to guest "
                          "(remote hash: %s, local hash:%s)",
                          basename, remote_hash, local_hash)
            vm.copy_files_to(local_path, remote_path)
        return hash_differs
Exemplo n.º 10
0
 def compare_md5sum(name):
     logging.info("Comparing md5sum of the files on guest and host")
     host_result = utils.hash_file(name, method="md5")
     try:
         o = session.cmd_output("md5sum %s" % name)
         guest_result = re.findall("\w+", o)[0]
     except IndexError:
         logging.error("Could not get file md5sum in guest")
         return False
     logging.debug("md5sum: guest(%s), host(%s)", guest_result, host_result)
     return guest_result == host_result
Exemplo n.º 11
0
    def _action_after_fsthaw(self, *args):
        if self.bg:
            self.bg.join()
        # Make sure the returned file is identical to the original one
        self.host_path_returned = "%s-returned" % self.host_path
        self.vm.copy_files_from(self.guest_path, self.host_path_returned)
        error.context("comparing hashes", logging.info)
        self.curr_hash = utils.hash_file(self.host_path_returned)
        if self.orig_hash != self.curr_hash:
            raise error.TestFail("Current file hash (%s) differs from "
                                 "original one (%s)" %
                                 (self.curr_hash, self.orig_hash))

        error.context("Reboot and shutdown guest.")
        self.vm.reboot()
        self.vm.destroy()
Exemplo n.º 12
0
    def _action_after_fsthaw(self, *args):
        if self.bg:
            self.bg.join()
        # Make sure the returned file is identical to the original one
        self.host_path_returned = "%s-returned" % self.host_path
        self.vm.copy_files_from(self.guest_path, self.host_path_returned)
        error.context("comparing hashes", logging.info)
        self.curr_hash = utils.hash_file(self.host_path_returned)
        if self.orig_hash != self.curr_hash:
            raise error.TestFail("Current file hash (%s) differs from "
                                 "original one (%s)" % (self.curr_hash,
                                                        self.orig_hash))

        error.context("Reboot and shutdown guest.")
        self.vm.reboot()
        self.vm.destroy()
    def _action_before_fsfreeze(self, *args):
        copy_timeout = int(self.params.get("copy_timeoout", 600))
        file_size = int(self.params.get("file_size", "500"))
        tmp_name = utils_misc.generate_random_string(5)
        self.host_path = self.guest_path = "/tmp/%s" % tmp_name
        if self.params.get("os_type") != "linux":
            self.guest_path = r"c:\%s" % tmp_name

        error.context("Create a file in host.")
        utils.run("dd if=/dev/urandom of=%s bs=1M count=%s" % (self.host_path,
                                                               file_size))
        self.orig_hash = utils.hash_file(self.host_path)
        error.context("Transfer file from %s to %s" % (self.host_path,
                                                       self.guest_path), logging.info)
        self.bg = utils.InterruptedThread(self.vm.copy_files_to,
                                          (self.host_path, self.guest_path),
                                          dict(verbose=True, timeout=copy_timeout))
        self.bg.start()
Exemplo n.º 14
0
    def _action_before_fsfreeze(self, *args):
        copy_timeout = int(self.params.get("copy_timeoout", 600))
        file_size = int(self.params.get("file_size", "500"))
        tmp_name = utils_misc.generate_random_string(5)
        self.host_path = self.guest_path = "/tmp/%s" % tmp_name
        if self.params.get("os_type") != "linux":
            self.guest_path = r"c:\%s" % tmp_name

        error.context("Create a file in host.")
        utils.run("dd if=/dev/urandom of=%s bs=1M count=%s" % (self.host_path,
                                                               file_size))
        self.orig_hash = utils.hash_file(self.host_path)
        error.context("Transfer file from %s to %s" % (self.host_path,
                      self.guest_path), logging.info)
        self.bg = utils.InterruptedThread(self.vm.copy_files_to,
                                          (self.host_path, self.guest_path),
                                          dict(verbose=True, timeout=copy_timeout))
        self.bg.start()
        def _copy_until_end(self, end_event):
            #Copy until migration not end.
            while not end_event.isSet():
                logging.info("Copy file to guest %s.", self.vm_addr)
                remote.copy_files_to(self.vm_addr, "scp", guest_root,
                                          guest_pass, 22, host_path,
                                          guest_path, limit=transfer_speed,
                                          verbose=True,
                                          timeout=transfer_timeout)
                logging.info("Copy file to guests %s done.", self.vm_addr)

                logging.info("Copy file from guest %s.", self.vm_addr)
                remote.copy_files_from(self.vm_addr, "scp", guest_root,
                                            guest_pass, 22, guest_path,
                                            host_path_returned,
                                            limit=transfer_speed, verbose=True,
                                            timeout=transfer_timeout)
                logging.info("Copy file from guests %s done.", self.vm_addr)
                check_sum = client_utils.hash_file(host_path_returned)
                #store checksum for later check.
                self.file_check_sums.append(check_sum)
Exemplo n.º 16
0
            answer = 'y'
        if answer == 'y':
            utils.interactive_download(
                url, destination, "Downloading %s" % title)
            had_to_download = True
        else:
            logging.warning("Missing file %s", destination)
    else:
        logging.info("Found %s", destination)
        if sha1 is None:
            answer = 'n'
        else:
            answer = 'y'

        if answer == 'y':
            actual_sha1 = utils.hash_file(destination, method='sha1')
            if actual_sha1 != sha1:
                logging.info("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = utils.ask("The file seems corrupted or outdated. "
                                       "Would you like to download it?")
                else:
                    logging.info("The file seems corrupted or outdated")
                    answer = 'y'
                if answer == 'y':
                    logging.info("Updating image to the latest available...")
                    while not file_ok:
                        utils.interactive_download(url, destination, title)
                        sha1_post_download = utils.hash_file(destination,
                                                             method='sha1')
                        had_to_download = True
Exemplo n.º 17
0
    vm = env.get_vm(params["main_vm"])
    local_dir = params.get("local_dir")
    if local_dir:
        local_dir = utils_misc.get_path(test.bindir, local_dir)
    else:
        local_dir = test.bindir
    if params.get("copy_to_local"):
        for param in params.get("copy_to_local").split():
            l_value = params.get(param)
            if l_value:
                need_copy = True
                nfs_link = utils_misc.get_path(test.bindir, l_value)
                i_name = os.path.basename(l_value)
                local_link = os.path.join(local_dir, i_name)
                if os.path.isfile(local_link):
                    file_hash = utils.hash_file(local_link, "md5")
                    expected_hash = utils.hash_file(nfs_link, "md5")
                    if file_hash == expected_hash:
                        need_copy = False
                if need_copy:
                    msg = "Copy %s to %s in local host." % (i_name, local_link)
                    error.context(msg, logging.info)
                    utils.get_file(nfs_link, local_link)
                    params[param] = local_link

    unattended_install_config = UnattendedInstallConfig(test, params, vm)
    unattended_install_config.setup()

    # params passed explicitly, because they may have been updated by
    # unattended install config code, such as when params['url'] == auto
    vm.create(params=params)
Exemplo n.º 18
0
    vm = env.get_vm(params["main_vm"])
    local_dir = params.get("local_dir")
    if local_dir:
        local_dir = utils_misc.get_path(test.bindir, local_dir)
    else:
        local_dir = test.bindir
    if params.get("copy_to_local"):
        for param in params.get("copy_to_local").split():
            l_value = params.get(param)
            if l_value:
                need_copy = True
                nfs_link = utils_misc.get_path(test.bindir, l_value)
                i_name = os.path.basename(l_value)
                local_link = os.path.join(local_dir, i_name)
                if os.path.isfile(local_link):
                    file_hash = utils.hash_file(local_link, "md5")
                    expected_hash = utils.hash_file(nfs_link, "md5")
                    if file_hash == expected_hash:
                        need_copy = False
                if need_copy:
                    msg = "Copy %s to %s in local host." % (i_name, local_link)
                    error.context(msg, logging.info)
                    utils.get_file(nfs_link, local_link)
                    params[param] = local_link

    unattended_install_config = UnattendedInstallConfig(test, params, vm)
    unattended_install_config.setup()

    # params passed explicitly, because they may have been updated by
    # unattended install config code, such as when params['url'] == auto
    vm.create(params=params)
Exemplo n.º 19
0
def run_file_transfer(test, params, env):
    """
    Transfer a file back and forth between host and guest.

    1) Boot up a VM.
    2) Create a large file by dd on host.
    3) Copy this file from host to guest.
    4) Copy this file from guest to host.
    5) Check if file transfers ended good.

    :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()
    login_timeout = int(params.get("login_timeout", 360))

    error.context("Login to guest", logging.info)
    session = vm.wait_for_login(timeout=login_timeout)

    dir_name = test.tmpdir
    transfer_timeout = int(params.get("transfer_timeout"))
    transfer_type = params.get("transfer_type")
    tmp_dir = params.get("tmp_dir", "/tmp/")
    clean_cmd = params.get("clean_cmd", "rm -f")
    filesize = int(params.get("filesize", 4000))
    count = int(filesize / 10)
    if count == 0:
        count = 1

    host_path = os.path.join(dir_name,
                             "tmp-%s" % utils_misc.generate_random_string(8))
    host_path2 = host_path + ".2"
    cmd = "dd if=/dev/zero of=%s bs=10M count=%d" % (host_path, count)
    guest_path = (tmp_dir +
                  "file_transfer-%s" % utils_misc.generate_random_string(8))

    try:
        error.context("Creating %dMB file on host" % filesize, logging.info)
        utils.run(cmd)

        if transfer_type != "remote":
            raise error.TestError("Unknown test file transfer mode %s" %
                                  transfer_type)

        error.context(
            "Transferring file host -> guest,"
            " timeout: %ss" % transfer_timeout, logging.info)
        t_begin = time.time()
        vm.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
        t_end = time.time()
        throughput = filesize / (t_end - t_begin)
        logging.info(
            "File transfer host -> guest succeed, "
            "estimated throughput: %.2fMB/s", throughput)

        error.context(
            "Transferring file guest -> host,"
            " timeout: %ss" % transfer_timeout, logging.info)
        t_begin = time.time()
        vm.copy_files_from(guest_path, host_path2, timeout=transfer_timeout)
        t_end = time.time()
        throughput = filesize / (t_end - t_begin)
        logging.info(
            "File transfer guest -> host succeed, "
            "estimated throughput: %.2fMB/s", throughput)

        error.context(
            "Compare md5sum between original file and"
            " transferred file", logging.info)
        if (utils.hash_file(host_path, method="md5") != utils.hash_file(
                host_path2, method="md5")):
            raise error.TestFail("File changed after transfer host -> guest "
                                 "and guest -> host")

    finally:
        logging.info('Cleaning temp file on guest')
        try:
            session.cmd("%s %s" % (clean_cmd, guest_path))
        except aexpect.ShellError, detail:
            logging.warn("Could not remove temp files in guest: '%s'", detail)

        logging.info('Cleaning temp files on host')
        try:
            os.remove(host_path)
            os.remove(host_path2)
        except OSError:
            pass
        session.close()
Exemplo n.º 20
0
def run(test, params, env):
    """
    Test Steps:

    1. boot up guest with this option sndbuf=1048576,...
    2. Transfer file between host and guest (by tcp,udp or both).
    3. Run netperf_udp with burst, check the guest works well.

    Params:
        :param test: QEMU test object.
        :param params: Dictionary with the test parameters.
        :param env: Dictionary with test environment.
    """
    def env_setup(session):
        """
        Linux guest env setup, install udt, set env and iptables
        """
        lib_path = "/usr/lib64"
        if '64' not in params.get("vm_arch_name", 'x86_64'):
            lib_path = "/usr/lib"
        cmd = r"git clone %s udt-git; " % params.get("udt_url")
        cmd += r"cd udt-git/udt4; make; make install; "
        cmd += r"cp -u src/*.so %s ; " % lib_path
        cmd += r"cp -u app/sendfile app/recvfile /usr/bin/; "
        cmd += r"iptables -I INPUT -p udp  -j ACCEPT; "
        cmd += r"iptables -I OUTPUT -p udp -j ACCEPT "
        if session.cmd_status(cmd):
            raise error.TestError("Install udt on guest failed")

    timeout = int(params.get("login_timeout", '360'))
    transfer_timeout = int(params.get("transfer_timeout", '120'))
    password = params.get("password")
    username = params.get("username")
    shell_port = params.get("shell_port")
    prompt = params.get("shell_prompt", "[\#\$]")
    client = params.get("shell_client")

    tmp_dir = params.get("tmp_dir", "/tmp/")
    host_file = (test.tmpdir + "tmp-%s" % utils_misc.generate_random_string(8))
    src_file = (tmp_dir + "src-%s" % utils_misc.generate_random_string(8))
    dst_file = (tmp_dir + "dst-%s" % utils_misc.generate_random_string(8))
    data_port = params.get("data_port", "9000")
    clean_cmd = params.get("clean_cmd", "rm -f")
    filesize = int(params.get("filesize", '100'))
    dd_cmd = params.get("dd_cmd", "dd if=/dev/urandom of=%s bs=1M count=%d")

    sessions = []
    addresses = []
    vms = []

    error.context("Init boot the vms")
    for vm_name in params.get("vms", "vm1 vm2 vm3 vm4").split():
        vms.append(env.get_vm(vm_name))
    for vm in vms:
        vm.verify_alive()
        sessions.append(vm.wait_for_login(timeout=timeout))
        addresses.append(vm.get_address())

    if params.get("copy_protocol", ""):
        logging.info("Creating %dMb file on host", filesize)
        cmd = dd_cmd % (host_file, filesize)
        utils.run(cmd)
        orig_md5 = utils.hash_file(host_file, method="md5")
    try:
        if "tcp" in params.get("copy_protocol", ""):
            error.context("Transfer data from host to each guest")
            for vm in vms:
                error.context(
                    "Transfer data from host to guest %s via tcp" % vm.name,
                    logging.info)
                vm.copy_files_to(host_file, src_file, timeout=transfer_timeout)
            for session in sessions:
                output = session.cmd_output("md5sum %s" % src_file)
                if orig_md5 not in output:
                    msg = "Md5sum mismatch, Md5 for original file:%s" % orig_md5
                    msg += "Md5sum command output in guest: %s" % output
                    raise error.TestFail(msg)

            error.context("Transfer data from guest to host by tcp")
            for vm in vms:
                error.context("Transfer date from guest %s to host" % vm.name,
                              logging.info)
                vm.copy_files_from(src_file,
                                   host_file,
                                   timeout=transfer_timeout)

                current_md5 = utils.hash_file(host_file, method="md5")
                if current_md5 != orig_md5:
                    raise error.TestError("Md5sum mismatch, ori:cur - %s:%s" %
                                          (orig_md5, current_md5))

        if "udp" in params.get("copy_protocol", ""):
            # transfer data between guest
            error.context("Transfer data between every guest by udp protocol")
            if params.get("os_type") == "linux":
                for session in sessions:
                    env_setup(session)

            for vm_src in addresses:
                for vm_dst in addresses:
                    if vm_src != vm_dst:
                        error.context(
                            "Transferring data %s to %s" % (vm_src, vm_dst),
                            logging.info)
                        remote.udp_copy_between_remotes(vm_src,
                                                        vm_dst,
                                                        shell_port,
                                                        password,
                                                        password,
                                                        username,
                                                        username,
                                                        src_file,
                                                        dst_file,
                                                        client,
                                                        prompt,
                                                        data_port,
                                                        timeout=1200)
        # do netperf test:
        sub_test = params.get("sub_test_name", 'netperf_udp')
        for vm in vms:
            params["main_vm"] = vm.name
            error.context("Run subtest %s " % sub_test, logging.info)
            utils_test.run_virt_sub_test(test, params, env, sub_type=sub_test)
            vm.wait_for_login(timeout=timeout)

    finally:
        utils.system("rm -rf %s " % host_file, ignore_status=True)
        for session in sessions:
            if session:
                session.cmd("%s %s %s" % (clean_cmd, src_file, dst_file),
                            ignore_all_errors=True)
                session.close()
Exemplo n.º 21
0
def _take_screendumps(test, params, env):
    global _screendump_thread_termination_event
    temp_dir = test.debugdir
    if params.get("screendump_temp_dir"):
        temp_dir = utils_misc.get_path(test.bindir,
                                       params.get("screendump_temp_dir"))
        try:
            os.makedirs(temp_dir)
        except OSError:
            pass
    temp_filename = os.path.join(
        temp_dir, "scrdump-%s.ppm" % utils_misc.generate_random_string(6))
    delay = float(params.get("screendump_delay", 5))
    quality = int(params.get("screendump_quality", 30))
    inactivity_treshold = float(params.get("inactivity_treshold", 1800))
    inactivity_watcher = params.get("inactivity_watcher", "log")

    cache = {}
    counter = {}
    inactivity = {}

    while True:
        for vm in env.get_all_vms():
            if vm.instance not in counter.keys():
                counter[vm.instance] = 0
            if vm.instance not in inactivity.keys():
                inactivity[vm.instance] = time.time()
            if not vm.is_alive():
                continue
            vm_pid = vm.get_pid()
            try:
                vm.screendump(filename=temp_filename, debug=False)
            except qemu_monitor.MonitorError, e:
                logging.warn(e)
                continue
            except AttributeError, e:
                logging.warn(e)
                continue
            if not os.path.exists(temp_filename):
                logging.warn("VM '%s' failed to produce a screendump", vm.name)
                continue
            if not ppm_utils.image_verify_ppm_file(temp_filename):
                logging.warn("VM '%s' produced an invalid screendump", vm.name)
                os.unlink(temp_filename)
                continue
            screendump_dir = os.path.join(
                test.debugdir, "screendumps_%s_%s" % (vm.name, vm_pid))
            try:
                os.makedirs(screendump_dir)
            except OSError:
                pass
            counter[vm.instance] += 1
            screendump_filename = os.path.join(
                screendump_dir, "%04d.jpg" % counter[vm.instance])
            vm.verify_bsod(screendump_filename)
            image_hash = utils.hash_file(temp_filename)
            if image_hash in cache:
                time_inactive = time.time() - inactivity[vm.instance]
                if time_inactive > inactivity_treshold:
                    msg = (
                        "%s screen is inactive for more than %d s (%d min)" %
                        (vm.name, time_inactive, time_inactive / 60))
                    if inactivity_watcher == "error":
                        try:
                            raise virt_vm.VMScreenInactiveError(
                                vm, time_inactive)
                        except virt_vm.VMScreenInactiveError:
                            logging.error(msg)
                            # Let's reset the counter
                            inactivity[vm.instance] = time.time()
                            test.background_errors.put(sys.exc_info())
                    elif inactivity_watcher == 'log':
                        logging.debug(msg)
                try:
                    os.link(cache[image_hash], screendump_filename)
                except OSError:
                    pass
            else:
                inactivity[vm.instance] = time.time()
                try:
                    try:
                        image = PIL.Image.open(temp_filename)
                        image.save(screendump_filename,
                                   format="JPEG",
                                   quality=quality)
                        cache[image_hash] = screendump_filename
                    except IOError, error_detail:
                        logging.warning(
                            "VM '%s' failed to produce a "
                            "screendump: %s", vm.name, error_detail)
                        # Decrement the counter as we in fact failed to
                        # produce a converted screendump
                        counter[vm.instance] -= 1
                except NameError:
                    pass
            os.unlink(temp_filename)
Exemplo n.º 22
0
        else:
            answer = 'y'
        if answer == 'y':
            utils.interactive_download(url, path, "JeOS x86_64 image")
            had_to_download = True
        else:
            logging.warning("Missing file %s", path)
    else:
        logging.info("Found %s", path)
        if sha1 is None:
            answer = 'n'
        else:
            answer = 'y'

        if answer == 'y':
            actual_sha1 = utils.hash_file(path, method='sha1')
            if actual_sha1 != sha1:
                logging.error("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = utils.ask("The file seems corrupted or outdated. "
                                       "Would you like to download it?")
                else:
                    answer = 'y'
                if answer == 'y':
                    logging.info("Updating image to the latest available...")
                    utils.interactive_download(url, path, title)
                    had_to_download = True
                    file_ok = True
            else:
                file_ok = True
                logging.info("SHA1 sum check OK")
Exemplo n.º 23
0
def _take_screendumps(test, params, env):
    global _screendump_thread_termination_event
    temp_dir = test.debugdir
    if params.get("screendump_temp_dir"):
        temp_dir = utils_misc.get_path(test.bindir,
                                      params.get("screendump_temp_dir"))
        try:
            os.makedirs(temp_dir)
        except OSError:
            pass
    temp_filename = os.path.join(temp_dir, "scrdump-%s.ppm" %
                                 utils_misc.generate_random_string(6))
    delay = float(params.get("screendump_delay", 5))
    quality = int(params.get("screendump_quality", 30))
    inactivity_treshold = float(params.get("inactivity_treshold", 1800))
    inactivity_watcher = params.get("inactivity_watcher", "log")

    cache = {}
    counter = {}
    inactivity = {}

    while True:
        for vm in env.get_all_vms():
            if vm not in counter.keys():
                counter[vm] = 0
            if vm not in inactivity.keys():
                inactivity[vm] = time.time()
            if not vm.is_alive():
                continue
            try:
                vm.screendump(filename=temp_filename, debug=False)
            except qemu_monitor.MonitorError, e:
                logging.warn(e)
                continue
            except AttributeError, e:
                logging.warn(e)
                continue
            if not os.path.exists(temp_filename):
                logging.warn("VM '%s' failed to produce a screendump", vm.name)
                continue
            if not ppm_utils.image_verify_ppm_file(temp_filename):
                logging.warn("VM '%s' produced an invalid screendump", vm.name)
                os.unlink(temp_filename)
                continue
            screendump_dir = os.path.join(test.debugdir,
                                          "screendumps_%s" % vm.name)
            try:
                os.makedirs(screendump_dir)
            except OSError:
                pass
            counter[vm] += 1
            screendump_filename = os.path.join(screendump_dir, "%04d.jpg" %
                                               counter[vm])
            image_hash = utils.hash_file(temp_filename)
            if image_hash in cache:
                time_inactive = time.time() - inactivity[vm]
                if time_inactive > inactivity_treshold:
                    msg = ("%s screen is inactive for more than %d s (%d min)" %
                           (vm.name, time_inactive, time_inactive/60))
                    if inactivity_watcher == "error":
                        try:
                            raise virt_vm.VMScreenInactiveError(vm,
                                                                time_inactive)
                        except virt_vm.VMScreenInactiveError:
                            logging.error(msg)
                            # Let's reset the counter
                            inactivity[vm] = time.time()
                            test.background_errors.put(sys.exc_info())
                    elif inactivity_watcher == 'log':
                        logging.debug(msg)
                try:
                    os.link(cache[image_hash], screendump_filename)
                except OSError:
                    pass
            else:
                inactivity[vm] = time.time()
                try:
                    try:
                        image = PIL.Image.open(temp_filename)
                        image.save(screendump_filename, format="JPEG",
                                   quality=quality)
                        cache[image_hash] = screendump_filename
                    except IOError, error_detail:
                        logging.warning("VM '%s' failed to produce a "
                                        "screendump: %s", vm.name, error_detail)
                        # Decrement the counter as we in fact failed to
                        # produce a converted screendump
                        counter[vm] -= 1
                except NameError:
                    pass
            os.unlink(temp_filename)
Exemplo n.º 24
0
            answer = 'y'
        if answer == 'y':
            utils.interactive_download(url, destination,
                                       "Downloading %s" % title)
            had_to_download = True
        else:
            logging.warning("Missing file %s", destination)
    else:
        logging.info("Found %s", destination)
        if sha1 is None:
            answer = 'n'
        else:
            answer = 'y'

        if answer == 'y':
            actual_sha1 = utils.hash_file(destination, method='sha1')
            if actual_sha1 != sha1:
                logging.info("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = utils.ask("The file seems corrupted or outdated. "
                                       "Would you like to download it?")
                else:
                    logging.info("The file seems corrupted or outdated")
                    answer = 'y'
                if answer == 'y':
                    logging.info("Updating image to the latest available...")
                    while not file_ok:
                        utils.interactive_download(url, destination, title)
                        sha1_post_download = utils.hash_file(destination,
                                                             method='sha1')
                        had_to_download = True
Exemplo n.º 25
0
def run_virtual_nic_send_buffer(test, params, env):
    """
    Test Steps:

    1. boot up guest with this option sndbuf=1048576,...
    2. Transfer file between host and guest (by tcp,udp or both).
    3. Run netperf_udp with burst, check the guest works well.

    Params:
        :param test: QEMU test object.
        :param params: Dictionary with the test parameters.
        :param env: Dictionary with test environment.
    """
    def env_setup(session):
        """
        Linux guest env setup, install udt, set env and iptables
        """
        lib_path = "/usr/lib64"
        if params.get("platform", 64) == 32:
            lib_path = "/usr/lib"
        cmd = r"git clone %s udt-git; " % params.get("udt_url")
        cmd += r"cd udt-git/udt4; make; make install; "
        cmd += r"cp -u src/*.so %s ; " % lib_path
        cmd += r"cp -u app/sendfile app/recvfile /usr/bin/; "
        cmd += r"iptables -I INPUT -p udp  -j ACCEPT; "
        cmd += r"iptables -I OUTPUT -p udp -j ACCEPT "
        if session.cmd_status(cmd):
            raise error.TestError("Install udt on guest failed")

    timeout = int(params.get("login_timeout", '360'))
    transfer_timeout = int(params.get("transfer_timeout", '120'))
    password = params.get("password")
    username = params.get("username")
    shell_port = params.get("shell_port")
    prompt = params.get("shell_prompt", "[\#\$]")
    client = params.get("shell_client")

    tmp_dir = params.get("tmp_dir", "/tmp/")
    host_file = (test.tmpdir + "tmp-%s" % utils_misc.generate_random_string(8))
    src_file = (tmp_dir + "src-%s" % utils_misc.generate_random_string(8))
    dst_file = (tmp_dir + "dst-%s" % utils_misc.generate_random_string(8))
    data_port = params.get("data_port", "9000")
    clean_cmd = params.get("clean_cmd", "rm -f")
    filesize = int(params.get("filesize", '100'))
    dd_cmd = params.get("dd_cmd", "dd if=/dev/urandom of=%s bs=1M count=%d")

    sessions = []
    addresses = []
    vms = []

    error.context("Init boot the vms")
    for vm_name in params.get("vms", "vm1 vm2 vm3 vm4").split():
        vms.append(env.get_vm(vm_name))
    for vm in vms:
        vm.verify_alive()
        sessions.append(vm.wait_for_login(timeout=timeout))
        addresses.append(vm.get_address())

    logging.info("Creating %dMb file on host", filesize)
    cmd = dd_cmd % (host_file, filesize)
    utils.run(cmd)
    orig_md5 = utils.hash_file(host_file, method="md5")

    try:
        if "tcp" in params.get("copy_protocol", ""):
            error.context("Transfer data from host to each guest")
            for vm in vms:
                error.context("Transfer data from host to guest %s via tcp" %
                              vm.name, logging.info)
                vm.copy_files_to(host_file, src_file, timeout=transfer_timeout)
            for session in sessions:
                output = session.cmd_output("md5sum %s" % src_file)
                if "such file" in output:
                    remote_hash = "0"
                elif output:
                    remote_hash = output.split()[0]
                else:
                    warn_msg = "MD5 check for remote path %s did not return."
                    logging.warning(warn_msg % src_file)
                    remote_hash = "0"
                if remote_hash != orig_md5:
                    raise error.TestError("Md5sum mismatch, ori:cur - %s:%s" %
                                          (orig_md5, remote_hash))

            error.context("Transfer data from guest to host by tcp")
            for vm in vms:
                error.context("Transfer date from guest %s to host" % vm.name,
                              logging.info)
                vm.copy_files_from(src_file, host_file,
                                   timeout=transfer_timeout)

                current_md5 = utils.hash_file(host_file, method="md5")
                if current_md5 != orig_md5:
                    raise error.TestError("Md5sum mismatch, ori:cur - %s:%s" %
                                          (orig_md5, remote_hash))

        if "udp" in params.get("copy_protocol", ""):
            # transfer data between guest
            error.context("Transfer data between every guest by udp protocol")
            if params.get("os_type") == "linux":
                for session in sessions:
                    env_setup(session)

            for vm_src in addresses:
                for vm_dst in addresses:
                    if vm_src != vm_dst:
                        error.context("Transferring data %s to %s" %
                                      (vm_src, vm_dst), logging.info)
                        remote.udp_copy_between_remotes(vm_src, vm_dst,
                                                        shell_port,
                                                        password, password,
                                                        username, username,
                                                        src_file, dst_file,
                                                        client, prompt,
                                                        data_port,
                                                        timeout=1200)
        # do netperf test:
        sub_test = params.get("sub_test_name", 'netperf_udp')
        for vm in vms:
            params["main_vm"] = vm.name
            error.context("Run subtest %s " % sub_test, logging.info)
            utils_test.run_virt_sub_test(test, params, env, sub_type=sub_test)
            vm.wait_for_login(timeout=timeout)

    finally:
        utils.system("rm -rf %s " % host_file, ignore_status=True)
        for session in sessions:
            if session:
                session.cmd("%s %s %s" % (clean_cmd, src_file, dst_file),
                            ignore_all_errors=True)
                session.close()
Exemplo n.º 26
0
def run_multi_vms_file_transfer(test, params, env):
    """
    Transfer a file back and forth between multi VMs for long time.

    1) Boot up two VMs .
    2) Create a large file by dd on host.
    3) Copy this file to VM1.
    4) Compare copied file's md5 with original file.
    5) Copy this file from VM1 to VM2.
    6) Compare copied file's md5 with original file.
    7) Copy this file from VM2 to VM1.
    8) Compare copied file's md5 with original file.
    9) Repeat step 5-8

    @param test: KVM test object.
    @param params: Dictionary with the test parameters.
    @param env: Dictionary with test environment.
    """
    def md5_check(session, orig_md5):
        msg = "Compare copied file's md5 with original file."
        error.context(msg, logging.info)
        md5_cmd = "md5sum %s | awk '{print $1}'" % guest_path
        s, o = session.cmd_status_output(md5_cmd)
        if s:
            msg = "Fail to get md5 value from guest. Output is %s" % o
            raise error.TestError(msg)
        new_md5 = o.splitlines()[-1]
        if new_md5 != orig_md5:
            msg = "File changed after transfer host -> VM1. Original md5 value"
            msg += " is %s. Current md5 value is %s" % (orig_md5, new_md5)
            raise error.TestFail(msg)

    vm1 = env.get_vm(params["main_vm"])
    vm1.verify_alive()
    login_timeout = int(params.get("login_timeout", 360))
    vm2 = env.get_vm(params["vms"].split()[-1])
    vm2.verify_alive()

    session_vm1 = vm1.wait_for_login(timeout=login_timeout)
    session_vm2 = vm2.wait_for_login(timeout=login_timeout)

    transfer_timeout = int(params.get("transfer_timeout", 1000))
    username = params.get("username")
    password = params.get("password")
    port = int(params.get("file_transfer_port"))
    if (not port) or (not username) or (not password):
        raise error.TestError("Please set file_transfer_port, username,"
                               " password paramters for guest")
    tmp_dir = params.get("tmp_dir", "/tmp/")
    repeat_time = int(params.get("repeat_time", "10"))
    clean_cmd = params.get("clean_cmd", "rm -f")
    filesize = int(params.get("filesize", 4000))
    count = int(filesize / 10)
    if count == 0:
        count = 1

    host_path = os.path.join(tmp_dir, "tmp-%s" %
                             utils_misc.generate_random_string(8))
    cmd = "dd if=/dev/zero of=%s bs=10M count=%d" % (host_path, count)
    guest_path = (tmp_dir + "file_transfer-%s" %
                  utils_misc.generate_random_string(8))
    try:
        error.context("Creating %dMB file on host" % filesize, logging.info)
        utils.run(cmd)
        orig_md5 = utils.hash_file(host_path, method="md5")
        error.context("Transfering file host -> VM1, timeout: %ss" % \
                       transfer_timeout, logging.info)
        t_begin = time.time()
        vm1.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
        t_end = time.time()
        throughput = filesize / (t_end - t_begin)
        logging.info("File transfer host -> VM1 succeed, "
                     "estimated throughput: %.2fMB/s", throughput)
        md5_check(session_vm1, orig_md5)

        ip_vm1 = vm1.get_address()
        ip_vm2 = vm2.get_address()
        for i in range(repeat_time):
            log_vm1 = os.path.join(test.debugdir, "remote_scp_to_vm1_%s.log" %i)
            log_vm2 = os.path.join(test.debugdir, "remote_scp_to_vm2_%s.log" %i)

            msg = "Transfering file VM1 -> VM2, timeout: %ss." % transfer_timeout
            msg += " Repeat: %s/%s" % (i + 1, repeat_time)
            error.context(msg, logging.info)
            t_begin = time.time()
            s = remote.scp_between_remotes(src=ip_vm1, dst=ip_vm2, port=port,
                                       s_passwd=password, d_passwd=password,
                                       s_name=username, d_name=username,
                                       s_path=guest_path, d_path=guest_path,
                                       timeout=transfer_timeout,
                                       log_filename=log_vm1)
            t_end = time.time()
            throughput = filesize / (t_end - t_begin)
            logging.info("File transfer VM1 -> VM2 succeed, "
                         "estimated throughput: %.2fMB/s", throughput)
            md5_check(session_vm2, orig_md5)
            session_vm1.cmd("rm -rf %s" % guest_path)

            msg = "Transfering file VM2 -> VM1, timeout: %ss." % transfer_timeout
            msg += " Repeat: %s/%s" % (i + 1, repeat_time)

            error.context(msg,  logging.info)
            t_begin = time.time()
            remote.scp_between_remotes(src=ip_vm2, dst=ip_vm1, port=port,
                                       s_passwd=password, d_passwd=password,
                                       s_name=username, d_name=username,
                                       s_path=guest_path, d_path=guest_path,
                                       timeout=transfer_timeout,
                                       log_filename=log_vm1)
            t_end = time.time()
            throughput = filesize / (t_end - t_begin)
            logging.info("File transfer VM2 -> VM1 succeed, "
                         "estimated throughput: %.2fMB/s", throughput)
            md5_check(session_vm1, orig_md5)
            session_vm2.cmd("%s %s" % (clean_cmd, guest_path))

    finally:
        try:
            session_vm1.cmd("%s %s" % (clean_cmd, guest_path))
        except Exception:
            pass
        try:
            session_vm2.cmd("%s %s" % (clean_cmd, guest_path))
        except Exception:
            pass
        try:
            os.remove(host_path)
        except OSError:
            pass
        if session_vm1:
            session_vm1.close()
        if session_vm2:
            session_vm2.close()
Exemplo n.º 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)
Exemplo n.º 28
0
def run(test, params, env):
    """
    Test nic driver load/unload.

    1) Boot a VM.
    2) Get the NIC driver name.
    3) Multi-session TCP transfer on test interface.
    4) Repeatedly unload/load NIC driver during file transfer.
    5) Check whether the test interface should still work.

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def reset_guest_udevrules(session, rules_file, rules_content):
        """
        Write guest udev rules, then reboot the guest and
        return the new session
        """
        set_cmd = "echo '%s' > %s" % (rules_content, rules_file)
        session.cmd_output_safe(set_cmd)
        return vm.reboot()

    def all_threads_done(threads):
        """
        Check whether all threads have finished
        """
        for thread in threads:
            if thread.isAlive():
                return False
            else:
                continue
        return True

    def all_threads_alive(threads):
        """
        Check whether all threads is alive
        """
        for thread in threads:
            if not thread.isAlive():
                return False
            else:
                continue
        return True

    timeout = int(params.get("login_timeout", 360))
    transfer_timeout = int(params.get("transfer_timeout", 1000))
    filesize = int(params.get("filesize", 512))

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    session = vm.wait_for_login(timeout=timeout)
    vm_mac_address = vm.get_mac_address()
    udev_rules_file = "/etc/udev/rules.d/70-persistent-net.rules"
    rules = params.get("rules")
    if not session.cmd_status("[ -e %s ]" % udev_rules_file):
        if not rules:
            raise error.TestNAError("You must set udev rules before test")
        rules = rules % vm_mac_address
        session = reset_guest_udevrules(session, udev_rules_file, rules)

    error.base_context("Test env prepare")
    error.context("Get NIC interface name in guest.", logging.info)
    ethname = utils_net.get_linux_ifname(session, vm.get_mac_address(0))
    # get ethernet driver from '/sys' directory.
    # ethtool can do the same thing and doesn't care about os type.
    # if we make sure all guests have ethtool, we can make a change here.
    sys_path = params.get("sys_path") % (ethname)
    # readlink in RHEL4.8 doesn't have '-e' param, should use '-f' in RHEL4.8.
    readlink_cmd = params.get("readlink_command", "readlink -e")
    driver = os.path.basename(session.cmd("%s %s" % (readlink_cmd,
                                                     sys_path)).strip())
    logging.info("The guest interface %s using driver %s" % (ethname, driver))

    error.context("Host test file prepare, create %dMB file on host" %
                  filesize, logging.info)
    tmp_dir = data_dir.get_tmp_dir()
    host_path = os.path.join(tmp_dir, "host_file_%s" %
                             utils_misc.generate_random_string(8))
    guest_path = os.path.join("/home", "guest_file_%s" %
                              utils_misc.generate_random_string(8))
    cmd = "dd if=/dev/zero of=%s bs=1M count=%d" % (host_path, filesize)
    utils.run(cmd)
    file_checksum = utils.hash_file(host_path, "md5")

    error.context("Guest test file prepare, Copy file %s from host to guest"
                  % host_path, logging.info)
    vm.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
    if session.cmd_status("md5sum %s | grep %s" %
                          (guest_path, file_checksum)):
        raise error.TestNAError("File MD5SUMs changed after copy to guest")
    logging.info("Test env prepare successfully")

    error.base_context("Nic driver load/unload testing", logging.info)
    session_serial = vm.wait_for_serial_login(timeout=timeout)
    try:
        error.context("Transfer file between host and guest", logging.info)
        threads = []
        file_paths = []
        host_file_paths = []
        for sess_index in range(int(params.get("sessions_num", "10"))):
            sess_path = os.path.join("/home", "dst-%s" % sess_index)
            host_sess_path = os.path.join(tmp_dir, "dst-%s" % sess_index)

            thread1 = utils.InterruptedThread(vm.copy_files_to,
                                              (host_path, sess_path),
                                              {"timeout": transfer_timeout})

            thread2 = utils.InterruptedThread(vm.copy_files_from,
                                              (guest_path, host_sess_path),
                                              {"timeout": transfer_timeout})
            thread1.start()
            threads.append(thread1)
            thread2.start()
            threads.append(thread2)
            file_paths.append(sess_path)
            host_file_paths.append(host_sess_path)

        utils_misc.wait_for(lambda: all_threads_alive(threads), 60, 10, 1)

        time.sleep(5)
        error.context("Repeatedly unload/load NIC driver during file transfer",
                      logging.info)
        while not all_threads_done(threads):
            error.context("Shutdown the driver for NIC interface.",
                          logging.info)
            session_serial.cmd_output_safe("ifconfig %s down" % ethname)
            error.context("Unload  NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe -r %s" % driver)
            error.context("Load NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe %s" % driver)
            error.context("Activate NIC driver.", logging.info)
            session_serial.cmd_output_safe("ifconfig %s up" % ethname)
            session_serial.cmd_output_safe("sleep %s" % random.randint(10, 60))

        # files md5sums check
        error.context("File transfer finished, checking files md5sums",
                      logging.info)
        err_info = []
        for copied_file in file_paths:
            if session_serial.cmd_status("md5sum %s | grep %s" %
                                         (copied_file, file_checksum)):
                err_msg = "Guest file %s md5sum changed"
                err_info.append(err_msg % copied_file)
        for copied_file in host_file_paths:
            if utils.system("md5sum %s | grep %s" %
                            (copied_file, file_checksum)):
                err_msg = "Host file %s md5sum changed"
                err_info.append(err_msg % copied_file)
        if err_info:
            raise error.TestError("files MD5SUMs changed after copying %s" %
                                  err_info)
    except Exception:
        for thread in threads:
            thread.join(suppress_exception=True)
            raise
    else:
        for thread in threads:
            thread.join()
        for copied_file in file_paths:
            session_serial.cmd("rm -rf %s" % copied_file)
        for copied_file in host_file_paths:
            utils.system("rm -rf %s" % copied_file)
        session_serial.cmd("%s %s" % ("rm -rf", guest_path))
        os.remove(host_path)
        session.close()
        session_serial.close()
Exemplo n.º 29
0
    def create(self, name=None, params=None, root_dir=None, timeout=5.0,
               migration_mode=None, mac_source=None):
        """
        Start the VM by running a qemu command.
        All parameters are optional. If name, params or root_dir are not
        supplied, the respective values stored as class attributes are used.

        @param name: The name of the object
        @param params: A dict containing VM params
        @param root_dir: Base directory for relative filenames
        @param migration_mode: If supplied, start VM for incoming migration
                using this protocol (either 'tcp', 'unix' or 'exec')
        @param migration_exec_cmd: Command to embed in '-incoming "exec: ..."'
                (e.g. 'gzip -c -d filename') if migration_mode is 'exec'
        @param mac_source: A VM object from which to copy MAC addresses. If not
                specified, new addresses will be generated.

        @raise VMCreateError: If qemu terminates unexpectedly
        @raise VMKVMInitError: If KVM initialization fails
        @raise VMHugePageError: If hugepage initialization fails
        @raise VMImageMissingError: If a CD image is missing
        @raise VMHashMismatchError: If a CD image hash has doesn't match the
                expected hash
        @raise VMBadPATypeError: If an unsupported PCI assignment type is
                requested
        @raise VMPAError: If no PCI assignable devices could be assigned
        """
        error.context("creating '%s'" % self.name)
        self.destroy(free_mac_addresses=False)

        if name is not None:
            self.name = name
        if params is not None:
            self.params = params
        if root_dir is not None:
            self.root_dir = root_dir
        name = self.name
        params = self.params
        root_dir = self.root_dir

        # Verify the md5sum of the ISO images
        for cdrom in params.objects("cdroms"):
            if params.get("medium") == "import":
                break
            cdrom_params = params.object_params(cdrom)
            iso = cdrom_params.get("cdrom")
            if ((self.driver_type == 'xen') and
                (params.get('hvm_or_pv') == 'pv') and
                (os.path.basename(iso) == 'ks.iso')):
                continue
            if iso:
                iso = utils_misc.get_path(data_dir.get_data_dir(), iso)
                if not os.path.exists(iso):
                    raise virt_vm.VMImageMissingError(iso)
                compare = False
                if cdrom_params.get("md5sum_1m"):
                    logging.debug("Comparing expected MD5 sum with MD5 sum of "
                                  "first MB of ISO file...")
                    actual_hash = utils.hash_file(iso, 1048576, method="md5")
                    expected_hash = cdrom_params.get("md5sum_1m")
                    compare = True
                elif cdrom_params.get("md5sum"):
                    logging.debug("Comparing expected MD5 sum with MD5 sum of "
                                  "ISO file...")
                    actual_hash = utils.hash_file(iso, method="md5")
                    expected_hash = cdrom_params.get("md5sum")
                    compare = True
                elif cdrom_params.get("sha1sum"):
                    logging.debug("Comparing expected SHA1 sum with SHA1 sum "
                                  "of ISO file...")
                    actual_hash = utils.hash_file(iso, method="sha1")
                    expected_hash = cdrom_params.get("sha1sum")
                    compare = True
                if compare:
                    if actual_hash == expected_hash:
                        logging.debug("Hashes match")
                    else:
                        raise virt_vm.VMHashMismatchError(actual_hash,
                                                          expected_hash)

        # Make sure the following code is not executed by more than one thread
        # at the same time
        lockfile = open("/tmp/libvirt-autotest-vm-create.lock", "w+")
        fcntl.lockf(lockfile, fcntl.LOCK_EX)

        try:
            # Handle port redirections
            redir_names = params.objects("redirs")
            host_ports = utils_misc.find_free_ports(5000, 6000, len(redir_names))
            self.redirs = {}
            for i in range(len(redir_names)):
                redir_params = params.object_params(redir_names[i])
                guest_port = int(redir_params.get("guest_port"))
                self.redirs[guest_port] = host_ports[i]

            # Find available PCI devices
            self.pci_devices = []
            for device in params.objects("pci_devices"):
                self.pci_devices.append(device)

            # Find available VNC port, if needed
            if params.get("display") == "vnc":
                if params.get("vnc_autoport") == "yes":
                    self.vnc_port = None
                    self.vnc_autoport = True
                else:
                    self.vnc_port = utils_misc.find_free_port(5900, 6100)
                    self.vnc_autoport = False

            # Find available spice port, if needed
            if params.get("spice"):
                self.spice_port = utils_misc.find_free_port(8000, 8100)

            # Find random UUID if specified 'uuid = random' in config file
            if params.get("uuid") == "random":
                f = open("/proc/sys/kernel/random/uuid")
                self.uuid = f.read().strip()
                f.close()

            # Generate or copy MAC addresses for all NICs
            for nic in self.virtnet:
                nic_params = dict(nic)
                if mac_source:
                    # Will raise exception if source doesn't
                    # have cooresponding nic
                    logging.debug("Copying mac for nic %s from VM %s"
                                    % (nic.nic_name, mac_source.nam))
                    nic_params['mac'] = mac_source.get_mac_address(nic.nic_name)
                # make_create_command() calls vm.add_nic (i.e. on a copy)
                nic = self.add_nic(**nic_params)
                logging.debug('VM.create activating nic %s' % nic)
                self.activate_nic(nic.nic_name)

            # Make qemu command
            install_command = self.make_create_command()

            logging.info("Running libvirt command (reformatted):")
            for item in install_command.replace(" -", " \n    -").splitlines():
                logging.info("%s", item)
            utils.run(install_command, verbose=False)
            # Wait for the domain to be created
            utils_misc.wait_for(func=self.is_alive, timeout=60,
                                text=("waiting for domain %s to start" %
                                      self.name))
            self.uuid = virsh.domuuid(self.name, uri=self.connect_uri)

            # Establish a session with the serial console
            if self.only_pty == True:
                self.serial_console = aexpect.ShellSession(
                    "virsh console %s" % self.name,
                    auto_close=False,
                    output_func=utils_misc.log_line,
                    output_params=("serial-%s.log" % name,))
            else:
                self.serial_console = aexpect.ShellSession(
                    "tail -f %s" % self.get_serial_console_filename(),
                    auto_close=False,
                    output_func=utils_misc.log_line,
                    output_params=("serial-%s.log" % name,))

        finally:
            fcntl.lockf(lockfile, fcntl.LOCK_UN)
            lockfile.close()
Exemplo n.º 30
0
        else:
            answer = 'y'
        if answer == 'y':
            utils.interactive_download(url, destination, "Downloading %s" % title)
            had_to_download = True
        else:
            logging.warning("Missing file %s", destination)
    else:
        logging.info("Found %s", destination)
        if sha1 is None:
            answer = 'n'
        else:
            answer = 'y'

        if answer == 'y':
            actual_sha1 = utils.hash_file(destination, method='sha1')
            if actual_sha1 != sha1:
                logging.error("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = utils.ask("The file seems corrupted or outdated. "
                                       "Would you like to download it?")
                else:
                    answer = 'y'
                if answer == 'y':
                    logging.info("Updating image to the latest available...")
                    utils.interactive_download(url, destination, title)
                    had_to_download = True
                    file_ok = True
            else:
                file_ok = True
                logging.info("SHA1 sum check OK")
Exemplo n.º 31
0
def run_nicdriver_unload(test, params, env):
    """
    Test nic driver load/unload.

    1) Boot a VM.
    2) Get the NIC driver name.
    3) Multi-session TCP transfer on test interface.
    4) Repeatedly unload/load NIC driver during file transfer.
    5) Check whether the test interface should still work.

    :param test: QEMU test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    def all_threads_done(threads):
        for thread in threads:
            if thread.isAlive():
                return False
            else:
                continue
        return True

    def all_threads_alive(threads):
        for thread in threads:
            if not thread.isAlive():
                return False
            else:
                continue
        return True

    timeout = int(params.get("login_timeout", 360))
    transfer_timeout = int(params.get("transfer_timeout", 1000))
    filesize = int(params.get("filesize", 512))

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

    error.base_context("Test env prepare")
    error.context("Get NIC interface name in guest.", logging.info)
    ethname = utils_net.get_linux_ifname(session, vm.get_mac_address(0))
    # get ethernet driver from '/sys' directory.
    # ethtool can do the same thing and doesn't care about os type.
    # if we make sure all guests have ethtool, we can make a change here.
    sys_path = params.get("sys_path") % (ethname)
    # readlink in RHEL4.8 doesn't have '-e' param, should use '-f' in RHEL4.8.
    readlink_cmd = params.get("readlink_command", "readlink -e")
    driver = os.path.basename(
        session.cmd("%s %s" % (readlink_cmd, sys_path)).strip())
    logging.info("The guest interface %s using driver %s" % (ethname, driver))

    error.context(
        "Host test file prepare, create %dMB file on host" % filesize,
        logging.info)
    tmp_dir = data_dir.get_tmp_dir()
    host_path = os.path.join(
        tmp_dir, "host_file_%s" % utils_misc.generate_random_string(8))
    guest_path = os.path.join(
        "/home", "guest_file_%s" % utils_misc.generate_random_string(8))
    cmd = "dd if=/dev/zero of=%s bs=1M count=%d" % (host_path, filesize)
    utils.run(cmd)
    file_checksum = utils.hash_file(host_path, "md5")

    error.context(
        "Guest test file prepare, Copy file %s from host to guest" % host_path,
        logging.info)
    vm.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
    if session.cmd_status("md5sum %s | grep %s" % (guest_path, file_checksum)):
        raise error.TestNAError("File MD5SUMs changed after copy to guest")
    logging.info("Test env prepare successfully")

    error.base_context("Nic driver load/unload testing", logging.info)
    session_serial = vm.wait_for_serial_login(timeout=timeout)
    try:
        error.context("Transfer file between host and guest", logging.info)
        threads = []
        file_paths = []
        host_file_paths = []
        for sess_index in range(int(params.get("sessions_num", "10"))):
            sess_path = os.path.join("/home", "dst-%s" % sess_index)
            host_sess_path = os.path.join(tmp_dir, "dst-%s" % sess_index)

            thread1 = utils.InterruptedThread(vm.copy_files_to,
                                              (host_path, sess_path),
                                              {"timeout": transfer_timeout})

            thread2 = utils.InterruptedThread(vm.copy_files_from,
                                              (guest_path, host_sess_path),
                                              {"timeout": transfer_timeout})
            thread1.start()
            threads.append(thread1)
            thread2.start()
            threads.append(thread2)
            file_paths.append(sess_path)
            host_file_paths.append(host_sess_path)

        utils_misc.wait_for(lambda: all_threads_alive(threads), 60, 10, 1)

        time.sleep(5)
        error.context("Repeatedly unload/load NIC driver during file transfer",
                      logging.info)
        while not all_threads_done(threads):
            error.context("Shutdown the driver for NIC interface.",
                          logging.info)
            session_serial.cmd_output_safe("ifconfig %s down" % ethname)
            error.context("Unload  NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe -r %s" % driver)
            error.context("Load NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe %s" % driver)
            error.context("Activate NIC driver.", logging.info)
            session_serial.cmd_output_safe("ifconfig %s up" % ethname)
            session_serial.cmd_output_safe("sleep %s" % random.randint(10, 60))

        # files md5sums check
        error.context("File transfer finished, checking files md5sums",
                      logging.info)
        err_info = []
        for copied_file in file_paths:
            if session_serial.cmd_status("md5sum %s | grep %s" %
                                         (copied_file, file_checksum)):
                err_msg = "Guest file %s md5sum changed"
                err_info.append(err_msg % copied_file)
        for copied_file in host_file_paths:
            if utils.system("md5sum %s | grep %s" %
                            (copied_file, file_checksum)):
                err_msg = "Host file %s md5sum changed"
                err_info.append(err_msg % copied_file)
        if err_info:
            raise error.TestError("files MD5SUMs changed after copying %s" %
                                  err_info)
    except Exception:
        for thread in threads:
            thread.join(suppress_exception=True)
            raise
    else:
        for thread in threads:
            thread.join()
        for copied_file in file_paths:
            session_serial.cmd("rm -rf %s" % copied_file)
        for copied_file in host_file_paths:
            utils.system("rm -rf %s" % copied_file)
        session_serial.cmd("%s %s" % ("rm -rf", guest_path))
        os.remove(host_path)
        session.close()
        session_serial.close()
Exemplo n.º 32
0
def migrate(vm, env=None, mig_timeout=3600, mig_protocol="tcp",
            mig_cancel=False, offline=False, stable_check=False,
            clean=False, save_path=None, dest_host='localhost', mig_port=None):
    """
    Migrate a VM locally and re-register it in the environment.

    :param vm: The VM to migrate.
    :param env: The environment dictionary.  If omitted, the migrated VM will
            not be registered.
    :param mig_timeout: timeout value for migration.
    :param mig_protocol: migration protocol
    :param mig_cancel: Test migrate_cancel or not when protocol is tcp.
    :param dest_host: Destination host (defaults to 'localhost').
    :param mig_port: Port that will be used for migration.
    :return: The post-migration VM, in case of same host migration, True in
            case of multi-host migration.
    """
    def mig_finished():
        if dest_vm.is_dead():
            raise error.TestFail("Dest VM died during migration.")
        if not offline and vm.is_dead():
            raise error.TestFail("Source VM died during migration")
        try:
            o = vm.monitor.info("migrate")
            if isinstance(o, str):
                return "status: active" not in o
            else:
                return o.get("status") != "active"
        except Exception:
            pass

    def mig_succeeded():
        o = vm.monitor.info("migrate")
        if isinstance(o, str):
            return "status: completed" in o
        else:
            return o.get("status") == "completed"

    def mig_failed():
        o = vm.monitor.info("migrate")
        if isinstance(o, str):
            return "status: failed" in o
        else:
            return o.get("status") == "failed"

    def mig_cancelled():
        o = vm.monitor.info("migrate")
        if isinstance(o, str):
            return ("Migration status: cancelled" in o or
                    "Migration status: canceled" in o)
        else:
            return (o.get("status") == "cancelled" or
                    o.get("status") == "canceled")

    def wait_for_migration():
        if not utils_misc.wait_for(mig_finished, mig_timeout, 2, 2,
                                   "Waiting for migration to finish"):
            raise error.TestFail("Timeout expired while waiting for migration "
                                 "to finish")

    if dest_host == 'localhost':
        dest_vm = vm.clone()

    if (dest_host == 'localhost') and stable_check:
        # Pause the dest vm after creation
        _ = dest_vm.params.get('extra_params', '') + ' -S'
        dest_vm.params['extra_params'] = _

    if dest_host == 'localhost':
        dest_vm.create(migration_mode=mig_protocol, mac_source=vm)

    try:
        try:
            if mig_protocol in ["tcp", "rdma", "x-rdma"]:
                if dest_host == 'localhost':
                    uri = mig_protocol + ":0:%d" % dest_vm.migration_port
                else:
                    uri = mig_protocol + ':%s:%d' % (dest_host, mig_port)
            elif mig_protocol == "unix":
                uri = "unix:%s" % dest_vm.migration_file
            elif mig_protocol == "exec":
                uri = '"exec:nc localhost %s"' % dest_vm.migration_port

            if offline:
                vm.pause()
            vm.monitor.migrate(uri)

            if mig_cancel:
                time.sleep(2)
                vm.monitor.cmd("migrate_cancel")
                if not utils_misc.wait_for(mig_cancelled, 60, 2, 2,
                                           "Waiting for migration "
                                           "cancellation"):
                    raise error.TestFail("Failed to cancel migration")
                if offline:
                    vm.resume()
                if dest_host == 'localhost':
                    dest_vm.destroy(gracefully=False)
                return vm
            else:
                wait_for_migration()
                if (dest_host == 'localhost') and stable_check:
                    save_path = None or "/tmp"
                    save1 = os.path.join(save_path, "src")
                    save2 = os.path.join(save_path, "dst")

                    vm.save_to_file(save1)
                    dest_vm.save_to_file(save2)

                    # Fail if we see deltas
                    md5_save1 = utils.hash_file(save1)
                    md5_save2 = utils.hash_file(save2)
                    if md5_save1 != md5_save2:
                        raise error.TestFail("Mismatch of VM state before "
                                             "and after migration")

                if (dest_host == 'localhost') and offline:
                    dest_vm.resume()
        except Exception:
            if dest_host == 'localhost':
                dest_vm.destroy()
            raise

    finally:
        if (dest_host == 'localhost') and stable_check and clean:
            logging.debug("Cleaning the state files")
            if os.path.isfile(save1):
                os.remove(save1)
            if os.path.isfile(save2):
                os.remove(save2)

    # Report migration status
    if mig_succeeded():
        logging.info("Migration finished successfully")
    elif mig_failed():
        raise error.TestFail("Migration failed")
    else:
        status = vm.monitor.info("migrate")
        raise error.TestFail("Migration ended with unknown status: %s" %
                             status)

    if dest_host == 'localhost':
        if dest_vm.monitor.verify_status("paused"):
            logging.debug("Destination VM is paused, resuming it")
            dest_vm.resume()

    # Kill the source VM
    vm.destroy(gracefully=False)

    # Replace the source VM with the new cloned VM
    if (dest_host == 'localhost') and (env is not None):
        env.register_vm(vm.name, dest_vm)

    # Return the new cloned VM
    if dest_host == 'localhost':
        return dest_vm
    else:
        return vm
Exemplo n.º 33
0
def run(test, params, env):
    """
    Test Step
        1. boot up two virtual machine
        2. Transfer data: host <--> guest1 <--> guest2 <-->host via ipv6
        3. after data transfer, check data have no change
    Params:
        :param test: QEMU test object
        :param params: Dictionary with the test parameters
        :param env: Dictionary with test environment.
    """
    timeout = int(params.get("login_timeout", '360'))
    client = params.get("file_transfer_client")
    port = params.get("file_transfer_port")
    password = params.get("password")
    username = params.get("username")
    tmp_dir = params.get("tmp_dir", "/tmp/")
    filesize = int(params.get("filesize", '4096'))
    dd_cmd = params.get("dd_cmd")
    file_trans_timeout = int(params.get("file_trans_timeout", '1200'))
    file_md5_check_timeout = int(params.get("file_md5_check_timeout", '600'))

    def get_linux_ipv6_linklocal_address(ifname, session=None):
        """
        Get host/guest ipv6 linklocal address via ifname
        """
        if session is not None:
            o = session.cmd_output("ifconfig %s" % ifname)
        else:
            o = utils.system_output("ifconfig %s" % ifname)
        ipv6_address_reg = re.compile(r"(fe80::[^\s|/]*)")
        if o:
            ipv6_linklocal_address = ipv6_address_reg.findall(o)
            if not ipv6_linklocal_address:
                raise error.TestError("Can't get %s linklocal address"
                                      % ifname)
            return ipv6_linklocal_address[0]
        else:
            return None

    def get_file_md5sum(file_name, session, timeout):
        """
        Get file md5sum from guest.
        """
        logging.info("Get md5sum of the file:'%s'" % file_name)
        try:
            o = session.cmd_output("md5sum %s" % file_name, timeout=timeout)
            file_md5sum = re.findall("\w+", o)[0]
        except IndexError:
            raise error.TestError("Could not get file md5sum in guest")
        return file_md5sum

    sessions = {}
    addresses = {}
    inet_name = {}
    vms = []

    error.context("Boot vms for test", logging.info)
    for vm_name in params.get("vms", "vm1 vm2").split():
        vms.append(env.get_vm(vm_name))

    # config ipv6 address host and guest.
    host_ifname = params.get("netdst")

    for vm in vms:
        vm.verify_alive()
        sessions[vm] = vm.wait_for_login(timeout=timeout)
        inet_name[vm] = utils_net.get_linux_ifname(sessions[vm],
                                                   vm.get_mac_address())
        addresses[vm] = get_linux_ipv6_linklocal_address(inet_name[vm],
                                                         sessions[vm])

    # prepare test data
    guest_path = (tmp_dir + "src-%s" % utils_misc.generate_random_string(8))
    dest_path = (tmp_dir + "dst-%s" % utils_misc.generate_random_string(8))
    host_path = os.path.join(test.tmpdir, "tmp-%s" %
                             utils_misc.generate_random_string(8))
    logging.info("Test setup: Creating %dMB file on host", filesize)
    utils.run(dd_cmd % (host_path, filesize))

    try:
        src_md5 = (utils.hash_file(host_path, method="md5"))
        # transfer data
        for vm in vms:
            error.context("Transfer data from host to %s" % vm.name,
                          logging.info)
            remote.copy_files_to(addresses[vm],
                                 client, username, password, port,
                                 host_path, guest_path,
                                 timeout=file_trans_timeout,
                                 interface=host_ifname)
            dst_md5 = get_file_md5sum(guest_path, sessions[vm],
                                      timeout=file_md5_check_timeout)
            if dst_md5 != src_md5:
                raise error.TestFail("File changed after transfer host -> %s"
                                     % vm.name)

        for vm_src in addresses:
            for vm_dst in addresses:
                if vm_src != vm_dst:
                    error.context("Transferring data from %s to %s" %
                                  (vm_src.name, vm_dst.name), logging.info)
                    remote.scp_between_remotes(addresses[vm_src],
                                               addresses[vm_dst],
                                               port, password, password,
                                               username, username,
                                               guest_path, dest_path,
                                               timeout=file_trans_timeout,
                                               src_inter=host_ifname,
                                               dst_inter=inet_name[vm_src])
                    dst_md5 = get_file_md5sum(dest_path, sessions[vm_dst],
                                              timeout=file_md5_check_timeout)
                    if dst_md5 != src_md5:
                        raise error.TestFail("File changed transfer %s -> %s"
                                             % (vm_src.name, vm_dst.name))

        for vm in vms:
            error.context("Transfer data from %s to host" % vm.name,
                          logging.info)
            remote.copy_files_from(addresses[vm],
                                   client, username, password, port,
                                   dest_path, host_path,
                                   timeout=file_trans_timeout,
                                   interface=host_ifname)
            error.context("Check whether the file changed after trans",
                          logging.info)
            dst_md5 = (utils.hash_file(host_path, method="md5"))
            if dst_md5 != src_md5:
                raise error.TestFail("File changed after transfer (md5sum mismatch)")
            utils.system_output("rm -rf %s" % host_path, timeout=timeout)

    finally:
        utils.system("rm -rf %s" % host_path, timeout=timeout,
                     ignore_status=True)
        for vm in vms:
            sessions[vm].cmd("rm -rf %s %s || true" % (guest_path, dest_path),
                             timeout=timeout, ignore_all_errors=True)
            sessions[vm].close()
Exemplo n.º 34
0
def migrate(vm, env=None, mig_timeout=3600, mig_protocol="tcp",
            mig_cancel=False, offline=False, stable_check=False,
            clean=False, save_path=None, dest_host='localhost', mig_port=None):
    """
    Migrate a VM locally and re-register it in the environment.

    :param vm: The VM to migrate.
    :param env: The environment dictionary.  If omitted, the migrated VM will
            not be registered.
    :param mig_timeout: timeout value for migration.
    :param mig_protocol: migration protocol
    :param mig_cancel: Test migrate_cancel or not when protocol is tcp.
    :param dest_host: Destination host (defaults to 'localhost').
    :param mig_port: Port that will be used for migration.
    :return: The post-migration VM, in case of same host migration, True in
            case of multi-host migration.
    """
    def mig_finished():
        try:
            o = vm.monitor.info("migrate")
            if isinstance(o, str):
                return "status: active" not in o
            else:
                return o.get("status") != "active"
        except Exception:
            pass

    def mig_succeeded():
        o = vm.monitor.info("migrate")
        if isinstance(o, str):
            return "status: completed" in o
        else:
            return o.get("status") == "completed"

    def mig_failed():
        o = vm.monitor.info("migrate")
        if isinstance(o, str):
            return "status: failed" in o
        else:
            return o.get("status") == "failed"

    def mig_cancelled():
        o = vm.monitor.info("migrate")
        if isinstance(o, str):
            return ("Migration status: cancelled" in o or
                    "Migration status: canceled" in o)
        else:
            return (o.get("status") == "cancelled" or
                    o.get("status") == "canceled")

    def wait_for_migration():
        if not utils_misc.wait_for(mig_finished, mig_timeout, 2, 2,
                                   "Waiting for migration to finish"):
            raise error.TestFail("Timeout expired while waiting for migration "
                                 "to finish")

    if dest_host == 'localhost':
        dest_vm = vm.clone()

    if (dest_host == 'localhost') and stable_check:
        # Pause the dest vm after creation
        dest_vm.params['extra_params'] = (dest_vm.params.get('extra_params', '')
                                          + ' -S')

    if dest_host == 'localhost':
        dest_vm.create(migration_mode=mig_protocol, mac_source=vm)

    try:
        try:
            if mig_protocol in ["tcp", "rdma", "x-rdma"]:
                if dest_host == 'localhost':
                    uri = mig_protocol + ":0:%d" % dest_vm.migration_port
                else:
                    uri = mig_protocol + ':%s:%d' % (dest_host, mig_port)
            elif mig_protocol == "unix":
                uri = "unix:%s" % dest_vm.migration_file
            elif mig_protocol == "exec":
                uri = '"exec:nc localhost %s"' % dest_vm.migration_port

            if offline:
                vm.pause()
            vm.monitor.migrate(uri)

            if mig_cancel:
                time.sleep(2)
                vm.monitor.cmd("migrate_cancel")
                if not utils_misc.wait_for(mig_cancelled, 60, 2, 2,
                                           "Waiting for migration "
                                           "cancellation"):
                    raise error.TestFail("Failed to cancel migration")
                if offline:
                    vm.resume()
                if dest_host == 'localhost':
                    dest_vm.destroy(gracefully=False)
                return vm
            else:
                wait_for_migration()
                if (dest_host == 'localhost') and stable_check:
                    save_path = None or "/tmp"
                    save1 = os.path.join(save_path, "src")
                    save2 = os.path.join(save_path, "dst")

                    vm.save_to_file(save1)
                    dest_vm.save_to_file(save2)

                    # Fail if we see deltas
                    md5_save1 = utils.hash_file(save1)
                    md5_save2 = utils.hash_file(save2)
                    if md5_save1 != md5_save2:
                        raise error.TestFail("Mismatch of VM state before "
                                             "and after migration")

                if (dest_host == 'localhost') and offline:
                    dest_vm.resume()
        except Exception:
            if dest_host == 'localhost':
                dest_vm.destroy()
            raise

    finally:
        if (dest_host == 'localhost') and stable_check and clean:
            logging.debug("Cleaning the state files")
            if os.path.isfile(save1):
                os.remove(save1)
            if os.path.isfile(save2):
                os.remove(save2)

    # Report migration status
    if mig_succeeded():
        logging.info("Migration finished successfully")
    elif mig_failed():
        raise error.TestFail("Migration failed")
    else:
        status = vm.monitor.info("migrate")
        raise error.TestFail("Migration ended with unknown status: %s" %
                             status)

    if dest_host == 'localhost':
        if dest_vm.monitor.verify_status("paused"):
            logging.debug("Destination VM is paused, resuming it")
            dest_vm.resume()

    # Kill the source VM
    vm.destroy(gracefully=False)

    # Replace the source VM with the new cloned VM
    if (dest_host == 'localhost') and (env is not None):
        env.register_vm(vm.name, dest_vm)

    # Return the new cloned VM
    if dest_host == 'localhost':
        return dest_vm
    else:
        return vm
Exemplo n.º 35
0
        else:
            answer = 'y'
        if answer == 'y':
            utils.interactive_download(url, path, "JeOS x86_64 image")
            had_to_download = True
        else:
            logging.warning("Missing file %s", path)
    else:
        logging.info("Found %s", path)
        if sha1 is None:
            answer = 'n'
        else:
            answer = 'y'

        if answer == 'y':
            actual_sha1 = utils.hash_file(path, method='sha1')
            if actual_sha1 != sha1:
                logging.error("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = utils.ask("The file seems corrupted or outdated. "
                                       "Would you like to download it?")
                else:
                    answer = 'y'
                if answer == 'y':
                    logging.info("Updating image to the latest available...")
                    utils.interactive_download(url, path, title)
                    had_to_download = True
                    file_ok = True
            else:
                file_ok = True
                logging.info("SHA1 sum check OK")
Exemplo n.º 36
0
def _take_screendumps(test, params, env):
    global _screendump_thread_termination_event
    temp_dir = test.debugdir
    if params.get("screendump_temp_dir"):
        temp_dir = virt_utils.get_path(test.bindir,
                                      params.get("screendump_temp_dir"))
        try:
            os.makedirs(temp_dir)
        except OSError:
            pass
    temp_filename = os.path.join(temp_dir, "scrdump-%s.ppm" %
                                 virt_utils.generate_random_string(6))
    delay = float(params.get("screendump_delay", 5))
    quality = int(params.get("screendump_quality", 30))

    cache = {}
    counter = {}

    while True:
        for vm in env.get_all_vms():
            if vm not in counter.keys():
                counter[vm] = 0
            if not vm.is_alive():
                continue
            try:
                vm.screendump(filename=temp_filename, debug=False)
            except kvm_monitor.MonitorError, e:
                logging.warn(e)
                continue
            except AttributeError, e:
                logging.warn(e)
                continue
            if not os.path.exists(temp_filename):
                logging.warn("VM '%s' failed to produce a screendump", vm.name)
                continue
            if not ppm_utils.image_verify_ppm_file(temp_filename):
                logging.warn("VM '%s' produced an invalid screendump", vm.name)
                os.unlink(temp_filename)
                continue
            screendump_dir = os.path.join(test.debugdir,
                                          "screendumps_%s" % vm.name)
            try:
                os.makedirs(screendump_dir)
            except OSError:
                pass
            counter[vm] += 1
            screendump_filename = os.path.join(screendump_dir, "%04d.jpg" %
                                               counter[vm])
            hash = utils.hash_file(temp_filename)
            if hash in cache:
                try:
                    os.link(cache[hash], screendump_filename)
                except OSError:
                    pass
            else:
                try:
                    try:
                        image = PIL.Image.open(temp_filename)
                        image.save(screendump_filename, format="JPEG",
                                   quality=quality)
                        cache[hash] = screendump_filename
                    except IOError, error_detail:
                        logging.warning("VM '%s' failed to produce a "
                                        "screendump: %s", vm.name, error_detail)
                        # Decrement the counter as we in fact failed to
                        # produce a converted screendump
                        counter[vm] -= 1
                except NameError:
                    pass
            os.unlink(temp_filename)
Exemplo n.º 37
0
def run(test, params, env):
    """
    Transfer a file back and forth between multi VMs for long time.

    1) Boot up two VMs .
    2) Create a large file by dd on host.
    3) Copy this file to VM1.
    4) Compare copied file's md5 with original file.
    5) Copy this file from VM1 to VM2.
    6) Compare copied file's md5 with original file.
    7) Copy this file from VM2 to VM1.
    8) Compare copied file's md5 with original file.
    9) Repeat step 5-8

    :param test: KVM test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """

    def md5_check(session, orig_md5):
        msg = "Compare copied file's md5 with original file."
        error.context(msg, logging.info)
        md5_cmd = "md5sum %s | awk '{print $1}'" % guest_path
        s, o = session.cmd_status_output(md5_cmd)
        if s:
            msg = "Fail to get md5 value from guest. Output is %s" % o
            raise error.TestError(msg)
        new_md5 = o.splitlines()[-1]
        if new_md5 != orig_md5:
            msg = "File changed after transfer host -> VM1. Original md5 value"
            msg += " is %s. Current md5 value is %s" % (orig_md5, new_md5)
            raise error.TestFail(msg)

    vm1 = env.get_vm(params["main_vm"])
    vm1.verify_alive()
    login_timeout = int(params.get("login_timeout", 360))
    vm2 = env.get_vm(params["vms"].split()[-1])
    vm2.verify_alive()

    session_vm1 = vm1.wait_for_login(timeout=login_timeout)
    session_vm2 = vm2.wait_for_login(timeout=login_timeout)

    transfer_timeout = int(params.get("transfer_timeout", 1000))
    username = params["username"]
    password = params["password"]
    port = int(params["file_transfer_port"])
    tmp_dir = data_dir.get_tmp_dir()
    tmp_dir_guest = params.get("tmp_dir_guest", "/var/tmp")
    repeat_time = int(params.get("repeat_time", "10"))
    clean_cmd = params.get("clean_cmd", "rm -f")
    filesize = int(params.get("filesize", 4000))
    count = int(filesize / 10)
    if count == 0:
        count = 1

    host_path = os.path.join(tmp_dir, "tmp-%s" % utils_misc.generate_random_string(8))
    cmd = "dd if=/dev/zero of=%s bs=10M count=%d" % (host_path, count)
    guest_path = os.path.join(tmp_dir_guest, "file_transfer-%s" % utils_misc.generate_random_string(8))
    try:
        error.context("Creating %dMB file on host" % filesize, logging.info)
        utils.run(cmd)
        orig_md5 = utils.hash_file(host_path, method="md5")
        error.context("Transferring file host -> VM1, timeout: %ss" % transfer_timeout, logging.info)
        t_begin = time.time()
        vm1.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
        t_end = time.time()
        throughput = filesize / (t_end - t_begin)
        logging.info("File transfer host -> VM1 succeed, " "estimated throughput: %.2fMB/s", throughput)
        md5_check(session_vm1, orig_md5)

        ip_vm1 = vm1.get_address()
        ip_vm2 = vm2.get_address()
        for i in range(repeat_time):
            log_vm1 = os.path.join(test.debugdir, "remote_scp_to_vm1_%s.log" % i)
            log_vm2 = os.path.join(test.debugdir, "remote_scp_to_vm2_%s.log" % i)

            msg = "Transferring file VM1 -> VM2, timeout: %ss." % transfer_timeout
            msg += " Repeat: %s/%s" % (i + 1, repeat_time)
            error.context(msg, logging.info)
            t_begin = time.time()
            s = remote.scp_between_remotes(
                src=ip_vm1,
                dst=ip_vm2,
                port=port,
                s_passwd=password,
                d_passwd=password,
                s_name=username,
                d_name=username,
                s_path=guest_path,
                d_path=guest_path,
                timeout=transfer_timeout,
                log_filename=log_vm2,
            )
            t_end = time.time()
            throughput = filesize / (t_end - t_begin)
            logging.info("File transfer VM1 -> VM2 succeed, " "estimated throughput: %.2fMB/s", throughput)
            md5_check(session_vm2, orig_md5)
            session_vm1.cmd("rm -rf %s" % guest_path)

            msg = "Transferring file VM2 -> VM1, timeout: %ss." % transfer_timeout
            msg += " Repeat: %s/%s" % (i + 1, repeat_time)

            error.context(msg, logging.info)
            t_begin = time.time()
            remote.scp_between_remotes(
                src=ip_vm2,
                dst=ip_vm1,
                port=port,
                s_passwd=password,
                d_passwd=password,
                s_name=username,
                d_name=username,
                s_path=guest_path,
                d_path=guest_path,
                timeout=transfer_timeout,
                log_filename=log_vm1,
            )
            t_end = time.time()
            throughput = filesize / (t_end - t_begin)
            logging.info("File transfer VM2 -> VM1 succeed, " "estimated throughput: %.2fMB/s", throughput)
            md5_check(session_vm1, orig_md5)
            session_vm2.cmd("%s %s" % (clean_cmd, guest_path))

    finally:
        try:
            session_vm1.cmd("%s %s" % (clean_cmd, guest_path))
        except Exception:
            pass
        try:
            session_vm2.cmd("%s %s" % (clean_cmd, guest_path))
        except Exception:
            pass
        try:
            os.remove(host_path)
        except OSError:
            pass
        if session_vm1:
            session_vm1.close()
        if session_vm2:
            session_vm2.close()
Exemplo n.º 38
0
    def create(self, name=None, params=None, root_dir=None, timeout=5.0, migration_mode=None, mac_source=None):
        """
        Start the VM by running a qemu command.
        All parameters are optional. If name, params or root_dir are not
        supplied, the respective values stored as class attributes are used.

        @param name: The name of the object
        @param params: A dict containing VM params
        @param root_dir: Base directory for relative filenames
        @param migration_mode: If supplied, start VM for incoming migration
                using this protocol (either 'tcp', 'unix' or 'exec')
        @param migration_exec_cmd: Command to embed in '-incoming "exec: ..."'
                (e.g. 'gzip -c -d filename') if migration_mode is 'exec'
        @param mac_source: A VM object from which to copy MAC addresses. If not
                specified, new addresses will be generated.

        @raise VMCreateError: If qemu terminates unexpectedly
        @raise VMKVMInitError: If KVM initialization fails
        @raise VMHugePageError: If hugepage initialization fails
        @raise VMImageMissingError: If a CD image is missing
        @raise VMHashMismatchError: If a CD image hash has doesn't match the
                expected hash
        @raise VMBadPATypeError: If an unsupported PCI assignment type is
                requested
        @raise VMPAError: If no PCI assignable devices could be assigned
        """
        error.context("creating '%s'" % self.name)
        self.destroy(free_mac_addresses=False)

        if name is not None:
            self.name = name
        if params is not None:
            self.params = params
        if root_dir is not None:
            self.root_dir = root_dir
        name = self.name
        params = self.params
        root_dir = self.root_dir

        # Verify the md5sum of the ISO images
        for cdrom in params.objects("cdroms"):
            if params.get("medium") == "import":
                break
            cdrom_params = params.object_params(cdrom)
            iso = cdrom_params.get("cdrom")
            if (
                (self.driver_type == "xen")
                and (params.get("hvm_or_pv") == "pv")
                and (os.path.basename(iso) == "ks.iso")
            ):
                continue
            if iso:
                iso = utils_misc.get_path(data_dir.get_data_dir(), iso)
                if not os.path.exists(iso):
                    raise virt_vm.VMImageMissingError(iso)
                compare = False
                if cdrom_params.get("md5sum_1m"):
                    logging.debug("Comparing expected MD5 sum with MD5 sum of " "first MB of ISO file...")
                    actual_hash = utils.hash_file(iso, 1048576, method="md5")
                    expected_hash = cdrom_params.get("md5sum_1m")
                    compare = True
                elif cdrom_params.get("md5sum"):
                    logging.debug("Comparing expected MD5 sum with MD5 sum of " "ISO file...")
                    actual_hash = utils.hash_file(iso, method="md5")
                    expected_hash = cdrom_params.get("md5sum")
                    compare = True
                elif cdrom_params.get("sha1sum"):
                    logging.debug("Comparing expected SHA1 sum with SHA1 sum " "of ISO file...")
                    actual_hash = utils.hash_file(iso, method="sha1")
                    expected_hash = cdrom_params.get("sha1sum")
                    compare = True
                if compare:
                    if actual_hash == expected_hash:
                        logging.debug("Hashes match")
                    else:
                        raise virt_vm.VMHashMismatchError(actual_hash, expected_hash)

        # Make sure the following code is not executed by more than one thread
        # at the same time
        lockfile = open("/tmp/libvirt-autotest-vm-create.lock", "w+")
        fcntl.lockf(lockfile, fcntl.LOCK_EX)

        try:
            # Handle port redirections
            redir_names = params.objects("redirs")
            host_ports = utils_misc.find_free_ports(5000, 6000, len(redir_names))
            self.redirs = {}
            for i in range(len(redir_names)):
                redir_params = params.object_params(redir_names[i])
                guest_port = int(redir_params.get("guest_port"))
                self.redirs[guest_port] = host_ports[i]

            # Find available PCI devices
            self.pci_devices = []
            for device in params.objects("pci_devices"):
                self.pci_devices.append(device)

            # Find available VNC port, if needed
            if params.get("display") == "vnc":
                if params.get("vnc_autoport") == "yes":
                    self.vnc_port = None
                    self.vnc_autoport = True
                else:
                    self.vnc_port = utils_misc.find_free_port(5900, 6100)
                    self.vnc_autoport = False

            # Find available spice port, if needed
            if params.get("spice"):
                self.spice_port = utils_misc.find_free_port(8000, 8100)

            # Find random UUID if specified 'uuid = random' in config file
            if params.get("uuid") == "random":
                f = open("/proc/sys/kernel/random/uuid")
                self.uuid = f.read().strip()
                f.close()

            # Generate or copy MAC addresses for all NICs
            for nic in self.virtnet:
                nic_params = dict(nic)
                if mac_source:
                    # Will raise exception if source doesn't
                    # have cooresponding nic
                    logging.debug("Copying mac for nic %s from VM %s" % (nic.nic_name, mac_source.nam))
                    nic_params["mac"] = mac_source.get_mac_address(nic.nic_name)
                # make_create_command() calls vm.add_nic (i.e. on a copy)
                nic = self.add_nic(**nic_params)
                logging.debug("VM.create activating nic %s" % nic)
                self.activate_nic(nic.nic_name)

            # Make qemu command
            install_command = self.make_create_command()

            logging.info("Running libvirt command (reformatted):")
            for item in install_command.replace(" -", " \n    -").splitlines():
                logging.info("%s", item)
            utils.run(install_command, verbose=False)
            # Wait for the domain to be created
            utils_misc.wait_for(func=self.is_alive, timeout=60, text=("waiting for domain %s to start" % self.name))
            self.uuid = virsh.domuuid(self.name, uri=self.connect_uri)

            # Establish a session with the serial console
            if self.only_pty == True:
                self.serial_console = aexpect.ShellSession(
                    "virsh console %s" % self.name,
                    auto_close=False,
                    output_func=utils_misc.log_line,
                    output_params=("serial-%s.log" % name,),
                )
            else:
                self.serial_console = aexpect.ShellSession(
                    "tail -f %s" % self.get_serial_console_filename(),
                    auto_close=False,
                    output_func=utils_misc.log_line,
                    output_params=("serial-%s.log" % name,),
                )

        finally:
            fcntl.lockf(lockfile, fcntl.LOCK_UN)
            lockfile.close()
Exemplo n.º 39
0
    if args:
        filenames = args
    else:
        parser.print_help()
        sys.exit(1)

    for filename in filenames:
        filename = os.path.abspath(filename)

        file_exists = os.path.isfile(filename)
        can_read_file = os.access(filename, os.R_OK)
        if not file_exists:
            logging.critical("File %s does not exist!", filename)
            continue
        if not can_read_file:
            logging.critical("File %s does not have read permissions!",
                             filename)
            continue

        logging.info("Hash values for file %s", os.path.basename(filename))
        logging.info("md5    (1m): %s",
                     utils.hash_file(filename, 1024 * 1024, method="md5"))
        logging.info("sha1   (1m): %s",
                     utils.hash_file(filename, 1024 * 1024, method="sha1"))
        logging.info("md5  (full): %s", utils.hash_file(filename,
                                                        method="md5"))
        logging.info("sha1 (full): %s", utils.hash_file(filename,
                                                        method="sha1"))
        logging.info("")
Exemplo n.º 40
0
def run(test, params, env):
    """
    Test Step
        1. boot up two virtual machine
        2. Transfer data: host <--> guest1 <--> guest2 <-->host via ipv6
        3. after data transfer, check data have no change
    Params:
        :param test: QEMU test object
        :param params: Dictionary with the test parameters
        :param env: Dictionary with test environment.
    """
    timeout = int(params.get("login_timeout", '360'))
    client = params.get("file_transfer_client")
    port = params.get("file_transfer_port")
    password = params.get("password")
    username = params.get("username")
    tmp_dir = params.get("tmp_dir", "/tmp/")
    filesize = int(params.get("filesize", '4096'))
    dd_cmd = params.get("dd_cmd")
    file_trans_timeout = int(params.get("file_trans_timeout", '1200'))
    file_md5_check_timeout = int(params.get("file_md5_check_timeout", '600'))

    def get_linux_ipv6_linklocal_address(ifname, session=None):
        """
        Get host/guest ipv6 linklocal address via ifname
        """
        if session is not None:
            o = session.cmd_output("ifconfig %s" % ifname)
        else:
            o = utils.system_output("ifconfig %s" % ifname)
        ipv6_address_reg = re.compile(r"(fe80::[^\s|/]*)")
        if o:
            ipv6_linklocal_address = ipv6_address_reg.findall(o)
            if not ipv6_linklocal_address:
                raise error.TestError("Can't get %s linklocal address" %
                                      ifname)
            return ipv6_linklocal_address[0]
        else:
            return None

    def get_file_md5sum(file_name, session, timeout):
        """
        Get file md5sum from guest.
        """
        logging.info("Get md5sum of the file:'%s'" % file_name)
        try:
            o = session.cmd_output("md5sum %s" % file_name, timeout=timeout)
            file_md5sum = re.findall("\w+", o)[0]
        except IndexError:
            raise error.TestError("Could not get file md5sum in guest")
        return file_md5sum

    sessions = {}
    addresses = {}
    inet_name = {}
    vms = []

    error.context("Boot vms for test", logging.info)
    for vm_name in params.get("vms", "vm1 vm2").split():
        vms.append(env.get_vm(vm_name))

    # config ipv6 address host and guest.
    host_ifname = params.get("netdst")

    for vm in vms:
        vm.verify_alive()
        sessions[vm] = vm.wait_for_login(timeout=timeout)
        inet_name[vm] = utils_net.get_linux_ifname(sessions[vm],
                                                   vm.get_mac_address())
        addresses[vm] = get_linux_ipv6_linklocal_address(
            inet_name[vm], sessions[vm])

    # prepare test data
    guest_path = (tmp_dir + "src-%s" % utils_misc.generate_random_string(8))
    dest_path = (tmp_dir + "dst-%s" % utils_misc.generate_random_string(8))
    host_path = os.path.join(test.tmpdir,
                             "tmp-%s" % utils_misc.generate_random_string(8))
    logging.info("Test setup: Creating %dMB file on host", filesize)
    utils.run(dd_cmd % (host_path, filesize))

    try:
        src_md5 = (utils.hash_file(host_path, method="md5"))
        # transfer data
        for vm in vms:
            error.context("Transfer data from host to %s" % vm.name,
                          logging.info)
            remote.copy_files_to(addresses[vm],
                                 client,
                                 username,
                                 password,
                                 port,
                                 host_path,
                                 guest_path,
                                 timeout=file_trans_timeout,
                                 interface=host_ifname)
            dst_md5 = get_file_md5sum(guest_path,
                                      sessions[vm],
                                      timeout=file_md5_check_timeout)
            if dst_md5 != src_md5:
                raise error.TestFail("File changed after transfer host -> %s" %
                                     vm.name)

        for vm_src in addresses:
            for vm_dst in addresses:
                if vm_src != vm_dst:
                    error.context(
                        "Transferring data from %s to %s" %
                        (vm_src.name, vm_dst.name), logging.info)
                    remote.scp_between_remotes(addresses[vm_src],
                                               addresses[vm_dst],
                                               port,
                                               password,
                                               password,
                                               username,
                                               username,
                                               guest_path,
                                               dest_path,
                                               timeout=file_trans_timeout,
                                               src_inter=host_ifname,
                                               dst_inter=inet_name[vm_src])
                    dst_md5 = get_file_md5sum(dest_path,
                                              sessions[vm_dst],
                                              timeout=file_md5_check_timeout)
                    if dst_md5 != src_md5:
                        raise error.TestFail("File changed transfer %s -> %s" %
                                             (vm_src.name, vm_dst.name))

        for vm in vms:
            error.context("Transfer data from %s to host" % vm.name,
                          logging.info)
            remote.copy_files_from(addresses[vm],
                                   client,
                                   username,
                                   password,
                                   port,
                                   dest_path,
                                   host_path,
                                   timeout=file_trans_timeout,
                                   interface=host_ifname)
            error.context("Check whether the file changed after trans",
                          logging.info)
            dst_md5 = (utils.hash_file(host_path, method="md5"))
            if dst_md5 != src_md5:
                raise error.TestFail("File changed after transfer",
                                     "Files md5sum mismatch!")
            utils.system_output("rm -rf %s" % host_path, timeout=timeout)

    finally:
        utils.system("rm -rf %s" % host_path,
                     timeout=timeout,
                     ignore_status=True)
        for vm in vms:
            sessions[vm].cmd("rm -rf %s %s || true" % (guest_path, dest_path),
                             timeout=timeout,
                             ignore_all_errors=True)
            sessions[vm].close()
Exemplo n.º 41
0
    logging_manager.configure_logging(utils_misc.VirtLoggingConfig())

    if args:
        filenames = args
    else:
        parser.print_help()
        sys.exit(1)

    for filename in filenames:
        filename = os.path.abspath(filename)

        file_exists = os.path.isfile(filename)
        can_read_file = os.access(filename, os.R_OK)
        if not file_exists:
            logging.critical("File %s does not exist!", filename)
            continue
        if not can_read_file:
            logging.critical("File %s does not have read permissions!",
                             filename)
            continue

        logging.info("Hash values for file %s", os.path.basename(filename))
        logging.info("md5    (1m): %s", utils.hash_file(filename, 1024*1024,
                                                        method="md5"))
        logging.info("sha1   (1m): %s", utils.hash_file(filename, 1024*1024,
                                                        method="sha1"))
        logging.info("md5  (full): %s", utils.hash_file(filename, method="md5"))
        logging.info("sha1 (full): %s", utils.hash_file(filename,
                                                        method="sha1"))
        logging.info("")
Exemplo n.º 42
0
def run_file_transfer(test, params, env):
    """
    Transfer a file back and forth between host and guest.

    1) Boot up a VM.
    2) Create a large file by dd on host.
    3) Copy this file from host to guest.
    4) Copy this file from guest to host.
    5) Check if file transfers ended good.

    :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()
    login_timeout = int(params.get("login_timeout", 360))

    error.context("Login to guest", logging.info)
    session = vm.wait_for_login(timeout=login_timeout)

    dir_name = test.tmpdir
    transfer_timeout = int(params.get("transfer_timeout"))
    transfer_type = params.get("transfer_type")
    tmp_dir = params.get("tmp_dir", "/tmp/")
    clean_cmd = params.get("clean_cmd", "rm -f")
    filesize = int(params.get("filesize", 4000))
    count = int(filesize / 10)
    if count == 0:
        count = 1

    host_path = os.path.join(dir_name, "tmp-%s" %
                             utils_misc.generate_random_string(8))
    host_path2 = host_path + ".2"
    cmd = "dd if=/dev/zero of=%s bs=10M count=%d" % (host_path, count)
    guest_path = (tmp_dir + "file_transfer-%s" %
                  utils_misc.generate_random_string(8))

    try:
        error.context("Creating %dMB file on host" % filesize, logging.info)
        utils.run(cmd)

        if transfer_type != "remote":
            raise error.TestError("Unknown test file transfer mode %s" %
                                  transfer_type)

        error.context("Transferring file host -> guest,"
                      " timeout: %ss" % transfer_timeout, logging.info)
        t_begin = time.time()
        vm.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
        t_end = time.time()
        throughput = filesize / (t_end - t_begin)
        logging.info("File transfer host -> guest succeed, "
                     "estimated throughput: %.2fMB/s", throughput)

        error.context("Transferring file guest -> host,"
                      " timeout: %ss" % transfer_timeout, logging.info)
        t_begin = time.time()
        vm.copy_files_from(guest_path, host_path2, timeout=transfer_timeout)
        t_end = time.time()
        throughput = filesize / (t_end - t_begin)
        logging.info("File transfer guest -> host succeed, "
                     "estimated throughput: %.2fMB/s", throughput)

        error.context("Compare md5sum between original file and"
                      " transferred file", logging.info)
        if (utils.hash_file(host_path, method="md5") !=
                utils.hash_file(host_path2, method="md5")):
            raise error.TestFail("File changed after transfer host -> guest "
                                 "and guest -> host")

    finally:
        logging.info('Cleaning temp file on guest')
        try:
            session.cmd("%s %s" % (clean_cmd, guest_path))
        except aexpect.ShellError, detail:
            logging.warn("Could not remove temp files in guest: '%s'", detail)

        logging.info('Cleaning temp files on host')
        try:
            os.remove(host_path)
            os.remove(host_path2)
        except OSError:
            pass
        session.close()
Exemplo n.º 43
0
def _take_screendumps(test, params, env):
    global _screendump_thread_termination_event
    temp_dir = test.debugdir
    if params.get("screendump_temp_dir"):
        temp_dir = utils_misc.get_path(test.bindir,
                                       params.get("screendump_temp_dir"))
        try:
            os.makedirs(temp_dir)
        except OSError:
            pass
    temp_filename = os.path.join(
        temp_dir, "scrdump-%s.ppm" % utils_misc.generate_random_string(6))
    delay = float(params.get("screendump_delay", 5))
    quality = int(params.get("screendump_quality", 30))

    cache = {}
    counter = {}

    while True:
        for vm in env.get_all_vms():
            if vm not in counter.keys():
                counter[vm] = 0
            if not vm.is_alive():
                continue
            try:
                vm.screendump(filename=temp_filename, debug=False)
            except kvm_monitor.MonitorError, e:
                logging.warn(e)
                continue
            except AttributeError, e:
                logging.warn(e)
                continue
            if not os.path.exists(temp_filename):
                logging.warn("VM '%s' failed to produce a screendump", vm.name)
                continue
            if not ppm_utils.image_verify_ppm_file(temp_filename):
                logging.warn("VM '%s' produced an invalid screendump", vm.name)
                os.unlink(temp_filename)
                continue
            screendump_dir = os.path.join(test.debugdir,
                                          "screendumps_%s" % vm.name)
            try:
                os.makedirs(screendump_dir)
            except OSError:
                pass
            counter[vm] += 1
            screendump_filename = os.path.join(screendump_dir,
                                               "%04d.jpg" % counter[vm])
            hsh = utils.hash_file(temp_filename)
            if hsh in cache:
                try:
                    os.link(cache[hsh], screendump_filename)
                except OSError:
                    pass
            else:
                try:
                    try:
                        image = PIL.Image.open(temp_filename)
                        image.save(screendump_filename,
                                   format="JPEG",
                                   quality=quality)
                        cache[hsh] = screendump_filename
                    except IOError, error_detail:
                        logging.warning(
                            "VM '%s' failed to produce a "
                            "screendump: %s", vm.name, error_detail)
                        # Decrement the counter as we in fact failed to
                        # produce a converted screendump
                        counter[vm] -= 1
                except NameError:
                    pass
            os.unlink(temp_filename)
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)
Exemplo n.º 45
0
def run_vmstop(test, params, env):
    """
    KVM guest stop test:
    1) Log into a guest
    2) Copy a file into guest
    3) Stop guest
    4) Check the status through monitor
    5) Check the session
    6) Migrat the vm to a file twice and compare them.

    :param test: kvm 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 = float(params.get("login_timeout", 240))
    session = vm.wait_for_login(timeout=timeout)

    save_path = params.get("save_path", "/tmp")
    clean_save = params.get("clean_save") == "yes"
    save1 = os.path.join(save_path, "save1")
    save2 = os.path.join(save_path, "save2")

    guest_path = params.get("guest_path", "/tmp")
    file_size = params.get("file_size", "1000")

    try:
        utils.run("dd if=/dev/zero of=/tmp/file bs=1M count=%s" % file_size)
        # Transfer file from host to guest, we didn't expect the finish of
        # transfer, we just let it to be a kind of stress in guest.
        bg = utils.InterruptedThread(vm.copy_files_to,
                                     ("/tmp/file", guest_path),
                                     dict(verbose=True, timeout=60))
        logging.info("Start the background transfer")
        bg.start()

        try:
            # wait for the transfer start
            time.sleep(5)
            logging.info("Stop the VM")
            vm.pause()

            # check with monitor
            logging.info("Check the status through monitor")
            if not vm.monitor.verify_status("paused"):
                status = str(vm.monitor.info("status"))
                raise error.TestFail("Guest did not pause after sending stop,"
                                     " guest status is %s" % status)

            # check through session
            logging.info("Check the session")
            if session.is_responsive():
                raise error.TestFail("Session still alive after sending stop")

            # Check with the migration file
            logging.info("Save and check the state files")
            for p in [save1, save2]:
                vm.save_to_file(p)
                time.sleep(1)
                if not os.path.isfile(p):
                    raise error.TestFail("VM failed to save state file %s" % p)

            # Fail if we see deltas
            md5_save1 = utils.hash_file(save1)
            md5_save2 = utils.hash_file(save2)
            if md5_save1 != md5_save2:
                raise error.TestFail("The produced state files differ")
        finally:
            bg.join(suppress_exception=True)

    finally:
        session.close()
        if clean_save:
            logging.debug("Clean the state files")
            if os.path.isfile(save1):
                os.remove(save1)
            if os.path.isfile(save2):
                os.remove(save2)
        vm.resume()