def test_virt_tar_out(vm, params): """ 1) Write a tempfile to guest 2) Copy file to host with tar-out 3) Delete created file """ content = "This is file for test of virt-tar-out." path = params.get("vt_temp_file", "/tmp/test_virt_tar_out") file_dir = os.path.dirname(path) path_on_host = os.path.join(data_dir.get_tmp_dir(), "test_virt_tar_out.tar") vt = utils_test.libguestfs.VirtTools(vm, params) mountpoint = params.get("vt_mountpoint") if mountpoint is None: tmpdir = "gmount-%s" % (utils_misc.generate_random_string(6)) mountpoint = "/tmp/%s" % tmpdir if not os.path.exists(mountpoint): os.mkdir(mountpoint) writes, writeo = vt.write_file_with_guestmount(mountpoint, path, content, cleanup=False) if utils_misc.umount("", mountpoint, "") is False: logging.error("Umount vm's filesytem failed.") if writes is False: raise error.TestFail("Write file to mounted filesystem failed.") logging.info("Create %s successfully.", path) # Copy file to host tar_out_result = vt.tar_out(file_dir, path_on_host) logging.debug(tar_out_result) if tar_out_result.exit_status: raise error.TestFail("Tar out failed.") logging.info("Tar out successfully.") # uncompress file and check file in it. uc_result = utils.run("cd %s && tar xf %s" % (file_dir, path_on_host)) logging.debug(uc_result) try: os.remove(path_on_host) except IOError, detail: raise error.testfail(str(detail))
# Copy file to host tar_out_result = vt.tar_out(file_dir, path_on_host) logging.debug(tar_out_result) if tar_out_result.exit_status: raise error.TestFail("Tar out failed.") logging.info("Tar out successfully.") # uncompress file and check file in it. uc_result = utils.run("cd %s && tar xf %s" % (file_dir, path_on_host)) logging.debug(uc_result) try: os.remove(path_on_host) except IOError, detail: raise error.testfail(str(detail)) if uc_result.exit_status: raise error.testfail("uncompress file on host failed.") logging.info("uncompress file on host successfully.") # Check file cat_result = utils.run("cat %s" % path, ignore_status=True) logging.debug(cat_result) try: os.remove(path) except IOError, detail: logging.error(detail) if cat_result.exit_status: raise error.TestFail("Cat file failed.") else: if not re.search(content, cat_result.stdout): raise error.TestFail("Catted file do not match.")