Skip to content

Commit e86fcf1

Browse files
committed
test(o11y): add comprehensive tests for generic feature resolver
1 parent 5adee0c commit e86fcf1

1 file changed

Lines changed: 84 additions & 69 deletions

File tree

packages/google-api-core/tests/unit/observability/test_options.py

Lines changed: 84 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -91,83 +91,98 @@ def test_set_test_env_override_clear_specific():
9191
assert _get_env_bool("TEST_B") is True
9292

9393

94-
def test_get_env_bool_with_dev_fallback_other_prefix(monkeypatch):
95-
"""Verify that environment variables without the 'GOOGLE_CLOUD_' prefix fall back directly.
9694

97-
This is important to ensure that generic or non-GCP environment variables
98-
are handled correctly by the fallback logic without triggering GCP-specific
99-
replacement logic.
100-
"""
101-
monkeypatch.setenv("OTHER_PREFIX_VAR", "true")
102-
assert options._get_env_bool_with_dev_fallback("OTHER_PREFIX_VAR") is True
10395

96+
def test_resolve_feature_flags_ga_enabled_via_env():
97+
"""Verify that a GA feature is enabled if its environment variable is True."""
98+
# Setup: We pass a GA environment variable set to True
99+
set_test_env_override("GOOGLE_SDK_PYTHON_TRACING_ENABLED", True)
104100

105-
@pytest.mark.parametrize(
106-
"feature_name, env_vars, client_options, default_val, expected",
107-
[
108-
# Default fallback tests
109-
("tracing", {}, None, False, False),
110-
# Default=True is ignored if blocked
111-
("tracing", {}, None, True, False),
112-
# Global GA env var is ignored if experimental flag is missing
113-
("tracing", {"GOOGLE_SDK_PYTHON_TRACING_ENABLED": True}, None, False, False),
114-
("tracing", {"GOOGLE_SDK_PYTHON_TRACING_ENABLED": False}, None, True, False),
115-
# Experimental fallback
116-
(
117-
"tracing",
118-
{"GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED": True},
119-
None,
120-
False,
121-
True,
122-
),
123-
(
124-
"tracing",
125-
{"GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED": False},
126-
None,
127-
True,
128-
False,
129-
),
130-
# Programmatic boolean flags are NOT supported in client_options
131-
# (should default/fallback to False)
132-
(
133-
"tracing",
134-
{"GOOGLE_SDK_PYTHON_TRACING_ENABLED": False},
135-
{"enable_metrics": True},
136-
False,
137-
False,
138-
),
139-
(
140-
"tracing",
141-
{"GOOGLE_SDK_PYTHON_TRACING_ENABLED": False},
142-
{"enable_tracing": True},
143-
False,
144-
False,
145-
),
146-
],
147-
)
148-
def test_resolve_feature_flags(
149-
feature_name, env_vars, client_options, default_val, expected
150-
):
151-
# Setup environment variables using our test overrides
152-
for k, v in env_vars.items():
153-
set_test_env_override(k, v)
154-
101+
# Action
155102
result = options.resolve_feature_flags(
156-
feature_name, client_options=client_options, default=default_val
103+
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
104+
provider_key="tracer_provider",
105+
client_options=None
157106
)
158-
assert result is expected
159-
160107

161-
def test_resolve_feature_flags_invalid_signal():
162-
with pytest.raises(ValueError, match="Only 'tracing' is supported"):
163-
options.resolve_feature_flags("metrics")
108+
# Assertion
109+
assert result is True
164110

165111

166-
def test_resolve_feature_flags_experimental_gate_blocks_provider():
167-
"""Verify that programmatic provider is blocked for experimental features without env var."""
168-
# Assume 'tracing' is experimental for this test.
169-
# We pass tracer_provider but do NOT set GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED
112+
@pytest.mark.parametrize("gate_value", [None, False])
113+
def test_resolve_feature_flags_exp_blocked_with_provider_fails_fast(gate_value):
114+
"""Verify that passing a provider to an experimental feature without the gate raises ValueError."""
115+
# Setup: Experimental env var is set to gate_value (None means not set)
116+
set_test_env_override("GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED", gate_value)
170117
client_options = {"tracer_provider": object()}
171118

119+
# Action & Assertion
172120
with pytest.raises(ValueError, match="Experimental feature"):
173-
options.resolve_feature_flags("tracing", client_options=client_options)
121+
options.resolve_feature_flags(
122+
env_var="GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED",
123+
provider_key="tracer_provider",
124+
client_options=client_options
125+
)
126+
127+
128+
def test_resolve_feature_flags_exp_enabled_with_provider():
129+
"""Verify that experimental feature is enabled if gate is True, even with provider."""
130+
set_test_env_override("GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED", True)
131+
client_options = {"tracer_provider": object()}
132+
133+
result = options.resolve_feature_flags(
134+
env_var="GOOGLE_SDK_EXPERIMENTAL_PYTHON_TRACING_ENABLED",
135+
provider_key="tracer_provider",
136+
client_options=client_options
137+
)
138+
assert result is True
139+
140+
141+
def test_resolve_feature_flags_ga_enabled_via_provider():
142+
"""Verify that a GA feature is enabled if a provider is passed, bypassing env var."""
143+
# Env var is False, but provider is present
144+
set_test_env_override("GOOGLE_SDK_PYTHON_TRACING_ENABLED", False)
145+
client_options = {"tracer_provider": object()}
146+
147+
result = options.resolve_feature_flags(
148+
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
149+
provider_key="tracer_provider",
150+
client_options=client_options
151+
)
152+
assert result is True
153+
154+
155+
@pytest.mark.parametrize("env_val", [None, False], ids=["env_not_set", "env_explicit_false"])
156+
def test_resolve_feature_flags_ga_fallback_to_false(env_val):
157+
"""Verify that a GA feature returns False if no flags are present."""
158+
set_test_env_override("GOOGLE_SDK_PYTHON_TRACING_ENABLED", env_val)
159+
result = options.resolve_feature_flags(
160+
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
161+
provider_key="tracer_provider",
162+
client_options=None
163+
)
164+
assert result is False
165+
166+
167+
class _MockOptions:
168+
def __init__(self):
169+
self.other_option = "value"
170+
171+
172+
@pytest.mark.parametrize(
173+
"client_options",
174+
[
175+
{"other_option": "value"},
176+
_MockOptions(),
177+
],
178+
ids=["dict_without_key", "object_without_key"]
179+
)
180+
def test_resolve_feature_flags_options_without_key(client_options):
181+
"""Verify behavior when client_options is present but missing the provider key."""
182+
# GA Path: should fall through to env var / fallback
183+
result = options.resolve_feature_flags(
184+
env_var="GOOGLE_SDK_PYTHON_TRACING_ENABLED",
185+
provider_key="tracer_provider",
186+
client_options=client_options
187+
)
188+
assert result is False

0 commit comments

Comments
 (0)