Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Cloud Storage Transfer Service system tests to AIP-47 #26036

Closed
Closed
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 @@ -58,7 +58,7 @@ For parameter definition, take a look at
Using the operator
""""""""""""""""""

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_cloud_storage_transfer_service_gcp.py
.. exampleinclude:: /../../tests/system/providers/google/cloud/cloud_storage_transfer_service/example_cloud_storage_transfer_service_gcp.py
:language: python
:start-after: [START howto_operator_gcp_transfer_create_job_body_gcp]
:end-before: [END howto_operator_gcp_transfer_create_job_body_gcp]
Expand Down Expand Up @@ -138,12 +138,12 @@ For parameter definition, take a look at
Using the operator
""""""""""""""""""

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_cloud_storage_transfer_service_gcp.py
.. exampleinclude:: /../../tests/system/providers/google/cloud/cloud_storage_transfer_service/example_cloud_storage_transfer_service_gcp.py
:language: python
:start-after: [START howto_operator_gcp_transfer_update_job_body]
:end-before: [END howto_operator_gcp_transfer_update_job_body]

.. exampleinclude:: /../../airflow/providers/google/cloud/example_dags/example_cloud_storage_transfer_service_gcp.py
.. exampleinclude:: /../../tests/system/providers/google/cloud/cloud_storage_transfer_service/example_cloud_storage_transfer_service_gcp.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gcp_transfer_update_job]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

with models.DAG(
DAG_ID,
schedule=None,
schedule="@once",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not related to this PR but it's a very minor fix to other system tests that wasn't worth separate PR - "None" schedule made this tests to always pass

start_date=datetime(2021, 1, 1),
catchup=False,
tags=["example", "cloud_sql"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

import os
from datetime import datetime, timedelta
from pathlib import Path

from airflow import models
from airflow.models.baseoperator import chain
from airflow.providers.google.cloud.hooks.cloud_storage_transfer_service import (
ALREADY_EXISTING_IN_SINK,
BUCKET_NAME,
Expand Down Expand Up @@ -61,17 +61,22 @@
CloudDataTransferServiceListOperationsOperator,
CloudDataTransferServiceUpdateJobOperator,
)
from airflow.providers.google.cloud.operators.gcs import GCSCreateBucketOperator, GCSDeleteBucketOperator
from airflow.providers.google.cloud.sensors.cloud_storage_transfer_service import (
CloudDataTransferServiceJobStatusSensor,
)
from airflow.providers.google.cloud.transfers.local_to_gcs import LocalFilesystemToGCSOperator
from airflow.utils.trigger_rule import TriggerRule

GCP_PROJECT_ID = os.environ.get("GCP_PROJECT_ID", "example-project")
GCP_TRANSFER_FIRST_TARGET_BUCKET = os.environ.get(
"GCP_TRANSFER_FIRST_TARGET_BUCKET", "gcp-transfer-first-target"
)
GCP_TRANSFER_SECOND_TARGET_BUCKET = os.environ.get(
"GCP_TRANSFER_SECOND_TARGET_BUCKET", "gcp-transfer-second-target"
)
ENV_ID = os.environ.get('SYSTEM_TESTS_ENV_ID')
GCP_PROJECT_ID = os.environ.get('SYSTEM_TESTS_GCP_PROJECT')
DAG_ID = "cloud_storage_transfer"

GCP_TRANSFER_FIRST_TARGET_BUCKET = f"bucket1_{DAG_ID}_{ENV_ID}"
GCP_TRANSFER_SECOND_TARGET_BUCKET = f"bucket2_{DAG_ID}_{ENV_ID}"

FILE_NAME = "text.txt"
UPLOAD_SRC = str(Path(__file__).parent / "resources" / FILE_NAME)

# [START howto_operator_gcp_transfer_create_job_body_gcp]
gcs_to_gcs_transfer_body = {
Expand Down Expand Up @@ -100,12 +105,26 @@
# [END howto_operator_gcp_transfer_update_job_body]

with models.DAG(
"example_gcp_transfer",
DAG_ID,
schedule="@once",
start_date=datetime(2021, 1, 1),
catchup=False,
tags=["example"],
tags=["example", "cloud_storage_transfer"],
) as dag:

create_bucket_1 = GCSCreateBucketOperator(
task_id="create_bucket_1", bucket_name=GCP_TRANSFER_FIRST_TARGET_BUCKET, project_id=GCP_PROJECT_ID
)
create_bucket_2 = GCSCreateBucketOperator(
task_id="create_bucket_2", bucket_name=GCP_TRANSFER_SECOND_TARGET_BUCKET, project_id=GCP_PROJECT_ID
)
upload_file = LocalFilesystemToGCSOperator(
task_id="upload_file",
src=UPLOAD_SRC,
dst=FILE_NAME,
bucket=GCP_TRANSFER_FIRST_TARGET_BUCKET,
)

create_transfer = CloudDataTransferServiceCreateJobOperator(
task_id="create_transfer", body=gcs_to_gcs_transfer_body
)
Expand Down Expand Up @@ -142,13 +161,40 @@
task_id="delete_transfer_from_gcp_job",
job_name="{{task_instance.xcom_pull('create_transfer')['name']}}",
project_id=GCP_PROJECT_ID,
trigger_rule=TriggerRule.ALL_DONE,
)

chain(
create_transfer,
wait_for_transfer,
update_transfer,
list_operations,
get_operation,
delete_transfer,
delete_bucket_1 = GCSDeleteBucketOperator(
task_id="delete_bucket_1",
bucket_name=GCP_TRANSFER_FIRST_TARGET_BUCKET,
trigger_rule=TriggerRule.ALL_DONE,
)
delete_bucket_2 = GCSDeleteBucketOperator(
task_id="delete_bucket_2",
bucket_name=GCP_TRANSFER_SECOND_TARGET_BUCKET,
trigger_rule=TriggerRule.ALL_DONE,
)

(
[create_bucket_1, create_bucket_2]
>> upload_file
>> create_transfer
>> wait_for_transfer
>> update_transfer
>> list_operations
>> get_operation
>> delete_transfer
>> [delete_bucket_1, delete_bucket_2]
)

from tests.system.utils.watcher import watcher

# This test needs watcher in order to properly mark success/failure
# when "tearDown" task with trigger rule is part of the DAG
list(dag.tasks) >> watcher()


from tests.system.utils import get_test_run # noqa: E402

# Needed to run the example DAG with pytest (see: tests/system/README.md#run_via_pytest)
test_run = get_test_run(dag)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sample text