network/tests/playbooks/integration_pytest_python3.yml
Rich Megginson 0c590cdf5a refactor: improve support for ostree systems
The dependency on `ansible.utils.update_fact` is causing issue with
some users who now must install that collection in order to run
the role, even if they do not care about ostree.

The fix is to stop trying to set `ansible_facts.pkg_mgr`, and instead
force the use of the ostree package manager with the `package:` module
`use:` option.  The strategy is - on ostree systems, set the flag
`__$ROLENAME_is_ostree` if the system is an ostree system.  The flag
will either be undefined or `false` on non-ostree systems.
Then, change every invocation of the `package:` module like this:

```yaml
- name: Ensure required packages are present
  package:
    name: "{{ __$ROLENAME_packages }}"
    state: present
    use: "{{ (__$ROLENAME_is_ostree | d(false)) |
      ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
```

This should ensure that the `use:` parameter is not used if the system
is non-ostree.  The goal is to make the ostree support as unobtrusive
as possible for non-ostree systems.
The user can also set `__$ROLENAME_is_ostree: true` in the inventory or play
if the user knows that ostree is being used and wants to skip the check.
Or, the user is concerned about the performance hit for ostree detection
on non-ostree systems, and sets `__$ROLENAME_is_ostree: false` to skip
the check.
The flag `__$ROLENAME_is_ostree` can also be used in the role or tests to
include or exclude tasks from being run on ostree systems.

This fix also improves error reporting in the `get_ostree_data.sh` script
when included roles cannot be found.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
2023-11-29 07:05:00 -07:00

181 lines
5.8 KiB
YAML

# SPDX-License-Identifier: BSD-3-Clause
---
- name: Install dependencies for integration tests
hosts: all
vars:
rpmdependencies:
- git
- python3-pip
- rsync
tasks:
- name: Install rpm dependencies
package:
state: present
name: "{{ rpmdependencies }}"
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Install Pytest
command: "pip3 install pytest"
changed_when: false
# Import needed in order to install initscripts dependencies on the remote
# system.
- name: Import the playbook '../tests_default_initscripts.yml'
import_playbook: "../tests_default_initscripts.yml"
# Import needed in order to install Network Manager dependencies on the remote
# system.
- name: Import the playbook '../tests_default_nm.yml'
import_playbook: "../tests_default_nm.yml"
- name: Run Pytest tests
hosts: all
tasks:
- name: Run Pytest 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 '../tasks/get_modules_and_utils_paths.yml'
include_tasks: ../tasks/get_modules_and_utils_paths.yml
- name: Get tests directory
set_fact:
tests_directory: "{{ lookup('first_found', params) }}"
vars:
params:
files:
- tests
- network
paths:
- "../.."
# 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 {{ tests_directory | realpath | dirname }}
{{ tests_directory | basename }}
-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' under '{{ _rundir.path }}'"
file:
state: directory
path: "{{ _rundir.path }}/ansible"
mode: "0755"
- 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: Set _lsr_python_path
set_fact:
_lsr_python_path: "{{
_rundir.path ~ '/' ~
modules_parent_and_dir.stdout_lines[1] ~ ':' ~ _rundir.path
}}"
- name: Show _lsr_python_path
debug:
msg: path {{ _lsr_python_path }}
- name: "List the files in {{ _rundir.path }}"
command: ls -alrtFR {{ _rundir.path }}
changed_when: false
- name: Run pytest with nm provider
block:
- name: Run pytest with nm
command: >
pytest
{{ _rundir.path }}/{{ tests_directory | basename }}/integration/
--provider=nm
register: playbook_run
environment:
PYTHONPATH: "{{ _lsr_python_path }}"
changed_when: false
always:
- name: Debug stdout_lines of the running playbook with nm
debug:
var: playbook_run.stdout_lines
- name: Run pytest with initscripts provider
block:
- name: Install network-scripts when running pytest with initscripts
package:
name: network-scripts
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Run pytest with initscripts
command: >
pytest
{{ _rundir.path }}/{{ tests_directory | basename }}/integration/
--provider=initscripts
register: playbook_run
environment:
PYTHONPATH: "{{ _lsr_python_path }}"
changed_when: false
always:
- name: Debug stdout_lines of the running playbook with initscripts
debug:
var: playbook_run.stdout_lines
always:
- name: Remove local tar file
file:
state: absent
path: "{{ temptar.path }}"
delegate_to: localhost
- name: Remove tempdir
file:
state: absent
path: "{{ _rundir.path }}"