-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
test_task_command.py
778 lines (673 loc) · 28.8 KB
/
test_task_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#
# 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.
from __future__ import annotations
import io
import json
import logging
import os
import re
import tempfile
import unittest
from argparse import ArgumentParser
from contextlib import contextmanager, redirect_stdout
from pathlib import Path
from unittest import mock
import pendulum
import pytest
from parameterized import parameterized
from airflow import DAG
from airflow.cli import cli_parser
from airflow.cli.commands import task_command
from airflow.configuration import conf
from airflow.exceptions import AirflowException, DagRunNotFound
from airflow.models import DagBag, DagRun, Pool, TaskInstance
from airflow.models.serialized_dag import SerializedDagModel
from airflow.utils import timezone
from airflow.utils.session import create_session
from airflow.utils.state import State
from airflow.utils.types import DagRunType
from tests.test_utils.config import conf_vars
from tests.test_utils.db import clear_db_pools, clear_db_runs
DEFAULT_DATE = timezone.datetime(2022, 1, 1)
ROOT_FOLDER = os.path.realpath(
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir)
)
def reset(dag_id):
with create_session() as session:
tis = session.query(TaskInstance).filter_by(dag_id=dag_id)
tis.delete()
runs = session.query(DagRun).filter_by(dag_id=dag_id)
runs.delete()
@contextmanager
def move_back(old_path, new_path):
os.rename(old_path, new_path)
yield
os.rename(new_path, old_path)
# TODO: Check if tests needs side effects - locally there's missing DAG
class TestCliTasks:
run_id = 'TEST_RUN_ID'
dag_id = 'example_python_operator'
parser: ArgumentParser
dagbag: DagBag
dag: DAG
dag_run: DagRun
@classmethod
def setup_class(cls):
cls.dagbag = DagBag(include_examples=True)
cls.parser = cli_parser.get_parser()
clear_db_runs()
cls.dag = cls.dagbag.get_dag(cls.dag_id)
cls.dagbag.sync_to_db()
cls.dag_run = cls.dag.create_dagrun(
state=State.NONE, run_id=cls.run_id, run_type=DagRunType.MANUAL, execution_date=DEFAULT_DATE
)
@classmethod
def teardown_class(cls) -> None:
clear_db_runs()
def test_cli_list_tasks(self):
for dag_id in self.dagbag.dags:
args = self.parser.parse_args(['tasks', 'list', dag_id])
task_command.task_list(args)
args = self.parser.parse_args(['tasks', 'list', 'example_bash_operator', '--tree'])
task_command.task_list(args)
@pytest.mark.filterwarnings("ignore::airflow.utils.context.AirflowContextDeprecationWarning")
def test_test(self):
"""Test the `airflow test` command"""
args = self.parser.parse_args(
["tasks", "test", "example_python_operator", 'print_the_context', '2018-01-01']
)
with redirect_stdout(io.StringIO()) as stdout:
task_command.task_test(args)
# Check that prints, and log messages, are shown
assert "'example_python_operator__print_the_context__20180101'" in stdout.getvalue()
@pytest.mark.filterwarnings("ignore::airflow.utils.context.AirflowContextDeprecationWarning")
@mock.patch('airflow.utils.timezone.utcnow')
def test_test_no_execution_date(self, mock_utcnow):
"""Test the `airflow test` command"""
now = pendulum.now('UTC')
mock_utcnow.return_value = now
ds = now.strftime("%Y%m%d")
args = self.parser.parse_args(["tasks", "test", "example_python_operator", 'print_the_context'])
with redirect_stdout(io.StringIO()) as stdout:
task_command.task_test(args)
# Check that prints, and log messages, are shown
assert f"'example_python_operator__print_the_context__{ds}'" in stdout.getvalue()
@pytest.mark.filterwarnings("ignore::airflow.utils.context.AirflowContextDeprecationWarning")
def test_test_with_existing_dag_run(self, caplog):
"""Test the `airflow test` command"""
task_id = 'print_the_context'
args = self.parser.parse_args(["tasks", "test", self.dag_id, task_id, DEFAULT_DATE.isoformat()])
with caplog.at_level("INFO", logger="airflow.task"):
task_command.task_test(args)
assert f"Marking task as SUCCESS. dag_id={self.dag_id}, task_id={task_id}" in caplog.text
@pytest.mark.filterwarnings("ignore::airflow.utils.context.AirflowContextDeprecationWarning")
def test_test_filters_secrets(self, capsys):
"""Test ``airflow test`` does not print secrets to stdout.
Output should be filtered by SecretsMasker.
"""
password = "somepassword1234!"
logging.getLogger("airflow.task").filters[0].add_mask(password)
args = self.parser.parse_args(
["tasks", "test", "example_python_operator", "print_the_context", "2018-01-01"],
)
with mock.patch("airflow.models.TaskInstance.run", new=lambda *_, **__: print(password)):
task_command.task_test(args)
assert capsys.readouterr().out.endswith("***\n")
not_password = "!4321drowssapemos"
with mock.patch("airflow.models.TaskInstance.run", new=lambda *_, **__: print(not_password)):
task_command.task_test(args)
assert capsys.readouterr().out.endswith(f"{not_password}\n")
def test_cli_test_different_path(self, session):
"""
When thedag processor has a different dags folder
from the worker, ``airflow tasks run --local`` should still work.
"""
repo_root = Path(__file__).parent.parent.parent.parent
orig_file_path = repo_root / 'tests/dags/test_dags_folder.py'
orig_dags_folder = orig_file_path.parent
# parse dag in original path
with conf_vars({('core', 'dags_folder'): orig_dags_folder.as_posix()}):
dagbag = DagBag(include_examples=False)
dag = dagbag.get_dag('test_dags_folder')
dagbag.sync_to_db(session=session)
dag.create_dagrun(
state=State.NONE,
run_id='abc123',
run_type=DagRunType.MANUAL,
execution_date=pendulum.now('UTC'),
session=session,
)
session.commit()
# now let's move the file
# additionally let's update the dags folder to be the new path
# ideally since dags_folder points correctly to the file, airflow
# should be able to find the dag.
with tempfile.TemporaryDirectory() as td:
new_file_path = Path(td) / Path(orig_file_path).name
new_dags_folder = new_file_path.parent
with move_back(orig_file_path, new_file_path), conf_vars(
{('core', 'dags_folder'): new_dags_folder.as_posix()}
):
ser_dag = (
session.query(SerializedDagModel)
.filter(SerializedDagModel.dag_id == 'test_dags_folder')
.one()
)
# confirm that the serialized dag location has not been updated
assert ser_dag.fileloc == orig_file_path.as_posix()
assert ser_dag.data['dag']['_processor_dags_folder'] == orig_dags_folder.as_posix()
assert ser_dag.data['dag']['fileloc'] == orig_file_path.as_posix()
assert ser_dag.dag._processor_dags_folder == orig_dags_folder.as_posix()
from airflow.settings import DAGS_FOLDER
assert DAGS_FOLDER == new_dags_folder.as_posix() != orig_dags_folder.as_posix()
task_command.task_run(
self.parser.parse_args(
[
'tasks',
'run',
'--ignore-all-dependencies',
'--local',
'test_dags_folder',
'task',
'abc123',
]
)
)
ti = (
session.query(TaskInstance)
.filter(
TaskInstance.task_id == 'task',
TaskInstance.dag_id == 'test_dags_folder',
TaskInstance.run_id == 'abc123',
TaskInstance.map_index == -1,
)
.one()
)
assert ti.state == 'success'
# verify that the file was in different location when run
assert ti.xcom_pull(ti.task_id) == new_file_path.as_posix()
@mock.patch("airflow.cli.commands.task_command.LocalTaskJob")
def test_run_with_existing_dag_run_id(self, mock_local_job):
"""
Test that we can run with existing dag_run_id
"""
task0_id = self.dag.task_ids[0]
args0 = [
'tasks',
'run',
'--ignore-all-dependencies',
'--local',
self.dag_id,
task0_id,
self.run_id,
]
task_command.task_run(self.parser.parse_args(args0), dag=self.dag)
mock_local_job.assert_called_once_with(
task_instance=mock.ANY,
mark_success=False,
ignore_all_deps=True,
ignore_depends_on_past=False,
ignore_task_deps=False,
ignore_ti_state=False,
pickle_id=None,
pool=None,
external_executor_id=None,
)
@mock.patch("airflow.cli.commands.task_command.LocalTaskJob")
def test_run_raises_when_theres_no_dagrun(self, mock_local_job):
"""
Test that run raises when there's run_id but no dag_run
"""
dag_id = 'test_run_ignores_all_dependencies'
dag = self.dagbag.get_dag(dag_id)
task0_id = 'test_run_dependent_task'
run_id = 'TEST_RUN_ID'
args0 = [
'tasks',
'run',
'--ignore-all-dependencies',
'--local',
dag_id,
task0_id,
run_id,
]
with pytest.raises(DagRunNotFound):
task_command.task_run(self.parser.parse_args(args0), dag=dag)
def test_cli_test_with_params(self):
task_command.task_test(
self.parser.parse_args(
[
'tasks',
'test',
'example_passing_params_via_test_command',
'run_this',
DEFAULT_DATE.isoformat(),
'--task-params',
'{"foo":"bar"}',
]
)
)
task_command.task_test(
self.parser.parse_args(
[
'tasks',
'test',
'example_passing_params_via_test_command',
'also_run_this',
DEFAULT_DATE.isoformat(),
'--task-params',
'{"foo":"bar"}',
]
)
)
def test_cli_test_with_env_vars(self):
with redirect_stdout(io.StringIO()) as stdout:
task_command.task_test(
self.parser.parse_args(
[
'tasks',
'test',
'example_passing_params_via_test_command',
'env_var_test_task',
DEFAULT_DATE.isoformat(),
'--env-vars',
'{"foo":"bar"}',
]
)
)
output = stdout.getvalue()
assert 'foo=bar' in output
assert 'AIRFLOW_TEST_MODE=True' in output
@parameterized.expand(
[
("--ignore-all-dependencies",),
("--ignore-depends-on-past",),
("--ignore-dependencies",),
("--force",),
],
)
def test_cli_run_invalid_raw_option(self, option: str):
with pytest.raises(
AirflowException,
match="Option --raw does not work with some of the other options on this command.",
):
task_command.task_run(
self.parser.parse_args(
[ # type: ignore
'tasks',
'run',
'example_bash_operator',
'runme_0',
DEFAULT_DATE.isoformat(),
'--raw',
option,
]
)
)
def test_cli_run_mutually_exclusive(self):
with pytest.raises(AirflowException, match="Option --raw and --local are mutually exclusive."):
task_command.task_run(
self.parser.parse_args(
[
'tasks',
'run',
'example_bash_operator',
'runme_0',
DEFAULT_DATE.isoformat(),
'--raw',
'--local',
]
)
)
def test_task_render(self):
"""
tasks render should render and displays templated fields for a given task
"""
with redirect_stdout(io.StringIO()) as stdout:
task_command.task_render(
self.parser.parse_args(['tasks', 'render', 'tutorial', 'templated', '2016-01-01'])
)
output = stdout.getvalue()
assert 'echo "2016-01-01"' in output
assert 'echo "2016-01-08"' in output
def test_cli_run_when_pickle_and_dag_cli_method_selected(self):
"""
tasks run should return an AirflowException when invalid pickle_id is passed
"""
pickle_id = 'pickle_id'
with pytest.raises(
AirflowException,
match=re.escape("You cannot use the --pickle option when using DAG.cli() method."),
):
task_command.task_run(
self.parser.parse_args(
[
'tasks',
'run',
'example_bash_operator',
'runme_0',
DEFAULT_DATE.isoformat(),
'--pickle',
pickle_id,
]
),
self.dag,
)
def test_task_state(self):
task_command.task_state(
self.parser.parse_args(
['tasks', 'state', self.dag_id, 'print_the_context', DEFAULT_DATE.isoformat()]
)
)
def test_task_states_for_dag_run(self):
dag2 = DagBag().dags['example_python_operator']
task2 = dag2.get_task(task_id='print_the_context')
default_date2 = timezone.datetime(2016, 1, 9)
dag2.clear()
dagrun = dag2.create_dagrun(
state=State.RUNNING,
execution_date=default_date2,
run_type=DagRunType.MANUAL,
external_trigger=True,
)
ti2 = TaskInstance(task2, dagrun.execution_date)
ti2.set_state(State.SUCCESS)
ti_start = ti2.start_date
ti_end = ti2.end_date
with redirect_stdout(io.StringIO()) as stdout:
task_command.task_states_for_dag_run(
self.parser.parse_args(
[
'tasks',
'states-for-dag-run',
'example_python_operator',
default_date2.isoformat(),
'--output',
"json",
]
)
)
actual_out = json.loads(stdout.getvalue())
assert len(actual_out) == 1
assert actual_out[0] == {
'dag_id': 'example_python_operator',
'execution_date': '2016-01-09T00:00:00+00:00',
'task_id': 'print_the_context',
'state': 'success',
'start_date': ti_start.isoformat(),
'end_date': ti_end.isoformat(),
}
def test_task_states_for_dag_run_when_dag_run_not_exists(self):
"""
task_states_for_dag_run should return an AirflowException when invalid dag id is passed
"""
with pytest.raises(DagRunNotFound):
default_date2 = timezone.datetime(2016, 1, 9)
task_command.task_states_for_dag_run(
self.parser.parse_args(
[
'tasks',
'states-for-dag-run',
'not_exists_dag',
default_date2.isoformat(),
'--output',
"json",
]
)
)
def test_subdag_clear(self):
args = self.parser.parse_args(['tasks', 'clear', 'example_subdag_operator', '--yes'])
task_command.task_clear(args)
args = self.parser.parse_args(
['tasks', 'clear', 'example_subdag_operator', '--yes', '--exclude-subdags']
)
task_command.task_clear(args)
def test_parentdag_downstream_clear(self):
args = self.parser.parse_args(['tasks', 'clear', 'example_subdag_operator.section-1', '--yes'])
task_command.task_clear(args)
args = self.parser.parse_args(
['tasks', 'clear', 'example_subdag_operator.section-1', '--yes', '--exclude-parentdag']
)
task_command.task_clear(args)
class TestLogsfromTaskRunCommand(unittest.TestCase):
def setUp(self) -> None:
self.dag_id = "test_logging_dag"
self.task_id = "test_task"
self.run_id = "test_run"
self.dag_path = os.path.join(ROOT_FOLDER, "dags", "test_logging_in_dag.py")
reset(self.dag_id)
self.execution_date = timezone.datetime(2017, 1, 1)
self.execution_date_str = self.execution_date.isoformat()
self.task_args = ['tasks', 'run', self.dag_id, self.task_id, '--local', self.execution_date_str]
self.log_dir = conf.get_mandatory_value('logging', 'base_log_folder')
self.log_filename = f"dag_id={self.dag_id}/run_id={self.run_id}/task_id={self.task_id}/attempt=1.log"
self.ti_log_file_path = os.path.join(self.log_dir, self.log_filename)
self.parser = cli_parser.get_parser()
DagBag().get_dag(self.dag_id).create_dagrun(
run_id=self.run_id,
execution_date=self.execution_date,
start_date=timezone.utcnow(),
state=State.RUNNING,
run_type=DagRunType.MANUAL,
)
root = self.root_logger = logging.getLogger()
self.root_handlers = root.handlers.copy()
self.root_filters = root.filters.copy()
self.root_level = root.level
try:
os.remove(self.ti_log_file_path)
except OSError:
pass
def tearDown(self) -> None:
root = self.root_logger
root.setLevel(self.root_level)
root.handlers[:] = self.root_handlers
root.filters[:] = self.root_filters
reset(self.dag_id)
try:
os.remove(self.ti_log_file_path)
except OSError:
pass
def assert_log_line(self, text, logs_list, expect_from_logging_mixin=False):
"""
Get Log Line and assert only 1 Entry exists with the given text. Also check that
"logging_mixin" line does not appear in that log line to avoid duplicate logging as below:
[2020-06-24 16:47:23,537] {logging_mixin.py:91} INFO - [2020-06-24 16:47:23,536] {python.py:135}
"""
log_lines = [log for log in logs_list if text in log]
assert len(log_lines) == 1
log_line = log_lines[0]
if not expect_from_logging_mixin:
# Logs from print statement still show with logging_mixing as filename
# Example: [2020-06-24 17:07:00,482] {logging_mixin.py:91} INFO - Log from Print statement
assert "logging_mixin.py" not in log_line
return log_line
@mock.patch("airflow.cli.commands.task_command.LocalTaskJob")
def test_external_executor_id_present_for_fork_run_task(self, mock_local_job):
args = self.parser.parse_args(self.task_args)
args.external_executor_id = "ABCD12345"
task_command.task_run(args)
mock_local_job.assert_called_once_with(
task_instance=mock.ANY,
mark_success=False,
pickle_id=None,
ignore_all_deps=False,
ignore_depends_on_past=False,
ignore_task_deps=False,
ignore_ti_state=False,
pool=None,
external_executor_id="ABCD12345",
)
@mock.patch("airflow.cli.commands.task_command.LocalTaskJob")
def test_external_executor_id_present_for_process_run_task(self, mock_local_job):
args = self.parser.parse_args(self.task_args)
args.external_executor_id = "ABCD12345"
with mock.patch.dict(os.environ, {"external_executor_id": "12345FEDCBA"}):
task_command.task_run(args)
mock_local_job.assert_called_once_with(
task_instance=mock.ANY,
mark_success=False,
pickle_id=None,
ignore_all_deps=False,
ignore_depends_on_past=False,
ignore_task_deps=False,
ignore_ti_state=False,
pool=None,
external_executor_id="ABCD12345",
)
@unittest.skipIf(not hasattr(os, 'fork'), "Forking not available")
def test_logging_with_run_task(self):
# We are not using self.assertLogs as we want to verify what actually is stored in the Log file
# as that is what gets displayed
with conf_vars({('core', 'dags_folder'): self.dag_path}):
task_command.task_run(self.parser.parse_args(self.task_args))
with open(self.ti_log_file_path) as l_file:
logs = l_file.read()
print(logs) # In case of a test failures this line would show detailed log
logs_list = logs.splitlines()
assert "INFO - Started process" in logs
assert f"Subtask {self.task_id}" in logs
assert "standard_task_runner.py" in logs
assert (
f"INFO - Running: ['airflow', 'tasks', 'run', '{self.dag_id}', "
f"'{self.task_id}', '{self.run_id}'," in logs
)
self.assert_log_line("Log from DAG Logger", logs_list)
self.assert_log_line("Log from TI Logger", logs_list)
self.assert_log_line("Log from Print statement", logs_list, expect_from_logging_mixin=True)
assert (
f"INFO - Marking task as SUCCESS. dag_id={self.dag_id}, "
f"task_id={self.task_id}, execution_date=20170101T000000" in logs
)
@unittest.skipIf(not hasattr(os, 'fork'), "Forking not available")
def test_run_task_with_pool(self):
pool_name = 'test_pool_run'
clear_db_pools()
with create_session() as session:
pool = Pool(pool=pool_name, slots=1)
session.add(pool)
session.commit()
assert session.query(TaskInstance).filter_by(pool=pool_name).first() is None
task_command.task_run(self.parser.parse_args(self.task_args + ['--pool', pool_name]))
assert session.query(TaskInstance).filter_by(pool=pool_name).first() is not None
session.delete(pool)
session.commit()
# For this test memory spins out of control on Python 3.6. TODO(potiuk): FIXME")
@pytest.mark.quarantined
@mock.patch("airflow.task.task_runner.standard_task_runner.CAN_FORK", False)
def test_logging_with_run_task_subprocess(self):
# We are not using self.assertLogs as we want to verify what actually is stored in the Log file
# as that is what gets displayed
with conf_vars({('core', 'dags_folder'): self.dag_path}):
task_command.task_run(self.parser.parse_args(self.task_args))
with open(self.ti_log_file_path) as l_file:
logs = l_file.read()
print(logs) # In case of a test failures this line would show detailed log
logs_list = logs.splitlines()
assert f"Subtask {self.task_id}" in logs
assert "base_task_runner.py" in logs
self.assert_log_line("Log from DAG Logger", logs_list)
self.assert_log_line("Log from TI Logger", logs_list)
self.assert_log_line("Log from Print statement", logs_list, expect_from_logging_mixin=True)
assert (
f"INFO - Running: ['airflow', 'tasks', 'run', '{self.dag_id}', "
f"'{self.task_id}', '{self.execution_date_str}'," in logs
)
assert (
f"INFO - Marking task as SUCCESS. dag_id={self.dag_id}, "
f"task_id={self.task_id}, execution_date=20170101T000000" in logs
)
def test_log_file_template_with_run_task(self):
"""Verify that the taskinstance has the right context for log_filename_template"""
with conf_vars({('core', 'dags_folder'): self.dag_path}):
# increment the try_number of the task to be run
with create_session() as session:
ti = session.query(TaskInstance).filter_by(run_id=self.run_id).first()
ti.try_number = 1
log_file_path = os.path.join(os.path.dirname(self.ti_log_file_path), "attempt=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
@mock.patch.object(task_command, "_run_task_by_selected_method")
def test_root_logger_restored(self, run_task_mock):
"""Verify that the root logging context is restored"""
logger = logging.getLogger("foo.bar")
def task_inner(*args, **kwargs):
logger.warning("redirected log message")
run_task_mock.side_effect = task_inner
config = {
('core', 'dags_folder'): self.dag_path,
('logging', 'logging_level'): "INFO",
}
with conf_vars(config):
with self.assertLogs(level=logging.WARNING) as captured:
logger.warning("not redirected")
task_command.task_run(self.parser.parse_args(self.task_args))
assert captured.output == ["WARNING:foo.bar:not redirected"]
assert self.root_logger.level == logging.WARNING
assert self.root_logger.handlers == self.root_handlers
@pytest.mark.quarantined
@mock.patch.object(task_command, "_run_task_by_selected_method")
def test_disable_handler_modifying(self, run_task_mock):
"""If [core] donot_modify_handlers is set to True, the root logger is untouched"""
from airflow import settings
logger = logging.getLogger("foo.bar")
def task_inner(*args, **kwargs):
logger.warning("not redirected")
run_task_mock.side_effect = task_inner
config = {
('core', 'dags_folder'): self.dag_path,
('logging', 'logging_level'): "INFO",
}
old_value = settings.DONOT_MODIFY_HANDLERS
settings.DONOT_MODIFY_HANDLERS = True
with conf_vars(config):
with self.assertLogs(level=logging.WARNING) as captured:
task_command.task_run(self.parser.parse_args(self.task_args))
assert captured.output == ["WARNING:foo.bar:not redirected"]
settings.DONOT_MODIFY_HANDLERS = old_value
def test_context_with_run():
dag_id = "test_parsing_context"
task_id = "task1"
run_id = "test_run"
dag_path = os.path.join(ROOT_FOLDER, "dags", "test_parsing_context.py")
reset(dag_id)
execution_date = timezone.datetime(2017, 1, 1)
execution_date_str = execution_date.isoformat()
task_args = ['tasks', 'run', dag_id, task_id, '--local', execution_date_str]
parser = cli_parser.get_parser()
DagBag().get_dag(dag_id).create_dagrun(
run_id=run_id,
execution_date=execution_date,
start_date=timezone.utcnow(),
state=State.RUNNING,
run_type=DagRunType.MANUAL,
)
with conf_vars({('core', 'dags_folder'): dag_path}):
task_command.task_run(parser.parse_args(task_args))
context_file = Path("/tmp/airflow_parsing_context")
text = context_file.read_text()
assert (
text == "_AIRFLOW_PARSING_CONTEXT_DAG_ID=test_parsing_context\n"
"_AIRFLOW_PARSING_CONTEXT_TASK_ID=task1\n"
)