diff --git a/azure_monitor/tests/metrics/test_metrics.py b/azure_monitor/tests/metrics/test_metrics.py index 7053dba..1984c3b 100644 --- a/azure_monitor/tests/metrics/test_metrics.py +++ b/azure_monitor/tests/metrics/test_metrics.py @@ -1,6 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. import os +import shutil import unittest from unittest import mock @@ -19,6 +20,19 @@ from azure_monitor.options import ExporterOptions from azure_monitor.protocol import Data, DataPoint, Envelope, MetricData +TEST_FOLDER = os.path.abspath(".test.exporter.trace") +STORAGE_PATH = os.path.join(TEST_FOLDER) + + +# pylint: disable=invalid-name +def setUpModule(): + os.makedirs(TEST_FOLDER) + + +# pylint: disable=invalid-name +def tearDownModule(): + shutil.rmtree(TEST_FOLDER) + def throw(exc_type, *args, **kwargs): def func(*_args, **_kwargs): @@ -34,6 +48,7 @@ def setUpClass(cls): os.environ[ "APPINSIGHTS_INSTRUMENTATIONKEY" ] = "1234abcd-5678-4efa-8abc-1234567890ab" + cls._exporter = AzureMonitorMetricsExporter(storage_path=STORAGE_PATH) metrics.set_meter_provider(MeterProvider()) cls._meter = metrics.get_meter(__name__) @@ -55,6 +70,17 @@ def setUpClass(cls): kvp = {"environment": "staging"} cls._test_label_set = cls._meter.get_label_set(kvp) + def setUp(self): + for filename in os.listdir(STORAGE_PATH): + file_path = os.path.join(STORAGE_PATH, filename) + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.unlink(file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + except OSError as e: + print("Failed to delete %s. Reason: %s" % (file_path, e)) + @classmethod def tearDownClass(cls): metrics._METER_PROVIDER = None @@ -62,7 +88,8 @@ def tearDownClass(cls): def test_constructor(self): """Test the constructor.""" exporter = AzureMonitorMetricsExporter( - instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab" + instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab", + storage_path=os.path.join(TEST_FOLDER, self.id()), ) self.assertIsInstance(exporter.options, ExporterOptions) self.assertEqual( @@ -74,7 +101,7 @@ def test_export(self,): record = MetricRecord( CounterAggregator(), self._test_label_set, self._test_metric ) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter with mock.patch( "azure_monitor.export.metrics.AzureMonitorMetricsExporter._transmit" ) as transmit: # noqa: E501 @@ -86,7 +113,7 @@ def test_export_failed_retryable(self): record = MetricRecord( CounterAggregator(), self._test_label_set, self._test_metric ) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter with mock.patch( "azure_monitor.export.metrics.AzureMonitorMetricsExporter._transmit" ) as transmit: # noqa: E501 @@ -102,7 +129,7 @@ def test_export_exception(self, logger_mock): record = MetricRecord( CounterAggregator(), self._test_label_set, self._test_metric ) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter with mock.patch( "azure_monitor.export.metrics.AzureMonitorMetricsExporter._transmit", throw(Exception), @@ -112,7 +139,7 @@ def test_export_exception(self, logger_mock): self.assertEqual(logger_mock.exception.called, True) def test_metric_to_envelope_none(self): - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter self.assertIsNone(exporter._metric_to_envelope(None)) def test_metric_to_envelope(self): @@ -122,7 +149,7 @@ def test_metric_to_envelope(self): record = MetricRecord( aggregator, self._test_label_set, self._test_metric ) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter envelope = exporter._metric_to_envelope(record) self.assertIsInstance(envelope, Envelope) self.assertEqual(envelope.ver, 1) @@ -162,7 +189,7 @@ def test_observer_to_envelope(self): aggregator.update(123) aggregator.take_checkpoint() record = MetricRecord(aggregator, self._test_label_set, self._test_obs) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter envelope = exporter._metric_to_envelope(record) self.assertIsInstance(envelope, Envelope) self.assertEqual(envelope.ver, 1) @@ -205,7 +232,7 @@ def test_observer_to_envelope_value_none(self): aggregator.update(None) aggregator.take_checkpoint() record = MetricRecord(aggregator, self._test_label_set, self._test_obs) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter envelope = exporter._metric_to_envelope(record) self.assertIsInstance(envelope, Envelope) self.assertEqual(envelope.ver, 1) @@ -251,7 +278,7 @@ def test_measure_to_envelope(self, logger_mock): record = MetricRecord( aggregator, self._test_label_set, self._test_measure ) - exporter = AzureMonitorMetricsExporter() + exporter = self._exporter envelope = exporter._metric_to_envelope(record) self.assertIsInstance(envelope, Envelope) self.assertEqual(envelope.ver, 1) diff --git a/azure_monitor/tests/test_base_exporter.py b/azure_monitor/tests/test_base_exporter.py index f328759..8cbb6ab 100644 --- a/azure_monitor/tests/test_base_exporter.py +++ b/azure_monitor/tests/test_base_exporter.py @@ -19,7 +19,8 @@ from azure_monitor.options import ExporterOptions from azure_monitor.protocol import Data, Envelope -TEST_FOLDER = os.path.abspath(".test.exporter") +TEST_FOLDER = os.path.abspath(".test.exporter.base") +STORAGE_PATH = os.path.join(TEST_FOLDER) # pylint: disable=invalid-name @@ -47,6 +48,19 @@ def setUpClass(cls): os.environ[ "APPINSIGHTS_INSTRUMENTATIONKEY" ] = "1234abcd-5678-4efa-8abc-1234567890ab" + cls._base = BaseExporter(storage_path=STORAGE_PATH) + + def setUp(self): + for filename in os.listdir(STORAGE_PATH): + file_path = os.path.join(STORAGE_PATH, filename) + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.unlink(file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + except OSError as e: + print("Failed to delete %s. Reason: %s" % (file_path, e)) + self._base.clear_telemetry_processors() def test_constructor(self): """Test the constructor.""" @@ -77,19 +91,19 @@ def test_constructor_wrong_options(self): BaseExporter(something_else=6) def test_telemetry_processor_add(self): - base = BaseExporter() + base = self._base base.add_telemetry_processor(lambda: True) self.assertEqual(len(base._telemetry_processors), 1) def test_telemetry_processor_clear(self): - base = BaseExporter() + base = self._base base.add_telemetry_processor(lambda: True) self.assertEqual(len(base._telemetry_processors), 1) base.clear_telemetry_processors() self.assertEqual(len(base._telemetry_processors), 0) def test_telemetry_processor_apply(self): - base = BaseExporter() + base = self._base def callback_function(envelope): envelope.data.base_type += "_world" @@ -100,7 +114,7 @@ def callback_function(envelope): self.assertEqual(envelope.data.base_type, "type1_world") def test_telemetry_processor_apply_multiple(self): - base = BaseExporter() + base = self._base base._telemetry_processors = [] def callback_function(envelope): @@ -116,7 +130,7 @@ def callback_function2(envelope): self.assertEqual(envelope.data.base_type, "type1_world_world2") def test_telemetry_processor_apply_exception(self): - base = BaseExporter() + base = self._base def callback_function(envelope): raise ValueError() @@ -131,7 +145,7 @@ def callback_function2(envelope): self.assertEqual(envelope.data.base_type, "type1_world2") def test_telemetry_processor_apply_not_accepted(self): - base = BaseExporter() + base = self._base def callback_function(envelope): return envelope.data.base_type == "type2" @@ -177,7 +191,7 @@ def test_transmission_lease_failure(self, requests_mock): exporter._transmit_from_storage() self.assertTrue(exporter.storage.get()) - def test_transmit_response_exception(self): + def test_(self): exporter = BaseExporter( storage_path=os.path.join(TEST_FOLDER, self.id()) ) diff --git a/azure_monitor/tests/trace/test_trace.py b/azure_monitor/tests/trace/test_trace.py index 513d990..3e3f161 100644 --- a/azure_monitor/tests/trace/test_trace.py +++ b/azure_monitor/tests/trace/test_trace.py @@ -19,6 +19,7 @@ from azure_monitor.options import ExporterOptions TEST_FOLDER = os.path.abspath(".test.exporter.trace") +STORAGE_PATH = os.path.join(TEST_FOLDER) # pylint: disable=invalid-name @@ -48,29 +49,41 @@ def setUpClass(cls): os.environ[ "APPINSIGHTS_INSTRUMENTATIONKEY" ] = "1234abcd-5678-4efa-8abc-1234567890ab" + cls._exporter = AzureMonitorSpanExporter(storage_path=STORAGE_PATH) + + def setUp(self): + for filename in os.listdir(STORAGE_PATH): + file_path = os.path.join(STORAGE_PATH, filename) + try: + if os.path.isfile(file_path) or os.path.islink(file_path): + os.unlink(file_path) + elif os.path.isdir(file_path): + shutil.rmtree(file_path) + except OSError as e: + print("Failed to delete %s. Reason: %s" % (file_path, e)) def test_constructor(self): """Test the constructor.""" exporter = AzureMonitorSpanExporter( - instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab" + instrumentation_key="4321abcd-5678-4efa-8abc-1234567890ab", + storage_path=os.path.join(TEST_FOLDER, self.id()), ) self.assertIsInstance(exporter.options, ExporterOptions) self.assertEqual( exporter.options.instrumentation_key, "4321abcd-5678-4efa-8abc-1234567890ab", ) + self.assertEqual( + exporter.options.storage_path, os.path.join(TEST_FOLDER, self.id()) + ) def test_export_empty(self): - exporter = AzureMonitorSpanExporter( - storage_path=os.path.join(TEST_FOLDER, self.id()) - ) + exporter = self._exporter exporter.export([]) self.assertEqual(len(os.listdir(exporter.storage.path)), 0) def test_export_failure(self): - exporter = AzureMonitorSpanExporter( - storage_path=os.path.join(TEST_FOLDER, self.id()) - ) + exporter = self._exporter with mock.patch( "azure_monitor.export.trace.AzureMonitorSpanExporter._transmit" ) as transmit: # noqa: E501 @@ -89,9 +102,7 @@ def test_export_failure(self): self.assertIsNone(exporter.storage.get()) def test_export_success(self): - exporter = AzureMonitorSpanExporter( - storage_path=os.path.join(TEST_FOLDER, self.id()) - ) + exporter = self._exporter test_span = Span( name="test", context=SpanContext( @@ -122,9 +133,7 @@ def test_export_exception(self, logger_mock): ) test_span.start() test_span.end() - exporter = AzureMonitorSpanExporter( - storage_path=os.path.join(TEST_FOLDER, self.id()) - ) + exporter = self._exporter with mock.patch( "azure_monitor.export.trace.AzureMonitorSpanExporter._transmit", throw(Exception), @@ -134,9 +143,7 @@ def test_export_exception(self, logger_mock): self.assertEqual(logger_mock.exception.called, True) def test_export_not_retryable(self): - exporter = AzureMonitorSpanExporter( - storage_path=os.path.join(TEST_FOLDER, self.id()) - ) + exporter = self._exporter test_span = Span( name="test", context=SpanContext( @@ -154,15 +161,15 @@ def test_export_not_retryable(self): self.assertEqual(result, SpanExportResult.FAILED_NOT_RETRYABLE) def test_span_to_envelope_none(self): - exporter = AzureMonitorSpanExporter() + exporter = self._exporter self.assertIsNone(exporter._span_to_envelope(None)) # pylint: disable=too-many-statements def test_span_to_envelope(self): - options = { - "instrumentation_key": "12345678-1234-5678-abcd-12345678abcd" - } - exporter = AzureMonitorSpanExporter(**options) + exporter = AzureMonitorSpanExporter( + instrumentation_key="12345678-1234-5678-abcd-12345678abcd", + storage_path=os.path.join(TEST_FOLDER, self.id()), + ) parent_span = Span( name="test", diff --git a/tox.ini b/tox.ini index 3968186..9966fa7 100644 --- a/tox.ini +++ b/tox.ini @@ -43,7 +43,6 @@ deps = flake8 isort black - psutil commands_pre = pip install ./azure_monitor