def test_log_file_template_with_run_task(self): """Verify that the taskinstance has the right context for log_filename_template""" with mock.patch.object(task_command, "_run_task_by_selected_method"): with conf_vars({('core', 'dags_folder'): self.dag_path}): # increment the try_number of the task to be run dag = DagBag().get_dag(self.dag_id) task = dag.get_task(self.task_id) with create_session() as session: dag.create_dagrun( execution_date=self.execution_date, start_date=timezone.utcnow(), state=State.RUNNING, run_type=DagRunType.MANUAL, session=session, ) ti = TaskInstance(task, self.execution_date) ti.refresh_from_db(session=session, lock_for_update=True) ti.try_number = 1 # not running, so starts at 0 session.merge(ti) log_file_path = os.path.join( os.path.dirname(self.ti_log_file_path), "2.log") try: task_command.task_run( self.parser.parse_args(self.task_args)) assert os.path.exists(log_file_path) finally: try: os.remove(log_file_path) except OSError: pass
def running_dag_run(session): dag = DagBag().get_dag("example_bash_operator") task0 = dag.get_task("runme_0") task1 = dag.get_task("runme_1") execution_date = timezone.datetime(2016, 1, 9) tis = [ TaskInstance(task0, execution_date, state="success"), TaskInstance(task1, execution_date, state="failed"), ] session.bulk_save_objects(tis) dr = dag.create_dagrun( state="running", execution_date=execution_date, run_id="test_dag_runs_action", session=session, ) return dr