コード例 #1
0
    def compress_and_check_dir(self, extension):
        hash_map_1 = {}
        for i in xrange(self.sys_random.randint(10, 20)):
            if i % 2 == 0:
                compressdir = tempfile.mkdtemp(dir=self.compressdir)
            else:
                compressdir = self.compressdir
            str_length = self.sys_random.randint(30, 50)
            fd, filename = tempfile.mkstemp(dir=compressdir, text=True)
            with os.fdopen(fd, 'w') as f:
                f.write(data_factory.generate_random_string(str_length))
            relative_path = filename.replace(self.compressdir, '')
            hash_map_1[relative_path] = crypto.hash_file(filename)

        archive_filename = self.compressdir + extension
        archive.compress(archive_filename, self.compressdir)
        archive.uncompress(archive_filename, self.decompressdir)

        hash_map_2 = {}
        for root, _, files in os.walk(self.decompressdir):
            for name in files:
                file_path = os.path.join(root, name)
                relative_path = file_path.replace(self.decompressdir, '')
                hash_map_2[relative_path] = crypto.hash_file(file_path)

        self.assertEqual(hash_map_1, hash_map_2)
コード例 #2
0
ファイル: archive_unittest.py プロジェクト: PyLearner/avocado
    def compress_and_check_dir(self, extension):
        hash_map_1 = {}
        for i in xrange(self.sys_random.randint(10, 20)):
            if i % 2 == 0:
                compressdir = tempfile.mkdtemp(dir=self.compressdir)
            else:
                compressdir = self.compressdir
            str_length = self.sys_random.randint(30, 50)
            fd, filename = tempfile.mkstemp(dir=compressdir, text=True)
            os.write(fd, data_factory.generate_random_string(str_length))
            relative_path = filename.replace(self.compressdir, '')
            hash_map_1[relative_path] = crypto.hash_file(filename)

        archive_filename = self.compressdir + extension
        archive.compress(archive_filename, self.compressdir)
        archive.uncompress(archive_filename, self.decompressdir)

        hash_map_2 = {}
        for root, _, files in os.walk(self.decompressdir):
            for name in files:
                file_path = os.path.join(root, name)
                relative_path = file_path.replace(self.decompressdir, '')
                hash_map_2[relative_path] = crypto.hash_file(file_path)

        self.assertEqual(hash_map_1, hash_map_2)
コード例 #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.context("create file on host, copy it to guest",
                              logging.info)
        cmd = params.get("dd_cmd") % src
        process.system(cmd, timeout=dd_timeout, shell=True)
        md5 = crypto.hash_file(src, algorithm="md5")
        vm.copy_files_to(src, dst, timeout=copy_timeout)
        process.system("rm -f %s" % src)
        error_context.context("create live snapshot", logging.info)
        if vm.live_snapshot(base_file, snapshot_file,
                            snapshot_format) != device:
            test.fail("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)
            test.fail("Got incorrect backing file")
        error_context.context("copy file to host, check content not changed",
                              logging.info)
        vm.copy_files_from(dst, src, timeout=copy_timeout)
        if md5 and (md5 != crypto.hash_file(src, algorithm="md5")):
            test.fail("diff md5 before/after create snapshot")
        session.cmd(params.get("alive_check_cmd", "dir"))
    finally:
        if session:
            session.close()
        process.system("rm -f %s %s" % (snapshot_file, src))
コード例 #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_file")
    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.context("create file on host, copy it to guest",
                              logging.info)
        cmd = params.get("dd_cmd") % src
        process.system(cmd, timeout=dd_timeout, shell=True)
        md5 = crypto.hash_file(src, algorithm="md5")
        vm.copy_files_to(src, dst, timeout=copy_timeout)
        process.system("rm -f %s" % src)
        error_context.context("create live snapshot", logging.info)
        if vm.live_snapshot(base_file, snapshot_file,
                            snapshot_format) != device:
            test.fail("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)
            test.fail("Got incorrect backing file")
        error_context.context("copy file to host, check content not changed",
                              logging.info)
        vm.copy_files_from(dst, src, timeout=copy_timeout)
        if md5 and (md5 != crypto.hash_file(src, algorithm="md5")):
            test.fail("diff md5 before/after create snapshot")
        session.cmd(params.get("alive_check_cmd", "dir"))
    finally:
        if session:
            session.close()
        process.system("rm -f %s %s" % (snapshot_file, src))
コード例 #5
0
def are_files_equal(filename, other):
    """
    Comparison of two files line by line
    :param filename: path to the first file
    :type filename: str
    :param other: path to the second file
    :type other: str
    :return: equality of file
    :rtype: boolean
    """
    hash_1 = crypto.hash_file(filename)
    hash_2 = crypto.hash_file(other)
    return hash_1 == hash_2
コード例 #6
0
ファイル: archive_unittest.py プロジェクト: PyLearner/avocado
 def compress_and_check_file(self, extension):
     str_length = self.sys_random.randint(30, 50)
     fd, filename = tempfile.mkstemp(dir=self.basedir, text=True)
     os.write(fd, data_factory.generate_random_string(str_length))
     original_hash = crypto.hash_file(filename)
     dstfile = filename + extension
     archive_filename = os.path.join(self.basedir, dstfile)
     archive.compress(archive_filename, filename)
     archive.uncompress(archive_filename, self.decompressdir)
     decompress_file = os.path.join(self.decompressdir,
                                    os.path.basename(filename))
     decompress_hash = crypto.hash_file(decompress_file)
     self.assertEqual(original_hash, decompress_hash)
コード例 #7
0
 def compress_and_check_file(self, extension):
     str_length = self.sys_random.randint(30, 50)
     fd, filename = tempfile.mkstemp(dir=self.basedir, text=True)
     os.write(fd, data_factory.generate_random_string(str_length))
     original_hash = crypto.hash_file(filename)
     dstfile = filename + extension
     archive_filename = os.path.join(self.basedir, dstfile)
     archive.compress(archive_filename, filename)
     archive.uncompress(archive_filename, self.decompressdir)
     decompress_file = os.path.join(self.decompressdir,
                                    os.path.basename(filename))
     decompress_hash = crypto.hash_file(decompress_file)
     self.assertEqual(original_hash, decompress_hash)
コード例 #8
0
ファイル: ethtool.py プロジェクト: ldoktor/tp-qemu
 def compare_md5sum(name):
     txt = "Comparing md5sum of the files on guest and host"
     error_context.context(txt, logging.info)
     host_result = crypto.hash_file(name, algorithm="md5")
     try:
         o = session.cmd_output("md5sum %s" % name)
         guest_result = re.findall(r"\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
コード例 #9
0
    def _create_hash_file(self, asset_path):
        """
        Compute the hash of the asset file and add it to the CHECKSUM
        file.

        :param asset_path: full path of the asset file.
        """
        result = crypto.hash_file(asset_path, algorithm=self.algorithm)
        hash_file = self._get_hash_file(asset_path)
        with FileLock(hash_file, 30):
            with open(hash_file, 'w', encoding='utf-8') as fp:
                fp.write(f'{self.algorithm} {result}\n')
コード例 #10
0
ファイル: asset.py プロジェクト: cliping/avocado
    def _create_hash_file(self, asset_path):
        """
        Compute the hash of the asset file and add it to the CHECKSUM
        file.

        :param asset_path: full path of the asset file.
        """
        result = crypto.hash_file(asset_path, algorithm=self.algorithm)
        hash_file = self._get_hash_file(asset_path)
        with FileLock(hash_file, 30):
            with open(hash_file, 'w') as fp:
                fp.write('%s %s\n' % (self.algorithm, result))
コード例 #11
0
 def compare_md5sum(name):
     txt = "Comparing md5sum of the files on guest and host"
     error_context.context(txt, logging.info)
     host_result = crypto.hash_file(name, algorithm="md5")
     try:
         o = session.cmd_output("md5sum %s" % name)
         guest_result = re.findall(r"\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
コード例 #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.context("comparing hashes", logging.info)
        self.curr_hash = crypto.hash_file(self.host_path_returned)
        if self.orig_hash != self.curr_hash:
            self.test.fail("Current file hash (%s) differs from "
                           "original one (%s)" % (self.curr_hash,
                                                  self.orig_hash))

        error_context.context("Reboot and shutdown guest.")
        self.vm.reboot()
        self.vm.destroy()
コード例 #13
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.context("comparing hashes", logging.info)
        self.curr_hash = crypto.hash_file(self.host_path_returned)
        if self.orig_hash != self.curr_hash:
            self.test.fail("Current file hash (%s) differs from "
                           "original one (%s)" %
                           (self.curr_hash, self.orig_hash))

        error_context.context("Reboot and shutdown guest.")
        self.vm.reboot()
        self.vm.destroy()
コード例 #14
0
ファイル: bridge_qinq.py プロジェクト: zixi-chen/tp-qemu
    def compare_host_guest_md5sum(name):
        """
        Compare md5 value of file on host and guest

        :param name: file name

        """
        txt = "Comparing md5sum of the file %s on guest and host" % name
        error_context.context(txt, logging.info)
        host_result = crypto.hash_file(name, algorithm="md5")
        try:
            output = session.cmd_output("md5sum %s" % name).split()[0]
            guest_result = re.findall(r"\w+", output)[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
コード例 #15
0
ファイル: bridge_qinq.py プロジェクト: liuyd96/tp-qemu
    def compare_host_guest_md5sum():
        """
        Compare md5 value of file on host and guest

        :param name: file name

        """
        logging.info("Comparing md5sum on guest and host")
        host_result = crypto.hash_file(host_path, algorithm="md5")
        try:
            output = session.cmd_output("md5sum %s" % guest_path,
                                        120).split()[0]
            guest_result = re.findall(r"\w+", output)[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
コード例 #16
0
    def _action_before_fsfreeze(self, *args):
        copy_timeout = int(self.params.get("copy_timeoout", 600))
        file_size = int(self.params.get("file_size", "1024"))
        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.context("Create a file in host.")
        process.run("dd if=/dev/urandom of=%s bs=1M count=%s" %
                    (self.host_path, file_size))
        self.orig_hash = crypto.hash_file(self.host_path)
        error_context.context(
            "Transfer file from %s to %s" % (self.host_path, self.guest_path),
            logging.info)
        self.bg = utils_misc.InterruptedThread(
            self.vm.copy_files_to, (self.host_path, self.guest_path),
            dict(verbose=True, timeout=copy_timeout))
        self.bg.start()
コード例 #17
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.context("Create a file in host.")
        process.run("dd if=/dev/urandom of=%s bs=1M count=%s"
                    % (self.host_path, file_size))
        self.orig_hash = crypto.hash_file(self.host_path)
        error_context.context("Transfer file from %s to %s" %
                              (self.host_path, self.guest_path), logging.info)
        self.bg = utils_misc.InterruptedThread(
            self.vm.copy_files_to,
            (self.host_path, self.guest_path),
            dict(verbose=True, timeout=copy_timeout))
        self.bg.start()
コード例 #18
0
 def _action_after_fsthaw(self, *args):
     if self.bg:
         self.bg.join()
     # Make sure the returned file is identical to the original one
     try:
         self.host_path_returned = "%s-returned" % self.host_path
         self.vm.copy_files_from(self.guest_path, self.host_path_returned)
         error_context.context("comparing hashes", logging.info)
         self.curr_hash = crypto.hash_file(self.host_path_returned)
         if self.orig_hash != self.curr_hash:
             self.test.fail("Current file hash (%s) differs from "
                            "original one (%s)" %
                            (self.curr_hash, self.orig_hash))
     finally:
         error_context.context("Delete the created files.", logging.info)
         process.run("rm -rf %s %s" %
                     (self.host_path, self.host_path_returned))
         session = self._get_session(self.params, None)
         self._open_session_list.append(session)
         cmd_del_file = "rm -rf %s" % self.guest_path
         if self.params.get("os_type") == "windows":
             cmd_del_file = r"del /f /q %s" % self.guest_path
         session.cmd(cmd_del_file)
コード例 #19
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.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
            test.error(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)
            test.fail(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.context("Creating %dMB file on host" % filesize,
                              logging.info)
        process.run(cmd)
        orig_md5 = crypto.hash_file(host_path, algorithm="md5")
        error_context.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.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.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()
コード例 #20
0
def run(test, params, env):
    """
    Test Step
        1. boot up two virtual machine
        2. For linux guest,Transfer data:
              host <--> guest1 <--> guest2 <-->host via ipv6
           For windows guest,Transfer data:
              host <--> guest1&guest2 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["tmp_dir"]
    filesize = int(params.get("filesize", '4096'))
    dd_cmd = params["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_file_md5sum(file_name, session, timeout):
        """
        Get file md5sum from guest.
        """
        logging.info("Get md5sum of the file:'%s'" % file_name)
        s, o = session.cmd_status_output("md5sum %s" % file_name, timeout=timeout)
        if s != 0:
            test.error("Get file md5sum failed as %s" % o)
        return re.findall(r"\w{32}", o)[0]

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

    error_context.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")
    host_address = utils_net.get_host_ip_address(
        params, ip_ver="ipv6", linklocal=True)

    error_context.context("Get ipv6 address of host: %s" % host_address,
                          logging.info)
    for vm in vms:
        vm.verify_alive()
        sessions[vm] = vm.wait_for_login(timeout=timeout)
        if params.get("os_type") == "linux":
            inet_name[vm] = utils_net.get_linux_ifname(sessions[vm],
                                                       vm.get_mac_address())
        addresses[vm] = utils_net.get_guest_ip_addr(
            sessions[vm],
            vm.get_mac_address(),
            params.get("os_type"),
            ip_version="ipv6",
            linklocal=True)

        error_context.context("Get ipv6 address of %s: %s" % (vm.name, addresses[vm]),
                              logging.info)

    # 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)
    process.run(dd_cmd % (host_path, filesize), shell=True)

    try:
        src_md5 = (crypto.hash_file(host_path, algorithm="md5"))
        error_context.context("md5 value of data from src: %s" % src_md5,
                              logging.info)
        # transfer data
        for vm in vms:
            error_context.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)
            error_context.context("md5 value of data in %s: %s" % (vm.name, dst_md5),
                                  logging.info)
            if dst_md5 != src_md5:
                test.fail("File changed after transfer host -> %s" % vm.name)

        if params.get("os_type") == "linux":
            for vm_src in addresses:
                for vm_dst in addresses:
                    if vm_src != vm_dst:
                        error_context.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)
                        error_context.context("md5 value of data in %s: %s" % (vm.name, dst_md5),
                                              logging.info)
                        if dst_md5 != src_md5:
                            test.fail("File changed transfer %s -> %s"
                                      % (vm_src.name, vm_dst.name))

        for vm in vms:
            error_context.context("Transfer data from %s to host" % vm.name,
                                  logging.info)
            remote.copy_files_from(addresses[vm],
                                   client, username, password, port,
                                   guest_path, host_path,
                                   timeout=file_trans_timeout,
                                   interface=host_ifname)
            error_context.context("Check whether the file changed after trans",
                                  logging.info)
            dst_md5 = (crypto.hash_file(host_path, algorithm="md5"))
            error_context.context("md5 value of data after copying to host: %s" % dst_md5,
                                  logging.info)

            if dst_md5 != src_md5:
                test.fail("File changed after transfer (md5sum mismatch)")
            process.system_output("rm -rf %s" % host_path, timeout=timeout)

    finally:
        process.system("rm -rf %s" % host_path, timeout=timeout,
                       ignore_status=True)
        for vm in vms:
            if params.get("os_type") == "linux":
                sessions[vm].cmd("rm -rf %s %s || true" % (guest_path, dest_path),
                                 timeout=timeout, ignore_all_errors=True)
            else:
                sessions[vm].cmd("del /f %s" % guest_path,
                                 timeout=timeout, ignore_all_errors=True)
            sessions[vm].close()
コード例 #21
0
def run(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:
        process.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_misc.InterruptedThread(vm.copy_files_to,
                                          ("/tmp/file", guest_path),
                                          dict(verbose=True, timeout=60))
        test.log.info("Start the background transfer")
        bg.start()

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

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

            # check through session
            test.log.info("Check the session")
            if session.is_responsive():
                test.fail("Session still alive after sending stop")

            # Check with the migration file
            test.log.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):
                    test.fail("VM failed to save state file %s" % p)

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

    finally:
        session.close()
        if clean_save:
            test.log.debug("Clean the state files")
            if os.path.isfile(save1):
                os.remove(save1)
            if os.path.isfile(save2):
                os.remove(save2)
        vm.resume()
コード例 #22
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:
        process.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,
                               env=env)
            except Exception:
                # If something bad happened in the main thread, ignore
                # exceptions raised in the background thread
                bg.join(suppress_exception=True)
                raise
            else:
                bg.join()

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

        error_context.context("transferring file back to host while migrating",
                              logging.info)
        bg = utils_misc.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.context("comparing hashes", logging.info)
        orig_hash = crypto.hash_file(host_path)
        returned_hash = crypto.hash_file(host_path_returned)
        if orig_hash != returned_hash:
            test.fail("Returned file hash (%s) differs from "
                      "original one (%s)" % (returned_hash, orig_hash))
        error_context.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)
コード例 #23
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.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
            test.error(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)
            test.fail(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.context("Creating %dMB file on host" % filesize,
                              logging.info)
        process.run(cmd)
        orig_md5 = crypto.hash_file(host_path, algorithm="md5")
        error_context.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.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.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()
コード例 #24
0
def run(test, params, env):
    """
    Test Step
        1. boot up two virtual machine
        2. For linux guest,Transfer data:
              host <--> guest1 <--> guest2 <-->host via ipv6
           For windows guest,Transfer data:
              host <--> guest1&guest2 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["tmp_dir"]
    filesize = int(params.get("filesize", '4096'))
    dd_cmd = params["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_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(r"\w+", o)[0]
        except IndexError:
            test.error("Could not get file md5sum in guest")
        return file_md5sum

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

    error_context.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")
    host_address = utils_net.get_host_ip_address(params,
                                                 ip_ver="ipv6",
                                                 linklocal=True)

    error_context.context("Get ipv6 address of host: %s" % host_address,
                          logging.info)
    for vm in vms:
        vm.verify_alive()
        sessions[vm] = vm.wait_for_login(timeout=timeout)
        if params.get("os_type") == "linux":
            inet_name[vm] = utils_net.get_linux_ifname(sessions[vm],
                                                       vm.get_mac_address())
        addresses[vm] = utils_net.get_guest_ip_addr(sessions[vm],
                                                    vm.get_mac_address(),
                                                    params.get("os_type"),
                                                    ip_version="ipv6",
                                                    linklocal=True)

        error_context.context(
            "Get ipv6 address of %s: %s" % (vm.name, addresses[vm]),
            logging.info)

    # 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)
    process.run(dd_cmd % (host_path, filesize), shell=True)

    try:
        src_md5 = (crypto.hash_file(host_path, algorithm="md5"))
        error_context.context("md5 value of data from src: %s" % src_md5,
                              logging.info)
        # transfer data
        for vm in vms:
            error_context.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)
            error_context.context(
                "md5 value of data in %s: %s" % (vm.name, dst_md5),
                logging.info)
            if dst_md5 != src_md5:
                test.fail("File changed after transfer host -> %s" % vm.name)

        if params.get("os_type") == "linux":
            for vm_src in addresses:
                for vm_dst in addresses:
                    if vm_src != vm_dst:
                        error_context.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)
                        error_context.context(
                            "md5 value of data in %s: %s" % (vm.name, dst_md5),
                            logging.info)
                        if dst_md5 != src_md5:
                            test.fail("File changed transfer %s -> %s" %
                                      (vm_src.name, vm_dst.name))

        for vm in vms:
            error_context.context("Transfer data from %s to host" % vm.name,
                                  logging.info)
            remote.copy_files_from(addresses[vm],
                                   client,
                                   username,
                                   password,
                                   port,
                                   guest_path,
                                   host_path,
                                   timeout=file_trans_timeout,
                                   interface=host_ifname)
            error_context.context("Check whether the file changed after trans",
                                  logging.info)
            dst_md5 = (crypto.hash_file(host_path, algorithm="md5"))
            error_context.context(
                "md5 value of data after copying to host: %s" % dst_md5,
                logging.info)

            if dst_md5 != src_md5:
                test.fail("File changed after transfer (md5sum mismatch)")
            process.system_output("rm -rf %s" % host_path, timeout=timeout)

    finally:
        process.system("rm -rf %s" % host_path,
                       timeout=timeout,
                       ignore_status=True)
        for vm in vms:
            if params.get("os_type") == "linux":
                sessions[vm].cmd("rm -rf %s %s || true" %
                                 (guest_path, dest_path),
                                 timeout=timeout,
                                 ignore_all_errors=True)
            else:
                sessions[vm].cmd("del /f %s" % guest_path,
                                 timeout=timeout,
                                 ignore_all_errors=True)
            sessions[vm].close()
コード例 #25
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 = crypto.hash_file(local_link, algorithm="md5")
                    expected_hash = crypto.hash_file(nfs_link, algorithm="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.context(msg, logging.info)
                    download.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)
コード例 #26
0
ファイル: win_nics_teaming.py プロジェクト: liuyd96/tp-qemu
def run(test, params, env):
    """
    Nic teaming test in guest.

    1) Start guest with four nic devices.
    2) Setup Team in guest.
    3) Execute file transfer from host to guest.
    4) Repeatedly set enable/disable interfaces by 'netsh interface set'
    5) Execute file transfer from guest to host.

    :param test: Kvm test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    tmp_dir = params["tmp_dir"]
    filesize = params.get_numeric("filesize")
    dd_cmd = params["dd_cmd"]
    delete_cmd = params["delete_cmd"]
    login_timeout = params.get_numeric("login_timeout", 1200)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    session_serial = vm.wait_for_serial_login(timeout=login_timeout)
    nics = params.objects("nics")
    ifnames = ()
    for i in range(len(nics)):
        mac = vm.get_mac_address(i)
        connection_id = utils_net.get_windows_nic_attribute(
            session_serial, "macaddress", mac, "netconnectionid")
        ifnames += (connection_id, )

    # get params of teaming
    setup_cmd = params["setup_cmd"]
    status, output = session_serial.cmd_status_output(setup_cmd % ifnames)
    if status:
        test.fail("Failed to setup team nic from powershell,"
                  "status=%s, output=%s" % (status, output))

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

    try:
        netsh_set_cmd = "netsh interface set interface \"%s\" %s"
        # transfer data
        original_md5 = crypto.hash_file(host_path, algorithm="md5")
        test.log.info("md5 value of data original: %s", original_md5)
        test.log.info("Failover test with file transfer")
        transfer_thread = utils_misc.InterruptedThread(vm.copy_files_to,
                                                       (host_path, guest_path))
        transfer_thread.start()
        try:
            while transfer_thread.is_alive():
                for ifname in ifnames:
                    session_serial.cmd(netsh_set_cmd % (ifname, "disable"))
                    time.sleep(random.randint(1, 30))
                    session_serial.cmd(netsh_set_cmd % (ifname, "enable"))
                    time.sleep(random.randint(1, 30))
        except aexpect.ShellProcessTerminatedError:
            transfer_thread.join(suppress_exception=True)
            raise
        else:
            transfer_thread.join()

        os.remove(host_path)
        test.log.info('Cleaning temp file on host')
        test.log.info("Failover test 2 with file transfer")
        transfer_thread = utils_misc.InterruptedThread(vm.copy_files_from,
                                                       (guest_path, host_path))
        transfer_thread.start()
        try:
            nic_num = len(ifnames)
            index = 0
            while transfer_thread.is_alive():
                index = index % nic_num
                for i in range(nic_num):
                    session_serial.cmd(netsh_set_cmd % (ifnames[i], "enable"))
                    for j in range(nic_num):
                        if i != j:
                            session_serial.cmd(netsh_set_cmd %
                                               (ifnames[j], "disable"))
                time.sleep(random.randint(1, 5))
                index += 1
        except aexpect.ShellProcessTerminatedError:
            transfer_thread.join(suppress_exception=True)
            raise
        else:
            transfer_thread.join()
        current_md5 = crypto.hash_file(host_path, algorithm="md5")
        test.log.info("md5 value of data current: %s", current_md5)
        if original_md5 != current_md5:
            test.fail("File changed after transfer host -> guest "
                      "and guest -> host")
    finally:
        os.remove(host_path)
        session_serial.cmd(delete_cmd % guest_path,
                           timeout=login_timeout,
                           ignore_all_errors=True)
        session_serial.close()
コード例 #27
0
 def _verify_hash(filename):
     if os.path.isfile(filename):
         return crypto.hash_file(filename, algorithm=hash_algorithm)
     return None
コード例 #28
0
ファイル: msi_change_flag.py プロジェクト: liuyd96/tp-qemu
def run(test, params, env):
    """
    vhost is no longer disabled when guest does not use MSI-X.
    The vhostforce flag is no longer required.

    1) Start guest with different NIC option
    2) Check virtio device's irq number,irq number should be greater than one.
    3) Disable msi of guest
    4) Reboot guest,check if msi is disabled and irq number should be equal to 1.
    5) Check network and vhost process (transfer data).
    6) Check md5 value of both sides.

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """
    def irq_check(session, device_name, devcon_folder):
        hwid = win_dev.get_hwids(session, device_name, devcon_folder,
                                 login_timeout)[0]
        get_irq_cmd = params["get_irq_cmd"] % (devcon_folder, hwid)
        irq_list = re.findall(r':\s+(\d+)', session.cmd_output(get_irq_cmd),
                              re.M)
        if not irq_list:
            test.error("device %s's irq checked fail" % device_name)
        return irq_list

    def get_file_md5sum(file_name, session, timeout):
        """
        return: Return the md5sum value of the guest.
        """
        logging.info("Get md5sum of the file:'%s'", file_name)
        s, o = session.cmd_status_output("md5sum %s" % file_name,
                                         timeout=timeout)
        if s != 0:
            test.error("Get file md5sum failed as %s" % o)
        return re.findall(r"\w{32}", o)[0]

    tmp_dir = params["tmp_dir"]
    filesize = int(params.get("filesize"))
    dd_cmd = params["dd_cmd"]
    delete_cmd = params["delete_cmd"]
    file_md5_check_timeout = int(params.get("file_md5_check_timeout"))
    login_timeout = int(params.get("login_timeout", 360))
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    session = vm.wait_for_serial_login()

    if params.get("os_type") == "linux":
        error_context.context("Check the pci msi in guest", logging.info)
        pci_id = session.cmd("lspci |grep Eth |awk {'print $1'}").strip()
        status = session.cmd("lspci -vvv -s %s|grep MSI-X" % pci_id).strip()
        enable_status = re.search(r'Enable\+', status, re.M | re.I)
        if enable_status.group() == "Enable+":
            error_context.context("Disable pci msi in guest", logging.info)
            utils_test.update_boot_option(vm, args_added="pci=nomsi")
            session_msi = vm.wait_for_serial_login(timeout=login_timeout)
            pci_id = session_msi.cmd(
                "lspci |grep Eth |awk {'print $1'}").strip()
            status = session_msi.cmd("lspci -vvv -s %s|grep MSI-X" %
                                     pci_id).strip()
            session_msi.close()
            change_status = re.search(r'Enable\-', status, re.M | re.I)
            if change_status.group() != "Enable-":
                test.fail("virtio device's statuts is not correct")
        elif enable_status.group() != "Enable+":
            test.fail("virtio device's statuts is not correct")
    else:
        driver = params.get("driver_name")
        driver_verifier = params.get("driver_verifier", driver)

        device_name = params["device_name"]
        devcon_folder = utils_misc.set_winutils_letter(session,
                                                       params["devcon_folder"])
        error_context.context("Boot guest with %s device" % driver,
                              logging.info)
        session = utils_test.qemu.windrv_check_running_verifier(
            session, vm, test, driver_verifier, login_timeout)
        error_context.context("Check %s's irq number" % device_name,
                              logging.info)
        irq_list = irq_check(session, device_name, devcon_folder)
        irq_nums = len(irq_list)
        if not irq_nums > 1 and\
                max(ctypes.c_int32(int(irq)).value for irq in irq_list) < 0:
            test.fail("%s's irq is not correct." % device_name)
        if params.get("msi_cmd"):
            error_context.context("Disable MSI in guest", logging.info)
            hwid_msi = win_dev.get_hwids(session, device_name, devcon_folder,
                                         login_timeout)[0]
            session.cmd(params["msi_cmd"] % (hwid_msi, 0))
            session = vm.reboot(session=session)
            error_context.context("Check %s's irq number" % device_name,
                                  logging.info)
            irq_list = irq_check(session, device_name, devcon_folder)
            irq_nums = len(irq_list)
            if not irq_nums == 1 and \
                    min(ctypes.c_int32(int(irq)).value for irq in irq_list) > 0:
                test.fail("%s's irq is not correct." % device_name)

    # prepare test data
    guest_path = (tmp_dir + "src-%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)
    process.run(dd_cmd % host_path, shell=True)

    try:
        src_md5 = crypto.hash_file(host_path, algorithm="md5")
        logging.info("md5 value of data from src: %s", src_md5)
        # transfer data
        error_context.context("Transfer data from host to %s" % vm.name,
                              logging.info)
        vm.copy_files_to(host_path, guest_path)
        dst_md5 = get_file_md5sum(guest_path,
                                  session,
                                  timeout=file_md5_check_timeout)
        logging.info("md5 value of data in %s: %s", vm.name, dst_md5)
        if dst_md5 != src_md5:
            test.fail("File changed after transfer host -> %s" % vm.name)
    finally:
        os.remove(host_path)
        session.cmd(delete_cmd % guest_path,
                    timeout=login_timeout,
                    ignore_all_errors=True)
        session.close()
コード例 #29
0
    parser = optparse.OptionParser("usage: %prog [options] [filenames]")
    options, args = parser.parse_args()

    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.error("File %s does not exist!", filename)
            continue
        if not can_read_file:
            logging.error("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",
                     crypto.hash_file(filename, 1024 * 1024, algorithm="md5"))
        logging.info("    sha1   (1m): %s",
                     crypto.hash_file(filename, 1024 * 1024, algorithm="sha1"))
        logging.info("    md5  (full): %s",
                     crypto.hash_file(filename, algorithm="md5"))
        logging.info("    sha1 (full): %s",
                     crypto.hash_file(filename, algorithm="sha1"))
コード例 #30
0
ファイル: vmstop.py プロジェクト: bssrikanth/tp-qemu
def run(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:
        process.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_misc.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"))
                test.fail("Guest did not pause after sending stop,"
                          " guest status is %s" % status)

            # check through session
            logging.info("Check the session")
            if session.is_responsive():
                test.fail("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):
                    test.fail("VM failed to save state file %s" % p)

            # Fail if we see deltas
            md5_save1 = crypto.hash_file(save1)
            md5_save2 = crypto.hash_file(save2)
            if md5_save1 != md5_save2:
                test.fail("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()
コード例 #31
0
ファイル: asset.py プロジェクト: zjiang2019/avocado-vt
def download_file(asset_info, interactive=False, force=False):
    """
    Verifies if file that can be find on url is on destination with right hash.

    This function will verify the SHA1 hash of the file. If the file
    appears to be missing or corrupted, let the user know.

    :param asset_info: Dictionary returned by get_asset_info
    """
    file_ok = False
    problems_ignored = False
    had_to_download = False
    sha1 = None

    url = asset_info['url']
    sha1_url = asset_info['sha1_url']
    destination = asset_info['destination']
    title = asset_info['title']

    if sha1_url is not None:
        try:
            logging.info("Verifying expected SHA1 sum from %s", sha1_url)
            sha1_file = urllib.request.urlopen(sha1_url)
            sha1_contents = decode_to_text(sha1_file.read())
            sha1 = sha1_contents.split(" ")[0]
            logging.info("Expected SHA1 sum: %s", sha1)
        except Exception as e:
            logging.error("Failed to get SHA1 from file: %s", e)
    else:
        sha1 = None

    destination_dir = os.path.dirname(destination)
    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)

    if not os.path.isfile(destination):
        logging.warning("File %s not found", destination)
        if interactive:
            answer = genio.ask("Would you like to download it from %s?" % url)
        else:
            answer = 'y'
        if answer == 'y':
            try:
                download.url_download_interactive(url, destination,
                                                  "Downloading %s" % title)
                had_to_download = True
            except Exception as download_failure:
                logging.error("Check your internet connection: %s",
                              download_failure)
        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 = crypto.hash_file(destination, algorithm='sha1')
            if actual_sha1 != sha1:
                logging.info("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = genio.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:
                        try:
                            download.url_download_interactive(
                                url, destination, title)
                        except Exception as download_failure:
                            logging.error("Check your internet connection: %s",
                                          download_failure)
                        sha1_post_download = crypto.hash_file(destination,
                                                              algorithm='sha1')
                        had_to_download = True
                        if sha1_post_download != sha1:
                            logging.error("Actual SHA1 sum: %s", actual_sha1)
                            if interactive:
                                answer = genio.ask("The file downloaded %s is "
                                                   "corrupted. Would you like "
                                                   "to try again?" %
                                                   destination)
                            else:
                                answer = 'n'
                            if answer == 'n':
                                problems_ignored = True
                                logging.error("File %s is corrupted" %
                                              destination)
                                file_ok = True
                            else:
                                file_ok = False
                        else:
                            file_ok = True
            else:
                file_ok = True
                logging.info("SHA1 sum check OK")
        else:
            problems_ignored = True
            logging.info("File %s present, but did not verify integrity",
                         destination)

    if file_ok:
        if not problems_ignored:
            logging.info("%s present, with proper checksum", destination)

    uncompress_asset(asset_info=asset_info, force=force or had_to_download)
コード例 #32
0
ファイル: cd_hash.py プロジェクト: ChenFanFnst/avocado-vt
if __name__ == "__main__":
    log_cfg = LoggingConfig(set_fmt=False)
    log_cfg.configure_logging()

    parser = optparse.OptionParser("usage: %prog [options] [filenames]")
    options, args = parser.parse_args()

    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.error("File %s does not exist!", filename)
            continue
        if not can_read_file:
            logging.error("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", crypto.hash_file(filename, 1024 * 1024, algorithm="md5"))
        logging.info("    sha1   (1m): %s", crypto.hash_file(filename, 1024 * 1024, algorithm="sha1"))
        logging.info("    md5  (full): %s", crypto.hash_file(filename, algorithm="md5"))
        logging.info("    sha1 (full): %s", crypto.hash_file(filename, algorithm="sha1"))
コード例 #33
0
class VirtBootstrap(plugin.Plugin):
    """
    Implements the avocado 'virt-bootstrap' subcommand
    """

    name = 'virt_bootstrap'
    enabled = True

    def configure(self, parser):
        self.parser = parser.subcommands.add_parser(
            'virt-bootstrap',
            help='Download image files important to avocado virt tests')
        super(VirtBootstrap, self).configure(self.parser)

    def run(self, args):
        fail = False
        view = output.View(app_args=args)
        view.notify(event='message',
                    msg='Probing your system for test requirements')
        try:
            utils_path.find_command('7za')
            view.notify(event='minor', msg='7zip present')
        except utils_path.CmdNotFoundError:
            view.notify(event='warning',
                        msg=("7za not installed. You may "
                             "install 'p7zip' (or the "
                             "equivalent on your distro) to "
                             "fix the problem"))
            fail = True

        jeos_sha1_url = 'https://lmr.fedorapeople.org/jeos/SHA1SUM_JEOS20'
        try:
            view.notify(event='minor',
                        msg=('Verifying expected SHA1 '
                             'sum from %s' % jeos_sha1_url))
            sha1_file = urllib2.urlopen(jeos_sha1_url)
            sha1_contents = sha1_file.read()
            sha1 = sha1_contents.split(" ")[0]
            view.notify(event='minor', msg='Expected SHA1 sum: %s' % sha1)
        except Exception, e:
            view.notify(event='error',
                        msg='Failed to get SHA1 from file: %s' % e)
            fail = True

        jeos_dst_dir = path.init_dir(
            os.path.join(data_dir.get_data_dir(), 'images'))
        jeos_dst_path = os.path.join(jeos_dst_dir, 'jeos-20-64.qcow2.7z')

        if os.path.isfile(jeos_dst_path):
            actual_sha1 = crypto.hash_file(filename=jeos_dst_path,
                                           algorithm="sha1")
        else:
            actual_sha1 = '0'

        if actual_sha1 != sha1:
            if actual_sha1 == '0':
                view.notify(event='minor',
                            msg=('JeOS could not be found at %s. Downloading '
                                 'it (173 MB). Please wait...' %
                                 jeos_dst_path))
            else:
                view.notify(event='minor',
                            msg=('JeOS at %s is either corrupted or outdated. '
                                 'Downloading a new copy (173 MB). '
                                 'Please wait...' % jeos_dst_path))
            jeos_url = 'https://lmr.fedorapeople.org/jeos/jeos-20-64.qcow2.7z'
            try:
                download.url_download(jeos_url, jeos_dst_path)
            except:
                view.notify(event='warning',
                            msg=('Exiting upon user request (Download '
                                 'not finished)'))
        else:
            view.notify(event='minor',
                        msg=('Compressed JeOS image found '
                             'in %s, with proper SHA1' % jeos_dst_path))

        view.notify(event='minor',
                    msg=('Uncompressing the JeOS image to restore pristine '
                         'state. Please wait...'))
        os.chdir(os.path.dirname(jeos_dst_path))
        result = process.run('7za -y e %s' % os.path.basename(jeos_dst_path),
                             ignore_status=True)
        if result.exit_status != 0:
            view.notify(event='error',
                        msg=('Error uncompressing the image '
                             '(see details below):\n%s' % result))
            fail = True
        else:
            view.notify(event='minor',
                        msg='Successfully uncompressed the image')

        if fail:
            view.notify(event='warning',
                        msg=('Problems found probing this system for tests '
                             'requirements. Please check the error messages '
                             'and fix the problems found'))
        else:
            view.notify(event='message',
                        msg=('Your system appears to be all '
                             'set to execute tests'))
コード例 #34
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.is_alive():
                return False
            else:
                continue
        return True

    def all_threads_alive(threads):
        """
        Check whether all threads is alive
        """
        for thread in threads:
            if not thread.is_alive():
                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:
            test.cancel("You must set udev rules before test")
        rules = rules % vm_mac_address
        session = reset_guest_udevrules(session, udev_rules_file, rules)

    error_context.base_context("Test env prepare")
    error_context.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.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)
    process.run(cmd)
    file_checksum = crypto.hash_file(host_path, algorithm="md5")

    error_context.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)):
        test.cancel("File MD5SUMs changed after copy to guest")
    logging.info("Test env prepare successfully")

    error_context.base_context("Nic driver load/unload testing", logging.info)
    session_serial = vm.wait_for_serial_login(timeout=timeout)
    try:
        error_context.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_misc.InterruptedThread(
                vm.copy_files_to, (host_path, sess_path),
                {"timeout": transfer_timeout})

            thread2 = utils_misc.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.context(
            "Repeatedly unload/load NIC driver during file "
            "transfer", logging.info)
        while not all_threads_done(threads):
            error_context.context("Shutdown the driver for NIC interface.",
                                  logging.info)
            session_serial.cmd_output_safe("ifconfig %s down" % ethname)
            error_context.context("Unload  NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe -r %s" % driver)
            error_context.context("Load NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe %s" % driver)
            error_context.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.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 process.system("md5sum %s | grep %s" %
                              (copied_file, file_checksum),
                              shell=True):
                err_msg = "Host file %s md5sum changed"
                err_info.append(err_msg % copied_file)
        if err_info:
            test.error("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:
            process.system("rm -rf %s" % copied_file)
        session_serial.cmd("%s %s" % ("rm -rf", guest_path))
        os.remove(host_path)
        session.close()
        session_serial.close()
コード例 #35
0
ファイル: asset.py プロジェクト: avocado-framework/avocado-vt
def download_file(asset_info, interactive=False, force=False):
    """
    Verifies if file that can be find on url is on destination with right hash.

    This function will verify the SHA1 hash of the file. If the file
    appears to be missing or corrupted, let the user know.

    :param asset_info: Dictionary returned by get_asset_info
    """
    file_ok = False
    problems_ignored = False
    had_to_download = False
    sha1 = None

    url = asset_info['url']
    sha1_url = asset_info['sha1_url']
    destination = asset_info['destination']
    title = asset_info['title']

    if sha1_url is not None:
        try:
            logging.info("Verifying expected SHA1 sum from %s", sha1_url)
            sha1_file = urllib.request.urlopen(sha1_url)
            sha1_contents = decode_to_text(sha1_file.read())
            sha1 = sha1_contents.split(" ")[0]
            logging.info("Expected SHA1 sum: %s", sha1)
        except Exception as e:
            logging.error("Failed to get SHA1 from file: %s", e)
    else:
        sha1 = None

    destination_dir = os.path.dirname(destination)
    if not os.path.isdir(destination_dir):
        os.makedirs(destination_dir)

    if not os.path.isfile(destination):
        logging.warning("File %s not found", destination)
        if interactive:
            answer = genio.ask("Would you like to download it from %s?" % url)
        else:
            answer = 'y'
        if answer == 'y':
            try:
                download.url_download_interactive(url, destination,
                                                  "Downloading %s" % title)
                had_to_download = True
            except Exception as download_failure:
                logging.error("Check your internet connection: %s",
                              download_failure)
        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 = crypto.hash_file(destination, algorithm='sha1')
            if actual_sha1 != sha1:
                logging.info("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = genio.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:
                        try:
                            download.url_download_interactive(url, destination,
                                                              title)
                        except Exception as download_failure:
                            logging.error("Check your internet connection: %s",
                                          download_failure)
                        sha1_post_download = crypto.hash_file(destination,
                                                              algorithm='sha1')
                        had_to_download = True
                        if sha1_post_download != sha1:
                            logging.error("Actual SHA1 sum: %s", actual_sha1)
                            if interactive:
                                answer = genio.ask("The file downloaded %s is "
                                                   "corrupted. Would you like "
                                                   "to try again?" %
                                                   destination)
                            else:
                                answer = 'n'
                            if answer == 'n':
                                problems_ignored = True
                                logging.error("File %s is corrupted" %
                                              destination)
                                file_ok = True
                            else:
                                file_ok = False
                        else:
                            file_ok = True
            else:
                file_ok = True
                logging.info("SHA1 sum check OK")
        else:
            problems_ignored = True
            logging.info("File %s present, but did not verify integrity",
                         destination)

    if file_ok:
        if not problems_ignored:
            logging.info("%s present, with proper checksum", destination)

    uncompress_asset(asset_info=asset_info, force=force or had_to_download)
コード例 #36
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:
            test.cancel("You must set udev rules before test")
        rules = rules % vm_mac_address
        session = reset_guest_udevrules(session, udev_rules_file, rules)

    error_context.base_context("Test env prepare")
    error_context.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.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)
    process.run(cmd)
    file_checksum = crypto.hash_file(host_path, algorithm="md5")

    error_context.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)):
        test.cancel("File MD5SUMs changed after copy to guest")
    logging.info("Test env prepare successfully")

    error_context.base_context("Nic driver load/unload testing", logging.info)
    session_serial = vm.wait_for_serial_login(timeout=timeout)
    try:
        error_context.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_misc.InterruptedThread(
                vm.copy_files_to, (host_path, sess_path),
                {"timeout": transfer_timeout})

            thread2 = utils_misc.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.context("Repeatedly unload/load NIC driver during file "
                              "transfer", logging.info)
        while not all_threads_done(threads):
            error_context.context("Shutdown the driver for NIC interface.",
                                  logging.info)
            session_serial.cmd_output_safe("ifconfig %s down" % ethname)
            error_context.context("Unload  NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe -r %s" % driver)
            error_context.context("Load NIC driver.", logging.info)
            session_serial.cmd_output_safe("modprobe %s" % driver)
            error_context.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.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 process.system("md5sum %s | grep %s" %
                              (copied_file, file_checksum), shell=True):
                err_msg = "Host file %s md5sum changed"
                err_info.append(err_msg % copied_file)
        if err_info:
            test.error("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:
            process.system("rm -rf %s" % copied_file)
        session_serial.cmd("%s %s" % ("rm -rf", guest_path))
        os.remove(host_path)
        session.close()
        session_serial.close()
コード例 #37
0
ファイル: cd_hash.py プロジェクト: alanoe/avocado-vt
    parser = optparse.OptionParser("usage: %prog [options] [filenames]")
    options, args = parser.parse_args()

    log.configure()
    view = output.View()

    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:
            view.notify(event='error', msg="File %s does not exist!" % filename)
            continue
        if not can_read_file:
            view.notify(event='error',
                        msg="File %s does not have read permissions!" % filename)
            continue

        view.notify(event='message', msg="Hash values for file %s" % os.path.basename(filename))
        view.notify(event='minor', msg="md5    (1m): %s" % crypto.hash_file(filename, 1024 * 1024, algorithm="md5"))
        view.notify(event='minor', msg="sha1   (1m): %s" % crypto.hash_file(filename, 1024 * 1024, algorithm="sha1"))
        view.notify(event='minor', msg="md5  (full): %s" % crypto.hash_file(filename, algorithm="md5"))
        view.notify(event='minor', msg="sha1 (full): %s" % crypto.hash_file(filename, algorithm="sha1"))
コード例 #38
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 = crypto.hash_file(local_link, algorithm="md5")
                    expected_hash = crypto.hash_file(nfs_link, algorithm="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.context(msg, logging.info)
                    download.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)
コード例 #39
0
            answer = 'y'
        if answer == 'y':
            download.url_download_interactive(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 = crypto.hash_file(destination, algorithm='sha1')
            if actual_sha1 != sha1:
                logging.info("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = genio.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:
                        download.url_download_interactive(url, destination,
                                                          title)
                        sha1_post_download = crypto.hash_file(destination,
                                                              algorithm='sha1')
コード例 #40
0
ファイル: asset.py プロジェクト: lento-sun/avocado-vt
            answer = 'y'
        if answer == 'y':
            download.url_download_interactive(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 = crypto.hash_file(destination, algorithm='sha1')
            if actual_sha1 != sha1:
                logging.info("Actual SHA1 sum: %s", actual_sha1)
                if interactive:
                    answer = genio.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:
                        download.url_download_interactive(url, destination,
                                                          title)
                        sha1_post_download = crypto.hash_file(destination,
                                                              algorithm='sha1')
コード例 #41
0
class VirtBootstrap(CLICmd):
    """
    Implements the avocado 'virt-bootstrap' subcommand
    """

    name = 'virt-bootstrap'
    description = "Avocado-Virt 'virt-bootstrap' subcommand"

    def run(self, args):
        fail = False
        LOG.info('Probing your system for test requirements')
        try:
            utils_path.find_command('xz')
            logging.debug('xz present')
        except utils_path.CmdNotFoundError:
            LOG.warn("xz not installed. You may install xz (or the "
                     "equivalent on your distro) to fix the problem")
            fail = True

        jeos_sha1_url = ("https://avocado-project.org/data/assets/jeos/25/"
                         "SHA1SUM_JEOS25")
        try:
            LOG.debug('Verifying expected SHA1 sum from %s', jeos_sha1_url)
            sha1_file = urllib2.urlopen(jeos_sha1_url)
            sha1_contents = sha1_file.read()
            sha1 = sha1_contents.split(" ")[0]
            LOG.debug('Expected SHA1 sum: %s', sha1)
        except Exception, exc:
            LOG.error('Failed to get SHA1 from file: %s', exc)
            fail = True
            sha1 = "FAILED TO GET DOWNLOADED FROM AVOCADO-PROJECT"

        jeos_dst_dir = path.init_dir(
            os.path.join(data_dir.get_data_dir(), 'images'))
        jeos_dst_path = os.path.join(jeos_dst_dir, 'jeos-25-64.qcow2.xz')

        if os.path.isfile(jeos_dst_path):
            actual_sha1 = crypto.hash_file(filename=jeos_dst_path,
                                           algorithm="sha1")
        else:
            actual_sha1 = 'FILE DOES NOT EXIST LOCALLY'

        if actual_sha1 != sha1:
            if actual_sha1 == 'FILE DOES NOT EXIST LOCALLY':
                LOG.debug(
                    'JeOS could not be found at %s. Downloading '
                    'it (205 MB). Please wait...', jeos_dst_path)
            else:
                LOG.debug(
                    'JeOS at %s is either corrupted or outdated. '
                    'Downloading a new copy (205 MB). '
                    'Please wait...', jeos_dst_path)
            jeos_url = ("https://avocado-project.org/data/assets/jeos/25/"
                        "jeos-25-64.qcow2.xz")
            try:
                download.url_download(jeos_url, jeos_dst_path)
            except:
                LOG.warn('Exiting upon user request (Download not finished)')
        else:
            LOG.debug('Compressed JeOS image found in %s, with proper SHA1',
                      jeos_dst_path)

        LOG.debug('Uncompressing the JeOS image to restore pristine '
                  'state. Please wait...')
        os.chdir(os.path.dirname(jeos_dst_path))
        cmd = 'xz --keep --force -d %s' % os.path.basename(jeos_dst_path)
        result = process.run(cmd, ignore_status=True)
        if result.exit_status != 0:
            LOG.error('Error uncompressing the image (see details below):\n%s',
                      result)
            fail = True
        else:
            LOG.debug('Successfully uncompressed the image')

        if fail:
            LOG.warn('Problems found probing this system for tests '
                     'requirements. Please check the error messages '
                     'and fix the problems found')
        else:
            LOG.info('Your system appears to be all set to execute tests')
コード例 #42
0
def run(test, params, env):
    """
    Nic bonding test in guest.

    1) Start guest with four nic devices.
    2) Setup bond0 in guest.
    3) Execute file transfer test between guest and host.
    4) Repeatedly put down/up interfaces by 'ip link'
    5) Execute file transfer test between guest and host.
    6) Check md5 value after transfered.

    :param test: Kvm test object.
    :param params: Dictionary with the test parameters.
    :param env: Dictionary with test environment.
    """
    tmp_dir = params["tmp_dir"]
    filesize = params.get_numeric("filesize")
    dd_cmd = params["dd_cmd"]
    login_timeout = params.get_numeric("login_timeout", 1200)
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    mac = vm.get_mac_address(0)
    session_serial = vm.wait_for_serial_login(timeout=login_timeout)
    ifnames = utils_net.get_linux_ifname(session_serial)

    ssh_login_cmd = (
        "echo LoginGraceTime 5m  >> /etc/ssh/sshd_config &&"
        " systemctl restart sshd.service || service sshd restart")
    session_serial.cmd_output_safe(ssh_login_cmd)

    # get params of bonding
    nm_stop_cmd = "service NetworkManager stop; true"
    session_serial.cmd_output_safe(nm_stop_cmd)
    modprobe_cmd = "modprobe bonding"
    bonding_params = params.get("bonding_params")
    if bonding_params:
        modprobe_cmd += " %s" % bonding_params
    session_serial.cmd_output_safe(modprobe_cmd)
    session_serial.cmd_output_safe("ip link set dev bond0 addr %s up" % mac)
    setup_cmd = "ifenslave bond0 " + " ".join(ifnames)
    session_serial.cmd_output_safe(setup_cmd)
    # do a pgrep to check if dhclient has already been running
    pgrep_cmd = "pgrep dhclient"
    try:
        session_serial.cmd_output_safe(pgrep_cmd)
    # if dhclient is there, killl it
    except aexpect.ShellCmdError:
        logging.info("it's safe to run dhclient now")
    else:
        logging.info("dhclient is already running, kill it")
        session_serial.cmd_output_safe("killall -9 dhclient")
        time.sleep(1)

    session_serial.cmd_output_safe("dhclient bond0")
    # prepare test data
    guest_path = os.path.join(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)
    process.run(dd_cmd % host_path, shell=True)

    # get_bonding_nic_mac and ip
    try:
        link_set_cmd = "ip link set dev %s %s"
        # transfer data
        original_md5 = crypto.hash_file(host_path, algorithm="md5")
        logging.info("md5 value of data original: %s", original_md5)
        logging.info("Failover test with file transfer")
        transfer_thread = utils_misc.InterruptedThread(
            vm.copy_files_to, (host_path, guest_path))
        transfer_thread.start()
        try:
            while transfer_thread.is_alive():
                for ifname in ifnames:
                    session_serial.cmd(link_set_cmd % (ifname, "down"))
                    time.sleep(random.randint(1, 30))
                    session_serial.cmd(link_set_cmd % (ifname, "up"))
                    time.sleep(random.randint(1, 30))
        except aexpect.ShellProcessTerminatedError:
            transfer_thread.join(suppress_exception=True)
            raise
        else:
            transfer_thread.join()

        logging.info('Cleaning temp file on host')
        os.remove(host_path)
        logging.info("Failover test 2 with file transfer")
        transfer_thread = utils_misc.InterruptedThread(
            vm.copy_files_from, (guest_path, host_path))
        transfer_thread.start()
        try:
            nic_num = len(ifnames)
            up_index = 0
            while transfer_thread.is_alive():
                nic_indexes = list(range(nic_num))
                up_index = up_index % nic_num
                session_serial.cmd(link_set_cmd % (ifnames[up_index], "up"))
                nic_indexes.remove(up_index)
                for num in nic_indexes:
                    session_serial.cmd(link_set_cmd % (ifnames[num], "down"))
                time.sleep(random.randint(3, 5))
                up_index += 1
        except aexpect.ShellProcessTerminatedError:
            transfer_thread.join(suppress_exception=True)
            raise
        else:
            transfer_thread.join()
        current_md5 = crypto.hash_file(host_path, algorithm="md5")
        logging.info("md5 value of data current: %s", current_md5)
        if original_md5 != current_md5:
            test.fail("File changed after transfer host -> guest "
                      "and guest -> host")

    finally:
        session_serial.sendline("ifenslave -d bond0 " + " ".join(ifnames))
        session_serial.sendline("kill -9 `pgrep dhclient`")
        session_serial.sendline("sed -i '$ d' /etc/ssh/sshd_config")
コード例 #43
0
ファイル: cd_hash.py プロジェクト: Hao-Liu/avocado-vt
    parser = optparse.OptionParser("usage: %prog [options] [filenames]")
    options, args = parser.parse_args()

    log.configure()
    view = output.View()

    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:
            view.notify(event='error', msg="File %s does not exist!" % filename)
            continue
        if not can_read_file:
            view.notify(event='error',
                        msg="File %s does not have read permissions!" % filename)
            continue

        view.notify(event='message', msg="Hash values for file %s" % os.path.basename(filename))
        view.notify(event='minor', msg="md5    (1m): %s" % crypto.hash_file(filename, 1024 * 1024, algorithm="md5"))
        view.notify(event='minor', msg="sha1   (1m): %s" % crypto.hash_file(filename, 1024 * 1024, algorithm="sha1"))
        view.notify(event='minor', msg="md5  (full): %s" % crypto.hash_file(filename, algorithm="md5"))
        view.notify(event='minor', msg="sha1 (full): %s" % crypto.hash_file(filename, algorithm="sha1"))