diff --git a/tests/unit/util/test_prometheus_exporter.py b/tests/unit/util/test_prometheus_exporter.py index 6a88b8ddc..5fdf30ba7 100644 --- a/tests/unit/util/test_prometheus_exporter.py +++ b/tests/unit/util/test_prometheus_exporter.py @@ -13,8 +13,12 @@ from logprep.util.prometheus_exporter import PrometheusStatsExporter +@mock.patch( + "logprep.util.prometheus_exporter.PrometheusStatsExporter._prepare_multiprocessing", + new=lambda *args, **kwargs: None, +) class TestPrometheusStatsExporter: - def setup_method(self): + def setup_method(self, tmpdir): REGISTRY.__init__() self.metrics_config = { "metrics": {"period": 10, "enabled": True, "cumulative": True, "port": 80} @@ -40,53 +44,6 @@ def test_default_port_if_missing_in_config(self): assert exporter._port == 8000 - @mock.patch("logprep.util.prometheus_exporter.multiprocess") - def test_prepare_multiprocessing_recreates_temp_directory_if_dir_exists_already( - self, mock_multiprocess, tmp_path - ): - multi_proc_dir = tmp_path / "multi_proc_dir" - os.makedirs(multi_proc_dir) - test_file = os.path.join(multi_proc_dir, "dummy.txt") - open(test_file, "w", encoding="utf-8").close() # pylint: disable=consider-using-with - - with mock.patch.dict(os.environ, {"PROMETHEUS_MULTIPROC_DIR": str(multi_proc_dir)}): - _ = PrometheusStatsExporter(self.metrics_config, getLogger("test-logger")) - - assert os.path.isdir(multi_proc_dir) - assert not os.path.isfile(test_file) - mock_multiprocess.assert_has_calls( - [mock.call.MultiProcessCollector(REGISTRY, str(multi_proc_dir))] - ) - - @mock.patch("logprep.util.prometheus_exporter.multiprocess") - def test_prepare_multiprocessing_creates_temp_dir_if_dir_does_not_exists( - self, mock_multiprocess, tmp_path - ): - multi_proc_dir = tmp_path / "multi_proc_dir" - assert not os.path.isdir(multi_proc_dir) - - with mock.patch.dict(os.environ, {"PROMETHEUS_MULTIPROC_DIR": str(multi_proc_dir)}): - _ = PrometheusStatsExporter(self.metrics_config, getLogger("test-logger")) - - assert os.path.isdir(multi_proc_dir) - mock_multiprocess.assert_has_calls( - [mock.call.MultiProcessCollector(REGISTRY, str(multi_proc_dir))] - ) - - def test_prepare_multiprocessing_fails_if_env_variable_is_file(self, tmp_path): - multi_proc_dir = tmp_path / "multi_proc_dir" - os.makedirs(multi_proc_dir) - test_file = os.path.join(multi_proc_dir, "dummy.txt") - open(test_file, "w", encoding="utf-8").close() # pylint: disable=consider-using-with - - with mock.patch.dict(os.environ, {"PROMETHEUS_MULTIPROC_DIR": str(test_file)}): - with pytest.raises( - ValueError, - match="Environment variable 'PROMETHEUS_MULTIPROC_DIR' " - "is a file and not a directory", - ): - _ = PrometheusStatsExporter(self.metrics_config, getLogger("test-logger")) - @mock.patch("logprep.util.prometheus_exporter.start_http_server") def test_run_starts_http_server(self, mock_http_server, caplog): with caplog.at_level(logging.INFO): @@ -95,23 +52,3 @@ def test_run_starts_http_server(self, mock_http_server, caplog): mock_http_server.assert_has_calls([mock.call(exporter._port)]) assert f"Prometheus Exporter started on port {exporter._port}" in caplog.text - - def test_remove_metrics_from_process_removes_database_file_of_given_pid(self, tmp_path, caplog): - # pylint: disable=consider-using-with - multi_proc_dir = tmp_path / "multi_proc_dir" - os.makedirs(multi_proc_dir) - - with mock.patch.dict(os.environ, {"PROMETHEUS_MULTIPROC_DIR": str(multi_proc_dir)}): - with caplog.at_level(logging.DEBUG): - exporter = PrometheusStatsExporter(self.metrics_config, getLogger("test-logger")) - metric_file_5416 = multi_proc_dir / "gauge_5416.db" - metric_file_8742 = multi_proc_dir / "gauge_8742.db" - open(metric_file_5416, "a", encoding="utf-8").close() - open(metric_file_8742, "a", encoding="utf-8").close() - - exporter.remove_metrics_from_process(pid=5416) - - assert not os.path.exists(metric_file_5416) - assert os.path.exists(metric_file_8742) - assert os.path.isfile(metric_file_8742) - assert "Removed stale metric files: ['gauge_5416.db']" in caplog.text