Skip to content

Commit 6a75dee

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 96c690b commit 6a75dee

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
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.postfix.
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.postfix
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.postfix
35+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
36+
public: "{{ __sr_public | default(false) }}"
37+
when: __sr_failed_when | d(true)

tests/tests_default.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
---
22
- name: Ensure that the rule runs with default parameters
33
hosts: all
4-
gather_facts: false
54
tasks:
65
- name: Run the postfix role
7-
include_role:
8-
name: linux-system-roles.postfix
9-
public: true
6+
include_tasks: tasks/run_role_with_clear_facts.yml
107
vars:
8+
__sr_public: true
119
postfix_manage_firewall: true
1210
postfix_manage_selinux: true
1311

tests/tests_disable_ipv6.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
hosts: all
44
tags:
55
- tests::reboot
6-
gather_facts: false
76
tasks:
87
- name: Disable IPv6
98
include_role:
@@ -20,9 +19,9 @@
2019
meta: flush_handlers
2120

2221
- name: Run the postfix role
23-
include_role:
24-
name: linux-system-roles.postfix
25-
public: true
22+
include_tasks: tasks/run_role_with_clear_facts.yml
23+
vars:
24+
__sr_public: true
2625

2726
- name: Enable IPv6
2827
include_role:

tests/tests_previous_replaced.yml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
hosts: all
44
tasks:
55
- name: Run the role with previous=replaced only
6-
include_role:
7-
name: linux-system-roles.postfix
8-
public: true
6+
include_tasks: tasks/run_role_with_clear_facts.yml
97
vars:
8+
__sr_public: true
109
postfix_manage_firewall: true
1110
postfix_manage_selinux: false
1211
postfix_conf:
@@ -16,10 +15,9 @@
1615
include_tasks: check_firewall_selinux.yml
1716

1817
- name: Run the role to configure relay_domains and relayhost
19-
include_role:
20-
name: linux-system-roles.postfix
21-
public: true
18+
include_tasks: tasks/run_role_with_clear_facts.yml
2219
vars:
20+
__sr_public: true
2321
postfix_manage_firewall: true
2422
postfix_manage_selinux: true
2523
postfix_conf:
@@ -39,8 +37,7 @@
3937
__fingerprint: "system_role:postfix"
4038

4139
- name: Reset all settings and configure relay_domains
42-
include_role:
43-
name: linux-system-roles.postfix
40+
include_tasks: tasks/run_role_with_clear_facts.yml
4441
vars:
4542
postfix_conf:
4643
previous: replaced

tests/tests_set_banner.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
tasks:
1212
- name: Run the role with test smtpd_banner
13-
include_role:
14-
name: linux-system-roles.postfix
15-
public: true
13+
include_tasks: tasks/run_role_with_clear_facts.yml
14+
vars:
15+
__sr_public: true
1616

1717
- name: Get smtpd banner
1818
command: postconf smtpd_banner

tests/tests_set_file.yml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
tasks:
1515
- name: Run the role with test postmap file
16-
include_role:
17-
name: linux-system-roles.postfix
18-
public: true
16+
include_tasks: tasks/run_role_with_clear_facts.yml
17+
vars:
18+
__sr_public: true
1919

2020
- name: Check if postmap file exists
2121
stat:
@@ -27,16 +27,19 @@
2727
assert:
2828
that: test_file.stat.exists
2929

30+
- name: Set fact for relay_domains check (ansible version)
31+
set_fact:
32+
__sr_when_relay_domains_check: "{{ ansible_version['major'] > 2 or ansible_version['minor'] > 9 }}"
33+
3034
- name: Check relay_domains
31-
when: ansible_version["major"] > 2 or ansible_version["minor"] > 9
35+
when: __sr_when_relay_domains_check
3236
block:
3337
# cannot use postfix_default_database_type with postfix_conf
3438
# in ansible 2.9
3539
- name: Run the role to test postfix_default_database_type
36-
include_role:
37-
name: linux-system-roles.postfix
38-
public: true
40+
include_tasks: tasks/run_role_with_clear_facts.yml
3941
vars:
42+
__sr_public: true
4043
postfix_conf:
4144
relay_domains: "{{
4245
postfix_default_database_type }}:/etc/postfix/relay_domains"

0 commit comments

Comments
 (0)