Skip to content

Conversation

@fstavela
Copy link
Contributor

@fstavela fstavela commented Dec 23, 2025

Overview

This PR is being created to address RHINENG-20830.

PR Checklist

  • Keep PR title short, ideally under 72 characters
  • Descriptive comments provided in complex code blocks
  • Include raw query examples in the PR description, if adding/modifying SQL query
  • Tests: validate optimal/expected output
  • Tests: validate exceptions and failure scenarios
  • Tests: edge cases
  • Recovers or fails gracefully during potential resource outages (e.g. DB, Kafka)
  • Uses type hinting, if convenient
  • Documentation, if this PR changes the way other services interact with host inventory
  • Links to related PRs

Secure Coding Practices Documentation Reference

You can find documentation on this checklist here.

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Refactor host inventory IQE tests to reuse uploaded hosts across multiple cases and tighten verification of host metadata and staleness timestamps.

Bug Fixes:

  • Ensure the last_check_in and staleness timestamps test compares against the host state captured before patching, preventing false negatives.

Enhancements:

  • Introduce shared, module-scoped fixtures to create and reuse uploaded hosts across patch, kessel, RBAC, and account-access tests for more efficient and consistent setup.
  • Simplify several tests by removing OS-specific upload parameters where not needed and relying on common host creation utilities.
  • Consolidate culling/org_id assertions into the main host creation test and remove redundant tests.

Tests:

  • Update patch, deletion, kessel groups/ungrouped, RBAC granular groups, and multi-account access tests to consume shared host fixtures instead of creating hosts inline.
  • Adjust image-mode and ingress/puptoo creation tests to use prepared hosts and validate org_id, reporter, and culling timestamps in a single flow.

@fstavela fstavela requested a review from a team as a code owner December 23, 2025 15:50
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 23, 2025

Reviewer's Guide

Refactors multiple IQE host-inventory REST tests to reuse pre-created hosts via shared fixtures, removes redundant OS parametrization and system-profile assertions where not needed, and tightens a few test assertions around org_id and staleness timestamps.

File-Level Changes

Change Details Files
Introduce shared host-preparation fixtures and reuse them across patch and kessel tests to reduce duplicate host creation logic.
  • Add module-scoped prepare_hosts fixtures that create and return multiple hosts via host_inventory.upload.create_hosts with cleanup_scope set to module
  • Update patch API tests to consume prepare_hosts instead of creating hosts inline
  • Update Kessel groups and ungrouped tests to consume prepare_hosts instead of creating their own hosts
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_patch.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/kessel/test_kessel_groups.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/kessel/test_kessel_ungrouped.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/rbac/groups/test_rbac_granular_groups.py
Standardize on HostOut typing and shared upload fixtures for account and upload tests, simplifying host setup and improving reusability.
  • Import HostOut from iqe_host_inventory_api where hosts are passed between tests or fixtures
  • Use shared hbi_upload_prepare_host_module fixture in account access and upload-related tests instead of per-test host creation
  • Adjust tests to assert on returned HostOut fields (e.g., display_name, org_id) using the shared host instances
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_upload_host_creation.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_account_access.py
Tighten staleness and org_id assertions around host creation and patching behavior while removing redundant or overlapping tests.
  • In the patch last_check_in test, capture the pre-patch host state and compare timestamps against that snapshot instead of the original object reference, while still asserting updated is changed
  • Fold org_id and staleness timestamp checks for puptoo-created hosts into the main create_new_host test and remove the dedicated culling-data test
  • Remove the account_upload_creates_org_id test now covered by other assertions
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_patch.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_upload_host_creation.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_account.py
Simplify host creation logic in selected tests by dropping unnecessary OS parametrization and archive/collect wiring.
  • Remove operating_system parametrization and archive/collect handling from tests where OS-specific behavior is no longer being asserted
  • Use host_inventory.upload.create_host() directly in the host deletion happy-path test
  • Drop per-OS system-profile assertions from tests that now rely on shared prepared hosts instead
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_patch.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_upload_host_creation.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_account_access.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_delete.py
iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_account.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 3 issues, and left some high level feedback:

  • The prepare_hosts helper in test_api_patch.py is decorated with @pytest.mark.fixture instead of @pytest.fixture, which will prevent pytest from registering it as a fixture; this should be corrected.
  • Several tests now share the same module-scoped hosts via prepare_hosts (e.g., in test_api_patch.py and kessel tests); double-check that each test either treats these hosts as immutable or fully accounts for prior mutations (like patched fields) to avoid hidden inter-test dependencies and flakiness.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `prepare_hosts` helper in `test_api_patch.py` is decorated with `@pytest.mark.fixture` instead of `@pytest.fixture`, which will prevent pytest from registering it as a fixture; this should be corrected.
- Several tests now share the same module-scoped hosts via `prepare_hosts` (e.g., in `test_api_patch.py` and kessel tests); double-check that each test either treats these hosts as immutable or fully accounts for prior mutations (like patched fields) to avoid hidden inter-test dependencies and flakiness.

## Individual Comments

### Comment 1
<location> `iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/test_api_patch.py:20-21` </location>
<code_context>
 logger = logging.getLogger(__name__)


[email protected](scope="module")
+def prepare_hosts(host_inventory: ApplicationHostInventory) -> list[HostOut]:
+    return host_inventory.upload.create_hosts(2, cleanup_scope="module")
+
</code_context>

<issue_to_address>
**issue (testing):** The fixture decorator is incorrect and will prevent the fixture from being registered.

Using `@pytest.mark.fixture(scope="module")` will not register `prepare_hosts` as a fixture, so any tests that use it will fail with `FixtureNotFound` during collection. Please change this decorator to `@pytest.fixture(scope="module")`.
</issue_to_address>

### Comment 2
<location> `iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/kessel/test_kessel_groups.py:28-29` </location>
<code_context>
 logger = logging.getLogger(__name__)


[email protected](scope="module")
+def prepare_hosts(host_inventory: ApplicationHostInventory) -> list[HostOut]:
+    return host_inventory.upload.create_hosts(3, cleanup_scope="module")
+
</code_context>

<issue_to_address>
**suggestion (testing):** Module-scoped `prepare_hosts` is shared across multiple group tests that mutate group membership, causing potential test ordering issues.

Because this fixture is module-scoped and shared by tests that mutate group membership for the same hosts, test behavior can depend on execution order, leading to flaky or order-dependent tests.

Please either make this fixture function-scoped so each test gets fresh hosts, or introduce a factory fixture (e.g. `prepare_hosts_factory()`) that creates hosts per test. This will keep the group-related tests isolated and predictable.

Suggested implementation:

```python
@pytest.fixture(scope="function")
def prepare_hosts(host_inventory: ApplicationHostInventory) -> list[HostOut]:
    return host_inventory.upload.create_hosts(3, cleanup_scope="function")

```

If there are any tests that rely on host state persisting across tests (within the module), they may need to be adjusted to either:
1. Use their own dedicated fixture with broader scope, or
2. Explicitly coordinate state using API calls instead of relying on shared `prepare_hosts` instances.
</issue_to_address>

### Comment 3
<location> `iqe-host-inventory-plugin/iqe_host_inventory/tests/rest/kessel/test_kessel_ungrouped.py:25-26` </location>
<code_context>
 logger = logging.getLogger(__name__)


[email protected](scope="module")
+def prepare_hosts(host_inventory: ApplicationHostInventory) -> list[HostOut]:
+    return host_inventory.upload.create_hosts(3, cleanup_scope="module")
+
</code_context>

<issue_to_address>
**suggestion (testing):** Shared module-scoped hosts in ungrouped tests can cause cross-test interference.

Here, `prepare_hosts` is module-scoped and reused by `test_kessel_filter_ungrouped_hosts` and `test_kessel_get_hosts_ordering_with_ungrouped`, both of which change grouping on the same hosts. This can make the second test observe hosts already grouped by the first.

Please switch this fixture to function scope or make it a factory that creates fresh hosts per test to avoid order-dependent/flaky behavior.

Suggested implementation:

```python
@pytest.fixture
def prepare_hosts(host_inventory: ApplicationHostInventory) -> list[HostOut]:
    return host_inventory.upload.create_hosts(3, cleanup_scope="function")

```

1. Ensure `pytest` is imported in this module (e.g. `import pytest`) if it is not already present at the top of the file.
2. No test changes should be needed, as the fixture name and signature remain the same; the behavior will now provide fresh hosts per test.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@fstavela fstavela force-pushed the fstavela/reuse-hosts-in-iqe branch from caded57 to ca90b85 Compare December 23, 2025 16:05
Copy link
Member

@rodrigonull rodrigonull left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants