Skip to content

Commit 3766e9e

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 <[email protected]>
1 parent a9e9c86 commit 3766e9e

54 files changed

Lines changed: 394 additions & 692 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tasks/main-blivet.yml

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,10 @@
7373
extra_pkgs:
7474
- kpartx
7575

76-
- name: Get service facts
77-
service_facts:
78-
when: storage_skip_checks is not defined or
79-
not "service_facts" in storage_skip_checks
80-
81-
# rejectattr required because the fix to service_facts is on Ansible > 2.12 only
82-
# https://github.com/ansible/ansible/pull/75326
83-
- name: Set storage_cryptsetup_services
84-
set_fact:
76+
- name: Manage storage devices and check for errors
77+
vars:
78+
# rejectattr required because the fix to service_facts is on Ansible > 2.12 only
79+
# https://github.com/ansible/ansible/pull/75326
8580
storage_cryptsetup_services: "{{
8681
ansible_facts.services.values() |
8782
selectattr('name', 'defined') |
@@ -91,10 +86,15 @@
9186
rejectattr('status', 'match', '^failed$') |
9287
map(attribute='name') |
9388
select('match', '^systemd-cryptsetup@') |
94-
list }}"
95-
96-
- name: Manage storage devices and check for errors
89+
list
90+
if 'services' in ansible_facts else [] }}"
9791
block:
92+
- name: Get service facts
93+
service_facts:
94+
when:
95+
- not "services" in ansible_facts
96+
- not "cryptsetup_services" in storage_skip_checks | d([])
97+
9898
- name: Mask the systemd cryptsetup services
9999
systemd:
100100
name: "{{ item }}"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
# Task file: save facts, clear_facts, run linux-system-roles.storage, then restore facts.
3+
# Include this with include_tasks or import_tasks; ensure tests/library is in module search path.
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+
# Output:
9+
# - ansible_facts: merged saved ansible_facts with ansible_facts modified by the role, if any
10+
- name: Clear facts
11+
meta: clear_facts
12+
13+
# note that you can use failed_when with import_role but not with include_role
14+
# so this simulates the __sr_failed_when false case
15+
# Q: Why do we need a separate task to run the role normally? Why not just
16+
# run the role in the block and rethrow the error in the rescue block?
17+
# A: Because you cannot rethrow the error in exactly the same way as the role does.
18+
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
19+
- name: Run the role with __sr_failed_when false
20+
when:
21+
- __sr_failed_when is defined
22+
- not __sr_failed_when
23+
block:
24+
- name: Run the role
25+
include_role:
26+
name: linux-system-roles.storage
27+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
28+
public: "{{ __sr_public | default(false) }}"
29+
rescue:
30+
- name: Ignore the failure when __sr_failed_when is false
31+
debug:
32+
msg: Ignoring failure when __sr_failed_when is false
33+
34+
- name: Run the role normally
35+
include_role:
36+
name: linux-system-roles.storage
37+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
38+
public: "{{ __sr_public | default(false) }}"
39+
when: __sr_failed_when | d(true)

tests/tests_change_disk_fs.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
else 'ext4' }}"
1313
tasks:
1414
- name: Run the role
15-
include_role:
16-
name: linux-system-roles.storage
15+
include_tasks: tasks/run_role_with_clear_facts.yml
1716

1817
- name: Mark tasks to be skipped
1918
set_fact:
@@ -22,7 +21,6 @@
2221
- "{{ (lookup('env',
2322
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
2423
ternary('packages_installed', '') }}"
25-
- service_facts
2624

2725
- name: Get unused disks
2826
include_tasks: get_unused_disk.yml
@@ -31,8 +29,7 @@
3129
max_return: 1
3230

3331
- name: Create a disk device with the default file system type
34-
include_role:
35-
name: linux-system-roles.storage
32+
include_tasks: tasks/run_role_with_clear_facts.yml
3633
vars:
3734
storage_volumes:
3835
- name: test1
@@ -44,8 +41,7 @@
4441
include_tasks: verify-role-results.yml
4542

4643
- name: Change the disk device file system type to {{ fs_type_after }}
47-
include_role:
48-
name: linux-system-roles.storage
44+
include_tasks: tasks/run_role_with_clear_facts.yml
4945
vars:
5046
storage_volumes:
5147
- name: test1
@@ -58,8 +54,7 @@
5854
include_tasks: verify-role-results.yml
5955

6056
- name: Repeat the previous invocation to verify idempotence
61-
include_role:
62-
name: linux-system-roles.storage
57+
include_tasks: tasks/run_role_with_clear_facts.yml
6358
vars:
6459
storage_volumes:
6560
- name: test1
@@ -72,8 +67,7 @@
7267
include_tasks: verify-role-results.yml
7368

7469
- name: Clean up
75-
include_role:
76-
name: linux-system-roles.storage
70+
include_tasks: tasks/run_role_with_clear_facts.yml
7771
vars:
7872
storage_volumes:
7973
- name: test1

tests/tests_change_disk_mount.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
changed_when: false
2121

2222
- name: Run the role
23-
include_role:
24-
name: linux-system-roles.storage
23+
include_tasks: tasks/run_role_with_clear_facts.yml
2524

2625
- name: Mark tasks to be skipped
2726
set_fact:
@@ -30,7 +29,6 @@
3029
- "{{ (lookup('env',
3130
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
3231
ternary('packages_installed', '') }}"
33-
- service_facts
3432

3533
- name: Get unused disks
3634
include_tasks: get_unused_disk.yml
@@ -39,8 +37,7 @@
3937
max_return: 1
4038

4139
- name: Create a disk device mounted at "{{ mount_location_before }}"
42-
include_role:
43-
name: linux-system-roles.storage
40+
include_tasks: tasks/run_role_with_clear_facts.yml
4441
vars:
4542
storage_volumes:
4643
- name: test1
@@ -52,8 +49,7 @@
5249
include_tasks: verify-role-results.yml
5350

5451
- name: Change the disk device mount location to {{ mount_location_after }}
55-
include_role:
56-
name: linux-system-roles.storage
52+
include_tasks: tasks/run_role_with_clear_facts.yml
5753
vars:
5854
storage_volumes:
5955
- name: test1
@@ -65,8 +61,7 @@
6561
include_tasks: verify-role-results.yml
6662

6763
- name: Repeat the previous invocation to verify idempotence
68-
include_role:
69-
name: linux-system-roles.storage
64+
include_tasks: tasks/run_role_with_clear_facts.yml
7065
vars:
7166
storage_volumes:
7267
- name: test1
@@ -78,8 +73,7 @@
7873
include_tasks: verify-role-results.yml
7974

8075
- name: Clean up
81-
include_role:
82-
name: linux-system-roles.storage
76+
include_tasks: tasks/run_role_with_clear_facts.yml
8377
vars:
8478
storage_volumes:
8579
- name: test1

tests/tests_change_fs.yml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414

1515
tasks:
1616
- name: Run the role
17-
include_role:
18-
name: linux-system-roles.storage
17+
include_tasks: tasks/run_role_with_clear_facts.yml
1918

2019
- name: Mark tasks to be skipped
2120
set_fact:
@@ -24,7 +23,6 @@
2423
- "{{ (lookup('env',
2524
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
2625
ternary('packages_installed', '') }}"
27-
- service_facts
2826

2927
- name: Get unused disks
3028
include_tasks: get_unused_disk.yml
@@ -33,8 +31,7 @@
3331
max_return: 1
3432

3533
- name: Create a LVM logical volume with default fs_type
36-
include_role:
37-
name: linux-system-roles.storage
34+
include_tasks: tasks/run_role_with_clear_facts.yml
3835
vars:
3936
storage_pools:
4037
- name: foo
@@ -48,8 +45,7 @@
4845
include_tasks: verify-role-results.yml
4946

5047
- name: Change the file system signature on the logical volume created above
51-
include_role:
52-
name: linux-system-roles.storage
48+
include_tasks: tasks/run_role_with_clear_facts.yml
5349
vars:
5450
storage_pools:
5551
- name: foo
@@ -64,8 +60,7 @@
6460
include_tasks: verify-role-results.yml
6561

6662
- name: Re-run the role on the same volume without specifying fs_type
67-
include_role:
68-
name: linux-system-roles.storage
63+
include_tasks: tasks/run_role_with_clear_facts.yml
6964
vars:
7065
storage_pools:
7166
- name: foo
@@ -86,8 +81,7 @@
8681
include_tasks: verify-role-results.yml
8782

8883
- name: Repeat the previous invocation to verify idempotence
89-
include_role:
90-
name: linux-system-roles.storage
84+
include_tasks: tasks/run_role_with_clear_facts.yml
9185
vars:
9286
storage_pools:
9387
- name: foo
@@ -102,8 +96,7 @@
10296
include_tasks: verify-role-results.yml
10397

10498
- name: Remove the FS
105-
include_role:
106-
name: linux-system-roles.storage
99+
include_tasks: tasks/run_role_with_clear_facts.yml
107100
vars:
108101
storage_pools:
109102
- name: foo
@@ -116,10 +109,8 @@
116109
- name: Verify role results - 5
117110
include_tasks: verify-role-results.yml
118111

119-
120112
- name: Clean up
121-
include_role:
122-
name: linux-system-roles.storage
113+
include_tasks: tasks/run_role_with_clear_facts.yml
123114
vars:
124115
storage_pools:
125116
- name: foo

tests/tests_change_fs_use_partitions.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
- tests::lvm
1616
tasks:
1717
- name: Run the role
18-
include_role:
19-
name: linux-system-roles.storage
18+
include_tasks: tasks/run_role_with_clear_facts.yml
2019

2120
- name: Mark tasks to be skipped
2221
set_fact:
@@ -25,7 +24,6 @@
2524
- "{{ (lookup('env',
2625
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
2726
ternary('packages_installed', '') }}"
28-
- service_facts
2927

3028
- name: Get unused disks
3129
include_tasks: get_unused_disk.yml
@@ -34,8 +32,7 @@
3432
max_return: 1
3533

3634
- name: Create an LVM partition with the default file system type
37-
include_role:
38-
name: linux-system-roles.storage
35+
include_tasks: tasks/run_role_with_clear_facts.yml
3936
vars:
4037
storage_pools:
4138
- name: bar
@@ -49,8 +46,7 @@
4946
include_tasks: verify-role-results.yml
5047

5148
- name: Change the LVM partition file system type to {{ fs_type_after }}
52-
include_role:
53-
name: linux-system-roles.storage
49+
include_tasks: tasks/run_role_with_clear_facts.yml
5450
vars:
5551
storage_pools:
5652
- name: bar
@@ -65,8 +61,7 @@
6561
include_tasks: verify-role-results.yml
6662

6763
- name: Repeat the previous invocation to verify idempotence
68-
include_role:
69-
name: linux-system-roles.storage
64+
include_tasks: tasks/run_role_with_clear_facts.yml
7065
vars:
7166
storage_pools:
7267
- name: bar
@@ -81,8 +76,7 @@
8176
include_tasks: verify-role-results.yml
8277

8378
- name: Clean up
84-
include_role:
85-
name: linux-system-roles.storage
79+
include_tasks: tasks/run_role_with_clear_facts.yml
8680
vars:
8781
storage_pools:
8882
- name: bar

tests/tests_change_mount.yml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
tasks:
1313
- name: Run the role
14-
include_role:
15-
name: linux-system-roles.storage
14+
include_tasks: tasks/run_role_with_clear_facts.yml
1615

1716
- name: Mark tasks to be skipped
1817
set_fact:
@@ -21,7 +20,6 @@
2120
- "{{ (lookup('env',
2221
'SYSTEM_ROLES_REMOVE_CLOUD_INIT') in ['', 'false']) |
2322
ternary('packages_installed', '') }}"
24-
- service_facts
2523

2624
- name: Get unused disks
2725
include_tasks: get_unused_disk.yml
@@ -30,8 +28,7 @@
3028
max_return: 1
3129

3230
- name: Create a LVM logical volume mounted at {{ mount_location_before }}
33-
include_role:
34-
name: linux-system-roles.storage
31+
include_tasks: tasks/run_role_with_clear_facts.yml
3532
vars:
3633
storage_pools:
3734
- name: foo
@@ -45,8 +42,7 @@
4542
include_tasks: verify-role-results.yml
4643

4744
- name: Change the mount location to {{ mount_location_after }}
48-
include_role:
49-
name: linux-system-roles.storage
45+
include_tasks: tasks/run_role_with_clear_facts.yml
5046
vars:
5147
storage_pools:
5248
- name: foo
@@ -60,8 +56,7 @@
6056
include_tasks: verify-role-results.yml
6157

6258
- name: Repeat the previous invocation to verify idempotence
63-
include_role:
64-
name: linux-system-roles.storage
59+
include_tasks: tasks/run_role_with_clear_facts.yml
6560
vars:
6661
storage_pools:
6762
- name: foo
@@ -75,8 +70,7 @@
7570
include_tasks: verify-role-results.yml
7671

7772
- name: Clean up
78-
include_role:
79-
name: linux-system-roles.storage
73+
include_tasks: tasks/run_role_with_clear_facts.yml
8074
vars:
8175
storage_pools:
8276
- name: foo

0 commit comments

Comments
 (0)