Ejemplo n.º 1
0
def uncollectif(item):
    """ Evaluates if an item should be uncollected

    Tests markers against a supplied lambda from the marker object to determine
    if the item should be uncollected or not.
    """

    marker = item.get_marker('uncollectif')
    if marker:
        log_msg = 'Trying uncollecting {}: {}'.format(item.name,
            marker.kwargs.get('reason', 'No reason given'))

        try:
            arg_names = inspect.getargspec(marker._arglist[0][0][0]).args
        except TypeError:
            logger.debug(log_msg)
            return not bool(marker.args[0])
        try:
            values = extract_fixtures_values(item)
            args = [values[arg] for arg in arg_names]
        except KeyError:
            missing_argnames = list(set(arg_names) - set(item._request.funcargnames))
            func_name = item.name
            if missing_argnames:
                raise Exception("You asked for a fixture which wasn't in the function {} "
                                "prototype {}".format(func_name, missing_argnames))
            else:
                raise Exception("Failed to uncollect {}, best guess a fixture wasn't "
                                "ready".format(func_name))
        retval = marker.args[0](*args)
        if retval:
            logger.debug(log_msg)
        return not retval
    else:
        return True
Ejemplo n.º 2
0
def resolve_blockers(item, blockers):
    if not isinstance(blockers, (list, tuple, set)):
        raise ValueError("Type of the 'blockers' parameter must be one of: list, tuple, set")

    # Prepare the global env for the kwarg insertion
    global_env = dict(
        appliance_version=version.current_version(),
        appliance_downstream=version.appliance_is_downstream(),
        item=item,
        blockers=blockers,
    )
    # We will now extend the env with fixtures, so they can be used in the guard functions
    # We will however add only those that are not in the global_env otherwise we could overwrite
    # our own stuff.
    params = extract_fixtures_values(item)
    for funcarg, value in params.iteritems():
        if funcarg not in global_env:
            global_env[funcarg] = value

    # Check blockers
    use_blockers = []
    # Bugzilla shortcut
    blockers = map(lambda b: "BZ#{}".format(b) if isinstance(b, int) else b, blockers)
    for blocker in map(Blocker.parse, blockers):
        if blocker.blocks:
            use_blockers.append(blocker)
    # Unblocking
    discard_blockers = set([])
    for blocker in use_blockers:
        unblock_func = kwargify(blocker.kwargs.get("unblock", None))
        local_env = {"blocker": blocker}
        local_env.update(global_env)
        if unblock_func(**local_env):
            discard_blockers.add(blocker)
    for blocker in discard_blockers:
        use_blockers.remove(blocker)
    # We now have those that block testing, so we have to skip
    # Let's go in the order that they were added
    # Custom actions first
    for blocker in use_blockers:
        if "custom_action" in blocker.kwargs:
            action = kwargify(blocker.kwargs["custom_action"])
            local_env = {"blocker": blocker}
            local_env.update(global_env)
            action(**local_env)
    # And then skip
    if use_blockers:
        name, location = get_test_idents(item)
        bugs = [bug.bug_id for bug in use_blockers if hasattr(bug, "bug_id")]
        skip_data = {'type': 'blocker', 'reason': bugs}
        art_client.fire_hook('skip_test', test_location=location, test_name=name,
            skip_data=skip_data)
        pytest.skip("Skipping due to these blockers:\n{}".format(
            "\n".join(
                "- {}".format(str(blocker))
                for blocker
                in use_blockers
            )
        ))
Ejemplo n.º 3
0
def resolve_blockers(item, blockers):
    # Prepare the global env for the kwarg insertion
    global_env = dict(
        appliance_version=version.current_version(),
        appliance_downstream=version.appliance_is_downstream(),
        item=item,
        blockers=blockers,
    )
    # We will now extend the env with fixtures, so they can be used in the guard functions
    # We will however add only those that are not in the global_env otherwise we could overwrite
    # our own stuff.
    params = extract_fixtures_values(item)
    for funcarg, value in params.iteritems():
        if funcarg not in global_env:
            global_env[funcarg] = value

    # Check blockers
    use_blockers = []
    if not isinstance(blockers, (list, tuple, set)):
        blockers = [blockers]
    # Bugzilla shortcut
    blockers = map(lambda b: "BZ#{}".format(b) if isinstance(b, int) else b, blockers)
    for blocker in map(Blocker.parse, blockers):
        if blocker.blocks:
            use_blockers.append(blocker)
    # Unblocking
    discard_blockers = set([])
    for blocker in use_blockers:
        unblock_func = kwargify(blocker.kwargs.get("unblock", None))
        local_env = {"blocker": blocker}
        local_env.update(global_env)
        if unblock_func(**local_env):
            discard_blockers.add(blocker)
    for blocker in discard_blockers:
        use_blockers.remove(blocker)
    # We now have those that block testing, so we have to skip
    # Let's go in the order that they were added
    # Custom actions first
    for blocker in use_blockers:
        if "custom_action" in blocker.kwargs:
            action = kwargify(blocker.kwargs["custom_action"])
            local_env = {"blocker": blocker}
            local_env.update(global_env)
            action(**local_env)
    # And then skip
    if use_blockers:
        name, location = get_test_idents(item)
        bugs = [bug.bug_id for bug in use_blockers if hasattr(bug, "bug_id")]
        skip_data = {'type': 'blocker', 'reason': bugs}
        art_client.fire_hook('skip_test', test_location=location, test_name=name,
            skip_data=skip_data)
        pytest.skip("Skipping due to these blockers:\n{}".format(
            "\n".join(
                "- {}".format(str(blocker))
                for blocker
                in use_blockers
            )
        ))
Ejemplo n.º 4
0
def skip_plugin(item, skip, reason="Skipped"):
    if isinstance(skip, bool):
        if skip:
            pytest.skip(reason)
    elif callable(skip):
        skip_kwargified = kwargify(skip)
        if skip_kwargified(**extract_fixtures_values(item)):
            pytest.skip(reason)
    else:
        if bool(skip):
            pytest.skip(reason)
Ejemplo n.º 5
0
def resolve_blockers(item, blockers):
    # Prepare the global env for the kwarg insertion
    global_env = dict(
        appliance_version=version.current_version(),
        appliance_downstream=version.appliance_is_downstream(),
        item=item,
        blockers=blockers,
    )
    # We will now extend the env with fixtures, so they can be used in the guard functions
    # We will however add only those that are not in the global_env otherwise we could overwrite
    # our own stuff.
    params = extract_fixtures_values(item)
    for funcarg, value in params.iteritems():
        if funcarg not in global_env:
            global_env[funcarg] = value

    # Check blockers
    use_blockers = []
    if not isinstance(blockers, (list, tuple, set)):
        blockers = [blockers]
    # Bugzilla shortcut
    blockers = map(lambda b: "BZ#{}".format(b) if isinstance(b, int) else b, blockers)
    for blocker in map(Blocker.parse, blockers):
        if blocker.blocks:
            use_blockers.append(blocker)
    # Unblocking
    discard_blockers = set([])
    for blocker in use_blockers:
        unblock_func = kwargify(blocker.kwargs.get("unblock", None))
        local_env = {"blocker": blocker}
        local_env.update(global_env)
        if unblock_func(**local_env):
            discard_blockers.add(blocker)
    for blocker in discard_blockers:
        use_blockers.remove(blocker)
    # We now have those that block testing, so we have to skip
    # Let's go in the order that they were added
    # Custom actions first
    for blocker in use_blockers:
        if "custom_action" in blocker.kwargs:
            action = kwargify(blocker.kwargs["custom_action"])
            local_env = {"blocker": blocker}
            local_env.update(global_env)
            action(**local_env)
    # And then skip
    if use_blockers:
        pytest.skip("Skipping due to these blockers:\n{}".format(
            "\n".join(
                "- {}".format(str(blocker))
                for blocker
                in use_blockers
            )
        ))
Ejemplo n.º 6
0
def get_testcase_data(tests, test_names, item, legacy=False):
    """Gets data for single testcase entry."""
    legacy_name, parametrized_name = get_polarion_name(item)
    name = legacy_name if legacy else parametrized_name
    if name in test_names:
        return

    work_items = []
    custom_fields = {}
    try:
        description = item.function.func_doc
    except Exception:
        description = ""
    try:
        requirement = item.get_marker('requirement').args[0]
        requirement_id = cfme_data['requirements'][requirement]
        work_items.append({'id': requirement_id, 'role': 'verifies'})
    except Exception:
        pass
    try:
        tier = item.get_marker('tier').args[0]
        tier_id = caselevels[str(tier)]
        custom_fields['caselevel'] = tier_id
    except Exception:
        pass

    param_list = extract_fixtures_values(item).keys() if not legacy else None

    manual = item.get_marker('manual')
    if not manual:
        # The master here should probably link the latest "commit" eventually
        automation_script = 'http://github.com/{0}/{1}/blob/master/{2}#L{3}'.format(
            xunit['gh_owner'],
            xunit['gh_repo'],
            item.location[0],
            item.function.func_code.co_firstlineno
        )
        custom_fields['caseautomation'] = "automated"
        custom_fields['automation_script'] = automation_script
        description = '{0}<br/><br/><a href="{1}">Test Source</a>'.format(
            description, automation_script)
    else:
        custom_fields['caseautomation'] = "manualonly"
        description = '{}'.format(description)

    test_names.append(name)
    tests.append(dict(
        test_name=name,
        description=description,
        parameters=param_list,
        linked_items=work_items,
        custom_fields=custom_fields))
Ejemplo n.º 7
0
def get_testcase_data(tests, test_names, item, legacy=False):
    """Gets data for single testcase entry."""
    name = re.sub(r'\[.*\]', '', item.name) if not legacy else item.name
    if name in test_names:
        return

    work_items = []
    custom_fields = {}
    try:
        description = item.function.func_doc
    except Exception:
        description = ""
    try:
        requirement = item.get_marker('requirement').args[0]
        requirement_id = cfme_data['requirements'][requirement]
        work_items.append({'id': requirement_id, 'role': 'verifies'})
    except Exception:
        pass
    try:
        tier = item.get_marker('tier').args[0]
        tier_id = caselevels[str(tier)]
        custom_fields['caselevel'] = tier_id
    except Exception:
        pass

    param_list = extract_fixtures_values(item).keys() if not legacy else None

    manual = item.get_marker('manual')
    if not manual:
        # The master here should probably link the latest "commit" eventually
        automation_script = 'http://github.com/{0}/{1}/blob/master/{2}#L{3}'.format(
            xunit['gh_owner'],
            xunit['gh_repo'],
            item.location[0],
            item.function.func_code.co_firstlineno
        )
        custom_fields['caseautomation'] = "automated"
        custom_fields['automation_script'] = automation_script
        description = '{0}<br/><br/><a href="{1}">Test Source</a>'.format(
            description, automation_script)
    else:
        custom_fields['caseautomation'] = "manualonly"
        description = '{}'.format(description)

    test_names.append(name)
    tests.append(dict(
        test_name=name,
        description=description,
        parameters=param_list,
        linked_items=work_items,
        custom_fields=custom_fields))
Ejemplo n.º 8
0
def uncollectif(item):
    """ Evaluates if an item should be uncollected

    Tests markers against a supplied lambda from the marker object to determine
    if the item should be uncollected or not.
    """

    from utils.pytest_shortcuts import extract_fixtures_values
    marker = item.get_marker('uncollectif')
    if marker:
        from utils.log import logger
        log_msg = 'Trying uncollecting {}: {}'.format(
            item.name, marker.kwargs.get('reason', 'No reason given'))

        try:
            arg_names = inspect.getargspec(get_uncollect_function(marker)).args
        except TypeError:
            logger.debug(log_msg)
            return not bool(marker.args[0])

        try:
            values = extract_fixtures_values(item)
            # The test has already been uncollected
            if arg_names and not values:
                return
            args = [values[arg] for arg in arg_names]
        except KeyError:
            missing_argnames = list(
                set(arg_names) - set(item._request.funcargnames))
            func_name = item.name
            if missing_argnames:
                raise Exception(
                    "You asked for a fixture which wasn't in the function {} "
                    "prototype {}".format(func_name, missing_argnames))
            else:
                raise Exception(
                    "Failed to uncollect {}, best guess a fixture wasn't "
                    "ready".format(func_name))
        retval = marker.args[0](*args)
        if retval:
            logger.debug(log_msg)
        return not retval
    else:
        return True
Ejemplo n.º 9
0
def pytest_collection_modifyitems(session, config, items):
    if not config.getoption('generate_xmls'):
        return
    a = defaultdict(dict)
    ntr = []
    for item in items:
        a[item.location[0]][re.sub(
            '\[.*\]', '', item.location[2])] = a[item.location[0]].get(
                re.sub('\[.*\]', '', item.location[2]), 0) + 1
    with open('duplicates.log', 'w') as f:
        for module, tests in a.iteritems():
            for test in tests:
                if test not in ntr:
                    ntr.append(test)
                else:
                    f.write("{}\n".format(test))

    test_name = []

    testcases = etree.Element("testcases")
    testcases.attrib['project-id'] = xunit['project_id']
    response_properties = etree.Element("response-properties")
    response_property = etree.Element("response-property",
                                      name=xunit['response']['id'],
                                      value=xunit['response']['value'])
    response_properties.append(response_property)
    properties = etree.Element("properties")
    lookup = etree.Element("property", name="lookup-method", value="custom")
    properties.append(lookup)
    testcases.append(response_properties)
    testcases.append(properties)

    for item in items:
        work_items = []
        custom_fields = {}
        try:
            description = item.function.func_doc
        except:
            description = ""
        try:
            requirement = item.get_marker('requirement').args[0]
            from utils.conf import cfme_data
            requirement_id = cfme_data['requirements'][requirement]
            work_items.append({'id': requirement_id, 'role': 'verifies'})
        except:
            pass
        try:
            tier = item.get_marker('tier').args[0]
            tier_id = caselevels[str(tier)]
            custom_fields['caselevel'] = tier_id
        except:
            pass

        param_list = extract_fixtures_values(item).keys()

        manual = item.get_marker('manual')
        if not manual:
            # The master here should probably link the latest "commit" eventually
            automation_script = 'http://github.com/{}/{}/blob/master/{}#L{}'.format(
                xunit['gh_owner'], xunit['gh_repo'], item.location[0],
                item.function.func_code.co_firstlineno)
            custom_fields['caseautomation'] = "automated"
            custom_fields['automation_script'] = automation_script
            description = '{}<br/><br/><a href="{}">Test Source</a>'.format(
                description, automation_script)
        else:
            custom_fields['caseautomation'] = "manualonly"
            description = '{}'.format(description)

        name = item.name
        name = re.sub('\[.*\]', '', name)
        if name not in test_name:
            test_name.append(name)
            testcases.append(
                testcase_gen(name,
                             description=description,
                             parameters=param_list,
                             linked_items=work_items,
                             custom_fields=custom_fields))

    xml = etree.ElementTree(testcases)
    xml.write('test_case_import.xml', pretty_print=True)

    tests = []
    for item in items:
        name = re.sub('\[.*\]', '', item.name)
        try:
            params = item.callspec.params
            param_dict = {p: get_name(v) for p, v in params.iteritems()}
        except:
            param_dict = {}
        tests.append({'name': name, 'params': param_dict, 'result': None})
    testrun_gen(tests, 'test_run_import.xml', collectonly=True)