sdevani commented on a change in pull request #4792: [AIRFLOW-3659] Create Google Cloud Transfer Service Operators URL: https://github.com/apache/airflow/pull/4792#discussion_r261427718
########## File path: tests/contrib/operators/test_gcp_transfer_operator.py ########## @@ -0,0 +1,702 @@ +# -*- coding: utf-8 -*- +# +# 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. +import itertools +import unittest +from copy import deepcopy +from datetime import date, time + +from parameterized import parameterized +from botocore.credentials import Credentials + +from airflow import AirflowException, configuration +from airflow.contrib.operators.gcp_transfer_operator import ( + GcpTransferServiceOperationCancelOperator, + GcpTransferServiceOperationResumeOperator, + GcpTransferServiceOperationsListOperator, + TransferJobValidator, + TransferJobPreprocessor, + GcpTransferServiceJobCreateOperator, + GcpTransferServiceJobUpdateOperator, + GcpTransferServiceOperationGetOperator, + GcpTransferServiceOperationPauseOperator, + S3ToGoogleCloudStorageTransferOperator, + GoogleCloudStorageToGoogleCloudStorageTransferOperator, + GcpTransferServiceJobDeleteOperator, +) +from airflow.models import TaskInstance, DAG +from airflow.utils import timezone + + +try: + # noinspection PyProtectedMember + from unittest import mock +except ImportError: # pragma: no cover + try: + import mock + except ImportError: + mock = None +try: + import boto3 +except ImportError: # pragma: no cover + boto3 = None + +GCP_PROJECT_ID = 'project-id' +TASK_ID = 'task-id' + +JOB_NAME = "job-name" +OPERATION_NAME = "operation-name" +AWS_BUCKET_NAME = "aws-bucket-name" +GCS_BUCKET_NAME = "gcp-bucket-name" +DESCRIPTION = "description" + +DEFAULT_DATE = timezone.datetime(2017, 1, 1) + +FILTER = {'jobNames': [JOB_NAME]} + +AWS_ACCESS_KEY_ID = "test-key-1" +AWS_ACCESS_SECRET = "test-secret-1" +AWS_ACCESS_KEY = {'accessKeyId': AWS_ACCESS_KEY_ID, 'secretAccessKey': AWS_ACCESS_SECRET} + +NATIVE_DATE = date(2018, 10, 15) +DICT_DATE = {'day': 15, 'month': 10, 'year': 2018} +NATIVE_TIME = time(hour=11, minute=42, second=43) +DICT_TIME = {'hours': 11, 'minutes': 42, 'seconds': 43} +SCHEDULE_NATIVE = { + 'scheduleStartDate': NATIVE_DATE, + 'scheduleEndDate': NATIVE_DATE, + 'startTimeOfDay': NATIVE_TIME, +} + +SCHEDULE_DICT = { + 'scheduleStartDate': {'day': 15, 'month': 10, 'year': 2018}, + 'scheduleEndDate': {'day': 15, 'month': 10, 'year': 2018}, + 'startTimeOfDay': {'hours': 11, 'minutes': 42, 'seconds': 43}, +} + +SOURCE_AWS = {"awsS3DataSource": {"bucketName": AWS_BUCKET_NAME}} +SOURCE_GCS = {"gcsDataSource": {"bucketName": GCS_BUCKET_NAME}} +SOURCE_HTTP = {"httpDataSource": {"list_url": "http://example.com"}} + +VALID_TRANSFER_JOB_BASE = { + "name": JOB_NAME, + 'description': DESCRIPTION, + 'status': 'ENABLED', + 'schedule': SCHEDULE_DICT, + 'transferSpec': {'gcsDataSink': {'bucketName': GCS_BUCKET_NAME}}, +} +VALID_TRANSFER_JOB_GCS = deepcopy(VALID_TRANSFER_JOB_BASE) +VALID_TRANSFER_JOB_GCS['transferSpec'].update(deepcopy(SOURCE_GCS)) +VALID_TRANSFER_JOB_AWS = deepcopy(VALID_TRANSFER_JOB_BASE) +VALID_TRANSFER_JOB_AWS['transferSpec'].update(deepcopy(SOURCE_AWS)) + +VALID_TRANSFER_JOB_GCS = { + "name": JOB_NAME, + 'description': DESCRIPTION, + 'status': 'ENABLED', + 'schedule': SCHEDULE_NATIVE, + 'transferSpec': { + "gcsDataSource": {"bucketName": GCS_BUCKET_NAME}, + 'gcsDataSink': {'bucketName': GCS_BUCKET_NAME}, + }, +} + +VALID_TRANSFER_JOB_RAW = { + 'description': DESCRIPTION, + 'status': 'ENABLED', + 'schedule': SCHEDULE_DICT, + 'transferSpec': {'gcsDataSink': {'bucketName': GCS_BUCKET_NAME}}, +} + +VALID_TRANSFER_JOB_GCS_RAW = deepcopy(VALID_TRANSFER_JOB_RAW) +VALID_TRANSFER_JOB_GCS_RAW['transferSpec'].update(SOURCE_GCS) +VALID_TRANSFER_JOB_AWS_RAW = deepcopy(VALID_TRANSFER_JOB_RAW) +VALID_TRANSFER_JOB_AWS_RAW['transferSpec'].update(deepcopy(SOURCE_AWS)) +VALID_TRANSFER_JOB_AWS_RAW['transferSpec']['awsS3DataSource']['awsAccessKey'] = AWS_ACCESS_KEY + + +VALID_OPERATION = {"name": "operation-name"} + + +class TransferJobPreprocessorTest(unittest.TestCase): + def test_should_do_nothing_on_empty(self): + body = {} + TransferJobPreprocessor(body=body).process_body() + self.assertEqual(body, {}) + + @unittest.skipIf(boto3 is None, "Skipping test because boto3 is not available") + @mock.patch('airflow.contrib.operators.gcp_transfer_operator.AwsHook') + def test_should_inject_aws_credentials(self, mock_hook): + mock_hook.return_value.get_credentials.return_value = Credentials( + AWS_ACCESS_KEY_ID, AWS_ACCESS_SECRET, None + ) + + body = {'transferSpec': deepcopy(SOURCE_AWS)} + body = TransferJobPreprocessor(body=body).process_body() + self.assertEqual(body['transferSpec']['awsS3DataSource']['awsAccessKey'], AWS_ACCESS_KEY) + + @parameterized.expand([('scheduleStartDate',), ('scheduleEndDate',)]) + def test_should_format_date_from_python_to_dict(self, field_attr): + body = {'schedule': {field_attr: NATIVE_DATE}} + TransferJobPreprocessor(body=body).process_body() + self.assertEqual(body['schedule'][field_attr], DICT_DATE) + + def test_should_format_time_from_python_to_dict(self): Review comment: For each of these let's also have a test to show that a regular dict would be accepted w/o change. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
