def test_empty_name(self, mock_hook): mock_hook.return_value.delete_function.return_value = \ self._DELETE_FUNCTION_EXPECTED with self.assertRaises(AttributeError) as cm: GcfFunctionDeleteOperator(name="", task_id="id") err = cm.exception self.assertEqual(str(err), 'Empty parameter: name') mock_hook.assert_not_called()
def test_correct_name(self, mock_hook): op = GcfFunctionDeleteOperator( name="projects/project_name/locations/project_location/functions" "/function_name", task_id="id") op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default')
def test_invalid_name(self, mock_hook): with self.assertRaises(AttributeError) as cm: op = GcfFunctionDeleteOperator(name="invalid_name", task_id="id") op.execute(None) err = cm.exception self.assertEqual( str(err), 'Parameter name must match pattern: {}'.format( FUNCTION_NAME_PATTERN)) mock_hook.assert_not_called()
def test_gcf_error_silenced_when_function_doesnt_exist(self, mock_hook): op = GcfFunctionDeleteOperator(name=self._FUNCTION_NAME, task_id="id") mock_hook.return_value.delete_function.side_effect = mock.Mock( side_effect=HttpError(resp=MOCK_RESP_404, content=b'not found')) op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') mock_hook.return_value.delete_function.assert_called_once_with( 'projects/project_name/locations/project_location/functions/function_name' )
def test_delete_execute(self, mock_hook): mock_hook.return_value.delete_function.return_value = \ self._DELETE_FUNCTION_EXPECTED op = GcfFunctionDeleteOperator(name=self._FUNCTION_NAME, task_id="id") result = op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') mock_hook.return_value.delete_function.assert_called_once_with( 'projects/project_name/locations/project_location/functions/function_name' ) self.assertEqual(result['name'], self._FUNCTION_NAME)
def test_non_404_gcf_error_bubbled_up(self, mock_hook): op = GcfFunctionDeleteOperator(name=self._FUNCTION_NAME, task_id="id") resp = type('', (object, ), {"status": 500})() mock_hook.return_value.delete_function.side_effect = mock.Mock( side_effect=HttpError(resp=resp, content=b'error')) with self.assertRaises(HttpError): op.execute(None) mock_hook.assert_called_once_with(api_version='v1', gcp_conn_id='google_cloud_default') mock_hook.return_value.delete_function.assert_called_once_with( 'projects/project_name/locations/project_location/functions/function_name' )
default_args['zip_path'] = GCF_ZIP_PATH elif GCF_SOURCE_UPLOAD_URL: body['sourceUploadUrl'] = GCF_SOURCE_UPLOAD_URL else: raise Exception("Please provide one of the source_code parameters") # [END howto_operator_gcf_deploy_variants] with models.DAG( 'example_gcp_function', default_args=default_args, schedule_interval=None # Override to match your needs ) as dag: # [START howto_operator_gcf_deploy] deploy_task = GcfFunctionDeployOperator(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 = GcfFunctionDeployOperator(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_delete] delete_task = GcfFunctionDeleteOperator(task_id="gcf_delete_task", name=FUNCTION_NAME) # [END howto_operator_gcf_delete] deploy_task >> deploy2_task >> delete_task