AUTHOR = "Yolkfull Chow <yzhou@redhat.com>"
TIME = "SHORT"
NAME = "Migration across multiple hosts"
TEST_CATEGORY = "Functional"
TEST_CLASS = "Virtualization"
TEST_TYPE = "Server"
DOC = """
Migrate KVM guest between two hosts. It parses the base config file, restricts
it with appropriate parameters, generates the test dicts, modify the test_dicts
so there's a distinction between the migration roles ('dest' or 'source').
"""

import sys, os, commands, glob, shutil, logging, random
from autotest_lib.server import utils
from autotest_lib.client.common_lib import cartesian_config

# Specify the directory of autotest before you start this test
AUTOTEST_DIR = '/usr/local/autotest'

# Specify the root directory that on client machines
rootdir = '/tmp/kvm_autotest_root'

KVM_DIR = os.path.join(AUTOTEST_DIR, 'client/tests/kvm')


def generate_mac_address():
    r = random.SystemRandom()
    mac = "9a:%02x:%02x:%02x:%02x:%02x" % (r.randint(0x00, 0xff),
                                           r.randint(0x00, 0xff),
                                           r.randint(0x00, 0xff),
                                           r.randint(0x00, 0xff),
                                           r.randint(0x00, 0xff))
    return mac


def run(pair):
    logging.info("KVM migration running on source host [%s] and destination "
                 "host [%s]\n", pair[0], pair[1])

    source = hosts.create_host(pair[0])
    dest = hosts.create_host(pair[1])
    source_at = autotest.Autotest(source)
    dest_at = autotest.Autotest(dest)

    cfg_file = os.path.join(KVM_DIR, "tests_base.cfg")

    if not os.path.exists(cfg_file):
        raise error.JobError("Config file %s was not found", cfg_file)

    # Get test set (dictionary list) from the configuration file
    parser = cartesian_config.Parser()
    test_variants = """
image_name(_.*)? ?<= /tmp/kvm_autotest_root/images/
cdrom(_.*)? ?<= /tmp/kvm_autotest_root/
floppy ?<= /tmp/kvm_autotest_root/
Linux:
    unattended_install:
        kernel ?<= /tmp/kvm_autotest_root/
        initrd ?<= /tmp/kvm_autotest_root/
qemu_binary = /usr/libexec/qemu-kvm
qemu_img_binary = /usr/bin/qemu-img
only qcow2
only virtio_net
only virtio_blk
only smp2
only no_pci_assignable
only smallpages
only Fedora.14.64
only migrate_multi_host
nic_mode = tap
nic_mac_nic1 = %s
""" % (generate_mac_address())
    parser.parse_file(cfg_file)
    parser.parse_string(test_variants)
    test_dicts = parser.get_dicts()

    source_control_file = dest_control_file = """
kvm_test_dir = os.path.join(os.environ['AUTODIR'],'tests/kvm')
sys.path.append(kvm_test_dir)\n
"""
    for params in test_dicts:
        params['srchost'] = source.ip
        params['dsthost'] = dest.ip
        params['rootdir'] = rootdir

        source_params = params.copy()
        source_params['role'] = "source"

        dest_params = params.copy()
        dest_params['role'] = "destination"
        dest_params['migration_mode'] = "tcp"

        # Report the parameters we've received
        print "Test parameters:"
        keys = params.keys()
        keys.sort()
        for key in keys:
            logging.debug("    %s = %s", key, params[key])

        source_control_file += ("job.run_test('kvm', tag='%s', params=%s)" %
                                (source_params['shortname'], source_params))
        dest_control_file += ("job.run_test('kvm', tag='%s', params=%s)" %
                              (dest_params['shortname'], dest_params))

        logging.info('Source control file:\n%s', source_control_file)
        logging.info('Destination control file:\n%s', dest_control_file)
        dest_command = subcommand(dest_at.run,
                                  [dest_control_file, dest.hostname])

        source_command = subcommand(source_at.run,
                                    [source_control_file, source.hostname])

        parallel([dest_command, source_command])

# Grab the pairs (and failures)
(pairs, failures) = utils.form_ntuples_from_machines(machines, 2)

# Log the failures
for failure in failures:
    job.record("FAIL", failure[0], "kvm", failure[1])

# Now run through each pair and run
job.parallel_simple(run, pairs, log=False)