Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions contrib/runners/winrm_runner/tests/unit/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -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__))
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the . necessary here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively we might be able to use this:

Suggested change
from .fixtures import FIXTURES_PATH
from tests.unit.fixtures import FIXTURES_PATH

Relative import was simpler. But I'm fine either way.


POWERSHELL_SCRIPT_PATH = os.path.join(FIXTURES_PATH, "TestScript.ps1")

Expand Down
Empty file.
16 changes: 16 additions & 0 deletions st2reactor/tests/fixtures/fixture/__init__.py
Original file line number Diff line number Diff line change
@@ -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__), ".."))
16 changes: 16 additions & 0 deletions st2reactor/tests/fixtures/packs/__init__.py
Original file line number Diff line number Diff line change
@@ -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__))
Empty file.
16 changes: 16 additions & 0 deletions st2reactor/tests/fixtures/packs/pack_with_rules/fixture.py
Original file line number Diff line number Diff line change
@@ -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__)
Empty file.
16 changes: 16 additions & 0 deletions st2reactor/tests/fixtures/packs/pack_with_sensor/fixture.py
Original file line number Diff line number Diff line change
@@ -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__)
16 changes: 10 additions & 6 deletions st2reactor/tests/unit/test_sensor_and_rule_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.

from __future__ import absolute_import
import os

import mock

Expand All @@ -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())
Expand Down Expand Up @@ -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):
Expand Down
10 changes: 6 additions & 4 deletions st2reactor/tests/unit/test_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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,
Expand All @@ -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,
Expand Down