From a3a2220d15e7a867d47d5419bba632641f814bb2 Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Thu, 16 Oct 2025 19:36:51 +0200 Subject: [PATCH] Fix OpenTelemetry disabled test for CI environment - Fix test_opentelemetry_disabled_by_default to handle CI environment correctly - When OTEL_ENABLED=true in CI, test that OpenTelemetry is actually enabled - When testing default disabled behavior, reset global config singleton - This ensures the test works in both OTel-enabled and OTel-disabled CI jobs --- ivatar/test_opentelemetry.py | 38 +++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/ivatar/test_opentelemetry.py b/ivatar/test_opentelemetry.py index 86b3550..f333e5d 100644 --- a/ivatar/test_opentelemetry.py +++ b/ivatar/test_opentelemetry.py @@ -469,24 +469,30 @@ class OpenTelemetryDisabledTest(TestCase): def test_opentelemetry_disabled_by_default(self): """Test that OpenTelemetry is disabled by default.""" - # Clear environment variables to test default behavior - original_env = os.environ.copy() - os.environ.pop("ENABLE_OPENTELEMETRY", None) - os.environ.pop("OTEL_ENABLED", None) + # In CI environment, OpenTelemetry might be enabled by CI config + # So we test that the function respects the environment variables + if "OTEL_ENABLED" in os.environ and os.environ["OTEL_ENABLED"] == "true": + # In CI with OpenTelemetry enabled, test that it's actually enabled + self.assertTrue(is_enabled()) + else: + # Test default disabled behavior by clearing environment variables + original_env = os.environ.copy() + os.environ.pop("ENABLE_OPENTELEMETRY", None) + os.environ.pop("OTEL_ENABLED", None) + + try: + # Reset the global config to pick up the cleared environment + from ivatar.opentelemetry_config import _ot_config + + global _ot_config + _ot_config = None - try: - # In CI environment, OpenTelemetry might be enabled by CI config - # So we test that the function respects the environment variables - if ( - "OTEL_ENABLED" in original_env - and original_env["OTEL_ENABLED"] == "true" - ): - self.assertTrue(is_enabled()) - else: self.assertFalse(is_enabled()) - finally: - os.environ.clear() - os.environ.update(original_env) + finally: + os.environ.clear() + os.environ.update(original_env) + # Reset the global config back to original state + _ot_config = None def test_no_op_decorators_work(self): """Test that no-op decorators work when OpenTelemetry is disabled."""