diff --git a/tests/run-tasks.yml b/tests/run-tasks.yml new file mode 100644 index 0000000..ea56720 --- /dev/null +++ b/tests/run-tasks.yml @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- name: Run the tasklist {{ task }} + hosts: all + tasks: + - include_tasks: "{{ task }}" diff --git a/tests/tasks/assert-device_absent.yml b/tests/tasks/assert-device_absent.yml new file mode 100644 index 0000000..06be2b6 --- /dev/null +++ b/tests/tasks/assert-device_absent.yml @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- setup: +- assert: + name: "assert that {{ interface }} is absent" + that: "{{ not interface in ansible_interfaces }}" + msg: "{{ interface }} is in ansible_interfaces: {{ ansible_interfaces }}" diff --git a/tests/tasks/assert-device_present.yml b/tests/tasks/assert-device_present.yml new file mode 100644 index 0000000..5c09bd3 --- /dev/null +++ b/tests/tasks/assert-device_present.yml @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +# need to run setup again when this is just a task to make sure ansible_interfaces is correct +- setup: +- assert: + name: "assert that {{ interface }} is present" + that: "{{ interface in ansible_interfaces }}" + msg: "{{ interface }} is not in ansible_interfaces: {{ ansible_interfaces }}" diff --git a/tests/tasks/create-and-remove-interface.yml b/tests/tasks/create-and-remove-interface.yml new file mode 100644 index 0000000..9bebf6e --- /dev/null +++ b/tests/tasks/create-and-remove-interface.yml @@ -0,0 +1,20 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- include_tasks: show-interfaces.yml +- include_tasks: manage-test-interface.yml + vars: + state: absent +- include_tasks: show-interfaces.yml +- include_tasks: assert-device_absent.yml + +- include_tasks: manage-test-interface.yml + vars: + state: present +- include_tasks: show-interfaces.yml +- include_tasks: assert-device_present.yml + +- include_tasks: manage-test-interface.yml + vars: + state: absent +- include_tasks: show-interfaces.yml +- include_tasks: assert-device_absent.yml diff --git a/tests/tasks/make-profile-absent.yml b/tests/tasks/make-profile-absent.yml new file mode 100644 index 0000000..95496aa --- /dev/null +++ b/tests/tasks/make-profile-absent.yml @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- name: Remove {{ profile }} + hosts: all + vars: + network_connections: + - name: "{{ profile }}" + state: absent + roles: + - linux-system-roles.network diff --git a/tests/tasks/make-profile-down.yml b/tests/tasks/make-profile-down.yml new file mode 100644 index 0000000..5087240 --- /dev/null +++ b/tests/tasks/make-profile-down.yml @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- name: Set {{ profile }} down + hosts: all + vars: + network_connections: + - name: "{{ profile }}" + state: down + roles: + - linux-system-roles.network diff --git a/tests/tasks/manage-test-interface.yml b/tests/tasks/manage-test-interface.yml new file mode 100644 index 0000000..6d6add8 --- /dev/null +++ b/tests/tasks/manage-test-interface.yml @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- fail: + msg: "state needs to be present or absent, not '{{ state }}'" + when: state not in ["present", "absent"] + +- fail: + msg: "type needs to be dummy, tap or veth, not '{{ type }}'" + when: type not in ["dummy", "tap", "veth"] + +# run setup again to make sure ansible_interfaces is accurate +- setup: + + # veth +- name: Create veth interface {{ interface }} + shell: ip link add {{ interface }} type veth peer name peer{{ interface }} + when: "type == 'veth' and state == 'present' and interface not in ansible_interfaces" + +- name: Delete veth interface {{ interface }} + shell: ip link del {{ interface }} type veth + when: "type == 'veth' and state == 'absent' and interface in ansible_interfaces" + + # dummy +- name: Create dummy interface {{ interface }} + shell: ip link add "{{ interface }}" type dummy + when: "type == 'dummy' and state == 'present' and interface not in ansible_interfaces" + +- name: Delete dummy interface {{ interface }} + shell: ip link del "{{ interface }}" type dummy + when: "type == 'dummy' and state == 'absent' and interface in ansible_interfaces" + + # tap +- name: Create tap interface {{ interface }} + shell: ip tuntap add dev {{ interface }} mode tap + when: "type == 'tap' and state == 'present' and interface not in ansible_interfaces" + +- name: Delete tap interface {{ interface }} + shell: ip tuntap del dev {{ interface }} mode tap + when: "type == 'tap' and state == 'absent' and interface in ansible_interfaces" diff --git a/tests/tasks/show-interfaces.yml b/tests/tasks/show-interfaces.yml new file mode 100644 index 0000000..a4b8c44 --- /dev/null +++ b/tests/tasks/show-interfaces.yml @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- debug: + msg: "ansible_interfaces: {{ ansible_interfaces }}" diff --git a/tests/tests_helpers-and-asserts.yml b/tests/tests_helpers-and-asserts.yml new file mode 100644 index 0000000..33f3e84 --- /dev/null +++ b/tests/tests_helpers-and-asserts.yml @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: BSD-3-Clause +--- +- name: Check that creating and removing test devices and assertions work + hosts: all + tasks: + - name: test veth interface management + include_tasks: tasks/create-and-remove-interface.yml + vars: + type: veth + interface: veth1298 + + - name: test veth interface management + include_tasks: tasks/create-and-remove-interface.yml + vars: + type: dummy + interface: dummy1298 + +# FIXME: when: does not seem to work with include_tasks, therefore this cannot be safely tested for now +# - name: test tap interfaces +# include_tasks: tasks/create-and-remove-interface.yml type=tap interface=tap1298 +# when: ansible_distribution_major_version > 6 +# # ip tuntap does not exist on RHEL6 +# # FIXME: Maybe use some other tool to manage devices, openvpn can do this, +# # but it is in EPEL