Skip to content

Commit 5966c87

Browse files
tests: Add a playbook and job to run sanity tests
This is meant to install every collection in a release's galaxy-requirements.yaml and then iterate over them to run sanity tests on each. Note that errors are not fatal yet (ignore_errors: true). Co-authored-by: David Moreau Simard <[email protected]> Co-authored-by: Felix Fontein <[email protected]>
1 parent 8d0292a commit 5966c87

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

.github/workflows/sanity-tests.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Sanity tests
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
# Run once per week (Monday at 04:00 UTC)
9+
schedule:
10+
- cron: '0 4 * * 1'
11+
12+
jobs:
13+
build:
14+
name: '${{ matrix.name }}'
15+
runs-on: ubuntu-latest
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
include:
20+
- name: latest Ansible 5 release
21+
ansible_major: 5
22+
ansible_core: 2.12
23+
ansible_core_next: 2.13
24+
- name: latest Ansible 6 release
25+
ansible_major: 6
26+
ansible_core: 2.13
27+
ansible_core_next: 2.14
28+
29+
steps:
30+
- name: Check out ansible-build-data
31+
uses: actions/checkout@v3
32+
with:
33+
path: ansible-build-data
34+
35+
- name: Set up Python 3.9
36+
uses: actions/setup-python@v2
37+
with:
38+
python-version: 3.9
39+
40+
- name: Install ansible-core ${{ matrix.ansible_core }}
41+
run: |
42+
python3 -m pip install --upgrade pip
43+
python3 -m pip install ansible-core>=${{ matrix.ansible_core }},${{ matrix.ansible_core_next }}
44+
45+
- name: "Run sanity tests for ${{ matrix.name }}"
46+
run: |
47+
ansible-playbook -vv tests/collection-tests.yaml -e 'galaxy_requirements="{{ playbook_dir | dirname }}/${{ matrix.ansible_major }}/galaxy-requirements.yaml"'
48+
working-directory: ansible-build-data

tests/collection-tests.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
- name: Run tests on packaged collections
3+
hosts: localhost
4+
gather_facts: false
5+
vars:
6+
galaxy_requirements: "{{ playbook_dir | dirname }}/6/galaxy-requirements.yaml"
7+
collections_path: /tmp/ansible_collections
8+
tasks:
9+
- name: Get list of installed packages to verify if podman is installed
10+
package_facts:
11+
manager: "auto"
12+
# This is noisy (and potentially sensitive) to print on stdout
13+
no_log: true
14+
15+
# This is so we can otherwise run unprivileged if podman is already installed
16+
- when: "'podman' not in ansible_facts['packages'].keys()"
17+
become: true
18+
block:
19+
- name: Install podman
20+
package:
21+
name: podman
22+
state: present
23+
rescue:
24+
- name: Could not install podman
25+
fail:
26+
msg: |
27+
Failed to elevate privileges and install podman.
28+
Install podman manually or run ansible-playbook with elevated privileges.
29+
30+
- name: Install collections from galaxy
31+
environment:
32+
ANSIBLE_COLLECTIONS_PATH: "{{ collections_path }}"
33+
command: ansible-galaxy collection install -r {{ galaxy_requirements }}
34+
args:
35+
creates: "{{ collections_path }}"
36+
37+
# ansible.builtin.find doesn't have mindepth
38+
# https://github.com/ansible/ansible/issues/36369
39+
- name: Find collection directories
40+
command: find {{ collections_path }} -mindepth 2 -maxdepth 2 -type d
41+
changed_when: false
42+
register: _collection_directories
43+
44+
# Tests are broken up such that there is a task per collection (instead of one very long task)
45+
- name: Run sanity tests
46+
include_tasks: tasks/sanity-tests.yaml
47+
loop: "{{ _collection_directories.stdout_lines }}"
48+
loop_control:
49+
loop_var: _collection_directory

tests/tasks/sanity-tests.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
- name: "Run sanity tests for {{ _collection_name }}"
2+
vars:
3+
_collection_name: >-
4+
{%- set _namespace = _collection_directory.split('/')[-2] -%}
5+
{%- set _name = _collection_directory.split('/')[-1] -%}
6+
{{ _namespace }}.{{ _name }}
7+
command: ansible-test sanity --skip-test pylint --docker
8+
changed_when: false
9+
ignore_errors: true
10+
args:
11+
chdir: "{{ _collection_directory }}"

0 commit comments

Comments
 (0)