Skip to content

Commit cc3da65

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 ded5545 commit cc3da65

119 files changed

Lines changed: 946 additions & 450 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.

‎tests/ensure_provider_tests.py‎

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#!/usr/bin/env python3
22
# SPDX-License-Identifier: BSD-3-Clause
33
"""Check that there is a playbook to run all role tests with both providers"""
4+
45
# vim: fileencoding=utf8
56

67
import difflib
78
import glob
89
import os
910
import sys
1011

11-
1212
GET_NM_VERSION = """
1313
- name: Install NetworkManager and get NetworkManager version
1414
when:
15-
- ansible_facts['distribution_major_version'] != '6'
15+
- __network_distro_major_version != '6'
1616
tags:
1717
- always
1818
block:
@@ -30,8 +30,7 @@
3030
ansible_facts.packages['NetworkManager'][0]['version'] }}"
3131
"""
3232

33-
MINIMUM_NM_VERSION_CHECK = """
34-
- networkmanager_version is version({minimum_nm_version}, '>=')
33+
MINIMUM_NM_VERSION_CHECK = """ - networkmanager_version is version({minimum_nm_version}, '>=')
3534
"""
3635

3736
EXTRA_RUN_CONDITION_PREFIX = " - "
@@ -51,31 +50,73 @@
5150
network_provider: nm
5251
tags:
5352
- always
53+
- name: Include distro variables
54+
include_vars: vars/rh_distros_vars.yml
55+
- name: Set platform facts
56+
set_fact:
57+
__network_distro_major_version: "{{{{ ansible_facts['distribution_major_version'] }}}}"
58+
__network_is_rhel: "{{{{ ansible_facts['distribution'] == 'RedHat' }}}}"
59+
__network_is_fedora: "{{{{ ansible_facts['distribution'] == 'Fedora' }}}}"
60+
__network_is_centos: "{{{{ ansible_facts['distribution'] == 'CentOS' }}}}"
61+
__network_is_os_family_rhel: "{{{{ ansible_facts['os_family'] == 'RedHat' }}}}"
5462
{get_nm_version}
5563
5664
# The test requires or should run with NetworkManager, therefore it cannot run
5765
# on RHEL/CentOS 6
5866
{comment}- name: Import the playbook '{test_playbook}'
5967
import_playbook: {test_playbook}
6068
when:
61-
- ansible_facts['distribution_major_version'] != '6'
69+
- __network_distro_major_version != '6'
6270
{minimum_nm_version_check}{extra_run_condition}"""
6371

72+
73+
RUN_PLAYBOOK_WITH_INITSCRIPTS = """# SPDX-License-Identifier: BSD-3-Clause
74+
# This file was generated by ensure_provider_tests.py
75+
---
76+
# yamllint disable rule:line-length
77+
- name: Run playbook '{test_playbook}' with initscripts as provider
78+
hosts: all
79+
tasks:
80+
- name: Include the task 'el_repo_setup.yml'
81+
include_tasks: tasks/el_repo_setup.yml
82+
- name: Set network provider to 'initscripts'
83+
set_fact:
84+
network_provider: initscripts
85+
tags:
86+
- always
87+
- name: Include distro variables
88+
include_vars: vars/rh_distros_vars.yml
89+
- name: Set platform facts
90+
set_fact:
91+
__network_distro_major_version: "{{{{ ansible_facts['distribution_major_version'] }}}}"
92+
__network_is_rhel: "{{{{ ansible_facts['distribution'] == 'RedHat' }}}}"
93+
__network_is_fedora: "{{{{ ansible_facts['distribution'] == 'Fedora' }}}}"
94+
__network_is_centos: "{{{{ ansible_facts['distribution'] == 'CentOS' }}}}"
95+
__network_is_os_family_rhel: "{{{{ ansible_facts['os_family'] == 'RedHat' }}}}"
96+
__is_rh_distro: "{{{{ __network_is_rh_distro }}}}"
97+
- name: Import the playbook '{test_playbook}'
98+
import_playbook: {test_playbook}
99+
when:
100+
- __is_rh_distro
101+
- __network_distro_major_version | int < 9
102+
"""
103+
104+
64105
MINIMUM_VERSION = "minimum_version"
65106
EXTRA_RUN_CONDITION = "extra_run_condition"
66107
NM_ONLY_TESTS = {
67108
"playbooks/tests_802_1x_updated.yml": {
68109
EXTRA_RUN_CONDITION: (
69-
"(ansible_facts['distribution'] != 'RedHat' and\n"
70-
" ansible_facts['distribution_major_version'] | int > 7) or\n"
71-
" ansible_facts['distribution_major_version'] | int == 8"
110+
"(not __network_is_rhel and\n"
111+
" __network_distro_major_version | int > 7) or\n"
112+
" __network_distro_major_version | int == 8"
72113
),
73114
},
74115
"playbooks/tests_802_1x.yml": {
75116
EXTRA_RUN_CONDITION: (
76-
"(ansible_facts['distribution'] != 'RedHat' and\n"
77-
" ansible_facts['distribution_major_version'] | int > 7) or\n"
78-
" ansible_facts['distribution_major_version'] | int == 8"
117+
"(not __network_is_rhel and\n"
118+
" __network_distro_major_version | int > 7) or\n"
119+
" __network_distro_major_version | int == 8"
79120
),
80121
},
81122
"playbooks/tests_ignore_auto_dns.yml": {},
@@ -92,20 +133,20 @@
92133
},
93134
"playbooks/tests_provider.yml": {
94135
MINIMUM_VERSION: "'1.20.0'",
95-
"comment": "# NetworKmanager 1.20.0 added support for forgetting profiles",
136+
"comment": "# NetworkManager 1.20.0 added support for forgetting profiles",
96137
EXTRA_RUN_CONDITION: (
97-
"(ansible_facts['distribution'] == 'Fedora'\n"
98-
" and ansible_facts['distribution_major_version'] | int < 41)\n"
99-
" or ansible_facts['distribution'] not in ['RedHat', 'CentOS', 'Fedora']\n"
100-
" or ansible_facts['distribution_major_version'] | int < 9"
138+
"(__network_is_fedora and\n"
139+
" __network_distro_major_version | int < 41)\n"
140+
" or not __network_is_os_family_rhel\n"
141+
" or __network_distro_major_version | int < 9"
101142
),
102143
},
103144
"playbooks/tests_eth_pci_address_match.yml": {
104145
MINIMUM_VERSION: "'1.26.0'",
105146
"comment": "# NetworkManager 1.26.0 added support for match.path setting",
106147
},
107148
"playbooks/tests_network_state.yml": {
108-
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] | int > 7",
149+
EXTRA_RUN_CONDITION: "__network_distro_major_version | int > 7",
109150
},
110151
"playbooks/tests_reapply.yml": {},
111152
"playbooks/tests_route_table.yml": {},
@@ -117,30 +158,30 @@
117158
"playbooks/tests_routing_rules.yml": {},
118159
# teaming support dropped in EL10
119160
"playbooks/tests_team.yml": {
120-
EXTRA_RUN_CONDITION: "ansible_facts['distribution'] not in ['RedHat', 'CentOS'] or\n ansible_facts['distribution_major_version'] | int < 10",
161+
EXTRA_RUN_CONDITION: "not __network_is_rh_distro or\n __network_distro_major_version | int < 10",
121162
},
122163
"playbooks/tests_team_plugin_installation.yml": {
123-
EXTRA_RUN_CONDITION: "ansible_facts['distribution'] not in ['RedHat', 'CentOS'] or\n ansible_facts['distribution_major_version'] | int < 10",
164+
EXTRA_RUN_CONDITION: "not __network_is_rh_distro or\n __network_distro_major_version | int < 10",
124165
},
125166
# mac80211_hwsim (used for tests_wireless) only seems to be available
126167
# and working on RHEL/CentOS 7
127168
"playbooks/tests_wireless.yml": {
128-
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] == '7'",
169+
EXTRA_RUN_CONDITION: "__network_distro_major_version == '7'",
129170
},
130171
"playbooks/tests_wireless_and_network_restart.yml": {},
131172
"playbooks/tests_wireless_plugin_installation.yml": {},
132173
"playbooks/tests_wireless_wpa3_owe.yml": {
133174
"comment": "# OWE has not been supported by NetworkManager 1.18.8 on \
134175
RHEL 7(dist-tag). Failed in setting up mock wifi on RHEL 8",
135-
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] > '7' and \
136-
ansible_facts['distribution'] == 'CentOS' or\n ansible_facts['distribution_major_version'] > '32' \
137-
and ansible_facts['distribution'] == 'Fedora'",
176+
EXTRA_RUN_CONDITION: "__network_distro_major_version | int > 7 and \
177+
__network_is_centos or\n __network_distro_major_version | int > 32 \
178+
and __network_is_fedora",
138179
},
139180
"playbooks/tests_wireless_wpa3_sae.yml": {
140181
"comment": "# SAE has not been supported by NetworkManager 1.18.8 on \
141182
RHEL 7. Failed in setting up mock wifi on RHEL 8",
142-
EXTRA_RUN_CONDITION: "ansible_facts['distribution_major_version'] != '7' and \
143-
ansible_facts['distribution'] != 'RedHat'",
183+
EXTRA_RUN_CONDITION: "__network_distro_major_version != '7' and \
184+
not __network_is_rhel",
144185
},
145186
}
146187
# NM_CONDITIONAL_TESTS is used to store the test playbooks which are demanding for NM
@@ -168,27 +209,6 @@
168209
"playbooks/tests_switch_provider.yml",
169210
]
170211

171-
RUN_PLAYBOOK_WITH_INITSCRIPTS = """# SPDX-License-Identifier: BSD-3-Clause
172-
# This file was generated by ensure_provider_tests.py
173-
---
174-
# yamllint disable rule:line-length
175-
- name: Run playbook '{test_playbook}' with initscripts as provider
176-
hosts: all
177-
tasks:
178-
- name: Include the task 'el_repo_setup.yml'
179-
include_tasks: tasks/el_repo_setup.yml
180-
- name: Set network provider to 'initscripts'
181-
set_fact:
182-
network_provider: initscripts
183-
tags:
184-
- always
185-
186-
- name: Import the playbook '{test_playbook}'
187-
import_playbook: {test_playbook}
188-
when: (ansible_facts['distribution'] in ['CentOS','RedHat'] and\n \
189-
ansible_facts['distribution_major_version'] | int < 9)
190-
"""
191-
192212

193213
def create_nm_playbook(test_playbook):
194214
fileroot = os.path.splitext(os.path.basename(test_playbook))[0]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../library/network_connections.py

‎tests/playbooks/manual_test_ethtool_coalesce.yml‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
register: original_ethtool_coalesce
3535
changed_when: false
3636
- name: Import network role
37-
import_role:
38-
name: linux-system-roles.network
37+
include_tasks: tasks/run_role_with_clear_facts.yml
3938
vars:
4039
network_connections:
4140
- name: "{{ interface }}"
@@ -57,8 +56,7 @@
5756
debug:
5857
msg: "##################################################"
5958
- name: Import network role
60-
import_role:
61-
name: linux-system-roles.network
59+
include_tasks: tasks/run_role_with_clear_facts.yml
6260
vars:
6361
network_connections:
6462
- name: "{{ interface }}"
@@ -91,8 +89,7 @@
9189
debug:
9290
msg: "##################################################"
9391
- name: Import network role
94-
import_role:
95-
name: linux-system-roles.network
92+
include_tasks: tasks/run_role_with_clear_facts.yml
9693
vars:
9794
network_connections:
9895
- name: "{{ interface }}"
@@ -118,14 +115,13 @@
118115
- "tests::cleanup"
119116
block:
120117
- name: Deactivate the connection and remove the connection profile
121-
import_role:
122-
name: linux-system-roles.network
118+
include_tasks: tasks/run_role_with_clear_facts.yml
123119
vars:
124120
network_connections:
125121
- name: "{{ interface }}"
126122
persistent_state: absent
127123
state: down
128-
failed_when: false
124+
__sr_failed_when: false
129125
- name: Include the task 'manage_test_interface.yml'
130126
include_tasks: tasks/manage_test_interface.yml
131127
vars:

‎tests/playbooks/tests_802_1x.yml‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
debug:
1717
msg: "##################################################"
1818
- name: Import network role
19-
import_role:
20-
name: linux-system-roles.network
19+
include_tasks: tasks/run_role_with_clear_facts.yml
2120
vars:
2221
network_connections:
2322
- name: "{{ interface }}"
@@ -48,8 +47,7 @@
4847
command: ping -c1 203.0.113.1
4948
changed_when: false
5049
- name: Import network role
51-
import_role:
52-
name: linux-system-roles.network
50+
include_tasks: tasks/run_role_with_clear_facts.yml
5351
vars:
5452
network_connections:
5553
- name: "{{ interface }}"
@@ -79,8 +77,7 @@
7977
executable: /bin/bash
8078
changed_when: false
8179
- name: Import network role
82-
import_role:
83-
name: linux-system-roles.network
80+
include_tasks: tasks/run_role_with_clear_facts.yml
8481
vars:
8582
network_connections:
8683
- name: "{{ interface }}"
@@ -105,8 +102,7 @@
105102
command: ping -c1 203.0.113.1
106103
changed_when: false
107104
- name: Import network role
108-
import_role:
109-
name: linux-system-roles.network
105+
include_tasks: tasks/run_role_with_clear_facts.yml
110106
vars:
111107
network_connections:
112108
- name: "{{ interface }}"
@@ -121,8 +117,7 @@
121117
- "tests::cleanup"
122118
block:
123119
- name: Deactivate the connection and remove the connection profile
124-
import_role:
125-
name: linux-system-roles.network
120+
include_tasks: tasks/run_role_with_clear_facts.yml
126121
vars:
127122
network_connections:
128123
- name: "{{ interface }}"
@@ -131,7 +126,7 @@
131126
- name: br1
132127
persistent_state: absent
133128
state: down
134-
failed_when: false
129+
__sr_failed_when: false
135130
- name: Include the task 'cleanup_802_1x_server.yml'
136131
include_tasks: tasks/cleanup_802_1x_server.yml
137132
- name: Remove test certificates

‎tests/playbooks/tests_auto_gateway.yml‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
debug:
2020
msg: "##################################################"
2121
- name: Import network role
22-
import_role:
23-
name: linux-system-roles.network
22+
include_tasks: tasks/run_role_with_clear_facts.yml
2423
vars:
2524
network_connections:
2625
- name: "{{ interface }}"
@@ -68,8 +67,7 @@
6867
debug:
6968
msg: "##################################################"
7069
- name: Import network role to remove interface
71-
import_role:
72-
name: linux-system-roles.network
70+
include_tasks: tasks/run_role_with_clear_facts.yml
7371
vars:
7472
network_connections:
7573
- name: "{{ interface }}"
@@ -89,8 +87,7 @@
8987
vars:
9088
state: present
9189
- name: Import network role to disable auto_gateway
92-
import_role:
93-
name: linux-system-roles.network
90+
include_tasks: tasks/run_role_with_clear_facts.yml
9491
vars:
9592
network_connections:
9693
- name: "{{ interface }}"
@@ -134,8 +131,7 @@
134131
debug:
135132
msg: "##################################################"
136133
- name: Import network role to remove interface again
137-
import_role:
138-
name: linux-system-roles.network
134+
include_tasks: tasks/run_role_with_clear_facts.yml
139135
vars:
140136
network_connections:
141137
- name: "{{ interface }}"

‎tests/playbooks/tests_bond.yml‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
debug:
3535
msg: "##################################################"
3636
- name: Import network role
37-
import_role:
38-
name: linux-system-roles.network
37+
include_tasks: tasks/run_role_with_clear_facts.yml
3938
vars:
4039
network_connections:
4140
# Create a bond controller
@@ -98,8 +97,7 @@
9897
- "tests::cleanup"
9998
block:
10099
- name: Import network role
101-
import_role:
102-
name: linux-system-roles.network
100+
include_tasks: tasks/run_role_with_clear_facts.yml
103101
vars:
104102
network_connections:
105103
- name: "{{ port2_profile }}"
@@ -111,7 +109,7 @@
111109
- name: "{{ controller_profile }}"
112110
persistent_state: absent
113111
state: down
114-
failed_when: false
112+
__sr_failed_when: false
115113
- name: Delete the device '{{ controller_device }}'
116114
command: ip link del {{ controller_device }}
117115
failed_when: false

0 commit comments

Comments
 (0)