diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ea86d6dfe0..d668e4bb9f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -91,7 +91,7 @@ Changed * Move from udatetime to ciso8601 for date functionality ahead of supporting python3.9 #5692 Contributed by Amanda McGuinness (@amanda11 intive) -* Refactor tests to use python imports to identify test fixtures. #5699 #5702 #5703 #5704 +* Refactor tests to use python imports to identify test fixtures. #5699 #5702 #5703 #5704 #5705 Contributed by @cognifloyd Removed diff --git a/contrib/packs/tests/__init__.py b/contrib/packs/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/contrib/packs/tests/fixtures/__init__.py b/contrib/packs/tests/fixtures/__init__.py new file mode 100644 index 0000000000..1effc747e8 --- /dev/null +++ b/contrib/packs/tests/fixtures/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2022 The StackStorm Authors. +# +# Licensed 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. +import os + +FIXTURES_DIR = os.path.dirname(os.path.abspath(__file__)) diff --git a/contrib/packs/tests/test_action_download.py b/contrib/packs/tests/test_action_download.py index c29e95fccc..a2ceeea152 100644 --- a/contrib/packs/tests/test_action_download.py +++ b/contrib/packs/tests/test_action_download.py @@ -37,7 +37,8 @@ from pack_mgmt.download import DownloadGitRepoAction -BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +# this import allows pants to detect that the fixtures are used in this file +from .fixtures import FIXTURES_DIR PACK_INDEX = { "test": { @@ -639,7 +640,7 @@ def test_run_pack_dowload_local_git_repo_detached_head_state(self): side_effect=TypeError("detached head") ) - pack_path = os.path.join(BASE_DIR, "fixtures/stackstorm-test") + pack_path = os.path.join(FIXTURES_DIR, "stackstorm-test") result = action.run( packs=["file://%s" % (pack_path)], abs_repo_base=self.repo_base @@ -665,7 +666,7 @@ def test_run_pack_download_local_directory(self): ) # 2. Local pack which is not a git repository - pack_path = os.path.join(BASE_DIR, "fixtures/stackstorm-test4") + pack_path = os.path.join(FIXTURES_DIR, "stackstorm-test4") result = action.run( packs=["file://%s" % (pack_path)], abs_repo_base=self.repo_base diff --git a/st2common/tests/fixtures/__init__.py b/st2common/tests/fixtures/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2common/tests/fixtures/local_runner/__init__.py b/st2common/tests/fixtures/local_runner/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2common/tests/fixtures/local_runner/fixture.py b/st2common/tests/fixtures/local_runner/fixture.py new file mode 100644 index 0000000000..eed9bd17a4 --- /dev/null +++ b/st2common/tests/fixtures/local_runner/fixture.py @@ -0,0 +1,16 @@ +# Copyright 2022 The StackStorm Authors. +# +# Licensed 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. +import os + +FIXTURE_DIR = os.path.abspath(os.path.dirname(__file__)) diff --git a/st2common/tests/integration/log_unicode_data.py b/st2common/tests/integration/log_unicode_data.py index a4a30f664f..9d8c616610 100644 --- a/st2common/tests/integration/log_unicode_data.py +++ b/st2common/tests/integration/log_unicode_data.py @@ -41,6 +41,7 @@ from st2common import log as logging from st2common.service_setup import setup as common_setup +# Do not use helpers from st2tests to calculate this (avoid extra imports). FIXTURES_DIR = os.path.join(ST2TESTS_PATH, "st2tests/fixtures") ST2_CONFIG_DEBUG_LL_PATH = os.path.join( FIXTURES_DIR, "conf/st2.tests.api.debug_log_level.conf" diff --git a/st2common/tests/integration/test_rabbitmq_ssl_listener.py b/st2common/tests/integration/test_rabbitmq_ssl_listener.py index b6c947e5fa..e7b2ce1247 100644 --- a/st2common/tests/integration/test_rabbitmq_ssl_listener.py +++ b/st2common/tests/integration/test_rabbitmq_ssl_listener.py @@ -25,11 +25,10 @@ from st2common.transport import utils as transport_utils -from st2tests.fixturesloader import get_fixtures_base_path +from st2tests.fixtures.ssl_certs.fixture import FIXTURE_PATH as CERTS_FIXTURES_PATH __all__ = ["RabbitMQTLSListenerTestCase"] -CERTS_FIXTURES_PATH = os.path.join(get_fixtures_base_path(), "ssl_certs/") ST2_CI = os.environ.get("ST2_CI", "false").lower() == "true" NON_SSL_LISTENER_PORT = 5672 diff --git a/st2common/tests/integration/test_register_content_script.py b/st2common/tests/integration/test_register_content_script.py index 212b0b7b99..d416ae9af1 100644 --- a/st2common/tests/integration/test_register_content_script.py +++ b/st2common/tests/integration/test_register_content_script.py @@ -27,6 +27,7 @@ # import this so that pants can infer dependencies for the glob below from st2tests.fixtures.packs.dummy_pack_1.fixture import PACK_PATH as DUMMY_PACK_1_PATH from st2tests.fixtures.packs.dummy_pack_4.fixture import PACK_PATH as DUMMY_PACK_4_PATH +from st2tests.fixtures.packs.runners.fixture import FIXTURE_PATH as RUNNER_DIRS from st2tests.fixtures.packs_1.dummy_pack_4.fixture import PACK_PATH as EMPTY_PACK_PATH @@ -49,7 +50,7 @@ def setUp(self): def test_register_from_pack_success(self): pack_dir = DUMMY_PACK_1_PATH - runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") + runner_dirs = RUNNER_DIRS opts = [ "--register-pack=%s" % (pack_dir), @@ -63,7 +64,7 @@ def test_register_from_pack_success(self): def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self): # No fail on failure flag, should succeed pack_dir = "doesntexistblah" - runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") + runner_dirs = RUNNER_DIRS opts = [ "--register-pack=%s" % (pack_dir), @@ -88,7 +89,7 @@ def test_register_from_pack_fail_on_failure_pack_dir_doesnt_exist(self): def test_register_from_pack_action_metadata_fails_validation(self): # No fail on failure flag, should succeed pack_dir = DUMMY_PACK_4_PATH - runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners") + runner_dirs = RUNNER_DIRS opts = [ "--register-pack=%s" % (pack_dir), diff --git a/st2common/tests/integration/test_service_setup_log_level_filtering.py b/st2common/tests/integration/test_service_setup_log_level_filtering.py index 4fcd526402..36c891896c 100644 --- a/st2common/tests/integration/test_service_setup_log_level_filtering.py +++ b/st2common/tests/integration/test_service_setup_log_level_filtering.py @@ -22,36 +22,34 @@ from eventlet.green import subprocess from st2tests.base import IntegrationTestCase -from st2tests.fixturesloader import get_fixtures_base_path +from st2tests.fixtures.conf.fixture import FIXTURE_PATH as CONF_FIXTURES_PATH __all__ = ["ServiceSetupLogLevelFilteringTestCase"] BASE_DIR = os.path.dirname(os.path.abspath(__file__)) -FIXTURES_DIR = get_fixtures_base_path() - ST2_CONFIG_INFO_LL_PATH = os.path.join( - FIXTURES_DIR, "conf/st2.tests.api.info_log_level.conf" + CONF_FIXTURES_PATH, "st2.tests.api.info_log_level.conf" ) ST2_CONFIG_INFO_LL_PATH = os.path.abspath(ST2_CONFIG_INFO_LL_PATH) ST2_CONFIG_DEBUG_LL_PATH = os.path.join( - FIXTURES_DIR, "conf/st2.tests.api.debug_log_level.conf" + CONF_FIXTURES_PATH, "st2.tests.api.debug_log_level.conf" ) ST2_CONFIG_DEBUG_LL_PATH = os.path.abspath(ST2_CONFIG_DEBUG_LL_PATH) ST2_CONFIG_AUDIT_LL_PATH = os.path.join( - FIXTURES_DIR, "conf/st2.tests.api.audit_log_level.conf" + CONF_FIXTURES_PATH, "st2.tests.api.audit_log_level.conf" ) ST2_CONFIG_AUDIT_LL_PATH = os.path.abspath(ST2_CONFIG_AUDIT_LL_PATH) ST2_CONFIG_SYSTEM_DEBUG_PATH = os.path.join( - FIXTURES_DIR, "conf/st2.tests.api.system_debug_true.conf" + CONF_FIXTURES_PATH, "st2.tests.api.system_debug_true.conf" ) ST2_CONFIG_SYSTEM_DEBUG_PATH = os.path.abspath(ST2_CONFIG_SYSTEM_DEBUG_PATH) ST2_CONFIG_SYSTEM_LL_DEBUG_PATH = os.path.join( - FIXTURES_DIR, "conf/st2.tests.api.system_debug_true_logging_debug.conf" + CONF_FIXTURES_PATH, "st2.tests.api.system_debug_true_logging_debug.conf" ) PYTHON_BINARY = sys.executable diff --git a/st2common/tests/unit/test_crypto_utils.py b/st2common/tests/unit/test_crypto_utils.py index 5f8f07fa69..7fbbe9bf8f 100644 --- a/st2common/tests/unit/test_crypto_utils.py +++ b/st2common/tests/unit/test_crypto_utils.py @@ -38,12 +38,10 @@ from st2common.util.crypto import cryptography_symmetric_encrypt from st2common.util.crypto import cryptography_symmetric_decrypt -from st2tests.fixturesloader import get_fixtures_base_path +from st2tests.fixtures.keyczar_keys.fixture import FIXTURE_PATH as KEY_FIXTURES_PATH __all__ = ["CryptoUtilsTestCase", "CryptoUtilsKeyczarCompatibilityTestCase"] -KEY_FIXTURES_PATH = os.path.join(get_fixtures_base_path(), "keyczar_keys/") - class CryptoUtilsTestCase(TestCase): @classmethod diff --git a/st2common/tests/unit/test_shell_action_system_model.py b/st2common/tests/unit/test_shell_action_system_model.py index 76609ab953..28cd78a00b 100644 --- a/st2common/tests/unit/test_shell_action_system_model.py +++ b/st2common/tests/unit/test_shell_action_system_model.py @@ -31,8 +31,8 @@ from local_runner.local_shell_script_runner import LocalShellScriptRunner -CURRENT_DIR = os.path.dirname(os.path.realpath(__file__)) -FIXTURES_DIR = os.path.abspath(os.path.join(CURRENT_DIR, "../fixtures")) +from tests.fixtures.local_runner.fixture import FIXTURE_DIR as LOCAL_RUNNER_FIXTURE_DIR + LOGGED_USER_USERNAME = pwd.getpwuid(os.getuid())[0] __all__ = ["ShellCommandActionTestCase", "ShellScriptActionTestCase"] @@ -110,7 +110,7 @@ def setUp(self): } def _get_fixture(self, name): - path = os.path.join(FIXTURES_DIR, "local_runner", name) + path = os.path.join(LOCAL_RUNNER_FIXTURE_DIR, name) with open(path, "r") as fp: content = fp.read().strip() diff --git a/st2common/tests/unit/test_spec_loader.py b/st2common/tests/unit/test_spec_loader.py index a9b91d96d2..284a38e838 100644 --- a/st2common/tests/unit/test_spec_loader.py +++ b/st2common/tests/unit/test_spec_loader.py @@ -19,6 +19,9 @@ from st2common.util import spec_loader +# indicate to pants that this test uses this fixture. +from st2tests.fixtures.specs import __package__ as specs_fixture_package + class SpecLoaderTest(unittest2.TestCase): def test_spec_loader(self): @@ -31,6 +34,6 @@ def test_bad_spec_duplicate_keys(self): yaml.constructor.ConstructorError, 'found duplicate key "swagger"', spec_loader.load_spec, - "st2tests.fixtures.specs", + specs_fixture_package, "openapi.yaml.j2", ) diff --git a/st2tests/st2tests/fixtures/conf/__init__.py b/st2tests/st2tests/fixtures/conf/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2tests/st2tests/fixtures/conf/fixture.py b/st2tests/st2tests/fixtures/conf/fixture.py new file mode 100644 index 0000000000..38db492a73 --- /dev/null +++ b/st2tests/st2tests/fixtures/conf/fixture.py @@ -0,0 +1,18 @@ +# Copyright 2022 The StackStorm Authors. +# +# Licensed 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. +import os + +# Warning: Do not import fixturesloader to avoid breaking +# st2common/tests/integration/log_unicode_data.py +FIXTURE_PATH = os.path.dirname(__file__) diff --git a/st2tests/st2tests/fixtures/keyczar_keys/__init__.py b/st2tests/st2tests/fixtures/keyczar_keys/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2tests/st2tests/fixtures/keyczar_keys/fixture.py b/st2tests/st2tests/fixtures/keyczar_keys/fixture.py new file mode 100644 index 0000000000..3463c93569 --- /dev/null +++ b/st2tests/st2tests/fixtures/keyczar_keys/fixture.py @@ -0,0 +1,16 @@ +# Copyright 2022 The StackStorm Authors. +# +# Licensed 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 st2tests import fixturesloader + +FIXTURE_NAME, FIXTURE_PATH = fixturesloader.get_fixture_name_and_path(__file__) diff --git a/st2tests/st2tests/fixtures/packs/runners/fixture.py b/st2tests/st2tests/fixtures/packs/runners/fixture.py new file mode 100644 index 0000000000..3463c93569 --- /dev/null +++ b/st2tests/st2tests/fixtures/packs/runners/fixture.py @@ -0,0 +1,16 @@ +# Copyright 2022 The StackStorm Authors. +# +# Licensed 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 st2tests import fixturesloader + +FIXTURE_NAME, FIXTURE_PATH = fixturesloader.get_fixture_name_and_path(__file__) diff --git a/st2tests/st2tests/fixtures/packs/runners/test_async_runner/__init__.py b/st2tests/st2tests/fixtures/packs/runners/test_async_runner/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2tests/st2tests/fixtures/packs/runners/test_polling_async_runner/__init__.py b/st2tests/st2tests/fixtures/packs/runners/test_polling_async_runner/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2tests/st2tests/fixtures/ssl_certs/__init__.py b/st2tests/st2tests/fixtures/ssl_certs/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2tests/st2tests/fixtures/ssl_certs/fixture.py b/st2tests/st2tests/fixtures/ssl_certs/fixture.py new file mode 100644 index 0000000000..3463c93569 --- /dev/null +++ b/st2tests/st2tests/fixtures/ssl_certs/fixture.py @@ -0,0 +1,16 @@ +# Copyright 2022 The StackStorm Authors. +# +# Licensed 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 st2tests import fixturesloader + +FIXTURE_NAME, FIXTURE_PATH = fixturesloader.get_fixture_name_and_path(__file__)