Skip to content

Commit 8893485

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 ea5e8fe commit 8893485

7 files changed

Lines changed: 52 additions & 25 deletions

tests/tasks/cleanup.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
- name: Use role purge to remove settings
88
block:
99
- name: Run role with purge to remove everything
10-
include_role:
11-
name: linux-system-roles.kernel_settings
10+
include_tasks: tasks/run_role_with_clear_facts.yml
1211
vars:
1312
kernel_settings_purge: true
1413
kernel_settings_sysctl: []
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# Task file: clear_facts, run linux-system-roles.kernel_settings.
3+
# Include this with include_tasks or import_tasks
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+
- name: Clear facts
9+
meta: clear_facts
10+
11+
# note that you can use failed_when with import_role but not with include_role
12+
# so this simulates the __sr_failed_when false case
13+
# Q: Why do we need a separate task to run the role normally? Why not just
14+
# run the role in the block and rethrow the error in the rescue block?
15+
# A: Because you cannot rethrow the error in exactly the same way as the role does.
16+
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
17+
- name: Run the role with __sr_failed_when false
18+
when:
19+
- __sr_failed_when is defined
20+
- not __sr_failed_when
21+
block:
22+
- name: Run the role
23+
include_role:
24+
name: linux-system-roles.kernel_settings
25+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
26+
public: "{{ __sr_public | default(false) }}"
27+
rescue:
28+
- name: Ignore the failure when __sr_failed_when is false
29+
debug:
30+
msg: Ignoring failure when __sr_failed_when is false
31+
32+
- name: Run the role normally
33+
include_role:
34+
name: linux-system-roles.kernel_settings
35+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
36+
public: "{{ __sr_public | default(false) }}"
37+
when: __sr_failed_when | d(true)

tests/tests_bool_not_allowed.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
- name: Validate no boolean values for sysctl values
66
block:
77
- name: Try to pass a boolean value for sysctl value
8-
include_role:
9-
name: linux-system-roles.kernel_settings
10-
public: true
8+
include_tasks: tasks/run_role_with_clear_facts.yml
119
vars:
10+
__sr_public: true
1211
kernel_settings_sysctl:
1312
- name: valid_value
1413
value: "true"

tests/tests_change_settings.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,9 @@
5858
enabled: true
5959

6060
- name: Apply kernel_settings
61-
include_role:
62-
name: linux-system-roles.kernel_settings
63-
public: true
61+
include_tasks: tasks/run_role_with_clear_facts.yml
6462
vars:
63+
__sr_public: true
6564
kernel_settings_sysctl:
6665
- name: fs.file-max
6766
value: 400000
@@ -120,8 +119,7 @@
120119
changed_when: false
121120

122121
- name: Apply role again and remove settings
123-
include_role:
124-
name: linux-system-roles.kernel_settings
122+
include_tasks: tasks/run_role_with_clear_facts.yml
125123
vars:
126124
kernel_settings_reboot_ok: true
127125
kernel_settings_sysctl:
@@ -148,8 +146,7 @@
148146
changed_when: false
149147

150148
- name: Apply kernel_settings for removing
151-
include_role:
152-
name: linux-system-roles.kernel_settings
149+
include_tasks: tasks/run_role_with_clear_facts.yml
153150
vars:
154151
kernel_settings_reboot_ok: true
155152
kernel_settings_sysctl:
@@ -181,8 +178,7 @@
181178
changed_when: false
182179

183180
- name: Apply kernel_settings for removing section
184-
include_role:
185-
name: linux-system-roles.kernel_settings
181+
include_tasks: tasks/run_role_with_clear_facts.yml
186182
vars:
187183
kernel_settings_reboot_ok: true
188184
kernel_settings_sysctl:

tests/tests_default.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
---
22
- name: Ensure that the role runs with default parameters
33
hosts: all
4-
gather_facts: false
54
tasks:
65
- name: Run test
76
block:
87
- name: Run role with no settings
9-
include_role:
10-
name: linux-system-roles.kernel_settings
11-
public: true
8+
include_tasks: tasks/run_role_with_clear_facts.yml
9+
vars:
10+
__sr_public: true
1211

1312
always:
1413
- name: Cleanup

tests/tests_include_vars_from_parent.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Test role variable override
33
hosts: all
4-
gather_facts: true
54
tasks:
65
- name: Run test
76
block:

tests/tests_simple_settings.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
---
22
- name: Test simple kernel settings
33
hosts: all
4-
gather_facts: true
54
tags:
65
- tests::reboot
76
tasks:
@@ -23,9 +22,9 @@
2322
# __kernel_settings_profile_filename to verify
2423
# that the settings were applied correctly
2524
- name: Apply the settings - call the role
26-
include_role:
27-
name: linux-system-roles.kernel_settings
28-
public: true
25+
include_tasks: tasks/run_role_with_clear_facts.yml
26+
vars:
27+
__sr_public: true
2928

3029
- name: Verify that settings were applied correctly
3130
include_tasks: tasks/assert_kernel_settings.yml
@@ -61,8 +60,7 @@
6160
- not kernel_settings_test_reboot_ok | d(false)
6261

6362
- name: Apply the settings again to check idempotency
64-
include_role:
65-
name: linux-system-roles.kernel_settings
63+
include_tasks: tasks/run_role_with_clear_facts.yml
6664

6765
- name: Ensure role reported not changed
6866
assert:

0 commit comments

Comments
 (0)