def test_schedule_dag_fake_scheduled_previous(self): """ Test scheduling a dag where there is a prior DagRun which has the same run_id as the next run should have """ delta = timedelta(hours=1) dag = DAG(self.TEST_SCHEDULE_DAG_FAKE_SCHEDULED_PREVIOUS_DAG_ID, schedule_interval=delta, start_date=DEFAULT_DATE) dag.add_task(BaseOperator( task_id="faketastic", owner='Also fake', start_date=DEFAULT_DATE)) dag_file_processor = DagFileProcessor(dag_ids=[], log=mock.MagicMock()) dag.create_dagrun(run_id=DagRun.id_for_date(DEFAULT_DATE), execution_date=DEFAULT_DATE, state=State.SUCCESS, external_trigger=True) dag_run = dag_file_processor.create_dag_run(dag) self.assertIsNotNone(dag_run) self.assertEqual(dag.dag_id, dag_run.dag_id) self.assertIsNotNone(dag_run.run_id) self.assertNotEqual('', dag_run.run_id) self.assertEqual( DEFAULT_DATE + delta, dag_run.execution_date, msg='dag_run.execution_date did not match expectation: {0}' .format(dag_run.execution_date) ) self.assertEqual(State.RUNNING, dag_run.state) self.assertFalse(dag_run.external_trigger)
def test_externally_triggered_dagrun(self): TI = TaskInstance # Create the dagrun between two "scheduled" execution dates of the DAG execution_date = DEFAULT_DATE + timedelta(days=2) execution_ds = execution_date.strftime('%Y-%m-%d') execution_ds_nodash = execution_ds.replace('-', '') dag = DAG( TEST_DAG_ID, default_args=self.args, schedule_interval=timedelta(weeks=1), start_date=DEFAULT_DATE) task = DummyOperator(task_id='test_externally_triggered_dag_context', dag=dag) dag.create_dagrun(run_id=DagRun.id_for_date(execution_date), execution_date=execution_date, state=State.RUNNING, external_trigger=True) task.run( start_date=execution_date, end_date=execution_date) ti = TI(task=task, execution_date=execution_date) context = ti.get_template_context() # next_ds/prev_ds should be the execution date for manually triggered runs self.assertEqual(context['next_ds'], execution_ds) self.assertEqual(context['next_ds_nodash'], execution_ds_nodash) self.assertEqual(context['prev_ds'], execution_ds) self.assertEqual(context['prev_ds_nodash'], execution_ds_nodash)
class TestDecorators(unittest.TestCase): EXAMPLE_DAG_DEFAULT_DATE = dates.days_ago(2) run_id = "test_{}".format(DagRun.id_for_date(EXAMPLE_DAG_DEFAULT_DATE)) @classmethod def setUpClass(cls): cls.dagbag = DagBag(include_examples=True) app = application.create_app(testing=True) app.config['WTF_CSRF_METHODS'] = [] cls.app = app.test_client() def setUp(self): self.session = Session() self.cleanup_dagruns() self.prepare_dagruns() def cleanup_dagruns(self): DR = DagRun dag_ids = 'example_bash_operator' (self.session.query(DR).filter(DR.dag_id == dag_ids).filter( DR.run_id == self.run_id).delete(synchronize_session='fetch')) self.session.commit() def prepare_dagruns(self): self.bash_dag = self.dagbag.dags['example_bash_operator'] self.bash_dag.sync_to_db() self.bash_dagrun = self.bash_dag.create_dagrun( run_id=self.run_id, execution_date=self.EXAMPLE_DAG_DEFAULT_DATE, start_date=timezone.utcnow(), state=State.RUNNING) def check_last_log(self, dag_id, event, execution_date=None): qry = self.session.query(Log.dag_id, Log.task_id, Log.event, Log.execution_date, Log.owner, Log.extra) qry = qry.filter(Log.dag_id == dag_id, Log.event == event) if execution_date: qry = qry.filter(Log.execution_date == execution_date) logs = qry.order_by(Log.dttm.desc()).limit(5).all() self.assertGreaterEqual(len(logs), 1) self.assertTrue(logs[0].extra) def test_action_logging_get(self): url = '/admin/airflow/graph?dag_id=example_bash_operator&execution_date={}'.format( quote_plus( self.EXAMPLE_DAG_DEFAULT_DATE.isoformat().encode('utf-8'))) self.app.get(url, follow_redirects=True) # In mysql backend, this commit() is needed to write down the logs self.session.commit() self.check_last_log("example_bash_operator", event="graph", execution_date=self.EXAMPLE_DAG_DEFAULT_DATE) def test_action_logging_post(self): form = dict( task_id="runme_1", dag_id="example_bash_operator", execution_date=self.EXAMPLE_DAG_DEFAULT_DATE.isoformat().encode( 'utf-8'), upstream="false", downstream="false", future="false", past="false", only_failed="false", ) self.app.post("/admin/airflow/clear", data=form) # In mysql backend, this commit() is needed to write down the logs self.session.commit() self.check_last_log("example_bash_operator", event="clear", execution_date=self.EXAMPLE_DAG_DEFAULT_DATE)