Skip to content

Commit

Permalink
fix tests for processors with metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 23, 2023
1 parent 54a9995 commit efc971b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
3 changes: 2 additions & 1 deletion logprep/abc/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from logprep.abc.component import Component
from logprep.framework.rule_tree.rule_tree import RuleTree, RuleTreeType
from logprep.metrics.metrics import HistogramMetric
from logprep.processor.base.exceptions import (
FieldExistsWarning,
ProcessingCriticalError,
Expand Down Expand Up @@ -75,13 +74,15 @@ class Config(Component.Config):
"_event",
"_specific_tree",
"_generic_tree",
"metrics",
]

rule_class: "Rule"
has_custom_tests: bool
_event: dict
_specific_tree: RuleTree
_generic_tree: RuleTree
metrics: Component.Metrics
_strategy = None

def __init__(self, name: str, configuration: "Processor.Config", logger: Logger):
Expand Down
15 changes: 11 additions & 4 deletions tests/unit/component/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Callable, Iterable
from unittest import mock

import pytest
from attrs import asdict
from prometheus_client import Counter, Gauge, Histogram

Expand Down Expand Up @@ -89,13 +88,21 @@ def test_custom_metrics_adds_custom_prefix_to_metrics_name(self):

def test_expected_metrics_attributes(self):
for expected_metric in self.expected_metrics:
metric_attribute = getattr(self.object.metrics, expected_metric)
metric_name = expected_metric.replace(
f"logprep_{camel_to_snake(self.object.__class__.__name__)}_", ""
)
metric_name = metric_name.replace("logprep_", "")
metric_attribute = getattr(self.object.metrics, metric_name)
assert metric_attribute is not None
assert isinstance(metric_attribute, Metric)

def test_expected_metrics_attributes_are_initialized(self):
for expected_metric in self.expected_metrics:
metric_attribute = getattr(self.object.metrics, expected_metric)
metric_name = expected_metric.replace(
f"logprep_{camel_to_snake(self.object.__class__.__name__)}_", ""
)
metric_name = metric_name.replace("logprep_", "")
metric_attribute = getattr(self.object.metrics, metric_name)
assert metric_attribute.tracker is not None
possibile_tracker_types = (Counter, Gauge, Histogram)
assert isinstance(metric_attribute.tracker, possibile_tracker_types)
Expand All @@ -106,5 +113,5 @@ def test_all_metric_attributes_are_tested(self):
assert self.expected_metrics, "expected_metrics is empty"
fullnames = {metric.fullname for metric in self.metric_attributes.values()}
difference = fullnames.difference(set(self.expected_metrics))
assert fullnames == set(self.expected_metrics)
assert not difference, f"{difference} are not defined in `expected_metrics`"
assert fullnames == set(self.expected_metrics)
4 changes: 2 additions & 2 deletions tests/unit/connector/test_confluent_kafka_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TestConfluentKafkaInput(BaseInputTestCase, CommonConfluentKafkaTestCase):
"topic": "test_input_raw",
}

expected_metrics = {
expected_metrics = [
"logprep_confluent_kafka_input_commit_failures",
"logprep_confluent_kafka_input_commit_success",
"logprep_confluent_kafka_input_current_offsets",
Expand All @@ -55,7 +55,7 @@ class TestConfluentKafkaInput(BaseInputTestCase, CommonConfluentKafkaTestCase):
"logprep_number_of_failed_events",
"logprep_number_of_warnings",
"logprep_number_of_errors",
}
]

@mock.patch("logprep.connector.confluent_kafka.input.Consumer")
def test_get_next_returns_none_if_no_records(self, _):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/connector/test_confluent_kafka_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TestConfluentKafkaOutput(BaseOutputTestCase, CommonConfluentKafkaTestCase)
},
}

expected_metrics = {
expected_metrics = [
"logprep_confluent_kafka_output_librdkafka_age",
"logprep_confluent_kafka_output_librdkafka_msg_cnt",
"logprep_confluent_kafka_output_librdkafka_msg_size",
Expand All @@ -54,7 +54,7 @@ class TestConfluentKafkaOutput(BaseOutputTestCase, CommonConfluentKafkaTestCase)
"logprep_number_of_failed_events",
"logprep_number_of_warnings",
"logprep_number_of_errors",
}
]

@mock.patch("logprep.connector.confluent_kafka.output.Producer", return_value="The Producer")
def test_producer_property_instanciates_kafka_producer(self, _):
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/processor/amides/test_amides.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class TestAmides(BaseProcessorTestCase):
"num_rule_attributions": 10,
}

expected_metrics = [
"logprep_amides_total_cmdlines",
"logprep_amides_new_results",
"logprep_amides_cached_results",
"logprep_amides_num_cache_entries",
"logprep_amides_cache_load",
"logprep_amides_mean_misuse_detection_time",
"logprep_amides_mean_rule_attribution_time",
]

def test_process_event_malicious_process_command_line(self):
self.object.metrics.total_cmdlines = 0
self.object.metrics.new_results = 0
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/processor/domain_resolver/test_domain_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class TestDomainResolver(BaseProcessorTestCase):
"tree_config": "tests/testdata/unit/shared_data/tree_config.json",
}

expected_metrics = [
"logprep_domain_resolver_total_urls",
"logprep_domain_resolver_resolved_new",
"logprep_domain_resolver_resolved_cached",
"logprep_domain_resolver_timeouts",
]

@mock.patch("socket.gethostbyname", return_value="1.2.3.4")
def test_domain_to_ip_resolved_and_added(self, mock_gethostbyname):
rule = {
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/processor/pseudonymizer/test_pseudonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class TestPseudonymizer(BaseProcessorTestCase):
"max_caching_days": 1,
}

expected_metrics = [
"logprep_pseudonymizer_pseudonymized_urls",
]

def setup_method(self) -> None:
super().setup_method()
self.regex_mapping = self.CONFIG.get("regex_mapping")
Expand Down

0 comments on commit efc971b

Please sign in to comment.