diff --git a/CHANGELOG.rst b/CHANGELOG.rst index d668e4bb9f..fae190eca7 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 #5705 +* Refactor tests to use python imports to identify test fixtures. #5699 #5702 #5703 #5704 #5705 #5706 Contributed by @cognifloyd Removed diff --git a/contrib/runners/winrm_runner/tests/unit/fixtures/__init__.py b/contrib/runners/winrm_runner/tests/unit/fixtures/__init__.py new file mode 100644 index 0000000000..38f37e7c05 --- /dev/null +++ b/contrib/runners/winrm_runner/tests/unit/fixtures/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2021 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_PATH = os.path.abspath(os.path.dirname(__file__)) diff --git a/contrib/runners/winrm_runner/tests/unit/test_winrm_ps_script_runner.py b/contrib/runners/winrm_runner/tests/unit/test_winrm_ps_script_runner.py index c1414c25e7..b44ff30c14 100644 --- a/contrib/runners/winrm_runner/tests/unit/test_winrm_ps_script_runner.py +++ b/contrib/runners/winrm_runner/tests/unit/test_winrm_ps_script_runner.py @@ -22,7 +22,7 @@ from winrm_runner import winrm_ps_script_runner from winrm_runner.winrm_base import WinRmBaseRunner -FIXTURES_PATH = os.path.join(os.path.dirname(__file__), "fixtures") +from .fixtures import FIXTURES_PATH POWERSHELL_SCRIPT_PATH = os.path.join(FIXTURES_PATH, "TestScript.ps1") diff --git a/st2reactor/tests/fixtures/__init__.py b/st2reactor/tests/fixtures/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2reactor/tests/fixtures/fixture/__init__.py b/st2reactor/tests/fixtures/fixture/__init__.py new file mode 100644 index 0000000000..b3efe379c5 --- /dev/null +++ b/st2reactor/tests/fixtures/fixture/__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.abspath(os.path.join(os.path.dirname(__file__), "..")) diff --git a/st2reactor/tests/fixtures/packs/__init__.py b/st2reactor/tests/fixtures/packs/__init__.py new file mode 100644 index 0000000000..0b7f0ff9c5 --- /dev/null +++ b/st2reactor/tests/fixtures/packs/__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 + +PACKS_DIR = os.path.abspath(os.path.dirname(__file__)) diff --git a/st2reactor/tests/fixtures/packs/pack_with_rules/__init__.py b/st2reactor/tests/fixtures/packs/pack_with_rules/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2reactor/tests/fixtures/packs/pack_with_rules/fixture.py b/st2reactor/tests/fixtures/packs/pack_with_rules/fixture.py new file mode 100644 index 0000000000..50c698989e --- /dev/null +++ b/st2reactor/tests/fixtures/packs/pack_with_rules/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 + +PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__) diff --git a/st2reactor/tests/fixtures/packs/pack_with_sensor/__init__.py b/st2reactor/tests/fixtures/packs/pack_with_sensor/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/st2reactor/tests/fixtures/packs/pack_with_sensor/fixture.py b/st2reactor/tests/fixtures/packs/pack_with_sensor/fixture.py new file mode 100644 index 0000000000..50c698989e --- /dev/null +++ b/st2reactor/tests/fixtures/packs/pack_with_sensor/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 + +PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__) diff --git a/st2reactor/tests/unit/test_sensor_and_rule_registration.py b/st2reactor/tests/unit/test_sensor_and_rule_registration.py index 50075690e9..fbf189edda 100644 --- a/st2reactor/tests/unit/test_sensor_and_rule_registration.py +++ b/st2reactor/tests/unit/test_sensor_and_rule_registration.py @@ -14,7 +14,6 @@ # limitations under the License. from __future__ import absolute_import -import os import mock @@ -27,17 +26,22 @@ from st2common.bootstrap.sensorsregistrar import SensorsRegistrar from st2common.bootstrap.rulesregistrar import RulesRegistrar -__all__ = ["SensorRegistrationTestCase", "RuleRegistrationTestCase"] +from tests.fixtures.packs import PACKS_DIR +from tests.fixtures.packs.pack_with_rules.fixture import ( + PACK_PATH as PACK_WITH_RULES_PATH, +) +from tests.fixtures.packs.pack_with_sensor.fixture import ( + PACK_PATH as PACK_WITH_SENSOR_PATH, +) -CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) -PACKS_DIR = os.path.abspath(os.path.join(CURRENT_DIR, "../fixtures/packs")) +__all__ = ["SensorRegistrationTestCase", "RuleRegistrationTestCase"] # NOTE: We need to perform this patching because test fixtures are located outside of the packs # base paths directory. This will never happen outside the context of test fixtures. @mock.patch( "st2common.content.utils.get_pack_base_path", - mock.Mock(return_value=os.path.join(PACKS_DIR, "pack_with_sensor")), + mock.Mock(return_value=PACK_WITH_SENSOR_PATH), ) class SensorRegistrationTestCase(DbTestCase): @mock.patch.object(PoolPublisher, "publish", mock.MagicMock()) @@ -142,7 +146,7 @@ def mock_load(*args, **kwargs): # base paths directory. This will never happen outside the context of test fixtures. @mock.patch( "st2common.content.utils.get_pack_base_path", - mock.Mock(return_value=os.path.join(PACKS_DIR, "pack_with_rules")), + mock.Mock(return_value=PACK_WITH_RULES_PATH), ) class RuleRegistrationTestCase(DbTestCase): def test_register_rules(self): diff --git a/st2reactor/tests/unit/test_tester.py b/st2reactor/tests/unit/test_tester.py index 526dc26744..066409cd42 100644 --- a/st2reactor/tests/unit/test_tester.py +++ b/st2reactor/tests/unit/test_tester.py @@ -24,6 +24,8 @@ from st2tests.fixtures.generic.fixture import PACK_NAME as FIXTURES_PACK from st2tests.fixturesloader import FixturesLoader +from tests.fixtures.fixture import FIXTURES_DIR + BASE_PATH = os.path.dirname(os.path.abspath(__file__)) TEST_MODELS_TRIGGERS = { @@ -43,9 +45,9 @@ def test_matching_trigger_from_file(self): FixturesLoader().save_fixtures_to_db( fixtures_pack=FIXTURES_PACK, fixtures_dict=TEST_MODELS_ACTIONS ) - rule_file_path = os.path.join(BASE_PATH, "../fixtures/rule.yaml") + rule_file_path = os.path.join(FIXTURES_DIR, "rule.yaml") trigger_instance_file_path = os.path.join( - BASE_PATH, "../fixtures/trigger_instance_1.yaml" + FIXTURES_DIR, "trigger_instance_1.yaml" ) tester = RuleTester( rule_file_path=rule_file_path, @@ -55,9 +57,9 @@ def test_matching_trigger_from_file(self): self.assertTrue(matching) def test_non_matching_trigger_from_file(self): - rule_file_path = os.path.join(BASE_PATH, "../fixtures/rule.yaml") + rule_file_path = os.path.join(FIXTURES_DIR, "rule.yaml") trigger_instance_file_path = os.path.join( - BASE_PATH, "../fixtures/trigger_instance_2.yaml" + FIXTURES_DIR, "trigger_instance_2.yaml" ) tester = RuleTester( rule_file_path=rule_file_path,