Skip to content

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

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

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

Conversation

@richm
Copy link
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

Introduce a shared test helper that clears Ansible facts before each role invocation and update tests to use it so the mssql role is exercised with explicit fact gathering.

Bug Fixes:

  • Prevent test failures caused by lazy evaluation of ansible_facts when facts are cleared between role runs.

Enhancements:

  • Centralize role execution in tests via a reusable run_role_with_clear_facts task file that supports tasks_from, public, and failed_when semantics.
  • Precompute fact-based conditions with set_fact in tests to avoid undefined variable errors after facts are cleared.
  • Simplify test playbooks by dropping redundant gather_facts directives, relying on the role to gather required facts.

Tests:

  • Refactor mssql role test playbooks to include the new run_role_with_clear_facts helper instead of calling the role directly, ensuring each test run starts from a clean fact state.

…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 21:37
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 19, 2026

Reviewer's Guide

This PR refactors the MSSQL role tests to run the role through a new helper task file that clears Ansible facts before each invocation, and restructures conditional logic that depended on ansible_facts to avoid undefined-variable errors when facts are cleared.

File-Level Changes

Change Details Files
Introduce a reusable helper task file to clear facts then run the mssql role with optional behavior flags.
  • Add tests/tasks/run_role_with_clear_facts.yml that calls meta: clear_facts before include_role for linux-system-roles.mssql.
  • Support parameters for tasks_from, public variable export, and a __sr_failed_when flag to simulate include_role failed_when behavior using a block/rescue.
  • Ensure the role is also run in a normal path guarded by __sr_failed_when defaulting to true.
tests/tasks/run_role_with_clear_facts.yml
Update tests to use the new helper instead of including the role directly, and to avoid explicit gather_facts where no longer needed.
  • Replace include_role: linux-system-roles.mssql with include_tasks: tasks/run_role_with_clear_facts.yml across many role test task files and playbooks.
  • Pass through __sr_public in tests that previously used include_role public: true.
  • Remove explicit gather_facts: true/false from several test playbooks now that the role is responsible for gathering the facts it needs.
tests/tasks/tests_ha_single.yml
tests/tasks/tests_tls.yml
tests/tasks/assert_fail_on_unsupported_ver.yml
tests/tests_selinux_enforcing_2022.yml
tests/tasks/tests_input_sql_file.yml
tests/tasks/tests_password.yml
tests/tests_2019_upgrade.yml
tests/tests_2022_upgrade.yml
tests/tests_configure_ha_cluster_external.yml
tests/tasks/tests_idempotency.yml
tests/tasks/tests_tcp_firewall.yml
tests/tasks/upgrade_and_assert.yml
tests/playbooks/tests_ad_integration.yml
tests/playbooks/tests_ad_integration_join_false.yml
tests/playbooks/tests_ad_integration_w_keytab.yml
tests/tests_accept_eula.yml
tests/tests_configure_ha_cluster_external_read_only.yml
tests/tests_configure_ha_cluster_read_scale.yml
tests/tests_default_2019.yml
tests/tests_ha_single_2017.yml
tests/tests_ha_single_2019.yml
tests/tests_ha_single_2022.yml
tests/tests_idempotency_2017.yml
tests/tests_idempotency_2019.yml
tests/tests_idempotency_2022.yml
tests/tests_include_vars_from_parent.yml
Prevent lazy evaluation of conditions referencing ansible_facts that would break after meta: clear_facts by precomputing them with set_fact.
  • Add set_fact tasks that compute boolean helper vars (for example, __mssql_ha_el7_configure_block, __mssql_assert_unsupported_platform, __mssql_selinux_unsupported_pre_rhel9) from ansible_facts before any facts are cleared.
  • Update when clauses to reference these helper vars instead of directly accessing ansible_facts, avoiding undefined-variable errors after clear_facts.
tests/tasks/tests_ha_single.yml
tests/tasks/assert_fail_on_unsupported_ver.yml
tests/tests_selinux_enforcing_2022.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
Contributor Author

richm commented Mar 19, 2026

[citest]

Copy link

@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 reviewed your changes and they look great!


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

richm commented Mar 19, 2026

@spetrosi are these errors expected? They don't look like they are related to clearing facts.

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