8000 Use python imports to identify fixtures (part 1/6) by cognifloyd · Pull Request #5699 · StackStorm/st2 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Use python imports to identify fixtures (part 1/6) #5699

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 10, 2022
Merged

Conversation

cognifloyd
Copy link
Member
@cognifloyd cognifloyd commented Aug 3, 2022

Background

I'm working towards introducing pants. Eventually I would like to use pants to run tests to take advantage of the fine-grained per-file caching of results that accounts for dependencies by python files (pants infers the deps by reading the python files).

In order to use the fine-grained caching, Pants needs to know which tests rely on which fixtures. We could add extra metadata to tie the tests to the fixtures, but that would be an additional maintenance burden that will become out-of-date. We can also include all fixtures for all tests, but then we don't benefit from the fine-grained per-file caching. However, pants can already infer dependencies based on python imports, so that is what this PR (and several follow-up PRs) takes advantage of.

Overview

This PR does the following:

  • adjust utils in st2tests/st2tests/fixturesloader.py so that we can use those utilities to identify every fixture.
  • turn every fixture into an importable python module with an __init__.py.
  • add a fixture.py file in each fixture that uses the fixturesloader utils (where helpful) to identify itself with PATH and NAME vars.
  • in every test that uses a given fixture, import PATH and/or NAME vars from that fixture.

This PR focuses on only 2 fixture packs: dummy_pack_1 and dummy_pack_10.
Follow-up PRs will address other fixture packs and other sets of fixtures. I will submit those PRs after this one is merged.
Follow-up PRS: #5702 #5703 #5704 #5705 #5706

I'm leaving a comment on one of the fixturesloader.py utils, as we could simplify the interface by using inspect. I'd like feedback on preferences about how much magic we're okay with here.

Commits

  • Ensure that pack fixture paths are absolute
  • Import PATH/NAME for each fixture pack
  • Minimize pack fixture boilerplate code
  • Rename fixtureloader func to be more generic

st2common.content.utils.get_relative_path_to_pack_file assumes
that a relative path is already relative to the pack dir.
So, make sure we are passing an absolute path to tests.
@cognifloyd cognifloyd added this to the 3.8.0 milestone Aug 3, 2022
@cognifloyd cognifloyd self-assigned this Aug 3, 2022
@cognifloyd cognifloyd requested a review from a team August 3, 2022 23:46
@pull-request-size pull-request-size bot added the size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review. label Aug 3, 2022
Copy link
Member Author
@cognifloyd cognifloyd left a comment

Choose a reason for hiding this comment

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

There is a lot of boilerplate in this PR - moving fixture path calculation from tests into the fixtures themselves - but it should be fairly easy to review as that boilerplate is very repetitive.

Comment on lines +172 to +175
def get_fixture_name_and_path(fixture_file):
pack_name = os.path.basename(os.path.dirname(fixture_file))
pack_path = os.path.join(get_fixtures_packs_base_path(), pack_name)
return pack_name, pack_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.

This is the primary fixture loader util - start by reviewing this and the other utils in this file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Most fixtures will do something like this to use this util:

from st2tests import fixturesloader

PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)

But the interface could be simplified to avoid passing __file__ if we change the get_fixture_name_and_path() util by using inspect like this:

import inspect
from types import FrameType, ModuleType

def get_fixture_name_and_path():
    currentframe = inspect.currentframe()
    assert isinstance(currentframe, FrameType)
    caller_frame = currentframe.f_back
    caller_module = inspect.getmodule(caller_frame)
    assert isinstance(caller_module, ModuleType)
    pack_name = caller_module.__package__.split(".")[-1]
    pack_path = os.path.abspath(os.path.dirname(caller_module.__file__))
    return pack_name, pack_path

Thoughts? Do we want to pass __file__ as an arg or discover it with inspect?

Copy link
Member
10000

Choose a reason for hiding this comment

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

I think passing file makes it more easier to understand what this piece of code is doing. inspect method here seems rather complicated.

Comment on lines +1 to +16
from st2tests import fixturesloader

PACK_NAME, PACK_PATH = fixturesloader.get_fixture_name_and_path(__file__)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is an example of the boilerplate added to each fixture pack.

@@ -23,6 +23,9 @@
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.

@@ -43,7 +46,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

@@ -30,11 +30,10 @@
from st2common.services import policies as policy_service

import st2tests
from st2tests.fixtures.generic.fixture import PACK_NAME as PACK
Copy link
Member Author

Choose a reason for hiding this comment

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

Many tests already used a constant to define the PACK name, so all I had to do was import the constant from the fixture instead of hard-coding it in the test files.

@pull-request-size pull-request-size bot added size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. and removed size/XL PR that changes 500-999 lines. Consider splitting work into several ones that easier to review. labels Aug 4, 2022
@cognifloyd cognifloyd marked this pull request as draft August 4, 2022 01:03
In order for pants to detect which changes need to be re-run after changing
fixtures, pants needs to be able to infer the relevant dependencies.

We could use a super-level target to depend on everything, but then we lose
the fine-grained invalidation. The only way to tell pants that a test file
is using a particular set of fixtures is via python imports.

So, this adds some boilerplate python in every pack dir:
__init__.py
fixture.py

We cannot use pack.yaml as a source root marker any more or importing these
files fails. We'll have to implement something else for the main packs under
contrib/.
@pull-request-size pull-request-size bot added size/L PR that changes 100-499 lines. Requires some effort to review. and removed size/XXL PR that changes 1000+ lines. You should absolutely split your PR into several. labels Aug 4, 2022
@cognifloyd cognifloyd marked this pull request as ready for review August 4, 2022 01:58
Comment on lines +172 to +175
def get_fixture_name_and_path(fixture_file):
pack_name = os.path.basename(os.path.dirname(fixture_file))
pack_path = os.path.join(get_fixtures_packs_base_path(), pack_name)
return pack_name, pack_path
Copy link
Member

Choose a reason for hiding this comment

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

I think passing file makes it more easier to understand what this piece of code is doing. inspect method here seems rather complicated.

@cognifloyd cognifloyd merged commit d37c901 into master Aug 10, 2022
@cognifloyd cognifloyd deleted the fixtures-loading branch August 10, 2022 04:57
@cognifloyd cognifloyd changed the title Use python imports to identify fixtures (part 1) Use python imports to identify fixtures (part 1/6) Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement maintenance pantsbuild size/L PR that changes 100-499 lines. Requires some effort to review. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0