Skip to content

test: ensure role gathers the facts it uses by having test clear_facts before include_role#593

Merged
richm merged 1 commit intolinux-system-roles:mainfrom
richm:clear_facts
Mar 19, 2026
Merged

test: ensure role gathers the facts it uses by having test clear_facts before include_role#593
richm merged 1 commit intolinux-system-roles:mainfrom
richm:clear_facts

Conversation

@richm
Copy link
Copy Markdown
Contributor

@richm richm commented Feb 28, 2026

The role gathers the facts it uses. For example, if the user uses
ANSIBLE_GATHERING=explicit, the role uses the setup module with the
facts and subsets it requires.

This change allows us to test this. Before every role invocation, the test
will use meta: clear_facts so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role. Note that this means we don't need to
use gather_facts for the tests.

Some vars defined using ansible_facts have been changed to be defined with
set_fact instead. This is because of the fact that vars are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like ansible_facts["distribution"] is undefined. This is
typically done for blocks that have a when condition that uses ansible_facts
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the when condition using set_fact. This
is because the when condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

Summary by Sourcery

Ensure storage role tests run with cleared Ansible facts so the role gathers required facts itself before execution.

Enhancements:

  • Make cryptsetup service masking conditional on service facts being present, gathering them only when needed.
  • Extend the role’s required fact subsets to explicitly include device facts.

Tests:

  • Run the storage role in all tests via a helper task file that clears facts before including the role, replacing direct include_role usage.
  • Adjust test playbooks to no longer rely on service_facts-based skip checks and to set OS/version flags and helper vars using set_fact where needed.
  • Update default test play and dependency test to invoke the role through the new helper, preserving public variable behavior.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Feb 28, 2026

Reviewer's Guide

Test playbooks are refactored to run the storage role via a new helper task file that clears ansible facts before each role invocation, while the role’s main task and supporting vars are updated to avoid depending on pre-gathered facts or explicit service_facts, and some fact-dependent logic is moved to set_fact for safe lazy evaluation.

Sequence diagram for tests running storage role with cleared facts

sequenceDiagram
  actor Tester
  participant TestPlaybook as tests_*.yml
  participant Helper as run_role_with_clear_facts.yml
  participant Ansible as Ansible_engine
  participant Role as storage_role
  participant Setup as setup_module
  participant ServiceFacts as service_facts_module

  Tester->>TestPlaybook: ansible-playbook tests_*.yml
  TestPlaybook->>Helper: include_tasks run_role_with_clear_facts.yml
  Helper->>Ansible: meta clear_facts
  Ansible-->>Helper: ansible_facts cleared
  Helper->>Role: include_role storage
  Role->>Setup: gather required fact subsets
  Setup-->>Role: minimal ansible_facts
  Role->>Role: compute storage_cryptsetup_services using ansible_facts.services if present
  alt services not in ansible_facts and cryptsetup_services not skipped
    Role->>ServiceFacts: gather service facts
    ServiceFacts-->>Role: ansible_facts.services populated
    Role->>Role: recompute storage_cryptsetup_services
  end
  Role->>Role: manage storage devices and mask cryptsetup services
  Role-->>Helper: role tasks completed
  Helper-->>TestPlaybook: role run finished
  TestPlaybook-->>Tester: test assertions and results
Loading

Flow diagram for storage role cryptsetup service management with conditional fact gathering

flowchart TD
  A[start role task main_blivet] --> B[Define storage_cryptsetup_services from ansible_facts.services if present else empty list]
  B --> C{services in ansible_facts?}
  C -->|yes| D[storage_cryptsetup_services based on existing ansible_facts.services]
  C -->|no| E[storage_cryptsetup_services set to empty list]
  D --> F{cryptsetup_services in storage_skip_checks?}
  E --> F
  F -->|yes| G[Skip service_facts and masking tasks]
  F -->|no| H[Run service_facts task]
  H --> I[ansible_facts.services populated]
  I --> J[Reevaluate storage_cryptsetup_services inside block using updated ansible_facts.services]
  J --> K[Mask systemd cryptsetup services]
  K --> L[Unmask systemd cryptsetup services]
  G --> M[end role task main_blivet]
  L --> M
Loading

File-Level Changes

Change Details Files
Introduce a helper task file to run the storage role after clearing facts and update all tests to use it instead of include_role.
  • Add tests/tasks/run_role_with_clear_facts.yml that clears facts via meta: clear_facts, then includes linux-system-roles.storage with support for __sr_tasks_from, __sr_public, and __sr_failed_when.
  • Refactor nearly all test playbooks to replace direct include_role calls of linux-system-roles.storage with include_tasks: tasks/run_role_with_clear_facts.yml, passing through role vars and, where needed, __sr_public.
  • Adjust tests that previously relied on gather_facts or existing ansible_facts to work correctly when facts are cleared before each role run.
tests/tasks/run_role_with_clear_facts.yml
tests/tests_resize.yml
tests/tests_lvm_pool_members.yml
tests/tests_create_raid_pool_then_remove.yml
tests/tests_stratis.yml
tests/tests_swap.yml
tests/tests_create_thinp_then_remove.yml
tests/tests_luks.yml
tests/tests_luks2.yml
tests/tests_create_multiple_partitions_gpt.yml
tests/tests_misc.yml
tests/tests_volume_relabel.yml
tests/tests_lvm_percent_size.yml
tests/tests_create_lvmvdo_then_remove.yml
tests/tests_change_fs.yml
tests/tests_luks_pool.yml
tests/tests_disk_errors.yml
tests/tests_lvm_one_disk_multiple_volumes.yml
tests/tests_lvm_pool_pv_grow.yml
tests/tests_raid_pool_options.yml
tests/tests_raid_volume_options.yml
tests/tests_change_disk_fs.yml
tests/tests_change_disk_mount.yml
tests/tests_change_fs_use_partitions.yml
tests/tests_change_mount.yml
tests/tests_create_lvm_cache_then_remove.yml
tests/tests_create_lvm_pool_then_remove.yml
tests/tests_create_partition_volume_then_remove.yml
tests/tests_remove_mount.yml
tests/tests_lvm_errors.yml
tests/tests_create_disk_then_remove.yml
tests/tests_create_multiple_partitions_dos.yml
tests/tests_create_raid_volume_then_remove.yml
tests/tests_existing_lvm_pool.yml
tests/tests_filesystem_one_disk.yml
tests/tests_lvm_auto_size_cap.yml
tests/tests_lvm_multiple_disks_multiple_volumes.yml
tests/tests_lvm_one_disk_one_volume.yml
tests/tests_missing_volume_type_in_pool.yml
tests/tests_raid_volume_cleanup.yml
tests/tests_create_lv_size_equal_to_vg.yml
tests/tests_lvm_pool_shared.yml
tests/tests_null_raid_pool.yml
tests/tests_remove_nonexistent_pool.yml
tests/tests_default.yml
tests/tests_deps.yml
tests/tests_fatals_cache_volume.yml
tests/tests_fatals_raid_pool.yml
tests/tests_fatals_raid_volume.yml
tests/tests_safe_mode_check.yml
tests/verify-role-failed.yml
Make the role robust when facts are cleared by tightening fact gathering requirements and avoiding unconditional service_facts usage.
  • Change __storage_required_facts_subsets to always request the 'devices' subset when gathering facts so device information is present even after explicit setup calls.
  • Refactor tasks/main-blivet.yml so service_facts is only run when the 'services' key is missing from ansible_facts and when cryptsetup service checks are not skipped, and compute storage_cryptsetup_services in a vars section that tolerates missing ansible_facts.services.
  • Remove test-side usage of the service_facts check in storage_skip_checks since the role now handles absence or presence of those facts internally.
vars/main.yml
tasks/main-blivet.yml
tests/tests_resize.yml
tests/tests_lvm_pool_members.yml
tests/tests_create_raid_pool_then_remove.yml
tests/tests_stratis.yml
tests/tests_swap.yml
tests/tests_create_thinp_then_remove.yml
tests/tests_luks.yml
tests/tests_luks2.yml
tests/tests_create_multiple_partitions_gpt.yml
tests/tests_misc.yml
tests/tests_volume_relabel.yml
tests/tests_lvm_percent_size.yml
tests/tests_create_lvmvdo_then_remove.yml
tests/tests_change_fs.yml
tests/tests_luks_pool.yml
tests/tests_disk_errors.yml
tests/tests_lvm_one_disk_multiple_volumes.yml
tests/tests_lvm_pool_pv_grow.yml
tests/tests_raid_pool_options.yml
tests/tests_raid_volume_options.yml
tests/tests_change_disk_fs.yml
tests/tests_change_disk_mount.yml
tests/tests_change_fs_use_partitions.yml
tests/tests_change_mount.yml
tests/tests_create_lvm_cache_then_remove.yml
tests/tests_create_lvm_pool_then_remove.yml
tests/tests_create_partition_volume_then_remove.yml
tests/tests_remove_mount.yml
tests/tests_lvm_errors.yml
tests/tests_create_disk_then_remove.yml
tests/tests_create_multiple_partitions_dos.yml
tests/tests_create_raid_volume_then_remove.yml
tests/tests_existing_lvm_pool.yml
tests/tests_filesystem_one_disk.yml
tests/tests_lvm_auto_size_cap.yml
tests/tests_lvm_multiple_disks_multiple_volumes.yml
tests/tests_lvm_one_disk_one_volume.yml
tests/tests_missing_volume_type_in_pool.yml
tests/tests_raid_volume_cleanup.yml
tests/tests_create_lv_size_equal_to_vg.yml
tests/tests_lvm_pool_shared.yml
tests/tests_null_raid_pool.yml
tests/tests_remove_nonexistent_pool.yml
tests/tests_default.yml
tests/tests_deps.yml
tests/tests_fatals_cache_volume.yml
tests/tests_fatals_raid_pool.yml
tests/tests_fatals_raid_volume.yml
tests/tests_safe_mode_check.yml
Adjust fact-dependent test logic to avoid lazy evaluation issues when ansible_facts may be undefined after clear_facts.
  • Move computation of OS-distribution helper flags (is_rhelX, is_fedora, etc.) from inline vars on blocks into explicit set_fact tasks executed after package_facts or setup, so they are evaluated with fresh facts and not lazily on later task runs.
  • Introduce is_fedora via set_fact in the VDO test and reuse it in conditions to avoid direct ansible_facts access in when clauses that may execute after facts are cleared.
  • Inline or delay evaluation of some expressions, such as fs_after and unused_disk_subfact, to the point where required ansible_facts are guaranteed to exist, or remove unused fact-based variables that could error when facts are absent.
  • Standardize on consistent Jinja quoting style for ansible_facts access where updated.
tests/tests_lvm_pool_members.yml
tests/tests_create_lvmvdo_then_remove.yml
tests/tests_create_thinp_then_remove.yml
tests/tests_resize.yml
tests/tests_lvm_errors.yml
tests/tests_create_lv_size_equal_to_vg.yml
tests/tests_swap.yml

Possibly linked issues

  • #???: PR adds devices to required fact subsets and tests with cleared facts, addressing the initial check-mode NameError on devices.

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
Copy Markdown

@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 left some high level feedback:

  • In merge_ansible_facts, you update saved in place; if callers reuse saved_facts elsewhere this can be surprising—consider copying (e.g. saved = copy.deepcopy(saved) before update) and/or documenting that the input dict will be mutated.
  • The new service_facts condition in tasks/main-blivet.yml (when: not "services" in ansible_facts) changes semantics from the previous storage_skip_checks-based gate; if the intent is still to allow explicit skipping, you may want to preserve the skip list check and layer the services presence check on top of it.
  • In tests/tasks/run_role_with_clear_facts.yml, if the included role fails after meta: clear_facts, the final merge step will be skipped and the play will continue with cleared facts; wrapping the role include and merge in a block with always for the merge step would make fact restoration more robust on failures.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `merge_ansible_facts`, you update `saved` in place; if callers reuse `saved_facts` elsewhere this can be surprising—consider copying (e.g. `saved = copy.deepcopy(saved)` before `update`) and/or documenting that the input dict will be mutated.
- The new `service_facts` condition in `tasks/main-blivet.yml` (`when: not "services" in ansible_facts`) changes semantics from the previous `storage_skip_checks`-based gate; if the intent is still to allow explicit skipping, you may want to preserve the skip list check and layer the `services` presence check on top of it.
- In `tests/tasks/run_role_with_clear_facts.yml`, if the included role fails after `meta: clear_facts`, the final merge step will be skipped and the play will continue with cleared facts; wrapping the role include and merge in a `block` with `always` for the merge step would make fact restoration more robust on failures.

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.

@richm richm marked this pull request as draft February 28, 2026 00:06
@codecov
Copy link
Copy Markdown

codecov bot commented Feb 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 10.33%. Comparing base (59fd1c6) to head (90be41a).
⚠️ Report is 124 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (59fd1c6) and HEAD (90be41a). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (59fd1c6) HEAD (90be41a)
sanity 1 0
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #593      +/-   ##
==========================================
- Coverage   16.54%   10.33%   -6.22%     
==========================================
  Files           2        8       +6     
  Lines         284     2023    +1739     
  Branches       79        0      -79     
==========================================
+ Hits           47      209     +162     
- Misses        237     1814    +1577     
Flag Coverage Δ
sanity ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@richm richm force-pushed the clear_facts branch 2 times, most recently from 16e7bf3 to 457b0ca Compare February 28, 2026 00:15
@richm
Copy link
Copy Markdown
Contributor Author

richm commented Feb 28, 2026

[citest]

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 2, 2026

@spetrosi new changes

  • make storage_cryptsetup_services a vars instead of a set_fact - it isn't used outside of the block, and move the service_facts inside the block
  • add support for a cryptsetup_services for storage_skip_checks - currently unused
  • add devices to the list of subsets of facts gathered by the role - this was causing undefined variable errors in some tests that used ansible_facts['devices'] - this adds about 0.5 sec to the fact gathering by the role
  • revert tests that changed vars to set_fact where they didn't need to use set_fact - in some cases I found out that the variable wasn't used at all and just removed it - in one case the variable was only used in one place so I refactored the code to not use a variable

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 2, 2026

[citest]

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 2, 2026

[citest_bad]

@richm richm force-pushed the clear_facts branch 3 times, most recently from 24688cb to 2d5c007 Compare March 10, 2026 12:29
@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 10, 2026

[citest]

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 10, 2026

[citest]

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 11, 2026

[citest]

@richm richm force-pushed the clear_facts branch 5 times, most recently from f22b519 to dc359b7 Compare March 16, 2026 22:43
…s before include_role

The role gathers the facts it uses.  For example, if the user uses
`ANSIBLE_GATHERING=explicit`, the role uses the `setup` module with the
facts and subsets it requires.

This change allows us to test this.  Before every role invocation, the test
will use `meta: clear_facts` so that the role starts with no facts.

Create a task file tests/tasks/run_role_with_clear_facts.yml to do the tasks
to clear the facts and run the role.  Note that this means we don't need to
use `gather_facts` for the tests.

Some vars defined using `ansible_facts` have been changed to be defined with
`set_fact` instead.  This is because of the fact that `vars` are lazily
evaluated - the var might be referenced when the facts have been cleared, and
will issue an error like `ansible_facts["distribution"] is undefined`.  This is
typically done for blocks that have a `when` condition that uses `ansible_facts`
and the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the `when` condition using `set_fact`.  This
is because the `when` condition is evaluated every time a task is invoked in the
block, and if the facts are cleared, this will raise an undefined variable error.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
@richm richm self-assigned this Mar 18, 2026
@richm richm changed the title test: ensure role gathers the facts it uses by having test clear_facts before include_role test: add tasks and action plugin to run role with clear facts [citest_skip] Mar 18, 2026
@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 18, 2026

[citest]

@richm richm changed the title test: add tasks and action plugin to run role with clear facts [citest_skip] test: ensure role gathers the facts it uses by having test clear_facts before include_role Mar 19, 2026
@richm richm marked this pull request as ready for review March 19, 2026 13:17
@richm richm merged commit 3766e9e into linux-system-roles:main Mar 19, 2026
39 of 42 checks passed
@richm richm deleted the clear_facts branch March 19, 2026 13:17
Copy link
Copy Markdown

@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 1 issue, and left some high level feedback:

  • The new tests/tasks/run_role_with_clear_facts.yml header and comments claim to save and restore facts and merge ansible_facts, but the implementation only clears facts and runs the role; either implement the described save/restore behavior or update the comments to match what the task file actually does.
  • In tasks/main-blivet.yml the skip mechanism has effectively changed from using service_facts to cryptsetup_services; if this rename is intentional, consider aligning the string used in storage_skip_checks (and existing callers) to avoid silently changing skip behavior.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `tests/tasks/run_role_with_clear_facts.yml` header and comments claim to save and restore facts and merge `ansible_facts`, but the implementation only clears facts and runs the role; either implement the described save/restore behavior or update the comments to match what the task file actually does.
- In `tasks/main-blivet.yml` the skip mechanism has effectively changed from using `service_facts` to `cryptsetup_services`; if this rename is intentional, consider aligning the string used in `storage_skip_checks` (and existing callers) to avoid silently changing skip behavior.

## Individual Comments

### Comment 1
<location path="tasks/main-blivet.yml" line_range="62-64" />
<code_context>
+  vars:
+    # rejectattr required because the fix to service_facts is on Ansible > 2.12 only
+    # https://github.com/ansible/ansible/pull/75326
     storage_cryptsetup_services: "{{
       ansible_facts.services.values() |
       selectattr('name', 'defined') |
</code_context>
<issue_to_address>
**issue (bug_risk):** Using a block-level `vars` for `storage_cryptsetup_services` prevents it from seeing services gathered by `service_facts` in the same block

Because block-level `vars` are evaluated before any tasks in the block run, `storage_cryptsetup_services` is computed once, when `ansible_facts` still lacks `services`. It becomes `[]` via the `if 'services' in ansible_facts else []` guard and is never recomputed after `service_facts` populates `ansible_facts.services`, so the later `systemd` masking step sees an empty list and is a no-op on the first run.

To have `storage_cryptsetup_services` use the newly gathered service facts, either move this expression into a `set_fact` that runs after `service_facts`, or define it as task-level `vars` on the `systemd` (or other consuming) tasks so it evaluates after `service_facts` has executed. Otherwise, cryptsetup services will be skipped when service facts weren’t pre-existing.
</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.

Comment on lines 62 to 64
storage_cryptsetup_services: "{{
ansible_facts.services.values() |
selectattr('name', 'defined') |
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Using a block-level vars for storage_cryptsetup_services prevents it from seeing services gathered by service_facts in the same block

Because block-level vars are evaluated before any tasks in the block run, storage_cryptsetup_services is computed once, when ansible_facts still lacks services. It becomes [] via the if 'services' in ansible_facts else [] guard and is never recomputed after service_facts populates ansible_facts.services, so the later systemd masking step sees an empty list and is a no-op on the first run.

To have storage_cryptsetup_services use the newly gathered service facts, either move this expression into a set_fact that runs after service_facts, or define it as task-level vars on the systemd (or other consuming) tasks so it evaluates after service_facts has executed. Otherwise, cryptsetup services will be skipped when service facts weren’t pre-existing.

Copy link
Copy Markdown
Contributor

@spetrosi spetrosi 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.

3 participants