|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License |
| 3 | +import pytest |
| 4 | + |
| 5 | +from azure.kusto.data._cloud_settings import CloudSettings, CloudInfo |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def clear_cache(): |
| 10 | + """Fixture to clear the CloudSettings cache before each test""" |
| 11 | + with CloudSettings._cloud_cache_lock: |
| 12 | + CloudSettings._cloud_cache.clear() |
| 13 | + yield |
| 14 | + # Clean up after test if needed |
| 15 | + with CloudSettings._cloud_cache_lock: |
| 16 | + CloudSettings._cloud_cache.clear() |
| 17 | + |
| 18 | + |
| 19 | +def test_normalize_uri_extracts_authority(): |
| 20 | + """Test that _normalize_uri extracts only the authority part (schema, host, port) from a URI.""" |
| 21 | + # Test with various URI formats |
| 22 | + test_cases = [ |
| 23 | + ("https://cluster.kusto.windows.net", "https://cluster.kusto.windows.net"), |
| 24 | + ("https://cluster.kusto.windows.net/", "https://cluster.kusto.windows.net"), |
| 25 | + ("https://cluster.kusto.windows.net/v1/rest", "https://cluster.kusto.windows.net"), |
| 26 | + ("https://cluster.kusto.windows.net:443/v1/rest", "https://cluster.kusto.windows.net:443"), |
| 27 | + ("http://localhost:8080/v1/rest/query", "http://localhost:8080"), |
| 28 | + ("https://cluster.kusto.windows.net/database", "https://cluster.kusto.windows.net"), |
| 29 | + ] |
| 30 | + |
| 31 | + for input_uri, expected_authority in test_cases: |
| 32 | + assert CloudSettings._normalize_uri(input_uri) == expected_authority |
| 33 | + |
| 34 | + |
| 35 | +def test_cloud_info_cached_by_authority(clear_cache): |
| 36 | + """Test that CloudInfo is cached by authority part of the URI (schema, host, port).""" |
| 37 | + # Create a test CloudInfo object |
| 38 | + test_cloud_info = CloudInfo( |
| 39 | + login_endpoint="https://login.test.com", |
| 40 | + login_mfa_required=False, |
| 41 | + kusto_client_app_id="test-app-id", |
| 42 | + kusto_client_redirect_uri="http://localhost/redirect", |
| 43 | + kusto_service_resource_id="https://test.kusto.windows.net", |
| 44 | + first_party_authority_url="https://login.test.com/tenant-id", |
| 45 | + ) |
| 46 | + |
| 47 | + # Add to cache with a specific URL |
| 48 | + base_url = "https://cluster.kusto.windows.net" |
| 49 | + CloudSettings.add_to_cache(base_url, test_cloud_info) |
| 50 | + |
| 51 | + # Test that it can be retrieved with different path variations but same authority |
| 52 | + variations = [ |
| 53 | + base_url + "/", |
| 54 | + base_url + "/database", |
| 55 | + base_url + "/v1/rest/query", |
| 56 | + base_url + "/some/other/path", |
| 57 | + ] |
| 58 | + |
| 59 | + for url in variations: |
| 60 | + # Use the internal _normalize_uri to get the cache key |
| 61 | + normalized_url = CloudSettings._normalize_uri(url) |
| 62 | + assert normalized_url == "https://cluster.kusto.windows.net" |
| 63 | + assert normalized_url in CloudSettings._cloud_cache |
| 64 | + |
| 65 | + # Verify the retrieved CloudInfo is the same instance |
| 66 | + retrieved_info = CloudSettings._cloud_cache[normalized_url] |
| 67 | + assert retrieved_info is test_cloud_info |
| 68 | + |
| 69 | + |
| 70 | +def test_cloud_info_cached_with_port(clear_cache): |
| 71 | + """Test that URIs with ports are cached separately from those without.""" |
| 72 | + # Create two different CloudInfo objects |
| 73 | + cloud_info_default = CloudInfo( |
| 74 | + login_endpoint="https://login.default.com", |
| 75 | + login_mfa_required=False, |
| 76 | + kusto_client_app_id="default-app-id", |
| 77 | + kusto_client_redirect_uri="http://localhost/redirect", |
| 78 | + kusto_service_resource_id="https://default.kusto.windows.net", |
| 79 | + first_party_authority_url="https://login.default.com/tenant-id", |
| 80 | + ) |
| 81 | + |
| 82 | + cloud_info_with_port = CloudInfo( |
| 83 | + login_endpoint="https://login.withport.com", |
| 84 | + login_mfa_required=True, |
| 85 | + kusto_client_app_id="port-app-id", |
| 86 | + kusto_client_redirect_uri="http://localhost/redirect", |
| 87 | + kusto_service_resource_id="https://port.kusto.windows.net", |
| 88 | + first_party_authority_url="https://login.withport.com/tenant-id", |
| 89 | + ) |
| 90 | + |
| 91 | + # Add both to cache with different authorities |
| 92 | + CloudSettings.add_to_cache("https://cluster.kusto.windows.net", cloud_info_default) |
| 93 | + CloudSettings.add_to_cache("https://cluster.kusto.windows.net:443", cloud_info_with_port) |
| 94 | + |
| 95 | + # Verify they are cached separately |
| 96 | + assert "https://cluster.kusto.windows.net" in CloudSettings._cloud_cache |
| 97 | + assert "https://cluster.kusto.windows.net:443" in CloudSettings._cloud_cache |
| 98 | + |
| 99 | + # Verify each URI gets the correct CloudInfo |
| 100 | + assert CloudSettings._cloud_cache["https://cluster.kusto.windows.net"] is cloud_info_default |
| 101 | + assert CloudSettings._cloud_cache["https://cluster.kusto.windows.net:443"] is cloud_info_with_port |
| 102 | + |
| 103 | + # Additional verification with variations |
| 104 | + assert CloudSettings._cloud_cache[CloudSettings._normalize_uri("https://cluster.kusto.windows.net/database")] is cloud_info_default |
| 105 | + assert CloudSettings._cloud_cache[CloudSettings._normalize_uri("https://cluster.kusto.windows.net:443/database")] is cloud_info_with_port |
0 commit comments