diff --git a/roles/xmpp_server/molecule/default/tests/conftest.py b/roles/xmpp_server/molecule/default/tests/conftest.py new file mode 100644 index 0000000000000000000000000000000000000000..2625c7875cd14aad0c588f32cfb8cd8e9f0087f1 --- /dev/null +++ b/roles/xmpp_server/molecule/default/tests/conftest.py @@ -0,0 +1,52 @@ +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.") diff --git a/roles/xmpp_server/molecule/default/tests/test_client.py b/roles/xmpp_server/molecule/default/tests/test_client.py index 7925f1e4f7552b708133eff6d4d4c267fba502c1..5f3f5c07a72992db2b56f07ce8ebe1a4a7357ea4 100644 --- a/roles/xmpp_server/molecule/default/tests/test_client.py +++ b/roles/xmpp_server/molecule/default/tests/test_client.py @@ -1,5 +1,4 @@ import os -import time import uuid import pytest @@ -108,29 +107,19 @@ def test_unauthorized_users_rejected(host, target_username, target_domain): ["jane.doe", "janepassword", "domain2", "parameters-optional"], ["mick.doe", "mickpassword", "domain3", "parameters-optional"], ]) -def test_http_file_upload(host, username, password, domain, server): +@pytest.mark.usefixtures("server_clean_domain_uploads") +def test_http_file_upload(host, server_host, username, password, domain): """ Tests if http file upload works correctly. """ + upload_directory_path = f"/var/lib/prosody/upload%2e{domain}/http_file_share" + # Prepare file for transfer. expected_content = str(uuid.uuid4()) create_sample_file = host.run("echo -n %s > /tmp/http_file_upload_sample.txt", expected_content) assert create_sample_file.rc == 0 - # Path where uploaded file will end-up. - upload_directory_path = f"/var/lib/prosody/upload%2e{domain}/http_file_share" - - # Find the host that serves the domain. Used for validating uploaded content. - 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]) - - # Clean up leftovers from previous run. - with server_host.sudo(): - server_host.run("rm -rf %s/*", upload_directory_path) - # Upload the file. send = host.run(f"go-sendxmpp --debug --username {username}@{domain} --password {password} --jserver {domain}:5222 " f"--http-upload /tmp/http_file_upload_sample.txt " @@ -161,25 +150,14 @@ def test_http_file_upload(host, username, password, domain, server): ["jane.doe", "janepassword", "domain2", "parameters-optional"], ["mick.doe", "mickpassword", "domain3", "parameters-optional"], ]) -def test_http_file_share_size_limit(host, username, password, domain, server): +@pytest.mark.usefixtures("server_clean_domain_uploads") +def test_http_file_share_size_limit(host, username, password, domain): """ Tests the maximum file size for files uploaded via XEP-0363. """ file_size_limit = 10 * 1024 * 1024 - # Find the host that serves the domain. Used for validating uploaded content. - 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]) - - # Clean up leftovers from previous run. - with server_host.sudo(): - server_host.run("rm -rf %s/*", f"/var/lib/prosody/upload%2e{domain}/") - server_host.run("systemctl restart prosody") - time.sleep(1) - # Test exact size limit. create_sample_file = host.run("dd if=/dev/zero of=/tmp/http_file_upload_sample.txt bs=%sB count=1", str(file_size_limit)) assert create_sample_file.rc == 0 @@ -204,25 +182,14 @@ def test_http_file_share_size_limit(host, username, password, domain, server): ["jane.doe", "janepassword", "domain2", "parameters-optional"], ["mick.doe", "mickpassword", "domain3", "parameters-optional"], ]) -def test_http_file_share_daily_quota(host, username, password, domain, server): +@pytest.mark.usefixtures("server_clean_domain_uploads") +def test_http_file_share_daily_quota(host, username, password, domain): """ Tests the user's daily quota for files uploaded via XEP-0363. """ file_size_limit = 10 * 1024 * 1024 - # Find the host that serves the domain. Used for validating uploaded content. - 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]) - - # Clean up leftovers from previous run. - with server_host.sudo(): - server_host.run("rm -rf %s/*", f"/var/lib/prosody/upload%2e{domain}/") - server_host.run("systemctl restart prosody") - time.sleep(1) - # Fill-up the daily quota. create_sample_file = host.run("dd if=/dev/zero of=/tmp/http_file_upload_sample.txt bs=%sB count=1", str(file_size_limit)) assert create_sample_file.rc == 0