Skip to content

Commit 80c3296

Browse files
committed
add unit tests for _utils.py for code coverage checks
1 parent cae0630 commit 80c3296

File tree

2 files changed

+97
-1
lines changed
  • aws-opentelemetry-distro

2 files changed

+97
-1
lines changed

aws-opentelemetry-distro/src/amazon/opentelemetry/distro/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def is_installed(req: str) -> bool:
2222
_logger.debug("Skipping instrumentation patch: package %s, exception: %s", req, exc)
2323
return False
2424

25-
if not req.specifier.filter([dist_version]):
25+
if not list(req.specifier.filter([dist_version])):
2626
_logger.debug(
2727
"instrumentation for package %s is available but version %s is installed. Skipping.",
2828
req,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
import os
5+
from importlib.metadata import PackageNotFoundError
6+
from unittest import TestCase
7+
from unittest.mock import patch
8+
9+
from amazon.opentelemetry.distro._utils import AGENT_OBSERVABILITY_ENABLED, is_agent_observability_enabled, is_installed
10+
11+
12+
class TestUtils(TestCase):
13+
def setUp(self):
14+
# Store original env var if it exists
15+
self.original_env = os.environ.get(AGENT_OBSERVABILITY_ENABLED)
16+
17+
def tearDown(self):
18+
# Restore original env var
19+
if self.original_env is not None:
20+
os.environ[AGENT_OBSERVABILITY_ENABLED] = self.original_env
21+
elif AGENT_OBSERVABILITY_ENABLED in os.environ:
22+
del os.environ[AGENT_OBSERVABILITY_ENABLED]
23+
24+
def test_is_installed_package_not_found(self):
25+
"""Test is_installed returns False when package is not found"""
26+
with patch("amazon.opentelemetry.distro._utils.version") as mock_version:
27+
# Simulate package not found
28+
mock_version.side_effect = PackageNotFoundError("test-package")
29+
30+
result = is_installed("test-package>=1.0.0")
31+
self.assertFalse(result)
32+
33+
def test_is_installed(self):
34+
"""Test is_installed returns True when version matches the specifier"""
35+
with patch("amazon.opentelemetry.distro._utils.version") as mock_version:
36+
# Package is installed and version matches requirement
37+
mock_version.return_value = "2.5.0"
38+
39+
# Test with compatible version requirement
40+
result = is_installed("test-package>=2.0.0")
41+
self.assertTrue(result)
42+
43+
# Test with exact version match
44+
mock_version.return_value = "1.0.0"
45+
result = is_installed("test-package==1.0.0")
46+
self.assertTrue(result)
47+
48+
# Test with version range
49+
mock_version.return_value = "1.5.0"
50+
result = is_installed("test-package>=1.0,<2.0")
51+
self.assertTrue(result)
52+
53+
def test_is_installed_version_mismatch(self):
54+
"""Test is_installed returns False when version doesn't match"""
55+
with patch("amazon.opentelemetry.distro._utils.version") as mock_version:
56+
# Package is installed but version doesn't match requirement
57+
mock_version.return_value = "1.0.0"
58+
59+
# Test with incompatible version requirement
60+
result = is_installed("test-package>=2.0.0")
61+
self.assertFalse(result)
62+
63+
def test_is_agent_observability_enabled_various_values(self):
64+
"""Test is_agent_observability_enabled with various environment variable values"""
65+
# Test with "True" (uppercase)
66+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "True"
67+
self.assertTrue(is_agent_observability_enabled())
68+
69+
# Test with "TRUE" (all caps)
70+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "TRUE"
71+
self.assertTrue(is_agent_observability_enabled())
72+
73+
# Test with "true" (lowercase)
74+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "true"
75+
self.assertTrue(is_agent_observability_enabled())
76+
77+
# Test with "false"
78+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "false"
79+
self.assertFalse(is_agent_observability_enabled())
80+
81+
# Test with "False"
82+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "False"
83+
self.assertFalse(is_agent_observability_enabled())
84+
85+
# Test with arbitrary string
86+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "yes"
87+
self.assertFalse(is_agent_observability_enabled())
88+
89+
# Test with empty string
90+
os.environ[AGENT_OBSERVABILITY_ENABLED] = ""
91+
self.assertFalse(is_agent_observability_enabled())
92+
93+
# Test when env var is not set
94+
if AGENT_OBSERVABILITY_ENABLED in os.environ:
95+
del os.environ[AGENT_OBSERVABILITY_ENABLED]
96+
self.assertFalse(is_agent_observability_enabled())

0 commit comments

Comments
 (0)