Skip to content

Commit

Permalink
Set up molecule testing (#3)
Browse files Browse the repository at this point in the history
- Add a `centos7` molecule scenario
(`tests/molecule/centos7/molecule.yml`) - it has two instances, one for
a database and another for a web server
- Add an inventory for testing
(`tests/molecule/resources/inventory/hosts.yml`)
- Add group vars for testing
(`tests/molecule/resources/inventory/group_vars/all.yml`)
- Add a converge playbook for testing
(`tests/molecule/resources/converge.yml`). This playbook uses tags to
limit which roles are included. This means we can run molecule only on
roles that have changed in a given pr.
- Add a workflow to run molecule on `mirsg.infrastructure.provision`
when the provision role has changed
- Add a requirements file so we can install this collection

Edit: @drmatthews after talking offline, we'll use a single instance for
running molecule tests rather than two (database + web server)
  • Loading branch information
p-j-smith authored Nov 24, 2023
1 parent b95116d commit f7d3661
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
39 changes: 39 additions & 0 deletions .github/workflows/molecule-provision.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test provision
on:
pull_request:
paths:
- "roles/provision/**"

jobs:
molecule:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
scenario:
- centos7
env:
MOLECULE_RUN_TAGS: provision
PY_COLORS: 1
ANSIBLE_FORCE_COLOR: 1
steps:
- name: Check out the codebase
uses: actions/checkout@v4
with:
path: ansible_collections/mirsg/infrastructure

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.11

- name: Install test dependencies
run: |
sudo apt-get update && sudo apt-get -y install rsync
python3 -m pip install --upgrade pip
python3 -m pip install ansible molecule molecule-plugins[docker] docker requests
- name: Test with molecule
run: |
cd ansible_collections/mirsg/infrastructure/tests
molecule test --scenario-name "${{ matrix.scenario }}"
7 changes: 7 additions & 0 deletions meta/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
collections:
- community.general
- ansible.posix
- community.postgresql
- community.docker
- community.crypto
2 changes: 1 addition & 1 deletion roles/provision/tasks/Rocky.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
- name: Install the ca-certificates package
- name: Ensure the ca-certificates package is installed
become: true
ansible.builtin.yum:
name: "ca-certificates"
Expand Down
62 changes: 62 additions & 0 deletions tests/molecule/centos7/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
dependency:
name: galaxy
options:
force: true
role-file: "${MOLECULE_SCENARIO_DIRECTORY}/../../../meta/requirements.yml"
requirements-file: "${MOLECULE_SCENARIO_DIRECTORY}/../../../meta/requirements.yml"

driver:
name: docker

platforms:
- name: instance
hostname: molecule.instnace.local
image: ${MOLECULE_DOCKER_IMAGE:-geerlingguy/docker-centos7-ansible:latest}
required: true
command: ""
cgroupns_mode: host
privileged: true
pre_build_image: ${MOLECULE_PRE_BUILD_IMAGE:-true}
volumes:
- ./molecule-data:/storage/molecule
keep_volumes: false
groups:
- all
- molecule
- centos7
docker_networks:
- name: molecule
ipam_config:
- subnet: "192.168.56.0/24"
gateway: "192.168.56.1"
networks:
- name: molecule
ipv4_address: "192.168.56.2"

provisioner:
name: ansible
log: true
config_options:
defaults:
callbacks_enabled: profile_tasks, timer, yaml
tags:
run: ${MOLECULE_RUN_TAGS:-all}
inventory:
links:
hosts: ../resources/inventory/hosts.yml
group_vars: ../resources/inventory/group_vars/
playbooks:
converge: ../resources/converge.yml
env:
ANSIBLE_VERBOSITY: "1"

verifier:
name: ansible
env:
ANSIBLE_VERBOSITY: "1"

lint: |
set -e
yamllint .
ansible-lint .
10 changes: 10 additions & 0 deletions tests/molecule/resources/converge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- name: Provision infrastructure
hosts: all
become: true
gather_facts: true
roles:
- role: mirsg.infrastructure.provision

Check failure on line 7 in tests/molecule/resources/converge.yml

View workflow job for this annotation

GitHub Actions / linting

syntax-check[specific]

the role 'mirsg.infrastructure.provision' was not found in /home/runner/work/ansible-collection-infra/ansible-collection-infra/tests/molecule/resources/roles:/home/runner/.cache/ansible-compat/b1a9ef/roles:/home/runner/work/ansible-collection-infra/ansible-collection-infra/roles:/home/runner/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles:/home/runner/work/ansible-collection-infra/ansible-collection-infra/tests/molecule/resources
tags: provision
- role: mirsg.infrastructure.install_python
tags: python
5 changes: 5 additions & 0 deletions tests/molecule/resources/inventory/group_vars/all.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
EXTERNAL_STORAGE_DRIVE: "/storage/molecule"

# mirsg.infrastructure.provision
server_locale: "en_GB.UTF-8"
17 changes: 17 additions & 0 deletions tests/molecule/resources/inventory/hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
all:
# List of all servers, defining their alias and IP (hostname)
hosts:
# Host for your database server. Variables in host_vars/instance will be available to this host
instance:
hostname: "molecule.instance.local"
ansible_ip: "192.168.56.2"
ansible_port: 22

# Ansible groups. Groups allow configuration and variables to be shared between hosts
# Variables in group_vars/all will be shared between all hosts
children:
# All molecule servers. Variables in group_vars/molecule will be shared between these hosts
molecule:
hosts:
instance:

0 comments on commit f7d3661

Please sign in to comment.