Skip to content

Commit b3b6ecc

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 6e72eff commit b3b6ecc

21 files changed

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

tests/tests_bz1855539.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
- name: Run test
2222
block:
2323
- name: Include role
24-
include_role:
25-
name: linux-system-roles.metrics
26-
public: true
24+
include_tasks: tasks/run_role_with_clear_facts.yml
25+
vars:
26+
__sr_public: true
2727

2828
- name: Flush handlers
2929
meta: flush_handlers

tests/tests_bz1855544.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
- name: Run test
2323
block:
2424
- name: Run the role
25-
include_role:
26-
name: linux-system-roles.metrics
27-
public: true
25+
include_tasks: tasks/run_role_with_clear_facts.yml
26+
vars:
27+
__sr_public: true
2828

2929
- name: Flush handlers
3030
meta: flush_handlers

tests/tests_default.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
- name: Run test
1212
block:
1313
- name: Include role
14-
include_role:
15-
name: linux-system-roles.metrics
16-
public: true
14+
include_tasks: tasks/run_role_with_clear_facts.yml
15+
vars:
16+
__sr_public: true
1717

1818
- name: Flush handlers
1919
meta: flush_handlers

tests/tests_verify_auth.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
- name: Run test
2525
block:
2626
- name: Run role
27-
include_role:
28-
name: linux-system-roles.metrics
29-
public: true
27+
include_tasks: tasks/run_role_with_clear_facts.yml
28+
vars:
29+
__sr_public: true
3030

3131
- name: Flush handlers
3232
meta: flush_handlers

tests/tests_verify_basic.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
- name: Run test
1616
block:
1717
- name: Run the role
18-
include_role:
19-
name: linux-system-roles.metrics
20-
public: true
18+
include_tasks: tasks/run_role_with_clear_facts.yml
19+
vars:
20+
__sr_public: true
2121

2222
- name: Flush handlers
2323
meta: flush_handlers

tests/tests_verify_bpftrace.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
- name: Run test
2626
block:
2727
- name: Run the role
28-
include_role:
29-
name: linux-system-roles.metrics
30-
public: true
28+
include_tasks: tasks/run_role_with_clear_facts.yml
29+
vars:
30+
__sr_public: true
3131

3232
- name: Flush handlers
3333
meta: flush_handlers

tests/tests_verify_from_elasticsearch.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
- name: Run test
2020
block:
2121
- name: Run the metrics role to configure Elasticsearch
22-
include_role:
23-
name: linux-system-roles.metrics
24-
public: true
22+
include_tasks: tasks/run_role_with_clear_facts.yml
23+
vars:
24+
__sr_public: true
2525

2626
- name: Flush handlers
2727
meta: flush_handlers

tests/tests_verify_from_spark.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
- name: Run test
2020
block:
2121
- name: Run the metrics role to configure Spark
22-
include_role:
23-
name: linux-system-roles.metrics
24-
public: true
22+
include_tasks: tasks/run_role_with_clear_facts.yml
23+
vars:
24+
__sr_public: true
2525

2626
- name: Flush handlers
2727
meta: flush_handlers

tests/tests_verify_fullstack.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
- name: Run test
2525
block:
2626
- name: Run the role
27-
include_role:
28-
name: linux-system-roles.metrics
29-
public: true
27+
include_tasks: tasks/run_role_with_clear_facts.yml
3028
when: not __bootc_validation | d(false)
29+
vars:
30+
__sr_public: true
3131

3232
- name: Create QEMU deployment during bootc end-to-end test
3333
delegate_to: localhost

0 commit comments

Comments
 (0)