Files @ c322cfcb5e2d
Branch filter:

Location: majic-ansible-roles/roles/xmpp_server/molecule/default/tests/conftest.py

branko
MAR-242: Refactor file upload tests:

- Use fixtures for grabbing the server host and cleaning up the
uploads directory. Deduplicates the test code a bit as well.
import os
import time

import pytest

import testinfra.utils.ansible_runner


@pytest.fixture
def server_host(host, server):
    """
    Helper fixture that returns (Ansible-backed) server host based
    on the passed-in server name and calling host's distribution
    release.

    For example, if the server name is 'paramters-mandatory', and the
    calling host distribution is 'bookworm', the fixture will look for
    server called 'parameters-mandatory-bookworm' in the Ansible
    inventory.
    """

    ansible_facts = host.ansible("setup")["ansible_facts"]
    ansible_distribution_release = ansible_facts['ansible_distribution_release']
    ansible_runner = testinfra.utils.ansible_runner.AnsibleRunner(os.environ['MOLECULE_INVENTORY_FILE'])
    server_host = ansible_runner.get_host(ansible_runner.get_hosts(f'{server}-{ansible_distribution_release}')[0])

    return server_host


@pytest.fixture
def server_clean_domain_uploads(server_host, domain):
    """
    Helper fixture that cleans the XMPP (Prosody) server's HTTP
    file uploads directory for the specified domain. This will wipe
    both the files and the stored upload-related metadata.

    The XMPP server can be considered fully operational once the
    fixture finishes the clean-up.
    """

    with server_host.sudo():
        server_host.run("rm -rf %s/*", f"/var/lib/prosody/upload%2e{domain}/")
        server_host.run("systemctl restart prosody")

        # Wait for Prosody to become available. Time-out after one
        # second.
        attempts = 1
        while server_host.run("prosodyctl status").rc != 0:
            time.sleep(0.1)
            attempts += 1
            if attempts > 10:
                raise Exception("Prosody failed to restart after file upload directory cleanup.")