Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ it will be retrieved from the Google Cloud connection used. Both variants are sh

Also for all this action you can use operator in the deferrable mode:

.. exampleinclude:: /../../tests/system/providers/google/cloud/cloud_sql/example_cloud_sql_deferrable.py
.. exampleinclude:: /../../tests/system/providers/google/cloud/cloud_sql/example_cloud_sql.py
:language: python
:dedent: 4
:start-after: [START howto_operator_cloudsql_export_async]
Expand Down
49 changes: 34 additions & 15 deletions tests/system/providers/google/cloud/cloud_sql/example_cloud_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@
PROJECT_ID = os.environ.get("SYSTEM_TESTS_GCP_PROJECT")
DAG_ID = "cloudsql"

INSTANCE_NAME = f"{DAG_ID}-{ENV_ID}-instance"
DB_NAME = f"{DAG_ID}-{ENV_ID}-db"
INSTANCE_NAME = f"{DAG_ID}-{ENV_ID}-instance".replace("_", "-")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I know why do we need these replace("_", "-"). It seems to be {DAG_ID}-{ENV_ID}-instance on the deferrable side as well

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thre cannot be _ in the instance name I believe (and I think the change to deferrable is coming as well :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking a look on this PR!
You are right, the name of the instance is valid only if consists of dashes and letters only, in this case.
As this PR combines 2 system tests into one, the deferrable and sync mode are combined in one file, so created instance will be used in both cases (def and sync)

DB_NAME = f"{DAG_ID}-{ENV_ID}-db".replace("_", "-")

BUCKET_NAME = f"{DAG_ID}_{ENV_ID}_bucket"
FILE_NAME = f"{DAG_ID}_{ENV_ID}_exportImportTestFile"
BUCKET_NAME = f"{DAG_ID}_{ENV_ID}_bucket".replace("-", "_")
FILE_NAME = f"{DAG_ID}_{ENV_ID}_exportImportTestFile".replace("-", "_")
FILE_NAME_DEFERRABLE = f"{DAG_ID}_{ENV_ID}_def_exportImportTestFile".replace("-", "_")
FILE_URI = f"gs://{BUCKET_NAME}/{FILE_NAME}"
FILE_URI_DEFERRABLE = f"gs://{BUCKET_NAME}/{FILE_NAME_DEFERRABLE}"

FAILOVER_REPLICA_NAME = f"{INSTANCE_NAME}-failover-replica"
READ_REPLICA_NAME = f"{INSTANCE_NAME}-read-replica"
Expand Down Expand Up @@ -125,6 +127,14 @@
"offload": True,
}
}
export_body_deferrable = {
"exportContext": {
"fileType": "sql",
"uri": FILE_URI_DEFERRABLE,
"sqlExportOptions": {"schemaOnly": False},
"offload": True,
}
}
# [END howto_operator_cloudsql_export_body]
# [START howto_operator_cloudsql_import_body]
import_body = {"importContext": {"fileType": "sql", "uri": FILE_URI}}
Expand Down Expand Up @@ -210,6 +220,15 @@
)
# [END howto_operator_cloudsql_export]

# [START howto_operator_cloudsql_export_async]
sql_export_def_task = CloudSQLExportInstanceOperator(
body=export_body_deferrable,
instance=INSTANCE_NAME,
task_id="sql_export_def_task",
deferrable=True,
)
# [END howto_operator_cloudsql_export_async]

# For import to work we need to add the Cloud SQL instance's Service Account
# read access to the target GCS object.
# [START howto_operator_cloudsql_import_gcs_permissions]
Expand Down Expand Up @@ -243,10 +262,12 @@

# [START howto_operator_cloudsql_db_delete]
sql_db_delete_task = CloudSQLDeleteInstanceDatabaseOperator(
instance=INSTANCE_NAME, database=DB_NAME, task_id="sql_db_delete_task"
instance=INSTANCE_NAME,
database=DB_NAME,
task_id="sql_db_delete_task",
trigger_rule=TriggerRule.ALL_DONE,
)
# [END howto_operator_cloudsql_db_delete]
sql_db_delete_task.trigger_rule = TriggerRule.ALL_DONE

# ############################################## #
# ### INSTANCES TEAR DOWN ###################### #
Expand All @@ -256,26 +277,27 @@
sql_instance_failover_replica_delete_task = CloudSQLDeleteInstanceOperator(
instance=FAILOVER_REPLICA_NAME,
task_id="sql_instance_failover_replica_delete_task",
trigger_rule=TriggerRule.ALL_DONE,
)

sql_instance_read_replica_delete_task = CloudSQLDeleteInstanceOperator(
instance=READ_REPLICA_NAME, task_id="sql_instance_read_replica_delete_task"
instance=READ_REPLICA_NAME,
task_id="sql_instance_read_replica_delete_task",
trigger_rule=TriggerRule.ALL_DONE,
)
# [END howto_operator_cloudsql_replicas_delete]
sql_instance_failover_replica_delete_task.trigger_rule = TriggerRule.ALL_DONE
sql_instance_read_replica_delete_task.trigger_rule = TriggerRule.ALL_DONE

sql_instance_clone_delete_task = CloudSQLDeleteInstanceOperator(
instance=CLONED_INSTANCE_NAME,
task_id="sql_instance_clone_delete_task",
trigger_rule=TriggerRule.ALL_DONE,
)

# [START howto_operator_cloudsql_delete]
sql_instance_delete_task = CloudSQLDeleteInstanceOperator(
instance=INSTANCE_NAME, task_id="sql_instance_delete_task"
instance=INSTANCE_NAME, task_id="sql_instance_delete_task", trigger_rule=TriggerRule.ALL_DONE
)
# [END howto_operator_cloudsql_delete]
sql_instance_delete_task.trigger_rule = TriggerRule.ALL_DONE

delete_bucket = GCSDeleteBucketOperator(
task_id="delete_bucket", bucket_name=BUCKET_NAME, trigger_rule=TriggerRule.ALL_DONE
Expand All @@ -292,6 +314,7 @@
>> sql_db_patch_task
>> sql_gcp_add_bucket_permission_task
>> sql_export_task
>> sql_export_def_task
>> sql_gcp_add_object_permission_task
>> sql_import_task
>> sql_instance_clone
Expand All @@ -304,10 +327,6 @@
>> delete_bucket
)

# Task dependencies created via `XComArgs`:
# sql_instance_create_task >> sql_gcp_add_bucket_permission_task
# sql_instance_create_task >> sql_gcp_add_object_permission_task

# ### Everything below this line is not part of example ###
# ### Just for system tests purpose ###
from tests.system.utils.watcher import watcher
Expand Down

This file was deleted.