File: conftest.py

package info (click to toggle)
python-azure 20250829%2Bgit-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 756,824 kB
  • sloc: python: 6,224,989; ansic: 804; javascript: 287; makefile: 198; sh: 195; xml: 109
file content (198 lines) | stat: -rw-r--r-- 8,322 bytes parent folder | download
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
# pylint: disable=line-too-long,useless-suppression
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import pytest
from devtools_testutils import (
    add_general_regex_sanitizer,
    add_body_key_sanitizer,
    remove_batch_sanitizers,
    get_credential,
    test_proxy,
)
from azure.ai.agents import AgentsClient
from dotenv import load_dotenv, find_dotenv

if not load_dotenv(find_dotenv(filename="azure_ai_agents_tests.env"), override=True):
    print("Failed to apply environment variables for azure-ai-projects tests.")


class SanitizedValues:
    SUBSCRIPTION_ID = "00000000-0000-0000-0000-000000000000"
    RESOURCE_GROUP_NAME = "00000"
    WORKSPACE_NAME = "00000"
    DATASET_NAME = "00000"
    TENANT_ID = "00000000-0000-0000-0000-000000000000"
    USER_OBJECT_ID = "00000000-0000-0000-0000-000000000000"
    API_KEY = "00000000000000000000000000000000000000000000000000000000000000000000"
    VECTOR_STORE_NAME = "vs_000000000000000000000000"
    # cSpell:disable-next-line
    FILE_BATCH = "vsfb_00000000000000000000000000000000"


@pytest.fixture(scope="session")
def mock_project_scope():
    return {
        "subscription_id": f"{SanitizedValues.SUBSCRIPTION_ID}",
        "resource_group_name": f"{SanitizedValues.RESOURCE_GROUP_NAME}",
        "project_name": f"{SanitizedValues.WORKSPACE_NAME}",
    }


@pytest.fixture(scope="session")
def mock_dataset_name():
    return {
        "dataset_name": f"{SanitizedValues.DATASET_NAME}",
    }


@pytest.fixture(scope="session")
def mock_vector_store_name():
    return {
        "vector_store_name": f"{SanitizedValues.VECTOR_STORE_NAME}",
        "file_batches": f"{SanitizedValues.FILE_BATCH}",
    }


# autouse=True will trigger this fixture on each pytest run, even if it's not explicitly used by a test method
@pytest.fixture(scope="session", autouse=True)
def start_proxy(test_proxy):
    return


@pytest.fixture(scope="session", autouse=True)
def add_sanitizers(test_proxy, mock_project_scope, mock_dataset_name, mock_vector_store_name):

    def azure_workspace_triad_sanitizer():
        """Sanitize subscription, resource group, and workspace."""

        add_general_regex_sanitizer(
            regex=r"/subscriptions/([-\w\._\(\)]+)",
            value=mock_project_scope["subscription_id"],
            group_for_replace="1",
        )

        add_general_regex_sanitizer(
            regex=r"/resource[gG]roups/([-\w\._\(\)]+)",
            value=mock_project_scope["resource_group_name"],
            group_for_replace="1",
        )

        add_general_regex_sanitizer(
            regex=r"/workspaces/([-\w\._\(\)]+)", value=mock_project_scope["project_name"], group_for_replace="1"
        )

        # TODO (Darren): Check why this is needed in addition to the above
        add_general_regex_sanitizer(
            regex=r"%2Fsubscriptions%2F([-\w\._\(\)]+)",
            value=mock_project_scope["subscription_id"],
            group_for_replace="1",
        )

        # TODO (Darren): Check why this is needed in addition to the above
        add_general_regex_sanitizer(
            regex=r"%2Fresource[gG]roups%2F([-\w\._\(\)]+)",
            value=mock_project_scope["resource_group_name"],
            group_for_replace="1",
        )

        add_general_regex_sanitizer(
            regex=r"api/projects/([-\w\._\(\)]+)",
            value=mock_project_scope["project_name"],
            group_for_replace="1",
        )

    azure_workspace_triad_sanitizer()

    add_general_regex_sanitizer(regex=r"/runs/([-\w\._\(\)]+)", value="Sanitized", group_for_replace="1")

    add_general_regex_sanitizer(
        regex=r"/data/([-\w\._\(\)]+)", value=mock_dataset_name["dataset_name"], group_for_replace="1"
    )

    add_general_regex_sanitizer(
        regex=r"/vector_stores/([-\w\._\(\)]+)",
        value=mock_vector_store_name["vector_store_name"],
        group_for_replace="1",
    )

    add_general_regex_sanitizer(
        regex=r"/file_batches/([-\w\._\(\)]+)/", value=mock_vector_store_name["file_batches"], group_for_replace="1"
    )

    # Sanitize Application Insights connection string from service response (/tests/telemetry)
    add_body_key_sanitizer(
        json_path="properties.ConnectionString",
        value="InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://region.applicationinsights.azure.com/;LiveEndpoint=https://region.livediagnostics.monitor.azure.com/;ApplicationId=00000000-0000-0000-0000-000000000000",
    )

    add_body_key_sanitizer(
        json_path="data_sources[*].uri",
        value="azureml://subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/00000/workspaces/00000/datastores/workspaceblobstore/paths/LocalUpload/00000000000/product_info_1.md",
    )

    add_body_key_sanitizer(
        json_path="configuration.data_sources[*].uri",
        value="azureml://subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/00000/workspaces/00000/datastores/workspaceblobstore/paths/LocalUpload/00000000000/product_info_1.md",
    )

    add_body_key_sanitizer(
        json_path="data_source.uri",
        value="azureml://subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/00000/workspaces/00000/datastores/workspaceblobstore/paths/LocalUpload/00000000000/product_info_1.md",
    )

    add_body_key_sanitizer(
        json_path="tool_resources.azure_ai_search.indexes[*].index_connection_id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.MachineLearningServices/workspaces/00000/connections/someindex",
    )

    # Sanitize the plain bing grounding.
    add_body_key_sanitizer(
        json_path="tools[*].bing_grounding.search_configurations[*].connection_id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.CognitiveServices/accounts/00000/projects/00000/connections/00000",
    )

    # Sanitize the custom bing grounding.
    add_body_key_sanitizer(
        json_path="tools[*].bing_custom_search.search_configurations[*].connection_id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.CognitiveServices/accounts/00000/projects/00000/connections/00000",
    )

    # Sanitize deep research tool bing connection ID
    add_body_key_sanitizer(
        json_path="tools[*].deep_research.bing_grounding_connections[*].connection_id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.CognitiveServices/accounts/00000/projects/00000/connections/00000",
    )

    # Sanitize Browser Automation tool's Playwright Workspace connection ID
    add_body_key_sanitizer(
        json_path="tools[*].browser_automation.connection.id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.CognitiveServices/accounts/00000/projects/00000/connections/00000",
    )

    # Sanitize Fabric data agent connection
    add_body_key_sanitizer(
        json_path="tools[*].fabric_dataagent.connections[*].connection_id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.CognitiveServices/accounts/00000/projects/00000/connections/00000",
    )

    # Sanitize SharePoint connection
    add_body_key_sanitizer(
        json_path="tools[*].sharepoint_grounding.connections[*].connection_id",
        value="/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/00000/providers/Microsoft.CognitiveServices/accounts/00000/projects/00000/connections/00000",
    )

    add_body_key_sanitizer(
        json_path="tool_resources.code_interpreter.data_sources[*].uri",
        value="azureai://accounts/0000-0000/projects/0000000/data/00000/versions/1.0",
    )

    # Sanitize API key from service response (/tests/connections)
    add_body_key_sanitizer(json_path="properties.credentials.key", value="Sanitized")

    # Remove the following sanitizers since certain fields are needed in tests and are non-sensitive:
    #  - AZSDK3493: $..name
    #  - AZSDK3430: $..id
    remove_batch_sanitizers(["AZSDK3493"])
    remove_batch_sanitizers(["AZSDK3430"])