|
- # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
- #
- # SPDX-License-Identifier: AGPL-3.0-or-later
-
- ---
- # What this proves: the bridge starts, reads the configuration and registration
- # the role rendered, opens its appservice port, and is the version the role
- # pins. It also proves the role composed the avatar-proxy labels out of the
- # hostname, scheme and path prefix it was given. It does NOT bridge anything -
- # there is no Discord on the other side and there is deliberately never going to
- # be one, because that would need a Discord account. See
- # docs/molecule-testing.md.
- - name: Verify mautrix-discord
- hosts: all
- become: true
- vars_files:
- - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/vars.yml"
- - "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/../../../molecule-shared/playbook-context.yml"
- gather_facts: false
-
- vars:
- # The role derives this from scheme + hostname + path prefix, and role
- # defaults are out of scope in this play, so it is recomposed here from the
- # same three values the scenario pinned in molecule.yml.
- mautrix_discord_expected_public_address: >-
- {{ matrix_bridge_mautrix_discord_scheme }}://{{ matrix_bridge_mautrix_discord_hostname }}{{ matrix_bridge_mautrix_discord_path_prefix }}
- mautrix_discord_expected_avatar_proxy_path_prefix: "{{ matrix_bridge_mautrix_discord_path_prefix }}/mautrix-discord/avatar"
-
- tasks:
- # Read from the role's own defaults rather than pinned in molecule.yml, so
- # the version assertion below compares the running image against what the
- # role ships instead of against the scenario itself.
- - name: Load the role's defaults under a separate name
- ansible.builtin.include_vars:
- file: "{{ lookup('env', 'MOLECULE_PROJECT_DIRECTORY') }}/defaults/main.yml"
- name: mautrix_discord_role_defaults
-
- - name: Wait for the mautrix-discord service to become active
- ansible.builtin.systemd_service:
- name: matrix-mautrix-discord.service
- register: mautrix_discord_service
- until: mautrix_discord_service.status.ActiveState == 'active'
- retries: 30
- delay: 5
- failed_when: false
-
- # `Restart=always` means a bridge crash-looping on a configuration it cannot
- # read still reports `active`, so the restart counter is checked too. It is
- # asserted `is defined` because `| int` turns a missing property into 0 and
- # would pass vacuously.
- - name: Assert the service is active and has not been restarting
- ansible.builtin.assert:
- that:
- - mautrix_discord_service.status.ActiveState == 'active'
- - mautrix_discord_service.status.NRestarts is defined
- - mautrix_discord_service.status.NRestarts | int == 0
- fail_msg: >-
- matrix-mautrix-discord.service is
- {{ mautrix_discord_service.status.ActiveState | default('unknown') }}
- after {{ mautrix_discord_service.status.NRestarts | default('?') }}
- automatic restart(s)
- success_msg: "matrix-mautrix-discord.service is active and has not restarted"
-
- # The appservice port is the bridge's own listener, the one a homeserver
- # would push transactions to. It opening at all means the bridge got through
- # reading its configuration and setting itself up.
- - name: Wait for the bridge to open its appservice port
- ansible.builtin.command:
- argv:
- - docker
- - run
- - --rm
- - --network={{ matrix_bridge_mautrix_discord_container_network }}
- - "{{ molecule_shared_image_curl }}"
- - --silent
- - --output
- - /dev/null
- - --write-out
- - "HTTP_STATUS=%{http_code}"
- - "http://matrix-mautrix-discord:8080/_matrix/mau/live"
- register: mautrix_discord_live
- changed_when: false
- until: "'HTTP_STATUS=000' not in mautrix_discord_live.stdout"
- retries: 24
- delay: 5
- failed_when: false
-
- - name: Assert the bridge answers on its appservice port
- ansible.builtin.assert:
- that:
- - "'HTTP_STATUS=000' not in mautrix_discord_live.stdout"
- fail_msg: >-
- The bridge did not answer on its appservice port
- ({{ mautrix_discord_live.stdout | default('no output') }})
- success_msg: "The bridge answers on its appservice port"
-
- - name: Read the configuration the role rendered
- ansible.builtin.slurp:
- src: "{{ matrix_bridge_mautrix_discord_config_path }}/config.yaml"
- register: mautrix_discord_config_file
-
- # Each of these differs from what the bridge would use on its own, so their
- # presence means the role's configuration is what the bridge is running on
- # rather than something that happened to agree with it. The public address
- # in particular is composed by the role out of three separate variables.
- #
- # Asserted against the parsed document rather than by substring, so a value
- # landing under the wrong key cannot pass.
- - name: Assert the rendered configuration carries this scenario's values
- ansible.builtin.assert:
- that:
- - mautrix_discord_config.homeserver.address == matrix_bridge_mautrix_discord_homeserver_address
- - mautrix_discord_config.homeserver.domain == matrix_bridge_mautrix_discord_homeserver_domain
- - mautrix_discord_config.appservice.bot.username == matrix_bridge_mautrix_discord_appservice_bot_username
- - mautrix_discord_config.appservice.as_token == matrix_bridge_mautrix_discord_appservice_token
- - mautrix_discord_config.appservice.hs_token == matrix_bridge_mautrix_discord_homeserver_token
- - mautrix_discord_config.appservice.database.type == 'sqlite3'
- - mautrix_discord_config.bridge.command_prefix == matrix_bridge_mautrix_discord_bridge_command_prefix
- - mautrix_discord_config.bridge.avatar_proxy_key == matrix_bridge_mautrix_discord_bridge_avatar_proxy_key
- - mautrix_discord_config.bridge.public_address == mautrix_discord_expected_public_address | trim
- - mautrix_discord_config.logging.min_level == matrix_bridge_mautrix_discord_logging_level
- fail_msg: "The rendered configuration does not carry the scenario's values"
- success_msg: "The rendered configuration carries the scenario's values"
- vars:
- mautrix_discord_config: "{{ mautrix_discord_config_file.content | b64decode | from_yaml }}"
-
- # The registration file is the half of the appservice handshake the
- # homeserver reads, and it is generated by the role rather than by the
- # bridge, so it is worth checking on its own. `sender_localpart` is the
- # role's own `_bot_` prefixing, not something the bridge would produce.
- - name: Read the appservice registration the role rendered
- ansible.builtin.slurp:
- src: "{{ matrix_bridge_mautrix_discord_config_path }}/registration.yaml"
- register: mautrix_discord_registration_file
-
- - name: Assert the registration carries the scenario's tokens and bot user
- ansible.builtin.assert:
- that:
- - mautrix_discord_registration.as_token == matrix_bridge_mautrix_discord_appservice_token
- - mautrix_discord_registration.hs_token == matrix_bridge_mautrix_discord_homeserver_token
- - mautrix_discord_registration.sender_localpart == '_bot_' ~ matrix_bridge_mautrix_discord_appservice_bot_username
- - mautrix_discord_registration.url == 'http://matrix-mautrix-discord:8080'
- - mautrix_discord_bot_user_regex in (mautrix_discord_registration.namespaces.users | map(attribute='regex') | list)
- fail_msg: "The appservice registration does not carry the scenario's tokens and bot user"
- success_msg: "The appservice registration carries the scenario's tokens and bot user"
- vars:
- mautrix_discord_registration: "{{ mautrix_discord_registration_file.content | b64decode | from_yaml }}"
- mautrix_discord_bot_user_regex: "^@{{ matrix_bridge_mautrix_discord_appservice_bot_username | regex_escape }}:{{ matrix_bridge_mautrix_discord_homeserver_domain | regex_escape }}$"
-
- # sqlite was chosen in molecule.yml, so the bridge should have created its
- # database under the role's data path. This is the cheap proof that the data
- # path reached the process and is writable by the uid the role runs it as.
- - name: Look for the bridge's sqlite database under the role's data path
- ansible.builtin.stat:
- path: "{{ matrix_bridge_mautrix_discord_data_path }}/mautrix-discord.db"
- register: mautrix_discord_database
-
- - name: Assert the bridge created its database where the role put its data path
- ansible.builtin.assert:
- that:
- - mautrix_discord_database.stat.exists
- - mautrix_discord_database.stat.uid | int == matrix_user_uid | int
- fail_msg: >-
- The bridge did not create its database under
- {{ matrix_bridge_mautrix_discord_data_path }}, or it is not owned by
- uid {{ matrix_user_uid }}
- success_msg: "The bridge created its database under the role's data path, as the role's uid"
-
- - name: Read the image of the running container
- ansible.builtin.command:
- argv:
- - docker
- - container
- - inspect
- - matrix-mautrix-discord
- - --format
- - "{{ '{{' }} .Config.Image {{ '}}' }}"
- register: mautrix_discord_image
- changed_when: false
-
- - name: Assert the running container is the version defaults/main.yml pins
- ansible.builtin.assert:
- that:
- - mautrix_discord_role_defaults.matrix_bridge_mautrix_discord_version in mautrix_discord_image.stdout
- fail_msg: >-
- The running container is {{ mautrix_discord_image.stdout }}, which does
- not carry the pinned version
- {{ mautrix_discord_role_defaults.matrix_bridge_mautrix_discord_version }}
- success_msg: "The running container is the version defaults/main.yml pins"
-
- # The avatar proxy is this role's own reverse-proxy wiring: the labels only
- # appear because a public address was configured, and their hostname and
- # path prefix are composed by the role rather than copied from a variable.
- - name: Read the labels the role rendered
- ansible.builtin.slurp:
- src: "{{ matrix_bridge_mautrix_discord_base_path }}/labels"
- register: mautrix_discord_labels
-
- - name: Assert the avatar-proxy labels were composed from the scenario's hostname and path prefix
- ansible.builtin.assert:
- that:
- - "'traefik.enable=true' in mautrix_discord_labels_rendered"
- - "'traefik.docker.network=' ~ matrix_bridge_mautrix_discord_container_network in mautrix_discord_labels_rendered"
- - "'traefik.http.routers.matrix-mautrix-discord-avatar-proxy.rule=Host(`' ~ matrix_bridge_mautrix_discord_hostname ~ '`) && PathPrefix(`' ~ mautrix_discord_expected_avatar_proxy_path_prefix ~ '`)' in mautrix_discord_labels_rendered"
- - "'traefik.http.middlewares.matrix-mautrix-discord-strip-prefix.stripprefix.prefixes=' ~ matrix_bridge_mautrix_discord_path_prefix in mautrix_discord_labels_rendered"
- fail_msg: >-
- The avatar-proxy labels do not carry the scenario's hostname
- ({{ matrix_bridge_mautrix_discord_hostname }}) and path prefix
- ({{ mautrix_discord_expected_avatar_proxy_path_prefix }})
- success_msg: "The avatar-proxy labels were composed from the scenario's hostname and path prefix"
- vars:
- mautrix_discord_labels_rendered: "{{ mautrix_discord_labels.content | b64decode }}"
-
- # The label file is fed to `docker create --label-file`, so a label the role
- # renders wrongly is not merely cosmetic - it would stop the container from
- # being created at all. Reading them back off the running container proves
- # Docker accepted them.
- - name: Read the labels Docker attached to the running container
- ansible.builtin.command:
- argv:
- - docker
- - container
- - inspect
- - matrix-mautrix-discord
- - --format
- - "{{ '{{' }} index .Config.Labels \"traefik.http.routers.matrix-mautrix-discord-avatar-proxy.rule\" {{ '}}' }}"
- register: mautrix_discord_container_label
- changed_when: false
-
- - name: Assert Docker carries the avatar-proxy router label the role rendered
- ansible.builtin.assert:
- that:
- - matrix_bridge_mautrix_discord_hostname in mautrix_discord_container_label.stdout
- - mautrix_discord_expected_avatar_proxy_path_prefix in mautrix_discord_container_label.stdout
- fail_msg: >-
- The running container's avatar-proxy router label is
- "{{ mautrix_discord_container_label.stdout }}"
- success_msg: "The running container carries the avatar-proxy router label the role rendered"
|