Skip to content

Commit 28d2adb

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 81b554f commit 28d2adb

13 files changed

Lines changed: 116 additions & 165 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.rhc.
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.rhc
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.rhc
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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
---
33
- name: Ensure that the role runs with default parameters
44
hosts: all
5-
gather_facts: false
65
tasks:
76
- name: Verify that by default the role fails without credentials
87
block:
98
- name: Run the role with default parameters
10-
include_role:
11-
name: linux-system-roles.rhc
9+
include_tasks: tasks/run_role_with_clear_facts.yml
1210

1311
- name: Unreachable task
1412
fail:

tests/tests_environments.yml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Basic repository enablement/disablement test
44
hosts: all
5-
gather_facts: false
65
become: true
76
tags:
87
- tests::slow
@@ -33,8 +32,7 @@
3332
- name: Try to register (wrong environment)
3433
block:
3534
- name: Register (wrong environment)
36-
include_role:
37-
name: linux-system-roles.rhc
35+
include_tasks: tasks/run_role_with_clear_facts.yml
3836
vars:
3937
rhc_auth:
4038
login:
@@ -64,8 +62,7 @@
6462
- name: Test environments on registration
6563
block:
6664
- name: Register (with existing environments)
67-
include_role:
68-
name: linux-system-roles.rhc
65+
include_tasks: tasks/run_role_with_clear_facts.yml
6966
vars:
7067
rhc_auth:
7168
login:
@@ -105,8 +102,7 @@
105102
106103
always:
107104
- name: Unregister
108-
include_role:
109-
name: linux-system-roles.rhc
105+
include_tasks: tasks/run_role_with_clear_facts.yml
110106
vars:
111107
rhc_state: absent
112108
always:

tests/tests_insights_ansible_host.yml

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Insights ansible host test
44
hosts: all
5-
gather_facts: false
65
become: true
76

87
tasks:
@@ -12,10 +11,9 @@
1211
- name: Test ansible_host
1312
block:
1413
- name: Add ansible_host and register insights
15-
include_role:
16-
name: linux-system-roles.rhc
17-
public: true
14+
include_tasks: tasks/run_role_with_clear_facts.yml
1815
vars:
16+
__sr_public: true
1917
rhc_auth:
2018
login:
2119
username: "{{ lsr_rhc_test_data.reg_username }}"
@@ -41,8 +39,7 @@
4139
- name: Test ansible_host changed value after registration
4240
block:
4341
- name: Change ansible host to 'new-host'
44-
include_role:
45-
name: linux-system-roles.rhc
42+
include_tasks: tasks/run_role_with_clear_facts.yml
4643
vars:
4744
rhc_insights:
4845
ansible_host: new-host
@@ -52,8 +49,7 @@
5249
grep -ixq "^ansible_host=new-host" {{ __rhc_insights_conf }}
5350
changed_when: false
5451
- name: Change ansible host to a null value (noop)
55-
include_role:
56-
name: linux-system-roles.rhc
52+
include_tasks: tasks/run_role_with_clear_facts.yml
5753
vars:
5854
rhc_insights:
5955
ansible_host: null
@@ -63,8 +59,7 @@
6359
grep -ixq "^ansible_host=new-host" {{ __rhc_insights_conf }}
6460
changed_when: false
6561
- name: Change ansible host to an empty string (noop)
66-
include_role:
67-
name: linux-system-roles.rhc
62+
include_tasks: tasks/run_role_with_clear_facts.yml
6863
vars:
6964
rhc_insights:
7065
ansible_host: ""
@@ -77,8 +72,7 @@
7772
- name: Test ansible_host set to an absent value
7873
block:
7974
- name: Set ansible host to an absent value
80-
include_role:
81-
name: linux-system-roles.rhc
75+
include_tasks: tasks/run_role_with_clear_facts.yml
8276
vars:
8377
rhc_insights:
8478
ansible_host: {state: absent}
@@ -95,8 +89,7 @@
9589
- name: Test ansible_host is removed during insights unregistration
9690
block:
9791
- name: Set ansible_host in config file
98-
include_role:
99-
name: linux-system-roles.rhc
92+
include_tasks: tasks/run_role_with_clear_facts.yml
10093
vars:
10194
rhc_insights:
10295
ansible_host: unreg-test
@@ -106,8 +99,7 @@
10699
grep -ixq "^ansible_host=unreg-test" {{ __rhc_insights_conf }}
107100
changed_when: false
108101
- name: Unregister insights to remove ansible_host in config file
109-
include_role:
110-
name: linux-system-roles.rhc
102+
include_tasks: tasks/run_role_with_clear_facts.yml
111103
vars:
112104
rhc_insights:
113105
state: absent
@@ -122,7 +114,6 @@
122114

123115
always:
124116
- name: Unregister
125-
include_role:
126-
name: linux-system-roles.rhc
117+
include_tasks: tasks/run_role_with_clear_facts.yml
127118
vars:
128119
rhc_state: absent

tests/tests_insights_autoupdate.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Basic insights autoupdate test
44
hosts: all
5-
gather_facts: false
65
become: true
76

87
tasks:
@@ -12,10 +11,9 @@
1211
- name: Test autoupdate
1312
block:
1413
- name: Add autoupdate and register insights
15-
include_role:
16-
name: linux-system-roles.rhc
17-
public: true
14+
include_tasks: tasks/run_role_with_clear_facts.yml
1815
vars:
16+
__sr_public: true
1917
rhc_auth:
2018
login:
2119
username: "{{ lsr_rhc_test_data.reg_username }}"
@@ -39,16 +37,14 @@
3937
changed_when: false
4038

4139
- name: Disable autoupdate
42-
include_role:
43-
name: linux-system-roles.rhc
40+
include_tasks: tasks/run_role_with_clear_facts.yml
4441
vars:
4542
rhc_insights:
4643
autoupdate: false
4744
remediation: absent
4845

4946
- name: Disable autoupdate (noop)
50-
include_role:
51-
name: linux-system-roles.rhc
47+
include_tasks: tasks/run_role_with_clear_facts.yml
5248
vars:
5349
rhc_insights:
5450
autoupdate: false
@@ -61,7 +57,6 @@
6157

6258
always:
6359
- name: Unregister
64-
include_role:
65-
name: linux-system-roles.rhc
60+
include_tasks: tasks/run_role_with_clear_facts.yml
6661
vars:
6762
rhc_state: absent

tests/tests_insights_client_register.yml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Basic insights-client register/unregister
44
hosts: all
5-
gather_facts: false
65

76
tasks:
87
- name: Setup Insights
@@ -11,8 +10,7 @@
1110
- name: Test Insights registration
1211
block:
1312
- name: Register insights
14-
include_role:
15-
name: linux-system-roles.rhc
13+
include_tasks: tasks/run_role_with_clear_facts.yml
1614
vars:
1715
rhc_auth:
1816
login:
@@ -31,8 +29,7 @@
3129
rhc_proxy: "{{ rhc_external_proxy | default({}) }}"
3230

3331
- name: Register insights (noop)
34-
include_role:
35-
name: linux-system-roles.rhc
32+
include_tasks: tasks/run_role_with_clear_facts.yml
3633
vars:
3734
rhc_auth:
3835
login:
@@ -58,8 +55,7 @@
5855
test_insights_uuid_before: "{{ test_insights_uuid }}"
5956

6057
- name: Register (force)
61-
include_role:
62-
name: linux-system-roles.rhc
58+
include_tasks: tasks/run_role_with_clear_facts.yml
6359
vars:
6460
rhc_auth:
6561
login:
@@ -94,22 +90,19 @@
9490
or test_insights_uuid_after == test_rhsm_uuid
9591
9692
- name: Unregister insights
97-
include_role:
98-
name: linux-system-roles.rhc
93+
include_tasks: tasks/run_role_with_clear_facts.yml
9994
vars:
10095
rhc_insights:
10196
state: absent
10297

10398
- name: Unregister insights (noop)
104-
include_role:
105-
name: linux-system-roles.rhc
99+
include_tasks: tasks/run_role_with_clear_facts.yml
106100
vars:
107101
rhc_insights:
108102
state: absent
109103

110104
always:
111105
- name: Unregister
112-
include_role:
113-
name: linux-system-roles.rhc
106+
include_tasks: tasks/run_role_with_clear_facts.yml
114107
vars:
115108
rhc_state: absent

tests/tests_insights_display_name.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Insights display name test
44
hosts: all
5-
gather_facts: false
65
become: true
76

87
tasks:
@@ -12,10 +11,9 @@
1211
- name: Test display_name
1312
block:
1413
- name: Add display_name and register insights
15-
include_role:
16-
name: linux-system-roles.rhc
17-
public: true
14+
include_tasks: tasks/run_role_with_clear_facts.yml
1815
vars:
16+
__sr_public: true
1917
rhc_auth:
2018
login:
2119
username: "{{ lsr_rhc_test_data.reg_username }}"
@@ -41,8 +39,7 @@
4139
- name: Test display_name changed value after registration
4240
block:
4341
- name: Change display name to 'new-name'
44-
include_role:
45-
name: linux-system-roles.rhc
42+
include_tasks: tasks/run_role_with_clear_facts.yml
4643
vars:
4744
rhc_insights:
4845
display_name: new-name
@@ -52,8 +49,7 @@
5249
grep -ixq "^display_name=new-name" {{ __rhc_insights_conf }}
5350
changed_when: false
5451
- name: Change display name to a null value (noop)
55-
include_role:
56-
name: linux-system-roles.rhc
52+
include_tasks: tasks/run_role_with_clear_facts.yml
5753
vars:
5854
rhc_insights:
5955
display_name: null
@@ -63,8 +59,7 @@
6359
grep -ixq "^display_name=new-name" {{ __rhc_insights_conf }}
6460
changed_when: false
6561
- name: Change display name to an empty string (noop)
66-
include_role:
67-
name: linux-system-roles.rhc
62+
include_tasks: tasks/run_role_with_clear_facts.yml
6863
vars:
6964
rhc_insights:
7065
display_name: ""
@@ -77,8 +72,7 @@
7772
- name: Test display_name is removed during insights unregistration
7873
block:
7974
- name: Set display_name in config file
80-
include_role:
81-
name: linux-system-roles.rhc
75+
include_tasks: tasks/run_role_with_clear_facts.yml
8276
vars:
8377
rhc_insights:
8478
display_name: unreg-test
@@ -88,8 +82,7 @@
8882
grep -ixq "^display_name=unreg-test" {{ __rhc_insights_conf }}
8983
changed_when: false
9084
- name: Unregister insights to remove display_name in config file
91-
include_role:
92-
name: linux-system-roles.rhc
85+
include_tasks: tasks/run_role_with_clear_facts.yml
9386
vars:
9487
rhc_insights:
9588
state: absent
@@ -104,7 +97,6 @@
10497

10598
always:
10699
- name: Unregister
107-
include_role:
108-
name: linux-system-roles.rhc
100+
include_tasks: tasks/run_role_with_clear_facts.yml
109101
vars:
110102
rhc_state: absent

0 commit comments

Comments
 (0)