1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
# ------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -------------------------------------------------------------------------
import unittest
import os
from unittest.mock import patch, Mock
from azure.appconfiguration.provider._request_tracing_context import (
_RequestTracingContext,
HostType,
RequestType,
CUSTOM_FILTER_KEY,
PERCENTAGE_FILTER_KEY,
TIME_WINDOW_FILTER_KEY,
TARGETING_FILTER_KEY,
PERCENTAGE_FILTER_NAMES,
TIME_WINDOW_FILTER_NAMES,
TARGETING_FILTER_NAMES,
FEATURE_FLAG_USES_SEED_TAG,
FEATURE_FLAG_USES_TELEMETRY_TAG,
)
from azure.appconfiguration.provider._constants import (
REQUEST_TRACING_DISABLED_ENVIRONMENT_VARIABLE,
ServiceFabricEnvironmentVariable,
AzureFunctionEnvironmentVariable,
AzureWebAppEnvironmentVariable,
ContainerAppEnvironmentVariable,
KubernetesEnvironmentVariable,
APP_CONFIG_AI_MIME_PROFILE,
APP_CONFIG_AICC_MIME_PROFILE,
)
class TestRequestTracingContext(unittest.TestCase):
"""Test the _RequestTracingContext class."""
def setUp(self):
"""Set up test environment."""
self.context = _RequestTracingContext()
def test_initialization_with_defaults(self):
"""Test initialization with default values."""
context = _RequestTracingContext()
self.assertFalse(context.uses_load_balancing)
self.assertFalse(context.uses_ai_configuration)
self.assertFalse(context.uses_aicc_configuration)
self.assertFalse(context.uses_telemetry)
self.assertFalse(context.uses_seed)
self.assertIsNone(context.max_variants)
self.assertEqual(context.feature_filter_usage, {})
self.assertEqual(context.host_type, HostType.UNIDENTIFIED)
self.assertFalse(context.is_key_vault_configured)
self.assertEqual(context.replica_count, 0)
self.assertFalse(context.is_failover_request)
def test_initialization_with_load_balancing(self):
"""Test initialization with load balancing enabled."""
context = _RequestTracingContext(load_balancing_enabled=True)
self.assertTrue(context.uses_load_balancing)
def testupdate_max_variants(self):
"""Test updating max variants."""
self.context.update_max_variants(5)
self.assertEqual(self.context.max_variants, 5)
# Should update to larger value
self.context.update_max_variants(10)
self.assertEqual(self.context.max_variants, 10)
# Should not update to smaller value
self.context.update_max_variants(3)
self.assertEqual(self.context.max_variants, 10)
def test__create_features_string_empty(self):
"""Test _create_features_string with no features enabled."""
result = self.context._create_features_string()
self.assertEqual(result, "")
def test__create_features_string_with_features(self):
"""Test _create_features_string with features enabled."""
self.context.uses_load_balancing = True
self.context.uses_ai_configuration = True
self.context.uses_aicc_configuration = True
result = self.context._create_features_string()
self.assertEqual(result, "LB+AI+AICC")
def test__create_ff_features_string_empty(self):
"""Test _create_feature_string with no FF features enabled."""
result = self.context._create_ff_features_string()
self.assertEqual(result, "")
def test__create_ff_features_string_with_features(self):
"""Test _create_ff_features_string with FF features enabled."""
self.context.uses_seed = True
self.context.uses_telemetry = True
result = self.context._create_ff_features_string()
expected = f"{FEATURE_FLAG_USES_SEED_TAG}+{FEATURE_FLAG_USES_TELEMETRY_TAG}"
self.assertEqual(result, expected)
def test_get_host_type_unidentified(self):
"""Test host type detection with no environment variables."""
with patch.dict(os.environ, {}, clear=True):
result = _RequestTracingContext.get_host_type()
self.assertEqual(result, HostType.UNIDENTIFIED)
def test_get_host_type_azure_function(self):
"""Test host type detection for Azure Functions."""
with patch.dict(os.environ, {AzureFunctionEnvironmentVariable: "test_value"}):
result = _RequestTracingContext.get_host_type()
self.assertEqual(result, HostType.AZURE_FUNCTION)
def test_get_host_type_azure_web_app(self):
"""Test host type detection for Azure Web App."""
with patch.dict(os.environ, {AzureWebAppEnvironmentVariable: "test_value"}):
result = _RequestTracingContext.get_host_type()
self.assertEqual(result, HostType.AZURE_WEB_APP)
def test_get_host_type_container_app(self):
"""Test host type detection for Container App."""
with patch.dict(os.environ, {ContainerAppEnvironmentVariable: "test_value"}):
result = _RequestTracingContext.get_host_type()
self.assertEqual(result, HostType.CONTAINER_APP)
def test_get_host_type_kubernetes(self):
"""Test host type detection for Kubernetes."""
with patch.dict(os.environ, {KubernetesEnvironmentVariable: "test_value"}):
result = _RequestTracingContext.get_host_type()
self.assertEqual(result, HostType.KUBERNETES)
def test_get_host_type_service_fabric(self):
"""Test host type detection for Service Fabric."""
with patch.dict(os.environ, {ServiceFabricEnvironmentVariable: "test_value"}):
result = _RequestTracingContext.get_host_type()
self.assertEqual(result, HostType.SERVICE_FABRIC)
@patch("azure.appconfiguration.provider._request_tracing_context.version")
def test_get_assembly_version_success(self, mock_version):
"""Test successful package version retrieval."""
mock_version.return_value = "1.2.3"
result = _RequestTracingContext.get_assembly_version("test_package")
self.assertEqual(result, "1.2.3")
@patch("azure.appconfiguration.provider._request_tracing_context.version")
def test_get_assembly_version_not_found(self, mock_version):
"""Test package version retrieval when package not found."""
from importlib.metadata import PackageNotFoundError
mock_version.side_effect = PackageNotFoundError()
result = _RequestTracingContext.get_assembly_version("nonexistent_package")
self.assertIsNone(result)
def test_get_assembly_version_empty_package_name(self):
"""Test package version retrieval with empty package name."""
result = _RequestTracingContext.get_assembly_version("")
self.assertIsNone(result)
def test_reset_ai_configuration_tracing(self):
"""Test reset_ai_configuration_tracing method."""
self.context.uses_ai_configuration = True
self.context.uses_aicc_configuration = True
self.context.reset_ai_configuration_tracing()
self.assertFalse(self.context.uses_ai_configuration)
self.assertFalse(self.context.uses_aicc_configuration)
def test_update_ai_configuration_tracing_ai_profile(self):
"""Test update_ai_configuration_tracing with AI profile."""
content_type = "application/json; " + APP_CONFIG_AI_MIME_PROFILE
self.context.update_ai_configuration_tracing(content_type)
self.assertTrue(self.context.uses_ai_configuration)
self.assertFalse(self.context.uses_aicc_configuration)
def test_update_ai_configuration_tracing_aicc_profile(self):
"""Test update_ai_configuration_tracing with AI Chat Completion profile."""
content_type = "application/json; " + APP_CONFIG_AICC_MIME_PROFILE
self.context.update_ai_configuration_tracing(content_type)
self.assertTrue(self.context.uses_ai_configuration)
self.assertTrue(self.context.uses_aicc_configuration)
def test_update_ai_configuration_tracing_no_content_type(self):
"""Test update_ai_configuration_tracing with no content type."""
self.context.update_ai_configuration_tracing(None)
self.assertFalse(self.context.uses_ai_configuration)
self.assertFalse(self.context.uses_aicc_configuration)
def test_create_filters_string_empty(self):
"""Test _create_filters_string with no filters."""
result = self.context._create_filters_string()
self.assertEqual(result, "")
def test_create_filters_string_with_filters(self):
"""Test _create_filters_string with all filter types."""
self.context.feature_filter_usage = {
CUSTOM_FILTER_KEY: True,
PERCENTAGE_FILTER_KEY: True,
TIME_WINDOW_FILTER_KEY: True,
TARGETING_FILTER_KEY: True,
}
result = self.context._create_filters_string()
expected = f"{CUSTOM_FILTER_KEY}+{PERCENTAGE_FILTER_KEY}+{TIME_WINDOW_FILTER_KEY}+{TARGETING_FILTER_KEY}"
self.assertEqual(result, expected)
def test_update_feature_filter_telemetry(self):
"""Test update_feature_filter_telemetry method."""
feature_flag = Mock()
feature_flag.filters = [
{"name": PERCENTAGE_FILTER_NAMES[0]},
{"name": TIME_WINDOW_FILTER_NAMES[0]},
{"name": TARGETING_FILTER_NAMES[0]},
{"name": "CustomFilter"},
]
self.context.update_feature_filter_telemetry(feature_flag)
self.assertTrue(self.context.feature_filter_usage[PERCENTAGE_FILTER_KEY])
self.assertTrue(self.context.feature_filter_usage[TIME_WINDOW_FILTER_KEY])
self.assertTrue(self.context.feature_filter_usage[TARGETING_FILTER_KEY])
self.assertTrue(self.context.feature_filter_usage[CUSTOM_FILTER_KEY])
def test_update_feature_filter_telemetry_no_filters(self):
"""Test update_feature_filter_telemetry with no filters."""
feature_flag = Mock()
feature_flag.filters = None
self.context.update_feature_filter_telemetry(feature_flag)
self.assertEqual(self.context.feature_filter_usage, {})
def test_reset_feature_filter_usage(self):
"""Test reset_feature_filter_usage method."""
self.context.feature_filter_usage = {CUSTOM_FILTER_KEY: True}
self.context.reset_feature_filter_usage()
self.assertEqual(self.context.feature_filter_usage, {})
class TestCreateCorrelationContextHeader(unittest.TestCase):
"""Test the update_correlation_context_header method."""
def setUp(self):
"""Set up test environment."""
self.context = _RequestTracingContext()
def test_disabled_tracing_returns_empty_string(self):
"""Test that disabled tracing returns empty string."""
with patch.dict(os.environ, {REQUEST_TRACING_DISABLED_ENVIRONMENT_VARIABLE: "true"}):
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertEqual(result, headers) # Should return headers unchanged
self.assertNotIn("Correlation-Context", result)
def test_basic_correlation_context(self):
"""Test basic correlation context generation."""
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("RequestType=Startup", result["Correlation-Context"])
def test_correlation_context_with_replica_count(self):
"""Test correlation context with replica count."""
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.WATCH, 3, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("RequestType=Watch", result["Correlation-Context"])
self.assertIn("ReplicaCount=3", result["Correlation-Context"])
def test_correlation_context_with_host_type(self):
"""Test correlation context with host type detection."""
with patch.object(_RequestTracingContext, "get_host_type", return_value=HostType.AZURE_FUNCTION):
# Update host_type since it's not automatically set
self.context.host_type = HostType.AZURE_FUNCTION
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("Host=AzureFunction", result["Correlation-Context"])
def test_correlation_context_with_feature_filters(self):
"""Test correlation context with feature filters."""
self.context.feature_filter_usage = {
CUSTOM_FILTER_KEY: True,
PERCENTAGE_FILTER_KEY: True,
}
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("Filter=", result["Correlation-Context"])
self.assertIn(CUSTOM_FILTER_KEY, result["Correlation-Context"])
self.assertIn(PERCENTAGE_FILTER_KEY, result["Correlation-Context"])
def test_correlation_context_with_max_variants(self):
"""Test correlation context with max variants."""
self.context.max_variants = 5
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("MaxVariants=5", result["Correlation-Context"])
def test_correlation_context_with_ff_features(self):
"""Test correlation context with feature flag features."""
self.context.uses_seed = True
self.context.uses_telemetry = True
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("FFFeatures=", result["Correlation-Context"])
self.assertIn(FEATURE_FLAG_USES_SEED_TAG, result["Correlation-Context"])
self.assertIn(FEATURE_FLAG_USES_TELEMETRY_TAG, result["Correlation-Context"])
@patch("azure.appconfiguration.provider._request_tracing_context.version")
def test_correlation_context_with_version(self, mock_version):
"""Test correlation context with feature management version."""
mock_version.return_value = "1.0.0"
self.context.feature_management_version = "1.0.0"
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("FMPyVer=1.0.0", result["Correlation-Context"])
def test_correlation_context_with_general_features(self):
"""Test correlation context with general features."""
self.context.uses_load_balancing = True
self.context.uses_ai_configuration = True
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, False, False)
self.assertIn("Correlation-Context", result)
self.assertIn("Features=LB+AI", result["Correlation-Context"])
def test_correlation_context_with_tags(self):
"""Test correlation context with various tags."""
self.context.is_key_vault_configured = True
self.context.is_failover_request = True
self.context.is_push_refresh_used = True
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.STARTUP, 0, True, False, True)
self.assertIn("Correlation-Context", result)
self.assertIn("UsesKeyVault", result["Correlation-Context"])
self.assertIn("Failover", result["Correlation-Context"])
def test_correlation_context_comprehensive(self):
"""Test correlation context with all features enabled."""
# Set up all possible features
self.context.host_type = HostType.AZURE_WEB_APP
self.context.feature_filter_usage = {CUSTOM_FILTER_KEY: True}
self.context.max_variants = 3
self.context.uses_seed = True
self.context.feature_management_version = "1.0.0"
self.context.uses_load_balancing = True
self.context.is_key_vault_configured = True
self.context.is_failover_request = True
headers = {}
result = self.context.update_correlation_context_header(headers, RequestType.WATCH, 2, True, True, True)
# Verify all components are present
self.assertIn("Correlation-Context", result)
correlation_context = result["Correlation-Context"]
self.assertIn("RequestType=Watch", correlation_context)
self.assertIn("ReplicaCount=2", correlation_context)
self.assertIn("Host=AzureWebApp", correlation_context)
self.assertIn("Filter=", correlation_context)
self.assertIn("MaxVariants=3", correlation_context)
self.assertIn("FFFeatures=", correlation_context)
self.assertIn("FMPyVer=1.0.0", correlation_context)
self.assertIn("Features=LB", correlation_context)
self.assertIn("UsesKeyVault", correlation_context)
self.assertIn("Failover", correlation_context)
class TestUpdateCorrelationContextHeader(unittest.TestCase):
"""Test the update_correlation_context_header method."""
def setUp(self):
"""Set up test environment."""
self.context = _RequestTracingContext()
self.headers = {}
def test_update_correlation_context_header_basic(self):
"""Test basic correlation context header update."""
result = self.context.update_correlation_context_header(
self.headers,
RequestType.STARTUP,
2, # replica_count
True, # uses_key_vault
True, # feature_flag_enabled
False, # is_failover_request
)
self.assertIn("Correlation-Context", result)
self.assertIn("RequestType=Startup", result["Correlation-Context"])
self.assertIn("ReplicaCount=2", result["Correlation-Context"])
self.assertIn("UsesKeyVault", result["Correlation-Context"])
@patch("azure.appconfiguration.provider._request_tracing_context.version")
def test_update_correlation_context_header_with_version(self, mock_version):
"""Test correlation context header update with feature management version."""
mock_version.return_value = "1.0.0"
result = self.context.update_correlation_context_header(
self.headers,
RequestType.STARTUP,
0, # replica_count
False, # uses_key_vault
True, # feature_flag_enabled
False, # is_failover_request
)
self.assertIn("Correlation-Context", result)
self.assertIn("FMPyVer=1.0.0", result["Correlation-Context"])
def test_update_correlation_context_header_failover(self):
"""Test correlation context header update with failover request."""
result = self.context.update_correlation_context_header(
self.headers,
RequestType.WATCH,
1, # replica_count
False, # uses_key_vault
False, # feature_flag_enabled
True, # is_failover_request
)
self.assertIn("Correlation-Context", result)
self.assertIn("Failover", result["Correlation-Context"])
def test_update_correlation_context_header_disabled_tracing(self):
"""Test correlation context header update with disabled tracing."""
with patch.dict(os.environ, {REQUEST_TRACING_DISABLED_ENVIRONMENT_VARIABLE: "true"}):
result = self.context.update_correlation_context_header(
self.headers,
RequestType.STARTUP,
1, # replica_count
False, # uses_key_vault
False, # feature_flag_enabled
False, # is_failover_request
)
self.assertNotIn("Correlation-Context", result)
|