Exemplo n.º 1
0
def test_matching_returns_things_once_if_multimatch(node_scenario):
    a, b = dummy_object(), dummy_object()
    a.attr = "a - this should match"
    b.attr = "b - this won't"
    node_scenario.schema = {
        "match": [
            {
                "property": {
                    "name": "attr",
                    "value": "a.*"
                }
            },
            {
                "property": {
                    "name": "attr",
                    "value": ".*"
                }
            },
        ]
    }
    node_scenario.inventory.find_nodes = MagicMock(return_value=[a, b])
    matched = node_scenario.match()
    assert len(matched) == 2
    assert a in matched
    assert b in matched
Exemplo n.º 2
0
def test_filter_day_time_of_day(noop_scenario, some_datetime, hour, minute,
                                second, should_pass):
    some_datetime = some_datetime.replace(
        hour=hour,
        minute=minute,
        second=second,
    )
    print("new datetime", some_datetime)
    criterion = {
        "onlyDays": [
            "friday",
        ],
        "startTime": {
            "hour": 9,
            "minute": 0,
            "second": 0
        },
        "endTime": {
            "hour": 17,
            "minute": 59,
            "second": 15
        },
    }
    candidates = [dummy_object(), dummy_object()]
    if should_pass:
        assert noop_scenario.filter_day_time(candidates,
                                             criterion,
                                             now=some_datetime) == candidates
    else:
        assert noop_scenario.filter_day_time(candidates,
                                             criterion,
                                             now=some_datetime) == []
Exemplo n.º 3
0
def test_filter_day_time_day_of_the_week(noop_scenario, some_datetime, day,
                                         should_pass):
    some_datetime = some_datetime.replace(day=day)
    print("new datetime", some_datetime)
    criterion = {
        "onlyDays": [
            "friday",
        ],
        "startTime": {
            "hour": 0,
            "minute": 0,
            "second": 0
        },
        "endTime": {
            "hour": 23,
            "minute": 59,
            "second": 59
        },
    }
    candidates = [dummy_object(), dummy_object()]
    if should_pass:
        assert noop_scenario.filter_day_time(candidates,
                                             criterion,
                                             now=some_datetime) == candidates
    else:
        assert noop_scenario.filter_day_time(candidates,
                                             criterion,
                                             now=some_datetime) == []
Exemplo n.º 4
0
def test_filter_property(noop_scenario):
    ATTR_NAME = "test"
    dummy1 = dummy_object()
    setattr(dummy1, ATTR_NAME, "yes")
    dummy2 = dummy_object()
    setattr(dummy2, ATTR_NAME, "no")
    criterion = {"name": ATTR_NAME, "value": getattr(dummy2, ATTR_NAME)}
    assert noop_scenario.filter_property([dummy1, dummy2],
                                         criterion) == [dummy2]
Exemplo n.º 5
0
def test_filter_probability(noop_scenario, proba):
    random.seed(7)  # make the tests deterministic
    SAMPLES = 100000
    candidates = [dummy_object(), dummy_object()]
    agg_len = 0.0
    criterion = {"probabilityPassAll": proba}
    for _ in range(SAMPLES):
        agg_len += len(noop_scenario.filter_probability(candidates, criterion))
    assert (agg_len / len(candidates)) / SAMPLES == pytest.approx(proba, 0.01)
Exemplo n.º 6
0
def test_acts_mapping_only_waits_once(noop_scenario):
    items = [dummy_object(), dummy_object()]
    mapping = {
        "wait": MagicMock(return_value=[]),
    }
    actions = [
        {
            "wait": {},
        },
    ]
    noop_scenario.act_mapping(items, actions, mapping)
    assert mapping.get("wait").call_count == 1
Exemplo n.º 7
0
def test_matching_namespace(pod_scenario):
    a, b = dummy_object(), dummy_object()
    pod_scenario.schema = {
        "match": [
            {
                "namespace": {
                    "name": "something",
                }
            },
        ]
    }
    pod_scenario.k8s_inventory.find_pods = MagicMock(return_value=[a, b])
    matched = pod_scenario.match()
    assert matched == list(set([a, b]))
    assert pod_scenario.k8s_inventory.find_pods.call_args[1] == {"namespace": "something"}
Exemplo n.º 8
0
def test_random_sample_size(noop_scenario):
    candidates = [dummy_object() for x in range(100)]
    for x in range(len(candidates) + 1):
        sample = noop_scenario.filter_random_sample(candidates, {"size": x})
        assert len(sample) == x
        for elem in sample:
            assert elem in candidates
Exemplo n.º 9
0
def test_matching_matches(node_scenario):
    a, b = dummy_object(), dummy_object()
    a.attr = "a - this should match"
    b.attr = "b - this won't"
    node_scenario.schema = {
        "match": [
            {
                "property": {
                    "name": "attr",
                    "value": "a.*"
                }
            },
        ]
    }
    node_scenario.inventory.find_nodes = MagicMock(return_value=[a, b])
    matched = node_scenario.match()
    assert matched == [a]
Exemplo n.º 10
0
def test_random_sample_percentage(noop_scenario):
    candidates = [dummy_object() for x in range(100)]
    for x in range(101):
        percentage = x / 100.00
        sample = noop_scenario.filter_random_sample(candidates,
                                                    {"ratio": percentage})
        assert len(sample) == int(percentage * len(candidates))
        for elem in sample:
            assert elem in candidates
def test_add_probability_filter_passed_no_nodes_metric(
        prometheus_noop_scenario):
    """
    Ensures that add_probability_filter_passed_no_nodes_metric is called when
    the filter decides to pass no nodes based on a probability
    """
    assert prometheus_noop_scenario.name == "test scenario"
    random.seed(6)  # make the tests deterministic
    candidates = [dummy_object()]

    before = REGISTRY.get_sample_value(
        PROBABILITY_FILTER_NOT_PASSED_METRIC_NAME)
    assert before == 0

    criterion = {"probabilityPassAll": 0.00000001}
    prometheus_noop_scenario.filter_probability(candidates, criterion)

    after = REGISTRY.get_sample_value(
        PROBABILITY_FILTER_NOT_PASSED_METRIC_NAME)
    assert after == 1
Exemplo n.º 12
0
def test_add_probability_filter_passed_no_nodes_metric(noop_scenario):
    """
    Ensures that add_probability_filter_passed_no_nodes_metric is called when
    the filter decides to pass no nodes based on a probability
    """
    assert noop_scenario.name == "test scenario"
    random.seed(6)  # make the tests deterministic
    candidates = [dummy_object()]

    with mock.patch('powerfulseal.metriccollectors.StdoutCollector.add_probability_filter_passed_no_nodes_filter') \
            as metric_function:
        # Ensure metric is not added when nodes are all passed
        criterion = {"probabilityPassAll": 1}
        noop_scenario.filter_probability(candidates, criterion)
        metric_function.assert_not_called()

        # Ensure metric is added when nodes are not passed
        criterion = {"probabilityPassAll": 0.00000001}
        noop_scenario.filter_probability(candidates, criterion)
        metric_function.assert_called()
Exemplo n.º 13
0
def test_filter_mapping_uses_the_mapping(noop_scenario):
    dummy = dummy_object()
    items = [dummy]
    mapping = {
        "filterA": MagicMock(return_value=items),
        "filterB": MagicMock(return_value=[]),
    }
    filters = [
        {
            "filterA": {},
        },
        {
            "filterB": {},
        },
    ]
    res = noop_scenario.filter_mapping(items, filters, mapping)
    assert res == []
    assert mapping.get("filterA").call_count == 1
    assert mapping.get("filterA").call_args[0] == (items, {})
    assert mapping.get("filterB").call_count == 1
    assert mapping.get("filterB").call_args[0] == (items, {})
Exemplo n.º 14
0
def test_random_doesnt_pass_on_empty_criterion(noop_scenario):
    candidates = [dummy_object() for x in range(100)]
    for x in range(101):
        sample = noop_scenario.filter_random_sample(candidates, None)
        assert len(sample) == 0