From efc971b65ed2922d46d446f5e6a686eb74ad9aaf Mon Sep 17 00:00:00 2001 From: ekneg54 Date: Mon, 23 Oct 2023 12:26:37 +0000 Subject: [PATCH] fix tests for processors with metrics --- logprep/abc/processor.py | 3 ++- tests/unit/component/base.py | 15 +++++++++++---- .../unit/connector/test_confluent_kafka_input.py | 4 ++-- .../unit/connector/test_confluent_kafka_output.py | 4 ++-- tests/unit/processor/amides/test_amides.py | 10 ++++++++++ .../domain_resolver/test_domain_resolver.py | 7 +++++++ .../processor/pseudonymizer/test_pseudonymizer.py | 4 ++++ 7 files changed, 38 insertions(+), 9 deletions(-) diff --git a/logprep/abc/processor.py b/logprep/abc/processor.py index 2e0dc2153..233f7f580 100644 --- a/logprep/abc/processor.py +++ b/logprep/abc/processor.py @@ -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, @@ -75,6 +74,7 @@ class Config(Component.Config): "_event", "_specific_tree", "_generic_tree", + "metrics", ] rule_class: "Rule" @@ -82,6 +82,7 @@ class Config(Component.Config): _event: dict _specific_tree: RuleTree _generic_tree: RuleTree + metrics: Component.Metrics _strategy = None def __init__(self, name: str, configuration: "Processor.Config", logger: Logger): diff --git a/tests/unit/component/base.py b/tests/unit/component/base.py index 2a5687418..458340b68 100644 --- a/tests/unit/component/base.py +++ b/tests/unit/component/base.py @@ -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 @@ -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) @@ -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) diff --git a/tests/unit/connector/test_confluent_kafka_input.py b/tests/unit/connector/test_confluent_kafka_input.py index 9cdc86465..6e17f72e4 100644 --- a/tests/unit/connector/test_confluent_kafka_input.py +++ b/tests/unit/connector/test_confluent_kafka_input.py @@ -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", @@ -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, _): diff --git a/tests/unit/connector/test_confluent_kafka_output.py b/tests/unit/connector/test_confluent_kafka_output.py index 7f9472cc6..e2579c6a8 100644 --- a/tests/unit/connector/test_confluent_kafka_output.py +++ b/tests/unit/connector/test_confluent_kafka_output.py @@ -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", @@ -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, _): diff --git a/tests/unit/processor/amides/test_amides.py b/tests/unit/processor/amides/test_amides.py index be742741b..0ee19ae3a 100644 --- a/tests/unit/processor/amides/test_amides.py +++ b/tests/unit/processor/amides/test_amides.py @@ -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 diff --git a/tests/unit/processor/domain_resolver/test_domain_resolver.py b/tests/unit/processor/domain_resolver/test_domain_resolver.py index 893b8d28c..c581249a1 100644 --- a/tests/unit/processor/domain_resolver/test_domain_resolver.py +++ b/tests/unit/processor/domain_resolver/test_domain_resolver.py @@ -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 = { diff --git a/tests/unit/processor/pseudonymizer/test_pseudonymizer.py b/tests/unit/processor/pseudonymizer/test_pseudonymizer.py index ef30d052a..28721bd72 100644 --- a/tests/unit/processor/pseudonymizer/test_pseudonymizer.py +++ b/tests/unit/processor/pseudonymizer/test_pseudonymizer.py @@ -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")