Skip to content

Commit bb5e9c4

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 89ac24c commit bb5e9c4

27 files changed

+212
-348
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.snapshot.
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.snapshot
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.snapshot
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: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
include_tasks: tasks/setup.yml
4040

4141
- name: Run the snapshot role to create snapshot LVs
42-
include_role:
43-
name: linux-system-roles.snapshot
42+
include_tasks: tasks/run_role_with_clear_facts.yml
4443
vars:
4544
snapshot_lvm_percent_space_required: 15
4645
snapshot_lvm_all_vgs: true
@@ -52,17 +51,15 @@
5251
that: snapshot_cmd["changed"]
5352

5453
- name: Verify the snapshot LVs are created
55-
include_role:
56-
name: linux-system-roles.snapshot
54+
include_tasks: tasks/run_role_with_clear_facts.yml
5755
vars:
5856
snapshot_lvm_all_vgs: true
5957
snapshot_lvm_snapset_name: snapset1
6058
snapshot_lvm_verify_only: true
6159
snapshot_lvm_action: check
6260

6361
- name: Run the snapshot role again to check idempotence
64-
include_role:
65-
name: linux-system-roles.snapshot
62+
include_tasks: tasks/run_role_with_clear_facts.yml
6663
vars:
6764
snapshot_lvm_percent_space_required: 15
6865
snapshot_lvm_all_vgs: true
@@ -74,8 +71,7 @@
7471
that: not snapshot_cmd["changed"]
7572

7673
- name: Verify again to check idempotence
77-
include_role:
78-
name: linux-system-roles.snapshot
74+
include_tasks: tasks/run_role_with_clear_facts.yml
7975
vars:
8076
snapshot_lvm_all_vgs: true
8177
snapshot_lvm_snapset_name: snapset1
@@ -87,8 +83,7 @@
8783
that: not snapshot_cmd["changed"]
8884

8985
- name: Run the snapshot role remove the snapshot LVs
90-
include_role:
91-
name: linux-system-roles.snapshot
86+
include_tasks: tasks/run_role_with_clear_facts.yml
9287
vars:
9388
snapshot_lvm_snapset_name: snapset1
9489
snapshot_lvm_action: remove
@@ -98,16 +93,14 @@
9893
that: snapshot_cmd["changed"]
9994

10095
- name: Use the snapshot_lvm_verify option to make sure remove is done
101-
include_role:
102-
name: linux-system-roles.snapshot
96+
include_tasks: tasks/run_role_with_clear_facts.yml
10397
vars:
10498
snapshot_lvm_snapset_name: snapset1
10599
snapshot_lvm_verify_only: true
106100
snapshot_lvm_action: remove
107101

108102
- name: Remove again to check idempotence
109-
include_role:
110-
name: linux-system-roles.snapshot
103+
include_tasks: tasks/run_role_with_clear_facts.yml
111104
vars:
112105
snapshot_lvm_snapset_name: snapset1
113106
snapshot_lvm_action: remove
@@ -117,8 +110,7 @@
117110
that: not snapshot_cmd["changed"]
118111

119112
- name: Verify remove again to check idempotence
120-
include_role:
121-
name: linux-system-roles.snapshot
113+
include_tasks: tasks/run_role_with_clear_facts.yml
122114
vars:
123115
snapshot_lvm_snapset_name: snapset1
124116
snapshot_lvm_verify_only: true

tests/tests_basic_bootable.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
include_tasks: tasks/setup.yml
4040

4141
- name: Run the snapshot role to create snapshot LVs
42-
include_role:
43-
name: linux-system-roles.snapshot
42+
include_tasks: tasks/run_role_with_clear_facts.yml
4443
vars:
4544
snapshot_lvm_percent_space_required: 15
4645
snapshot_lvm_all_vgs: true
@@ -53,17 +52,15 @@
5352
that: snapshot_cmd["changed"]
5453

5554
- name: Verify the snapshot LVs are created
56-
include_role:
57-
name: linux-system-roles.snapshot
55+
include_tasks: tasks/run_role_with_clear_facts.yml
5856
vars:
5957
snapshot_lvm_all_vgs: true
6058
snapshot_lvm_snapset_name: snapset1
6159
snapshot_lvm_verify_only: true
6260
snapshot_lvm_action: check
6361

6462
- name: Run the snapshot role again to check idempotence
65-
include_role:
66-
name: linux-system-roles.snapshot
63+
include_tasks: tasks/run_role_with_clear_facts.yml
6764
vars:
6865
snapshot_lvm_percent_space_required: 15
6966
snapshot_lvm_all_vgs: true
@@ -75,8 +72,7 @@
7572
that: not snapshot_cmd["changed"]
7673

7774
- name: Verify again to check idempotence
78-
include_role:
79-
name: linux-system-roles.snapshot
75+
include_tasks: tasks/run_role_with_clear_facts.yml
8076
vars:
8177
snapshot_lvm_all_vgs: true
8278
snapshot_lvm_snapset_name: snapset1
@@ -88,8 +84,7 @@
8884
that: not snapshot_cmd["changed"]
8985

9086
- name: Run the snapshot role remove the snapshot LVs
91-
include_role:
92-
name: linux-system-roles.snapshot
87+
include_tasks: tasks/run_role_with_clear_facts.yml
9388
vars:
9489
snapshot_lvm_snapset_name: snapset1
9590
snapshot_lvm_action: remove
@@ -99,16 +94,14 @@
9994
that: snapshot_cmd["changed"]
10095

10196
- name: Use the snapshot_lvm_verify option to make sure remove is done
102-
include_role:
103-
name: linux-system-roles.snapshot
97+
include_tasks: tasks/run_role_with_clear_facts.yml
10498
vars:
10599
snapshot_lvm_snapset_name: snapset1
106100
snapshot_lvm_verify_only: true
107101
snapshot_lvm_action: remove
108102

109103
- name: Remove again to check idempotence
110-
include_role:
111-
name: linux-system-roles.snapshot
104+
include_tasks: tasks/run_role_with_clear_facts.yml
112105
vars:
113106
snapshot_lvm_snapset_name: snapset1
114107
snapshot_lvm_action: remove
@@ -118,8 +111,7 @@
118111
that: not snapshot_cmd["changed"]
119112

120113
- name: Verify remove again to check idempotence
121-
include_role:
122-
name: linux-system-roles.snapshot
114+
include_tasks: tasks/run_role_with_clear_facts.yml
123115
vars:
124116
snapshot_lvm_snapset_name: snapset1
125117
snapshot_lvm_verify_only: true

tests/tests_default.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
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
vars:
76
# only use vgs matching this pattern
87
snapshot_lvm_vg_include: "^test_"
98
snapshot_lvm_action: check
109
snapshot_lvm_percent_space_required: 2
1110
snapshot_lvm_snapset_name: snapset1
1211
snapshot_lvm_all_vgs: true
13-
roles:
14-
- linux-system-roles.snapshot
12+
tasks:
13+
- name: Run the role
14+
include_tasks: tasks/run_role_with_clear_facts.yml

tests/tests_extend_basic.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,23 @@
3636
include_tasks: tasks/setup.yml
3737

3838
- name: Run the snapshot role to create snapshot LVs
39-
include_role:
40-
name: linux-system-roles.snapshot
39+
include_tasks: tasks/run_role_with_clear_facts.yml
4140
vars:
4241
snapshot_lvm_percent_space_required: 50
4342
snapshot_lvm_all_vgs: true
4443
snapshot_lvm_snapset_name: snapset1
4544
snapshot_lvm_action: snapshot
4645

4746
- name: Verify the snapshot LVs are created
48-
include_role:
49-
name: linux-system-roles.snapshot
47+
include_tasks: tasks/run_role_with_clear_facts.yml
5048
vars:
5149
snapshot_lvm_all_vgs: true
5250
snapshot_lvm_snapset_name: snapset1
5351
snapshot_lvm_verify_only: true
5452
snapshot_lvm_action: check
5553

5654
- name: Extend the snapshots size to 60 percent of the source
57-
include_role:
58-
name: linux-system-roles.snapshot
55+
include_tasks: tasks/run_role_with_clear_facts.yml
5956
vars:
6057
snapshot_lvm_percent_space_required: 60
6158
snapshot_lvm_all_vgs: true
@@ -67,8 +64,7 @@
6764
that: snapshot_cmd["changed"]
6865

6966
- name: Use the snapshot_lvm_verify option to make sure extend is done
70-
include_role:
71-
name: linux-system-roles.snapshot
67+
include_tasks: tasks/run_role_with_clear_facts.yml
7268
vars:
7369
snapshot_lvm_percent_space_required: 60
7470
snapshot_lvm_all_vgs: true
@@ -85,8 +81,7 @@
8581
that: not snapshot_cmd["changed"]
8682

8783
- name: Extend again to check idempotence
88-
include_role:
89-
name: linux-system-roles.snapshot
84+
include_tasks: tasks/run_role_with_clear_facts.yml
9085
vars:
9186
snapshot_lvm_percent_space_required: 60
9287
snapshot_lvm_all_vgs: true
@@ -98,8 +93,7 @@
9893
that: not snapshot_cmd["changed"]
9994

10095
- name: Verify extend again to check idempotence
101-
include_role:
102-
name: linux-system-roles.snapshot
96+
include_tasks: tasks/run_role_with_clear_facts.yml
10397
vars:
10498
snapshot_lvm_percent_space_required: 60
10599
snapshot_lvm_all_vgs: true

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
vars:
65
# only use vgs matching this pattern
76
snapshot_lvm_vg_include: "^test_"

tests/tests_list.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,23 @@
3939
include_tasks: tasks/setup.yml
4040

4141
- name: Run the snapshot role to create snapshot LVs
42-
include_role:
43-
name: linux-system-roles.snapshot
42+
include_tasks: tasks/run_role_with_clear_facts.yml
4443
vars:
4544
snapshot_lvm_percent_space_required: 15
4645
snapshot_lvm_all_vgs: true
4746
snapshot_lvm_snapset_name: snapset1
4847
snapshot_lvm_action: snapshot
4948

5049
- name: Verify the snapshot LVs are created
51-
include_role:
52-
name: linux-system-roles.snapshot
50+
include_tasks: tasks/run_role_with_clear_facts.yml
5351
vars:
5452
snapshot_lvm_all_vgs: true
5553
snapshot_lvm_snapset_name: snapset1
5654
snapshot_lvm_verify_only: true
5755
snapshot_lvm_action: check
5856

5957
- name: List
60-
include_role:
61-
name: linux-system-roles.snapshot
58+
include_tasks: tasks/run_role_with_clear_facts.yml
6259
vars:
6360
snapshot_lvm_snapset_name: snapset1
6461
snapshot_lvm_action: list
@@ -68,15 +65,13 @@
6865
that: not snapshot_cmd["changed"]
6966

7067
- name: Run the snapshot role remove the snapshot LVs
71-
include_role:
72-
name: linux-system-roles.snapshot
68+
include_tasks: tasks/run_role_with_clear_facts.yml
7369
vars:
7470
snapshot_lvm_snapset_name: snapset1
7571
snapshot_lvm_action: remove
7672

7773
- name: Use the snapshot_lvm_verify option to make sure remove is done
78-
include_role:
79-
name: linux-system-roles.snapshot
74+
include_tasks: tasks/run_role_with_clear_facts.yml
8075
vars:
8176
snapshot_lvm_snapset_name: snapset1
8277
snapshot_lvm_verify_only: true

0 commit comments

Comments
 (0)