network/tests/playbooks/tests_route_table.yml
Rich Megginson 88a2609327 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>
2026-03-19 10:47:09 -06:00

194 lines
6.6 KiB
YAML

# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for testing route table
hosts: all
vars:
type: veth
interface: ethtest0
tasks:
- name: Include the task 'show_interfaces.yml'
include_tasks: tasks/show_interfaces.yml
- name: Include the task 'manage_test_interface.yml'
include_tasks: tasks/manage_test_interface.yml
vars:
state: present
- name: Include the task 'assert_device_present.yml'
include_tasks: tasks/assert_device_present.yml
- name: Configure connection profile and specify the numeric table in
static routes
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
network_connections:
- name: "{{ interface }}"
interface_name: "{{ interface }}"
state: up
type: ethernet
autoconnect: true
ip:
dhcp4: false
address:
- 198.51.100.3/26
route:
- network: 198.51.100.128
prefix: 26
gateway: 198.51.100.1
metric: 2
table: 30400
- network: 198.51.100.64
prefix: 26
gateway: 198.51.100.6
metric: 4
table: 30200
- network: 192.0.2.64
prefix: 26
gateway: 198.51.100.8
metric: 50
table: 30200
src: 198.51.100.3
- network: 198.51.101.128
prefix: 26
gateway: 198.51.101.1
metric: 2
table: main
- name: Get the routes from the route table 30200
command: ip route show table 30200
register: route_table_30200
ignore_errors: true
changed_when: false
- name: Get the routes from the route table 30400
command: ip route show table 30400
register: route_table_30400
ignore_errors: true
changed_when: false
- name: Get the routes from the route table main
command: ip route show table main
register: route_table_main
ignore_errors: true
changed_when: false
- name: Assert that the route table 30200 contains the specified route
assert:
that:
- route_table_30200.stdout is search("198.51.100.64/26 via
198.51.100.6 dev ethtest0 proto static metric 4")
- route_table_30200.stdout is search("192.0.2.64/26 via
198.51.100.8 dev ethtest0 proto static src 198.51.100.3
metric 50")
msg: "the route table 30200 does not exist or does not contain the
specified route"
- name: Assert that the route table 30400 contains the specified route
assert:
that:
- route_table_30400.stdout is search("198.51.100.128/26 via
198.51.100.1 dev ethtest0 proto static metric 2")
msg: "the route table 30400 does not exist or does not contain the
specified route"
- name: Assert that the route table main contains the specified route
assert:
that:
- route_table_main.stdout is search("198.51.101.128/26 via
198.51.101.1 dev ethtest0 proto static metric 2")
msg: "the route table main does not exist or does not contain the
specified route"
- name: Create a dedicated test file in `/etc/iproute2/rt_tables.d/` and
add a new routing table
lineinfile:
path: /etc/iproute2/rt_tables.d/table.conf
line: "200 custom"
mode: "0644"
create: true
- name: Reconfigure connection profile and specify the named table in
static routes
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
network_connections:
- name: "{{ interface }}"
interface_name: "{{ interface }}"
state: up
type: ethernet
autoconnect: true
ip:
dhcp4: false
address:
- 198.51.100.3/26
route:
- network: 198.51.100.128
prefix: 26
gateway: 198.51.100.1
metric: 2
table: custom
- network: 198.51.100.64
prefix: 26
gateway: 198.51.100.6
metric: 4
table: custom
- network: 192.0.2.64
prefix: 26
gateway: 198.51.100.8
metric: 50
table: custom
src: 198.51.100.3
- network: 198.51.101.128
prefix: 26
gateway: 198.51.101.1
metric: 2
table: custom
- name: Get the routes from the named route table 'custom'
command: ip route show table custom
register: route_table_custom
ignore_errors: true
changed_when: false
- name: Assert that the named route table 'custom' contains the
specified route
assert:
that:
- route_table_custom.stdout is search("198.51.100.128/26 via
198.51.100.1 dev ethtest0 proto static metric 2")
- route_table_custom.stdout is search("198.51.100.64/26 via
198.51.100.6 dev ethtest0 proto static metric 4")
- route_table_custom.stdout is search("192.0.2.64/26 via
198.51.100.8 dev ethtest0 proto static src 198.51.100.3
metric 50")
- route_table_custom.stdout is search("198.51.101.128/26 via
198.51.101.1 dev ethtest0 proto static metric 2")
msg: "the named route table 'custom' does not exist or does not contain
the specified route"
- name: Remove the dedicated test file in `/etc/iproute2/rt_tables.d/`
file:
state: absent
path: /etc/iproute2/rt_tables.d/table.conf
- name: Include the tasks 'down_profile+delete_interface.yml'
include_tasks: tasks/down_profile+delete_interface.yml
vars:
profile: "{{ interface }}"
# FIXME: assert profile/device down
- name: Include the task 'remove_profile.yml'
include_tasks: tasks/remove_profile.yml
vars:
profile: "{{ interface }}"
- name: Include the task 'assert_profile_absent.yml'
include_tasks: tasks/assert_profile_absent.yml
vars:
profile: "{{ interface }}"
- name: Include the task 'assert_device_absent.yml'
include_tasks: tasks/assert_device_absent.yml
- name: Verify network state restored to default
include_tasks: tasks/check_network_dns.yml