Skip to content

Commit b269e48

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 6733078 commit b269e48

9 files changed

Lines changed: 43 additions & 17 deletions
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.sudo.
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.sudo
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.sudo
35+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
36+
public: "{{ __sr_public | default(false) }}"
37+
when: __sr_failed_when | d(true)

tests/tests_check_if_configured.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Ensure that the role runs with default parameters
44
hosts: all
5-
gather_facts: false # test that role works in this case
65
tasks:
76
- name: Run tests
87
block:
@@ -32,8 +31,7 @@
3231
changed_when: true
3332

3433
- name: Run the role
35-
include_role:
36-
name: linux-system-roles.sudo
34+
include_tasks: tasks/run_role_with_clear_facts.yml
3735
vars:
3836
sudo_check_if_configured: true
3937

tests/tests_default.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Ensure that the role runs with default parameters
44
hosts: all
5-
gather_facts: false # test that role works in this case
65
tasks:
76
- name: Run tests
87
block:
@@ -11,8 +10,7 @@
1110
when: not __bootc_validation | d(false)
1211

1312
- name: Run the role
14-
include_role:
15-
name: linux-system-roles.sudo
13+
include_tasks: tasks/run_role_with_clear_facts.yml
1614
vars:
1715
sudo_check_if_configured: false
1816
when: not __bootc_validation | d(false)

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 include variable override
33
hosts: all
4-
gather_facts: true
54
tasks:
65
- name: Run tests
76
block:

tests/tests_large_configuration.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
include_tasks: tasks/setup.yml
99

1010
- name: Run the role
11-
include_role:
12-
name: linux-system-roles.sudo
11+
include_tasks: tasks/run_role_with_clear_facts.yml
1312
vars:
1413
sudo_rewrite_default_sudoers_file: true
1514
sudo_remove_unauthorized_included_files: true

tests/tests_multiple_sudoers.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
include_tasks: tasks/setup.yml
99

1010
- name: Run the role
11-
include_role:
12-
name: linux-system-roles.sudo
11+
include_tasks: tasks/run_role_with_clear_facts.yml
1312
vars:
1413
sudo_rewrite_default_sudoers_file: true
1514
sudo_remove_unauthorized_included_files: true

tests/tests_role_applied.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
include_tasks: tasks/setup.yml
99

1010
- name: Run the role
11-
include_role:
12-
name: linux-system-roles.sudo
11+
include_tasks: tasks/run_role_with_clear_facts.yml
1312
vars:
1413
sudo_rewrite_default_sudoers_file: true
1514
sudo_remove_unauthorized_included_files: true

tests/tests_scan_sudoers.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Ensure that the role can parse existing sudoers
44
hosts: all
5-
gather_facts: false # test that role works in this case
65
vars:
76
alias_values:
87
Cmnd_Alias:
@@ -55,8 +54,7 @@
5554
mode: preserve
5655

5756
- name: Run the role
58-
include_role:
59-
name: linux-system-roles.sudo
57+
include_tasks: tasks/run_role_with_clear_facts.yml
6058
vars:
6159
sudo_rewrite_default_sudoers_file: true
6260
sudo_remove_unauthorized_included_files: true

tests/unit/test_scan_sudoers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#
66
"""Unit tests for the scan_sudoers module"""
77

8-
98
from __future__ import absolute_import, division, print_function
109

1110
__metaclass__ = type

0 commit comments

Comments
 (0)