Skip to content

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

Closed
richm wants to merge 1 commit intolinux-system-roles:mainfrom
richm:clear_facts
Closed

test: ensure role gathers the facts it uses by having test clear_facts before include_role#5
richm wants to merge 1 commit intolinux-system-roles:mainfrom
richm:clear_facts

Conversation

@richm
Copy link
Copy Markdown
Contributor

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

Tests:

  • Update default and include-vars tests to run the trustee_attestation_server role via a helper that clears facts before role execution, avoiding reliance on play-level fact gathering.

@richm richm requested a review from spetrosi as a code owner March 20, 2026 19:11
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 20, 2026

Reviewer's Guide

Tests now invoke linux-system-roles.trustee_attestation_server through a reusable helper task that clears facts before including the role, ensuring the role gathers required facts even when ANSIBLE_GATHERING is explicit, and related fact-dependent vars are refactored to rely on set_fact.

File-Level Changes

Change Details Files
Tests run the role via a helper task that clears facts and optionally suppresses failures before include_role execution.
  • tests_default.yml now includes tasks/run_role_with_clear_facts.yml instead of using gather_facts toggle and direct role invocation.
  • New tests/tasks/run_role_with_clear_facts.yml clears facts, conditionally emulates include_role failed_when false handling via block/rescue, and runs linux-system-roles.trustee_attestation_server with configurable tasks/public exports.
tests/tests_default.yml
tests/tasks/run_role_with_clear_facts.yml
Role include-var tests updated to rely on helper task semantics and correct role name.
  • Removed gather_facts usage in tests_include_vars_from_parent.yml and ensured the role under test aligns with trustee_attestation_server.
  • Var override test now imports linux-system-roles.trustee_attestation_server instead of linux-system-roles.template.
tests/tests_include_vars_from_parent.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 20, 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 new tests/tasks/run_role_with_clear_facts.yml and the updated tests now hard-code the role name linux-system-roles.trustee_attestation_server, whereas tests_default.yml previously referenced linux-system-roles.template; if this helper is meant to be generic or re-used across roles, consider parameterizing the role name instead of hard-coding it.
  • The header comment in run_role_with_clear_facts.yml still refers to "run linux-system-roles.trustee_attestation_server"; if the intent is to make this a generic helper for running roles with cleared facts, updating the comment and interface to reflect that would make its purpose clearer and reduce confusion.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `tests/tasks/run_role_with_clear_facts.yml` and the updated tests now hard-code the role name `linux-system-roles.trustee_attestation_server`, whereas `tests_default.yml` previously referenced `linux-system-roles.template`; if this helper is meant to be generic or re-used across roles, consider parameterizing the role name instead of hard-coding it.
- The header comment in `run_role_with_clear_facts.yml` still refers to "run linux-system-roles.trustee_attestation_server"; if the intent is to make this a generic helper for running roles with cleared facts, updating the comment and interface to reflect that would make its purpose clearer and reduce confusion.

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.

…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
Copy link
Copy Markdown
Contributor Author

richm commented Mar 20, 2026

[citest]

@richm richm closed this Mar 20, 2026
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