Skip to content

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

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

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

Conversation

@richm
Copy link
Collaborator

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

Ensure handlers are flushed so that services are in the correct state, restarted
if necessary, and wait a couple of seconds for them to start. This allows the
checks to pass consistently instead of potentially checking a service which is
in an indeterminate state. This also clears up an issue on some platforms where
a service triggered multiple times at the end of a test because the test should
have flushed the handlers and restarted the services during the test.

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

Summary by Sourcery

Run the metrics role in tests via a shared task file that clears facts first, ensuring the role consistently gathers all required facts itself.

Enhancements:

  • Declare architecture as an additional required fact for the metrics role.

Tests:

  • Add a reusable test task file that clears Ansible facts then runs the metrics role, with optional handling for expected failures.
  • Update all metrics role test playbooks to invoke the role through the new clear-facts task file instead of including the role directly.

…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 review from natoscott and sfeifer as code owners March 20, 2026 21:24
@sourcery-ai
Copy link

sourcery-ai bot commented Mar 20, 2026

Reviewer's Guide

This PR refactors all metrics role tests to execute the role through a new helper task file that clears Ansible facts before each invocation, adds a new required fact, and keeps role failure handling behavior consistent with include_role semantics.

Sequence diagram for running metrics role tests with cleared facts

sequenceDiagram
  actor Tester
  participant AnsibleTestPlaybook as Ansible_test_playbook
  participant HelperTask as tasks_run_role_with_clear_facts
  participant MetricsRole as metrics_role
  participant AnsibleFacts as ansible_facts

  Tester->>AnsibleTestPlaybook: run tests_*.yml
  AnsibleTestPlaybook->>HelperTask: include_tasks run_role_with_clear_facts.yml

  HelperTask->>AnsibleFacts: meta clear_facts
  HelperTask->>MetricsRole: include_role metrics_role

  activate MetricsRole
  MetricsRole->>AnsibleFacts: setup gather __metrics_required_facts
  Note over MetricsRole,AnsibleFacts: __metrics_required_facts includes architecture, distribution, distribution_major_version, distribution_version

  MetricsRole-->>HelperTask: role tasks complete
  deactivate MetricsRole

  HelperTask-->>AnsibleTestPlaybook: return
  AnsibleTestPlaybook-->>Tester: report test result
Loading

Flow diagram for metrics role fact handling in tests

flowchart TD
  A[Start test task
  run_role_with_clear_facts] --> B[meta clear_facts]
  B --> C[include_role metrics_role]

  subgraph Role_execution
    C --> D[Read __metrics_required_facts
    architecture
    distribution
    distribution_major_version
    distribution_version]
    D --> E[Run setup module
    gather required facts]
    E --> F[Role tasks use facts
    and set_fact for when conditions]
  end

  F --> G[End role execution]
  G --> H[Continue next test task]
Loading

File-Level Changes

Change Details Files
Introduce reusable test helper to clear facts then run the metrics role, preserving include_role semantics including optional failure ignoring.
  • Create tests/tasks/run_role_with_clear_facts.yml that first runs meta: clear_facts then includes the linux-system-roles.metrics role
  • Support __sr_tasks_from to select role task entrypoint and __sr_public to control variable export analogous to include_role parameters
  • Implement __sr_failed_when handling via a block/rescue wrapper that ignores role failures when explicitly requested while still running the role normally otherwise
tests/tasks/run_role_with_clear_facts.yml
Update all metrics test playbooks to use the new helper instead of include_role, passing through public visibility and any existing vars/conditions.
  • Replace include_role invocations of linux-system-roles.metrics with include_tasks: tasks/run_role_with_clear_facts.yml across test playbooks
  • Pass __sr_public: true where tests previously set public: true on include_role
  • Preserve existing when conditions and additional vars blocks while switching to the new helper task include
tests/tests_bz1855539.yml
tests/tests_bz1855544.yml
tests/tests_default.yml
tests/tests_verify_auth.yml
tests/tests_verify_basic.yml
tests/tests_verify_bpftrace.yml
tests/tests_verify_from_elasticsearch.yml
tests/tests_verify_from_spark.yml
tests/tests_verify_fullstack.yml
tests/tests_verify_into_elasticsearch.yml
tests/tests_verify_into_spark.yml
tests/tests_verify_mssql.yml
tests/tests_verify_notification.yml
tests/tests_verify_pmie_webhook.yml
tests/tests_verify_postfix.yml
tests/tests_verify_query.yml
tests/tests_verify_retention.yml
tests/tests_verify_graph.yml
tests/tests_verify_graph_use_cert_role.yml
Extend the role’s declared required facts to include architecture, ensuring the role explicitly gathers all facts it relies on.
  • Add architecture to the __metrics_required_facts list so the role’s internal fact gathering covers it
vars/main.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
Collaborator Author

richm commented Mar 20, 2026

NOTE - doing this testing actually found a case where the architecture fact was not being gathered, so it was added to the list of facts to gather for the role.

@richm
Copy link
Collaborator Author

richm commented Mar 20, 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 richm merged commit ac79a37 into linux-system-roles:main Mar 20, 2026
30 of 31 checks passed
@richm richm deleted the clear_facts branch March 20, 2026 22:31
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