Skip to content

Commit da5af33

Browse files
authored
Create the Athena Spark work group and results bucket in the system test (#73929)
The work group was preconfigured infrastructure, which meant the test could not run against a fresh account and two runs shared one work group. Athena creates a work group synchronously and it has no CREATING state, so the test can own it without a waiter. The results bucket is created and force-deleted by the test too, like the other AWS system tests, and is used as the work group's output location. Athena rejects a PySpark work group without an ExecutionRole, so the execution role stays preconfigured and the context now supplies only its ARN.
1 parent 867327c commit da5af33

1 file changed

Lines changed: 45 additions & 7 deletions

File tree

‎providers/amazon/tests/system/amazon/aws/example_athena_spark.py‎

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from airflow.providers.amazon.aws.hooks.athena import AthenaHook
2525
from airflow.providers.amazon.aws.operators.athena_spark import AthenaSparkOperator
26+
from airflow.providers.amazon.aws.operators.s3 import S3CreateBucketOperator, S3DeleteBucketOperator
2627

2728
from tests_common.test_utils.version_compat import AIRFLOW_V_3_0_PLUS
2829

@@ -40,15 +41,34 @@
4041
# Compatibility for Airflow < 3.1
4142
from airflow.utils.trigger_rule import TriggerRule # type: ignore[no-redef,attr-defined]
4243

43-
from system.amazon.aws.utils import SystemTestContextBuilder
44+
from system.amazon.aws.utils import ENV_ID_KEY, SystemTestContextBuilder
4445

4546
DAG_ID = "example_athena_spark"
4647

47-
# The Spark workgroup is preconfigured test infrastructure; this DAG creates only the session.
48-
# Test runners can override the default by exporting ATHENA_SPARK_WORK_GROUP.
49-
ATHENA_SPARK_WORK_GROUP_KEY = "ATHENA_SPARK_WORK_GROUP"
48+
# Athena rejects a PySpark work group without an execution role, so the role is preconfigured
49+
# test infrastructure. The results bucket and the work group are created here.
50+
EXECUTION_ROLE_ARN_KEY = "EXECUTION_ROLE_ARN"
5051

51-
sys_test_context_task = SystemTestContextBuilder().add_variable(ATHENA_SPARK_WORK_GROUP_KEY).build()
52+
sys_test_context_task = SystemTestContextBuilder().add_variable(EXECUTION_ROLE_ARN_KEY).build()
53+
54+
55+
@task
56+
def create_work_group(work_group: str, execution_role_arn: str, bucket_name: str) -> None:
57+
client = boto3.client("athena")
58+
client.create_work_group(
59+
Name=work_group,
60+
Configuration={
61+
"ExecutionRole": execution_role_arn,
62+
"ResultConfiguration": {"OutputLocation": f"s3://{bucket_name}/"},
63+
"EngineVersion": {"SelectedEngineVersion": "PySpark engine version 3"},
64+
},
65+
)
66+
67+
68+
@task(trigger_rule=TriggerRule.ALL_DONE)
69+
def delete_work_group(work_group: str) -> None:
70+
client = boto3.client("athena")
71+
client.delete_work_group(WorkGroup=work_group, RecursiveDeleteOption=True)
5272

5373

5474
@task
@@ -83,9 +103,16 @@ def stop_athena_spark_session(session_id: str) -> None:
83103
catchup=False,
84104
) as dag:
85105
test_context = sys_test_context_task()
86-
athena_spark_work_group = test_context[ATHENA_SPARK_WORK_GROUP_KEY]
106+
env_id = test_context[ENV_ID_KEY]
107+
108+
work_group = f"{env_id}-athena-spark"
109+
bucket_name = f"{env_id}-athena-spark-bucket"
110+
111+
create_bucket = S3CreateBucketOperator(task_id="create_bucket", bucket_name=bucket_name)
87112

88-
session_id = start_athena_spark_session(athena_spark_work_group)
113+
setup_work_group = create_work_group(work_group, test_context[EXECUTION_ROLE_ARN_KEY], bucket_name)
114+
115+
session_id = start_athena_spark_session(work_group)
89116
idle_session_id = wait_for_athena_spark_session(session_id)
90117

91118
# [START howto_operator_athena_spark]
@@ -100,15 +127,26 @@ def stop_athena_spark_session(session_id: str) -> None:
100127

101128
stop_session = stop_athena_spark_session(session_id)
102129

130+
delete_bucket = S3DeleteBucketOperator(
131+
task_id="delete_bucket",
132+
bucket_name=bucket_name,
133+
force_delete=True,
134+
trigger_rule=TriggerRule.ALL_DONE,
135+
)
136+
103137
chain(
104138
# TEST SETUP
105139
test_context,
140+
create_bucket,
141+
setup_work_group,
106142
session_id,
107143
idle_session_id,
108144
# TEST BODY
109145
run_spark_calculation,
110146
# TEST TEARDOWN
111147
stop_session,
148+
delete_work_group(work_group),
149+
delete_bucket,
112150
)
113151

114152
from tests_common.test_utils.watcher import watcher

0 commit comments

Comments
 (0)