1import logging, time, os
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.bin import utils
4from autotest_lib.client.virt import virt_utils
5
6
7def run_file_transfer(test, params, env):
8    """
9    Test ethrnet device function by ethtool
10
11    1) Boot up a VM.
12    2) Create a large file by dd on host.
13    3) Copy this file from host to guest.
14    4) Copy this file from guest to host.
15    5) Check if file transfers ended good.
16
17    @param test: KVM test object.
18    @param params: Dictionary with the test parameters.
19    @param env: Dictionary with test environment.
20    """
21    vm = env.get_vm(params["main_vm"])
22    vm.verify_alive()
23    login_timeout = int(params.get("login_timeout", 360))
24
25    session = vm.wait_for_login(timeout=login_timeout)
26
27    dir_name = test.tmpdir
28    transfer_timeout = int(params.get("transfer_timeout"))
29    transfer_type = params.get("transfer_type")
30    tmp_dir = params.get("tmp_dir", "/tmp/")
31    clean_cmd = params.get("clean_cmd", "rm -f")
32    filesize = int(params.get("filesize", 4000))
33    count = int(filesize / 10)
34    if count == 0:
35        count = 1
36
37    host_path = os.path.join(dir_name, "tmp-%s" %
38                             virt_utils.generate_random_string(8))
39    host_path2 = host_path + ".2"
40    cmd = "dd if=/dev/zero of=%s bs=10M count=%d" % (host_path, count)
41    guest_path = (tmp_dir + "file_transfer-%s" %
42                  virt_utils.generate_random_string(8))
43
44    try:
45        logging.info("Creating %dMB file on host", filesize)
46        utils.run(cmd)
47
48        if transfer_type == "remote":
49            logging.info("Transfering file host -> guest, timeout: %ss",
50                         transfer_timeout)
51            t_begin = time.time()
52            vm.copy_files_to(host_path, guest_path, timeout=transfer_timeout)
53            t_end = time.time()
54            throughput = filesize / (t_end - t_begin)
55            logging.info("File transfer host -> guest succeed, "
56                         "estimated throughput: %.2fMB/s", throughput)
57
58            logging.info("Transfering file guest -> host, timeout: %ss",
59                         transfer_timeout)
60            t_begin = time.time()
61            vm.copy_files_from(guest_path, host_path2, timeout=transfer_timeout)
62            t_end = time.time()
63            throughput = filesize / (t_end - t_begin)
64            logging.info("File transfer guest -> host succeed, "
65                         "estimated throughput: %.2fMB/s", throughput)
66        else:
67            raise error.TestError("Unknown test file transfer mode %s" %
68                                  transfer_type)
69
70        if (utils.hash_file(host_path, method="md5") !=
71            utils.hash_file(host_path2, method="md5")):
72            raise error.TestFail("File changed after transfer host -> guest "
73                                 "and guest -> host")
74
75    finally:
76        logging.info('Cleaning temp file on guest')
77        session.cmd("%s %s" % (clean_cmd, guest_path))
78        logging.info('Cleaning temp files on host')
79        try:
80            os.remove(host_path)
81            os.remove(host_path2)
82        except OSError:
83            pass
84        session.close()
85