Ejemplo n.º 1
0
 def receiver():
     """ Receive side """
     logging.info("Starting receiver process on %s", receiver_addr)
     if vm_receiver:
         session = vm_receiver.wait_for_login(timeout=login_timeout)
     else:
         username = params.get("username", "")
         password = params.get("password", "")
         prompt = params.get("shell_prompt", "[\#\$]")
         linesep = eval("'%s'" % params.get("shell_linesep", r"\n"))
         client = params.get("shell_client")
         port = int(params.get("shell_port"))
         log_filename = ("session-%s-%s.log" % (receiver_addr,
                         virt_utils.generate_random_string(4)))
         session = virt_utils.remote_login(client, receiver_addr, port,
                                          username, password, prompt,
                                          linesep, log_filename, timeout)
         session.set_status_test_command("echo %errorlevel%")
     install_ntttcp(session)
     ntttcp_receiver_cmd = params.get("ntttcp_receiver_cmd")
     global _receiver_ready
     f = open(results_path + ".receiver", 'a')
     for b in buffers:
         virt_utils.wait_for(lambda: not _wait(), timeout)
         _receiver_ready = True
         rbuf = params.get("fixed_rbuf", b)
         cmd = ntttcp_receiver_cmd % (session_num, receiver_addr, rbuf, buf_num)
         r = session.cmd_output(cmd, timeout=timeout,
                                print_func=logging.debug)
         f.write("Send buffer size: %s\n%s\n%s" % (b, cmd, r))
     f.close()
     session.close()
Ejemplo n.º 2
0
 def test_full_set(self):
     props = {}
     for propertea in self.VirtIface.__slots__:
         props[propertea] = virt_utils.generate_random_string(16)
     virtiface = self.VirtIface(props)
     what_func = lambda propertea: props[propertea]
     self.loop_assert(virtiface, props.keys(), what_func, str(props))
Ejemplo n.º 3
0
 def test_full_set(self):
     props = {}
     for propertea in self.VirtIface.__slots__:
         props[propertea] = virt_utils.generate_random_string(16)
     virtiface = self.VirtIface(props)
     what_func = lambda propertea:props[propertea]
     self.loop_assert(virtiface, props.keys(), what_func, str(props))
Ejemplo n.º 4
0
 def test_half_set(self):
     half_prop_end = (len(self.VirtIface.__slots__) / 2) + 1
     props = {}
     for propertea in self.VirtIface.__slots__[0:half_prop_end]:
         props[propertea] = virt_utils.generate_random_string(16)
     virtiface = self.VirtIface(props)
     what_func = lambda propertea: props[propertea]
     self.loop_assert(virtiface, props.keys(), what_func, str(props))
Ejemplo n.º 5
0
 def test_half_set(self):
     half_prop_end = (len(self.VirtIface.__slots__) / 2) + 1
     props = {}
     for propertea in self.VirtIface.__slots__[0:half_prop_end]:
         props[propertea] = virt_utils.generate_random_string(16)
     virtiface = self.VirtIface(props)
     what_func = lambda propertea:props[propertea]
     self.loop_assert(virtiface, props.keys(), what_func, str(props))
Ejemplo n.º 6
0
 def __init__(self, vm_name, params):
     self.name = vm_name
     self.params = params
     self.get_params = lambda: self.params
     self.vm_type = self.params.get('vm_type')
     self.driver_type = self.params.get('driver_type')
     self.instance = ("%s-%s" % (time.strftime("%Y%m%d-%H%M%S"),
                                 virt_utils.generate_random_string(16)))
Ejemplo n.º 7
0
 def __init__(self, vm_name, params):
     self.name = vm_name
     self.params = params
     self.get_params = lambda :self.params
     self.vm_type = self.params.get('vm_type')
     self.driver_type = self.params.get('driver_type')
     self.instance = ( "%s-%s" % (
         time.strftime("%Y%m%d-%H%M%S"),
         virt_utils.generate_random_string(16)) )
Ejemplo n.º 8
0
 def test_apendex_set(self):
     """
     Verify container ignores unknown key names
     """
     props = {}
     for propertea in self.VirtIface.__slots__:
         props[propertea] = virt_utils.generate_random_string(16)
     more_props = {}
     for idx in xrange(0, 16):
         more_props[virt_utils.generate_random_string(
             16)] = virt_utils.generate_random_string(16)
     #Keep seperated for testing
     apendex_set = {}
     apendex_set.update(props)
     apendex_set.update(more_props)
     virtiface = self.VirtIface(apendex_set)
     what_func = lambda propertea: props[propertea]
     # str(props) guarantees apendex set wasn't incorporated
     self.loop_assert(virtiface, props.keys(), what_func, str(props))
Ejemplo n.º 9
0
 def test_apendex_set(self):
     """
     Verify container ignores unknown key names
     """
     props = {}
     for propertea in self.VirtIface.__slots__:
         props[propertea] = virt_utils.generate_random_string(16)
     more_props = {}
     for idx in xrange(0,16):
         more_props[virt_utils.generate_random_string(
             16)] = virt_utils.generate_random_string(16)
     #Keep seperated for testing
     apendex_set = {}
     apendex_set.update(props)
     apendex_set.update(more_props)
     virtiface = self.VirtIface(apendex_set)
     what_func = lambda propertea:props[propertea]
     # str(props) guarantees apendex set wasn't incorporated
     self.loop_assert(virtiface, props.keys(), what_func, str(props))
Ejemplo n.º 10
0
 def test_libvirt(self):
     map = self.get_a_map('libvirt', virt_utils.generate_random_string(16))
     self.assertEqual(map['container_class'], virt_utils.LibvirtIface)
     self.assert_(issubclass(map['container_class'], virt_utils.VirtIface))
Ejemplo n.º 11
0
 def test_default_default(self):
     map = self.get_a_map(virt_utils.generate_random_string(16),
                          virt_utils.generate_random_string(16))
     self.assertEqual(map['mac_prefix'], '9a')
     self.assertEqual(map['container_class'], virt_utils.KVMIface)
     self.assert_(issubclass(map['container_class'], virt_utils.VirtIface))
def run_migration_with_file_transfer(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: kvm test object.
    @param params: Dictionary with test parameters.
    @param env: Dictionary with the test environment.
    """
    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    login_timeout = int(params.get("login_timeout", 360))
    session = vm.wait_for_login(timeout=login_timeout)

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

    host_path = "/tmp/file-%s" % virt_utils.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"))

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

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

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

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

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

    finally:
        session.close()
        if os.path.isfile(host_path):
            os.remove(host_path)
        if os.path.isfile(host_path_returned):
            os.remove(host_path_returned)
Ejemplo n.º 13
0
def run_whql_client_install(test, params, env):
    """
    WHQL DTM client installation:
    1) Log into the guest (the client machine) and into a DTM server machine
    2) Stop the DTM client service (wttsvc) on the client machine
    3) Delete the client machine from the server's data store
    4) Rename the client machine (give it a randomly generated name)
    5) Move the client machine into the server's workgroup
    6) Reboot the client machine
    7) Install the DTM client software
    8) Setup auto logon for the user created by the installation
       (normally DTMLLUAdminUser)
    9) Reboot again

    @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()
    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))

    # Collect test params
    server_address = params.get("server_address")
    server_shell_port = int(params.get("server_shell_port"))
    server_file_transfer_port = int(params.get("server_file_transfer_port"))
    server_studio_path = params.get("server_studio_path", "%programfiles%\\ "
                                    "Microsoft Driver Test Manager\\Studio")
    server_username = params.get("server_username")
    server_password = params.get("server_password")
    client_username = params.get("client_username")
    client_password = params.get("client_password")
    dsso_delete_machine_binary = params.get("dsso_delete_machine_binary",
                                            "deps/whql_delete_machine_15.exe")
    dsso_delete_machine_binary = virt_utils.get_path(test.bindir,
                                                    dsso_delete_machine_binary)
    install_timeout = float(params.get("install_timeout", 600))
    install_cmd = params.get("install_cmd")
    wtt_services = params.get("wtt_services")

    # Stop WTT service(s) on client
    for svc in wtt_services.split():
        virt_test_utils.stop_windows_service(session, svc)

    # Copy dsso_delete_machine_binary to server
    rss_client.upload(server_address, server_file_transfer_port,
                             dsso_delete_machine_binary, server_studio_path,
                             timeout=60)

    # Open a shell session with server
    server_session = virt_utils.remote_login("nc", server_address,
                                            server_shell_port, "", "",
                                            session.prompt, session.linesep)
    server_session.set_status_test_command(session.status_test_command)

    # Get server and client information
    cmd = "echo %computername%"
    server_name = server_session.cmd_output(cmd).strip()
    client_name = session.cmd_output(cmd).strip()
    cmd = "wmic computersystem get domain"
    server_workgroup = server_session.cmd_output(cmd).strip()
    server_workgroup = server_workgroup.splitlines()[-1]
    regkey = r"HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
    cmd = "reg query %s /v Domain" % regkey
    o = server_session.cmd_output(cmd).strip().splitlines()[-1]
    try:
        server_dns_suffix = o.split(None, 2)[2]
    except IndexError:
        server_dns_suffix = ""

    # Delete the client machine from the server's data store (if it's there)
    server_session.cmd("cd %s" % server_studio_path)
    cmd = "%s %s %s" % (os.path.basename(dsso_delete_machine_binary),
                        server_name, client_name)
    server_session.cmd(cmd, print_func=logging.info)
    server_session.close()

    # Rename the client machine
    client_name = "autotest_%s" % virt_utils.generate_random_string(4)
    logging.info("Renaming client machine to '%s'", client_name)
    cmd = ('wmic computersystem where name="%%computername%%" rename name="%s"'
           % client_name)
    session.cmd(cmd, timeout=600)

    # Join the server's workgroup
    logging.info("Joining workgroup '%s'", server_workgroup)
    cmd = ('wmic computersystem where name="%%computername%%" call '
           'joindomainorworkgroup name="%s"' % server_workgroup)
    session.cmd(cmd, timeout=600)

    # Set the client machine's DNS suffix
    logging.info("Setting DNS suffix to '%s'", server_dns_suffix)
    cmd = 'reg add %s /v Domain /d "%s" /f' % (regkey, server_dns_suffix)
    session.cmd(cmd, timeout=300)

    # Reboot
    session = vm.reboot(session)

    # Access shared resources on the server machine
    logging.info("Attempting to access remote share on server")
    cmd = r"net use \\%s /user:%s %s" % (server_name, server_username,
                                         server_password)
    end_time = time.time() + 120
    while time.time() < end_time:
        try:
            session.cmd(cmd)
            break
        except Exception:
            pass
        time.sleep(5)
    else:
        raise error.TestError("Could not access server share from client "
                              "machine")

    # Install
    logging.info("Installing DTM client (timeout=%ds)", install_timeout)
    install_cmd = r"cmd /c \\%s\%s" % (server_name, install_cmd.lstrip("\\"))
    session.cmd(install_cmd, timeout=install_timeout)

    # Setup auto logon
    logging.info("Setting up auto logon for user '%s'", client_username)
    cmd = ('reg add '
           '"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon" '
           '/v "%s" /d "%s" /t REG_SZ /f')
    session.cmd(cmd % ("AutoAdminLogon", "1"))
    session.cmd(cmd % ("DefaultUserName", client_username))
    session.cmd(cmd % ("DefaultPassword", client_password))

    # Reboot one more time
    session = vm.reboot(session)
    session.close()
Ejemplo n.º 14
0
 def test_libvirt(self):
     map = self.get_a_map('libvirt',
                          virt_utils.generate_random_string(16))
     self.assertEqual(map['container_class'], virt_utils.LibvirtIface)
     self.assert_(issubclass(map['container_class'], virt_utils.VirtIface))
Ejemplo n.º 15
0
 def test_default_default(self):
     map = self.get_a_map(virt_utils.generate_random_string(16),
                          virt_utils.generate_random_string(16))
     self.assertEqual(map['mac_prefix'], '9a')
     self.assertEqual(map['container_class'], virt_utils.KVMIface)
     self.assert_(issubclass(map['container_class'], virt_utils.VirtIface))
def run_migration_multi_host_with_file_transfer(test, params, env):
    """
    KVM multi-host migration test:

    Migration execution progress is described in documentation
    for migrate method in class MultihostMigration.

    This test starts vm on master host. When vm is started then it starts file
    transfer between vm and master host:
          work:                             migration:
       host1->vm                        mig_1(host1->host2)
       vm->host1
       checksum file
       host1->vm
       vm->host1                        mig_2(host2<-host1)
       checksum file
       host1->vm
       vm->host1
       checksum file                    mig_3(host1<-host2)
           ...                                 ...
           ...                                 ...
           ...                                 ...
       host1->vm                               ...
       vm->host1                               ...
       checksum file                    mig_migrate_count(host2<-host1)

     end:
       check all checksum with orig_file checksum

    @param test: Kvm test object.
    @param params: Dictionary with test parameters.
    @param env: Dictionary with the test environment.
    @param cfg:
        file_size: Size of generated file.
        transfer_timeout: Timeout for file transfer.
        transfer_speed: File transfer speed limit.
        guest_path: Path where file is stored on guest.
    """
    guest_root = params.get("guest_root", "root")
    guest_pass = params.get("password", "123456")

    shell_client = params.get("shell_client", "ssh")
    shell_port = int(params.get("shell_port", "22"))
    shell_prompt = params.get("shell_prompt")

    #Path where file is stored on guest.
    guest_path = params.get("guest_path", "/tmp/file")
    #Path where file is generated.
    host_path = "/tmp/file-%s" % virt_utils.generate_random_string(6)
    #Path on host for file copied from vm.
    host_path_returned = "%s-returned" % host_path
    file_size = params.get("file_size", "500")
    transfer_timeout = int(params.get("transfer_timeout", "240"))
    transfer_speed = int(params.get("transfer_speed", "100")) * 1000
    d_transfer_timeout = 2 * transfer_timeout

    #Count of migration during file transfer.
    migrate_count = int(params.get("migrate_count", "3"))

    class TestMultihostMigration(virt_test_utils.MultihostMigration):
        def __init__(self, test, params, env):
            super(TestMultihostMigration, self).__init__(test, params, env)
            self.vm = None
            self.vm_addr = None
            self.srchost = self.params.get("hosts")[0]
            self.dsthost = self.params.get("hosts")[1]
            self.slave = self.dsthost
            self.id = {'src': self.srchost,
                       'dst': self.dsthost,
                       "type": "file_trasfer"}
            self.file_check_sums = []

        def check_vms(self, mig_data):
            """
            Check vms after migrate.

            @param mig_data: object with migration data.
            """
            for vm in mig_data.vms:
                if not virt_test_utils.guest_active(vm):
                    raise error.TestFail("Guest not active after migration")

            logging.info("Migrated guest appears to be running")

            logging.info("Logging into migrated guest after migration...")
            for vm in mig_data.vms:
                vm.wait_for_login(timeout=self.login_timeout)

        def _prepare_vm(self, vm_name):
            """
            Prepare, start vm and return vm.

            @param vm_name: Class with data necessary for migration.

            @return: Started VM.
            """
            new_params = self.params.copy()

            new_params['migration_mode'] = None
            new_params['start_vm'] = 'yes'
            self.vm_lock.acquire()
            virt_env_process.process(self.test, new_params, self.env,
                                     virt_env_process.preprocess_image,
                                     virt_env_process.preprocess_vm)
            self.vm_lock.release()
            vm = self.env.get_vm(vm_name)
            vm.wait_for_login(timeout=self.login_timeout)
            return vm

        def _copy_until_end(self, end_event):
            #Copy until migration not end.
            while not end_event.isSet():
                logging.info("Copy file to guest %s.", self.vm_addr)
                virt_remote.copy_files_to(self.vm_addr, "scp", guest_root,
                                          guest_pass, 22, host_path,
                                          guest_path, limit=transfer_speed,
                                          verbose=True,
                                          timeout=transfer_timeout)
                logging.info("Copy file to guests %s done.", self.vm_addr)

                logging.info("Copy file from guest %s.", self.vm_addr)
                virt_remote.copy_files_from(self.vm_addr, "scp", guest_root,
                                            guest_pass, 22, guest_path,
                                            host_path_returned,
                                            limit=transfer_speed, verbose=True,
                                            timeout=transfer_timeout)
                logging.info("Copy file from guests %s done.", self.vm_addr)
                check_sum = client_utils.hash_file(host_path_returned)
                #store checksum for later check.
                self.file_check_sums.append(check_sum)

        def _run_and_migrate(self, bg, end_event, sync, migrate_count):
            bg.start()
            try:
                while bg.isAlive():
                    logging.info("File transfer not ended, starting"
                                 " a round of migration...")
                    sync.sync(True, timeout=d_transfer_timeout)
                    self.migrate_wait([self.vm],
                                      self.srchost,
                                      self.dsthost)
                    tmp = self.dsthost
                    self.dsthost = self.srchost
                    self.srchost = tmp
                    migrate_count -= 1
                    if (migrate_count <= 0):
                        end_event.set()
                        bg.join()

                sync.sync(False, timeout=d_transfer_timeout)
            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()

        def _slave_migrate(self, sync):
            while True:
                done = sync.sync(timeout=d_transfer_timeout)[self.master_id()]
                if not done:
                    break
                logging.info("File transfer not ended, starting"
                             " a round of migration...")
                self.migrate_wait([self.vm],
                                  self.srchost,
                                  self.dsthost)

                tmp = self.dsthost
                self.dsthost = self.srchost
                self.srchost = tmp

        def migration_scenario(self):
            sync = SyncData(self.master_id(), self.hostid, self.hosts,
                            self.id, self.sync_server)
            self.vm = params.get("vms").split()[0]
            address_cache = env.get("address_cache")

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

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

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

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

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

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

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

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

                #Wait for check if guest lives.
                self._hosts_barrier(self.hosts, self.id, "After_check", 120)

    mig = TestMultihostMigration(test, params, env)
    mig.run()
def run_whql_client_install(test, params, env):
    """
    WHQL DTM client installation:
    1) Log into the guest (the client machine) and into a DTM server machine
    2) Stop the DTM client service (wttsvc) on the client machine
    3) Delete the client machine from the server's data store
    4) Rename the client machine (give it a randomly generated name)
    5) Move the client machine into the server's workgroup
    6) Reboot the client machine
    7) Install the DTM client software
    8) Setup auto logon for the user created by the installation
       (normally DTMLLUAdminUser)
    9) Reboot again

    @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()
    session = vm.wait_for_login(timeout=int(params.get("login_timeout", 360)))

    # Collect test params
    server_address = params.get("server_address")
    server_shell_port = int(params.get("server_shell_port"))
    server_file_transfer_port = int(params.get("server_file_transfer_port"))
    server_studio_path = params.get(
        "server_studio_path", "%programfiles%\\ "
        "Microsoft Driver Test Manager\\Studio")
    server_username = params.get("server_username")
    server_password = params.get("server_password")
    client_username = params.get("client_username")
    client_password = params.get("client_password")
    dsso_delete_machine_binary = params.get("dsso_delete_machine_binary",
                                            "deps/whql_delete_machine_15.exe")
    dsso_delete_machine_binary = virt_utils.get_path(
        test.bindir, dsso_delete_machine_binary)
    install_timeout = float(params.get("install_timeout", 600))
    install_cmd = params.get("install_cmd")
    wtt_services = params.get("wtt_services")

    # Stop WTT service(s) on client
    for svc in wtt_services.split():
        virt_test_utils.stop_windows_service(session, svc)

    # Copy dsso_delete_machine_binary to server
    rss_client.upload(server_address,
                      server_file_transfer_port,
                      dsso_delete_machine_binary,
                      server_studio_path,
                      timeout=60)

    # Open a shell session with server
    server_session = virt_utils.remote_login("nc", server_address,
                                             server_shell_port, "", "",
                                             session.prompt, session.linesep)
    server_session.set_status_test_command(session.status_test_command)

    # Get server and client information
    cmd = "echo %computername%"
    server_name = server_session.cmd_output(cmd).strip()
    client_name = session.cmd_output(cmd).strip()
    cmd = "wmic computersystem get domain"
    server_workgroup = server_session.cmd_output(cmd).strip()
    server_workgroup = server_workgroup.splitlines()[-1]
    regkey = r"HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
    cmd = "reg query %s /v Domain" % regkey
    o = server_session.cmd_output(cmd).strip().splitlines()[-1]
    try:
        server_dns_suffix = o.split(None, 2)[2]
    except IndexError:
        server_dns_suffix = ""

    # Delete the client machine from the server's data store (if it's there)
    server_session.cmd("cd %s" % server_studio_path)
    cmd = "%s %s %s" % (os.path.basename(dsso_delete_machine_binary),
                        server_name, client_name)
    server_session.cmd(cmd, print_func=logging.info)
    server_session.close()

    # Rename the client machine
    client_name = "autotest_%s" % virt_utils.generate_random_string(4)
    logging.info("Renaming client machine to '%s'", client_name)
    cmd = (
        'wmic computersystem where name="%%computername%%" rename name="%s"' %
        client_name)
    session.cmd(cmd, timeout=600)

    # Join the server's workgroup
    logging.info("Joining workgroup '%s'", server_workgroup)
    cmd = ('wmic computersystem where name="%%computername%%" call '
           'joindomainorworkgroup name="%s"' % server_workgroup)
    session.cmd(cmd, timeout=600)

    # Set the client machine's DNS suffix
    logging.info("Setting DNS suffix to '%s'", server_dns_suffix)
    cmd = 'reg add %s /v Domain /d "%s" /f' % (regkey, server_dns_suffix)
    session.cmd(cmd, timeout=300)

    # Reboot
    session = vm.reboot(session)

    # Access shared resources on the server machine
    logging.info("Attempting to access remote share on server")
    cmd = r"net use \\%s /user:%s %s" % (server_name, server_username,
                                         server_password)
    end_time = time.time() + 120
    while time.time() < end_time:
        try:
            session.cmd(cmd)
            break
        except Exception:
            pass
        time.sleep(5)
    else:
        raise error.TestError("Could not access server share from client "
                              "machine")

    # Install
    logging.info("Installing DTM client (timeout=%ds)", install_timeout)
    install_cmd = r"cmd /c \\%s\%s" % (server_name, install_cmd.lstrip("\\"))
    session.cmd(install_cmd, timeout=install_timeout)

    # Setup auto logon
    logging.info("Setting up auto logon for user '%s'", client_username)
    cmd = ('reg add '
           '"HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion\\winlogon" '
           '/v "%s" /d "%s" /t REG_SZ /f')
    session.cmd(cmd % ("AutoAdminLogon", "1"))
    session.cmd(cmd % ("DefaultUserName", client_username))
    session.cmd(cmd % ("DefaultPassword", client_password))

    # Reboot one more time
    session = vm.reboot(session)
    session.close()
def run_migration_multi_host_with_file_transfer(test, params, env):
    """
    KVM multi-host migration test:

    Migration execution progress is described in documentation
    for migrate method in class MultihostMigration.

    This test starts vm on master host. When vm is started then it starts file
    transfer between vm and master host:
          work:                             migration:
       host1->vm                        mig_1(host1->host2)
       vm->host1
       checksum file
       host1->vm
       vm->host1                        mig_2(host2<-host1)
       checksum file
       host1->vm
       vm->host1
       checksum file                    mig_3(host1<-host2)
           ...                                 ...
           ...                                 ...
           ...                                 ...
       host1->vm                               ...
       vm->host1                               ...
       checksum file                    mig_migrate_count(host2<-host1)

     end:
       check all checksum with orig_file checksum

    @param test: Kvm test object.
    @param params: Dictionary with test parameters.
    @param env: Dictionary with the test environment.
    @param cfg:
        file_size: Size of generated file.
        transfer_timeout: Timeout for file transfer.
        transfer_speed: File transfer speed limit.
        guest_path: Path where file is stored on guest.
    """
    guest_root = params.get("guest_root", "root")
    guest_pass = params.get("password", "123456")

    shell_client = params.get("shell_client", "ssh")
    shell_port = int(params.get("shell_port", "22"))
    shell_prompt = params.get("shell_prompt")

    #Path where file is stored on guest.
    guest_path = params.get("guest_path", "/tmp/file")
    #Path where file is generated.
    host_path = "/tmp/file-%s" % virt_utils.generate_random_string(6)
    #Path on host for file copied from vm.
    host_path_returned = "%s-returned" % host_path
    file_size = params.get("file_size", "500")
    transfer_timeout = int(params.get("transfer_timeout", "240"))
    transfer_speed = int(params.get("transfer_speed", "100")) * 1000
    d_transfer_timeout = 2 * transfer_timeout

    #Count of migration during file transfer.
    migrate_count = int(params.get("migrate_count", "3"))

    class TestMultihostMigration(virt_test_utils.MultihostMigration):
        def __init__(self, test, params, env):
            super(TestMultihostMigration, self).__init__(test, params, env)
            self.vm = None
            self.vm_addr = None
            self.srchost = self.params.get("hosts")[0]
            self.dsthost = self.params.get("hosts")[1]
            self.slave = self.dsthost
            self.id = {
                'src': self.srchost,
                'dst': self.dsthost,
                "type": "file_trasfer"
            }
            self.file_check_sums = []

        def check_vms(self, mig_data):
            """
            Check vms after migrate.

            @param mig_data: object with migration data.
            """
            for vm in mig_data.vms:
                if not virt_test_utils.guest_active(vm):
                    raise error.TestFail("Guest not active after migration")

            logging.info("Migrated guest appears to be running")

            logging.info("Logging into migrated guest after migration...")
            for vm in mig_data.vms:
                vm.wait_for_login(timeout=self.login_timeout)

        def _prepare_vm(self, vm_name):
            """
            Prepare, start vm and return vm.

            @param vm_name: Class with data necessary for migration.

            @return: Started VM.
            """
            new_params = self.params.copy()

            new_params['migration_mode'] = None
            new_params['start_vm'] = 'yes'
            self.vm_lock.acquire()
            virt_env_process.process(self.test, new_params, self.env,
                                     virt_env_process.preprocess_image,
                                     virt_env_process.preprocess_vm)
            self.vm_lock.release()
            vm = self.env.get_vm(vm_name)
            vm.wait_for_login(timeout=self.login_timeout)
            return vm

        def _copy_until_end(self, end_event):
            #Copy until migration not end.
            while not end_event.isSet():
                logging.info("Copy file to guest %s.", self.vm_addr)
                virt_remote.copy_files_to(self.vm_addr,
                                          "scp",
                                          guest_root,
                                          guest_pass,
                                          22,
                                          host_path,
                                          guest_path,
                                          limit=transfer_speed,
                                          verbose=True,
                                          timeout=transfer_timeout)
                logging.info("Copy file to guests %s done.", self.vm_addr)

                logging.info("Copy file from guest %s.", self.vm_addr)
                virt_remote.copy_files_from(self.vm_addr,
                                            "scp",
                                            guest_root,
                                            guest_pass,
                                            22,
                                            guest_path,
                                            host_path_returned,
                                            limit=transfer_speed,
                                            verbose=True,
                                            timeout=transfer_timeout)
                logging.info("Copy file from guests %s done.", self.vm_addr)
                check_sum = client_utils.hash_file(host_path_returned)
                #store checksum for later check.
                self.file_check_sums.append(check_sum)

        def _run_and_migrate(self, bg, end_event, sync, migrate_count):
            bg.start()
            try:
                while bg.isAlive():
                    logging.info("File transfer not ended, starting"
                                 " a round of migration...")
                    sync.sync(True, timeout=d_transfer_timeout)
                    self.migrate_wait([self.vm], self.srchost, self.dsthost)
                    tmp = self.dsthost
                    self.dsthost = self.srchost
                    self.srchost = tmp
                    migrate_count -= 1
                    if (migrate_count <= 0):
                        end_event.set()
                        bg.join()

                sync.sync(False, timeout=d_transfer_timeout)
            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()

        def _slave_migrate(self, sync):
            while True:
                done = sync.sync(timeout=d_transfer_timeout)[self.master_id()]
                if not done:
                    break
                logging.info("File transfer not ended, starting"
                             " a round of migration...")
                self.migrate_wait([self.vm], self.srchost, self.dsthost)

                tmp = self.dsthost
                self.dsthost = self.srchost
                self.srchost = tmp

        def migration_scenario(self):
            sync = SyncData(self.master_id(), self.hostid, self.hosts, self.id,
                            self.sync_server)
            self.vm = params.get("vms").split()[0]
            address_cache = env.get("address_cache")

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

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

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

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

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

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

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

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

                #Wait for check if guest lives.
                self._hosts_barrier(self.hosts, self.id, "After_check", 120)

    mig = TestMultihostMigration(test, params, env)
    mig.run()