Exemplo n.º 1
0
def run(test, params, env):
    """
    Test of libvirt SPICE related features.

    1) Block specified ports if required;
    2) Setup SPICE TLS certification if required;
    3) Setup graphics tag in VM;
    4) Try to start VM;
    5) Parse and check result with expected.
    6) Clean up environment.
    """

    vm_name = params.get("main_vm", "virt-tests-vm1")
    spice_xml = params.get("spice_xml", "no") == 'yes'
    vnc_xml = params.get("vnc_xml", "no") == 'yes'
    vm = env.get_vm(vm_name)
    if vm.is_alive():
        vm.destroy()

    sockets = block_ports(params)
    networks = setup_networks(params)

    expected_result = get_expected_results(params, networks)
    env_state = EnvState(params, expected_result)

    vm_xml = VMXML.new_from_inactive_dumpxml(vm_name)
    vm_xml_backup = vm_xml.copy()
    try:
        vm_xml.remove_all_graphics()
        if spice_xml:
            spice_graphic = generate_spice_graphic_xml(params, expected_result)
            logging.debug('Test SPICE XML is: %s', spice_graphic)
            vm_xml.devices = vm_xml.devices.append(spice_graphic)
        if vnc_xml:
            vnc_graphic = generate_vnc_graphic_xml(params, expected_result)
            logging.debug('Test VNC XML is: %s', vnc_graphic)
            vm_xml.devices = vm_xml.devices.append(vnc_graphic)
        vm_xml.sync()
        all_ips = utils_net.get_all_ips()

        fail_patts = expected_result['fail_patts']
        try:
            vm.start()
        except virt_vm.VMStartError, detail:
            if not fail_patts:
                raise error.TestFail(
                    "Expect VM can be started, but failed with: %s" % detail)
            for patt in fail_patts:
                if re.search(patt, str(detail)):
                    return
            raise error.TestFail(
                "Expect fail with error in %s, but failed with: %s" %
                (fail_patts, detail))
        else:
def run(test, params, env):
    """
    Test of libvirt SPICE related features.

    1) Block specified ports if required;
    2) Setup SPICE TLS certification if required;
    3) Setup graphics tag in VM;
    4) Try to start VM;
    5) Parse and check result with expected.
    6) Clean up environment.
    """

    vm_name = params.get("main_vm", "virt-tests-vm1")
    spice_xml = params.get("spice_xml", "no") == 'yes'
    vnc_xml = params.get("vnc_xml", "no") == 'yes'
    vm = env.get_vm(vm_name)
    if vm.is_alive():
        vm.destroy()

    sockets = block_ports(params)
    networks = setup_networks(params)

    expected_result = get_expected_results(params, networks)
    env_state = EnvState(params, expected_result)

    vm_xml = VMXML.new_from_inactive_dumpxml(vm_name)
    vm_xml_backup = vm_xml.copy()
    try:
        vm_xml.remove_all_graphics()
        if spice_xml:
            spice_graphic = generate_spice_graphic_xml(params, expected_result)
            logging.debug('Test SPICE XML is: %s', spice_graphic)
            vm_xml.devices = vm_xml.devices.append(spice_graphic)
        if vnc_xml:
            vnc_graphic = generate_vnc_graphic_xml(params, expected_result)
            logging.debug('Test VNC XML is: %s', vnc_graphic)
            vm_xml.devices = vm_xml.devices.append(vnc_graphic)
        vm_xml.sync()
        all_ips = utils_net.get_all_ips()

        fail_patts = expected_result['fail_patts']
        try:
            vm.start()
        except virt_vm.VMStartError, detail:
            if not fail_patts:
                raise error.TestFail(
                    "Expect VM can be started, but failed with: %s" % detail)
            for patt in fail_patts:
                if re.search(patt, str(detail)):
                    return
            raise error.TestFail(
                "Expect fail with error in %s, but failed with: %s"
                % (fail_patts, detail))
        else:
Exemplo n.º 3
0
def get_host_ip(test):
    """Get IP for host.

    Parameters
    ----------
    test : SpiceTest
        Spice test object.

    """
    try:
        ip = utils_net.get_host_ip_address(test.cfg)
    except utils_net.NetError:
        ips = utils_net.get_all_ips()
        ip = ips[0]
        logger.info("Take as a host IP: %s", ip)
    if test.kvm_g.listening_addr == "ipv6":
        ip = "[" + utils_misc.convert_ipv4_to_ipv6(ip) + "]"
    return ip
Exemplo n.º 4
0
def get_host_ip(test):
    """Get IP for host.

    Parameters
    ----------
    test : SpiceTest
        Spice test object.

    """
    ip_ver = test.kvm_g.listening_addr
    if not ip_ver:
        ip_ver = "ipv4"
    try:
        ip = utils_net.get_host_ip_address(test.cfg, ip_ver)
    except utils_net.NetError:
        ips = utils_net.get_all_ips()
        ip = ips[0]
        logger.info("Take as a host IP: %s", ip)
    if ip_ver == "ipv6":
        ip = "[" + ip + "]"
    return ip
Exemplo n.º 5
0
def get_expected_listen_ips(params, networks, expected_result):
    """
    Predict listen IPs from parameters.
    """
    def random_ip(version='ipv4'):
        """
        Randomly select a valid ip with target version.
        """
        ips = [ip for ip in utils_net.get_all_ips() if ip.version == version]
        return random.choice(ips)

    spice_listen_type = params.get("spice_listen_type", "not_set")
    if spice_listen_type == 'none':
        expected_spice_ips = []
    elif spice_listen_type == 'network':
        net_type = params.get("spice_network_type", "vnet")
        spice_network = networks[net_type]
        expected_spice_ips = [utils_net.IPAddress(spice_network.ip)]
    else:
        if spice_listen_type == 'address':
            spice_listen = params.get("spice_listen_address", 'not_set')
        else:
            spice_listen = params.get("spice_listen", "not_set")

        if spice_listen == 'not_set':
            expected_spice_ips = [utils_net.IPAddress('127.0.0.1')]
        elif spice_listen == 'valid_ipv4':
            expected_spice_ips = [random_ip('ipv4')]
        elif spice_listen == 'valid_ipv6':
            expected_spice_ips = [random_ip('ipv6')]
        else:
            listen_ip = utils_net.IPAddress(spice_listen)
            if listen_ip == utils_net.IPAddress('0.0.0.0'):
                expected_spice_ips = [ip for ip in utils_net.get_all_ips()
                                      if ip.version == 'ipv4']
            elif listen_ip == utils_net.IPAddress('::'):
                expected_spice_ips = [ip for ip in utils_net.get_all_ips()]
            else:
                expected_spice_ips = [listen_ip]
    for ip in expected_spice_ips:
        logging.debug("Expected SPICE IP: %s", ip)

    expected_result['spice_ips'] = expected_spice_ips

    vnc_listen_type = params.get("vnc_listen_type", "not_set")
    vnc_listen = params.get("vnc_listen", "not_set")
    if vnc_listen_type == 'none':
        expected_vnc_ips = []
    elif vnc_listen_type == 'network':
        net_type = params.get("vnc_network_type", "vnet")
        vnc_network = networks[net_type]
        expected_vnc_ips = [utils_net.IPAddress(vnc_network.ip)]
    else:
        if vnc_listen_type == 'address':
            vnc_listen = params.get("vnc_listen_address", 'not_set')
        else:
            vnc_listen = params.get("vnc_listen", "not_set")

        if vnc_listen == 'not_set':
            expected_vnc_ips = [utils_net.IPAddress('127.0.0.1')]
        elif vnc_listen == 'valid_ipv4':
            expected_vnc_ips = [random_ip('ipv4')]
        elif vnc_listen == 'valid_ipv6':
            expected_vnc_ips = [random_ip('ipv6')]
        else:
            listen_ip = utils_net.IPAddress(vnc_listen)
            if listen_ip == utils_net.IPAddress('0.0.0.0'):
                expected_vnc_ips = [ip for ip in utils_net.get_all_ips()
                                    if ip.version == 'ipv4']
            elif listen_ip == utils_net.IPAddress('::'):
                expected_vnc_ips = [ip for ip in utils_net.get_all_ips()]
            else:
                expected_vnc_ips = [listen_ip]
    for ip in expected_vnc_ips:
        logging.debug("Expected VNC IP: %s", ip)

    expected_result['vnc_ips'] = expected_vnc_ips
Exemplo n.º 6
0
def get_fail_pattern(params, expected_result):
    """
    Predict patterns of failure messages from parameters.
    """
    spice_xml = params.get("spice_xml", "no") == 'yes'
    vnc_xml = params.get("vnc_xml", "no") == 'yes'

    fail_patts = []
    if spice_xml:
        spice_prepare_cert = params.get("spice_prepare_cert", "yes")
        spice_tls = params.get("spice_tls", "not_set")
        default_mode = params.get("defaultMode", "any")

        plaintext_channels = expected_result['plaintext_channels']
        tls_channels = expected_result['tls_channels']
        expected_spice_ips = expected_result['spice_ips']
        expected_spice_port = expected_result['spice_port']
        expected_spice_tls_port = expected_result['spice_tls_port']

        tls_fail_patts = []
        if expected_spice_tls_port == 'not_set':
            if tls_channels:
                tls_fail_patts.append(r'TLS port( is)? not provided')
        else:
            if spice_tls != '1':
                tls_fail_patts.append(r'TLS is disabled')

        plaintext_fail_patts = []
        if expected_spice_port == 'not_set':
            if plaintext_channels:
                plaintext_fail_patts.append(r'plain port( is)? not provided')

        if default_mode == 'any':
            if tls_fail_patts and plaintext_fail_patts:
                fail_patts += tls_fail_patts + plaintext_fail_patts
            elif tls_fail_patts and not plaintext_fail_patts:
                if tls_channels:
                    fail_patts += tls_fail_patts
                else:
                    expected_spice_tls_port = expected_result['spice_tls_port'] = 'not_set'
            elif not tls_fail_patts and plaintext_fail_patts:
                if plaintext_channels:
                    fail_patts += plaintext_fail_patts
                else:
                    expected_spice_port = expected_result['spice_port'] = 'not_set'
        else:
            fail_patts += tls_fail_patts + plaintext_fail_patts

        if expected_spice_port == expected_spice_tls_port:
            if expected_spice_port != 'not_set':
                if 0 <= int(expected_spice_port) < 1024:
                    fail_patts.append(r'binding socket to \S* failed')
                elif int(expected_spice_port) > 65535 or int(expected_spice_port) < -1:
                    fail_patts.append(r'port is out of range')
                else:
                    fail_patts.append(r'Failed to reserve port')
                    fail_patts.append(r'binding socket to \S* failed')

        spice_fail_patts = []
        if expected_spice_tls_port != 'not_set' and not fail_patts:
            if spice_prepare_cert == 'no':
                spice_fail_patts.append(r'Could not load certificates')
            if 0 <= int(expected_spice_tls_port) < 1024:
                spice_fail_patts.append(r'binding socket to \S* failed')
            elif int(expected_spice_tls_port) > 65535 or int(expected_spice_tls_port) < -1:
                spice_fail_patts.append(r'port is out of range')

        if expected_spice_port != 'not_set' and not fail_patts:
            if 0 <= int(expected_spice_port) < 1024:
                spice_fail_patts.append(r'binding socket to \S* failed')
            elif int(expected_spice_port) > 65535 or int(expected_spice_port) < -1:
                spice_fail_patts.append(r'port is out of range')
        fail_patts += spice_fail_patts

        if fail_patts or expected_spice_port == 'not_set':
            fail_patts += tls_fail_patts

        if any([ip not in utils_net.get_all_ips() for ip in expected_spice_ips]):
            fail_patts.append(r'binding socket to \S* failed')

    if vnc_xml:
        vnc_prepare_cert = params.get("vnc_prepare_cert", "yes")

        expected_vnc_ips = expected_result['vnc_ips']
        vnc_tls = params.get("vnc_tls", "not_set")
        expected_vnc_port = expected_result['vnc_port']

        vnc_fail_patts = []
        if vnc_tls == '1' and vnc_prepare_cert == 'no':
            vnc_fail_patts.append('Failed to find x509 certificates')

        if expected_vnc_port != 'not_set':
            if int(expected_vnc_port) < 5900:
                vnc_fail_patts.append('Failed to start VNC server')
                vnc_fail_patts.append(r'vnc port must be in range \[5900,65535\]')
            elif int(expected_vnc_port) > 65535:
                vnc_fail_patts.append('Failed to start VNC server')
                vnc_fail_patts.append(r'vnc port must be in range \[5900,65535\]')
        fail_patts += vnc_fail_patts

        if any([ip not in utils_net.get_all_ips() for ip in expected_vnc_ips]):
            fail_patts.append(r'Cannot assign requested address')

    expected_result['fail_patts'] = fail_patts
Exemplo n.º 7
0
def get_expected_ports(params, expected_result):
    """
    Predict listening ports from parameters.
    """
    spice_xml = params.get("spice_xml", "no") == 'yes'
    vnc_xml = params.get("vnc_xml", "no") == 'yes'
    port_min = params.get("remote_display_port_min", '5900')
    port_max = params.get("remote_display_port_max", '65535')

    expected_spice_ips = expected_result['spice_ips']
    expected_vnc_ips = expected_result['vnc_ips']
    expected_ips = expected_spice_ips + expected_vnc_ips

    port_allocator = PortAllocator(expected_ips, port_min, port_max)

    if spice_xml:
        spice_port = params.get("spice_port", "not_set")
        spice_tls_port = params.get("spice_tlsPort", "not_set")
        spice_autoport = params.get("spice_autoport", "yes")
        default_mode = params.get("defaultMode", "any")

        get_expected_channels(params, expected_result)

        plaintext_channels = expected_result['plaintext_channels']
        tls_channels = expected_result['tls_channels']

        if spice_port == '0':
            spice_port = 'not_set'
        if spice_tls_port == '0':
            spice_tls_port = 'not_set'

        expected_port = spice_port
        expected_tls_port = spice_tls_port

        insecure_autoport = spice_port == '-1' or spice_autoport == 'yes'
        secure_autoport = spice_tls_port == '-1' or spice_autoport == 'yes'

        if insecure_autoport:
            if plaintext_channels or default_mode == 'any':
                expected_port = port_allocator.allocate()
            else:
                expected_port = 'not_set'

        if secure_autoport:
            if tls_channels or default_mode == 'any':
                expected_tls_port = port_allocator.allocate()
            else:
                expected_tls_port = 'not_set'

        if expected_port == expected_tls_port == 'not_set':
            if insecure_autoport:
                expected_port = port_allocator.allocate()
            if secure_autoport:
                expected_tls_port = port_allocator.allocate()

        if expected_tls_port != 'not_set' and int(expected_tls_port) < -1:
            expected_tls_port = 'not_set'

        if expected_port != 'not_set' and int(expected_port) < -1:
            if expected_tls_port != 'not_set':
                expected_port = 'not_set'

        logging.debug('Expected SPICE port: ' + expected_port)
        logging.debug('Expected SPICE tls_port: ' + expected_tls_port)
        expected_result['spice_port'] = expected_port
        expected_result['spice_tls_port'] = expected_tls_port

    if vnc_xml:
        vnc_port = params.get("vnc_port", "not_set")
        vnc_autoport = params.get("vnc_autoport", "yes")
        auto_unix_socket = params.get("vnc_auto_unix_socket", 'not_set')

        if any([ip not in utils_net.get_all_ips() for ip in expected_vnc_ips]):
            expected_vnc_port = 'not_set'
            expected_result['vnc_port'] = 'not_set'
        else:
            expected_vnc_port = vnc_port

            vnc_autoport = vnc_port == '-1' or vnc_autoport == 'yes'

            if vnc_autoport:
                expected_vnc_port = port_allocator.allocate()

            if auto_unix_socket == '1':
                expected_vnc_port = 'not_set'

        logging.debug('Expected VNC port: ' + expected_vnc_port)
        expected_result['vnc_port'] = expected_vnc_port
Exemplo n.º 8
0
 def random_ip(version='ipv4'):
     """
     Randomly select a valid ip with target version.
     """
     ips = [ip for ip in utils_net.get_all_ips() if ip.version == version]
     return random.choice(ips)
Exemplo n.º 9
0
def get_expected_listen_ips(params, networks, expected_result):
    """
    Predict listen IPs from parameters.
    """
    def random_ip(version='ipv4'):
        """
        Randomly select a valid ip with target version.
        """
        ips = [ip for ip in utils_net.get_all_ips() if ip.version == version]
        return random.choice(ips)

    spice_listen_type = params.get("spice_listen_type", "not_set")
    if spice_listen_type == 'network':
        net_type = params.get("spice_network_type", "vnet")
        spice_network = networks[net_type]
        expected_spice_ips = [utils_net.IPAddress(spice_network.ip)]
    else:
        if spice_listen_type == 'address':
            spice_listen = params.get("spice_listen_address", 'not_set')
        else:
            spice_listen = params.get("spice_listen", "not_set")

        if spice_listen == 'not_set':
            expected_spice_ips = [utils_net.IPAddress('127.0.0.1')]
        elif spice_listen == 'valid_ipv4':
            expected_spice_ips = [random_ip('ipv4')]
        elif spice_listen == 'valid_ipv6':
            expected_spice_ips = [random_ip('ipv6')]
        else:
            listen_ip = utils_net.IPAddress(spice_listen)
            if listen_ip == utils_net.IPAddress('0.0.0.0'):
                expected_spice_ips = [ip for ip in utils_net.get_all_ips()
                                      if ip.version == 'ipv4']
            elif listen_ip == utils_net.IPAddress('::'):
                expected_spice_ips = [ip for ip in utils_net.get_all_ips()]
            else:
                expected_spice_ips = [listen_ip]
        for ip in expected_spice_ips:
            logging.debug("Expected SPICE IP: %s", ip)

    expected_result['spice_ips'] = expected_spice_ips

    vnc_listen_type = params.get("vnc_listen_type", "not_set")
    vnc_listen = params.get("vnc_listen", "not_set")
    if vnc_listen_type == 'network':
        net_type = params.get("vnc_network_type", "vnet")
        vnc_network = networks[net_type]
        expected_vnc_ips = [utils_net.IPAddress(vnc_network.ip)]
    else:
        if vnc_listen_type == 'address':
            vnc_listen = params.get("vnc_listen_address", 'not_set')
        else:
            vnc_listen = params.get("vnc_listen", "not_set")

        if vnc_listen == 'not_set':
            expected_vnc_ips = [utils_net.IPAddress('127.0.0.1')]
        elif vnc_listen == 'valid_ipv4':
            expected_vnc_ips = [random_ip('ipv4')]
        elif vnc_listen == 'valid_ipv6':
            expected_vnc_ips = [random_ip('ipv6')]
        else:
            listen_ip = utils_net.IPAddress(vnc_listen)
            if listen_ip == utils_net.IPAddress('0.0.0.0'):
                expected_vnc_ips = [ip for ip in utils_net.get_all_ips()
                                    if ip.version == 'ipv4']
            elif listen_ip == utils_net.IPAddress('::'):
                expected_vnc_ips = [ip for ip in utils_net.get_all_ips()]
            else:
                expected_vnc_ips = [listen_ip]
        for ip in expected_vnc_ips:
            logging.debug("Expected VNC IP: %s", ip)

    expected_result['vnc_ips'] = expected_vnc_ips
Exemplo n.º 10
0
def run(test, params, env):
    """
    Test of libvirt SPICE related features.

    1) Block specified ports if required;
    2) Setup SPICE TLS certification if required;
    3) Setup graphics tag in VM;
    4) Try to start VM;
    5) Parse and check result with expected.
    6) Clean up environment.
    """
    # Since 3.1.0, '-1' is not an valid value for the VNC port while
    # autoport is disabled, it means the VM will be failed to be
    # started as expected. So, cancel test this case since there is
    # one similar test scenario in the negative_test, which is
    # negative_tests.vnc_only.no_autoport.port_-2
    if libvirt_version.version_compare(3, 1, 0) and (params.get("vnc_port")
                                                     == '-1'):
        test.cancel('Cancel this case, since it is equivalence class test '
                    'with case negative_tests.vnc_only.no_autoport.port_-2')

    # Since 2.0.0, there are some changes for listen type and port
    # 1. Two new listen types: 'none' and 'socket'(not covered in this test)
    #    In 'none' listen type, no listen address and port is 0,
    #    that means we need reset the listen_type for previous versions,
    #    so we can get the expect result(vm start fail)
    # 2. Spice port accept negative number less than -1
    #    If spice port less than -1, the VM can start normally, but both
    #    listen address and port will be omitted
    spice_listen_type = params.get('spice_listen_type', 'not_set')
    vnc_listen_type = params.get('vnc_listen_type', 'not_set')
    spice_port = params.get('spice_port', 'not_set')
    spice_tlsPort = params.get('spice_tlsPort', 'not_set')
    if not libvirt_version.version_compare(2, 0, 0):
        for listen_type in [spice_listen_type, vnc_listen_type]:
            if listen_type == 'none':
                params[listen_type] = 'not_set'
    else:
        try:
            if int(spice_port) < -1:
                params["negative_test"] = "no"
        except ValueError:
            pass

    vm_name = params.get("main_vm", "avocado-vt-vm1")
    spice_xml = params.get("spice_xml", "no") == 'yes'
    vnc_xml = params.get("vnc_xml", "no") == 'yes'
    is_negative = params.get("negative_test", "no") == 'yes'
    graphic_passwd = params.get("graphic_passwd")
    remote_viewer_check = params.get("remote_viewer_check", "no") == 'yes'
    valid_time = params.get("valid_time")

    sockets = block_ports(params)
    networks = setup_networks(params, test)

    expected_result = get_expected_results(params, networks)
    env_state = EnvState(params, expected_result)

    vm = env.get_vm(vm_name)
    vm_xml = VMXML.new_from_inactive_dumpxml(vm_name)
    vm_xml_backup = vm_xml.copy()

    config = utils_config.LibvirtQemuConfig()
    libvirtd = utils_libvirtd.Libvirtd()

    if graphic_passwd:
        spice_passwd_place = params.get("spice_passwd_place", "not_set")
        vnc_passwd_place = params.get("vnc_passwd_place", "not_set")
        if spice_passwd_place == "qemu":
            config['spice_password'] = '******' % graphic_passwd
            libvirtd.restart()
        if vnc_passwd_place == "qemu":
            config['vnc_password'] = '******' % graphic_passwd
            libvirtd.restart()
    try:
        vm_xml.remove_all_graphics()
        if spice_xml:
            spice_graphic = generate_spice_graphic_xml(params, expected_result)
            logging.debug('Test SPICE XML is: %s', spice_graphic)
            vm_xml.devices = vm_xml.devices.append(spice_graphic)
        if vnc_xml:
            vnc_graphic = generate_vnc_graphic_xml(params, expected_result)
            logging.debug('Test VNC XML is: %s', vnc_graphic)
            vm_xml.devices = vm_xml.devices.append(vnc_graphic)
        vm_xml.sync()
        all_ips = utils_net.get_all_ips()

        fail_patts = expected_result['fail_patts']
        try:
            vm.start()
        except virt_vm.VMStartError as detail:
            if not fail_patts:
                test.fail("Expect VM can be started, but failed with: %s" %
                          detail)
            for patt in fail_patts:
                if re.search(patt, str(detail)):
                    return
            test.fail("Expect fail with error in %s, but failed with: %s" %
                      (fail_patts, detail))
        else:
            if fail_patts:
                test.fail("Expect VM can't be started with %s, but started." %
                          fail_patts)

        if spice_xml:
            spice_opts, plaintext_channels, tls_channels = qemu_spice_options(
                vm)
            check_spice_result(spice_opts, plaintext_channels, tls_channels,
                               expected_result, all_ips, test)
            # Use remote-viewer to connect guest
            if remote_viewer_check:
                rv_log_str = params.get("rv_log_str")
                rv_log_auth = params.get("rv_log_auth")
                opt_str = params.get('opt_str')
                check_connection('spice', expected_result['spice_port'],
                                 graphic_passwd, test, rv_log_str, rv_log_auth,
                                 opt_str, valid_time)

        if vnc_xml:
            vnc_opts = qemu_vnc_options(vm)
            check_vnc_result(vnc_opts, expected_result, all_ips, test)
            # Use remote-viewer to connect guest
            if remote_viewer_check:
                rv_log_str = params.get("rv_log_str")
                rv_log_auth = params.get("rv_log_auth")
                opt_str = params.get('opt_str')
                check_connection('vnc', expected_result['vnc_port'],
                                 graphic_passwd, test, rv_log_str, rv_log_auth,
                                 opt_str, valid_time)

        if is_negative:
            test.fail("Expect negative result. But start succeed!")
    except exceptions.TestFail as detail:
        bug_url = params.get('bug_url', None)
        if bug_url:
            logging.error("You probably encountered a known bug. "
                          "Please check %s for reference" % bug_url)
        raise
    finally:
        for sock in sockets:
            sock.close()
        if networks:
            for network in list(networks.values()):
                network.cleanup()
        vm_xml_backup.sync()
        os.system('rm -f /dev/shm/spice*')
        env_state.restore()
        config.restore()
        libvirtd.restart()
Exemplo n.º 11
0
def get_fail_pattern(params, expected_result):
    """
    Predict patterns of failure messages from parameters.
    """
    spice_xml = params.get("spice_xml", "no") == 'yes'
    vnc_xml = params.get("vnc_xml", "no") == 'yes'

    fail_patts = []
    if spice_xml:
        spice_prepare_cert = params.get("spice_prepare_cert", "yes")
        spice_tls = params.get("spice_tls", "not_set")
        default_mode = params.get("defaultMode", "any")

        plaintext_channels = expected_result['plaintext_channels']
        tls_channels = expected_result['tls_channels']
        expected_spice_ips = expected_result['spice_ips']
        expected_spice_port = expected_result['spice_port']
        expected_spice_tls_port = expected_result['spice_tls_port']

        tls_fail_patts = []
        if expected_spice_tls_port == 'not_set':
            if tls_channels:
                tls_fail_patts.append(r'TLS port( is)? not provided')
        else:
            if spice_tls != '1':
                tls_fail_patts.append(r'TLS is disabled')

        plaintext_fail_patts = []
        if expected_spice_port == 'not_set':
            if plaintext_channels:
                plaintext_fail_patts.append(r'plain port( is)? not provided')

        if default_mode == 'any':
            if tls_fail_patts and plaintext_fail_patts:
                fail_patts += tls_fail_patts + plaintext_fail_patts
            elif tls_fail_patts and not plaintext_fail_patts:
                if tls_channels:
                    fail_patts += tls_fail_patts
                else:
                    expected_spice_tls_port = expected_result[
                        'spice_tls_port'] = 'not_set'
            elif not tls_fail_patts and plaintext_fail_patts:
                if plaintext_channels:
                    fail_patts += plaintext_fail_patts
                else:
                    expected_spice_port = expected_result[
                        'spice_port'] = 'not_set'
        else:
            fail_patts += tls_fail_patts + plaintext_fail_patts

        if expected_spice_port == expected_spice_tls_port:
            if expected_spice_port == 'not_set':
                fail_patts.append(r'neither port nor tls-port specified')
                fail_patts.append(r'port is out of range')
            else:
                if 0 <= int(expected_spice_port) < 1024:
                    fail_patts.append(r'binding socket to \S* failed')
                elif int(expected_spice_port) > 65535 or int(
                        expected_spice_port) < -1:
                    fail_patts.append(r'port is out of range')
                else:
                    fail_patts.append(r'Failed to reserve port')
                    fail_patts.append(r'binding socket to \S* failed')

        spice_fail_patts = []
        if expected_spice_tls_port != 'not_set' and not fail_patts:
            if spice_prepare_cert == 'no':
                spice_fail_patts.append(r'Could not load certificates')
            if 0 <= int(expected_spice_tls_port) < 1024:
                spice_fail_patts.append(r'binding socket to \S* failed')
            elif int(expected_spice_tls_port) > 65535 or int(
                    expected_spice_tls_port) < -1:
                spice_fail_patts.append(r'port is out of range')

        if expected_spice_port != 'not_set' and not fail_patts:
            if 0 <= int(expected_spice_port) < 1024:
                spice_fail_patts.append(r'binding socket to \S* failed')
            elif int(expected_spice_port) > 65535 or int(
                    expected_spice_port) < -1:
                spice_fail_patts.append(r'port is out of range')
        fail_patts += spice_fail_patts

        if fail_patts or expected_spice_port == 'not_set':
            fail_patts += tls_fail_patts

        if any(
            [ip not in utils_net.get_all_ips() for ip in expected_spice_ips]):
            fail_patts.append(r'binding socket to \S* failed')

    if vnc_xml:
        vnc_prepare_cert = params.get("vnc_prepare_cert", "yes")

        expected_vnc_ips = expected_result['vnc_ips']
        vnc_tls = params.get("vnc_tls", "not_set")
        expected_vnc_port = expected_result['vnc_port']

        vnc_fail_patts = []
        if vnc_tls == '1' and vnc_prepare_cert == 'no':
            vnc_fail_patts.append('Failed to find x509 certificates')

        if expected_vnc_port != 'not_set':
            if int(expected_vnc_port) - 5900 < 0:
                vnc_fail_patts.append('Failed to start VNC server')
            elif int(expected_vnc_port) > 65535:
                vnc_fail_patts.append('Failed to start VNC server')
        fail_patts += vnc_fail_patts

        if any([ip not in utils_net.get_all_ips() for ip in expected_vnc_ips]):
            fail_patts.append(r'Cannot assign requested address')

    expected_result['fail_patts'] = fail_patts