Ejemplo n.º 1
0
def test_route_scoped_groups_without_report_route_id(kong_state):
    config = plugin_config(report_route_id=False)
    grouper = Grouper(kong_state, config)
    distinct, indistinct = grouper.get_route_scoped_groups()
    assert distinct == []
    assert indistinct == set.union(
        *[g for _id, g in kong_state.route_ids.items() if _id])
Ejemplo n.º 2
0
def test_scoped_groups_with_report_id_and_with_name(kong_state_from_file,
                                                    resource_type):
    kong_state = kong_state_from_file('status_with_renames.json')
    config = plugin_config(resource_type, True, True)
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    assert indistinct == set()
    num_groups = len(getattr(
        kong_state,
        '{0}_ids'.format(resource_type)))  # Account for added name and id
    assert len(distinct) == num_groups
    encountered_names = set()
    encountered_ids = set()
    for group in distinct:
        name = None
        _id = None
        for member in group:
            resource = kong_state.resource_metrics[member]
            if name is None:
                name = resource['{0}_name'.format(resource_type)]
                _id = resource['{0}_id'.format(resource_type)]
                continue
            assert resource['{0}_name'.format(resource_type)] == name
            assert resource['{0}_id'.format(resource_type)] == _id
        assert not (name in encountered_names and _id in encountered_ids)
        encountered_names.add(name)
        encountered_ids.add(_id)
Ejemplo n.º 3
0
def test_calculate_status_code_scope_metrics_with_partial_scoping(
        reporter, metric):
    reporter.config = plugin_config(report_api_id=False,
                                    report_api_name=False,
                                    report_service_id=True,
                                    report_service_name=True,
                                    report_route_id=True,
                                    report_http_method=True,
                                    report_status_code=True)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    metric_dimensions = [m.dimensions for m in metrics]
    all_dimensions = []
    for ctx_hash, ctx in reporter.kong_state.resource_metrics.items():
        if ctx['api_id']:
            continue
        if not ctx['api_id'] and not ctx['service_id']:
            continue  # unscoped metrics will roll up with api metrics
        for sc in ctx['status_codes']:
            all_dimensions.append(to_metric_dimensions(ctx, sc))
    api_member_dimensions = []
    for api_id in [_id for _id in reporter.kong_state.api_ids if _id]:
        for ctx_hash in reporter.kong_state.api_ids[api_id]:
            ctx = reporter.kong_state.resource_metrics[ctx_hash]
            for sc in ctx['status_codes']:
                dimensions = dict(http_method=ctx['http_method'],
                                  status_code=sc)
                if dimensions not in api_member_dimensions:
                    api_member_dimensions.append(dimensions)
    all_dimensions.extend(api_member_dimensions)
    for dim in metric_dimensions:
        assert dim in all_dimensions
    for dim in all_dimensions:
        assert dim in metric_dimensions
    assert len(metric_dimensions) == len(all_dimensions)
Ejemplo n.º 4
0
def test_calculate_status_code_scope_metrics_with_partial_blacklist(
        reporter, metric):
    blacklist = ['200', '201', '202', '203']
    reporter.config = plugin_config(resource_types=['api', 'service'],
                                    report_id=True,
                                    report_name=True,
                                    report_route_id=True,
                                    report_http_method=True,
                                    report_status_code=True,
                                    status_code_blacklist=blacklist)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    metric_dimensions = [m.dimensions for m in metrics]
    all_dimensions = []
    for ctx_hash, ctx in reporter.kong_state.resource_metrics.items():
        for sc in ctx['status_codes']:
            if sc in blacklist:
                sc = None
            dimensions = to_metric_dimensions(ctx, sc)
            if dimensions not in all_dimensions:
                all_dimensions.append(dimensions)
    for dim in metric_dimensions:
        assert dim in all_dimensions
    for dim in all_dimensions:
        assert dim in metric_dimensions
    assert len(metric_dimensions) == len(all_dimensions)
Ejemplo n.º 5
0
def test_scoped_groups_without_report_id_and_without_name(
        kong_state, resource_type):
    config = plugin_config(resource_type, False, False)
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    assert distinct == []
    id_store = getattr(kong_state, '{0}_ids'.format(resource_type))
    assert indistinct == set.union(*[g for _id, g in id_store.items() if _id])
Ejemplo n.º 6
0
def test_http_scoped_groups_with_report_http_and_without_whitelist_and_blacklist(
        kong_state):
    config = plugin_config(report_http_method=True)
    grouper = Grouper(kong_state, config)
    groups = grouper.get_http_method_scoped_groups()
    for group in groups:
        assert group
    for method_group in kong_state.http_methods.values():
        assert method_group in groups
Ejemplo n.º 7
0
def test_http_scoped_groups_with_report_http_and_with_whitelist_and_blacklist(
        kong_state):
    methods = ['HEAD', 'OPTIONS']
    config = plugin_config(report_http_method=True,
                           http_whitelist=methods,
                           http_blacklist=methods)
    grouper = Grouper(kong_state, config)
    groups = grouper.get_http_method_scoped_groups()
    assert len(groups) == 1
    assert groups.pop() == set.union(*kong_state.http_methods.values())
Ejemplo n.º 8
0
def test_route_scoped_groups_with_report_route_and_with_whitelist_and_blacklist(
        kong_state):
    undesired_id = [_id for _id in kong_state.route_ids if _id].pop()
    config = plugin_config(report_route_id=True,
                           route_id_whitelist=[undesired_id],
                           route_id_blacklist=[undesired_id])
    grouper = Grouper(kong_state, config)
    distinct, indistinct = grouper.get_route_scoped_groups()
    assert distinct == []
    assert indistinct == set.union(
        *[g for _id, g in kong_state.route_ids.items() if _id])
Ejemplo n.º 9
0
def test_http_scoped_groups_with_report_http_and_without_whitelist_and_with_blacklist(
        kong_state):
    methods = ['HEAD', 'OPTIONS']
    config = plugin_config(report_http_method=True, http_blacklist=methods)
    grouper = Grouper(kong_state, config)
    groups = grouper.get_http_method_scoped_groups()
    assert len(groups) == 6
    method_groups = kong_state.http_methods
    for group in [g for m, g in method_groups.items() if m not in methods]:
        assert group in groups[:5]
    assert groups[5] == set.union(
        *[g for m, g in method_groups.items() if m in methods])
Ejemplo n.º 10
0
def test_calculate_http_scope_metrics_without_scoping(reporter, metric):
    reporter.config = plugin_config(report_id=False,
                                    report_name=False,
                                    report_route_id=False,
                                    report_http_method=False,
                                    report_status_code=False)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_http_method_scope_metrics(metric)
    assert len(metrics) == 1
    calculated = metrics.pop()
    expected = sum(
        [v[metric] for v in reporter.kong_state.resource_metrics.values()])
    assert calculated.value == expected
    assert calculated.dimensions == {}
Ejemplo n.º 11
0
def test_route_scoped_groups_with_report_route_id_and_without_whitelist_and_blacklist(
        kong_state):
    config = plugin_config(report_route_id=True)
    grouper = Grouper(kong_state, config)
    distinct, indistinct = grouper.get_route_scoped_groups()
    assert indistinct == set()
    for group in distinct:
        assert group
    id_groups = [g for _id, g in kong_state.route_ids.items() if _id]
    for id_group in id_groups:
        assert id_group in distinct
    for group in distinct:
        assert group in id_groups
    assert len(distinct) == len(id_groups)
Ejemplo n.º 12
0
def test_partially_scoped_groups_with_blacklists(kong_state):
    metrics = kong_state.resource_metrics
    undesired_api_name = [name for name in kong_state.api_names if name].pop()
    undesired_api_id = metrics[next(
        iter(kong_state.api_names[undesired_api_name]))]['api_id']
    undesired_service_name = [
        name for name in kong_state.service_names if name
    ].pop()
    undesired_service_id = metrics[next(
        iter(kong_state.service_names[undesired_service_name]))]['service_id']
    undesired_route_id = metrics[next(
        iter(kong_state.service_names[undesired_service_name]))]['route_id']

    indistinct = kong_state.api_ids[undesired_api_id] | kong_state.route_ids[
        undesired_route_id]
    indistinct.update(kong_state.api_ids[None]
                      & kong_state.service_ids[None])  # add unscoped requests
    indistinct_http = defaultdict(set)
    for http_method in kong_state.http_methods:
        indistinct_http[
            http_method] = kong_state.http_methods[http_method] & indistinct

    desired_groups = [
        set((ctx_hash, )) for ctx_hash in metrics if ctx_hash not in indistinct
    ]
    config = plugin_config(report_api_id=True,
                           report_api_name=True,
                           api_id_blacklist=[undesired_api_id],
                           api_name_blacklist=[undesired_api_name],
                           report_service_id=True,
                           report_service_name=True,
                           service_id_blacklist=[undesired_service_id],
                           service_name_blacklist=[undesired_service_name],
                           report_route_id=True,
                           route_id_blacklist=[undesired_route_id],
                           report_http_method=True)
    grouper = Grouper(kong_state, config)
    groups = grouper.get_http_method_scoped_groups()
    cutoff = len(indistinct_http)
    for group in desired_groups:
        assert group in groups[:-cutoff]
    for group in groups[:-cutoff]:
        assert group in desired_groups
    assert len(groups) == len(desired_groups) + cutoff
    for group in indistinct_http.values():
        assert group in groups[-cutoff:]
    for group in groups[-cutoff:]:
        assert group in indistinct_http.values()
Ejemplo n.º 13
0
def test_scoping_on_empty_kong_state(kong_state_from_file, resource_type):
    kong_state = kong_state_from_file('status_empty.json')
    grouper = Grouper(
        kong_state,
        plugin_config(resource_types=['api', 'service'],
                      report_id=True,
                      report_name=True,
                      report_route_id=True,
                      report_http_method=True))
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    groups = method()
    if resource_type == 'http_method':
        assert groups == []
    else:
        assert groups[0] == []
        assert groups[1] == set()
Ejemplo n.º 14
0
def test_fully_scoped_groups(kong_state):
    config = plugin_config(resource_types=['api', 'service'],
                           report_id=True,
                           report_name=True,
                           report_route_id=True,
                           report_http_method=True)
    grouper = Grouper(kong_state, config)
    groups = grouper.get_http_method_scoped_groups()
    desired_groups = [
        set((ctx_hash, )) for ctx_hash in kong_state.resource_metrics
    ]
    for group in desired_groups:
        assert group in groups
    for group in groups:
        assert group in desired_groups
    assert len(groups) == len(desired_groups)
Ejemplo n.º 15
0
def test_calculate_status_code_scope_metrics_with_no_scoping_and_status_code_groups(
        reporter, metric):
    reporter.config = plugin_config(report_status_code_group=True)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    metric_dimensions = [m.dimensions for m in metrics]
    all_dimensions = []
    for sc in reporter.kong_state.status_codes:
        sc = '{0}xx'.format(sc[0])
        dimension = to_metric_dimensions({}, sc)
        if dimension not in all_dimensions:
            all_dimensions.append(dimension)
    for dim in metric_dimensions:
        assert dim in all_dimensions
    for dim in all_dimensions:
        assert dim in metric_dimensions
    assert len(metric_dimensions) == len(all_dimensions)
Ejemplo n.º 16
0
def test_route_scoped_groups_with_report_route_and_without_whitelist_and_with_blacklist(
        kong_state):
    undesired_id = [_id for _id in kong_state.route_ids if _id].pop()
    config = plugin_config(report_route_id=True,
                           route_id_blacklist=[undesired_id])
    grouper = Grouper(kong_state, config)
    distinct, indistinct = grouper.get_route_scoped_groups()
    assert indistinct == kong_state.route_ids[undesired_id]
    id_groups = [
        g for _id, g in kong_state.route_ids.items()
        if _id not in (undesired_id, None)
    ]
    for group in id_groups:
        assert group in distinct
    for group in distinct:
        assert group in id_groups
    assert len(distinct) == len(id_groups)
Ejemplo n.º 17
0
def test_scoped_groups_without_report_id_and_with_name_whitelist_and_blacklist(
        kong_state, resource_type):
    name_store = getattr(kong_state, '{0}_names'.format(resource_type))
    desired_name = [name for name in name_store if name is not None].pop()
    config = plugin_config(resource_type,
                           False,
                           False,
                           name_whitelist=[desired_name],
                           name_blacklist=[desired_name])
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    assert distinct == []
    id_store = getattr(kong_state, '{0}_ids'.format(
        resource_type))  # Use IDs since not all resources have names
    resource_ids = [_id for _id in id_store if _id is not None]
    all_resources = set.union(*[id_store[_id] for _id in resource_ids])
    assert indistinct == all_resources
Ejemplo n.º 18
0
def test_scoped_groups_without_report_id_and_with_name_whitelist_and_no_blacklist(
        kong_state, resource_type):
    desired_name = [
        name for name in getattr(kong_state, '{0}_names'.format(resource_type))
        if name is not None
    ].pop()
    config = plugin_config(resource_type,
                           False,
                           False,
                           name_whitelist=[desired_name])
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    assert len(distinct) == 1
    for ctx_hash in distinct[0]:
        assert kong_state.resource_metrics[ctx_hash]['{0}_name'.format(
            resource_type)] == desired_name
    assert indistinct - distinct[0] == indistinct
Ejemplo n.º 19
0
def test_calculate_http_scope_metrics_with_http_scoping(reporter, metric):
    reporter.config = plugin_config(report_id=False,
                                    report_name=False,
                                    report_route_id=False,
                                    report_http_method=True,
                                    report_status_code=False)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_http_method_scope_metrics(metric)
    assert set([m.dimensions['http_method']
                for m in metrics]) == set(reporter.kong_state.http_methods)
    for calculated in metrics:
        http_method = calculated.dimensions['http_method']
        group_members = reporter.kong_state.http_methods[http_method]
        expected = sum([
            reporter.kong_state.resource_metrics[ctx_hash][metric]
            for ctx_hash in group_members
        ])
        assert calculated.value == expected
        assert calculated.dimensions == dict(http_method=http_method)
Ejemplo n.º 20
0
def test_scoped_groups_with_report_id_whitelist_and_blacklist_and_without_name(
        kong_state, resource_type):
    desired_id = [
        _id for _id in getattr(kong_state, '{0}_ids'.format(resource_type))
        if _id is not None
    ].pop()
    config = plugin_config(resource_type,
                           False,
                           False,
                           id_whitelist=[desired_id],
                           id_blacklist=[desired_id])
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    assert distinct == []
    id_store = getattr(kong_state, '{0}_ids'.format(resource_type))
    resource_ids = [_id for _id in id_store if _id is not None]
    all_resources = set.union(*[id_store[_id] for _id in resource_ids])
    assert indistinct == all_resources
Ejemplo n.º 21
0
def test_calculate_status_code_scope_metrics_with_full_scoping(
        reporter, metric):
    reporter.config = plugin_config(resource_types=['api', 'service'],
                                    report_id=True,
                                    report_name=True,
                                    report_route_id=True,
                                    report_http_method=True,
                                    report_status_code=True)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    metric_dimensions = [m.dimensions for m in metrics]
    all_dimensions = []
    for ctx_hash, ctx in reporter.kong_state.resource_metrics.items():
        for sc in ctx['status_codes']:
            all_dimensions.append(to_metric_dimensions(ctx, sc))
    for dim in metric_dimensions:
        assert dim in all_dimensions
    for dim in all_dimensions:
        assert dim in metric_dimensions
    assert len(metric_dimensions) == len(all_dimensions)
Ejemplo n.º 22
0
def test_scoped_groups_with_report_id_and_without_name(kong_state,
                                                       resource_type):
    config = plugin_config(resource_type, True, False)
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    assert indistinct == set()
    id_store = getattr(kong_state, '{0}_ids'.format(resource_type))
    num_ids = len(id_store) - 1  # account for None
    assert len(distinct) == num_ids
    id_owner_hashes = [next(iter(g)) for g in distinct]
    ids = [
        kong_state.resource_metrics[ctx_hash]['{0}_id'.format(resource_type)]
        for ctx_hash in id_owner_hashes
    ]
    id_groups = [
        getattr(kong_state, '{0}_ids'.format(resource_type))[_id]
        for _id in ids
    ]
    assert distinct == id_groups
Ejemplo n.º 23
0
def test_calculate_status_code_scope_metrics_with_full_blacklist(
        reporter, metric):
    reporter.config = plugin_config(report_api_id=True,
                                    report_api_name=True,
                                    report_service_id=True,
                                    report_service_name=True,
                                    report_route_id=True,
                                    report_http_method=True,
                                    report_status_code=True,
                                    status_code_blacklist=['*'])
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    metric_dimensions = [m.dimensions for m in metrics]
    all_dimensions = []
    for ctx_hash, ctx in reporter.kong_state.resource_metrics.items():
        all_dimensions.append(to_metric_dimensions(ctx))
    for dim in metric_dimensions:
        assert dim in all_dimensions
    for dim in all_dimensions:
        assert dim in metric_dimensions
    assert len(metric_dimensions) == len(all_dimensions)
Ejemplo n.º 24
0
def test_calculate_status_code_scope_metrics_with_status_code_scoping(
        reporter, metric):
    reporter.config = plugin_config(report_id=False,
                                    report_name=False,
                                    report_route_id=False,
                                    report_http_method=False,
                                    report_status_code=True)
    reporter.update_http_method_scope_groups()
    metrics = reporter.calculate_status_code_scope_metrics(metric)
    assert set([m.dimensions['status_code']
                for m in metrics]) == set(reporter.kong_state.status_codes)
    expected = []
    for calculated in metrics:
        status_code = calculated.dimensions['status_code']
        group_members = reporter.kong_state.status_codes[status_code]
        exp = sum([
            reporter.kong_state.resource_metrics[ctx_hash]['status_codes'].get(
                status_code, {metric: 0})[metric] for ctx_hash in group_members
        ])
        expected.append(exp)
        assert calculated.dimensions == dict(status_code=status_code)
    assert [m.value for m in metrics] == expected
Ejemplo n.º 25
0
def test_scoped_groups_without_report_id_and_with_name(kong_state,
                                                       resource_type):
    config = plugin_config(resource_type, False, True)
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    num_names = len(getattr(kong_state, '{0}_names'.format(resource_type))) - 1
    assert len(distinct) == num_names
    name_owner_hashes = [next(iter(g)) for g in distinct]
    names = [
        kong_state.resource_metrics[ctx_hash]['{0}_name'.format(resource_type)]
        for ctx_hash in name_owner_hashes
    ]
    name_groups = [
        getattr(kong_state, '{0}_names'.format(resource_type))[name]
        for name in names
    ]
    assert distinct == name_groups
    name_owners = set.union(*name_groups)
    members = set.union(*[
        g for _id, g in getattr(kong_state, '{0}_ids'.format(
            resource_type)).items() if _id is not None
    ])
    assert indistinct == members - name_owners
Ejemplo n.º 26
0
def test_scoped_groups_without_report_id_and_with_name_without_whitelist_and_with_blacklist(
        kong_state, resource_type):
    undesired_name = [
        name for name in getattr(kong_state, '{0}_names'.format(resource_type))
        if name is not None
    ].pop()
    config = plugin_config(resource_type,
                           False,
                           True,
                           name_whitelist=[],
                           name_blacklist=[undesired_name])
    grouper = Grouper(kong_state, config)
    method = getattr(grouper, 'get_{0}_scoped_groups'.format(resource_type))
    distinct, indistinct = method()
    name_store = getattr(kong_state, '{0}_names'.format(resource_type))
    resource_names = [name for name in name_store if name is not None]
    assert distinct == [
        name_store[name] for name in resource_names if name != undesired_name
    ]
    id_store = getattr(kong_state, '{0}_ids'.format(resource_type))
    resource_ids = [_id for _id in id_store if _id is not None]
    all_resources = set.union(*[id_store[_id] for _id in resource_ids])
    assert indistinct == name_store[undesired_name] | (name_store[None]
                                                       & all_resources)
Ejemplo n.º 27
0
def test_http_scoped_groups_without_report_http(kong_state):
    config = plugin_config(report_http_method=False)
    grouper = Grouper(kong_state, config)
    groups = grouper.get_http_method_scoped_groups()
    assert len(groups) == 1
    assert groups.pop() == set.union(*kong_state.http_methods.values())