예제 #1
0
    def __init__(self, name, params):
        self.name = name
        self.params = params
        #
        # Assuming all low-level hypervisors will have a serial (like) console
        # connection to the guest. libvirt also supports serial (like) consoles
        # (virDomainOpenConsole). subclasses should set this to an object that
        # is or behaves like aexpect.ShellSession.
        #
        self.serial_console = None
        self.remote_sessions = []
        # Create instance if not already set
        if not hasattr(self, 'instance'):
            self._generate_unique_id()
        # Don't overwrite existing state, update from params
        if hasattr(self, 'virtnet'):
            # Direct reference to self.virtnet makes pylint complain
            # note: virtnet.__init__() supports being called anytime
            getattr(self, 'virtnet').__init__(self.params, self.name,
                                              self.instance)
        else:  # Create new
            self.virtnet = utils_net.VirtNet(self.params, self.name,
                                             self.instance)

        if not hasattr(self, 'cpuinfo'):
            self.cpuinfo = CpuInfo()
예제 #2
0
 def needs_restart(self, name, params, basedir):
     """
     Verifies whether the current virt_install commandline matches the
     requested one, based on the test parameters.
     """
     try:
         need_restart = (self.make_create_command() !=
                         self.make_create_command(name, params, basedir))
     except Exception:
         need_restart = True
     if need_restart:
         logging.debug(
             "VM params in env don't match requested, restarting.")
         return True
     else:
         # Command-line encoded state doesn't include all params
         # TODO: Check more than just networking
         other_virtnet = utils_net.VirtNet(params, name, self.instance)
         if self.virtnet != other_virtnet:
             logging.debug("VM params in env match, but network differs, "
                           "restarting")
             logging.debug("\t" + str(self.virtnet))
             logging.debug("\t!=")
             logging.debug("\t" + str(other_virtnet))
             return True
         else:
             logging.debug(
                 "VM params in env do match requested, continuing.")
             return False
예제 #3
0
 def test_04_VirtNet(self):
     """
     Populate database with max - 1 mac addresses
     """
     try:
         os.unlink(self.db_filename)
     except OSError:
         pass
     self.zero_counter(25)
     # setup() method already set LASTBYTE to '-1'
     for lastbyte in xrange(0, 0xFF):
         # test_07_VirtNet demands last byte in name and mac match
         vm_name = "vm%d" % lastbyte
         if lastbyte < 16:
             mac = "%s0%x" % (self.mac_prefix, lastbyte)
         else:
             mac = "%s%x" % (self.mac_prefix, lastbyte)
         params = utils_params.Params({
             "nics": "nic1",
             "vms": vm_name,
             "mac_nic1": mac,
         })
         virtnet = utils_net.VirtNet(params, vm_name,
                                     vm_name, self.db_filename)
         virtnet.mac_prefix = self.mac_prefix
         self.assertEqual(virtnet['nic1'].mac, mac)
         self.assertEqual(virtnet.get_mac_address(0), mac)
         # Confirm only lower-case macs are stored
         self.assertEqual(virtnet.get_mac_address(0).lower(),
                          virtnet.get_mac_address(0))
         self.assertEqual(virtnet.mac_list(), [mac])
         self.print_and_inc()
예제 #4
0
 def test_cmp_Virtnet(self):
     self.zero_counter()
     to_test = 600  # Random generator slows this test way down
     for fakevm1 in self.fakevm_generator():
         to_test -= 1
         if to_test < 1:
             break
         fvm1p = fakevm1.get_params()
         fakevm1.virtnet = utils_net.VirtNet(fvm1p, fakevm1.name,
                                             fakevm1.instance,
                                             self.db_filename)
         if len(fakevm1.virtnet) < 2:
             continue
         fakevm2 = FakeVm(fakevm1.name + "_2", fvm1p)
         fakevm2.virtnet = utils_net.VirtNet(fvm1p, fakevm2.name,
                                             fakevm2.instance,
                                             self.db_filename)
         # Verify nic order doesn't matter
         fvm3p = utils_params.Params(fvm1p.items())  # work on copy
         nic_list = fvm1p.object_params(fakevm1.name).get(
             "nics", fvm1p.get('nics', "")).split()
         random.shuffle(nic_list)
         fvm3p['nics'] = " ".join(nic_list)
         fakevm3 = FakeVm(fakevm1.name + "_3", fvm3p)
         fakevm3.virtnet = utils_net.VirtNet(fvm3p, fakevm3.name,
                                             fakevm3.instance,
                                             self.db_filename)
         self.assertTrue(fakevm1.virtnet == fakevm1.virtnet)
         self.assertTrue(fakevm1.virtnet == fakevm2.virtnet)
         self.assertTrue(fakevm1.virtnet == fakevm3.virtnet)
         self.assertTrue(fakevm2.virtnet == fakevm3.virtnet)
         if len(fakevm1.virtnet) > 1:
             del fakevm1.virtnet[0]
             self.assertFalse(fakevm1.virtnet == fakevm2.virtnet)
             self.assertFalse(fakevm1.virtnet == fakevm3.virtnet)
             self.assertTrue(fakevm1.virtnet != fakevm2.virtnet)
             self.assertTrue(fakevm1.virtnet != fakevm3.virtnet)
         self.print_and_inc()
예제 #5
0
 def test_08_ifname(self):
     for fakevm in self.fakevm_generator():
         # only need to test kvm instance
         if fakevm.vm_type != 'kvm':
             continue
         test_params = fakevm.get_params()
         virtnet = utils_net.VirtNet(test_params, fakevm.name, fakevm.name)
         for virtnet_index in xrange(0, len(virtnet)):
             result = virtnet.generate_ifname(virtnet_index)
             self.assertEqual(result, virtnet[virtnet_index].ifname)
             # assume less than 10 nics
             self.assert_(len(result) < 11)
         if len(virtnet) == 2:
             break  # no need to test every possible combination
예제 #6
0
    def __init__(self, name, params):
        self.name = name
        self.params = params
        # Create instance if not already set
        if not hasattr(self, 'instance'):
            self._generate_unique_id()
        # Don't overwrite existing state, update from params
        if hasattr(self, 'virtnet'):
            # Direct reference to self.virtnet makes pylint complain
            # note: virtnet.__init__() supports being called anytime
            getattr(self, 'virtnet').__init__(self.params, self.name,
                                              self.instance)
        else:  # Create new
            self.virtnet = utils_net.VirtNet(self.params, self.name,
                                             self.instance)

        if not hasattr(self, 'cpuinfo'):
            self.cpuinfo = CpuInfo()
예제 #7
0
    def test_06_VirtNet(self):
        """
        Generate last possibly mac and verify value.

        DEPENDS ON test_05_VirtNet running first
        """
        self.zero_counter(25)
        # test two nics, second mac generation should fail (pool exhausted)
        params = utils_params.Params({"nics": "nic1 nic2", "vms": "vm255"})
        virtnet = utils_net.VirtNet(params, 'vm255', 'vm255', self.db_filename)
        virtnet.mac_prefix = self.mac_prefix
        self.assertRaises(AttributeError, virtnet.get_mac_address, 'nic1')
        mac = "%s%x" % (self.mac_prefix, 255)
        # This will grab the last available address
        # only try 300 times, guarantees LASTBYTE counter will loop once
        self.assertEqual(virtnet.generate_mac_address(0, 300), mac)
        # This will fail allocation
        self.assertRaises(utils_net.NetError, virtnet.generate_mac_address, 1,
                          300)
예제 #8
0
    def test_05_VirtNet(self):
        """
        Load max - 1 entries from db, overriding params.

        DEPENDS ON test_04_VirtNet running first
        """
        self.zero_counter(25)
        # second loop forces db load from disk
        # also confirming params merge with db data
        for lastbyte in xrange(0, 0xFF):
            vm_name = "vm%d" % lastbyte
            params = utils_params.Params({"nics": "nic1", "vms": vm_name})
            virtnet = utils_net.VirtNet(params, vm_name, vm_name,
                                        self.db_filename)
            if lastbyte < 16:
                mac = "%s0%x" % (self.mac_prefix, lastbyte)
            else:
                mac = "%s%x" % (self.mac_prefix, lastbyte)
            self.assertEqual(virtnet['nic1'].mac, mac)
            self.assertEqual(virtnet.get_mac_address(0), mac)
            self.print_and_inc()
예제 #9
0
 def test_07_VirtNet(self):
     """
     Release mac from beginning, midle, and end, re-generate + verify value
     """
     self.zero_counter(1)
     beginning_params = utils_params.Params({
         "nics": "nic1 nic2",
         "vms": "vm0"
     })
     middle_params = utils_params.Params({
         "nics": "nic1 nic2",
         "vms": "vm127"
     })
     end_params = utils_params.Params({
         "nics": "nic1 nic2",
         "vms": "vm255",
     })
     for params in (beginning_params, middle_params, end_params):
         vm_name = params['vms']
         virtnet = utils_net.VirtNet(params, vm_name,
                                     vm_name, self.db_filename)
         virtnet.mac_prefix = self.mac_prefix
         iface = virtnet['nic1']
         last_db_mac_byte = iface.mac_str_to_int_list(iface.mac)[-1]
         last_vm_name_byte = int(vm_name[2:])
         # Sequential generation from test_04_VirtNet guarantee
         self.assertEqual(last_db_mac_byte, last_vm_name_byte)
         # only try 300 times, guarantees LASTBYTE counter will loop once
         self.assertRaises(
             utils_net.NetError, virtnet.generate_mac_address, 1, 300)
         virtnet.free_mac_address(0)
         virtnet.free_mac_address(1)
         # generate new on nic1 to verify mac_index generator catches it
         # and to signify database updated after generation
         virtnet.generate_mac_address(1, 300)
         last_db_mac_byte = virtnet['nic2'].mac_str_to_int_list(
             virtnet['nic2'].mac)[-1]
         self.assertEqual(last_db_mac_byte, last_vm_name_byte)
         self.assertEqual(virtnet.get_mac_address(1), virtnet[1].mac)
         self.print_and_inc()
예제 #10
0
def preprocess_vm(test, params, env, name):
    """
    Preprocess a single VM object according to the instructions in params.
    Start the VM if requested and get a screendump.

    :param test: An Autotest test object.
    :param params: A dict containing VM preprocessing parameters.
    :param env: The environment (a dict-like object).
    :param name: The name of the VM object.
    """
    vm = env.get_vm(name)
    vm_type = params.get('vm_type')
    target = params.get('target')
    if not vm:
        vm = env.create_vm(vm_type, target, name, params, test.bindir)
    old_vm = copy.copy(vm)

    remove_vm = False
    if params.get("force_remove_vm") == "yes":
        remove_vm = True

    if remove_vm:
        vm.remove()

    start_vm = False
    update_virtnet = False
    gracefully_kill = params.get("kill_vm_gracefully") == "yes"

    if params.get("migration_mode"):
        start_vm = True
    elif params.get("start_vm") == "yes":
        # need to deal with libvirt VM differently than qemu
        if vm_type == 'libvirt' or vm_type == 'v2v':
            if not vm.is_alive():
                start_vm = True
        else:
            if not vm.is_alive():
                start_vm = True
            if params.get("check_vm_needs_restart", "yes") == "yes":
                if vm.needs_restart(name=name,
                                    params=params,
                                    basedir=test.bindir):
                    vm.devices = None
                    start_vm = True
                    old_vm.destroy(gracefully=gracefully_kill)
                    update_virtnet = True

    if start_vm:
        if vm_type == "libvirt" and params.get("type") != "unattended_install":
            vm.params = params
            vm.start()
        elif vm_type == "v2v":
            vm.params = params
            vm.start()
        else:
            if update_virtnet:
                vm.update_vm_id()
                vm.virtnet = utils_net.VirtNet(params, name, vm.instance)
            # Start the VM (or restart it if it's already up)
            if params.get("reuse_previous_config", "no") == "no":
                vm.create(
                    name,
                    params,
                    test.bindir,
                    migration_mode=params.get("migration_mode"),
                    migration_fd=params.get("migration_fd"),
                    migration_exec_cmd=params.get("migration_exec_cmd_dst"))
            else:
                vm.create(
                    migration_mode=params.get("migration_mode"),
                    migration_fd=params.get("migration_fd"),
                    migration_exec_cmd=params.get("migration_exec_cmd_dst"))
    elif not vm.is_alive():  # VM is dead and won't be started, update params
        vm.devices = None
        vm.params = params
    else:  # VM is alive and we don't care
        if params.get("kill_vm_before_test") == "yes":
            # Destroy the VM if kill_vm_before_test = "yes".
            old_vm.destroy(gracefully=gracefully_kill)
        else:
            # VM is alive and we just need to open the serial console
            vm.create_serial_console()

    pause_vm = False

    if params.get("paused_after_start_vm") == "yes":
        pause_vm = True
        # Check the status of vm
        if (not vm.is_alive()) or (vm.is_paused()):
            pause_vm = False

    if pause_vm:
        vm.pause()
예제 #11
0
def preprocess_vm(test, params, env, name):
    """
    Preprocess a single VM object according to the instructions in params.
    Start the VM if requested and get a screendump.

    :param test: An Autotest test object.
    :param params: A dict containing VM preprocessing parameters.
    :param env: The environment (a dict-like object).
    :param name: The name of the VM object.
    """
    vm = env.get_vm(name)
    vm_type = params.get('vm_type')
    connect_uri = params.get('connect_uri')
    target = params.get('target')

    create_vm = False
    if not vm:
        create_vm = True
    elif vm_type == 'libvirt':
        connect_uri = libvirt_vm.normalize_connect_uri(connect_uri)
        if (not vm.connect_uri == connect_uri):
            create_vm = True
    else:
        pass
    if create_vm:
        vm = env.create_vm(vm_type, target, name, params, test.bindir)

    old_vm = copy.copy(vm)

    if vm_type == 'libvirt':
        if not vm.exists() and (params.get("type") != "unattended_install"
                                and params.get("type") != "svirt_install"):
            error_msg = "Test VM %s does not exist." % name
            if name == params.get("main_vm"):
                error_msg += " You may need --install option to create the guest."
                raise error.TestError(error_msg)
            else:
                raise error.TestNAError(error_msg)

    remove_vm = False
    if params.get("force_remove_vm") == "yes":
        remove_vm = True

    if remove_vm:
        vm.remove()

    start_vm = False
    update_virtnet = False
    gracefully_kill = params.get("kill_vm_gracefully") == "yes"

    if params.get("migration_mode"):
        start_vm = True
    elif params.get("start_vm") == "yes":
        # need to deal with libvirt VM differently than qemu
        if vm_type == 'libvirt' or vm_type == 'v2v':
            if not vm.is_alive():
                start_vm = True
        else:
            if not vm.is_alive():
                start_vm = True
            if params.get("check_vm_needs_restart", "yes") == "yes":
                if vm.needs_restart(name=name,
                                    params=params,
                                    basedir=test.bindir):
                    vm.devices = None
                    start_vm = True
                    old_vm.destroy(gracefully=gracefully_kill)
                    update_virtnet = True

    if start_vm:
        if vm_type == "libvirt" and params.get("type") != "unattended_install":
            vm.params = params
            vm.start()
        elif vm_type == "v2v":
            vm.params = params
            vm.start()
        else:
            if update_virtnet:
                vm.update_vm_id()
                vm.virtnet = utils_net.VirtNet(params, name, vm.instance)
            # Start the VM (or restart it if it's already up)
            if params.get("reuse_previous_config", "no") == "no":
                vm.create(
                    name,
                    params,
                    test.bindir,
                    migration_mode=params.get("migration_mode"),
                    migration_fd=params.get("migration_fd"),
                    migration_exec_cmd=params.get("migration_exec_cmd_dst"))
            else:
                vm.create(
                    migration_mode=params.get("migration_mode"),
                    migration_fd=params.get("migration_fd"),
                    migration_exec_cmd=params.get("migration_exec_cmd_dst"))
    elif not vm.is_alive():  # VM is dead and won't be started, update params
        vm.devices = None
        vm.params = params
    else:
        # Only work when parameter 'start_vm' is no and VM is alive
        if params.get("kill_vm_before_test") == "yes" and\
           params.get("start_vm") == "no":
            old_vm.destroy(gracefully=gracefully_kill)
        else:
            # VM is alive and we just need to open the serial console
            vm.create_serial_console()

    pause_vm = False

    if params.get("paused_after_start_vm") == "yes":
        pause_vm = True
        # Check the status of vm
        if (not vm.is_alive()) or (vm.is_paused()):
            pause_vm = False

    if pause_vm:
        vm.pause()

    if params.get("check_kernel_cmd_line_from_serial") == "yes":
        debug_msg = ""
        if vm.is_paused():
            debug_msg += "VM is paused."
        elif not vm.is_alive():
            debug_msg += "VM is not alive."
        elif vm.serial_console is None:
            debug_msg += "There is no serial console in VM."
        if debug_msg:
            debug_msg += " Skip the kernel command line check."
            logging.warn(debug_msg)
            return
        cmd_line = params.get("kernel_cmd_line_str", "Command line:")
        try:
            output = vm.serial_console.read_until_output_matches(cmd_line,
                                                                 timeout=60)

            kernel_cmd_line = re.findall("%s.*" % cmd_line, output[1])[0]
            kernel_options_exist = params.get("kernel_options_exist", "")
            kernel_options_not_exist = params.get("kernel_options_not_exist",
                                                  "")

            err_msg = ""
            for kernel_option in kernel_options_exist.split():
                if kernel_option not in kernel_cmd_line:
                    err_msg += "%s not in kernel command line" % kernel_option
                    err_msg += " as expect."
            for kernel_option in kernel_options_not_exist.split():
                if kernel_option in kernel_cmd_line:
                    err_msg += "%s exist in kernel command" % kernel_option
                    err_msg += " line."
            if err_msg:
                err_msg += " Kernel command line get from"
                err_msg += " serial output is %s" % kernel_cmd_line
                raise error.TestError(err_msg)

            logging.info("Kernel command line get from serial port is"
                         " as expect")
        except Exception, err:
            logging.warn("Did not get the kernel command line from serial "
                         "port output. Skip the kernel command line check."
                         "Error is %s" % err)