From 226882a5ed41e7f2d3bc0db95f21563b0c4a8fcc 2020-01-06 23:56:34 From: Branko Majic Date: 2020-01-06 23:56:34 Subject: [PATCH] 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. --- diff --git a/roles/mail_server/molecule/default/tests/test_client2.py b/roles/mail_server/molecule/default/tests/test_client2.py index 4b1527ecb71fe2e2bfa5a5deb9147b2c972db724..7e3c99a584832c6605b6c06e09d66190b508ee7a 100644 --- a/roles/mail_server/molecule/default/tests/test_client2.py +++ b/roles/mail_server/molecule/default/tests/test_client2.py @@ -236,10 +236,21 @@ def test_sieve_authentication_requires_tls(host): 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 @@ -289,17 +300,17 @@ def test_dovecot_sieve(host): """ # 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 diff --git a/roles/mail_server/molecule/default/tests/test_default.py b/roles/mail_server/molecule/default/tests/test_default.py index 8af748a494d2c2ccd695190b1e097f492ce8bc18..ce8e4b055da0abd4e59e470ae3cd279d5d7f0d6f 100644 --- a/roles/mail_server/molecule/default/tests/test_default.py +++ b/roles/mail_server/molecule/default/tests/test_default.py @@ -162,11 +162,11 @@ def test_postfix_ldap_configuration(host): # 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") @@ -176,11 +176,11 @@ def test_postfix_ldap_configuration(host): # 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") @@ -199,11 +199,11 @@ def test_postfix_ldap_configuration(host): # 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") @@ -232,7 +232,7 @@ 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()) diff --git a/roles/mail_server/molecule/default/tests/test_mandatory.py b/roles/mail_server/molecule/default/tests/test_mandatory.py index 7a19a46f4da9122ee2fa2a9eef4dc4da19518b73..538686361642479f14afc1fb57fb4e3d88a90cf1 100644 --- a/roles/mail_server/molecule/default/tests/test_mandatory.py +++ b/roles/mail_server/molecule/default/tests/test_mandatory.py @@ -12,7 +12,7 @@ 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(): @@ -51,7 +51,7 @@ def test_certificate_validity_check_configuration(host): 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 @@ -74,7 +74,7 @@ def test_mailname_file_content(host): """ mailname = host.file('/etc/mailname') - hostname = host.run('hostname').stdout + hostname = host.run('hostname').stdout.strip() assert mailname.content == hostname @@ -84,7 +84,7 @@ 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") diff --git a/roles/mail_server/molecule/default/tests/test_optional.py b/roles/mail_server/molecule/default/tests/test_optional.py index cb5e108d121ab89870e37eac27cf1756cf5cfa04..96819d675d50d11a663bc6f9c38a0e4af267a765 100644 --- a/roles/mail_server/molecule/default/tests/test_optional.py +++ b/roles/mail_server/molecule/default/tests/test_optional.py @@ -14,7 +14,7 @@ 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(): @@ -53,7 +53,7 @@ def test_certificate_validity_check_configuration(host): 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 @@ -76,7 +76,7 @@ def test_mailname_file_content(host): """ mailname = host.file('/etc/mailname') - hostname = host.run('hostname').stdout + hostname = host.run('hostname').stdout.strip() assert mailname.content == hostname @@ -94,7 +94,7 @@ def test_postfix_main_cf_file_content(host): 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")