From 9fc698b4c6ca64ce54712670e2d578847f4224e0 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Wed, 22 May 2024 22:16:05 -0500 Subject: [PATCH 1/9] tests: Ensure tests can run in isolation Various tests were relying on the side effects of tests that nosetest runs before they ran. These include: - 3 in test_action_alias_utils.py::TestInjectImmutableParameters - 2 in test_jinja_render_data_filters.py - 1 in test_logging_middleware.py - 9 in test_operators.py::SearchOperatorTest - 3 in test_util_payload.py In particular, the oslo config initialization from the tests in st2common/tests/unit/services/ happens before these test ran, obscuring their dependence on this initialization. Pants runs each test file separately for fine-grained caching. --- st2common/tests/unit/test_action_alias_utils.py | 6 ++++++ st2common/tests/unit/test_jinja_render_data_filters.py | 6 ++++++ st2common/tests/unit/test_logging_middleware.py | 6 ++++++ st2common/tests/unit/test_operators.py | 6 ++++++ st2common/tests/unit/test_util_payload.py | 2 ++ 5 files changed, 26 insertions(+) diff --git a/st2common/tests/unit/test_action_alias_utils.py b/st2common/tests/unit/test_action_alias_utils.py index ec3251070a..1fc54359b0 100644 --- a/st2common/tests/unit/test_action_alias_utils.py +++ b/st2common/tests/unit/test_action_alias_utils.py @@ -30,6 +30,7 @@ search_regex_tokens, inject_immutable_parameters, ) +import st2tests.config as tests_config class TestActionAliasParser(TestCase): @@ -357,6 +358,11 @@ def test_subpatterns(self): class TestInjectImmutableParameters(TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + def test_immutable_parameters_are_injected(self): action_alias_db = Mock() action_alias_db.immutable_parameters = {"env": "dev"} diff --git a/st2common/tests/unit/test_jinja_render_data_filters.py b/st2common/tests/unit/test_jinja_render_data_filters.py index 8db175cac2..bc9ad1c02c 100644 --- a/st2common/tests/unit/test_jinja_render_data_filters.py +++ b/st2common/tests/unit/test_jinja_render_data_filters.py @@ -21,9 +21,15 @@ from st2common.constants.keyvalue import FULL_SYSTEM_SCOPE from st2common.util import jinja as jinja_utils from st2common.services.keyvalues import KeyValueLookup +import st2tests.config as tests_config class JinjaUtilsDataFilterTestCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + def test_filter_from_json_string(self): env = jinja_utils.get_jinja_environment() expected_obj = {"a": "b", "c": {"d": "e", "f": 1, "g": True}} diff --git a/st2common/tests/unit/test_logging_middleware.py b/st2common/tests/unit/test_logging_middleware.py index 2da4bb5875..8521a622e6 100644 --- a/st2common/tests/unit/test_logging_middleware.py +++ b/st2common/tests/unit/test_logging_middleware.py @@ -20,11 +20,17 @@ from st2common.middleware.logging import LoggingMiddleware from st2common.constants.secrets import MASKED_ATTRIBUTE_VALUE +import st2tests.config as tests_config __all__ = ["LoggingMiddlewareTestCase"] class LoggingMiddlewareTestCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + @mock.patch("st2common.middleware.logging.LOG") @mock.patch("st2common.middleware.logging.Request") def test_secret_parameters_are_masked_in_log_message(self, mock_request, mock_log): diff --git a/st2common/tests/unit/test_operators.py b/st2common/tests/unit/test_operators.py index 9bb6161f91..39b2cc7a8c 100644 --- a/st2common/tests/unit/test_operators.py +++ b/st2common/tests/unit/test_operators.py @@ -18,6 +18,7 @@ from st2common import operators from st2common.util import date as date_utils +import st2tests.config as tests_config def list_of_dicts_strict_equal(lofd1, lofd2): @@ -157,6 +158,11 @@ def test_less_simple_dicts(self): class SearchOperatorTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + # The search command extends the rules engine into being a recursive descent # parser. As such, its tests are much more complex than other commands, so we # pull its tests out into their own test case. diff --git a/st2common/tests/unit/test_util_payload.py b/st2common/tests/unit/test_util_payload.py index d6629d6d57..715786e2f0 100644 --- a/st2common/tests/unit/test_util_payload.py +++ b/st2common/tests/unit/test_util_payload.py @@ -18,6 +18,7 @@ import unittest from st2common.util.payload import PayloadLookup +import st2tests.config as tests_config __all__ = ["PayloadLookupTestCase"] @@ -32,6 +33,7 @@ def setUpClass(cls): } ) super(PayloadLookupTestCase, cls).setUpClass() + tests_config.parse_args() def test_get_key(self): self.assertEqual(self.payload.get_value("trigger.pikachu"), ["Has no ears"]) From cc0910e43ddb0010fb0ec6eaae9f65bf2eb3d8d6 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Thu, 23 May 2024 16:22:03 -0500 Subject: [PATCH 2/9] pants: monkey patch in each test_purge* test file so pytest can run them --- st2common/tests/unit/test_purge_rule_enforcement.py | 6 ++++++ st2common/tests/unit/test_purge_task_executions.py | 6 ++++++ st2common/tests/unit/test_purge_token.py | 6 ++++++ st2common/tests/unit/test_purge_trace.py | 6 ++++++ st2common/tests/unit/test_purge_worklows.py | 6 ++++++ 5 files changed, 30 insertions(+) diff --git a/st2common/tests/unit/test_purge_rule_enforcement.py b/st2common/tests/unit/test_purge_rule_enforcement.py index 90b4d23799..1b00228fa3 100644 --- a/st2common/tests/unit/test_purge_rule_enforcement.py +++ b/st2common/tests/unit/test_purge_rule_enforcement.py @@ -13,6 +13,12 @@ # limitations under the License. from __future__ import absolute_import + +# pytest: make sure monkey_patching happens before importing mongoengine +from st2common.util.monkey_patch import monkey_patch + +monkey_patch() + from datetime import timedelta import bson diff --git a/st2common/tests/unit/test_purge_task_executions.py b/st2common/tests/unit/test_purge_task_executions.py index b0c7cd8bc2..b5c2dc19fe 100644 --- a/st2common/tests/unit/test_purge_task_executions.py +++ b/st2common/tests/unit/test_purge_task_executions.py @@ -13,6 +13,12 @@ # limitations under the License. from __future__ import absolute_import + +# pytest: make sure monkey_patching happens before importing mongoengine +from st2common.util.monkey_patch import monkey_patch + +monkey_patch() + from datetime import timedelta from st2common import log as logging diff --git a/st2common/tests/unit/test_purge_token.py b/st2common/tests/unit/test_purge_token.py index 75c24e62cd..1bad08d097 100644 --- a/st2common/tests/unit/test_purge_token.py +++ b/st2common/tests/unit/test_purge_token.py @@ -13,6 +13,12 @@ # limitations under the License. from __future__ import absolute_import + +# pytest: make sure monkey_patching happens before importing mongoengine +from st2common.util.monkey_patch import monkey_patch + +monkey_patch() + from datetime import timedelta import bson diff --git a/st2common/tests/unit/test_purge_trace.py b/st2common/tests/unit/test_purge_trace.py index 9a819b4018..7dde63f9f1 100644 --- a/st2common/tests/unit/test_purge_trace.py +++ b/st2common/tests/unit/test_purge_trace.py @@ -13,6 +13,12 @@ # limitations under the License. from __future__ import absolute_import + +# pytest: make sure monkey_patching happens before importing mongoengine +from st2common.util.monkey_patch import monkey_patch + +monkey_patch() + from datetime import timedelta import bson diff --git a/st2common/tests/unit/test_purge_worklows.py b/st2common/tests/unit/test_purge_worklows.py index 2975c504cf..383030b1e3 100644 --- a/st2common/tests/unit/test_purge_worklows.py +++ b/st2common/tests/unit/test_purge_worklows.py @@ -13,6 +13,12 @@ # limitations under the License. from __future__ import absolute_import + +# pytest: make sure monkey_patching happens before importing mongoengine +from st2common.util.monkey_patch import monkey_patch + +monkey_patch() + from datetime import timedelta from st2common import log as logging From ecd30c8e84212ddfe2eb7a1dd6282df94acd319c Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Thu, 23 May 2024 23:30:36 -0500 Subject: [PATCH 3/9] tests: drop unnecessary calls to tests_config.parse_args() This looks like copy pasta as some of these files have a comment saying that running this before importing something else is required. However, by importing st2tests, that already implicitly happens in st2tests/st2tests/base.py. Then tests_config.parse_args() gets called again in the class init. Plus, I reviewed all the other imports, and none of them have import time side effects that matter for oslo config bits. So, these calls are not necessary, and the comments about them are wrong. --- st2common/tests/unit/services/test_workflow.py | 4 ---- st2common/tests/unit/services/test_workflow_cancellation.py | 4 ---- .../tests/unit/services/test_workflow_identify_orphans.py | 5 ----- st2common/tests/unit/services/test_workflow_rerun.py | 4 ---- .../tests/unit/services/test_workflow_service_retries.py | 5 ----- 5 files changed, 22 deletions(-) diff --git a/st2common/tests/unit/services/test_workflow.py b/st2common/tests/unit/services/test_workflow.py index bcce91af00..d4897ff322 100644 --- a/st2common/tests/unit/services/test_workflow.py +++ b/st2common/tests/unit/services/test_workflow.py @@ -24,10 +24,6 @@ import st2tests -import st2tests.config as tests_config - -tests_config.parse_args() - from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar from st2common.exceptions import action as action_exc diff --git a/st2common/tests/unit/services/test_workflow_cancellation.py b/st2common/tests/unit/services/test_workflow_cancellation.py index d8b7b2206f..88e81e4fbf 100644 --- a/st2common/tests/unit/services/test_workflow_cancellation.py +++ b/st2common/tests/unit/services/test_workflow_cancellation.py @@ -21,10 +21,6 @@ import st2tests -import st2tests.config as tests_config - -tests_config.parse_args() - from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar from st2common.models.db import liveaction as lv_db_models diff --git a/st2common/tests/unit/services/test_workflow_identify_orphans.py b/st2common/tests/unit/services/test_workflow_identify_orphans.py index 7110b509c9..ba1df395a8 100644 --- a/st2common/tests/unit/services/test_workflow_identify_orphans.py +++ b/st2common/tests/unit/services/test_workflow_identify_orphans.py @@ -22,11 +22,6 @@ import st2tests -# XXX: actionsensor import depends on config being setup. -import st2tests.config as tests_config - -tests_config.parse_args() - from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar from st2common.constants import action as ac_const diff --git a/st2common/tests/unit/services/test_workflow_rerun.py b/st2common/tests/unit/services/test_workflow_rerun.py index b1bcb7417c..bb3d1595ba 100644 --- a/st2common/tests/unit/services/test_workflow_rerun.py +++ b/st2common/tests/unit/services/test_workflow_rerun.py @@ -23,10 +23,6 @@ import st2tests -import st2tests.config as tests_config - -tests_config.parse_args() - from local_runner import local_shell_command_runner from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar diff --git a/st2common/tests/unit/services/test_workflow_service_retries.py b/st2common/tests/unit/services/test_workflow_service_retries.py index 0e322fe573..ca2fab6f9f 100644 --- a/st2common/tests/unit/services/test_workflow_service_retries.py +++ b/st2common/tests/unit/services/test_workflow_service_retries.py @@ -30,11 +30,6 @@ import st2tests -# XXX: actionsensor import depends on config being setup. -import st2tests.config as tests_config - -tests_config.parse_args() - from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar from st2common.constants import action as ac_const From 1fd30eea63c30be00c7c3bb9482bf6a14e607a5a Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Thu, 23 May 2024 23:32:20 -0500 Subject: [PATCH 4/9] tests: add missing monkey_patch for isolated test support pants runs each test file separately. test_workflow_rerun only worked under nosetest because earlier files already did the monkey_patch. Without this, running this file in isolation, with either nosetest or pytest, hangs. --- st2common/tests/unit/services/test_workflow_rerun.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/st2common/tests/unit/services/test_workflow_rerun.py b/st2common/tests/unit/services/test_workflow_rerun.py index bb3d1595ba..c91e140d6c 100644 --- a/st2common/tests/unit/services/test_workflow_rerun.py +++ b/st2common/tests/unit/services/test_workflow_rerun.py @@ -15,6 +15,11 @@ from __future__ import absolute_import +from st2common.util.monkey_patch import monkey_patch + +monkey_patch() + + import mock import uuid From b709b9d9ee8407d68f74b1fc18a6fa45b5b934aa Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 24 May 2024 11:40:17 -0500 Subject: [PATCH 5/9] tests: reorder st2tests imports in st2actions tests for import side-effects importing anything form st2tests already handles running st2tests.config.parse_args() on import before loading the files from st2common that need those side effects. So, rely on that, and on the db test case base classes for running parse_args() where appropriate. The import side-effects are unfortunate, but this reduces how many places are making those changes. --- st2actions/tests/unit/policies/test_base.py | 7 ++----- st2actions/tests/unit/policies/test_concurrency.py | 7 ++----- .../unit/policies/test_concurrency_by_attr.py | 6 ++---- st2actions/tests/unit/test_action_runner_worker.py | 9 ++++++--- .../tests/unit/test_execution_cancellation.py | 7 ++----- st2actions/tests/unit/test_executions.py | 7 ++----- st2actions/tests/unit/test_notifier.py | 6 ++---- st2actions/tests/unit/test_output_schema.py | 5 +---- st2actions/tests/unit/test_parallel_ssh.py | 7 +++++-- .../unit/test_paramiko_remote_script_runner.py | 7 +++++-- st2actions/tests/unit/test_paramiko_ssh.py | 7 +++++-- st2actions/tests/unit/test_paramiko_ssh_runner.py | 7 +++++-- st2actions/tests/unit/test_queue_consumers.py | 8 +++----- st2actions/tests/unit/test_remote_runners.py | 14 ++++++++------ st2actions/tests/unit/test_runner_container.py | 7 +++---- st2actions/tests/unit/test_scheduler.py | 4 ---- st2actions/tests/unit/test_scheduler_entrypoint.py | 4 ---- st2actions/tests/unit/test_scheduler_retry.py | 8 +++----- st2actions/tests/unit/test_worker.py | 8 ++++---- st2actions/tests/unit/test_workflow_engine.py | 6 +----- 20 files changed, 61 insertions(+), 80 deletions(-) diff --git a/st2actions/tests/unit/policies/test_base.py b/st2actions/tests/unit/policies/test_base.py index fb475fbf66..1b345d2b7a 100644 --- a/st2actions/tests/unit/policies/test_base.py +++ b/st2actions/tests/unit/policies/test_base.py @@ -16,9 +16,8 @@ from __future__ import absolute_import import mock -from st2tests import config as test_config - -test_config.parse_args() +# This import must be early for import-time side-effects. +from st2tests.base import CleanDbTestCase, DbTestCase import st2common from st2common.bootstrap.policiesregistrar import register_policy_types @@ -28,8 +27,6 @@ from st2common.services import action as action_service from st2common.services import policies as policy_service from st2common.bootstrap import runnersregistrar as runners_registrar -from st2tests.base import DbTestCase -from st2tests.base import CleanDbTestCase from st2tests.fixtures.generic.fixture import PACK_NAME as PACK from st2tests.fixturesloader import FixturesLoader diff --git a/st2actions/tests/unit/policies/test_concurrency.py b/st2actions/tests/unit/policies/test_concurrency.py index 1be4b86da3..7612bd5396 100644 --- a/st2actions/tests/unit/policies/test_concurrency.py +++ b/st2actions/tests/unit/policies/test_concurrency.py @@ -19,10 +19,9 @@ from mock import call from six.moves import range +# This import must be early for import-time side-effects. # Importing st2actions.scheduler relies on config being parsed :/ -import st2tests.config as tests_config - -tests_config.parse_args() +from st2tests import DbTestCase, EventletTestCase, ExecutionDbTestCase import st2common from st2actions.scheduler import handler as scheduling_queue @@ -38,8 +37,6 @@ from st2common.transport.liveaction import LiveActionPublisher from st2common.transport.publishers import CUDPublisher from st2common.bootstrap import runnersregistrar as runners_registrar -from st2tests import DbTestCase, EventletTestCase -from st2tests import ExecutionDbTestCase import st2tests.config as tests_config from st2tests.fixtures.generic.fixture import PACK_NAME as PACK from st2tests.fixturesloader import FixturesLoader diff --git a/st2actions/tests/unit/policies/test_concurrency_by_attr.py b/st2actions/tests/unit/policies/test_concurrency_by_attr.py index 937a4149ef..2edcdb4af7 100644 --- a/st2actions/tests/unit/policies/test_concurrency_by_attr.py +++ b/st2actions/tests/unit/policies/test_concurrency_by_attr.py @@ -18,10 +18,9 @@ import mock from mock import call +# This import must be early for import-time side-effects. # Importing st2actions.scheduler relies on config being parsed :/ -import st2tests.config as tests_config - -tests_config.parse_args() +from st2tests import ExecutionDbTestCase, EventletTestCase import st2common from st2actions.scheduler import handler as scheduling_queue @@ -36,7 +35,6 @@ from st2common.transport.liveaction import LiveActionPublisher from st2common.transport.publishers import CUDPublisher from st2common.bootstrap import runnersregistrar as runners_registrar -from st2tests import ExecutionDbTestCase, EventletTestCase import st2tests.config as tests_config from st2tests.fixtures.generic.fixture import PACK_NAME as PACK from st2tests.fixturesloader import FixturesLoader diff --git a/st2actions/tests/unit/test_action_runner_worker.py b/st2actions/tests/unit/test_action_runner_worker.py index 96e049b179..8477281b97 100644 --- a/st2actions/tests/unit/test_action_runner_worker.py +++ b/st2actions/tests/unit/test_action_runner_worker.py @@ -20,14 +20,17 @@ from st2common.transport.consumers import ActionsQueueConsumer from st2common.models.db.liveaction import LiveActionDB -from st2tests import config as test_config - -test_config.parse_args() +from st2tests import config as tests_config __all__ = ["ActionsQueueConsumerTestCase"] class ActionsQueueConsumerTestCase(TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + def test_process_right_dispatcher_is_used(self): handler = Mock() handler.message_type = LiveActionDB diff --git a/st2actions/tests/unit/test_execution_cancellation.py b/st2actions/tests/unit/test_execution_cancellation.py index 96eacc1a89..539e930d25 100644 --- a/st2actions/tests/unit/test_execution_cancellation.py +++ b/st2actions/tests/unit/test_execution_cancellation.py @@ -20,10 +20,8 @@ from oslo_config import cfg -# XXX: actionsensor import depends on config being setup. -import st2tests.config as tests_config - -tests_config.parse_args() +# This import must be early for import-time side-effects. +from st2tests import ExecutionDbTestCase from st2common.constants import action as action_constants from st2common.models.api.action import ActionAPI @@ -36,7 +34,6 @@ from st2common.services import trace as trace_service from st2common.transport.liveaction import LiveActionPublisher from st2common.transport.publishers import CUDPublisher -from st2tests import ExecutionDbTestCase from st2tests.fixtures.generic.fixture import PACK_NAME as PACK from st2tests.fixturesloader import FixturesLoader from st2tests.mocks.execution import MockExecutionPublisher diff --git a/st2actions/tests/unit/test_executions.py b/st2actions/tests/unit/test_executions.py index 1c95a51061..dcb50c70f5 100644 --- a/st2actions/tests/unit/test_executions.py +++ b/st2actions/tests/unit/test_executions.py @@ -18,10 +18,8 @@ import mock -# XXX: actionsensor import depends on config being setup. -import st2tests.config as tests_config - -tests_config.parse_args() +# This import must be early for import-time side-effects. +from st2tests import ExecutionDbTestCase import st2common.bootstrap.runnersregistrar as runners_registrar from st2common.constants import action as action_constants @@ -46,7 +44,6 @@ from local_runner.local_shell_command_runner import LocalShellCommandRunner from st2tests.fixtures.packs import executions as fixture -from st2tests import ExecutionDbTestCase from st2tests.mocks.liveaction import MockLiveActionPublisher diff --git a/st2actions/tests/unit/test_notifier.py b/st2actions/tests/unit/test_notifier.py index b648d7fad3..f599cea08a 100644 --- a/st2actions/tests/unit/test_notifier.py +++ b/st2actions/tests/unit/test_notifier.py @@ -19,9 +19,8 @@ import bson import mock -import st2tests.config as tests_config - -tests_config.parse_args() +# This import must be early for import-time side-effects. +from st2tests.base import CleanDbTestCase from st2actions.notifier.notifier import Notifier from st2common.constants.action import LIVEACTION_COMPLETED_STATES @@ -40,7 +39,6 @@ from st2common.models.system.common import ResourceReference from st2common.util import date as date_utils from st2common.util import isotime -from st2tests.base import CleanDbTestCase ACTION_TRIGGER_TYPE = INTERNAL_TRIGGER_TYPES["action"][0] NOTIFY_TRIGGER_TYPE = INTERNAL_TRIGGER_TYPES["action"][1] diff --git a/st2actions/tests/unit/test_output_schema.py b/st2actions/tests/unit/test_output_schema.py index a66f9ffb12..d4ae6bd9cc 100644 --- a/st2actions/tests/unit/test_output_schema.py +++ b/st2actions/tests/unit/test_output_schema.py @@ -20,12 +20,9 @@ from python_runner import python_runner from orquesta_runner import orquesta_runner +# This import must be early for import-time side-effects. import st2tests -import st2tests.config as tests_config - -tests_config.parse_args() - from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar from st2common.constants import action as ac_const diff --git a/st2actions/tests/unit/test_parallel_ssh.py b/st2actions/tests/unit/test_parallel_ssh.py index c1ef2e998a..70c9b79b68 100644 --- a/st2actions/tests/unit/test_parallel_ssh.py +++ b/st2actions/tests/unit/test_parallel_ssh.py @@ -25,8 +25,6 @@ from st2common.runners.paramiko_ssh import SSHCommandTimeoutError import st2tests.config as tests_config -tests_config.parse_args() - MOCK_STDERR_SUDO_PASSWORD_ERROR = """ [sudo] password for bar: Sorry, try again.\n [sudo] password for bar:' Sorry, try again.\n @@ -36,6 +34,11 @@ class ParallelSSHTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + @patch("paramiko.SSHClient", Mock) @patch.object( ParamikoSSHClient, diff --git a/st2actions/tests/unit/test_paramiko_remote_script_runner.py b/st2actions/tests/unit/test_paramiko_remote_script_runner.py index 27495463ff..726456d11a 100644 --- a/st2actions/tests/unit/test_paramiko_remote_script_runner.py +++ b/st2actions/tests/unit/test_paramiko_remote_script_runner.py @@ -22,8 +22,6 @@ # before importing remote_script_runner classes. import st2tests.config as tests_config -tests_config.parse_args() - from st2common.util import jsonify from st2common.models.db.action import ActionDB from st2common.runners.parallel_ssh import ParallelSSHClient @@ -48,6 +46,11 @@ class ParamikoScriptRunnerTestCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + @patch("st2common.runners.parallel_ssh.ParallelSSHClient", Mock) @patch.object(jsonify, "json_loads", MagicMock(return_value={})) @patch.object(ParallelSSHClient, "run", MagicMock(return_value={})) diff --git a/st2actions/tests/unit/test_paramiko_ssh.py b/st2actions/tests/unit/test_paramiko_ssh.py index 1ccdc110a2..d60c227b1d 100644 --- a/st2actions/tests/unit/test_paramiko_ssh.py +++ b/st2actions/tests/unit/test_paramiko_ssh.py @@ -29,12 +29,15 @@ from st2tests.fixturesloader import get_resources_base_path import st2tests.config as tests_config -tests_config.parse_args() - __all__ = ["ParamikoSSHClientTestCase"] class ParamikoSSHClientTestCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + @patch("paramiko.SSHClient", Mock) def setUp(self): """ diff --git a/st2actions/tests/unit/test_paramiko_ssh_runner.py b/st2actions/tests/unit/test_paramiko_ssh_runner.py index 6700bb0347..116ea4eced 100644 --- a/st2actions/tests/unit/test_paramiko_ssh_runner.py +++ b/st2actions/tests/unit/test_paramiko_ssh_runner.py @@ -30,8 +30,6 @@ import st2tests.config as tests_config from st2tests.fixturesloader import get_resources_base_path -tests_config.parse_args() - class Runner(BaseParallelSSHRunner): def run(self): @@ -39,6 +37,11 @@ def run(self): class ParamikoSSHRunnerTestCase(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + @mock.patch("st2common.runners.paramiko_ssh_runner.ParallelSSHClient") def test_pre_run(self, mock_client): # Test case which verifies that ParamikoSSHClient is instantiated with the correct arguments diff --git a/st2actions/tests/unit/test_queue_consumers.py b/st2actions/tests/unit/test_queue_consumers.py index 0ddfaea164..594ecac401 100644 --- a/st2actions/tests/unit/test_queue_consumers.py +++ b/st2actions/tests/unit/test_queue_consumers.py @@ -15,13 +15,12 @@ from __future__ import absolute_import -import st2tests.config as tests_config - -tests_config.parse_args() - import mock from kombu.message import Message +# This import must be early for import-time side-effects. +from st2tests.base import ExecutionDbTestCase + from st2actions import worker from st2actions.scheduler import entrypoint as scheduling from st2actions.scheduler import handler as scheduling_queue @@ -35,7 +34,6 @@ from st2common.transport.publishers import PoolPublisher from st2common.util import action_db as action_utils from st2common.util import date as date_utils -from st2tests.base import ExecutionDbTestCase from st2tests.fixtures.packs.core.fixture import PACK_PATH as CORE_PACK_PATH diff --git a/st2actions/tests/unit/test_remote_runners.py b/st2actions/tests/unit/test_remote_runners.py index 19f5cb40f1..06f9058fa9 100644 --- a/st2actions/tests/unit/test_remote_runners.py +++ b/st2actions/tests/unit/test_remote_runners.py @@ -13,18 +13,20 @@ # See the License for the specific language governing permissions and # limitations under the License. -# XXX: FabricRunner import depends on config being setup. -from __future__ import absolute_import -import st2tests.config as tests_config - -tests_config.parse_args() - from unittest import TestCase +# This import must be early for import-time side-effects. +import st2tests.config as tests_config + from st2common.models.system.action import RemoteScriptAction class RemoteScriptActionTestCase(TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + def test_parameter_formatting(self): # Only named args named_args = { diff --git a/st2actions/tests/unit/test_runner_container.py b/st2actions/tests/unit/test_runner_container.py index 1134e176bf..15a591649f 100644 --- a/st2actions/tests/unit/test_runner_container.py +++ b/st2actions/tests/unit/test_runner_container.py @@ -19,6 +19,9 @@ from oslo_config import cfg +# This import must be early for import-time side-effects. +from st2tests.base import DbTestCase + from st2common.constants import action as action_constants from st2common.runners.base import get_runner from st2common.exceptions.actionrunner import ( @@ -35,10 +38,6 @@ from st2common.util import date as date_utils from st2common.transport.publishers import PoolPublisher -from st2tests.base import DbTestCase -import st2tests.config as tests_config - -tests_config.parse_args() from st2tests.fixtures.generic.fixture import PACK_NAME as FIXTURES_PACK from st2tests.fixturesloader import FixturesLoader diff --git a/st2actions/tests/unit/test_scheduler.py b/st2actions/tests/unit/test_scheduler.py index 7556c7b036..65a59bd869 100644 --- a/st2actions/tests/unit/test_scheduler.py +++ b/st2actions/tests/unit/test_scheduler.py @@ -19,10 +19,6 @@ import mock import eventlet -from st2tests import config as test_config - -test_config.parse_args() - import st2common from st2tests import ExecutionDbTestCase from st2tests.fixtures.generic.fixture import PACK_NAME as PACK diff --git a/st2actions/tests/unit/test_scheduler_entrypoint.py b/st2actions/tests/unit/test_scheduler_entrypoint.py index 2bc535d99d..2862ba2b3c 100644 --- a/st2actions/tests/unit/test_scheduler_entrypoint.py +++ b/st2actions/tests/unit/test_scheduler_entrypoint.py @@ -16,10 +16,6 @@ import eventlet import mock -from st2tests import config as test_config - -test_config.parse_args() - from st2actions.cmd.scheduler import _run_scheduler from st2actions.scheduler.handler import ActionExecutionSchedulingQueueHandler from st2actions.scheduler.entrypoint import SchedulerEntrypoint diff --git a/st2actions/tests/unit/test_scheduler_retry.py b/st2actions/tests/unit/test_scheduler_retry.py index ad1f221df1..d975964bc6 100644 --- a/st2actions/tests/unit/test_scheduler_retry.py +++ b/st2actions/tests/unit/test_scheduler_retry.py @@ -13,19 +13,17 @@ # See the License for the specific language governing permissions and # limitations under the License. +# This import must be first for import-time side-effects. +from st2tests.base import CleanDbTestCase + import eventlet import mock import pymongo import uuid -from st2tests import config as test_config - -test_config.parse_args() - from st2actions.scheduler import handler from st2common.models.db import execution_queue as ex_q_db from st2common.persistence import execution_queue as ex_q_db_access -from st2tests.base import CleanDbTestCase __all__ = ["SchedulerHandlerRetryTestCase"] diff --git a/st2actions/tests/unit/test_worker.py b/st2actions/tests/unit/test_worker.py index ca2bf172dc..b335b6f2be 100644 --- a/st2actions/tests/unit/test_worker.py +++ b/st2actions/tests/unit/test_worker.py @@ -14,6 +14,7 @@ # limitations under the License. from __future__ import absolute_import + from bson.errors import InvalidStringData import eventlet import mock @@ -21,6 +22,9 @@ from oslo_config import cfg import tempfile +# This import must be early for import-time side-effects. +from st2tests.base import DbTestCase + import st2actions.worker as actions_worker from st2common.constants import action as action_constants from st2common.models.db.liveaction import LiveActionDB @@ -33,14 +37,10 @@ from st2common.bootstrap import runnersregistrar as runners_registrar from local_runner.local_shell_command_runner import LocalShellCommandRunner -from st2tests.base import DbTestCase from st2tests.fixtures.generic.fixture import PACK_NAME as FIXTURES_PACK from st2tests.fixturesloader import FixturesLoader -import st2tests.config as tests_config from six.moves import range -tests_config.parse_args() - TEST_FIXTURES = {"actions": ["local.yaml"]} NON_UTF8_RESULT = { diff --git a/st2actions/tests/unit/test_workflow_engine.py b/st2actions/tests/unit/test_workflow_engine.py index 955f7ca2f0..68d68a3b2f 100644 --- a/st2actions/tests/unit/test_workflow_engine.py +++ b/st2actions/tests/unit/test_workflow_engine.py @@ -18,17 +18,13 @@ import eventlet import mock +# This import must be early for import-time side-effects. import st2tests from orquesta import statuses as wf_statuses from oslo_config import cfg from tooz import coordination -# XXX: actionsensor import depends on config being setup. -import st2tests.config as tests_config - -tests_config.parse_args() - from st2actions.workflows import workflows from st2common.bootstrap import actionsregistrar from st2common.bootstrap import runnersregistrar From e5bba8dc6ff3ab664ed763947bbf129843fbbfab Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 24 May 2024 11:55:33 -0500 Subject: [PATCH 6/9] tests: reorder st2tests imports in st2common tests for import side-effects importing anything form st2tests already handles running st2tests.config.parse_args() on import before loading the files from st2common that need those side effects. So, rely on that, and on the db test case base classes for running parse_args() where appropriate. The import side-effects are unfortunate, but this reduces how many places are making those changes. --- st2common/tests/unit/services/test_policy.py | 6 ++---- st2common/tests/unit/test_executions_util.py | 6 +++--- st2common/tests/unit/test_jsonify.py | 3 +-- st2common/tests/unit/test_runners_utils.py | 11 ++--------- 4 files changed, 8 insertions(+), 18 deletions(-) diff --git a/st2common/tests/unit/services/test_policy.py b/st2common/tests/unit/services/test_policy.py index a590322452..5e891e9c65 100644 --- a/st2common/tests/unit/services/test_policy.py +++ b/st2common/tests/unit/services/test_policy.py @@ -15,9 +15,8 @@ from __future__ import absolute_import -import st2tests.config as tests_config - -tests_config.parse_args() +# This import must be early for import-time side-effects. +import st2tests import st2common @@ -29,7 +28,6 @@ from st2common.services import action as action_service from st2common.services import policies as policy_service -import st2tests from st2tests.fixtures.generic.fixture import PACK_NAME as PACK from st2tests import fixturesloader as fixtures diff --git a/st2common/tests/unit/test_executions_util.py b/st2common/tests/unit/test_executions_util.py index 00b89c7433..0776ef57cc 100644 --- a/st2common/tests/unit/test_executions_util.py +++ b/st2common/tests/unit/test_executions_util.py @@ -17,6 +17,9 @@ import mock import six +# This import must be early for import-time side-effects. +from st2tests.base import CleanDbTestCase + from st2common.constants import action as action_constants from st2common.models.api.action import RunnerTypeAPI, ActionAPI, LiveActionAPI from st2common.models.api.trigger import TriggerTypeAPI, TriggerAPI, TriggerInstanceAPI @@ -30,15 +33,12 @@ import st2common.util.action_db as action_utils import st2common.util.date as date_utils -from st2tests.base import CleanDbTestCase from st2tests.fixtures.generic.fixture import PACK_NAME as FIXTURES_PACK from st2tests.fixtures.descendants.fixture import PACK_NAME as DESCENDANTS_PACK from st2tests.fixturesloader import FixturesLoader -import st2tests.config as tests_config from six.moves import range -tests_config.parse_args() TEST_FIXTURES = { "liveactions": [ diff --git a/st2common/tests/unit/test_jsonify.py b/st2common/tests/unit/test_jsonify.py index b4a375be69..906a548d4c 100644 --- a/st2common/tests/unit/test_jsonify.py +++ b/st2common/tests/unit/test_jsonify.py @@ -22,14 +22,13 @@ import st2tests.config as tests_config -tests_config.parse_args() - import st2common.util.jsonify as jsonify class JsonifyTests(unittest.TestCase): @classmethod def setUpClass(cls): + tests_config.parse_args() jsonify.DEFAULT_JSON_LIBRARY = "orjson" @classmethod diff --git a/st2common/tests/unit/test_runners_utils.py b/st2common/tests/unit/test_runners_utils.py index 773fa9cc39..a7ed4c40d3 100644 --- a/st2common/tests/unit/test_runners_utils.py +++ b/st2common/tests/unit/test_runners_utils.py @@ -15,10 +15,8 @@ from __future__ import absolute_import -# pytest: make sure monkey_patching happens before importing mongoengine -from st2common.util.monkey_patch import monkey_patch - -monkey_patch() +# This import must be early for import-time side-effects. +from st2tests import base import mock @@ -30,11 +28,6 @@ from st2tests.fixtures.generic.fixture import PACK_NAME as FIXTURES_PACK -from st2tests import config as tests_config - -tests_config.parse_args() - - TEST_FIXTURES = { "liveactions": ["liveaction1.yaml"], "actions": ["local.yaml"], From f5015d44c8f73ead9ceca599b1e14cb76926c08a Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 29 Jul 2024 21:59:40 -0500 Subject: [PATCH 7/9] fmt --- st2common/tests/unit/test_runners_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/st2common/tests/unit/test_runners_utils.py b/st2common/tests/unit/test_runners_utils.py index a7ed4c40d3..b6f61bd61c 100644 --- a/st2common/tests/unit/test_runners_utils.py +++ b/st2common/tests/unit/test_runners_utils.py @@ -23,7 +23,6 @@ from st2common.runners import utils from st2common.services import executions as exe_svc from st2common.util import action_db as action_db_utils -from st2tests import base from st2tests import fixturesloader from st2tests.fixtures.generic.fixture import PACK_NAME as FIXTURES_PACK From 3fb7b48f0d329f6352aa2670b5ff76ce749a9187 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Fri, 24 May 2024 12:31:11 -0500 Subject: [PATCH 8/9] tests: move st2reactor test side-effects into setUpClass --- st2reactor/tests/unit/test_garbage_collector.py | 8 ++++++-- st2reactor/tests/unit/test_process_container.py | 7 +++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/st2reactor/tests/unit/test_garbage_collector.py b/st2reactor/tests/unit/test_garbage_collector.py index 93de6b25d0..f9ca515d0a 100644 --- a/st2reactor/tests/unit/test_garbage_collector.py +++ b/st2reactor/tests/unit/test_garbage_collector.py @@ -20,14 +20,18 @@ from oslo_config import cfg +# This import must be early for import-time side-effects. import st2tests.config as tests_config -tests_config.parse_args() - from st2reactor.garbage_collector import base as garbage_collector class GarbageCollectorServiceTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + def tearDown(self): # Reset gc_max_idle_sec with a value of 1 to reenable for other tests. cfg.CONF.set_override("gc_max_idle_sec", 1, group="workflow_engine") diff --git a/st2reactor/tests/unit/test_process_container.py b/st2reactor/tests/unit/test_process_container.py index b05175b805..747618a4f0 100644 --- a/st2reactor/tests/unit/test_process_container.py +++ b/st2reactor/tests/unit/test_process_container.py @@ -27,8 +27,6 @@ import st2tests.config as tests_config -tests_config.parse_args() - MOCK_PACK_DB = PackDB( ref="wolfpack", name="wolf pack", @@ -38,6 +36,11 @@ class ProcessContainerTests(unittest.TestCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + tests_config.parse_args() + def test_no_sensors_dont_quit(self): process_container = ProcessSensorContainer(None, poll_interval=0.1) process_container_thread = concurrency.spawn(process_container.run) From 3dc9974c4cc82785304b1f6e5d228b1f7c9fce60 Mon Sep 17 00:00:00 2001 From: Jacob Floyd Date: Mon, 16 Sep 2024 14:26:59 -0500 Subject: [PATCH 9/9] update changelog entry --- CHANGELOG.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b746bbedb3..ed30d55e08 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -33,7 +33,7 @@ Added * Continue introducing `pants `_ to improve DX (Developer Experience) working on StackStorm, improve our security posture, and improve CI reliability thanks in part to pants' use of PEX lockfiles. This is not a user-facing addition. - #6118 #6141 #6133 #6120 #6181 #6183 #6200 #6237 #6229 #6240 + #6118 #6141 #6133 #6120 #6181 #6183 #6200 #6237 #6229 #6240 #6241 Contributed by @cognifloyd * Build of ST2 EL9 packages #6153 Contributed by @amanda11