network/tests/playbooks/tests_network_state.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 configuring network using network state variable
hosts: all
vars:
type: veth
interface0: ethtest0
interface1: ethtest1
tasks:
- name: "Set type={{ type }} and interface={{ interface0 }}" # noqa name
set_fact:
type: "{{ type }}"
interface: "{{ interface0 }}"
- 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: "Set type={{ type }} and interface={{ interface1 }}" # noqa name
set_fact:
type: "{{ type }}"
interface: "{{ interface1 }}"
- name: Include the task 'show_interfaces.yml' again
include_tasks: tasks/show_interfaces.yml
- name: Include the task 'manage_test_interface.yml' after changing interface
include_tasks: tasks/manage_test_interface.yml
vars:
state: present
- name: Include the task 'assert_device_present.yml' after changing interface
include_tasks: tasks/assert_device_present.yml
- name: Configure the IP addresses
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
network_state:
interfaces:
- name: ethtest0
type: ethernet
state: up
ipv4:
enabled: true
address:
- ip: 192.168.122.250
prefix-length: 24
dhcp: false
ipv6:
enabled: true
address:
- ip: 2001:db8::1:1
prefix-length: 64
autoconf: false
dhcp: false
- name: ethtest1
type: ethernet
state: up
ipv4:
enabled: true
auto-dns: false
address:
- ip: 192.168.122.88
prefix-length: 24
dhcp: false
ipv6:
enabled: true
auto-dns: false
dhcp: true
- name: Get the ethtest0 state configuration
command: nmstatectl show ethtest0
register: ethtest0_state
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Get the ethtest1 state configuration
command: nmstatectl show ethtest1
register: ethtest1_state
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Assert that the ethtest0 state configuration contains the specified
settings
assert:
that:
- ethtest0_state.stdout is search("192.168.122.250")
- ethtest0_state.stdout is search("2001:db8::1:1")
msg: the ethtest0 state configuration does not contain the specified
settings
- name: Assert that the ethtest1 state configuration contains the specified
settings
assert:
that:
- ethtest1_state.stdout is search("192.168.122.88")
msg: the ethtest1 state configuration does not contain the specified
settings
- name: Configure the route
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
network_state:
routes:
config:
- destination: 192.0.2.100/30
metric: 150
next-hop-address: 192.168.122.250
next-hop-interface: ethtest0
table-id: 254
- name: Get the route configuration
command: nmstatectl show
register: route
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Assert that the route configuration contains the specified route
assert:
that:
- route.stdout is search("destination:(\s+)192.0.2.100/30")
- route.stdout is search("metric:(\s+)150")
- route.stdout is search("next-hop-address:(\s+)192.168.122.250")
- route.stdout is search("next-hop-interface:(\s+)ethtest0")
- route.stdout is search("table-id:(\s+)254")
msg: the route configuration does not contain the specified route
- name: Configure the DNS
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
network_state:
dns-resolver:
config:
search:
- example.com
- example.org
server:
- 2001:4860:4860::8888
- name: Get the DNS configuration from nmstatectl
command: nmstatectl show
register: nmstatectl
ignore_errors: true # noqa ignore-errors
changed_when: false
- name: Get the DNS configuration from the file
`/run/NetworkManager/no-stub-resolv.conf`
command: cat /run/NetworkManager/no-stub-resolv.conf
register: no_stub_resolvconf
changed_when: false
- name: Assert that the nmstatectl contains the specified DNS configuration
assert:
that:
- nmstatectl.stdout is search("example.com")
- nmstatectl.stdout is search("example.org")
- nmstatectl.stdout is search("2001:4860:4860::8888")
msg: the nmstatectl does not contain the specified DNS configuration
- name: Assert that the file `/run/NetworkManager/no-stub-resolv.conf`
contains the specified DNS configuration
assert:
that:
- no_stub_resolvconf.stdout is search("example.com")
- no_stub_resolvconf.stdout is search("example.org")
- no_stub_resolvconf.stdout is search("2001:4860:4860::8888")
msg: the file `/run/NetworkManager/no-stub-resolv.conf` does not
contain the specified DNS configuration
- name: Purge the DNS
include_tasks: tasks/run_role_with_clear_facts.yml
vars:
network_state:
dns-resolver:
config: {}
- name: Include the task 'delete_interface.yml'
include_tasks: tasks/delete_interface.yml
- name: Include the task 'assert_device_absent.yml'
include_tasks: tasks/assert_device_absent.yml
- name: "Set interface={{ interface0 }}"
set_fact:
type: "{{ type }}"
interface: "{{ interface0 }}"
- name: Include the task 'delete_interface.yml' again
include_tasks: tasks/delete_interface.yml
- name: Include the task 'assert_device_absent.yml' again
include_tasks: tasks/assert_device_absent.yml
- name: Verify network state restored to default
include_tasks: tasks/check_network_dns.yml
...