Debian Codegraph @

tests/i/test_stop_after_cycle_point.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 """Test logic pertaining to the stop after cycle points.
18
19 This may be defined in different ways:
20 * In the workflow configuration.
21 * On the command line.
22 * Or loaded from the database.
23
24 When the workflow hits the "stop after" point, it should be wiped (i.e. set
25 to None).
26 """
27
28 from typing import Optional
29
30 from cylc.flow import commands
31 from cylc.flow.cycling.integer import IntegerPoint
32 from cylc.flow.id import Tokens
33 from cylc.flow.workflow_status import StopMode
34
35
36 async def test_stop_after_cycle_point(
37 flow,
38 scheduler,
39 run,
40 reflog,
41 complete,
42 ):
43 """Test the stop after cycle point.
44
45 This ensures:
46 * The stop after point gets loaded from the config.
47 * The workflow stops when it hits this point.
48 * The point gets wiped when the workflow hits this point.
49 * The point is stored/retrieved from the DB as appropriate.
50
51 """
52 async def stops_after_cycle(schd) -> Optional[str]:
53 """Run the workflow until it stops and return the cycle point."""
54 triggers = reflog(schd)
55 await complete(schd, timeout=2)
56 assert len(triggers) == 1 # only one task (i.e. cycle) should be run
57 return Tokens(list(triggers)[0][0], relative=True)['cycle']
58
59 def get_db_value(schd) -> Optional[str]:
60 """Return the cycle point value stored in the DB."""
61 with schd.workflow_db_mgr.get_pri_dao() as pri_dao:
62 return dict(pri_dao.select_workflow_params())['stopcp']
63
64 config = {
65 'scheduling': {
66 'cycling mode': 'integer',
67 'initial cycle point': '1',
68 'stop after cycle point': '1',
69 'graph': {
70 'P1': 'a[-P1] => a',
71 },
72 },
73 }
74 id_ = flow(config)
75 schd = scheduler(id_, paused_start=False)
76 async with run(schd):
77 # the cycle point should be loaded from the workflow configuration
78 assert schd.config.stop_point == IntegerPoint('1')
79
80 # this value should *not* be written to the database
81 assert get_db_value(schd) is None
82
83 # the workflow should stop after cycle 1
84 assert await stops_after_cycle(schd) == '1'
85
86 # change the configured cycle point to "2"
87 config['scheduling']['stop after cycle point'] = '2'
88 id_ = flow(config, workflow_id=id_)
89 schd = scheduler(id_, paused_start=False)
90 async with run(schd):
91 # the cycle point should be reloaded from the workflow configuration
92 assert schd.config.stop_point == IntegerPoint('2')
93
94 # this value should not be written to the database
95 assert get_db_value(schd) is None
96
97 # the workflow should stop after cycle 2
98 assert await stops_after_cycle(schd) == '2'
99
100 # override the configured value via the CLI option
101 schd = scheduler(id_, paused_start=False, **{'stopcp': '3'})
102 async with run(schd):
103 # the CLI should take precedence over the config
104 assert schd.config.stop_point == IntegerPoint('3')
105
106 # this value *should* be written to the database
107 assert get_db_value(schd) == '3'
108
109 # the workflow should stop after cycle 3
110 assert await stops_after_cycle(schd) == '3'
111
112 # once the workflow hits this point, it should get cleared
113 assert get_db_value(schd) is None
114
115 schd = scheduler(id_, paused_start=False)
116 async with run(schd):
117 # the workflow should fall back to the configured value
118 assert schd.config.stop_point == IntegerPoint('2')
119
120 # override this value whilst the workflow is running
121 await commands.run_cmd(
122 commands.stop(
123 schd,
124 cycle_point=IntegerPoint('4'),
125 mode=StopMode.REQUEST_CLEAN,
126 )
127 )
128 assert schd.config.stop_point == IntegerPoint('4')
129
130 # the new *should* be written to the database
131 assert get_db_value(schd) == '4'
132
133 schd = scheduler(id_, paused_start=False)
134 async with run(schd):
135 # the workflow should stop after cycle 4
136 assert await stops_after_cycle(schd) == '4'