test: ensure role gathers the facts it uses by having test clear_facts before include_role#338
Merged
Merged
Conversation
Reviewer's GuideIntroduces a shared test helper task file that clears Ansible facts before invoking the linux-system-roles.timesync role, refactors tests to use it instead of direct include_role/roles and gather_facts, and adjusts tests to rely on a set_fact-derived interface variable so they remain robust when facts are cleared. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Collaborator
Author
|
[citest] |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The new
__timesync_default_ipv4_interfaceis set viaset_factintasks/setup.yml, butmeta: clear_factsinrun_role_with_clear_facts.ymlwill clear facts set byset_factas well, so anything relying on that value after the role run (or inside the role viatimesync_ptp_domains) may see it undefined; consider storing this in a normal variable (not viaset_fact) or moving theclear_factsso it does not wipe this value. - Several plays now depend on
__timesync_default_ipv4_interfacein play-level vars (e.g.timesync_ptp_domains), which are evaluated lazily; it would be safer to ensure this variable is defined before any task that may reference those vars, or to compute the interface directly in the vars to avoid ordering issues betweentasks/setup.ymland the role include.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `__timesync_default_ipv4_interface` is set via `set_fact` in `tasks/setup.yml`, but `meta: clear_facts` in `run_role_with_clear_facts.yml` will clear facts set by `set_fact` as well, so anything relying on that value after the role run (or inside the role via `timesync_ptp_domains`) may see it undefined; consider storing this in a normal variable (not via `set_fact`) or moving the `clear_facts` so it does not wipe this value.
- Several plays now depend on `__timesync_default_ipv4_interface` in play-level vars (e.g. `timesync_ptp_domains`), which are evaluated lazily; it would be safer to ensure this variable is defined before any task that may reference those vars, or to compute the interface directly in the vars to avoid ordering issues between `tasks/setup.yml` and the role include.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Collaborator
Author
|
[citest] |
1 similar comment
Collaborator
Author
|
[citest] |
Collaborator
Author
|
[citest] |
1 similar comment
Collaborator
Author
|
[citest] |
…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. 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>
Collaborator
Author
|
[citest] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The role gathers the facts it uses. For example, if the user uses
ANSIBLE_GATHERING=explicit, the role uses thesetupmodule with thefacts and subsets it requires.
This change allows us to test this. Before every role invocation, the test
will use
meta: clear_factsso 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_factsfor the tests.Some vars defined using
ansible_factshave been changed to be defined withset_factinstead. This is because of the fact thatvarsare lazilyevaluated - the var might be referenced when the facts have been cleared, and
will issue an error like
ansible_facts["distribution"] is undefined. This istypically done for blocks that have a
whencondition that usesansible_factsand the block has a role invocation using run_role_with_clear_facts.yml
These have been rewritten to define the
whencondition usingset_fact. Thisis because the
whencondition is evaluated every time a task is invoked in theblock, and if the facts are cleared, this will raise an undefined variable error.
Signed-off-by: Rich Megginson rmeggins@redhat.com
Summary by Sourcery
Ensure the timesync role is always executed with a clean fact set in tests by routing test role invocations through a helper task file that clears facts before including the role.
New Features:
Enhancements:
Tests: