Пример #1
0
    def test_run_cli(self, mock_popen, mock_temp_dir):
        mock_subprocess = MockSubProcess()
        mock_popen.return_value = mock_subprocess
        mock_temp_dir.return_value = "test_run_cli"

        with mock.patch.dict('os.environ', {
            'AIRFLOW_CTX_DAG_ID': 'test_dag_id',
            'AIRFLOW_CTX_TASK_ID': 'test_task_id',
            'AIRFLOW_CTX_EXECUTION_DATE': '2015-01-01T00:00:00+00:00',
            'AIRFLOW_CTX_DAG_RUN_ID': '55',
            'AIRFLOW_CTX_DAG_OWNER': 'airflow',
            'AIRFLOW_CTX_DAG_EMAIL': '*****@*****.**',
        }):

            hook = MockHiveCliHook()
            hook.run_cli("SHOW DATABASES")

        hive_cmd = ['beeline', '-u', '"jdbc:hive2://localhost:10000/default"', '-hiveconf',
                    'airflow.ctx.dag_id=test_dag_id', '-hiveconf', 'airflow.ctx.task_id=test_task_id',
                    '-hiveconf', 'airflow.ctx.execution_date=2015-01-01T00:00:00+00:00', '-hiveconf',
                    'airflow.ctx.dag_run_id=55', '-hiveconf', 'airflow.ctx.dag_owner=airflow',
                    '-hiveconf', '[email protected]', '-hiveconf',
                    'mapreduce.job.queuename=airflow', '-hiveconf', 'mapred.job.queue.name=airflow',
                    '-hiveconf', 'tez.queue.name=airflow', '-f',
                    '/tmp/airflow_hiveop_test_run_cli/tmptest_run_cli']

        mock_popen.assert_called_with(
            hive_cmd,
            stdout=mock_subprocess.PIPE,
            stderr=mock_subprocess.STDOUT,
            cwd="/tmp/airflow_hiveop_test_run_cli",
            close_fds=True
        )
    def test_run_cli_with_hive_conf(self, mock_popen):
        hql = (
            "set key;\n"
            "set airflow.ctx.dag_id;\nset airflow.ctx.dag_run_id;\n"
            "set airflow.ctx.task_id;\nset airflow.ctx.execution_date;\n"
        )

        dag_id_ctx_var_name = AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_ID']['env_var_format']
        task_id_ctx_var_name = AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_TASK_ID']['env_var_format']
        execution_date_ctx_var_name = AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_EXECUTION_DATE'][
            'env_var_format'
        ]
        dag_run_id_ctx_var_name = AIRFLOW_VAR_NAME_FORMAT_MAPPING['AIRFLOW_CONTEXT_DAG_RUN_ID'][
            'env_var_format'
        ]

        mock_output = [
            'Connecting to jdbc:hive2://localhost:10000/default',
            'log4j:WARN No appenders could be found for logger (org.apache.hive.jdbc.Utils).',
            'log4j:WARN Please initialize the log4j system properly.',
            'log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.',
            'Connected to: Apache Hive (version 1.2.1.2.3.2.0-2950)',
            'Driver: Hive JDBC (version 1.2.1.spark2)',
            'Transaction isolation: TRANSACTION_REPEATABLE_READ',
            '0: jdbc:hive2://localhost:10000/default> USE default;',
            'No rows affected (0.37 seconds)',
            '0: jdbc:hive2://localhost:10000/default> set key;',
            '+------------+--+',
            '|    set     |',
            '+------------+--+',
            '| key=value  |',
            '+------------+--+',
            '1 row selected (0.133 seconds)',
            '0: jdbc:hive2://localhost:10000/default> set airflow.ctx.dag_id;',
            '+---------------------------------+--+',
            '|               set               |',
            '+---------------------------------+--+',
            '| airflow.ctx.dag_id=test_dag_id  |',
            '+---------------------------------+--+',
            '1 row selected (0.008 seconds)',
            '0: jdbc:hive2://localhost:10000/default> set airflow.ctx.dag_run_id;',
            '+-----------------------------------------+--+',
            '|                   set                   |',
            '+-----------------------------------------+--+',
            '| airflow.ctx.dag_run_id=test_dag_run_id  |',
            '+-----------------------------------------+--+',
            '1 row selected (0.007 seconds)',
            '0: jdbc:hive2://localhost:10000/default> set airflow.ctx.task_id;',
            '+-----------------------------------+--+',
            '|                set                |',
            '+-----------------------------------+--+',
            '| airflow.ctx.task_id=test_task_id  |',
            '+-----------------------------------+--+',
            '1 row selected (0.009 seconds)',
            '0: jdbc:hive2://localhost:10000/default> set airflow.ctx.execution_date;',
            '+-------------------------------------------------+--+',
            '|                       set                       |',
            '+-------------------------------------------------+--+',
            '| airflow.ctx.execution_date=test_execution_date  |',
            '+-------------------------------------------------+--+',
            '1 row selected (0.006 seconds)',
            '0: jdbc:hive2://localhost:10000/default> ',
            '0: jdbc:hive2://localhost:10000/default> ',
            'Closing: 0: jdbc:hive2://localhost:10000/default',
            '',
        ]

        with mock.patch.dict(
            'os.environ',
            {
                dag_id_ctx_var_name: 'test_dag_id',
                task_id_ctx_var_name: 'test_task_id',
                execution_date_ctx_var_name: 'test_execution_date',
                dag_run_id_ctx_var_name: 'test_dag_run_id',
            },
        ):

            hook = MockHiveCliHook()
            mock_popen.return_value = MockSubProcess(output=mock_output)

            output = hook.run_cli(hql=hql, hive_conf={'key': 'value'})
            process_inputs = " ".join(mock_popen.call_args_list[0][0][0])

            self.assertIn('value', process_inputs)
            self.assertIn('test_dag_id', process_inputs)
            self.assertIn('test_task_id', process_inputs)
            self.assertIn('test_execution_date', process_inputs)
            self.assertIn('test_dag_run_id', process_inputs)

            self.assertIn('value', output)
            self.assertIn('test_dag_id', output)
            self.assertIn('test_task_id', output)
            self.assertIn('test_execution_date', output)
            self.assertIn('test_dag_run_id', output)