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' )
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_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' )
# [END howto_operator_gcf_default_args] # [START howto_operator_gcf_deploy_variants] if SOURCE_ARCHIVE_URL: body['sourceArchiveUrl'] = SOURCE_ARCHIVE_URL elif SOURCE_REPOSITORY: body['sourceRepository'] = {'url': SOURCE_REPOSITORY} elif ZIP_PATH: body['sourceUploadUrl'] = '' default_args['zip_path'] = ZIP_PATH elif SOURCE_UPLOAD_URL: body['sourceUploadUrl'] = 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_deploy_delete', default_args=default_args, schedule_interval=datetime.timedelta(days=1)) as dag: # [START howto_operator_gcf_deploy] deploy_task = GcfFunctionDeployOperator(task_id="gcf_deploy_task", name=FUNCTION_NAME, project_id=PROJECT_ID, location=LOCATION, body=body, validate_body=VALIDATE_BODY) # [END howto_operator_gcf_deploy] delete_task = GcfFunctionDeleteOperator(task_id="gcf_delete_task", name=FUNCTION_NAME) deploy_task >> delete_task