Skip to content

Commit 220ef08

Browse files
ashbYour Name
authored andcommitted
[v2-10-test] Improve speed of tests by not creating connections at parse time (#45690)
The DAG serialization tests load all of the example and system test DAGs, and there were two places that these tests opened connections at parse time resulting in loads of extra of test time. - The SystemTestContextBuilder was trying to fetch things from SSM. This was addressed by adding a functools.cache on the function - The Bedrock example dag was setting/caching the underlying conn object globally. This was addressed by making the Airflow connection a global, rather than the Bedrock conn. This fix is not _great_, but it does massively help Before: > 111 passed, 1 warning in 439.37s (0:07:19) After: > 111 passed, 1 warning in 71.76s (0:01:11) (cherry picked from commit 102e853) Co-authored-by: Ash Berlin-Taylor <[email protected]>
1 parent 04d0381 commit 220ef08

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

tests/providers/amazon/aws/system/utils/test_helpers.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import os
2525
import sys
2626
from io import StringIO
27-
from unittest.mock import ANY, patch
27+
from unittest.mock import patch
2828

2929
import pytest
3030
from moto import mock_aws
@@ -79,8 +79,15 @@ def test_fetch_variable_success(
7979
) -> None:
8080
mock_getenv.return_value = env_value or ssm_value
8181

82-
result = utils.fetch_variable(ANY, default_value) if default_value else utils.fetch_variable(ANY_STR)
82+
utils._fetch_from_ssm.cache_clear()
8383

84+
result = (
85+
utils.fetch_variable("some_key", default_value)
86+
if default_value
87+
else utils.fetch_variable(ANY_STR)
88+
)
89+
90+
utils._fetch_from_ssm.cache_clear()
8491
assert result == expected_result
8592

8693
def test_fetch_variable_no_value_found_raises_exception(self):

tests/system/providers/amazon/aws/example_bedrock_retrieve_and_generate.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ def create_opensearch_policies(bedrock_role_arn: str, collection_name: str, poli
127127

128128
def _create_security_policy(name, policy_type, policy):
129129
try:
130-
aoss_client.create_security_policy(name=name, policy=json.dumps(policy), type=policy_type)
130+
aoss_client.conn.create_security_policy(name=name, policy=json.dumps(policy), type=policy_type)
131131
except ClientError as e:
132132
if e.response["Error"]["Code"] == "ConflictException":
133133
log.info("OpenSearch security policy %s already exists.", name)
134134
raise
135135

136136
def _create_access_policy(name, policy_type, policy):
137137
try:
138-
aoss_client.create_access_policy(name=name, policy=json.dumps(policy), type=policy_type)
138+
aoss_client.conn.create_access_policy(name=name, policy=json.dumps(policy), type=policy_type)
139139
except ClientError as e:
140140
if e.response["Error"]["Code"] == "ConflictException":
141141
log.info("OpenSearch data access policy %s already exists.", name)
@@ -204,9 +204,9 @@ def create_collection(collection_name: str):
204204
:param collection_name: The name of the Collection to create.
205205
"""
206206
log.info("\nCreating collection: %s.", collection_name)
207-
return aoss_client.create_collection(name=collection_name, type="VECTORSEARCH")["createCollectionDetail"][
208-
"id"
209-
]
207+
return aoss_client.conn.create_collection(name=collection_name, type="VECTORSEARCH")[
208+
"createCollectionDetail"
209+
]["id"]
210210

211211

212212
@task
@@ -317,7 +317,7 @@ def get_collection_arn(collection_id: str):
317317
"""
318318
return next(
319319
colxn["arn"]
320-
for colxn in aoss_client.list_collections()["collectionSummaries"]
320+
for colxn in aoss_client.conn.list_collections()["collectionSummaries"]
321321
if colxn["id"] == collection_id
322322
)
323323

@@ -336,7 +336,9 @@ def delete_data_source(knowledge_base_id: str, data_source_id: str):
336336
:param data_source_id: The unique identifier of the data source to delete.
337337
"""
338338
log.info("Deleting data source %s from Knowledge Base %s.", data_source_id, knowledge_base_id)
339-
bedrock_agent_client.delete_data_source(dataSourceId=data_source_id, knowledgeBaseId=knowledge_base_id)
339+
bedrock_agent_client.conn.delete_data_source(
340+
dataSourceId=data_source_id, knowledgeBaseId=knowledge_base_id
341+
)
340342

341343

342344
# [END howto_operator_bedrock_delete_data_source]
@@ -355,7 +357,7 @@ def delete_knowledge_base(knowledge_base_id: str):
355357
:param knowledge_base_id: The unique identifier of the knowledge base to delete.
356358
"""
357359
log.info("Deleting Knowledge Base %s.", knowledge_base_id)
358-
bedrock_agent_client.delete_knowledge_base(knowledgeBaseId=knowledge_base_id)
360+
bedrock_agent_client.conn.delete_knowledge_base(knowledgeBaseId=knowledge_base_id)
359361

360362

361363
# [END howto_operator_bedrock_delete_knowledge_base]
@@ -393,7 +395,7 @@ def delete_collection(collection_id: str):
393395
:param collection_id: ID of the collection to be indexed.
394396
"""
395397
log.info("Deleting collection %s.", collection_id)
396-
aoss_client.delete_collection(id=collection_id)
398+
aoss_client.conn.delete_collection(id=collection_id)
397399

398400

399401
@task(trigger_rule=TriggerRule.ALL_DONE)
@@ -404,26 +406,26 @@ def delete_opensearch_policies(collection_name: str):
404406
:param collection_name: All policies in the given collection name will be deleted.
405407
"""
406408

407-
access_policies = aoss_client.list_access_policies(
409+
access_policies = aoss_client.conn.list_access_policies(
408410
type="data", resource=[f"collection/{collection_name}"]
409411
)["accessPolicySummaries"]
410412
log.info("Found access policies for %s: %s", collection_name, access_policies)
411413
if not access_policies:
412414
raise Exception("No access policies found?")
413415
for policy in access_policies:
414416
log.info("Deleting access policy for %s: %s", collection_name, policy["name"])
415-
aoss_client.delete_access_policy(name=policy["name"], type="data")
417+
aoss_client.conn.delete_access_policy(name=policy["name"], type="data")
416418

417419
for policy_type in ["encryption", "network"]:
418-
policies = aoss_client.list_security_policies(
420+
policies = aoss_client.conn.list_security_policies(
419421
type=policy_type, resource=[f"collection/{collection_name}"]
420422
)["securityPolicySummaries"]
421423
if not policies:
422424
raise Exception("No security policies found?")
423425
log.info("Found %s security policies for %s: %s", policy_type, collection_name, policies)
424426
for policy in policies:
425427
log.info("Deleting %s security policy for %s: %s", policy_type, collection_name, policy["name"])
426-
aoss_client.delete_security_policy(name=policy["name"], type=policy_type)
428+
aoss_client.conn.delete_security_policy(name=policy["name"], type=policy_type)
427429

428430

429431
with DAG(
@@ -436,8 +438,8 @@ def delete_opensearch_policies(collection_name: str):
436438
test_context = sys_test_context_task()
437439
env_id = test_context["ENV_ID"]
438440

439-
aoss_client = OpenSearchServerlessHook(aws_conn_id=None).conn
440-
bedrock_agent_client = BedrockAgentHook(aws_conn_id=None).conn
441+
aoss_client = OpenSearchServerlessHook(aws_conn_id=None)
442+
bedrock_agent_client = BedrockAgentHook(aws_conn_id=None)
441443

442444
region_name = boto3.session.Session().region_name
443445

tests/system/providers/amazon/aws/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# under the License.
1717
from __future__ import annotations
1818

19+
import functools
1920
import inspect
2021
import json
2122
import logging
@@ -92,6 +93,7 @@ def _validate_env_id(env_id: str) -> str:
9293
return env_id.lower()
9394

9495

96+
@functools.cache
9597
def _fetch_from_ssm(key: str, test_name: str | None = None) -> str:
9698
"""
9799
Test values are stored in the SSM Value as a JSON-encoded dict of key/value pairs.

0 commit comments

Comments
 (0)