Skip to content

Commit

Permalink
Merge pull request #17 from gardar/ci/testing
Browse files Browse the repository at this point in the history
feat: role/collection testing with ansible-test and molecule
  • Loading branch information
SuperQ committed Feb 1, 2023
2 parents 1e41dec + c664e63 commit 5f93f50
Show file tree
Hide file tree
Showing 84 changed files with 465 additions and 977 deletions.
2 changes: 2 additions & 0 deletions .config/ansible-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
profile: production
60 changes: 60 additions & 0 deletions .config/molecule/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
dependency:
name: galaxy
driver:
name: docker
lint: |
set -e
yamllint -c ${YAMLLINT_CONFIG_FILE} .
ansible-lint
platforms:
- name: almalinux-8
image: dokken/almalinux-8
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
- name: centos-7
image: dokken/centos-7
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /usr/lib/systemd/systemd
- name: centos-stream-8
image: dokken/centos-stream-8
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
- name: debian-9
image: dokken/debian-9
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
- name: debian-10
image: dokken/debian-10
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
- name: debian-11
image: dokken/debian-11
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
- name: fedora-30
image: dokken/fedora-30
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
- name: ubuntu-18.04
image: dokken/ubuntu-18.04
pre_build_image: true
privileged: true
cgroup_parent: docker.slice
command: /lib/systemd/systemd
verifier:
name: testinfra
23 changes: 23 additions & 0 deletions .config/yamllint/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
extends: default

rules:
braces:
max-spaces-inside: 1
level: error
brackets:
max-spaces-inside: 1
level: error
colons:
max-spaces-after: -1
level: error
commas:
max-spaces-after: -1
level: error
empty-lines:
max: 3
level: error
hyphens:
level: error
line-length:
max: 160
98 changes: 98 additions & 0 deletions .github/workflows/ansible-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
name: Ansible CI
on: [push, pull_request]

env:
ANSIBLE_FORCE_COLOR: true

jobs:
ansible-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Lint collection
uses: ansible/ansible-lint-action@main

discover-ansible-versions:
runs-on: ubuntu-latest
outputs:
versions: ${{ steps.supported-ansible-versions.outputs.result }}
steps:
- uses: actions/checkout@v3

- name: Get Ansible versions that the collection supports
id: supported-ansible-versions
uses: mikefarah/yq@master
with:
cmd: yq -o json -I=0 '.requires_ansible | split(",") | .[] |= sub("(.*)", "stable-${1}")' meta/runtime.yml

ansible-test-sanity:
uses: ./.github/workflows/ansible-test-sanity.yml
needs:
- ansible-lint
- discover-ansible-versions
with:
ansible-core-versions: ${{ needs.discover-ansible-versions.outputs.versions }}

discover-ansible-tests:
runs-on: ubuntu-latest
needs: ansible-lint
outputs:
molecule-tests: ${{ steps.set-molecule-tests.outputs.tests }}
integration-tests: ${{ steps.set-integration-tests.outputs.tests }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Get changed roles
id: changed-roles
uses: tj-actions/changed-files@v35
with:
path: "roles"
diff_relative: "true"
files: "**"
dir_names: "true"
dir_names_max_depth: "1"
since_last_remote_commit: "true"

- name: Discover role tests
if: steps.changed-roles.outputs.any_changed == 'true'
id: set-molecule-tests
run: |
echo tests="[`for role in ${{ steps.changed-roles.outputs.all_changed_and_modified_files }}; do
for test in $(find tests/integration/targets -maxdepth 1 -mindepth 1 -type d -iname "molecule-${role}-*" -printf "%f\n"); do
echo '{"test":\"'"${test}"'\","name":\"'"${test#*-}\"'"}';
done
done | tr '\n' ',' | sed '$s/,$//'`]" >> $GITHUB_OUTPUT
- name: Discover integration tests
id: set-integration-tests
run: |
echo tests="[`for test in $(find tests/integration/targets -maxdepth 1 -mindepth 1 -type d -not -iname "molecule-*" -printf "%f\n"); do
echo '{"test":\"'"${test}"'\","name":\"'"${test}\"'"}';
done | tr '\n' ',' | sed '$s/,$//'`]" >> $GITHUB_OUTPUT
ansible-test-molecule:
uses: ./.github/workflows/ansible-test-integration.yml
needs:
- discover-ansible-tests
- discover-ansible-versions
if: needs.discover-ansible-tests.outputs.molecule-tests != '[]' &&
needs.discover-ansible-tests.outputs.molecule-tests != ''
with:
targets: ${{ needs.discover-ansible-tests.outputs.molecule-tests }}
ansible-core-versions: ${{ needs.discover-ansible-versions.outputs.versions }}

ansible-test-integration:
uses: ./.github/workflows/ansible-test-integration.yml
needs:
- discover-ansible-tests
- discover-ansible-versions
if:
needs.discover-ansible-tests.outputs.integration-tests != '[]' &&
needs.discover-ansible-tests.outputs.integration-tests != ''
with:
targets: ${{ needs.discover-ansible-tests.outputs.integration-tests }}
ansible-core-versions: ${{ needs.discover-ansible-versions.outputs.versions }}
18 changes: 0 additions & 18 deletions .github/workflows/ansible-lint.yml

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/ansible-test-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
name: Ansible Integration Test
on:
workflow_call:
inputs:
targets:
required: true
type: string
ansible-core-versions:
required: false
default: '["stable-2.14"]'
type: string
target-python-versions:
required: false
default: '["default"]'
type: string

jobs:
ansible-test-integration:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
targets: ${{ fromJson(inputs.targets) }}
ansible-core-versions: ${{ fromJson(inputs.ansible-core-versions) }}
target-python-versions: ${{ fromJson(inputs.target-python-versions) }}

name: "${{ matrix.targets.name }}-${{ matrix.ansible-core-versions }}"
steps:
- name: "Perform ${{ matrix.targets.test }} integration test with ansible-test"
uses: ansible-community/ansible-test-gh-action@release/v1
with:
ansible-core-version: ${{ matrix.ansible-core-versions }}
target-python-version: ${{ matrix.target-python-versions }}
controller-python-version: auto
docker-image: "--docker-privileged" # Workaround because ansible-test action doesn't support setting privileged flag
testing-type: integration
target: ${{ matrix.targets.test }}
# coverage: never # https://github.com/ansible-community/ansible-test-gh-action/pull/50
25 changes: 25 additions & 0 deletions .github/workflows/ansible-test-sanity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
name: Ansible Sanity Test
on:
workflow_call:
inputs:
ansible-core-versions:
required: false
default: '["stable-2.14"]'
type: string

jobs:
ansible-test-sanity:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ansible-core-versions: ${{ fromJson(inputs.ansible-core-versions) }}

name: "sanity-${{ matrix.ansible-core-versions }}"
steps:
- name: "Perform sanity test with ansible-test"
uses: ansible-community/ansible-test-gh-action@release/v1
with:
ansible-core-version: ${{ matrix.ansible-core-versions }}
testing-type: sanity
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
namespace: prometheus
name: prometheus
version: 0.0.1
version: 1.0.0

readme: README.md
authors:
Expand Down
2 changes: 1 addition & 1 deletion meta/runtime.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
requires_ansible: '>=2.9.10'
requires_ansible: "2.9,2.10,2.11,2.12,2.13,2.14"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
hosts: all
any_errors_fatal: true
roles:
- alertmanager
- prometheus.prometheus.alertmanager
vars:
alertmanager_binary_local_dir: '/tmp/alertmanager-linux-amd64'
alertmanager_config_dir: /opt/am/etc
Expand Down
69 changes: 0 additions & 69 deletions roles/alertmanager/molecule/alternative/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,70 +1 @@
---
dependency:
name: galaxy
driver:
name: docker
platforms:
- name: bionic
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:ubuntu-18.04
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: xenial
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:ubuntu-16.04
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: stretch
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:debian-9
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: buster
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:debian-10
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: centos7
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:centos-7
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- name: centos8
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:centos-8
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
groups:
- python3
- name: fedora
pre_build_image: true
image: quay.io/paulfantom/molecule-systemd:fedora-30
docker_host: "${DOCKER_HOST:-unix://var/run/docker.sock}"
privileged: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:ro
groups:
- python3
provisioner:
name: ansible
playbooks:
prepare: prepare.yml
converge: playbook.yml
inventory:
group_vars:
python3:
ansible_python_interpreter: /usr/bin/python3
verifier:
name: testinfra
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

import os
import testinfra.utils.ansible_runner
import pytest

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
hosts: all
any_errors_fatal: true
roles:
- alertmanager
- prometheus.prometheus.alertmanager
vars:
alertmanager_slack_api_url: "http://example.com"
alertmanager_receivers:
Expand Down
Loading

0 comments on commit 5f93f50

Please sign in to comment.