Skip to content

Commit c6386f0

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 6ea6da5 commit c6386f0

File tree

5 files changed

+75
-55
lines changed

5 files changed

+75
-55
lines changed
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.systemd.
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.systemd
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.systemd
35+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
36+
public: "{{ __sr_public | default(false) }}"
37+
when: __sr_failed_when | d(true)

tests/tests_basic.yml

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@
22
---
33
- name: Ensure that deploying unit files work correctly
44
hosts: all
5-
gather_facts: false
65
vars:
76
systemd_unit_file_templates:
87
- nested/dir/templates/foo.service.j2
9-
roles:
10-
- linux-system-roles.systemd
118
tasks:
9+
- name: Run the role
10+
include_tasks: tasks/run_role_with_clear_facts.yml
1211
- name: Verify that unit file was deployed
1312
stat:
1413
path: /etc/systemd/system/foo.service
1514

1615
- name: Ensure that deploying unit file dropins work correctly
1716
hosts: all
18-
gather_facts: false
1917
vars:
2018
systemd_dropins:
2119
- nested/dir/templates/foo.service.conf.j2
22-
roles:
23-
- linux-system-roles.systemd
2420
tasks:
21+
- name: Run the role
22+
include_tasks: tasks/run_role_with_clear_facts.yml
2523
- name: Stat the dropin directory path
2624
stat:
2725
path: /etc/systemd/system/foo.service.d
@@ -38,13 +36,12 @@
3836

3937
- name: Ensure that we can start units using the role
4038
hosts: all
41-
gather_facts: false
4239
vars:
4340
systemd_started_units:
4441
- foo.service
45-
roles:
46-
- linux-system-roles.systemd
4742
tasks:
43+
- name: Run the role
44+
include_tasks: tasks/run_role_with_clear_facts.yml
4845
- name: Make sure that foo.service main state is active
4946
assert:
5047
that:
@@ -59,19 +56,18 @@
5956
6057
- name: Ensure that we can restart units using the role
6158
hosts: all
62-
gather_facts: false
6359
vars:
6460
systemd_restarted_units:
6561
- foo.service
66-
roles:
67-
- linux-system-roles.systemd
6862
pre_tasks:
6963
- name: Save MainPID of foo.service before restart
7064
# noqa command-instead-of-module
7165
command: systemctl show -p MainPID foo.service
7266
register: pid_before_restart
7367
changed_when: false
7468
tasks:
69+
- name: Run the role
70+
include_tasks: tasks/run_role_with_clear_facts.yml
7571
- name: Get MainPID of foo.service
7672
# noqa command-instead-of-module
7773
command: systemctl show -p MainPID foo.service
@@ -85,37 +81,35 @@
8581

8682
- name: Ensure that we can reload units
8783
hosts: all
88-
gather_facts: false
8984
vars:
9085
systemd_reloaded_units:
9186
- foo.service
92-
roles:
93-
- linux-system-roles.systemd
9487
pre_tasks:
9588
- name: Delete reload flag file
9689
file:
9790
path: /tmp/foo-service-reloaded
9891
state: absent
9992
tasks:
93+
- name: Run the role
94+
include_tasks: tasks/run_role_with_clear_facts.yml
10095
- name: Reload operation should created flag file
10196
stat:
10297
path: /tmp/foo-service-reloaded
10398

10499
- name: Ensure that we can enable unit files
105100
hosts: all
106-
gather_facts: false
107101
vars:
108102
systemd_enabled_units:
109103
- foo.service
110-
roles:
111-
- linux-system-roles.systemd
112104
pre_tasks:
113105
- name: Save UnitFileState before calling role
114106
# noqa command-instead-of-module
115107
command: systemctl show -p UnitFileState foo.service
116108
register: unit_file_state_before
117109
changed_when: false
118110
tasks:
111+
- name: Run the role
112+
include_tasks: tasks/run_role_with_clear_facts.yml
119113
- name: Get UnitFileState=
120114
# noqa command-instead-of-module
121115
command: systemctl show -p UnitFileState foo.service
@@ -130,13 +124,12 @@
130124

131125
- name: Ensure that we can disable unit files
132126
hosts: all
133-
gather_facts: false
134127
vars:
135128
systemd_disabled_units:
136129
- foo.service
137-
roles:
138-
- linux-system-roles.systemd
139130
tasks:
131+
- name: Run the role
132+
include_tasks: tasks/run_role_with_clear_facts.yml
140133
- name: Get UnitFileState= - 2
141134
# noqa command-instead-of-module
142135
command: systemctl show -p UnitFileState foo.service
@@ -150,20 +143,19 @@
150143

151144
- name: Ensure that we can mask unit files
152145
hosts: all
153-
gather_facts: false
154146
vars:
155147
# It is not possible to mask admin units files in /etc/systemd/system.
156148
systemd_masked_units:
157149
- sshd.service
158-
roles:
159-
- linux-system-roles.systemd
160150
pre_tasks:
161151
- name: Save UnitFileState before calling role - 2
162152
# noqa command-instead-of-module
163153
command: systemctl show -p UnitFileState sshd.service
164154
register: sshd_state_before
165155
changed_when: false
166156
tasks:
157+
- name: Run the role
158+
include_tasks: tasks/run_role_with_clear_facts.yml
167159
- name: Get UnitFileState= - 3
168160
# noqa command-instead-of-module
169161
command: systemctl show -p UnitFileState sshd.service
@@ -180,13 +172,12 @@
180172
181173
- name: Ensure that we can unmask unit files
182174
hosts: all
183-
gather_facts: false
184175
vars:
185176
systemd_unmasked_units:
186177
- sshd.service
187-
roles:
188-
- linux-system-roles.systemd
189178
tasks:
179+
- name: Run the role
180+
include_tasks: tasks/run_role_with_clear_facts.yml
190181
- name: Get UnitFileState= - 4
191182
# noqa command-instead-of-module
192183
command: systemctl show -p UnitFileState sshd.service
@@ -200,20 +191,18 @@
200191

201192
- name: Ensure that we can stop units
202193
hosts: all
203-
gather_facts: false
204194
vars:
205195
systemd_stopped_units:
206196
- foo.service
207-
roles:
208-
- linux-system-roles.systemd
209197
tasks:
198+
- name: Run the role
199+
include_tasks: tasks/run_role_with_clear_facts.yml
210200
- name: Foo.service shouldn't be in systemd_units
211201
fail:
212202
when: ansible_facts['systemd_units']['foo.service'] is defined
213203

214204
- name: Test unmask and start
215205
hosts: all
216-
gather_facts: false
217206
vars:
218207
# we need a
219208
# * system service provided in /usr/lib/systemd/system
@@ -247,8 +236,7 @@
247236
test_unit: "{{ __find_test_unit.stdout | trim }}"
248237

249238
- name: Ensure test unit is running and unmasked
250-
include_role:
251-
name: linux-system-roles.systemd
239+
include_tasks: tasks/run_role_with_clear_facts.yml
252240
vars:
253241
systemd_started_units:
254242
- "{{ test_unit }}"
@@ -269,8 +257,7 @@
269257
- test_unit_state.stdout is search("SubState=running")
270258

271259
- name: Stop and mask test unit
272-
include_role:
273-
name: linux-system-roles.systemd
260+
include_tasks: tasks/run_role_with_clear_facts.yml
274261
vars:
275262
systemd_stopped_units:
276263
- "{{ test_unit }}"
@@ -292,8 +279,7 @@
292279
and test_unit_state.stdout is search('SubState=dead') }}"
293280

294281
- name: Ensure test unit is running and unmasked - 2
295-
include_role:
296-
name: linux-system-roles.systemd
282+
include_tasks: tasks/run_role_with_clear_facts.yml
297283
vars:
298284
systemd_started_units:
299285
- "{{ test_unit }}"

tests/tests_default.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
---
33
- name: Ensure that the role runs with no arguments
44
hosts: all
5-
gather_facts: false
6-
roles:
7-
- linux-system-roles.systemd
5+
tasks:
6+
- name: Run the role
7+
include_tasks: tasks/run_role_with_clear_facts.yml

tests/tests_facts.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
---
33
- name: Ensure that basic features of role work correctly
44
hosts: all
5-
roles:
6-
- linux-system-roles.systemd
75
tasks:
6+
- name: Run the role
7+
include_tasks: tasks/run_role_with_clear_facts.yml
8+
vars:
9+
__sr_public: true
10+
811
- name: Print units facts
912
debug:
1013
msg: "{{ systemd_units }}"

tests/tests_user_units.yml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Ensure that the role works with a mix of user and system units
44
hosts: all
5-
gather_facts: false
65
vars:
76
systemd_fail_if_too_old: false # allow test to pass on el7
87
__bar_service_name: bar.service
@@ -67,10 +66,9 @@
6766
loop: "{{ __users }}"
6867

6968
- name: Run role to create and start units
70-
include_role:
71-
name: linux-system-roles.systemd
72-
public: true
69+
include_tasks: tasks/run_role_with_clear_facts.yml
7370
vars:
71+
__sr_public: true
7472
systemd_unit_files: "{{ __systemd_unit_files }}"
7573
systemd_unit_file_templates: "{{ __systemd_unit_file_templates }}"
7674
systemd_dropins: "{{ __systemd_dropins }}"
@@ -136,8 +134,7 @@
136134
scope: "{{ '--system' if item.user == 'root' else '--user' }}"
137135

138136
- name: Run role to enable units
139-
include_role:
140-
name: linux-system-roles.systemd
137+
include_tasks: tasks/run_role_with_clear_facts.yml
141138
vars:
142139
systemd_enabled_units: "{{ __systemd_enabled_units }}"
143140

@@ -156,8 +153,7 @@
156153
scope: "{{ '--system' if item.user == 'root' else '--user' }}"
157154

158155
- name: Run role to disable units
159-
include_role:
160-
name: linux-system-roles.systemd
156+
include_tasks: tasks/run_role_with_clear_facts.yml
161157
vars:
162158
systemd_disabled_units: "{{ __systemd_disabled_units }}"
163159

@@ -176,8 +172,7 @@
176172
scope: "{{ '--system' if item.user == 'root' else '--user' }}"
177173

178174
- name: Run role to stop units
179-
include_role:
180-
name: linux-system-roles.systemd
175+
include_tasks: tasks/run_role_with_clear_facts.yml
181176
vars:
182177
systemd_stopped_units: "{{ __systemd_stopped_units }}"
183178

@@ -187,10 +182,9 @@
187182
loop: "{{ __all_units }}"
188183

189184
- name: Run role to remove unit files and dropins
190-
include_role:
191-
name: linux-system-roles.systemd
192-
public: true
185+
include_tasks: tasks/run_role_with_clear_facts.yml
193186
vars:
187+
__sr_public: true
194188
systemd_unit_files: "{{ __systemd_unit_files |
195189
map('combine', __absent) | list }}"
196190
systemd_unit_file_templates: "{{ __systemd_unit_file_templates |

0 commit comments

Comments
 (0)