Files
matrix-docker-ansible-deploy/roles/custom/matrix-alertmanager-receiver/molecule/default/prepare.yml
T
Slavi PantaleevandClaude Opus 5 ebe13bc360 Add a Molecule scenario for matrix-alertmanager-receiver
Not for merging as-is. One role's scenario plus the workflow that would
make per-role testing affordable here, so we can see how it behaves before
deciding whether to do the other 69.

Unlike the MASH repositories, one repository holds every role, so running
everything on every push is not an option. The workflow's first job works
out which roles a push touched and builds the matrix from that; a change to
docs, or to a role with no scenario yet, runs nothing.

Three things this role needed that a MASH role does not:

- The variables matrix-base would supply (matrix_base_data_path,
  matrix_domain, matrix_user_name, matrix_group_name and the uid/gid) have
  to be provided by the scenario, and the user and group have to exist
  before the role's file tasks run.
- The service contacts the homeserver while starting up - it fetches
  /_matrix/client/v3/joined_rooms to resolve its room mapping and exits 1
  if that fails - so prepare.yml stands up a stub homeserver. Most roles
  here are bridges and bots, so this is likely the rule rather than the
  exception.
- verify.yml runs as its own play, where role defaults are out of scope, so
  the paths it reads are pinned in molecule.yml. The version is deliberately
  NOT pinned: it is read from defaults/main.yml so the assertion compares
  the running image against what the role ships.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 18:02:53 +03:00

127 lines
4.5 KiB
YAML

# SPDX-FileCopyrightText: 2026 Slavi Pantaleev
#
# SPDX-License-Identifier: AGPL-3.0-or-later
---
- name: Prepare matrix-alertmanager-receiver Molecule tests
hosts: all
become: true
gather_facts: true
tasks:
- name: Ensure apt cache is updated
ansible.builtin.apt:
update_cache: true
cache_valid_time: 600
when: ansible_os_family == 'Debian'
- name: Ensure required packages are installed
ansible.builtin.package:
name:
- python3-requests
- fuse-overlayfs
state: present
- name: Ensure Docker is installed
ansible.builtin.include_role:
name: ansible-role-docker
vars:
docker_daemon_options:
storage-driver: fuse-overlayfs
# The role's file tasks set owner/group by name, and Ansible resolves those
# through the passwd database - so they have to exist before it runs. In a
# real deployment `matrix-base` creates them.
- name: Ensure the matrix group exists
ansible.builtin.group:
name: "{{ matrix_group_name }}"
gid: "{{ matrix_user_gid }}"
state: present
- name: Ensure the matrix user exists
ansible.builtin.user:
name: "{{ matrix_user_name }}"
uid: "{{ matrix_user_uid }}"
group: "{{ matrix_group_name }}"
create_home: false
system: true
state: present
- name: Ensure the base data path exists
ansible.builtin.file:
path: "{{ matrix_base_data_path }}"
state: directory
owner: "{{ matrix_user_name }}"
group: "{{ matrix_group_name }}"
mode: "0750"
- name: Ensure the container network the role attaches to exists
ansible.builtin.command:
argv:
- docker
- network
- create
- "{{ matrix_alertmanager_receiver_container_network }}"
register: matrix_alertmanager_receiver_molecule_network
changed_when: matrix_alertmanager_receiver_molecule_network.rc == 0
failed_when:
- matrix_alertmanager_receiver_molecule_network.rc != 0
- "'already exists' not in matrix_alertmanager_receiver_molecule_network.stderr"
# matrix-alertmanager-receiver contacts the homeserver while starting up -
# it fetches /_matrix/client/v3/joined_rooms to resolve its room mapping -
# and exits 1 if that fails. So a homeserver has to exist for the service
# to come up at all. A stub is enough: the scenario is testing this role,
# not Synapse, and it keeps the run offline and fast.
- name: Ensure the Matrix homeserver stub script exists
ansible.builtin.copy:
dest: /root/matrix-homeserver-stub.py
mode: "0755"
content: |
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
ROOMS = {"joined_rooms": ["{{ matrix_alertmanager_receiver_config_matrix_room_mapping['molecule-room'] }}"]}
class Handler(BaseHTTPRequestHandler):
def _send(self, payload):
body = json.dumps(payload).encode()
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def do_GET(self):
if self.path.endswith("/joined_rooms"):
self._send(ROOMS)
else:
self._send({})
def do_POST(self):
self._send({"event_id": "$molecule-event-id"})
def log_message(self, *args):
pass
HTTPServer(("0.0.0.0", 8008), Handler).serve_forever()
- name: Ensure the Matrix homeserver stub is running on the role's network
ansible.builtin.command:
argv:
- docker
- run
- --detach
- --rm
- --name=matrix-homeserver-stub
- --network={{ matrix_alertmanager_receiver_container_network }}
- --network-alias=matrix.molecule.local
- --volume=/root/matrix-homeserver-stub.py:/stub.py:ro
- docker.io/library/python:3.13-alpine
- python3
- /stub.py
register: matrix_alertmanager_receiver_molecule_stub
changed_when: matrix_alertmanager_receiver_molecule_stub.rc == 0
failed_when:
- matrix_alertmanager_receiver_molecule_stub.rc != 0
- "'already in use' not in matrix_alertmanager_receiver_molecule_stub.stderr"