Skip to content
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

Set up molecule testing #3

Merged
merged 10 commits into from
Nov 24, 2023
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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this limits molecule to run only mirsg.infrastructure.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}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

if we don't define MOLECULE_RUN_TAGS, all converge steps are performed

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
Comment on lines +7 to +10
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we will need to add the other roles here, and probably target specific groups for some of them

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:
Loading