Skip to content

Commit 9321b60

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. Add "architecture" to __hpc_required_facts and make the list in alphabetical order. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent 53391aa commit 9321b60

7 files changed

Lines changed: 44 additions & 13 deletions

File tree

tests/playbooks/verify_custom_storage.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Verify if role configures a custom storage properly
44
hosts: localhost
5-
gather_facts: false
65
vars:
76
hpc_install_prefix: /opt
87
__hpc_hpcx_info:
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# DO NOT EDIT THIS FILE - managed by linux-system-roles/.github
3+
# Task file: clear_facts, run linux-system-roles.hpc.
4+
# Include this with include_tasks or import_tasks
5+
# Input:
6+
# - __sr_tasks_from: tasks_from to run - same as tasks_from in include_role
7+
# - __sr_public: export private vars from role - same as public in include_role
8+
# - __sr_failed_when: set to false to ignore role errors - same as failed_when in include_role
9+
- name: Clear facts
10+
meta: clear_facts
11+
12+
# note that you can use failed_when with import_role but not with include_role
13+
# so this simulates the __sr_failed_when false case
14+
# Q: Why do we need a separate task to run the role normally? Why not just
15+
# run the role in the block and rethrow the error in the rescue block?
16+
# A: Because you cannot rethrow the error in exactly the same way as the role does.
17+
# It might be possible to exactly reconstruct ansible_failed_result but it's not worth the effort.
18+
- name: Run the role with __sr_failed_when false
19+
when:
20+
- __sr_failed_when is defined
21+
- not __sr_failed_when
22+
block:
23+
- name: Run the role
24+
include_role:
25+
name: linux-system-roles.hpc
26+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
27+
public: "{{ __sr_public | default(false) }}"
28+
rescue:
29+
- name: Ignore the failure when __sr_failed_when is false
30+
debug:
31+
msg: Ignoring failure when __sr_failed_when is false
32+
33+
- name: Run the role normally
34+
include_role:
35+
name: linux-system-roles.hpc
36+
tasks_from: "{{ __sr_tasks_from | default('main') }}"
37+
public: "{{ __sr_public | default(false) }}"
38+
when: __sr_failed_when | d(true)

tests/tests_azure.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Verify if role configures a custom storage properly
44
hosts: all
5-
gather_facts: true
65
vars:
76
hpc_manage_firewall: true
87
hpc_reboot_ok: true
@@ -13,5 +12,4 @@
1312
when: ansible_facts['system_vendor'] != "Microsoft Corporation"
1413

1514
- name: Run the role
16-
include_role:
17-
name: linux-system-roles.hpc
15+
include_tasks: tasks/run_role_with_clear_facts.yml

tests/tests_default.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Ensure that the role runs with default parameters
44
hosts: all
5-
gather_facts: false
65
# Do not do anything in this test, otherwise fails with not enough storage
76
vars:
87
hpc_manage_storage: false
@@ -31,8 +30,7 @@
3130
- name: Run test in a block to clean up in always
3231
block:
3332
- name: Run the role
34-
include_role:
35-
name: linux-system-roles.hpc
33+
include_tasks: tasks/run_role_with_clear_facts.yml
3634
always:
3735
- name: Cleanup
3836
include_tasks: tasks/cleanup.yml

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
tasks:
65
- name: Skip unsupported architectures
76
include_tasks: tasks/skip_unsupported_archs.yml

tests/tests_skip_toolkit.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
---
33
- name: Verify if role configures a custom storage properly
44
hosts: all
5-
gather_facts: true
65
vars:
76
hpc_rootvg_name: rootvg
87
hpc_rootlv_name: rootlv
@@ -98,8 +97,7 @@
9897
mount_point: "{{ hpc_usrlv_mount }}"
9998

10099
- name: Run the role
101-
include_role:
102-
name: linux-system-roles.hpc
100+
include_tasks: tasks/run_role_with_clear_facts.yml
103101

104102
# Both should be 2G
105103
# rootlv because it was set to 2G with the storage role

vars/main.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
---
33
# ansible_facts required by the role
44
__hpc_required_facts:
5+
- architecture
6+
# required for ansible_facts["system_vendor"]
7+
- devices
58
- distribution
69
- distribution_major_version
710
- distribution_version
811
- os_family
9-
# required for ansible_facts["system_vendor"]
10-
- devices
1112
# required for ansible_facts["processor_nproc"]
1213
- processor
1314
# the subsets of ansible_facts that need to be gathered in case any of the

0 commit comments

Comments
 (0)