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
3 changes: 2 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ Changed

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

Contributed by @cognifloyd

* 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
Contributed by @cognifloyd

Removed
~~~~~~~
Expand Down
13 changes: 5 additions & 8 deletions contrib/packs/tests/test_action_unload.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

from oslo_config import cfg

from st2common.util.monkey_patch import use_select_poll_workaround
Expand All @@ -36,16 +34,15 @@

from st2tests.base import BaseActionTestCase
from st2tests.base import CleanDbTestCase
from st2tests import fixturesloader
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
PACK_NAME as DUMMY_PACK_1,
PACK_PATH as PACK_PATH_1,
)

from pack_mgmt.unload import UnregisterPackAction

__all__ = ["UnloadActionTestCase"]

PACK_PATH_1 = os.path.join(
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1"
)


class UnloadActionTestCase(BaseActionTestCase, CleanDbTestCase):
action_cls = UnregisterPackAction
Expand Down Expand Up @@ -73,7 +70,7 @@ def setUp(self):
register_content()

def test_run(self):
pack = "dummy_pack_1"
pack = DUMMY_PACK_1
# Verify all the resources are there

pack_dbs = Pack.query(ref=pack)
Expand Down
4 changes: 3 additions & 1 deletion st2actions/tests/unit/test_output_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@
from st2common.transport import publishers
from st2tests.mocks import liveaction as mock_lv_ac_xport

from st2tests.fixtures.packs.dummy_pack_1.fixture import PACK_PATH as DUMMY_PACK_1_PATH


PACKS = [
st2tests.fixturesloader.get_fixtures_packs_base_path() + "/dummy_pack_1",
DUMMY_PACK_1_PATH,
st2tests.fixturesloader.get_fixtures_packs_base_path() + "/orquesta_tests",
]

Expand Down
21 changes: 11 additions & 10 deletions st2api/tests/unit/controllers/v1/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
from st2common.constants.pack import SYSTEM_PACK_NAME
from st2common.persistence.pack import Pack
from st2api.controllers.v1.actions import ActionsController
from st2tests.fixturesloader import get_fixtures_packs_base_path
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
PACK_NAME as DUMMY_PACK_1,
PACK_PATH as DUMMY_PACK_1_PATH,
)
from st2tests.base import CleanFilesTestCase

from st2tests.api import FunctionalTest
Expand Down Expand Up @@ -207,7 +210,7 @@
"name": "st2.dummy.action1",
"description": "test description",
"enabled": True,
"pack": "dummy_pack_1",
"pack": DUMMY_PACK_1,
"entry_point": "/tmp/test/action1.sh",
"runner_type": "local-shell-script",
"parameters": {
Expand All @@ -225,7 +228,7 @@
"name": "st2.dummy.action2",
"description": "test description",
"enabled": True,
"pack": "dummy_pack_1",
"pack": DUMMY_PACK_1,
"entry_point": "/tmp/test/action1.sh",
"runner_type": "local-shell-script",
"parameters": {
Expand All @@ -238,7 +241,7 @@
"name": "st2.dummy.action14",
"description": "test description",
"enabled": True,
"pack": "dummy_pack_1",
"pack": DUMMY_PACK_1,
"entry_point": "/tmp/test/action1.sh",
"runner_type": "local-shell-script",
"parameters": {
Expand All @@ -252,7 +255,7 @@
"name": "st2.dummy.action15",
"description": "test description",
"enabled": True,
"pack": "dummy_pack_1",
"pack": DUMMY_PACK_1,
"entry_point": "/tmp/test/action1.sh",
"runner_type": "local-shell-script",
"parameters": {
Expand Down Expand Up @@ -295,7 +298,7 @@
"name": "st2.dummy.action_notify_test",
"description": "test description",
"enabled": True,
"pack": "dummy_pack_1",
"pack": DUMMY_PACK_1,
"entry_point": "/tmp/test/action1.sh",
"runner_type": "local-shell-script",
"parameters": {
Expand All @@ -311,7 +314,7 @@
"name": "st2.dummy.action_unicode_我爱狗",
"description": "test description",
"enabled": True,
"pack": "dummy_pack_1",
"pack": DUMMY_PACK_1,
"entry_point": "/tmp/test/action1.sh",
"runner_type": "local-shell-script",
"parameters": {
Expand All @@ -333,9 +336,7 @@ class ActionsControllerTestCase(

register_packs = True

to_delete_files = [
os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1/actions/filea.txt")
]
to_delete_files = [os.path.join(DUMMY_PACK_1_PATH, "actions/filea.txt")]

@mock.patch.object(
action_validator, "validate_action", mock.MagicMock(return_value=True)
Expand Down
45 changes: 23 additions & 22 deletions st2api/tests/unit/controllers/v1/test_packs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import requests
import mock
import sys
Expand All @@ -32,7 +30,14 @@
from st2tests.api import FunctionalTest
from st2tests.api import APIControllerWithIncludeAndExcludeFilterTestCase

from st2tests.fixturesloader import get_fixtures_base_path
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
PACK_NAME as DUMMY_PACK_1,
PACK_PATH as DUMMY_PACK_1_PATH,
)
from st2tests.fixtures.packs.dummy_pack_10.fixture import (
PACK_NAME as DUMMY_PACK_10,
PACK_PATH as DUMMY_PACK_10_PATH,
)

__all__ = ["PacksControllerTestCase"]

Expand Down Expand Up @@ -498,15 +503,10 @@ def test_packs_register_endpoint(self, mock_get_packs):

# Note: We only register a couple of packs and not all on disk to speed
# things up. Registering all the packs takes a long time.
fixtures_base_path = get_fixtures_base_path()
packs_base_path = os.path.join(fixtures_base_path, "packs")
pack_names = [
"dummy_pack_1",
"dummy_pack_10",
]
mock_return_value = {}
for pack_name in pack_names:
mock_return_value[pack_name] = os.path.join(packs_base_path, pack_name)
mock_return_value = {
DUMMY_PACK_1: DUMMY_PACK_1_PATH,
DUMMY_PACK_10: DUMMY_PACK_10_PATH,
}

mock_get_packs.return_value = mock_return_value

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

# Register resources from a specific pack
resp = self.app.post_json(
"/v1/packs/register", {"packs": ["dummy_pack_1"], "fail_on_failure": False}
"/v1/packs/register",
{"packs": [DUMMY_PACK_1], "fail_on_failure": False},
)

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

# Verify metadata_file attribute is set
action_dbs = Action.query(pack="dummy_pack_1")
action_dbs = Action.query(pack=DUMMY_PACK_1)
self.assertEqual(action_dbs[0].metadata_file, "actions/my_action.yaml")

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

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

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

resp = self.app.post_json(
"/v1/packs/register",
{"packs": ["dummy_pack_1"], "fail_on_failure": False, "types": ["rules"]},
{"packs": [DUMMY_PACK_1], "fail_on_failure": False, "types": ["rules"]},
)

self.assertEqual(resp.status_int, 200)
Expand Down Expand Up @@ -611,7 +612,7 @@ def test_packs_register_endpoint(self, mock_get_packs):

# Register specific type for a single packs
resp = self.app.post_json(
"/v1/packs/register", {"packs": ["dummy_pack_1"], "types": ["action"]}
"/v1/packs/register", {"packs": [DUMMY_PACK_1], "types": ["action"]}
)

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

# Verify that plural name form also works
resp = self.app.post_json(
"/v1/packs/register", {"packs": ["dummy_pack_1"], "types": ["actions"]}
"/v1/packs/register", {"packs": [DUMMY_PACK_1], "types": ["actions"]}
)

self.assertEqual(resp.status_int, 200)
Expand All @@ -632,7 +633,7 @@ def test_packs_register_endpoint(self, mock_get_packs):
resp = self.app.post_json(
"/v1/packs/register",
{
"packs": ["dummy_pack_1", "dummy_pack_1", "dummy_pack_1"],
"packs": [DUMMY_PACK_1, DUMMY_PACK_1, DUMMY_PACK_1],
"types": ["actions"],
"fail_on_failure": False,
},
Expand All @@ -653,13 +654,13 @@ def test_packs_register_endpoint(self, mock_get_packs):
# Fail on failure is enabled by default
resp = self.app.post_json("/v1/packs/register", expect_errors=True)

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

# Fail on failure (broken pack metadata)
resp = self.app.post_json(
"/v1/packs/register", {"packs": ["dummy_pack_1"]}, expect_errors=True
"/v1/packs/register", {"packs": [DUMMY_PACK_1]}, expect_errors=True
)

expected_msg = 'Referenced policy_type "action.mock_policy_error" doesnt exist'
Expand Down
11 changes: 6 additions & 5 deletions st2common/tests/integration/test_register_content_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from st2common.util.shell import run_command
from st2tests import config as test_config
from st2tests.fixturesloader import get_fixtures_packs_base_path
from st2tests.fixtures.packs.dummy_pack_1.fixture import PACK_PATH as DUMMY_PACK_1_PATH
Copy link
Member Author

Choose a reason for hiding this comment

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

Here is an import that would show pants that this test file uses the dummy_pack_1 fixture.



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

def test_register_from_pack_success(self):
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
Copy link
Member Author

@cognifloyd cognifloyd Aug 4, 2022

Choose a reason for hiding this comment

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

In most tests, we actually calculate the fixture path every time it's used. By adding the boilerplate inside each fixture, that calculation only needs to be done in one place (DRY). Instead, we just need to use the imported DUMMY_PACK_1_PATH constant here.

This line gets simplified to be:

        pack_dir = DUMMY_PACK_1_PATH

pack_dir = DUMMY_PACK_1_PATH
runner_dirs = os.path.join(get_fixtures_packs_base_path(), "runners")

opts = [
Expand Down Expand Up @@ -142,7 +143,7 @@ def test_register_from_packs_doesnt_throw_on_missing_pack_resource_folder(self):
def test_register_all_and_register_setup_virtualenvs(self):
# Verify that --register-all works in combinations with --register-setup-virtualenvs
# Single pack
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
pack_dir = DUMMY_PACK_1_PATH
cmd = BASE_CMD_ARGS + [
"--register-pack=%s" % (pack_dir),
"--register-all",
Expand All @@ -157,7 +158,7 @@ def test_register_all_and_register_setup_virtualenvs(self):

def test_register_setup_virtualenvs(self):
# Single pack
pack_dir = os.path.join(get_fixtures_packs_base_path(), "dummy_pack_1")
pack_dir = DUMMY_PACK_1_PATH

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

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

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

cmd = BASE_CMD_ARGS + [
"--register-pack=%s" % (pack_dir),
Expand Down
8 changes: 3 additions & 5 deletions st2common/tests/unit/test_aliasesregistrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
from st2common.persistence.action import ActionAlias

from st2tests import DbTestCase
from st2tests import fixturesloader
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
PACK_PATH as ALIASES_FIXTURE_PACK_PATH,
)

__all__ = ["TestAliasRegistrar"]


ALIASES_FIXTURE_PACK_PATH = os.path.join(
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1"
)
ALIASES_FIXTURE_PATH = os.path.join(ALIASES_FIXTURE_PACK_PATH, "aliases")


Expand Down
9 changes: 5 additions & 4 deletions st2common/tests/unit/test_configs_registrar.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@

from st2tests.base import CleanDbTestCase
from st2tests import fixturesloader
from st2tests.fixtures.packs.dummy_pack_1.fixture import (
PACK_NAME as DUMMY_PACK_1,
PACK_PATH as PACK_1_PATH,
)


__all__ = ["ConfigsRegistrarTestCase"]

PACK_1_PATH = os.path.join(
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_1"
)
PACK_6_PATH = os.path.join(
fixturesloader.get_fixtures_packs_base_path(), "dummy_pack_6"
)
Expand All @@ -60,7 +61,7 @@ def test_register_configs_for_all_packs(self):

registrar = ConfigsRegistrar(use_pack_cache=False)
registrar._pack_loader.get_packs = mock.Mock()
registrar._pack_loader.get_packs.return_value = {"dummy_pack_1": PACK_1_PATH}
registrar._pack_loader.get_packs.return_value = {DUMMY_PACK_1: PACK_1_PATH}
packs_base_paths = content_utils.get_packs_base_paths()
registrar.register_from_packs(base_dirs=packs_base_paths)

Expand Down
Loading