Skip to content

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

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

Conversation

@richm
Copy link
Copy Markdown
Contributor

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

Introduce a reusable test task to run the linux-system-roles.network role after clearing facts, and update network tests to use fact-independent conditions and this new helper.

Tests:

  • Add a run_role_with_clear_facts.yml task helper and refactor tests to invoke the network role through it so each run starts without cached facts.
  • Define and use synthetic platform fact variables in tests to replace direct ansible_facts access, improving robustness under explicit fact gathering modes.
  • Adjust various test playbook conditions, cleanup flows, and failure expectations to work with the new helper and platform fact variables.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Mar 9, 2026

Reviewer's Guide

Test harness is updated so every network role invocation is run via a new helper task file that clears Ansible facts first, and all test playbooks are refactored to use precomputed platform helper facts and the helper task instead of directly importing the role or referencing ansible_facts in when-conditions.

File-Level Changes

Change Details Files
Introduce run_role_with_clear_facts helper task and refactor tests to use it instead of directly including/importing the network role, with support for selectively ignoring failures.
  • Add tests/tasks/run_role_with_clear_facts.yml which clears facts, runs linux-system-roles.network via include_role, and optionally suppresses failures when __sr_failed_when is false.
  • Replace many import_role/include_role usages in test playbooks and test task files with include_tasks: tasks/run_role_with_clear_facts.yml, passing through existing role variables (e.g. network_connections, network_state).
  • Where tests previously used failed_when: false around role invocations, switch to passing __sr_failed_when: false into run_role_with_clear_facts.yml to preserve behavior.
tests/tasks/run_role_with_clear_facts.yml
tests/playbooks/tests_ipv6_dns_search.yml
tests/tests_change_indication_on_repeat_run.yml
tests/tests_default.yml
tests/tasks/cleanup_bond_profile+device.yml
tests/tasks/cleanup_vlan_and_parent_profile+device.yml
tests/playbooks/tests_bridge.yml
tests/playbooks/tests_bridge_cloned_mac.yml
tests/playbooks/tests_eth_dns_support.yml
tests/playbooks/tests_ethernet.yml
tests/playbooks/tests_ipv6_disabled.yml
tests/playbooks/tests_routing_rules.yml
tests/playbooks/tests_wireless_plugin_installation.yml
tests/tasks/activate_profile.yml
tests/tasks/create_bond_port_match_by_mac.yml
tests/tasks/create_bond_profile.yml
tests/tasks/create_bond_profile_reconfigure.yml
tests/tasks/create_bridge_profile.yml
tests/tasks/create_bridge_profile_no_autoconnect.yml
tests/tasks/create_dummy_profile.yml
tests/tasks/create_mac_address_match.yml
tests/tasks/create_team_profile.yml
tests/tasks/create_wireless_profile_restart_network.yml
tests/tasks/down_profile.yml
tests/tasks/find+remove_profile.yml
tests/tasks/provider/create_and_remove_with_initscripts.yml
tests/tasks/provider/create_with_nm.yml
tests/tasks/provider/default_with_nm.yml
tests/tasks/remove+down_profile.yml
tests/tasks/remove_profile.yml
tests/tasks/test_802.1x_capath.yml
tests/playbooks/manual_test_ethtool_coalesce.yml
tests/playbooks/tests_auto_gateway.yml
tests/playbooks/tests_bond.yml
tests/playbooks/tests_bond_cloned_mac.yml
tests/playbooks/tests_bond_deprecated.yml
tests/playbooks/tests_bond_removal.yml
tests/playbooks/tests_ethtool_coalesce.yml
tests/playbooks/tests_ethtool_ring.yml
tests/playbooks/tests_eth_pci_address_match.yml
tests/playbooks/tests_infiniband.yml
tests/playbooks/tests_ipv6.yml
tests/playbooks/tests_network_state.yml
tests/playbooks/tests_route_device.yml
tests/playbooks/tests_route_table.yml
tests/playbooks/tests_route_type.yml
tests/playbooks/tests_switch_provider.yml
tests/playbooks/tests_vlan_mtu.yml
tests/playbooks/tests_wireless.yml
tests/playbooks/tests_wireless_wpa3_owe.yml
tests/playbooks/tests_wireless_wpa3_sae.yml
tests/playbooks/tests_checkpoint_cleanup.yml
tests/playbooks/tests_ignore_auto_dns.yml
tests/playbooks/tests_reapply.yml
Make network role tests resilient to ANSIBLE_GATHERING=explicit and fact clearing by computing and using stable helper variables instead of direct ansible_facts access in conditions.
  • Introduce a standard pattern in many test playbooks to include vars/rh_distros_vars.yml and then set platform helper facts like __network_distro_major_version, __network_is_rhel, __network_is_fedora, __network_is_centos, __network_is_os_family_rhel, and __is_rh_distro via set_fact.
  • Update when conditions and extra run condition strings in Python-generated playbooks and YAML tests to use these helper variables instead of ansible_facts[...] expressions so they remain valid even when facts have been cleared.
  • Refactor certain tests (e.g. IPv6 DNS search error case) to use block/rescue with run_role_with_clear_facts.yml and rely on __network_connections_result instead of failed_when: false and direct ansible_facts checks.
tests/ensure_provider_tests.py
tests/tests_provider_nm.yml
tests/tests_regression_nm.yml
tests/tests_802_1x_nm.yml
tests/tests_802_1x_updated_nm.yml
tests/tests_integration_pytest.yml
tests/tests_auto_gateway_initscripts.yml
tests/tests_bond_cloned_mac_initscripts.yml
tests/tests_bond_deprecated_initscripts.yml
tests/tests_bond_initscripts.yml
tests/tests_bond_removal_initscripts.yml
tests/tests_bridge_cloned_mac_initscripts.yml
tests/tests_bridge_initscripts.yml
tests/tests_ethernet_initscripts.yml
tests/tests_ethtool_coalesce_initscripts.yml
tests/tests_ethtool_features_initscripts.yml
tests/tests_ethtool_ring_initscripts.yml
tests/tests_ipv6_initscripts.yml
tests/tests_route_device_initscripts.yml
tests/tests_states_initscripts.yml
tests/tests_team_nm.yml
tests/tests_team_plugin_installation_nm.yml
tests/tests_vlan_mtu_initscripts.yml
tests/tests_wireless_wpa3_owe_nm.yml
tests/tests_default_initscripts.yml
tests/tests_eth_pci_address_match_nm.yml
tests/tests_ethtool_coalesce_nm.yml
tests/tests_ethtool_features_nm.yml
tests/tests_ethtool_ring_nm.yml
tests/tests_mac_address_match_nm.yml
tests/tests_route_type_nm.yml
tests/tests_network_state_nm.yml
tests/tests_wireless_nm.yml
tests/tests_wireless_wpa3_sae_nm.yml
tests/tests_auto_gateway_nm.yml
tests/tests_bond_cloned_mac_nm.yml
tests/tests_bond_deprecated_nm.yml
tests/tests_bond_nm.yml
tests/tests_bond_options_nm.yml
tests/tests_bond_port_match_by_mac_nm.yml
tests/tests_bond_removal_nm.yml
tests/tests_bridge_cloned_mac_nm.yml
tests/tests_bridge_nm.yml
tests/tests_dummy_nm.yml
tests/tests_eth_dns_support_nm.yml
tests/tests_ethernet_nm.yml
tests/tests_ignore_auto_dns_nm.yml
tests/tests_infiniband_nm.yml
tests/tests_ipv6_disabled_nm.yml
tests/tests_ipv6_dns_search_nm.yml
tests/tests_ipv6_nm.yml
tests/tests_reapply_nm.yml
tests/tests_route_device_nm.yml
tests/tests_route_table_nm.yml
tests/tests_routing_rules_nm.yml
tests/tests_states_nm.yml
tests/tests_switch_provider.yml
tests/tests_vlan_mtu_nm.yml
tests/tests_wireless_and_network_restart_nm.yml
tests/tests_wireless_nm.yml
tests/tests_wireless_plugin_installation_nm.yml
tests/tests_wireless_wpa3_owe_nm.yml
tests/tests_wireless_wpa3_sae_nm.yml
Expose the role’s network_connections module implementation to tests.
  • Add tests/library/network_connections.py as a shim that points to ../../library/network_connections.py so tests can import/use the same module implementation as the role.
tests/library/network_connections.py

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:

  • The new merge_ansible_facts module updates saved_ansible_facts in-place (saved.update(current)); if callers ever re-use the original saved_facts value elsewhere this could introduce subtle side effects, so consider copying (saved = saved.copy()) before updating.
  • The MINIMUM_NM_VERSION_CHECK template string was changed to put the condition on the same line as the opening triple quote, which makes its indentation brittle and harder to read compared to the other multiline YAML snippets; it may be clearer and safer to keep a leading newline and consistent indentation like the other constants.
  • The repeated include_vars: vars/rh_distros_vars.yml plus set_fact block to derive __network_* platform facts appears in many test playbooks; consider moving this into a small shared task file and including it to reduce duplication and the risk of divergence between files.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `merge_ansible_facts` module updates `saved_ansible_facts` in-place (`saved.update(current)`); if callers ever re-use the original `saved_facts` value elsewhere this could introduce subtle side effects, so consider copying (`saved = saved.copy()`) before updating.
- The `MINIMUM_NM_VERSION_CHECK` template string was changed to put the condition on the same line as the opening triple quote, which makes its indentation brittle and harder to read compared to the other multiline YAML snippets; it may be clearer and safer to keep a leading newline and consistent indentation like the other constants.
- The repeated `include_vars: vars/rh_distros_vars.yml` plus `set_fact` block to derive `__network_*` platform facts appears in many test playbooks; consider moving this into a small shared task file and including it to reduce duplication and the risk of divergence between files.

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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 43.18%. Comparing base (1b57520) to head (06a9b47).
⚠️ Report is 76 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #845      +/-   ##
==========================================
+ Coverage   43.11%   43.18%   +0.06%     
==========================================
  Files          12       12              
  Lines        3124     3126       +2     
==========================================
+ Hits         1347     1350       +3     
+ Misses       1777     1776       -1     

☔ 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 7ad7f7d to 82431bc Compare March 9, 2026 21:55
@richm richm marked this pull request as draft March 9, 2026 21:55
@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 richm force-pushed the clear_facts branch 5 times, most recently from c5a0984 to 1e4391c Compare March 16, 2026 22:44
@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 18, 2026
@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 18, 2026

[citest]

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 19, 2026

[citest]

@richm
Copy link
Copy Markdown
Contributor Author

richm commented Mar 19, 2026

[citest]

@richm richm marked this pull request as ready for review March 19, 2026 13:27
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:

  • run_role_with_clear_facts.yml is documented as saving/restoring facts but only calls meta: clear_facts and then runs the role; either implement the save/restore behavior or update the header comment so it accurately describes what the task file does.
  • The repeated "Include distro variables" / "Set platform facts" blocks across many test playbooks are identical; consider extracting them into a shared task file (e.g. tests/tasks/set_platform_facts.yml) to reduce duplication and keep future changes to the platform predicates in one place.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- run_role_with_clear_facts.yml is documented as saving/restoring facts but only calls `meta: clear_facts` and then runs the role; either implement the save/restore behavior or update the header comment so it accurately describes what the task file does.
- The repeated "Include distro variables" / "Set platform facts" blocks across many test playbooks are identical; consider extracting them into a shared task file (e.g. tests/tasks/set_platform_facts.yml) to reduce duplication and keep future changes to the platform predicates in one place.

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 19, 2026

[citest]

@richm richm merged commit 88a2609 into linux-system-roles:main Mar 19, 2026
36 of 41 checks passed
@richm richm deleted the clear_facts branch March 19, 2026 16:47
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