Files @ 814be5def61d
Branch filter:

Location: majic-ansible-roles/roles/mail_forwarder/molecule/default/tests/test_smtp_relay_host_port.py

branko
MAR-189: Added support for Debian 11 Bullseye to xmpp_server role:

- Roll-out LDAP client configuration since Bullseye does not come with
a stock one at /etc/ldap/ldap.conf that sets the trust anchor
correctly for validating LDAP server certificates.
- Drop the backports pinning in case of Bullseye (for now let's try to
keep the Buster and Bullseye at same versions for simplicity).
- Drop installation of Python apt bindings (no longer used).
- Tests for Buster and Bullseye need to be split-up a bit due to some
differences around backports etc.
import os
import re
import time


import testinfra.utils.ansible_runner


testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('parameters-no-incoming')


def test_postfix_main_cf_file_content(host):
    """
    Tests if the Postfix main configuration file content is correct.
    """

    hostname = host.run('hostname').stdout.strip()
    config = host.file('/etc/postfix/main.cf')
    config_lines = config.content_string.split("\n")

    assert "myhostname = %s" % hostname in config_lines
    assert "mydestination = %s, %s, localhost.localdomain, localhost" % (hostname, hostname) in config_lines
    assert "relayhost = mail-server" in config_lines
    assert "mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" in config_lines
    assert "smtp_tls_security_level=verify" in config_lines
    assert "smtp_tls_CAfile=/etc/ssl/certs/smtp_relay_truststore.pem" in config_lines
    assert "smtp_host_lookup = dns, native" in config_lines


def test_relay_mail_sending(host):
    """
    Tests if mails are sent correctly via relay if relay has been configured.
    """

    send = host.run('swaks --suppress-data --to root@domain1 --server localhost')
    assert send.rc == 0
    message_id = re.search('Ok: queued as (.*)', send.stdout).group(1)

    # Wait for a little while for message to be processed.
    time.sleep(5)

    with host.sudo():
        mail_log = host.file('/var/log/mail.log')
        # Pattern used to verify the mail was sent over relay on default port.
        pattern = r"%s: to=<root@domain1>, relay=mail-server\[[^]]*\]:25.*status=sent" % message_id

        assert re.search(pattern, mail_log.content_string) is not None