network/tests/tests_unit.yml
Rich Megginson e4d499763c ci: Use supported ansible-lint action; run ansible-lint against the collection
The old ansible-community ansible-lint is deprecated.  There is a
new ansible-lint github action.  The new ansible-lint has several checks
related to ansible-test and the ignore files.  Many of our ignore settings
are not allowed any more and are required to be fixed or addressed in the
Ansible preferred way.

The python imports have to be wrapped in a try/except ImportError, and
where possible, an error must be returned from the module explaining
what was not able to be imported.

The module documentation must comply with the Ansible standards.  One
aspect of this is the `version_added` must be a valid ansible-core
version in X.Y.Z format.  Note that this version isn't really used
anywhere, so it doesn't matter for users of the role, it is purely
an `ansible-test` and import gating issue.

The result of this is that the .sanity files can be reduced to the
bare minimum which will greatly reduce the maintenance burden of
those files, make it easier to support newer versions of Ansible,
and make it easier to import the system roles collection into Galaxy
and Automation Hub.

The latest Ansible repo gating tests run ansible-lint against
the collection format instead of against individual roles.
We have to convert the role to collection format before running
ansible-test.

Role developers can run this locally using
`tox -e collection,ansible-lint-collection`
See https://github.com/linux-system-roles/tox-lsr/pull/125

Add `---` doc start to .markdownlint.yaml

The file `examples/down_profile+delete_interface.yml`
was not used and was causing ansible-lint errors.

ansible-lint enforces the order of keywords in plays - `name`,
then `hosts`, then `vars`, then `tasks`.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2024-01-05 17:36:07 -07:00

183 lines
5.9 KiB
YAML

# SPDX-License-Identifier: BSD-3-Clause
---
- name: Setup for test running
hosts: all
tasks:
- name: Include the task 'el_repo_setup.yml'
include_tasks: tasks/el_repo_setup.yml
- name: Install dependencies
package:
name: "{{ item }}"
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
# Ignore error because some package names might not be available
ignore_errors: true # noqa ignore-errors
loop:
- NetworkManager-libnm
- python2-gobject-base
- python3-gobject-base
- python-gobject-base
- python2-mock
- name: Execute python unit tests
hosts: all
tasks:
- name: Execute python unit tests
block:
- name: Create tempdir for code to test
tempfile:
state: directory
prefix: lsrtest_
register: _rundir
- name: Get tempfile for tar
tempfile:
prefix: lsrtest_
suffix: ".tar"
register: temptar
delegate_to: localhost
- name: Include the task 'get_modules_and_utils_paths.yml'
include_tasks: tasks/get_modules_and_utils_paths.yml
# TODO: using tar and copying the file is a workaround for the
# synchronize module that does not work in test-harness. Related issue:
# https://github.com/linux-system-roles/test-harness/issues/102
#
- name: Create Tar file
command: >
tar -cvf {{ temptar.path }} --exclude "*.pyc"
--exclude "__pycache__"
-C {{ modules_parent_and_dir.stdout_lines[0] }}
{{ modules_parent_and_dir.stdout_lines[1] }}
-C {{ module_utils_parent_and_dir.stdout_lines[0] }}
{{ module_utils_parent_and_dir.stdout_lines[1] }}
# noqa command-instead-of-module
delegate_to: localhost
changed_when: false
- name: Copy testrepo.tar to the remote system
copy:
src: "{{ temptar.path }}"
dest: "{{ _rundir.path }}"
mode: preserve
- name: Untar testrepo.tar
unarchive:
src: "{{ _rundir.path }}/{{ temptar.path | basename }}"
dest: "{{ _rundir.path }}"
remote_src: true
- name: Create subdirectory '/ansible/module_utils' under
'{{ _rundir.path }}'
file:
state: directory
path: "{{ item }}"
mode: "0755"
loop:
- "{{ _rundir.path }}/ansible"
- "{{ _rundir.path }}/ansible/module_utils"
- name: Move module_utils to ansible directory
shell: |
if [ -d {{ _rundir.path }}/module_utils ]; then
mv {{ _rundir.path }}/module_utils {{ _rundir.path }}/ansible
fi
changed_when: false
- name: Fake out python module directories, primarily for python2
shell: |
for dir in $(find {{ _rundir.path }} -type d -print); do
if [ ! -f "$dir/__init__.py" ]; then
touch "$dir/__init__.py"
fi
done
changed_when: false
- name: Copy unit test to remote system
copy:
src: unit/test_network_connections.py
dest: "{{ _rundir.path }}"
mode: preserve
- name: Set _lsr_python_path
set_fact:
_lsr_python_path: "{{
_rundir.path ~ '/' ~
modules_parent_and_dir.stdout_lines[1] ~ ':' ~
_rundir.path ~ '/' ~ 'ansible' ~ '/' ~
module_utils_parent_and_dir.stdout_lines[1] ~ ':' ~
_rundir.path ~ '/' ~
module_utils_parent_and_dir.stdout_lines[1] ~ ':' ~
_rundir.path
}}"
- name: "List the files in {{ _rundir.path }}"
command: ls -alrtFR {{ _rundir.path }}
changed_when: false
- name: Debug the lsr Python path
debug:
msg: path {{ _lsr_python_path }}
- name: Check if python2 is available
command: python2 --version
ignore_errors: true
register: python2_available
when: true
changed_when: false
- name: Run python2 unit tests
command: >
python2 {{ _rundir.path }}/test_network_connections.py --verbose
environment:
PYTHONPATH: "{{ _lsr_python_path }}"
when: >
python2_available is succeeded and ansible_distribution != 'Fedora'
register: python2_result
changed_when: false
- name: Check if python3 is available
command: python3 --version
ignore_errors: true
register: python3_available
when: true
changed_when: false
- name: Run python3 unit tests
command: >
python3 {{ _rundir.path }}/test_network_connections.py --verbose
environment:
PYTHONPATH: "{{ _lsr_python_path }}"
when: python3_available is succeeded
register: python3_result
changed_when: false
- name: Show python2 unit test results
debug:
var: python2_result.stderr_lines
when: python2_result is succeeded
- name: Show python3 unit test results
debug:
var: python3_result.stderr_lines
when: python3_result is succeeded
always:
- name: Remove local tar file
file:
state: absent
path: "{{ temptar.path }}"
delegate_to: localhost
- name: Remove tempdir
file:
state: absent
path: "{{ _rundir.path }}"
- name: Ensure that at least one python unit test ran
fail:
msg: Tests did not run with python2 or python3
when: not python2_available is succeeded and
not python3_available is succeeded