Skip to content

Commit d37c901

Browse files
authored
Merge pull request #5699: Use python imports to identify fixtures (part 1)
2 parents 2e74993 + a1c1daf commit d37c901

File tree

17 files changed

+144
-96
lines changed

17 files changed

+144
-96
lines changed

CHANGELOG.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ Changed
8686

8787
* Use PEP 440 direct reference requirements instead of legacy PIP VCS requirements. Now, our ``*.requirements.txt`` files use
8888
``package-name@ git+https://url@version ; markers`` instead of ``git+https://url@version#egg=package-name ; markers``. #5673
89-
9089
Contributed by @cognifloyd
9190

9291
* Move from udatetime to ciso8601 for date functionality ahead of supporting python3.9 #5692
9392
Contributed by Amanda McGuinness (@amanda11 intive)
9493

94+
* Refactor tests to use python imports to identify test fixtures. #5699
95+
Contributed by @cognifloyd
9596

9697
Removed
9798
~~~~~~~

contrib/packs/tests/test_action_unload.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717

18-
import os
19-
2018
from oslo_config import cfg
2119

2220
from st2common.util.monkey_patch import use_select_poll_workaround
@@ -36,16 +34,15 @@
3634

3735
from st2tests.base import BaseActionTestCase
3836
from st2tests.base import CleanDbTestCase
39-
from st2tests import fixturesloader
37+
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
38+
PACK_NAME as DUMMY_PACK_1,
39+
PACK_PATH as PACK_PATH_1,
40+
)
4041

4142
from pack_mgmt.unload import UnregisterPackAction
4243

4344
__all__ = ["UnloadActionTestCase"]
4445

45-
PACK_PATH_1 = os.path.join(
46-
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1"
47-
)
48-
4946

5047
class UnloadActionTestCase(BaseActionTestCase, CleanDbTestCase):
5148
action_cls = UnregisterPackAction
@@ -73,7 +70,7 @@ def setUp(self):
7370
register_content()
7471

7572
def test_run(self):
76-
pack = "dummy_pack_1"
73+
pack = DUMMY_PACK_1
7774
# Verify all the resources are there
7875

7976
pack_dbs = Pack.query(ref=pack)

st2actions/tests/unit/test_output_schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
from st2common.transport import publishers
3838
from st2tests.mocks import liveaction as mock_lv_ac_xport
3939

40+
from st2tests.fixtures.packs.dummy_pack_1.fixture import PACK_PATH as DUMMY_PACK_1_PATH
41+
4042

4143
PACKS = [
42-
st2tests.fixturesloader.get_fixtures_packs_base_path() + "/dummy_pack_1",
44+
DUMMY_PACK_1_PATH,
4345
st2tests.fixturesloader.get_fixtures_packs_base_path() + "/orquesta_tests",
4446
]
4547

st2api/tests/unit/controllers/v1/test_actions.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@
3434
from st2common.constants.pack import SYSTEM_PACK_NAME
3535
from st2common.persistence.pack import Pack
3636
from st2api.controllers.v1.actions import ActionsController
37-
from st2tests.fixturesloader import get_fixtures_packs_base_path
37+
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
38+
PACK_NAME as DUMMY_PACK_1,
39+
PACK_PATH as DUMMY_PACK_1_PATH,
40+
)
3841
from st2tests.base import CleanFilesTestCase
3942

4043
from st2tests.api import FunctionalTest
@@ -207,7 +210,7 @@
207210
"name": "st2.dummy.action1",
208211
"description": "test description",
209212
"enabled": True,
210-
"pack": "dummy_pack_1",
213+
"pack": DUMMY_PACK_1,
211214
"entry_point": "/tmp/test/action1.sh",
212215
"runner_type": "local-shell-script",
213216
"parameters": {
@@ -225,7 +228,7 @@
225228
"name": "st2.dummy.action2",
226229
"description": "test description",
227230
"enabled": True,
228-
"pack": "dummy_pack_1",
231+
"pack": DUMMY_PACK_1,
229232
"entry_point": "/tmp/test/action1.sh",
230233
"runner_type": "local-shell-script",
231234
"parameters": {
@@ -238,7 +241,7 @@
238241
"name": "st2.dummy.action14",
239242
"description": "test description",
240243
"enabled": True,
241-
"pack": "dummy_pack_1",
244+
"pack": DUMMY_PACK_1,
242245
"entry_point": "/tmp/test/action1.sh",
243246
"runner_type": "local-shell-script",
244247
"parameters": {
@@ -252,7 +255,7 @@
252255
"name": "st2.dummy.action15",
253256
"description": "test description",
254257
"enabled": True,
255-
"pack": "dummy_pack_1",
258+
"pack": DUMMY_PACK_1,
256259
"entry_point": "/tmp/test/action1.sh",
257260
"runner_type": "local-shell-script",
258261
"parameters": {
@@ -295,7 +298,7 @@
295298
"name": "st2.dummy.action_notify_test",
296299
"description": "test description",
297300
"enabled": True,
298-
"pack": "dummy_pack_1",
301+
"pack": DUMMY_PACK_1,
299302
"entry_point": "/tmp/test/action1.sh",
300303
"runner_type": "local-shell-script",
301304
"parameters": {
@@ -311,7 +314,7 @@
311314
"name": "st2.dummy.action_unicode_我爱狗",
312315
"description": "test description",
313316
"enabled": True,
314-
"pack": "dummy_pack_1",
317+
"pack": DUMMY_PACK_1,
315318
"entry_point": "/tmp/test/action1.sh",
316319
"runner_type": "local-shell-script",
317320
"parameters": {
@@ -333,9 +336,7 @@ class ActionsControllerTestCase(
333336

334337
register_packs = True
335338

336-
to_delete_files = [
337-
os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1/actions/filea.txt")
338-
]
339+
to_delete_files = [os.path.join(DUMMY_PACK_1_PATH, "actions/filea.txt")]
339340

340341
@mock.patch.object(
341342
action_validator, "validate_action", mock.MagicMock(return_value=True)

st2api/tests/unit/controllers/v1/test_packs.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
import os
17-
1816
import requests
1917
import mock
2018
import sys
@@ -32,7 +30,14 @@
3230
from st2tests.api import FunctionalTest
3331
from st2tests.api import APIControllerWithIncludeAndExcludeFilterTestCase
3432

35-
from st2tests.fixturesloader import get_fixtures_base_path
33+
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
34+
PACK_NAME as DUMMY_PACK_1,
35+
PACK_PATH as DUMMY_PACK_1_PATH,
36+
)
37+
from st2tests.fixtures.packs.dummy_pack_10.fixture import (
38+
PACK_NAME as DUMMY_PACK_10,
39+
PACK_PATH as DUMMY_PACK_10_PATH,
40+
)
3641

3742
__all__ = ["PacksControllerTestCase"]
3843

@@ -498,15 +503,10 @@ def test_packs_register_endpoint(self, mock_get_packs):
498503

499504
# Note: We only register a couple of packs and not all on disk to speed
500505
# things up. Registering all the packs takes a long time.
501-
fixtures_base_path = get_fixtures_base_path()
502-
packs_base_path = os.path.join(fixtures_base_path, "packs")
503-
pack_names = [
504-
"dummy_pack_1",
505-
"dummy_pack_10",
506-
]
507-
mock_return_value = {}
508-
for pack_name in pack_names:
509-
mock_return_value[pack_name] = os.path.join(packs_base_path, pack_name)
506+
mock_return_value = {
507+
DUMMY_PACK_1: DUMMY_PACK_1_PATH,
508+
DUMMY_PACK_10: DUMMY_PACK_10_PATH,
509+
}
510510

511511
mock_get_packs.return_value = mock_return_value
512512

@@ -529,7 +529,8 @@ def test_packs_register_endpoint(self, mock_get_packs):
529529

530530
# Register resources from a specific pack
531531
resp = self.app.post_json(
532-
"/v1/packs/register", {"packs": ["dummy_pack_1"], "fail_on_failure": False}
532+
"/v1/packs/register",
533+
{"packs": [DUMMY_PACK_1], "fail_on_failure": False},
533534
)
534535

535536
self.assertEqual(resp.status_int, 200)
@@ -538,13 +539,13 @@ def test_packs_register_endpoint(self, mock_get_packs):
538539
self.assertTrue(resp.json["configs"] >= 1)
539540

540541
# Verify metadata_file attribute is set
541-
action_dbs = Action.query(pack="dummy_pack_1")
542+
action_dbs = Action.query(pack=DUMMY_PACK_1)
542543
self.assertEqual(action_dbs[0].metadata_file, "actions/my_action.yaml")
543544

544545
# Register 'all' resource types should try include any possible content for the pack
545546
resp = self.app.post_json(
546547
"/v1/packs/register",
547-
{"packs": ["dummy_pack_1"], "fail_on_failure": False, "types": ["all"]},
548+
{"packs": [DUMMY_PACK_1], "fail_on_failure": False, "types": ["all"]},
548549
)
549550

550551
self.assertEqual(resp.status_int, 200)
@@ -566,7 +567,7 @@ def test_packs_register_endpoint(self, mock_get_packs):
566567
# * policies -> policy types
567568
resp = self.app.post_json(
568569
"/v1/packs/register",
569-
{"packs": ["dummy_pack_1"], "fail_on_failure": False, "types": ["actions"]},
570+
{"packs": [DUMMY_PACK_1], "fail_on_failure": False, "types": ["actions"]},
570571
)
571572

572573
self.assertEqual(resp.status_int, 200)
@@ -575,7 +576,7 @@ def test_packs_register_endpoint(self, mock_get_packs):
575576

576577
resp = self.app.post_json(
577578
"/v1/packs/register",
578-
{"packs": ["dummy_pack_1"], "fail_on_failure": False, "types": ["rules"]},
579+
{"packs": [DUMMY_PACK_1], "fail_on_failure": False, "types": ["rules"]},
579580
)
580581

581582
self.assertEqual(resp.status_int, 200)
@@ -611,7 +612,7 @@ def test_packs_register_endpoint(self, mock_get_packs):
611612

612613
# Register specific type for a single packs
613614
resp = self.app.post_json(
614-
"/v1/packs/register", {"packs": ["dummy_pack_1"], "types": ["action"]}
615+
"/v1/packs/register", {"packs": [DUMMY_PACK_1], "types": ["action"]}
615616
)
616617

617618
self.assertEqual(resp.status_int, 200)
@@ -620,7 +621,7 @@ def test_packs_register_endpoint(self, mock_get_packs):
620621

621622
# Verify that plural name form also works
622623
resp = self.app.post_json(
623-
"/v1/packs/register", {"packs": ["dummy_pack_1"], "types": ["actions"]}
624+
"/v1/packs/register", {"packs": [DUMMY_PACK_1], "types": ["actions"]}
624625
)
625626

626627
self.assertEqual(resp.status_int, 200)
@@ -632,7 +633,7 @@ def test_packs_register_endpoint(self, mock_get_packs):
632633
resp = self.app.post_json(
633634
"/v1/packs/register",
634635
{
635-
"packs": ["dummy_pack_1", "dummy_pack_1", "dummy_pack_1"],
636+
"packs": [DUMMY_PACK_1, DUMMY_PACK_1, DUMMY_PACK_1],
636637
"types": ["actions"],
637638
"fail_on_failure": False,
638639
},
@@ -653,13 +654,13 @@ def test_packs_register_endpoint(self, mock_get_packs):
653654
# Fail on failure is enabled by default
654655
resp = self.app.post_json("/v1/packs/register", expect_errors=True)
655656

656-
expected_msg = 'Failed to register pack "dummy_pack_10":'
657+
expected_msg = f'Failed to register pack "{DUMMY_PACK_10}":'
657658
self.assertEqual(resp.status_int, 400)
658659
self.assertIn(expected_msg, resp.json["faultstring"])
659660

660661
# Fail on failure (broken pack metadata)
661662
resp = self.app.post_json(
662-
"/v1/packs/register", {"packs": ["dummy_pack_1"]}, expect_errors=True
663+
"/v1/packs/register", {"packs": [DUMMY_PACK_1]}, expect_errors=True
663664
)
664665

665666
expected_msg = 'Referenced policy_type "action.mock_policy_error" doesnt exist'

st2common/tests/integration/test_register_content_script.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from st2common.util.shell import run_command
2424
from st2tests import config as test_config
2525
from st2tests.fixturesloader import get_fixtures_packs_base_path
26+
from st2tests.fixtures.packs.dummy_pack_1.fixture import PACK_PATH as DUMMY_PACK_1_PATH
2627

2728

2829
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -43,7 +44,7 @@ def setUp(self):
4344
test_config.parse_args()
4445

4546
def test_register_from_pack_success(self):
46-
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
47+
pack_dir = DUMMY_PACK_1_PATH
4748
runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners")
4849

4950
opts = [
@@ -142,7 +143,7 @@ def test_register_from_packs_doesnt_throw_on_missing_pack_resource_folder(self):
142143
def test_register_all_and_register_setup_virtualenvs(self):
143144
# Verify that --register-all works in combinations with --register-setup-virtualenvs
144145
# Single pack
145-
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
146+
pack_dir = DUMMY_PACK_1_PATH
146147
cmd = BASE_CMD_ARGS + [
147148
"--register-pack=%s" % (pack_dir),
148149
"--register-all",
@@ -157,7 +158,7 @@ def test_register_all_and_register_setup_virtualenvs(self):
157158

158159
def test_register_setup_virtualenvs(self):
159160
# Single pack
160-
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
161+
pack_dir = DUMMY_PACK_1_PATH
161162

162163
cmd = BASE_CMD_ARGS + [
163164
"--register-pack=%s" % (pack_dir),
@@ -173,7 +174,7 @@ def test_register_setup_virtualenvs(self):
173174
def test_register_recreate_virtualenvs(self):
174175
# 1. Register the pack and ensure it exists and doesn't rely on state from previous
175176
# test methods
176-
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
177+
pack_dir = DUMMY_PACK_1_PATH
177178

178179
cmd = BASE_CMD_ARGS + [
179180
"--register-pack=%s" % (pack_dir),
@@ -187,7 +188,7 @@ def test_register_recreate_virtualenvs(self):
187188
self.assertEqual(exit_code, 0)
188189

189190
# 2. Run it again with --register-recreate-virtualenvs flag
190-
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
191+
pack_dir = DUMMY_PACK_1_PATH
191192

192193
cmd = BASE_CMD_ARGS + [
193194
"--register-pack=%s" % (pack_dir),

st2common/tests/unit/test_aliasesregistrar.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
from st2common.persistence.action import ActionAlias
2121

2222
from st2tests import DbTestCase
23-
from st2tests import fixturesloader
23+
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
24+
PACK_PATH as ALIASES_FIXTURE_PACK_PATH,
25+
)
2426

2527
__all__ = ["TestAliasRegistrar"]
2628

27-
28-
ALIASES_FIXTURE_PACK_PATH = os.path.join(
29-
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1"
30-
)
3129
ALIASES_FIXTURE_PATH = os.path.join(ALIASES_FIXTURE_PACK_PATH, "aliases")
3230

3331

st2common/tests/unit/test_configs_registrar.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929
from st2tests.base import CleanDbTestCase
3030
from st2tests import fixturesloader
31+
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
32+
PACK_NAME as DUMMY_PACK_1,
33+
PACK_PATH as PACK_1_PATH,
34+
)
3135

3236

3337
__all__ = ["ConfigsRegistrarTestCase"]
3438

35-
PACK_1_PATH = os.path.join(
36-
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1"
37-
)
3839
PACK_6_PATH = os.path.join(
3940
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_6"
4041
)
@@ -60,7 +61,7 @@ def test_register_configs_for_all_packs(self):
6061

6162
registrar = ConfigsRegistrar(use_pack_cache=False)
6263
registrar._pack_loader.get_packs = mock.Mock()
63-
registrar._pack_loader.get_packs.return_value = {"dummy_pack_1": PACK_1_PATH}
64+
registrar._pack_loader.get_packs.return_value = {DUMMY_PACK_1: PACK_1_PATH}
6465
packs_base_paths = content_utils.get_packs_base_paths()
6566
registrar.register_from_packs(base_dirs=packs_base_paths)
6667

0 commit comments

Comments
 (0)