Skip to content

Commit

Permalink
fix: fix insights_client <-> external_inventory interaction
Browse files Browse the repository at this point in the history
Apparently something in my local testing did not work, and the "request"
object for a session-scoped fixture is really not able to fetch
function-scoped fixtures.

Hence, change the approach a bit:
- stop saving the "request" object of "external_inventory", as it is of
  no use
- add an helper fixture for tests that use both "insights_client" and
  "external_inventory", which takes care of setting an internal field in
  the Inventory instance so it can work with the current instance of
  InsightsClient; make sure to unset it, so other tests do not get a
  leaked instance

This hopefully should make the helper methods of the Inventory class
that need to interact with insights-client usable again.

Fixes/reworks commit 51ef24c.

Signed-off-by: Pino Toscano <[email protected]>
  • Loading branch information
ptoscano committed Aug 12, 2024
1 parent af5bafa commit 02155ab
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
13 changes: 6 additions & 7 deletions pytest_client_tools/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Inventory:
This class represents an Inventory server.
"""

def __init__(self, base_url, verify=True, request=None):
def __init__(self, base_url, verify=True):
self._rest_client = RestClient(
base_url=base_url,
verify=verify,
Expand All @@ -20,7 +20,7 @@ def __init__(self, base_url, verify=True, request=None):
"/etc/pki/consumer/key.pem",
),
)
self._request = request
self._insights_client = None

@property
def base_url(self):
Expand All @@ -46,16 +46,15 @@ def this_system(self):
:return: The dict of the current system in Inventory
:rtype: dict
"""
if not self._request:
if not self._insights_client:
raise RuntimeError(
"Inventory.this_system(): cannot invoke without a pytest request set"
"Inventory.this_system(): cannot invoke without insights_client"
)
insights_client = self._request.getfixturevalue("insights_client")
path = f"hosts?insights_id={insights_client.uuid}"
path = f"hosts?insights_id={self._insights_client.uuid}"
res_json = self.get(path).json()
if res_json["total"] != 1:
raise RuntimeError(
f"Inventory.this_system(): {res_json['total']} hosts returned "
f"for the current UUID ({insights_client.uuid})"
f"for the current UUID ({self._insights_client.uuid})"
)
return res_json["results"][0]
17 changes: 16 additions & 1 deletion pytest_client_tools/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,21 @@ def external_inventory(request, test_config):
inventory = Inventory(
base_url=test_config.get("insights", "base_url") + "/inventory/v1",
verify=verify,
request=request,
)
yield inventory


@pytest.fixture
def _init_inventory_from_insights_client(request):
insights_client = request.getfixturevalue("insights_client")
assert insights_client
external_inventory = request.getfixturevalue("external_inventory")
assert external_inventory
external_inventory._insights_client = insights_client
yield
external_inventory._insights_client = None


def pytest_addoption(parser):
group = parser.getgroup("client-tools")
group.addoption(
Expand All @@ -278,6 +288,11 @@ def pytest_collection_modifyitems(config, items):
item.fixturenames.append("subman")
if "insights_client" not in item.fixturenames:
item.fixturenames.append("insights_client")
if (
"insights_client" in item.fixturenames
and "external_inventory" in item.fixturenames
):
item.fixturenames.append("_init_inventory_from_insights_client")
for jira_marker in item.iter_markers(name="jira"):
for jira in jira_marker.args:
jira_item = jira.lower()
Expand Down

0 comments on commit 02155ab

Please sign in to comment.