seabios_strict.py 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
import os
import re
import logging

from virttest import error_context
from virttest import utils_misc
from virttest import data_dir
from virttest import env_process

from avocado.utils import process


@error_context.context_aware
def run(test, params, env):
    """
    KVM seabios test:
    1) Start guest with sga bios
    2) Check the boot result when '-boot strict=on/off'
       on:  Hard Disk -> NIC
       off: Hard Disk -> NIC -> DVD/CD ...

    :param test: QEMU test object
    :param params: Dictionary with the test parameters
    :param env: Dictionary with test environment.
    """

    def create_cdrom():
        """
        Create 'test' cdrom
        """
        logging.info("creating test cdrom")
        cdrom_test = params.get("cdrom_test", '/tmp/test.iso')
        cdrom_test = utils_misc.get_path(data_dir.get_data_dir(), cdrom_test)
        process.run("dd if=/dev/urandom of=test bs=10M count=1")
        process.run("mkisofs -o %s test" % cdrom_test)
        process.run("rm -f test")

    def cleanup_cdrom():
        """
        Remove 'test' cdrom
        """
        logging.info("cleaning up test cdrom")
        cdrom_test = utils_misc.get_path(data_dir.get_data_dir(),
                                         params.get("cdrom_test"))
        os.remove(cdrom_test)

    def boot_check(info):
        """
        boot info check
        """
        return re.search(info, vm.serial_console.get_stripped_output(), re.S)

    error_context.context("Start guest with sga bios", logging.info)
    create_cdrom()
    params["start_vm"] = "yes"
    env_process.preprocess_vm(test, params, env, params.get("main_vm"))

    vm = env.get_vm(params["main_vm"])
    vm.verify_alive()
    vm.pause()

    # Disable nic device, boot fail from nic device except user model
    if params['nettype'] != 'user':
        for nic in vm.virtnet:
            process.system("ifconfig %s down" % nic.ifname)
    vm.resume()

    timeout = float(params.get("login_timeout", 240))
    fail_infos = params['boot_fail_infos']
    fail_infos_ex = params['boot_fail_infos_extra']
    boot_strict = (params['boot_strict'] == 'on')

    try:
        error_context.context("Check guest boot result", logging.info)
        if not utils_misc.wait_for(lambda: boot_check(fail_infos), timeout, 1):
            err = "Guest does not boot from Hard Disk first and then NIC"
            test.fail(err)

        if utils_misc.wait_for(lambda: boot_check(fail_infos_ex), timeout, 1):
            if boot_strict:
                err = "Guest tries to boot from DVD/CD when 'strict' is on"
                test.fail(err)
        else:
            if not boot_strict:
                err = "Guest does not try to boot from DVD/CD"
                err += " when 'strict' is off"
                test.fail(err)
    finally:
        cleanup_cdrom()