Skip to content

tests: Add a playbook and job to run sanity tests #122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/sanity-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Sanity tests

on:
push:
branches: [main]
pull_request:
branches: [main]
# Run once per week (Monday at 04:00 UTC)
schedule:
- cron: '0 4 * * 1'

jobs:
build:
name: '${{ matrix.name }}'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- name: latest Ansible 5 release
ansible_major: 5
ansible_core: 2.12
ansible_core_next: 2.13
- name: latest Ansible 6 release
ansible_major: 6
ansible_core: 2.13
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: this won't work until 2.13 releases next week

ansible_core_next: 2.14

steps:
- name: Check out ansible-build-data
uses: actions/checkout@v3
with:
path: ansible-build-data

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: Install ansible-core ${{ matrix.ansible_core }}
run: |
python3 -m pip install --upgrade pip
python3 -m pip install ansible-core>=${{ matrix.ansible_core }},${{ matrix.ansible_core_next }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way it should also install pre-releases of ansible-core, without installing pre-releases of ansible-core's dependencies:

Suggested change
python3 -m pip install ansible-core>=${{ matrix.ansible_core }},${{ matrix.ansible_core_next }}
python3 -m pip install ansible-core>=${{ matrix.ansible_core }}a1,${{ matrix.ansible_core_next }}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious and want to test it out of curiosity, let me get back to you on that.


- name: "Run sanity tests for ${{ matrix.name }}"
run: |
ansible-playbook -vv tests/collection-tests.yaml -e 'galaxy_requirements="{{ playbook_dir | dirname }}/${{ matrix.ansible_major }}/galaxy-requirements.yaml"'
working-directory: ansible-build-data
49 changes: 49 additions & 0 deletions tests/collection-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
- name: Run tests on packaged collections
hosts: localhost
gather_facts: false
vars:
galaxy_requirements: "{{ playbook_dir | dirname }}/6/galaxy-requirements.yaml"
collections_path: /tmp/ansible_collections
tasks:
- name: Get list of installed packages to verify if podman is installed
package_facts:
manager: "auto"
# This is noisy (and potentially sensitive) to print on stdout
no_log: true

# This is so we can otherwise run unprivileged if podman is already installed
- when: "'podman' not in ansible_facts['packages'].keys()"
become: true
block:
- name: Install podman
package:
name: podman
state: present
rescue:
- name: Could not install podman
fail:
msg: |
Failed to elevate privileges and install podman.
Install podman manually or run ansible-playbook with elevated privileges.

- name: Install collections from galaxy
environment:
ANSIBLE_COLLECTIONS_PATH: "{{ collections_path }}"
command: ansible-galaxy collection install -r {{ galaxy_requirements }}
args:
creates: "{{ collections_path }}"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to add a retry here because it can (and will) fail like this:

 ________________________________________   
< TASK [Install collections from galaxy] > 
 ----------------------------------------
        \   ^__^            
         \  (oo)\_______    
            (__)\       )\/\
                ||----w |                                                      
                ||     ||
                                                                               
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["ansible-galaxy", "collection", "install", "-r", "/root/src/ansible-build-data/6/galaxy-requirements.yaml"], "delta": "0:00:28.981410", "end": "2022-05-17 19:47:26.388533", "msg": "non-zero return code", "rc": 1, "start": "2022-05-17 19:46:57.407123", "stderr": 
"ERROR! Error when getting the collection info for cloud.common from default (https://galaxy.ansible.com/api/) (HTTP Code: 502, Message: Bad Gateway Code: Unknown)", "stderr_lines": ["ERROR! Error when getting the collection info for cloud.common from default (https://galaxy.ansible.com/api/) (HTTP Code: 502, Message:
 Bad Gateway Code: Unknown)"], "stdout": "Starting galaxy collection install process\nProcess install dependency map", "stdout_lines": ["Starting galaxy collection install process", "Process install dependency map"]}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


# ansible.builtin.find doesn't have mindepth
# https://github.com/ansible/ansible/issues/36369
- name: Find collection directories
command: find {{ collections_path }} -mindepth 2 -maxdepth 2 -type d
changed_when: false
register: _collection_directories

# Tests are broken up such that there is a task per collection (instead of one very long task)
- name: Run sanity tests
include_tasks: tasks/sanity-tests.yaml
loop: "{{ _collection_directories.stdout_lines }}"
loop_control:
loop_var: _collection_directory
11 changes: 11 additions & 0 deletions tests/tasks/sanity-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
- name: "Run sanity tests for {{ _collection_name }}"
vars:
_collection_name: >-
{%- set _namespace = _collection_directory.split('/')[-2] -%}
{%- set _name = _collection_directory.split('/')[-1] -%}
{{ _namespace }}.{{ _name }}
command: ansible-test sanity --skip-test pylint --docker
changed_when: false
ignore_errors: true
args:
chdir: "{{ _collection_directory }}"