Skip to content

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

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#226
richm merged 1 commit intolinux-system-roles:mainfrom
richm:clear_facts

Conversation

@richm
Copy link
Copy Markdown
Contributor

@richm richm commented Mar 19, 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.

Signed-off-by: Rich Megginson rmeggins@redhat.com

Summary by Sourcery

Update postfix role tests to clear Ansible facts before each role invocation and verify the role gathers required facts itself.

Tests:

  • Introduce a reusable test task file that clears facts and then runs the postfix role, optionally exporting role variables and handling failures.
  • Update existing postfix tests to invoke the role via the new clear-facts task file instead of including the role directly, removing unnecessary gather_facts usage.
  • Adjust a relay_domains test condition to use a precomputed fact-based variable to avoid errors when facts are cleared.

…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 requested a review from spetrosi as a code owner March 19, 2026 22:34
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 19, 2026

Reviewer's Guide

Tests now invoke the postfix role via a shared helper task file that clears all Ansible facts before including the role, and a fact-dependent test condition has been refactored to avoid lazy evaluation issues when facts are cleared.

File-Level Changes

Change Details Files
Introduce helper task file to clear facts and then run the postfix role for tests.
  • Add tests/tasks/run_role_with_clear_facts.yml that first runs meta: clear_facts and then includes the linux-system-roles.postfix role.
  • Support optional inputs __sr_tasks_from, __sr_public, and __sr_failed_when to control which tasks run, variable export behavior, and failure handling semantics.
  • Implement a block/rescue pattern to emulate include_role failed_when=false behavior while still running the role normally when failures are allowed.
tests/tasks/run_role_with_clear_facts.yml
Update tests to use the new helper instead of including the postfix role directly.
  • Replace direct include_role calls with include_tasks: tasks/run_role_with_clear_facts.yml in tests_set_file.yml, tests_previous_replaced.yml, tests_disable_ipv6.yml, and tests_default.yml.
  • Pass __sr_public: true where tests previously used public: true on include_role invocations.
  • Remove explicit gather_facts: false in playbooks where facts are now controlled via the helper task’s clear_facts step.
tests/tests_set_file.yml
tests/tests_previous_replaced.yml
tests/tests_disable_ipv6.yml
tests/tests_default.yml
Avoid lazy-evaluated uses of ansible_facts in a test when condition that can run after facts are cleared.
  • Introduce a set_fact task that precomputes the relay_domains check condition into __sr_when_relay_domains_check based on ansible_version.
  • Change the subsequent block when expression to use __sr_when_relay_domains_check instead of directly referencing ansible_version so it remains defined even if facts are cleared between tasks.
tests/tests_set_file.yml

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

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 19, 2026

[citest]

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:

  • The run_role_with_clear_facts.yml control flow for __sr_failed_when is a bit hard to follow; you could simplify it by using a single include_role with failed_when: __sr_failed_when | default(true) on that task instead of a separate block/rescue plus a second include_role.
  • The rescue path in run_role_with_clear_facts.yml always emits a debug message, which may add noisy output for expected failures; consider switching this to a no-op or using a verbosity-gated debug to keep test logs cleaner.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `run_role_with_clear_facts.yml` control flow for `__sr_failed_when` is a bit hard to follow; you could simplify it by using a single `include_role` with `failed_when: __sr_failed_when | default(true)` on that task instead of a separate block/rescue plus a second `include_role`.
- The rescue path in `run_role_with_clear_facts.yml` always emits a debug message, which may add noisy output for expected failures; consider switching this to a no-op or using a `verbosity`-gated debug to keep test logs cleaner.

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 merged commit 6a75dee into linux-system-roles:main Mar 19, 2026
29 checks passed
@richm richm deleted the clear_facts branch March 19, 2026 22:49
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.

1 participant