Skip to content

Commit babfd66

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

File tree

1 file changed

+86
-0
lines changed
  • aws-opentelemetry-distro/tests/amazon/opentelemetry/distro

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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_agent_observability_enabled_various_values(self):
54+
"""Test is_agent_observability_enabled with various environment variable values"""
55+
# Test with "True" (uppercase)
56+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "True"
57+
self.assertTrue(is_agent_observability_enabled())
58+
59+
# Test with "TRUE" (all caps)
60+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "TRUE"
61+
self.assertTrue(is_agent_observability_enabled())
62+
63+
# Test with "true" (lowercase)
64+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "true"
65+
self.assertTrue(is_agent_observability_enabled())
66+
67+
# Test with "false"
68+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "false"
69+
self.assertFalse(is_agent_observability_enabled())
70+
71+
# Test with "False"
72+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "False"
73+
self.assertFalse(is_agent_observability_enabled())
74+
75+
# Test with arbitrary string
76+
os.environ[AGENT_OBSERVABILITY_ENABLED] = "yes"
77+
self.assertFalse(is_agent_observability_enabled())
78+
79+
# Test with empty string
80+
os.environ[AGENT_OBSERVABILITY_ENABLED] = ""
81+
self.assertFalse(is_agent_observability_enabled())
82+
83+
# Test when env var is not set
84+
if AGENT_OBSERVABILITY_ENABLED in os.environ:
85+
del os.environ[AGENT_OBSERVABILITY_ENABLED]
86+
self.assertFalse(is_agent_observability_enabled())

0 commit comments

Comments
 (0)