Esempio n. 1
0
def _get_vm_name(request):
    """Helper function to get vm name from test requirement mark.

    At first we try to get a requirement value from ``pytestmark`` module list. If it's missing we
    can try to look up it in the test function itself. There is one restriction for it. We cannot
    get the test function mark from module scoped fixtures.
    """
    try:
        req = [
            mark.args[0] for mark in request.module.pytestmark
            if mark.name == "requirement"
        ]
    except AttributeError:
        req = None
        logger.debug("Could not get the requirement from pytestmark")
    if not req and request.scope == "function":
        try:
            req = [
                mark.args for mark in request.function.pytestmark
                if mark.name == 'requirement'
            ][0]
        except AttributeError:
            raise CFMEException("VM name can not be obtained")

    vm_name = random_vm_name(req[0])

    if not request.config.getoption('--no-assignee-vm-name'):
        if isinstance(request.node, pytest.Function):
            assignee = get_parsed_docstring(
                request.node,
                request.session._docstrings_cache).get('assignee', '')
        else:
            # Fetch list of tests in the module object
            test_list = [
                item for item in dir(request.module) if
                item.startswith('test_') and not ('test_requirements' == item)
            ]
            # Find out assignee for each test in test_list
            assignee_list = list()
            for test in test_list:
                nodeid = f'{request.node.fspath.strpath}::{test}'
                try:
                    assignee_list.append(
                        request.session._docstrings_cache[nodeid]['assignee'])
                except KeyError:
                    continue
            # If all tests have same assignee, set length will be 1, else set assignee='module'
            assignee = assignee_list[0] if len(
                set(assignee_list)) == 1 else 'module'
        vm_name = f'{vm_name}-{assignee}'

    return vm_name
Esempio n. 2
0
def _create_vm(request, template, provider, vm_name):
    if not request.config.getoption('--no-assignee-vm-name'):
        if isinstance(request.node, pytest.Function):
            assignee = get_parsed_docstring(
                request.node,
                request.session._docstrings_cache).get('assignee', '')
        else:
            # Fetch list of tests in the module object
            test_list = [
                item for item in dir(request.module) if
                item.startswith('test_') and not ('test_requirements' == item)
            ]
            # Find out assignee for each test in test_list
            assignee_list = list()
            for test in test_list:
                nodeid = f'{request.node.fspath.strpath}::{test}'
                try:
                    assignee_list.append(
                        request.session._docstrings_cache[nodeid]['assignee'])
                except KeyError:
                    continue
            # If all tests have same assignee, set length will be 1, else set assignee='module'
            assignee = assignee_list[0] if len(
                set(assignee_list)) == 1 else 'module'
        vm_name = f'{vm_name}-{assignee}'
    collection = provider.appliance.provider_based_collection(provider)
    try:
        vm_obj = collection.instantiate(
            vm_name,
            provider,
            template_name=provider.data['templates'][template].name)
    except (KeyError, AttributeError):
        pytest.skip('No appropriate template was passed to the fixture.')
    vm_obj.create_on_provider(allow_skip='default')

    @request.addfinalizer
    def _cleanup():
        vm_obj.cleanup_on_provider()
        provider.refresh_provider_relationships()

    vm_obj.mgmt.ensure_state(VmState.RUNNING)

    if not vm_obj.exists:
        provider.refresh_provider_relationships()
        vm_obj.wait_to_appear()

    return vm_obj