Changeset - 226882a5ed41
[Not reviewed]
0 4 0
Branko Majic (branko) - 6 years ago 2020-01-06 23:56:34
branko@majic.rs
MAR-148: Fixed tests for the mail_server role:

- Implement small workaround for sieve-connect invocations (due to
special treatment of 255 exit code by Testinfra itself).
- Fixed a couple of newline-related errors coming from command
outputs.
4 files changed with 32 insertions and 21 deletions:
0 comments (0 inline, 0 general)
roles/mail_server/molecule/default/tests/test_client2.py
Show inline comments
 
@@ -233,16 +233,27 @@ def test_sieve_authentication_requires_tls(host):
 

	
 
    command = host.run("echo 'LOGOUT' | nc parameters-optional 4190")
 
    assert command.rc == 0
 
    assert "PLAIN LOGIN" not in command.stdout
 

	
 
    # STARTTLS
 
    command = host.run("echo 'johnpassword' | sieve-connect -u john.doe@domain1 --password 0 --server parameters-mandatory --port 4190 --list")
 
    # @TODO: In case of failed login (authentication rejected),
 
    #        sieve-connect will return error code 255. However,
 
    #        internally Testinfra treats error code as if it is a
 
    #        signal from the SSH (over which the command is being run)
 
    #        that the SSH itself has failed, and throws a runtime
 
    #        exception. The use of || //bin/false is a workaround for
 
    #        now. It would be a good idea to get in contact with
 
    #        Testinfra maintainer and raise an issue around
 
    #        this. There are more similar uses of sieve-connect in the
 
    #        test files, don't forget to update those as well if some
 
    #        kind of better solution pops-up.
 
    command = host.run("echo 'johnpassword' | sieve-connect -u john.doe@domain1 --password 0 --server parameters-mandatory --port 4190 --list || /bin/false")
 
    assert command.rc == 0
 

	
 
    command = host.run("echo 'johnpassword' | sieve-connect -u john.doe@domain1 --password 0 --server parameters-optional --port 4190 --list")
 
    command = host.run("echo 'johnpassword' | sieve-connect -u john.doe@domain1 --password 0 --server parameters-optional --port 4190 --list || /bin/false")
 
    assert command.rc == 0
 

	
 

	
 
def test_connectivity(host):
 
    """
 
    Tests connectivity to the mail server (ports that should be reachable).
 
@@ -286,20 +297,20 @@ def test_port_forwarding(host):
 
def test_dovecot_sieve(host):
 
    """
 
    Tests if Sieve service is available.
 
    """
 

	
 
    # Test valid users.
 
    command = host.run('echo johnpassword | sieve-connect --list -s parameters-mandatory -p 4190 -u john.doe@domain1 --password 0')
 
    command = host.run('echo johnpassword | sieve-connect --list -s parameters-mandatory -p 4190 -u john.doe@domain1 --password 0 || /bin/false')
 
    assert command.rc == 0
 

	
 
    command = host.run('echo janepassword | sieve-connect --list -s parameters-optional -p 4190 -u jane.doe@domain2 --password 0')
 
    command = host.run('echo janepassword | sieve-connect --list -s parameters-optional -p 4190 -u jane.doe@domain2 --password 0 || /bin/false')
 
    assert command.rc == 0
 

	
 
    # Test invalid users.
 
    command = host.run('echo johnpassword | sieve-connect --list -s parameters-mandatory -p 4190 -u john.doe@domain2 --password 0')
 
    command = host.run('echo johnpassword | sieve-connect --list -s parameters-mandatory -p 4190 -u john.doe@domain2 --password 0 || /bin/false' )
 
    assert command.rc != 0
 
    assert "Authentication refused by server" in command.stderr
 

	
 
    command = host.run('echo janepassword | sieve-connect --list -s parameters-optional -p 4190 -u jane.doe@domain1 --password 0')
 
    command = host.run('echo janepassword | sieve-connect --list -s parameters-optional -p 4190 -u jane.doe@domain1 --password 0 || /bin/false')
 
    assert command.rc != 0
 
    assert "Authentication refused by server" in command.stderr
roles/mail_server/molecule/default/tests/test_default.py
Show inline comments
 
@@ -159,31 +159,31 @@ def test_postfix_ldap_configuration(host):
 

	
 
    with host.sudo():
 

	
 
        # Test for valid domains.
 
        command = host.run("postmap -q domain1 ldap:/etc/postfix/ldap-virtual-mailbox-domains.cf")
 
        assert command.rc == 0
 
        assert command.stdout == "domain1"
 
        assert command.stdout == "domain1\n"
 

	
 
        command = host.run("postmap -q domain2 ldap:/etc/postfix/ldap-virtual-mailbox-domains.cf")
 
        assert command.rc == 0
 
        assert command.stdout == "domain2"
 
        assert command.stdout == "domain2\n"
 

	
 
        # Test for invalid domains.
 
        command = host.run("postmap -q domain3 ldap:/etc/postfix/ldap-virtual-mailbox-domains.cf")
 
        assert command.rc == 1
 
        assert command.stdout == ""
 

	
 
        # Test for valid mail addresses.
 
        command = host.run("postmap -q 'john.doe@domain1' ldap:/etc/postfix/ldap-virtual-mailbox-maps.cf")
 
        assert command.rc == 0
 
        assert command.stdout == 'john.doe@domain1'
 
        assert command.stdout == 'john.doe@domain1\n'
 

	
 
        command = host.run("postmap -q 'jane.doe@domain2' ldap:/etc/postfix/ldap-virtual-mailbox-maps.cf")
 
        assert command.rc == 0
 
        assert command.stdout == 'jane.doe@domain2'
 
        assert command.stdout == 'jane.doe@domain2\n'
 

	
 
        # Test for invalid mail addresses.
 
        command = host.run("postmap -q 'jane.doe@domain1' ldap:/etc/postfix/ldap-virtual-mailbox-maps.cf")
 
        assert command.rc == 1
 
        assert command.stdout == ''
 

	
 
@@ -196,17 +196,17 @@ def test_postfix_ldap_configuration(host):
 
        assert command.rc == 1
 
        assert command.stdout == ''
 

	
 
        # Test for valid mail aliases.
 
        command = host.run("postmap -q postmaster@domain1 ldap:/etc/postfix/ldap-virtual-alias-maps.cf")
 
        assert command.rc == 0
 
        assert command.stdout == "john.doe@domain1"
 
        assert command.stdout == "john.doe@domain1\n"
 

	
 
        command = host.run("postmap -q webmaster@domain2 ldap:/etc/postfix/ldap-virtual-alias-maps.cf")
 
        assert command.rc == 0
 
        assert command.stdout == "jane.doe@domain2"
 
        assert command.stdout == "jane.doe@domain2\n"
 

	
 
        # Test for invalid mail aliases.
 
        command = host.run("postmap -q postmaster@domain2 ldap:/etc/postfix/ldap-virtual-alias-maps.cf")
 
        assert command.rc == 1
 
        assert command.stdout == ""
 

	
 
@@ -229,13 +229,13 @@ def test_postfix_main_cf_file(host):
 

	
 
def test_postfix_delivery_to_dovecot(host):
 
    """
 
    Tests if mail received by Postfix is properly delivered to Dovecot.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    message_id = str(uuid.uuid4())
 

	
 
    # Virtual account.
 
    send = host.run('swaks --header %s --suppress-data --to john.doe@domain1 --server %s', "Message-Id: <%s>" % message_id, hostname)
 
    assert send.rc == 0
roles/mail_server/molecule/default/tests/test_mandatory.py
Show inline comments
 
@@ -9,13 +9,13 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 

	
 
def test_smtp_tls_files(host):
 
    """
 
    Tests if SMTP TLS private key has been deployed correctly.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    with host.sudo():
 

	
 
        tls_file = host.file('/etc/ssl/private/%s_smtp.key' % hostname)
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
@@ -48,13 +48,13 @@ def test_smtp_tls_files(host):
 
def test_certificate_validity_check_configuration(host):
 
    """
 
    Tests if certificate validity check configuration file has been deployed
 
    correctly.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    config = host.file('/etc/check_certificate/%s_smtp.conf' % hostname)
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
@@ -71,23 +71,23 @@ def test_certificate_validity_check_configuration(host):
 
def test_mailname_file_content(host):
 
    """
 
    Tests the system mail name file content.
 
    """
 

	
 
    mailname = host.file('/etc/mailname')
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    assert mailname.content == hostname
 

	
 

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

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    config = host.file('/etc/postfix/main.cf')
 
    config_lines = config.content.split("\n")
 

	
 
    assert "myhostname = %s" % hostname in config_lines
 
    assert "mydestination = %s, %s, localhost.localdomain, localhost" % (hostname, hostname) in config_lines
roles/mail_server/molecule/default/tests/test_optional.py
Show inline comments
 
@@ -11,13 +11,13 @@ testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
 

	
 
def test_smtp_tls_files(host):
 
    """
 
    Tests if SMTP TLS private key has been deployed correctly.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    with host.sudo():
 

	
 
        tls_file = host.file('/etc/ssl/private/%s_smtp.key' % hostname)
 
        assert tls_file.is_file
 
        assert tls_file.user == 'root'
 
@@ -50,13 +50,13 @@ def test_smtp_tls_files(host):
 
def test_certificate_validity_check_configuration(host):
 
    """
 
    Tests if certificate validity check configuration file has been deployed
 
    correctly.
 
    """
 

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    config = host.file('/etc/check_certificate/%s_smtp.conf' % hostname)
 
    assert config.is_file
 
    assert config.user == 'root'
 
    assert config.group == 'root'
 
    assert config.mode == 0o644
 
@@ -73,13 +73,13 @@ def test_certificate_validity_check_configuration(host):
 
def test_mailname_file_content(host):
 
    """
 
    Tests the system mail name file content.
 
    """
 

	
 
    mailname = host.file('/etc/mailname')
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    assert mailname.content == hostname
 

	
 

	
 
def test_postfix_main_cf_file_content(host):
 
    """
 
@@ -91,13 +91,13 @@ def test_postfix_main_cf_file_content(host):
 
    distribution_release = host.ansible("setup")["ansible_facts"]["ansible_distribution_release"]
 
    if distribution_release == "jessie":
 
        allow_relay_from_ip = "10.31.127.20"
 
    elif distribution_release == "stretch":
 
        allow_relay_from_ip = "10.31.127.22"
 

	
 
    hostname = host.run('hostname').stdout
 
    hostname = host.run('hostname').stdout.strip()
 

	
 
    config = host.file('/etc/postfix/main.cf')
 
    config_lines = config.content.split("\n")
 

	
 
    assert "myhostname = %s" % hostname in config_lines
 
    assert "mydestination = %s, %s, localhost.localdomain, localhost" % (hostname, hostname) in config_lines
0 comments (0 inline, 0 general)