Skip to content

Commit 5501cd6

Browse files
committed
test: ensure role gathers the facts it uses by having test clear_facts 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>
1 parent f60df3a commit 5501cd6

File tree

52 files changed

+181
-159
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+181
-159
lines changed

tests/library

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/tasks/assert_node_options_check.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
- name: Run the role and check for errors
88
block:
99
- name: Run the role
10-
include_role:
11-
name: linux-system-roles.ha_cluster
10+
include_tasks: tasks/run_role_with_clear_facts.yml
1211

1312
- name: Fail
1413
fail:

tests/tasks/fixture_psks.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
file: vars/rh_distros_vars.yml
66
when: __ha_cluster_is_rh_distro is not defined
77

8+
- name: Set variable for crypto-policies PQC block
9+
set_fact:
10+
__need_crypto_policies_pqc: >-
11+
{{ __ha_cluster_is_rh_distro | bool and (
12+
(ansible_facts['distribution'] == 'RedHat' and
13+
ansible_facts['distribution_version'] is version('9.7', '>=') and
14+
ansible_facts['distribution_version'] is version('10', '<'))
15+
or (ansible_facts['distribution'] != 'RedHat' and
16+
ansible_facts['distribution_major_version'] is version('9', '=='))
17+
) }}
18+
819
- name: Check if the managed node needs crypto-policies to be able to use PQC
9-
when:
10-
- __ha_cluster_is_rh_distro | bool
11-
- (ansible_facts["distribution"] == "RedHat" and ansible_facts["distribution_version"] is version("9.7", ">=")
12-
and ansible_facts["distribution_version"] is version("10", "<"))
13-
or (ansible_facts["distribution"] != "RedHat" and
14-
ansible_facts['distribution_major_version'] is version("9", "=="))
20+
when: __need_crypto_policies_pqc | bool
1521
block:
1622
# calling role with null will just return the current policy
1723
- name: Get current crypto policy
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# Task file: save facts, clear_facts, run linux-system-roles.ha_cluster, then restore facts.
3+
# Include this with include_tasks or import_tasks; ensure tests/library is in module search path.
4+
# Input:
5+
# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role
6+
# - __sr_public: export private vars from role - same as public in include_role
7+
# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role
8+
# Output:
9+
# - ansible_facts: merged saved ansible_facts with ansible_facts modified by the role, if any
10+
- name: Clear facts
11+
meta: clear_facts
12+
13+
# note that you can use failed_when with import_role but not with include_role
14+
# so this simulates the __sr_failed_when false case
15+
# Q: Why do we need a separate task to run the role normally? Why not just
16+
# run the role in the block and rethrow the error in the rescue block?
17+
# A: Because you cannot rethrow the error in exactly the same way as the role does.
18+
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
19+
- name: Run the role with __sr_failed_when false
20+
when:
21+
- __sr_failed_when is defined
22+
- not __sr_failed_when
23+
block:
24+
- name: Run the role
25+
include_role:
26+
name: linux-system-roles.ha_cluster
27+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
28+
public: "{{ __sr_public | default(false) }}"
29+
rescue:
30+
- name: Ignore the failure when __sr_failed_when is false
31+
debug:
32+
msg: Ignoring failure when __sr_failed_when is false
33+
34+
- name: Run the role normally
35+
include_role:
36+
name: linux-system-roles.ha_cluster
37+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
38+
public: "{{ __sr_public | default(false) }}"
39+
when: __sr_failed_when | d(true)

tests/template_qdevice.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
run_once: true # noqa: run_once[task]
2828

2929
- name: Run HA Cluster role
30-
include_role:
31-
name: linux-system-roles.ha_cluster
32-
# variables for the role are supposed to be defined in a playbook which
33-
# includes this task file
30+
include_tasks: tasks/run_role_with_clear_facts.yml
31+
# variables for the role are supposed to be defined in a playbook which
32+
# includes this task file
3433

3534
# The role removed qnetd configuration
3635
- name: Restore qnetd

tests/template_sbd_all_options.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
- /etc/modules-load.d/softdog.conf
2424

2525
- name: Run HA Cluster role
26-
include_role:
27-
name: linux-system-roles.ha_cluster
28-
public: true
26+
include_tasks: tasks/run_role_with_clear_facts.yml
27+
vars:
28+
__sr_public: true
2929

3030
- name: Slurp generated SBD watchdog blocklist file
3131
slurp:

tests/tests_bootc_e2e_cluster.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
when: not __bootc_validation | d(false)
1818
block:
1919
- name: Run HA Cluster role
20-
include_role:
21-
name: linux-system-roles.ha_cluster
22-
public: true
20+
include_tasks: tasks/run_role_with_clear_facts.yml
21+
vars:
22+
__sr_public: true
2323
when: not __bootc_validation | d(false)
2424

2525
- name: Create QEMU deployment during bootc end-to-end test

tests/tests_bootc_e2e_qnetd.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
when: not __bootc_validation | d(false)
2323
block:
2424
- name: Run HA Cluster role
25-
include_role:
26-
name: linux-system-roles.ha_cluster
27-
public: true
25+
include_tasks: tasks/run_role_with_clear_facts.yml
26+
vars:
27+
__sr_public: true
2828
when: not __bootc_validation | d(false)
2929

3030
- name: Create QEMU deployment during bootc end-to-end test

tests/tests_cib_acls.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
tasks_from: test_setup.yml
2020

2121
- name: Run HA Cluster role
22-
include_role:
23-
name: linux-system-roles.ha_cluster
24-
public: true
22+
include_tasks: tasks/run_role_with_clear_facts.yml
2523
vars:
24+
__sr_public: true
2625
ha_cluster_cluster_name: test-cluster
2726
ha_cluster_manage_firewall: true
2827
ha_cluster_manage_selinux: true

tests/tests_cib_alerts.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
tasks_from: test_setup.yml
2020

2121
- name: Run HA Cluster role
22-
include_role:
23-
name: linux-system-roles.ha_cluster
24-
public: true
22+
include_tasks: tasks/run_role_with_clear_facts.yml
2523
vars:
24+
__sr_public: true
2625
ha_cluster_cluster_name: test-cluster
2726
ha_cluster_manage_firewall: true
2827
ha_cluster_manage_selinux: true

0 commit comments

Comments
 (0)