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

[AIRFLOW-6432] Raise appropriate exception in EmrAddStepsOperator when using job_flow_name and no cluster is found #6898

6 changes: 4 additions & 2 deletions airflow/contrib/operators/emr_add_steps_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ def __init__(
self.steps = steps

def execute(self, context):
emr = EmrHook(aws_conn_id=self.aws_conn_id).get_conn()
emr_hook = EmrHook(aws_conn_id=self.aws_conn_id)

emr = emr_hook.get_conn()

job_flow_id = self.job_flow_id

if not job_flow_id:
job_flow_id = emr.get_cluster_id_by_name(self.job_flow_name, self.cluster_states)
job_flow_id = emr_hook.get_cluster_id_by_name(self.job_flow_name, self.cluster_states)

if self.do_xcom_push:
context['ti'].xcom_push(key='job_flow_id', value=job_flow_id)
Expand Down
25 changes: 14 additions & 11 deletions tests/contrib/operators/test_emr_add_steps_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,26 @@ def test_execute_returns_step_id(self):
def test_init_with_cluster_name(self):
expected_job_flow_id = 'j-1231231234'

self.emr_client_mock.get_cluster_id_by_name.return_value = expected_job_flow_id
self.emr_client_mock.add_job_flow_steps.return_value = ADD_STEPS_SUCCESS_RETURN

with patch('boto3.session.Session', self.boto3_session_mock):
operator = EmrAddStepsOperator(
task_id='test_task',
job_flow_name='test_cluster',
cluster_states=['RUNNING', 'WAITING'],
aws_conn_id='aws_default',
dag=DAG('test_dag_id', default_args=self.args)
)
with patch('airflow.contrib.hooks.emr_hook.EmrHook.get_cluster_id_by_name') \
as mock_get_cluster_id_by_name:
mock_get_cluster_id_by_name.return_value = expected_job_flow_id

operator.execute(self.mock_context)
operator = EmrAddStepsOperator(
task_id='test_task',
job_flow_name='test_cluster',
cluster_states=['RUNNING', 'WAITING'],
aws_conn_id='aws_default',
dag=DAG('test_dag_id', default_args=self.args)
)

ti = self.mock_context['ti']
operator.execute(self.mock_context)

ti.xcom_push.assert_any_call(key='job_flow_id', value=expected_job_flow_id)
ti = self.mock_context['ti']

ti.xcom_push.assert_any_call(key='job_flow_id', value=expected_job_flow_id)
ashb marked this conversation as resolved.
Show resolved Hide resolved


if __name__ == '__main__':
Expand Down