-
-
Notifications
You must be signed in to change notification settings - Fork 355
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
Replicate most of the ansible style host matching logic #753
Open
Code0x58
wants to merge
5
commits into
pytest-dev:main
Choose a base branch
from
Code0x58:rewrite-logic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
779b33b
Replicate more of the ansible style host matching logic
Code0x58 62f9a0b
Possibly allow ansible use in python3.8 - 1
Code0x58 6e5e98e
Possibly allow ansible use in python3.8 - 2
Code0x58 6f78505
Merge branch 'main' into rewrite-logic
Code0x58 c502b00
Rename test_ansible.py to test_ansible_inventory.py
Code0x58 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import pytest | ||
|
||
from testinfra.backend import parse_hostspec | ||
from testinfra.utils.ansible_runner import expand_pattern, get_hosts, Inventory | ||
|
||
|
||
@pytest.fixture | ||
def inventory() -> Inventory: | ||
"""Hosts are always under a group, the default is "ungrouped" if using the | ||
ini file format. The "all" meta-group always contains all hosts when | ||
expanded.""" | ||
return { | ||
"_meta": { | ||
"hostvars": { | ||
"a": None, | ||
"b": None, | ||
"c": None, | ||
} | ||
}, | ||
"all": { | ||
"children": ["nested"], | ||
}, | ||
"left": { | ||
"hosts": ["a", "b"], | ||
}, | ||
"right": { | ||
"hosts": ["b", "c"], | ||
}, | ||
"combined": { | ||
"children": ["left", "right"], | ||
}, | ||
"nested": { | ||
"children": ["combined"], | ||
} | ||
} | ||
|
||
|
||
def test_expand_pattern_simple(inventory: Inventory): | ||
"""Simple names are matched, recurring into groups if needed.""" | ||
# direct hostname | ||
assert expand_pattern("a", inventory) == {"a"} | ||
# group | ||
assert expand_pattern("left", inventory) == {"a", "b"} | ||
# meta-group | ||
assert expand_pattern("combined", inventory) == {"a", "b", "c"} | ||
# meta-meta-group | ||
assert expand_pattern("nested", inventory) == {"a", "b", "c"} | ||
|
||
|
||
def test_expand_pattern_fnmatch(inventory: Inventory): | ||
"""Simple names are matched, recurring into groups if needed.""" | ||
# l->left | ||
assert expand_pattern("l*", inventory) == {"a", "b"} | ||
# any single letter name | ||
assert expand_pattern("?", inventory) == {"a", "b", "c"} | ||
|
||
|
||
def test_expand_pattern_regex(inventory: Inventory): | ||
"""Simple names are matched, recurring into groups if needed.""" | ||
# simple character matching - "l" matches "left" but not "all" | ||
assert expand_pattern("~l", inventory) == {"a", "b"} | ||
# "b" matches an exact host, not any group | ||
assert expand_pattern("~b", inventory) == {"b"} | ||
# "a" will match all | ||
assert expand_pattern("~a", inventory) == {"a", "b", "c"} | ||
|
||
|
||
def test_get_hosts(inventory: Inventory): | ||
"""Multiple names/patterns can be combined.""" | ||
assert get_hosts("a", inventory) == ["a"] | ||
# the two pattern separators are handled | ||
assert get_hosts("a:b", inventory) == ["a", "b"] | ||
assert get_hosts("a,b", inventory) == ["a", "b"] | ||
# difference works | ||
assert get_hosts("left:!right", inventory) == ["a"] | ||
# intersection works | ||
assert get_hosts("left:&right", inventory) == ["b"] | ||
# intersection is taken with the intersection of the intersection groups | ||
assert get_hosts("all:&left:&right", inventory) == ["b"] | ||
# when the intersections ends up empty, so does the result | ||
assert get_hosts("all:&a:&c", inventory) == [] | ||
# negation is taken with the union of negation groups | ||
assert get_hosts("all:!a:!c", inventory) == ["b"] | ||
|
||
|
||
@pytest.mark.parametrize("left", ["h1", "!h1", "&h1", "~h1", "*h1"]) | ||
@pytest.mark.parametrize("sep", [":", ","]) | ||
@pytest.mark.parametrize("right", ["h2", "!h2", "&h2", "~h2", "*h2", ""]) | ||
def test_parse_hostspec(left: str, sep: str, right: str): | ||
"""Ansible's host patterns are parsed without issue.""" | ||
if right: | ||
pattern = f"{left}{sep}{right}" | ||
else: | ||
pattern = left | ||
assert parse_hostspec(pattern) == (pattern, {}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment about something that I don't see as worth changing right now as there hasn't been any response on this yet, and a significant improvement is much better than nothing.
I suspect a perfect copy of the logic from ansible would use something like this here:
Then you imitate behaviour like
!right
which is equivalent toall:!right
, but this would also be best with additions to manual testing and adding of tests to this codebase for the confirmed functionality.Given that adding
all:
to the start of a host pattern/query is equivalent, it's a fine enough workaround. If this change gets merged/released, I'll follow up with this change.