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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
|
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import json
import unittest
from unittest.mock import Mock, patch
import requests
from azure.monitor.opentelemetry.exporter._configuration._utils import (
_ConfigurationProfile,
OneSettingsResponse,
make_onesettings_request,
_parse_onesettings_response,
evaluate_feature,
_matches_override_rule,
_matches_condition,
_compare_versions,
_parse_version_with_beta,
)
from azure.monitor.opentelemetry.exporter._constants import (
_ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS,
_ONE_SETTINGS_CHANGE_VERSION_KEY,
)
class TestConfigurationProfile(unittest.TestCase):
"""Test cases for _ConfigurationProfile class."""
def setUp(self):
"""Reset profile before each test."""
_ConfigurationProfile.os = ""
_ConfigurationProfile.rp = ""
_ConfigurationProfile.attach = ""
_ConfigurationProfile.version = ""
_ConfigurationProfile.component = ""
_ConfigurationProfile.region = ""
def test_fill_empty_profile(self):
"""Test filling an empty profile with all parameters."""
_ConfigurationProfile.fill(
os="w",
rp="f",
attach="m",
version="1.0.0",
component="ext",
region="westus"
)
self.assertEqual(_ConfigurationProfile.os, "w")
self.assertEqual(_ConfigurationProfile.rp, "f")
self.assertEqual(_ConfigurationProfile.attach, "m")
self.assertEqual(_ConfigurationProfile.version, "1.0.0")
self.assertEqual(_ConfigurationProfile.component, "ext")
self.assertEqual(_ConfigurationProfile.region, "westus")
def test_fill_partial_profile(self):
"""Test filling profile with only some parameters."""
_ConfigurationProfile.fill(os="l", version="2.0.0")
self.assertEqual(_ConfigurationProfile.os, "l")
self.assertEqual(_ConfigurationProfile.version, "2.0.0")
self.assertEqual(_ConfigurationProfile.rp, "")
self.assertEqual(_ConfigurationProfile.attach, "")
self.assertEqual(_ConfigurationProfile.component, "")
self.assertEqual(_ConfigurationProfile.region, "")
def test_fill_no_overwrite(self):
"""Test that fill doesn't overwrite existing values."""
# Set initial values
_ConfigurationProfile.os = "w"
_ConfigurationProfile.version = "1.0.0"
# Try to overwrite - should be ignored
_ConfigurationProfile.fill(os="l", version="2.0.0", rp="f")
# Original values should be preserved
self.assertEqual(_ConfigurationProfile.os, "w")
self.assertEqual(_ConfigurationProfile.version, "1.0.0")
# New value should be set
self.assertEqual(_ConfigurationProfile.rp, "f")
class TestOneSettingsResponse(unittest.TestCase):
"""Test cases for OneSettingsResponse class."""
def test_default_initialization(self):
"""Test OneSettingsResponse with default values."""
response = OneSettingsResponse()
self.assertIsNone(response.etag)
self.assertEqual(response.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(response.settings, {})
self.assertIsNone(response.version)
self.assertEqual(response.status_code, 200)
self.assertFalse(response.has_exception)
def test_custom_initialization(self):
"""Test OneSettingsResponse with custom values."""
settings = {"key": "value"}
response = OneSettingsResponse(
etag="test-etag",
refresh_interval=3600,
settings=settings,
version=5,
status_code=304
)
self.assertEqual(response.etag, "test-etag")
self.assertEqual(response.refresh_interval, 3600)
self.assertEqual(response.settings, settings)
self.assertEqual(response.version, 5)
self.assertEqual(response.status_code, 304)
self.assertFalse(response.has_exception)
def test_exception_initialization(self):
"""Test OneSettingsResponse with exception indicator."""
response = OneSettingsResponse(
has_exception=True,
status_code=500
)
self.assertIsNone(response.etag)
self.assertEqual(response.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(response.settings, {})
self.assertIsNone(response.version)
self.assertEqual(response.status_code, 500)
self.assertTrue(response.has_exception)
def test_timeout_initialization(self):
"""Test OneSettingsResponse with timeout indicator."""
response = OneSettingsResponse(
has_exception=True
)
self.assertIsNone(response.etag)
self.assertEqual(response.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(response.settings, {})
self.assertIsNone(response.version)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.has_exception)
def test_all_error_indicators(self):
"""Test OneSettingsResponse with all error indicators set."""
response = OneSettingsResponse(
status_code=408,
has_exception=True,
)
self.assertEqual(response.status_code, 408)
self.assertTrue(response.has_exception)
class TestMakeOneSettingsRequest(unittest.TestCase):
"""Test cases for make_onesettings_request function."""
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_successful_request(self, mock_get):
"""Test successful OneSettings request."""
# Setup mock response
mock_response = Mock()
mock_response.status_code = 200
mock_response.headers = {
"ETag": "test-etag",
"x-ms-onesetinterval": "30"
}
mock_response.content = json.dumps({
"settings": {"key": "value", _ONE_SETTINGS_CHANGE_VERSION_KEY: "5"}
}).encode('utf-8')
mock_get.return_value = mock_response
# Make request
result = make_onesettings_request("http://test.com", {"param": "value"}, {"header": "value"})
# Verify request was made correctly
mock_get.assert_called_once_with(
"http://test.com",
params={"param": "value"},
headers={"header": "value"},
timeout=10
)
# Verify response
self.assertEqual(result.etag, "test-etag")
self.assertEqual(result.refresh_interval, 1800) # 30 minutes * 60
self.assertEqual(result.settings, {"key": "value", _ONE_SETTINGS_CHANGE_VERSION_KEY: "5"})
self.assertEqual(result.version, 5)
self.assertEqual(result.status_code, 200)
self.assertFalse(result.has_exception)
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_request_timeout_exception(self, mock_get):
"""Test OneSettings request with timeout exception."""
mock_get.side_effect = requests.exceptions.Timeout("Request timed out")
result = make_onesettings_request("http://test.com")
# Should return response with timeout and exception indicators
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
self.assertTrue(result.has_exception)
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_request_connection_exception(self, mock_get):
"""Test OneSettings request with connection exception."""
mock_get.side_effect = requests.exceptions.ConnectionError("Connection failed")
result = make_onesettings_request("http://test.com")
# Should return response with exception indicator but no timeout
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
self.assertTrue(result.has_exception)
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_request_http_exception(self, mock_get):
"""Test OneSettings request with HTTP exception."""
mock_get.side_effect = requests.exceptions.HTTPError("HTTP 500 Error")
result = make_onesettings_request("http://test.com")
# Should return response with exception indicator
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
self.assertTrue(result.has_exception)
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_request_generic_exception(self, mock_get):
"""Test OneSettings request with generic exception."""
mock_get.side_effect = Exception("Unexpected error")
result = make_onesettings_request("http://test.com")
# Should return response with exception indicator
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
self.assertTrue(result.has_exception)
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
@patch('azure.monitor.opentelemetry.exporter._configuration._utils._parse_onesettings_response')
def test_json_decode_exception(self, mock_parse, mock_get):
"""Test OneSettings request with JSON decode exception."""
# Setup mock response
mock_response = Mock()
mock_response.status_code = 200
mock_response.headers = {"ETag": "test-etag"}
mock_response.content = b"invalid json content"
mock_get.return_value = mock_response
# Mock _parse_onesettings_response to raise JSONDecodeError
from json import JSONDecodeError
mock_parse.side_effect = JSONDecodeError("Expecting value", "invalid json content", 0)
result = make_onesettings_request("http://test.com")
# Should return response with exception indicator
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
self.assertTrue(result.has_exception)
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_http_error_status_codes(self, mock_get):
"""Test OneSettings request with various HTTP error status codes."""
# Test different HTTP error codes
error_codes = [400, 401, 403, 404, 429, 500, 502, 503, 504]
for status_code in error_codes:
with self.subTest(status_code=status_code):
mock_response = Mock()
mock_response.status_code = status_code
mock_response.raise_for_status.side_effect = requests.exceptions.HTTPError(f"HTTP {status_code}")
mock_get.return_value = mock_response
result = make_onesettings_request("http://test.com")
# Should return response with exception indicator
self.assertTrue(result.has_exception)
self.assertEqual(result.status_code, 200) # Default status when exception occurs
@patch('azure.monitor.opentelemetry.exporter._configuration._utils.requests.get')
def test_request_exception_legacy(self, mock_get):
"""Test OneSettings request with network exception (legacy behavior test)."""
mock_get.side_effect = requests.exceptions.RequestException("Network error")
result = make_onesettings_request("http://test.com")
# Should return response with exception indicator
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
self.assertTrue(result.has_exception)
class TestParseOneSettingsResponse(unittest.TestCase):
"""Test cases for _parse_onesettings_response function."""
def test_parse_200_response(self):
"""Test parsing successful 200 response."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.headers = {
"ETag": "test-etag",
"x-ms-onesetinterval": "45"
}
mock_response.content = json.dumps({
"settings": {"feature": "enabled", _ONE_SETTINGS_CHANGE_VERSION_KEY: "10"}
}).encode('utf-8')
result = _parse_onesettings_response(mock_response)
self.assertEqual(result.etag, "test-etag")
self.assertEqual(result.refresh_interval, 2700) # 45 minutes * 60
self.assertEqual(result.settings, {"feature": "enabled", _ONE_SETTINGS_CHANGE_VERSION_KEY: "10"})
self.assertEqual(result.version, 10)
self.assertEqual(result.status_code, 200)
def test_parse_304_response(self):
"""Test parsing 304 Not Modified response."""
mock_response = Mock()
mock_response.status_code = 304
mock_response.headers = {
"ETag": "cached-etag",
"x-ms-onesetinterval": "60"
}
mock_response.content = b""
result = _parse_onesettings_response(mock_response)
self.assertEqual(result.etag, "cached-etag")
self.assertEqual(result.refresh_interval, 3600) # 60 minutes * 60
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 304)
def test_parse_invalid_json(self):
"""Test parsing response with invalid JSON."""
mock_response = Mock()
mock_response.status_code = 200
mock_response.headers = {}
mock_response.content = b"invalid json"
result = _parse_onesettings_response(mock_response)
self.assertIsNone(result.etag)
self.assertEqual(result.refresh_interval, _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS)
self.assertEqual(result.settings, {})
self.assertIsNone(result.version)
self.assertEqual(result.status_code, 200)
class TestEvaluateFeature(unittest.TestCase):
"""Test cases for evaluate_feature function."""
def setUp(self):
"""Set up test configuration profile."""
_ConfigurationProfile.os = "w"
_ConfigurationProfile.rp = "f"
_ConfigurationProfile.attach = "m"
_ConfigurationProfile.version = "1.0.0"
_ConfigurationProfile.component = "ext"
_ConfigurationProfile.region = "westus"
def tearDown(self):
"""Reset profile after each test."""
_ConfigurationProfile.os = ""
_ConfigurationProfile.rp = ""
_ConfigurationProfile.attach = ""
_ConfigurationProfile.version = ""
_ConfigurationProfile.component = ""
_ConfigurationProfile.region = ""
def test_feature_enabled_by_default(self):
"""Test feature that is enabled by default with no overrides."""
settings = {
"test_feature": {
"default": "enabled"
}
}
result = evaluate_feature("test_feature", settings)
self.assertTrue(result)
def test_feature_disabled_by_default(self):
"""Test feature that is disabled by default with no overrides."""
settings = {
"test_feature": {
"default": "disabled"
}
}
result = evaluate_feature("test_feature", settings)
self.assertFalse(result)
def test_feature_override_matches(self):
"""Test feature override that matches current profile."""
settings = {
"test_feature": {
"default": "disabled",
"override": [
{"os": "w", "component": "ext"}
]
}
}
result = evaluate_feature("test_feature", settings)
self.assertTrue(result) # Override flips disabled to enabled
def test_feature_override_no_match(self):
"""Test feature override that doesn't match current profile."""
settings = {
"test_feature": {
"default": "enabled",
"override": [
{"os": "l", "component": "dst"}
]
}
}
result = evaluate_feature("test_feature", settings)
self.assertTrue(result) # No override, stays default
def test_feature_multiple_overrides(self):
"""Test feature with multiple override rules."""
settings = {
"test_feature": {
"default": "disabled",
"override": [
{"os": "l"}, # Doesn't match
{"component": "ext", "rp": "f"}, # Matches
{"region": "eastus"} # Doesn't match
]
}
}
result = evaluate_feature("test_feature", settings)
self.assertTrue(result) # Second override matches
def test_invalid_inputs(self):
"""Test evaluate_feature with invalid inputs."""
# Empty feature key
self.assertIsNone(evaluate_feature("", {}))
# None settings
self.assertIsNone(evaluate_feature("test", None))
# Feature not in settings
self.assertIsNone(evaluate_feature("missing", {}))
# Invalid feature config
self.assertIsNone(evaluate_feature("test", {"test": "invalid"}))
class TestMatchesOverrideRule(unittest.TestCase):
"""Test cases for _matches_override_rule function."""
def setUp(self):
"""Set up test configuration profile."""
_ConfigurationProfile.os = "w"
_ConfigurationProfile.version = "1.0.0"
_ConfigurationProfile.component = "ext"
def tearDown(self):
"""Reset profile after each test."""
_ConfigurationProfile.os = ""
_ConfigurationProfile.version = ""
_ConfigurationProfile.component = ""
def test_all_conditions_match(self):
"""Test rule where all conditions match."""
rule = {"os": "w", "component": "ext"}
self.assertTrue(_matches_override_rule(rule))
def test_some_conditions_match(self):
"""Test rule where only some conditions match."""
rule = {"os": "w", "component": "dst"} # component doesn't match
self.assertFalse(_matches_override_rule(rule))
def test_empty_rule(self):
"""Test empty rule."""
self.assertFalse(_matches_override_rule({}))
self.assertFalse(_matches_override_rule(None))
class TestMatchesCondition(unittest.TestCase):
"""Test cases for _matches_condition function."""
def setUp(self):
"""Set up test configuration profile."""
_ConfigurationProfile.os = "w"
_ConfigurationProfile.version = "1.0.0"
_ConfigurationProfile.component = "ext"
_ConfigurationProfile.rp = "f"
_ConfigurationProfile.region = "westus"
_ConfigurationProfile.attach = "m"
def tearDown(self):
"""Reset profile after each test."""
_ConfigurationProfile.os = ""
_ConfigurationProfile.version = ""
_ConfigurationProfile.component = ""
_ConfigurationProfile.rp = ""
_ConfigurationProfile.region = ""
_ConfigurationProfile.attach = ""
def test_os_condition_single_value(self):
"""Test OS condition with single value."""
self.assertTrue(_matches_condition("os", "w"))
self.assertFalse(_matches_condition("os", "l"))
def test_os_condition_list_value(self):
"""Test OS condition with list value."""
self.assertTrue(_matches_condition("os", ["w", "l"]))
self.assertFalse(_matches_condition("os", ["l", "d"]))
def test_version_condition_exact(self):
"""Test version condition with exact match."""
self.assertTrue(_matches_condition("ver", "1.0.0"))
self.assertFalse(_matches_condition("ver", "2.0.0"))
def test_version_condition_range(self):
"""Test version condition with min/max range."""
# Within range
self.assertTrue(_matches_condition("ver", {"min": "0.9.0", "max": "1.1.0"}))
# Below minimum
self.assertFalse(_matches_condition("ver", {"min": "1.1.0", "max": "2.0.0"}))
# Above maximum
self.assertFalse(_matches_condition("ver", {"min": "0.5.0", "max": "0.9.0"}))
def test_component_condition(self):
"""Test component condition."""
self.assertTrue(_matches_condition("component", "ext"))
self.assertFalse(_matches_condition("component", "dst"))
def test_rp_condition_single_value(self):
"""Test runtime platform condition with single value."""
self.assertTrue(_matches_condition("rp", "f"))
self.assertFalse(_matches_condition("rp", "a"))
def test_rp_condition_list_value(self):
"""Test runtime platform condition with list value."""
self.assertTrue(_matches_condition("rp", ["f", "a"]))
self.assertFalse(_matches_condition("rp", ["a", "k"]))
def test_region_condition(self):
"""Test region condition."""
self.assertTrue(_matches_condition("region", "westus"))
self.assertTrue(_matches_condition("region", ["westus", "eastus"]))
self.assertFalse(_matches_condition("region", "eastus"))
self.assertFalse(_matches_condition("region", ["eastus", "centralus"]))
def test_attach_condition(self):
"""Test attach condition."""
self.assertTrue(_matches_condition("attach", "m"))
self.assertTrue(_matches_condition("attach", ["m", "i"]))
self.assertFalse(_matches_condition("attach", "i"))
self.assertFalse(_matches_condition("attach", ["i"]))
def test_invalid_condition(self):
"""Test invalid condition key."""
self.assertFalse(_matches_condition("unknown", "value"))
self.assertFalse(_matches_condition("", "value"))
self.assertFalse(_matches_condition("os", None))
class TestCompareVersions(unittest.TestCase):
"""Test cases for _compare_versions function."""
def test_basic_version_comparison(self):
"""Test basic semantic version comparison."""
# Greater than
self.assertTrue(_compare_versions("2.0.0", "1.0.0", ">="))
self.assertTrue(_compare_versions("1.1.0", "1.0.0", ">"))
# Less than
self.assertTrue(_compare_versions("1.0.0", "2.0.0", "<="))
self.assertTrue(_compare_versions("1.0.0", "1.1.0", "<"))
# Equal
self.assertTrue(_compare_versions("1.0.0", "1.0.0", "=="))
self.assertTrue(_compare_versions("1.0.0", "1.0.0", ">="))
self.assertTrue(_compare_versions("1.0.0", "1.0.0", "<="))
def test_beta_version_comparison(self):
"""Test comparison with beta versions."""
# Beta vs beta
self.assertTrue(_compare_versions("1.0.0b2", "1.0.0b1", ">"))
self.assertTrue(_compare_versions("1.0.0b1", "1.0.0b2", "<"))
# Beta vs release
self.assertTrue(_compare_versions("1.0.0", "1.0.0b1", ">"))
self.assertTrue(_compare_versions("1.0.0b1", "1.0.0", "<"))
def test_invalid_version_fallback(self):
"""Test fallback to string comparison for invalid versions."""
# Should fall back to string comparison
self.assertTrue(_compare_versions("invalid", "invalid", "=="))
self.assertTrue(_compare_versions("z", "a", ">"))
class TestParseVersionWithBeta(unittest.TestCase):
"""Test cases for _parse_version_with_beta function."""
def test_release_version(self):
"""Test parsing release version."""
result = _parse_version_with_beta("1.2.3")
self.assertEqual(result, (1, 2, 3, float('inf')))
def test_beta_version(self):
"""Test parsing beta version."""
result = _parse_version_with_beta("1.2.3b10")
self.assertEqual(result, (1, 2, 3, 10))
def test_beta_version_no_number(self):
"""Test parsing beta version without number."""
result = _parse_version_with_beta("1.2.3b")
self.assertEqual(result, (1, 2, 3, 0))
class TestOneSettingsResponseErrorHandling(unittest.TestCase):
"""Test cases specifically for OneSettingsResponse error handling scenarios."""
def test_response_with_timeout_only(self):
"""Test response that indicates timeout but not general exception."""
# This scenario shouldn't normally happen but test for completeness
response = OneSettingsResponse(
has_exception=False,
status_code=408
)
self.assertFalse(response.has_exception)
self.assertEqual(response.status_code, 408)
def test_response_error_combinations(self):
"""Test various combinations of error indicators."""
test_cases = [
# (has_timeout, has_exception, status_code, description)
(True, True, 200, "timeout with exception"),
(False, True, 500, "exception without timeout"),
(True, False, 408, "timeout without exception flag"),
(False, False, 429, "no error flags but error status"),
]
for has_timeout, has_exception, status_code, description in test_cases:
with self.subTest(description=description):
response = OneSettingsResponse(
has_exception=has_exception,
status_code=status_code
)
self.assertEqual(response.has_exception, has_exception)
self.assertEqual(response.status_code, status_code)
class TestFeatureEvaluationIntegration(unittest.TestCase):
"""Integration tests for complete feature evaluation scenarios."""
def setUp(self):
"""Set up test configuration profile."""
_ConfigurationProfile.os = "w"
_ConfigurationProfile.rp = "f"
_ConfigurationProfile.attach = "m"
_ConfigurationProfile.version = "1.0.0b20"
_ConfigurationProfile.component = "ext"
_ConfigurationProfile.region = "westus"
def tearDown(self):
"""Reset profile after each test."""
_ConfigurationProfile.os = ""
_ConfigurationProfile.rp = ""
_ConfigurationProfile.attach = ""
_ConfigurationProfile.version = ""
_ConfigurationProfile.component = ""
_ConfigurationProfile.region = ""
def test_complex_feature_evaluation(self):
"""Test complex feature evaluation with multiple conditions."""
settings = {
"live_metrics": {
"default": "disabled",
"override": [
{"os": "w"}, # This should match
{"os": "l", "ver": {"min": "1.0.0b20"}},
{"component": "dst", "rp": "f"}
]
},
"sampling": {
"default": "enabled",
"override": [
{"os": ["w", "l"]}, # This should match and disable
]
},
"profiling": {
"default": "disabled",
"override": [
{"os": "w", "ver": {"min": "2.0.0", "max": "3.0.0"}}, # Version doesn't match
{"component": "ext", "rp": ["f", "a"], "region": ["westus", "eastus"]} # All match
]
}
}
# live_metrics: disabled by default, but Windows override matches
self.assertTrue(evaluate_feature("live_metrics", settings))
# sampling: enabled by default, but OS override matches to disable
self.assertFalse(evaluate_feature("sampling", settings))
# profiling: disabled by default, second override matches to enable
self.assertTrue(evaluate_feature("profiling", settings))
if __name__ == '__main__':
unittest.main()
|