From 6ad985052b9fd74ac7375602ce76050661aa91cf Mon Sep 17 00:00:00 2001 From: jeremydvoss Date: Tue, 10 Sep 2024 14:43:49 -0700 Subject: [PATCH] Clean up --- .../tests/test_fastapi_instrumentation.py | 54 ++----------------- 1 file changed, 4 insertions(+), 50 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py index 973ef87432..9a01908831 100644 --- a/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py +++ b/instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py @@ -1038,32 +1038,13 @@ def client_response_hook(send_span, scope, message): def get_distribution_with_fastapi(*args, **kwargs): - print("JEREVOSS get_distribution_with_fastapi") - print("args: %s" % args) - print("kwargs: %s" % kwargs) dist = args[0] - print("JEREVOSS get_distribution_with_fastapi: dist: %s" % dist) if dist == "fastapi~=0.58": return None #Value does not matter. Only whether an exception is thrown - elif "fastapi-slim" in dist: - raise DistributionNotFound() - else: - return None #TODO: may want to change to not found + raise DistributionNotFound() def get_distribution_without_fastapi(*args, **kwargs): - print("JEREVOSS get_distribution_without_fastapi") - print("args: %s" % args) - print("kwargs: %s" % kwargs) - dist = args[0] - print("JEREVOSS get_distribution_without_fastapi: dist: %s" % dist) - if dist == "fastapi~=0.58": - print("JEREVOSS get_distribution_without_fastapi IF: dist: %s" % dist) - raise DistributionNotFound() - elif "fastapi-slim" in dist: - raise DistributionNotFound() - else: - print("JEREVOSS get_distribution_without_fastapi ELSE: dist: %s" % dist) - return None #TODO: may want to change to not found + raise DistributionNotFound() class TestAutoInstrumentation(TestBaseAutoFastAPI): @@ -1084,64 +1065,37 @@ def test_entry_point_exists(self): self.assertEqual(ep.name, 'fastapi') self.assertIsNone(next(eps, None)) - # @patch("pkg_resources.get_distribution", side_effect=get_distribution_with_fastapi) @patch("opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts") @patch("opentelemetry.instrumentation.dependencies.get_dependency_conflicts") @patch("opentelemetry.instrumentation.dependencies.get_distribution") def test_instruments_with_fastapi_installed(self, mock_get_distribution, mock_get_dependency_conflicts, mock_get_dist_dependency_conflicts): - # Need to use matchers - # mock_get_distribution.side_effect = TestAutoInstrumentationget_distribution_with_fastapi - # mock_get_distribution("foobar").side_effect = DistributionNotFound() - # mock_get_distribution("fastapi-slim ~= 0.111").side_effect = DistributionNotFound() - # mock_get_distribution("fastapi ~= 0.58").return_value = None #Value does not matter. Only whether an exception is thrown mock_get_dist_dependency_conflicts.side_effect = get_dist_dependency_conflicts mock_get_dependency_conflicts.side_effect = get_dependency_conflicts mock_get_distribution.side_effect = get_distribution_with_fastapi mock_distro = Mock() _load_instrumentors(mock_distro) - print("JEREVOSS mock_get_dist_dependency_conflicts.call_args_list: %s" % mock_get_dist_dependency_conflicts.call_args_list) - print("JEREVOSS mock_get_dependency_conflicts.call_args_list: %s" % mock_get_dependency_conflicts.call_args_list) - print("JEREVOSS mock_get_distribution.call_args_list: %s" % mock_get_distribution.call_args_list) mock_get_distribution.assert_called_once_with("fastapi~=0.58") - # print("JEREVOSS mock_get_distribution().call_args_list: %s" % mock_get_distribution().call_args_list) self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 1) - call_args = mock_distro.load_instrumentor.call_args - call_args_args = call_args.args - print("JEREVOSS call_args: %s" % str(call_args)) - print(call_args) - print("JEREVOSS call_args_args: %s" % call_args_args) - ep = call_args_args[0] + args = mock_distro.load_instrumentor.call_args.args + ep = args[0] self.assertEqual(ep.dist.key, 'opentelemetry-instrumentation-fastapi') self.assertEqual(ep.module_name, 'opentelemetry.instrumentation.fastapi') self.assertEqual(ep.attrs, ('FastAPIInstrumentor',)) self.assertEqual(ep.name, 'fastapi') - # mock_distro.load_instrumentor.assert_called_once_with(self.entry_point, skip_dep_check=True) Doesn't work - # @patch("pkg_resources.get_distribution", side_effect=get_distribution_without_fastapi) @patch("opentelemetry.instrumentation.auto_instrumentation._load.get_dist_dependency_conflicts") @patch("opentelemetry.instrumentation.dependencies.get_dependency_conflicts") @patch("opentelemetry.instrumentation.dependencies.get_distribution") def test_instruments_without_fastapi_installed(self, mock_get_distribution, mock_get_dependency_conflicts, mock_get_dist_dependency_conflicts): - # mock_get_distribution.side_effect = get_distribution_without_fastapi - # mock_get_distribution("foobar").side_effect = DistributionNotFound() - # mock_get_distribution("fastapi-slim ~= 0.111").side_effect = DistributionNotFound() - # mock_get_distribution("fastapi ~= 0.58").side_effect = DistributionNotFound() mock_get_dist_dependency_conflicts.side_effect = get_dist_dependency_conflicts mock_get_dependency_conflicts.side_effect = get_dependency_conflicts mock_get_distribution.side_effect = get_distribution_without_fastapi mock_distro = Mock() _load_instrumentors(mock_distro) - print("JEREVOSS mock_get_dist_dependency_conflicts.call_args_list: %s" % mock_get_dist_dependency_conflicts.call_args_list) - print("JEREVOSS mock_get_dependency_conflicts.call_args_list: %s" % mock_get_dependency_conflicts.call_args_list) - print("JEREVOSS mock_get_distribution.call_args_list: %s" % mock_get_distribution.call_args_list) - print("JEREVOSS mock_get_distribution.side_effect: %s" % mock_get_distribution.side_effect) mock_get_distribution.assert_called_once_with("fastapi~=0.58") - print("-----\nJEREVOSS about to test error") with self.assertRaises(DistributionNotFound): mock_get_distribution("fastapi~=0.58") self.assertEqual(len(mock_distro.load_instrumentor.call_args_list), 0) - # print("JEREVOSS mock_get_distribution().call_args_list: %s" % mock_get_distribution().call_args_list) - # mock_get_distribution.assert_called_once_with("fastapi ~= 0.58") mock_distro.load_instrumentor.assert_not_called() def _create_app(self):