|
20 | 20 | import mock |
21 | 21 | import os |
22 | 22 | from oslo_config import cfg |
| 23 | +from tooz.drivers.redis import RedisDriver |
23 | 24 | import tempfile |
24 | 25 |
|
25 | 26 | # This import must be early for import-time side-effects. |
26 | 27 | from st2tests.base import DbTestCase |
27 | 28 |
|
28 | 29 | import st2actions.worker as actions_worker |
| 30 | +import st2tests.config as tests_config |
29 | 31 | from st2common.constants import action as action_constants |
30 | 32 | from st2common.models.db.liveaction import LiveActionDB |
31 | 33 | from st2common.models.system.common import ResourceReference |
@@ -66,6 +68,35 @@ def setUpClass(cls): |
66 | 68 | ) |
67 | 69 | WorkerTestCase.local_action_db = models["actions"]["local.yaml"] |
68 | 70 |
|
| 71 | + @staticmethod |
| 72 | + def reset_config( |
| 73 | + graceful_shutdown=True, # default is True (st2common.config) |
| 74 | + exit_still_active_check=None, # default is 300 (st2common.config) |
| 75 | + still_active_check_interval=None, # default is 2 (st2common.config) |
| 76 | + service_registry=None, # default is False (st2common.config) |
| 77 | + ): |
| 78 | + tests_config.reset() |
| 79 | + tests_config.parse_args() |
| 80 | + cfg.CONF.set_override( |
| 81 | + name="graceful_shutdown", override=graceful_shutdown, group="actionrunner" |
| 82 | + ) |
| 83 | + if exit_still_active_check is not None: |
| 84 | + cfg.CONF.set_override( |
| 85 | + name="exit_still_active_check", |
| 86 | + override=exit_still_active_check, |
| 87 | + group="actionrunner", |
| 88 | + ) |
| 89 | + if still_active_check_interval is not None: |
| 90 | + cfg.CONF.set_override( |
| 91 | + name="still_active_check_interval", |
| 92 | + override=still_active_check_interval, |
| 93 | + group="actionrunner", |
| 94 | + ) |
| 95 | + if service_registry is not None: |
| 96 | + cfg.CONF.set_override( |
| 97 | + name="service_registry", override=service_registry, group="coordination" |
| 98 | + ) |
| 99 | + |
69 | 100 | def _get_liveaction_model(self, action_db, params): |
70 | 101 | status = action_constants.LIVEACTION_STATUS_REQUESTED |
71 | 102 | start_timestamp = date_utils.get_datetime_utc_now() |
@@ -116,9 +147,8 @@ def test_non_utf8_action_result_string(self): |
116 | 147 | ) |
117 | 148 |
|
118 | 149 | def test_worker_shutdown(self): |
119 | | - cfg.CONF.set_override( |
120 | | - name="graceful_shutdown", override=False, group="actionrunner" |
121 | | - ) |
| 150 | + self.reset_config(graceful_shutdown=False) |
| 151 | + |
122 | 152 | action_worker = actions_worker.get_worker() |
123 | 153 | temp_file = None |
124 | 154 |
|
@@ -169,14 +199,19 @@ def test_worker_shutdown(self): |
169 | 199 | runner_thread.wait() |
170 | 200 |
|
171 | 201 | @mock.patch.object( |
172 | | - coordination.NoOpDriver, |
| 202 | + RedisDriver, |
173 | 203 | "get_members", |
174 | | - mock.MagicMock(return_value=coordination.NoOpAsyncResult("member-1")), |
| 204 | + mock.MagicMock( |
| 205 | + return_value=coordination.NoOpAsyncResult(("member-1", "member-2")) |
| 206 | + ), |
175 | 207 | ) |
176 | 208 | def test_worker_graceful_shutdown_with_multiple_runners(self): |
177 | | - cfg.CONF.set_override( |
178 | | - name="graceful_shutdown", override=True, group="actionrunner" |
| 209 | + self.reset_config( |
| 210 | + exit_still_active_check=10, |
| 211 | + still_active_check_interval=1, |
| 212 | + service_registry=True, |
179 | 213 | ) |
| 214 | + |
180 | 215 | action_worker = actions_worker.get_worker() |
181 | 216 | temp_file = None |
182 | 217 |
|
@@ -234,9 +269,12 @@ def test_worker_graceful_shutdown_with_multiple_runners(self): |
234 | 269 | shutdown_thread.kill() |
235 | 270 |
|
236 | 271 | def test_worker_graceful_shutdown_with_single_runner(self): |
237 | | - cfg.CONF.set_override( |
238 | | - name="graceful_shutdown", override=True, group="actionrunner" |
| 272 | + self.reset_config( |
| 273 | + exit_still_active_check=10, |
| 274 | + still_active_check_interval=1, |
| 275 | + service_registry=True, |
239 | 276 | ) |
| 277 | + |
240 | 278 | action_worker = actions_worker.get_worker() |
241 | 279 | temp_file = None |
242 | 280 |
|
@@ -296,17 +334,13 @@ def test_worker_graceful_shutdown_with_single_runner(self): |
296 | 334 | shutdown_thread.kill() |
297 | 335 |
|
298 | 336 | @mock.patch.object( |
299 | | - coordination.NoOpDriver, |
| 337 | + RedisDriver, |
300 | 338 | "get_members", |
301 | | - mock.MagicMock(return_value=coordination.NoOpAsyncResult("member-1")), |
| 339 | + mock.MagicMock(return_value=coordination.NoOpAsyncResult(("member-1",))), |
302 | 340 | ) |
303 | 341 | def test_worker_graceful_shutdown_exit_timeout(self): |
304 | | - cfg.CONF.set_override( |
305 | | - name="graceful_shutdown", override=True, group="actionrunner" |
306 | | - ) |
307 | | - cfg.CONF.set_override( |
308 | | - name="exit_still_active_check", override=5, group="actionrunner" |
309 | | - ) |
| 342 | + self.reset_config(exit_still_active_check=5) |
| 343 | + |
310 | 344 | action_worker = actions_worker.get_worker() |
311 | 345 | temp_file = None |
312 | 346 |
|
|
0 commit comments