コード例 #1
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_update_function_if_exists(self, mock_hook):
     mock_hook.return_value.get_function.return_value = True
     mock_hook.return_value.update_function.return_value = True
     op = CloudFunctionDeployFunctionOperator(project_id=GCP_PROJECT_ID,
                                              location=GCP_LOCATION,
                                              body=deepcopy(VALID_BODY),
                                              task_id="id")
     op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1',
         gcp_conn_id='google_cloud_default',
         impersonation_chain=None,
     )
     mock_hook.return_value.get_function.assert_called_once_with(
         'projects/test_project_id/locations/test_region/functions/helloWorld'
     )
     expected_body = deepcopy(VALID_BODY)
     expected_body['labels'] = {
         'airflow-version':
         'v' + version.replace('.', '-').replace('+', '-')
     }
     mock_hook.return_value.update_function.assert_called_once_with(
         'projects/test_project_id/locations/test_region/functions/helloWorld',
         expected_body,
         expected_body.keys(),
     )
     mock_hook.return_value.create_new_function.assert_not_called()
コード例 #2
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_deploy_execute(self, mock_hook):
     mock_hook.return_value.get_function.side_effect = mock.Mock(
         side_effect=HttpError(resp=MOCK_RESP_404, content=b'not found'))
     mock_hook.return_value.create_new_function.return_value = True
     op = CloudFunctionDeployFunctionOperator(project_id=GCP_PROJECT_ID,
                                              location=GCP_LOCATION,
                                              body=deepcopy(VALID_BODY),
                                              task_id="id")
     op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1',
         gcp_conn_id='google_cloud_default',
         impersonation_chain=None,
     )
     mock_hook.return_value.get_function.assert_called_once_with(
         'projects/test_project_id/locations/test_region/functions/helloWorld'
     )
     expected_body = deepcopy(VALID_BODY)
     expected_body['labels'] = {
         'airflow-version':
         'v' + version.replace('.', '-').replace('+', '-')
     }
     mock_hook.return_value.create_new_function.assert_called_once_with(
         project_id='test_project_id',
         location='test_region',
         body=expected_body)
コード例 #3
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_valid_trigger_union_field(self, trigger, mock_hook):
     mock_hook.return_value.upload_function_zip.return_value = 'https://uploadUrl'
     mock_hook.return_value.get_function.side_effect = mock.Mock(
         side_effect=HttpError(resp=MOCK_RESP_404, content=b'not found'))
     mock_hook.return_value.create_new_function.return_value = True
     body = deepcopy(VALID_BODY)
     body.pop('httpsTrigger', None)
     body.pop('eventTrigger', None)
     body.update(trigger)
     op = CloudFunctionDeployFunctionOperator(
         project_id="test_project_id",
         location="test_region",
         body=body,
         task_id="id",
     )
     op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1',
         gcp_conn_id='google_cloud_default',
         impersonation_chain=None,
     )
     mock_hook.return_value.get_function.assert_called_once_with(
         'projects/test_project_id/locations/test_region/functions/helloWorld'
     )
     mock_hook.return_value.create_new_function.assert_called_once_with(
         project_id='test_project_id', location='test_region', body=body)
     mock_hook.reset_mock()
コード例 #4
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_body_empty_or_missing_fields(self, body, message, mock_hook):
     mock_hook.return_value.upload_function_zip.return_value = 'https://uploadUrl'
     with pytest.raises(AirflowException) as ctx:
         op = CloudFunctionDeployFunctionOperator(
             project_id="test_project_id",
             location="test_region",
             body=body,
             task_id="id")
         op.execute(None)
     err = ctx.value
     assert message in str(err)
コード例 #5
0
 def test_extra_parameter(self, mock_hook):
     mock_hook.return_value.create_new_function.return_value = True
     body = deepcopy(VALID_BODY)
     body['extra_parameter'] = 'extra'
     op = CloudFunctionDeployFunctionOperator(project_id="test_project_id",
                                              location="test_region",
                                              body=body,
                                              task_id="id")
     op.execute(None)
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     mock_hook.reset_mock()
コード例 #6
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_valid_labels_field(self, labels, mock_hook):
     mock_hook.return_value.create_new_function.return_value = True
     body = deepcopy(VALID_BODY)
     body['labels'] = labels
     op = CloudFunctionDeployFunctionOperator(project_id="test_project_id",
                                              location="test_region",
                                              body=body,
                                              task_id="id")
     op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1',
         gcp_conn_id='google_cloud_default',
         impersonation_chain=None,
     )
     mock_hook.reset_mock()
コード例 #7
0
 def test_validation_disabled(self, mock_hook):
     mock_hook.return_value.create_new_function.return_value = True
     body = {
         "name": "function_name",
         "some_invalid_body_field": "some_invalid_body_field_value"
     }
     op = CloudFunctionDeployFunctionOperator(project_id="test_project_id",
                                              location="test_region",
                                              body=body,
                                              validate_body=False,
                                              task_id="id")
     op.execute(None)
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     mock_hook.reset_mock()
コード例 #8
0
 def test_empty_project_id_is_ok(self, mock_hook):
     mock_hook.return_value.get_function.side_effect = \
         HttpError(resp=MOCK_RESP_404, content=b'not found')
     operator = CloudFunctionDeployFunctionOperator(
         location="test_region", body=deepcopy(VALID_BODY), task_id="id")
     operator.execute(None)
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     new_body = deepcopy(VALID_BODY)
     new_body['labels'] = {
         'airflow-version':
         'v' + version.replace('.', '-').replace('+', '-')
     }
     mock_hook.return_value.create_new_function.assert_called_once_with(
         project_id=None, location="test_region", body=new_body)
コード例 #9
0
 def test_invalid_source_code_union_field(self, source_code, message):
     body = deepcopy(VALID_BODY)
     body.pop('sourceUploadUrl', None)
     body.pop('sourceArchiveUrl', None)
     zip_path = source_code.pop('zip_path', None)
     body.update(source_code)
     with self.assertRaises(AirflowException) as cm:
         op = CloudFunctionDeployFunctionOperator(
             project_id="test_project_id",
             location="test_region",
             body=body,
             task_id="id",
             zip_path=zip_path)
         op.execute(None)
     err = cm.exception
     self.assertIn(message, str(err))
コード例 #10
0
 def test_invalid_field_values(self, key, value, message, mock_hook):
     mock_hook.return_value.create_new_function.return_value = True
     body = deepcopy(VALID_BODY)
     body[key] = value
     with self.assertRaises(AirflowException) as cm:
         op = CloudFunctionDeployFunctionOperator(
             project_id="test_project_id",
             location="test_region",
             body=body,
             task_id="id")
         op.execute(None)
     err = cm.exception
     self.assertIn(message, str(err))
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     mock_hook.reset_mock()
コード例 #11
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_empty_body(self, mock_hook):
     with pytest.raises(AirflowException) as ctx:
         CloudFunctionDeployFunctionOperator(project_id="test_project_id",
                                             location="test_region",
                                             body=None,
                                             task_id="id")
     err = ctx.value
     assert "The required parameter 'body' is missing" in str(err)
コード例 #12
0
 def test_empty_body(self, mock_hook):
     with self.assertRaises(AirflowException) as cm:
         CloudFunctionDeployFunctionOperator(project_id="test_project_id",
                                             location="test_region",
                                             body=None,
                                             task_id="id")
     err = cm.exception
     self.assertIn("The required parameter 'body' is missing", str(err))
コード例 #13
0
 def test_body_validation_simple(self, mock_hook):
     mock_hook.return_value.create_new_function.return_value = True
     body = deepcopy(VALID_BODY)
     body['name'] = ''
     with self.assertRaises(AirflowException) as cm:
         op = CloudFunctionDeployFunctionOperator(
             project_id="test_project_id",
             location="test_region",
             body=body,
             task_id="id")
         op.execute(None)
     err = cm.exception
     self.assertIn("The body field 'name' of value '' does not match",
                   str(err))
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     mock_hook.reset_mock()
コード例 #14
0
 def test_invalid_trigger_union_field(self, trigger, message, mock_hook):
     mock_hook.return_value.upload_function_zip.return_value = 'https://uploadUrl'
     body = deepcopy(VALID_BODY)
     body.pop('httpsTrigger', None)
     body.pop('eventTrigger', None)
     body.update(trigger)
     with self.assertRaises(AirflowException) as cm:
         op = CloudFunctionDeployFunctionOperator(
             project_id="test_project_id",
             location="test_region",
             body=body,
             task_id="id",
         )
         op.execute(None)
     err = cm.exception
     self.assertIn(message, str(err))
     mock_hook.assert_called_once_with(api_version='v1',
                                       gcp_conn_id='google_cloud_default')
     mock_hook.reset_mock()
コード例 #15
0
ファイル: test_functions.py プロジェクト: ysktir/airflow-1
 def test_valid_source_code_union_field(self, source_code, project_id,
                                        mock_hook):
     mock_hook.return_value.upload_function_zip.return_value = 'https://uploadUrl'
     mock_hook.return_value.get_function.side_effect = mock.Mock(
         side_effect=HttpError(resp=MOCK_RESP_404, content=b'not found'))
     mock_hook.return_value.create_new_function.return_value = True
     body = deepcopy(VALID_BODY)
     body.pop('sourceUploadUrl', None)
     body.pop('sourceArchiveUrl', None)
     body.pop('sourceRepository', None)
     body.pop('sourceRepositoryUrl', None)
     zip_path = source_code.pop('zip_path', None)
     body.update(source_code)
     if project_id:
         op = CloudFunctionDeployFunctionOperator(
             project_id="test_project_id",
             location="test_region",
             body=body,
             task_id="id",
             zip_path=zip_path,
         )
     else:
         op = CloudFunctionDeployFunctionOperator(location="test_region",
                                                  body=body,
                                                  task_id="id",
                                                  zip_path=zip_path)
     op.execute(None)
     mock_hook.assert_called_once_with(
         api_version='v1',
         gcp_conn_id='google_cloud_default',
         impersonation_chain=None,
     )
     if zip_path:
         mock_hook.return_value.upload_function_zip.assert_called_once_with(
             project_id=project_id,
             location='test_region',
             zip_path='/path/to/file')
     mock_hook.return_value.get_function.assert_called_once_with(
         'projects/test_project_id/locations/test_region/functions/helloWorld'
     )
     mock_hook.return_value.create_new_function.assert_called_once_with(
         project_id=project_id, location='test_region', body=body)
     mock_hook.reset_mock()
コード例 #16
0
# [END howto_operator_gcf_deploy_variants]


with models.DAG(
    'example_gcp_function',
    default_args=default_args,
    schedule_interval='@once',  # Override to match your needs
    start_date=datetime(2021, 1, 1),
    catchup=False,
    tags=['example'],
) as dag:
    # [START howto_operator_gcf_deploy]
    deploy_task = CloudFunctionDeployFunctionOperator(
        task_id="gcf_deploy_task",
        project_id=GCP_PROJECT_ID,
        location=GCP_LOCATION,
        body=body,
        validate_body=GCP_VALIDATE_BODY,
    )
    # [END howto_operator_gcf_deploy]
    # [START howto_operator_gcf_deploy_no_project_id]
    deploy2_task = CloudFunctionDeployFunctionOperator(
        task_id="gcf_deploy2_task", location=GCP_LOCATION, body=body, validate_body=GCP_VALIDATE_BODY
    )
    # [END howto_operator_gcf_deploy_no_project_id]
    # [START howto_operator_gcf_invoke_function]
    invoke_task = CloudFunctionInvokeFunctionOperator(
        task_id="invoke_task",
        project_id=GCP_PROJECT_ID,
        location=GCP_LOCATION,
        input_data={},