From c96978a2529b49d01afe87c1f7df4b42cc166d4e Mon Sep 17 00:00:00 2001 From: David Moreau Simard Date: Thu, 12 May 2022 09:30:00 -0400 Subject: [PATCH] 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). --- .github/workflows/sanity-tests.yml | 43 ++++++++++++++++++++++++++ tests/collection-tests.yaml | 49 ++++++++++++++++++++++++++++++ tests/tasks/sanity-tests.yaml | 11 +++++++ 3 files changed, 103 insertions(+) create mode 100644 .github/workflows/sanity-tests.yml create mode 100644 tests/collection-tests.yaml create mode 100644 tests/tasks/sanity-tests.yaml diff --git a/.github/workflows/sanity-tests.yml b/.github/workflows/sanity-tests.yml new file mode 100644 index 00000000000..a36b8c6be29 --- /dev/null +++ b/.github/workflows/sanity-tests.yml @@ -0,0 +1,43 @@ +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: + matrix: + include: + - name: latest Ansible 5 release + options: '-e galaxy_requirements="{{ playbook_dir | dirname }}/5/galaxy-requirements.yaml"' + - name: latest Ansible 6 release + options: '-e galaxy_requirements="{{ playbook_dir | dirname }}/6/galaxy-requirements.yaml"' + + 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 dependencies + run: | + python3 -m pip install --upgrade pip + python3 -m pip install ansible-core + + - name: "Run sanity tests for ${{ matrix.name }}" + run: | + ansible-playbook -vv tests/collection-tests.yaml ${{ matrix.options }} + working-directory: ansible-build-data diff --git a/tests/collection-tests.yaml b/tests/collection-tests.yaml new file mode 100644 index 00000000000..dd4568a228b --- /dev/null +++ b/tests/collection-tests.yaml @@ -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 }}" + + # 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 diff --git a/tests/tasks/sanity-tests.yaml b/tests/tasks/sanity-tests.yaml new file mode 100644 index 00000000000..b1a0cb0dd39 --- /dev/null +++ b/tests/tasks/sanity-tests.yaml @@ -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 }}"