ashb commented on a change in pull request #6090: [AIRFLOW-5470] Add Apache 
Livy REST operator
URL: https://github.com/apache/airflow/pull/6090#discussion_r334406839
 
 

 ##########
 File path: tests/contrib/operators/test_livy_operator.py
 ##########
 @@ -0,0 +1,156 @@
+# -*- 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 unittest
+from unittest.mock import MagicMock, patch
+
+from airflow import DAG, AirflowException
+from airflow.contrib.hooks.livy_hook import BatchState, LivyHook
+from airflow.contrib.operators.livy_operator import LivyOperator
+from airflow.models import Connection
+from airflow.utils import db, timezone
+
+DEFAULT_DATE = timezone.datetime(2017, 1, 1)
+mock_livy_client = MagicMock()
+
+BATCH_ID = 100
+
+
+class TestLivyOperator(unittest.TestCase):
+    def setUp(self):
+        args = {
+            'owner': 'airflow',
+            'start_date': DEFAULT_DATE
+        }
+        self.dag = DAG('test_dag_id', default_args=args)
+        db.merge_conn(Connection(
+            conn_id='livyunittest', conn_type='livy',
+            host='localhost:8998', port='8998', schema='http'
+        ))
+
+    @patch('airflow.contrib.operators.livy_operator.LivyHook.get_batch_state')
+    def test_poll_for_termination(self, mock_livy):
+
+        state_list = 2 * [BatchState.RUNNING] + [BatchState.SUCCESS]
+
+        def side_effect(_):
+            if state_list:
+                return state_list.pop(0)
+            # fail if does not stop right before
+            raise AssertionError()
+
+        mock_livy.side_effect = side_effect
+
+        task = LivyOperator(
+            file='sparkapp',
+            polling_interval=1,
+            dag=self.dag,
+            task_id='livy_example'
+        )
+        task._livy_hook = task.get_hook()
+        task.poll_for_termination(BATCH_ID)
+
+        mock_livy.assert_called_with(BATCH_ID)
+        self.assertEqual(mock_livy.call_count, 3)
+
+    @patch('airflow.contrib.operators.livy_operator.LivyHook.get_batch_state')
+    def test_poll_for_termination_fail(self, mock_livy):
+
+        state_list = 2 * [BatchState.RUNNING] + [BatchState.ERROR]
+
+        def side_effect(_):
+            if state_list:
+                return state_list.pop(0)
+            # fail if does not stop right before
+            raise AssertionError()
+
+        mock_livy.side_effect = side_effect
+
+        task = LivyOperator(
+            file='sparkapp',
+            polling_interval=1,
+            dag=self.dag,
+            task_id='livy_example'
+        )
+        task._livy_hook = task.get_hook()
+
+        with self.assertRaises(AirflowException):
+            task.poll_for_termination(BATCH_ID)
+
+        mock_livy.assert_called_with(BATCH_ID)
+        self.assertEqual(mock_livy.call_count, 3)
+
+    @patch('airflow.contrib.operators.livy_operator.LivyHook.get_batch_state',
+           return_value=BatchState.SUCCESS)
+    @patch('airflow.contrib.operators.livy_operator.LivyHook.post_batch', 
return_value=BATCH_ID)
+    def test_execution(self, mock_post, mock_get):
+        task = LivyOperator(
+            livy_conn_id='livyunittest',
+            file='sparkapp',
+            polling_interval=1,
+            dag=self.dag,
+            task_id='livy_example'
+        )
+        task.execute(context={})
+
+        call_args = {k: v for k, v in mock_post.call_args[1].items() if v}
+        self.assertEqual(call_args, {'file': 'sparkapp'})
+        mock_get.assert_called_once_with(BATCH_ID)
+
+    @patch('airflow.contrib.operators.livy_operator.LivyHook.delete_batch')
+    @patch('airflow.contrib.operators.livy_operator.LivyHook.post_batch', 
return_value=BATCH_ID)
+    def test_deletion(self, mock_post, mock_delete):
+        task = LivyOperator(
+            livy_conn_id='livyunittest',
+            file='sparkapp',
+            dag=self.dag,
+            task_id='livy_example'
+        )
+        task.execute(context={})
+        task.kill()
+
+        mock_delete.assert_called_once_with(BATCH_ID)
+
+    def test_missing_id_on_deletion(self):
+        task = LivyOperator(
+            livy_conn_id='livyunittest',
+            file='sparkapp',
+            dag=self.dag,
+            task_id='livy_example'
+        )
+
+        with self.assertRaises(AirflowException):
+            task.kill()
+
+    def test_injected_hook(self):
+        def_hook = LivyHook(livy_conn_id='livyunittest')
+
+        task = LivyOperator(
+            file='sparkapp',
+            dag=self.dag,
+            task_id='livy_example'
+        )
+        task._livy_hook = def_hook
+
+        self.assertEqual(task.get_hook(), def_hook)
+
 
 Review comment:
   Could you add another test that checks that `.execute()` raises an 
AirflowError when the state isn't success please?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to