forked from cylc/cylc-flow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_config.py
More file actions
1484 lines (1386 loc) · 45.2 KB
/
test_config.py
File metadata and controls
1484 lines (1386 loc) · 45.2 KB
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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
# Copyright (C) NIWA & British Crown (Met Office) & Contributors.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from optparse import Values
from typing import Any, Callable, Dict, Optional, Tuple, Type, List
from pathlib import Path
import pytest
import logging
from types import SimpleNamespace
from unittest.mock import Mock
from cylc.flow import CYLC_LOG
from cylc.flow.config import WorkflowConfig
from cylc.flow.cycling import loader, PointBase, SequenceBase
from cylc.flow.cycling.loader import INTEGER_CYCLING_TYPE, ISO8601_CYCLING_TYPE
from cylc.flow.cycling.integer import IntegerSequence, IntegerPoint
from cylc.flow.exceptions import (
PointParsingError,
InputError,
WorkflowConfigError,
XtriggerConfigError,
)
from cylc.flow.scheduler_cli import RunOptions
from cylc.flow.scripts.validate import ValidateOptions
from cylc.flow.workflow_files import WorkflowFiles
from cylc.flow.wallclock import get_utc_mode, set_utc_mode
from cylc.flow.xtrigger_mgr import XtriggerManager
from cylc.flow.task_outputs import (
TASK_OUTPUT_SUBMITTED,
TASK_OUTPUT_SUCCEEDED
)
Fixture = Any
@pytest.fixture
def tmp_flow_config(tmp_run_dir: Callable):
"""Create a temporary flow config file for use in init'ing WorkflowConfig.
Args:
reg: Workflow name.
config: The flow file content.
Returns the path to the flow file.
"""
def _tmp_flow_config(reg: str, config: str) -> Path:
run_dir: Path = tmp_run_dir(reg)
flow_file = run_dir / WorkflowFiles.FLOW_FILE
flow_file.write_text(config)
return flow_file
return _tmp_flow_config
class TestWorkflowConfig:
"""Test class for the Cylc WorkflowConfig object."""
def test_xfunction_imports(
self, mock_glbl_cfg: Fixture, tmp_path: Path,
xtrigger_mgr: XtriggerManager):
"""Test for a workflow configuration with valid xtriggers"""
mock_glbl_cfg(
'cylc.flow.platforms.glbl_cfg',
'''
[platforms]
[[localhost]]
hosts = localhost
'''
)
python_dir = tmp_path / "lib" / "python"
python_dir.mkdir(parents=True)
name_a_tree_file = python_dir / "name_a_tree.py"
# NB: we are not returning a lambda, instead we have a scalar
name_a_tree_file.write_text("""name_a_tree = lambda: 'jacaranda'""")
flow_file = tmp_path / WorkflowFiles.FLOW_FILE
flow_config = """
[scheduler]
allow implicit tasks = True
[scheduling]
initial cycle point = 2018-01-01
[[xtriggers]]
tree = name_a_tree()
[[graph]]
R1 = '@tree => qux'
"""
flow_file.write_text(flow_config)
workflow_config = WorkflowConfig(
workflow="name_a_tree", fpath=flow_file, options=Mock(spec=[]),
xtrigger_mgr=xtrigger_mgr
)
assert 'tree' in workflow_config.xtrigger_mgr.functx_map
def test_xfunction_import_error(self, mock_glbl_cfg, tmp_path):
"""Test for error when a xtrigger function cannot be imported."""
mock_glbl_cfg(
'cylc.flow.platforms.glbl_cfg',
'''
[platforms]
[[localhost]]
hosts = localhost
'''
)
python_dir = tmp_path / "lib" / "python"
python_dir.mkdir(parents=True)
caiman_file = python_dir / "caiman.py"
# NB: we are not returning a lambda, instead we have a scalar
caiman_file.write_text("""caiman = lambda: True""")
flow_file = tmp_path / WorkflowFiles.FLOW_FILE
flow_config = """
[scheduling]
initial cycle point = 2018-01-01
[[xtriggers]]
oopsie = piranha()
[[graph]]
R1 = '@oopsie => qux'
"""
flow_file.write_text(flow_config)
with pytest.raises(XtriggerConfigError) as excinfo:
WorkflowConfig(
workflow="caiman_workflow",
fpath=flow_file,
options=Mock(spec=[])
)
assert "not found" in str(excinfo.value)
def test_xfunction_attribute_error(self, mock_glbl_cfg, tmp_path):
"""Test for error when a xtrigger function cannot be imported."""
mock_glbl_cfg(
'cylc.flow.platforms.glbl_cfg',
'''
[platforms]
[[localhost]]
hosts = localhost
'''
)
python_dir = tmp_path / "lib" / "python"
python_dir.mkdir(parents=True)
capybara_file = python_dir / "capybara.py"
# NB: we are not returning a lambda, instead we have a scalar
capybara_file.write_text("""toucan = lambda: True""")
flow_file = tmp_path / WorkflowFiles.FLOW_FILE
flow_config = """
[scheduling]
initial cycle point = 2018-01-01
[[xtriggers]]
oopsie = capybara()
[[graph]]
R1 = '@oopsie => qux'
"""
flow_file.write_text(flow_config)
with pytest.raises(XtriggerConfigError) as excinfo:
WorkflowConfig(workflow="capybara_workflow", fpath=flow_file,
options=Mock(spec=[]))
assert "not found" in str(excinfo.value)
def test_xfunction_not_callable(self, mock_glbl_cfg, tmp_path):
"""Test for error when a xtrigger function is not callable."""
mock_glbl_cfg(
'cylc.flow.platforms.glbl_cfg',
'''
[platforms]
[[localhost]]
hosts = localhost
'''
)
python_dir = tmp_path / "lib" / "python"
python_dir.mkdir(parents=True)
not_callable_file = python_dir / "not_callable.py"
# NB: we are not returning a lambda, instead we have a scalar
not_callable_file.write_text("""not_callable = 42""")
flow_file = tmp_path / WorkflowFiles.FLOW_FILE
flow_config = """
[scheduling]
initial cycle point = 2018-01-01
[[xtriggers]]
oopsie = not_callable()
[[graph]]
R1 = '@oopsie => qux'
"""
flow_file.write_text(flow_config)
with pytest.raises(XtriggerConfigError) as excinfo:
WorkflowConfig(
workflow="workflow_with_not_callable",
fpath=flow_file,
options=Mock(spec=[])
)
assert "callable" in str(excinfo.value)
@pytest.mark.parametrize(
'fam_txt',
[pytest.param('"SOMEFAM"', id="double quoted"),
pytest.param('\'SOMEFAM\'', id="single quoted"),
pytest.param('SOMEFAM', id="unquoted")]
)
def test_family_inheritance_and_quotes(
fam_txt: str,
mock_glbl_cfg: Callable, tmp_flow_config: Callable
) -> None:
"""Test that inheritance does not ignore items, if not all quoted.
For example:
inherit = 'MAINFAM<major, minor>', SOMEFAM
inherit = 'BIGFAM', SOMEFAM
See bug #2700 for more/
"""
mock_glbl_cfg(
'cylc.flow.platforms.glbl_cfg',
'''
[platforms]
[[localhost]]
hosts = localhost
'''
)
reg = 'test'
file_path = tmp_flow_config(reg, f'''
[scheduler]
allow implicit tasks = True
[task parameters]
major = 1..5
minor = 10..20
[scheduling]
[[graph]]
R1 = """hello => MAINFAM<major, minor>
hello => SOMEFAM"""
[runtime]
[[root]]
script = true
[[MAINFAM<major, minor>]]
[[SOMEFAM]]
[[ goodbye_0<major, minor> ]]
inherit = 'MAINFAM<major, minor>', {fam_txt}
''')
config = WorkflowConfig(
reg, file_path, template_vars={}, options=Values()
)
assert ('goodbye_0_major1_minor10' in
config.runtime['descendants']['MAINFAM_major1_minor10'])
assert ('goodbye_0_major1_minor10' in
config.runtime['descendants']['SOMEFAM'])
@pytest.mark.parametrize(
('cycling_type', 'scheduling_cfg', 'expected_icp', 'expected_opt_icp',
'expected_err'),
[
pytest.param(
ISO8601_CYCLING_TYPE,
{'initial cycle point': None},
None,
None,
(WorkflowConfigError, "requires an initial cycle point"),
id="Lack of icp"
),
pytest.param(
INTEGER_CYCLING_TYPE,
{'initial cycle point': None},
'1',
None,
None,
id="Default icp for integer cycling type"
),
pytest.param(
INTEGER_CYCLING_TYPE,
{'initial cycle point': "now"},
None,
None,
(PointParsingError, "invalid literal for int()"),
id="Non-integer ICP for integer cycling type"
),
pytest.param(
INTEGER_CYCLING_TYPE,
{'initial cycle point': "20500808T0000Z"},
None,
None,
(PointParsingError, "invalid literal for int()"),
id="More non-integer ICP for integer cycling type"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{'initial cycle point': "1"},
None,
None,
(PointParsingError, "Invalid ISO 8601 date representation"),
id="Non-ISO8601 ICP for ISO8601 cycling type"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{'initial cycle point': 'now'},
'20050102T0615+0530',
'20050102T0615+0530',
None,
id="ICP = now"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2013',
'initial cycle point constraints': ['T00', 'T12']
},
'20130101T0000+0530',
None,
None,
id="Constraints"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2021-01-20',
'initial cycle point constraints': ['--01-19', '--01-21']
},
None,
None,
(WorkflowConfigError, "does not meet the constraints"),
id="Violated constraints"
),
]
)
def test_process_icp(
cycling_type: str,
scheduling_cfg: Dict[str, Any],
expected_icp: Optional[str],
expected_opt_icp: Optional[str],
expected_err: Optional[Tuple[Type[Exception], str]],
monkeypatch: pytest.MonkeyPatch, set_cycling_type: Fixture
) -> None:
"""Test WorkflowConfig.process_initial_cycle_point().
"now" is assumed to be 2005-01-02T06:15+0530
Params:
cycling_type: Workflow cycling type.
scheduling_cfg: 'scheduling' section of workflow config.
expected_icp: The expected icp value that gets set.
expected_opt_icp: The expected value of options.icp that gets set
(this gets stored in the workflow DB).
expected_err: Exception class expected to be raised plus the message.
"""
set_cycling_type(cycling_type, time_zone="+0530")
mocked_config = Mock(cycling_type=cycling_type)
mocked_config.cfg = {
'scheduling': {
'initial cycle point constraints': [],
**scheduling_cfg
}
}
mocked_config.options.icp = None
monkeypatch.setattr('cylc.flow.config.get_current_time_string',
lambda: '20050102T0615+0530')
if expected_err:
err, msg = expected_err
with pytest.raises(err) as exc:
WorkflowConfig.process_initial_cycle_point(mocked_config)
assert msg in str(exc.value)
else:
WorkflowConfig.process_initial_cycle_point(mocked_config)
assert mocked_config.cfg[
'scheduling']['initial cycle point'] == expected_icp
assert str(mocked_config.initial_point) == expected_icp
opt_icp = mocked_config.options.icp
if opt_icp is not None:
opt_icp = str(loader.get_point(opt_icp).standardise())
assert opt_icp == expected_opt_icp
@pytest.mark.parametrize(
'startcp, starttask, expected, expected_err',
[
(
'20210120T1700+0530',
None,
'20210120T1700+0530',
None
),
(
'now',
None,
'20050102T0615+0530',
None
),
(
None,
None,
'18990501T0000+0530',
None
),
(
None,
['20090802T0615+0530/foo', '20090802T0515+0530/bar'],
'20090802T0515+0530',
None
),
(
'20210120T1700+0530',
['20090802T0615+0530/foo'],
None,
(
InputError,
"--start-cycle-point and --start-task are mutually exclusive"
),
)
]
)
def test_process_startcp(
startcp: Optional[str],
starttask: Optional[str],
expected: str,
expected_err: Optional[Tuple[Type[Exception], str]],
monkeypatch: pytest.MonkeyPatch, set_cycling_type: Fixture
) -> None:
"""Test WorkflowConfig.process_start_cycle_point().
An icp of 1899-05-01T00+0530 is assumed, and "now" is assumed to be
2005-01-02T06:15+0530
Params:
startcp: The start cycle point given by cli option.
expected: The expected startcp value that gets set.
expected_err: Expected exception.
"""
set_cycling_type(ISO8601_CYCLING_TYPE, time_zone="+0530")
mocked_config = Mock(initial_point='18990501T0000+0530')
mocked_config.options.startcp = startcp
mocked_config.options.starttask = starttask
monkeypatch.setattr('cylc.flow.config.get_current_time_string',
lambda: '20050102T0615+0530')
if expected_err is not None:
err, msg = expected_err
with pytest.raises(err) as exc:
WorkflowConfig.process_start_cycle_point(mocked_config)
assert msg in str(exc.value)
else:
WorkflowConfig.process_start_cycle_point(mocked_config)
assert str(mocked_config.start_point) == expected
@pytest.mark.parametrize(
'cycling_type, scheduling_cfg, options_fcp, expected_fcp, expected_err',
[
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2021',
'final cycle point': None,
},
None,
None,
None,
id="No fcp"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2021',
'final cycle point': '',
},
None,
None,
None,
id="Empty fcp in cfg"
# This test is needed because fcp is treated as string by parsec,
# unlike other cycle point settings (allows for e.g. '+P1Y')
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2016',
'final cycle point': '2021',
},
None,
'20210101T0000+0530',
None,
id="fcp in cfg"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2016',
'final cycle point': '2021',
},
'2019',
'20190101T0000+0530',
None,
id="Overriden by cli option"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2017-02-11',
'final cycle point': '+P4D',
},
None,
'20170215T0000+0530',
None,
id="Relative fcp"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2017-02-11',
'final cycle point': '---04',
},
None,
'20170215T0000+0530',
None,
id="Relative truncated fcp", marks=pytest.mark.xfail
# https://github.com/metomi/isodatetime/issues/80
),
pytest.param(
INTEGER_CYCLING_TYPE,
{
'initial cycle point': '1',
'final cycle point': '4',
},
None,
'4',
None,
id="Integer cycling"
),
pytest.param(
INTEGER_CYCLING_TYPE,
{
'initial cycle point': '1',
'final cycle point': '+P2',
},
None,
'3',
None,
id="Relative fcp, integer cycling"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2013',
'final cycle point': '2009',
},
None,
None,
(WorkflowConfigError,
"initial cycle point:20130101T0000+0530 is after the "
"final cycle point"),
id="fcp before icp"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2013',
'final cycle point': '-PT1S',
},
None,
None,
(WorkflowConfigError,
"initial cycle point:20130101T0000+0530 is after the "
"final cycle point"),
id="Negative relative fcp"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2013',
'final cycle point': '2021',
'final cycle point constraints': ['T00', 'T12']
},
None,
'20210101T0000+0530',
None,
id="Constraints"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2013',
'final cycle point': '2021-01-19',
'final cycle point constraints': ['--01-19', '--01-21']
},
'2021-01-20',
None,
(WorkflowConfigError, "does not meet the constraints"),
id="Violated constraints"
),
pytest.param(
ISO8601_CYCLING_TYPE,
{
'initial cycle point': '2013',
'final cycle point': '2021',
},
'reload',
'20210101T0000+0530',
None,
id="--fcp=reload"
),
]
)
def test_process_fcp(
cycling_type: str,
scheduling_cfg: dict,
options_fcp: Optional[str],
expected_fcp: Optional[str],
expected_err: Optional[Tuple[Type[Exception], str]],
set_cycling_type: Fixture
) -> None:
"""Test WorkflowConfig.process_final_cycle_point().
Params:
cycling_type: Workflow cycling type.
scheduling_cfg: 'scheduling' section of workflow config.
options_fcp: The fcp set by cli option.
expected_fcp: The expected fcp value that gets set.
expected_err: Exception class expected to be raised plus the message.
"""
set_cycling_type(cycling_type, time_zone='+0530')
mocked_config = Mock(cycling_type=cycling_type)
mocked_config.cfg = {
'scheduling': {
'final cycle point constraints': [],
**scheduling_cfg
}
}
mocked_config.initial_point = loader.get_point(
scheduling_cfg['initial cycle point']).standardise()
mocked_config.final_point = None
mocked_config.options.fcp = options_fcp
if expected_err:
err, msg = expected_err
with pytest.raises(err) as exc:
WorkflowConfig.process_final_cycle_point(mocked_config)
assert msg in str(exc.value)
else:
WorkflowConfig.process_final_cycle_point(mocked_config)
assert mocked_config.cfg[
'scheduling']['final cycle point'] == expected_fcp
assert str(mocked_config.final_point) == str(expected_fcp)
@pytest.mark.parametrize(
('cfg_stopcp', 'options_stopcp', 'expected_value',
'expected_options_value', 'expected_warning'),
[
pytest.param(
None, None, None, None, None,
id="No stopcp"
),
pytest.param(
'1993', None, '1993', None, None,
id="From config by default"
),
pytest.param(
'1993', '1066', '1066', '1066', None,
id="From options"
),
pytest.param(
'1993', 'reload', '1993', None, None,
id="From cfg if --stopcp=reload on restart"
),
pytest.param(
'3000', None, None, None,
"will have no effect as it is after the final cycle point",
id="stopcp > fcp"
),
]
)
def test_process_stop_cycle_point(
cfg_stopcp: Optional[str],
options_stopcp: Optional[str],
expected_value: Optional[str],
expected_options_value: Optional[str],
expected_warning: Optional[str],
set_cycling_type: Callable,
caplog: pytest.LogCaptureFixture
):
"""Test WorkflowConfig.process_stop_cycle_point().
Params:
cfg_stopcp: [scheduling]stop after cycle point
options_stopcp: The stopcp from cli option / database.
expected_value: The expected stopcp value that gets set.
expected_options_value: The expected options.stopcp that gets set.
expected_warning: Expected warning message, if any.
"""
set_cycling_type(ISO8601_CYCLING_TYPE, dump_format='CCYY')
caplog.set_level(logging.WARNING, CYLC_LOG)
fcp = loader.get_point('2012').standardise()
mock_config = Mock(
cfg={
'scheduling': {
'stop after cycle point': cfg_stopcp
}
},
final_point=fcp,
stop_point=None,
options=RunOptions(stopcp=options_stopcp),
)
WorkflowConfig.process_stop_cycle_point(mock_config)
assert str(mock_config.stop_point) == str(expected_value)
assert mock_config.cfg['scheduling']['stop after cycle point'] == (
expected_value
)
assert mock_config.options.stopcp == expected_options_value
if expected_warning:
assert expected_warning in caplog.text
else:
assert not caplog.record_tuples
@pytest.mark.parametrize(
'cfg_fcp, cfg_stopcp, opts, warning_expected',
[
pytest.param(
'2005', '2017', {}, True,
id="cfg stopcp > fcp bad"
),
pytest.param(
'2017', '2017', {}, False,
id="cfg stopcp == fcp ok"
),
pytest.param(
'', '', {'fcp': '2005', 'stopcp': '2017'}, True,
id="options stopcp > fcp bad"
),
pytest.param(
'', '', {'fcp': '2017', 'stopcp': '2017'}, False,
id="options stopcp == fcp ok"
),
pytest.param(
'2017', '2005', {'stopcp': '2022'}, True,
id="options stopcp > cfg fcp bad"
),
pytest.param(
'2017', '2005', {'stopcp': '2022'}, True,
id="options stopcp > cfg fcp bad"
),
pytest.param(
'2022', '2017', {'fcp': '2005'}, True,
id="cfg stopcp > options fcp bad"
),
pytest.param(
'', '2022', {}, False,
id="no fcp"
),
]
)
def test_stopcp_after_fcp(
cfg_fcp: str,
cfg_stopcp: str,
opts: Dict[str, str],
warning_expected: bool,
tmp_flow_config: Callable,
caplog: pytest.LogCaptureFixture,
):
"""Test that setting a stop after cycle point that is beyond the final
cycle point is handled correctly."""
caplog.set_level(logging.WARNING, CYLC_LOG)
reg = 'cassini'
flow_file: Path = tmp_flow_config(reg, f"""
[scheduler]
allow implicit tasks = True
[scheduling]
initial cycle point = 1997
final cycle point = {cfg_fcp}
stop after cycle point = {cfg_stopcp}
[[graph]]
P1Y = huygens
""")
cfg = WorkflowConfig(reg, flow_file, options=RunOptions(**opts))
msg = "will have no effect as it is after the final cycle point"
if warning_expected:
assert msg in caplog.text
assert cfg.stop_point is None
else:
assert msg not in caplog.text
if cfg_stopcp or opts.get('stopcp'):
assert cfg.stop_point
@pytest.mark.parametrize(
'scheduling_cfg, scheduling_expected, expected_err',
[
pytest.param(
{
'graph': {}
},
None,
(WorkflowConfigError, "No workflow dependency graph defined"),
id="Empty graph"
),
pytest.param(
{
'graph': {'R1': 'foo'}
},
{
'cycling mode': 'integer',
'initial cycle point': '1',
'final cycle point': '1',
'graph': {'R1': 'foo'}
},
None,
id="Pure acyclic graph"
),
pytest.param(
{
'cycling mode': "",
'graph': {'R1': 'foo'}
},
{
'cycling mode': "",
'graph': {'R1': 'foo'}
},
None,
id="Pure acyclic graph but datetime cycling"
),
pytest.param(
{
'graph': {'R1': 'foo', 'R2': 'bar'}
},
{
'graph': {'R1': 'foo', 'R2': 'bar'}
},
None,
id="Acyclic graph with >1 recurrence"
),
]
)
def test_prelim_process_graph(
scheduling_cfg: Dict[str, Any],
scheduling_expected: Optional[Dict[str, Any]],
expected_err: Optional[Tuple[Type[Exception], str]]):
"""Test WorkflowConfig.prelim_process_graph().
Params:
scheduling_cfg: 'scheduling' section of workflow config.
scheduling_expected: The expected scheduling section after preliminary
processing.
expected_err: Exception class expected to be raised plus the message.
"""
mock_config = Mock(cfg={
'scheduling': scheduling_cfg
})
if expected_err:
err, msg = expected_err
with pytest.raises(err) as exc:
WorkflowConfig.prelim_process_graph(mock_config)
assert msg in str(exc.value)
else:
WorkflowConfig.prelim_process_graph(mock_config)
assert mock_config.cfg['scheduling'] == scheduling_expected
def test_utc_mode(caplog, mock_glbl_cfg):
"""Test that UTC mode is handled correctly."""
caplog.set_level(logging.WARNING, CYLC_LOG)
def _test(utc_mode, expected, expected_warnings=0):
mock_glbl_cfg(
'cylc.flow.config.glbl_cfg',
f'''
[scheduler]
UTC mode = {utc_mode['glbl']}
'''
)
mock_config = Mock()
mock_config.cfg = {
'scheduler': {
'UTC mode': utc_mode['workflow']
}
}
mock_config.options.utc_mode = utc_mode['stored']
WorkflowConfig.process_utc_mode(mock_config)
assert mock_config.cfg['scheduler']['UTC mode'] is expected
assert get_utc_mode() is expected
assert len(caplog.record_tuples) == expected_warnings
caplog.clear()
tests = [
{
'utc_mode': {'glbl': True, 'workflow': None, 'stored': None},
'expected': True
},
{
'utc_mode': {'glbl': True, 'workflow': False, 'stored': None},
'expected': False
},
{
# On restart
'utc_mode': {'glbl': False, 'workflow': None, 'stored': True},
'expected': True
},
{
# Changed config value between restarts
'utc_mode': {'glbl': False, 'workflow': False, 'stored': True},
'expected': True,
'expected_warnings': 1
}
]
for case in tests:
_test(**case)
def test_cycle_point_tz(caplog, monkeypatch):
"""Test that `[scheduler]cycle point time zone` is handled correctly."""
caplog.set_level(logging.WARNING, CYLC_LOG)
local_tz = '-0230'
monkeypatch.setattr(
'cylc.flow.config.get_local_time_zone_format',
lambda: local_tz
)
def _test(cp_tz, utc_mode, expected, expected_warnings=0):
set_utc_mode(utc_mode)
mock_config = Mock()
mock_config.cfg = {
'scheduler': {
'cycle point time zone': cp_tz['workflow']
}
}
mock_config.options.cycle_point_tz = cp_tz['stored']
WorkflowConfig.process_cycle_point_tz(mock_config)
assert mock_config.cfg['scheduler'][
'cycle point time zone'] == expected
assert len(caplog.record_tuples) == expected_warnings
caplog.clear()
tests = [
{
'cp_tz': {'workflow': None, 'stored': None},
'utc_mode': True,
'expected': 'Z'
},
{
'cp_tz': {'workflow': None, 'stored': None},
'utc_mode': False,
'expected': 'Z'
},
{
'cp_tz': {'workflow': '+0530', 'stored': None},
'utc_mode': True,
'expected': '+0530'
},
{
# On restart
'cp_tz': {'workflow': None, 'stored': '+0530'},
'utc_mode': True,
'expected': '+0530',
'expected_warnings': 1
},
{
# Changed config value between restarts
'cp_tz': {'workflow': '+0530', 'stored': '-0030'},
'utc_mode': True,
'expected': '-0030',
'expected_warnings': 1
},
{
'cp_tz': {'workflow': 'Z', 'stored': 'Z'},
'utc_mode': False,
'expected': 'Z'
}
]
for case in tests:
_test(**case)
def test_rsync_includes_will_not_accept_sub_directories(tmp_flow_config):
reg = 'rsynctest'
flow_file = tmp_flow_config(reg, """
[scheduling]
initial cycle point = 2020-01-01
[[dependencies]]
graph = "blah => deeblah"
[scheduler]
install = dir/, dir2/subdir2/, file1, file2
""")
with pytest.raises(WorkflowConfigError) as exc:
WorkflowConfig(
workflow=reg, fpath=flow_file, options=Values()
)
assert "Directories can only be from the top level" in str(exc.value)
def test_valid_rsync_includes_returns_correct_list(tmp_flow_config):