From b886fee3e387f3330d6036c4b35310f3f327c575 Mon Sep 17 00:00:00 2001 From: Aasav Badera Date: Thu, 5 Nov 2026 12:50:21 +0530 Subject: [PATCH] test: replace statusreturner with local HTTP fixtures Share a local redirect server between synchronous and asynchronous client tests, use dummy credentials, and remove live Function App E2E references. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 40145fed-85f7-45a5-814a-2beea3c6a507 --- .../tests/aio/test_kusto_client.py | 16 +++++- azure-kusto-data/tests/conftest.py | 38 +++++++++++++ azure-kusto-data/tests/test_e2e_data.py | 54 +------------------ azure-kusto-data/tests/test_kusto_client.py | 11 ++++ 4 files changed, 65 insertions(+), 54 deletions(-) diff --git a/azure-kusto-data/tests/aio/test_kusto_client.py b/azure-kusto-data/tests/aio/test_kusto_client.py index 1bf7fc6d7..2c928bff5 100644 --- a/azure-kusto-data/tests/aio/test_kusto_client.py +++ b/azure-kusto-data/tests/aio/test_kusto_client.py @@ -4,11 +4,13 @@ from unittest.mock import patch import pytest +from aiohttp import ClientResponse +from azure.kusto.data import KustoConnectionStringBuilder from azure.kusto.data._cloud_settings import CloudSettings from azure.kusto.data._decorators import aio_documented_by from azure.kusto.data.client_request_properties import ClientRequestProperties -from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError +from azure.kusto.data.exceptions import KustoClosedError, KustoMultiApiError, KustoNetworkError, KustoServiceError from azure.kusto.data.helpers import dataframe_from_result_table from ..kusto_client_common import KustoClientTestsMixin, mocked_requests_post from ..test_kusto_client import TestKustoClient as KustoClientTestsSync @@ -18,6 +20,18 @@ @aio_documented_by(KustoClientTestsSync) class TestKustoClient(KustoClientTestsMixin): + @aio_documented_by(KustoClientTestsSync.test_no_redirects) + @pytest.mark.asyncio + async def test_no_redirects(self, redirect_server: tuple[str, int, list[str]]) -> None: + cluster, code, requests_seen = redirect_server + kcsb = KustoConnectionStringBuilder.with_aad_user_token_authentication(cluster, "test-token") + async with KustoClient(kcsb) as client: + with pytest.raises(KustoServiceError) as error: + _ = await client.execute("db", "table") + assert isinstance(error.value.http_response, ClientResponse) + assert error.value.http_response.status == code + assert requests_seen == ["/v2/rest/query"] + @staticmethod def _mock_callback(url, **kwargs): body = json.dumps(mocked_requests_post(str(url), **kwargs).json()) diff --git a/azure-kusto-data/tests/conftest.py b/azure-kusto-data/tests/conftest.py index 3c5eb5f16..027f07ad3 100644 --- a/azure-kusto-data/tests/conftest.py +++ b/azure-kusto-data/tests/conftest.py @@ -1,12 +1,50 @@ import asyncio +from collections.abc import Iterator +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from threading import Thread from typing import Tuple import pytest from azure.kusto.data import KustoConnectionStringBuilder +from azure.kusto.data._cloud_settings import CloudSettings from .kusto_client_common import KustoClientTestsMixin +@pytest.fixture(params=[301, 302, 307, 308]) +def redirect_server(request: pytest.FixtureRequest, monkeypatch: pytest.MonkeyPatch) -> Iterator[tuple[str, int, list[str]]]: + code: int = request.param + requests_seen: list[str] = [] + + class RedirectHandler(BaseHTTPRequestHandler): + def do_GET(self) -> None: + self._respond() + + def do_POST(self) -> None: + self._respond() + + def _respond(self) -> None: + requests_seen.append(self.path) + self.send_response(200 if self.path == "/redirected" else code) + if self.path != "/redirected": + self.send_header("Location", "/redirected") + self.send_header("Content-Length", "0") + self.end_headers() + + with ThreadingHTTPServer(("127.0.0.1", 0), RedirectHandler) as server: + cluster = f"http://127.0.0.1:{server.server_port}" + monkeypatch.setattr(CloudSettings, "_cloud_cache", {cluster: CloudSettings.DEFAULT_CLOUD}) + monkeypatch.setenv("NO_PROXY", "127.0.0.1") + monkeypatch.setenv("no_proxy", "127.0.0.1") + thread = Thread(target=server.serve_forever, daemon=True) + thread.start() + try: + yield cluster, code, requests_seen + finally: + server.shutdown() + thread.join() + + @pytest.fixture( params=[ "user_password", diff --git a/azure-kusto-data/tests/test_e2e_data.py b/azure-kusto-data/tests/test_e2e_data.py index 2076eec34..d7b55230c 100644 --- a/azure-kusto-data/tests/test_e2e_data.py +++ b/azure-kusto-data/tests/test_e2e_data.py @@ -3,7 +3,6 @@ import asyncio import csv -import dataclasses import json import os import platform @@ -13,15 +12,13 @@ from urllib.parse import urljoin import pytest -from requests import Response from azure.identity import DefaultAzureCredential from azure.kusto.data import KustoClient, KustoConnectionStringBuilder -from azure.kusto.data._cloud_settings import CloudSettings, DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID +from azure.kusto.data._cloud_settings import CloudSettings from azure.kusto.data._models import WellKnownDataSet from azure.kusto.data.aio import KustoClient as AsyncKustoClient from azure.kusto.data.env_utils import get_env, set_env, prepare_app_key_auth from azure.kusto.data.exceptions import KustoServiceError -from azure.kusto.data.kusto_trusted_endpoints import MatchRule, well_known_kusto_endpoints from azure.kusto.data.streaming_response import FrameType @@ -337,55 +334,6 @@ def test_cloud_info(self): assert cloud_info is CloudSettings.get_cloud_info_for_cluster(self.engine_cs) assert cloud_info == CloudSettings.get_cloud_info_for_cluster(urljoin(self.engine_cs, "/test1/test2/test3")) - def test_cloud_info_404(self): - pytest.skip("This test is currently wrong - until all cluster are updated to the redirect uri, this test will fail") - cloud_info = CloudSettings.get_cloud_info_for_cluster("https://statusreturner.azurewebsites.net/404/test") - default_dev_cloud = dataclasses.replace(CloudSettings.DEFAULT_CLOUD, kusto_service_resource_id=DEFAULT_DEV_KUSTO_SERVICE_RESOURCE_ID) - assert cloud_info == CloudSettings.DEFAULT_CLOUD or cloud_info == default_dev_cloud - - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - def test_no_redirects_fail_in_cloud(self, code): - pytest.skip("This test is currently not supported as it relies on the URI path witch we now ignore") - with KustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/nocloud", self.__class__.cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - client.execute("db", "table") - assert ex.value.http_response.status_code == code - - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - def test_no_redirects_fail_in_client(self, code): - well_known_kusto_endpoints.add_trusted_hosts([MatchRule("statusreturner.azurewebsites.net", False)], False) - with KustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/segment", self.__class__.cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - client.execute("db", "table") - assert isinstance(ex.value.http_response, Response) - assert ex.value.http_response.status_code == code - - @pytest.mark.asyncio - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - async def test_no_redirects_fail_in_cloud_async(self, code): - pytest.skip("This test is currently not supported as it relies on the URI path witch we now ignore") - async with AsyncKustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/nocloud", self.__class__.async_cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - await client.execute("db", "table") - assert ex.value.http_response.status_code == code - - @pytest.mark.asyncio - @pytest.mark.parametrize("code", [301, 302, 307, 308]) - async def test_no_redirects_fail_in_client_async(self, code): - well_known_kusto_endpoints.add_trusted_hosts([MatchRule("statusreturner.azurewebsites.net", False)], False) - async with AsyncKustoClient( - KustoConnectionStringBuilder.with_azure_token_credential(f"https://statusreturner.azurewebsites.net/{code}/segment", self.__class__.async_cred()) - ) as client: - with pytest.raises(KustoServiceError) as ex: - await client.execute("db", "table") - assert ex.value.http_response.status == code - def test_semantic_error(self): with self.get_client() as client: with pytest.raises(KustoServiceError) as ex: diff --git a/azure-kusto-data/tests/test_kusto_client.py b/azure-kusto-data/tests/test_kusto_client.py index 72a6a706a..2997e916b 100644 --- a/azure-kusto-data/tests/test_kusto_client.py +++ b/azure-kusto-data/tests/test_kusto_client.py @@ -5,6 +5,7 @@ import pandas import pytest +from requests import Response from azure.kusto.data import ClientRequestProperties, KustoClient, KustoConnectionStringBuilder from azure.kusto.data._cloud_settings import CloudSettings @@ -22,6 +23,16 @@ def method(request): class TestKustoClient(KustoClientTestsMixin): """Tests class for KustoClient API""" + def test_no_redirects(self, redirect_server: tuple[str, int, list[str]]) -> None: + cluster, code, requests_seen = redirect_server + kcsb = KustoConnectionStringBuilder.with_aad_user_token_authentication(cluster, "test-token") + with KustoClient(kcsb) as client: + with pytest.raises(KustoServiceError) as error: + _ = client.execute("db", "table") + assert isinstance(error.value.http_response, Response) + assert error.value.http_response.status_code == code + assert requests_seen == ["/v2/rest/query"] + def test_throws_on_close(self): """Test query V2.""" with KustoClient(self.HOST) as client: