From 7becd45f64e3be822ef11eaf677ab31f369fbeda Mon Sep 17 00:00:00 2001 From: Thomas VIAL Date: Wed, 17 Aug 2016 14:03:33 +0200 Subject: [PATCH] Refactored. --- ...{ENABLE_POP3.bats => env_ENABLE_POP3.bats} | 2 +- ...{SASL_PASSWD.bats => env_SASL_PASSWD.bats} | 0 test/env_SA_XXX.bats | 59 +++++ test/env_SMTP_ONLY.bats | 4 + test/global.bats | 160 +++++++++++++ test/tests._bats | 214 ------------------ 6 files changed, 224 insertions(+), 215 deletions(-) rename test/{ENABLE_POP3.bats => env_ENABLE_POP3.bats} (96%) rename test/{SASL_PASSWD.bats => env_SASL_PASSWD.bats} (100%) create mode 100644 test/env_SA_XXX.bats create mode 100644 test/env_SMTP_ONLY.bats create mode 100644 test/global.bats diff --git a/test/ENABLE_POP3.bats b/test/env_ENABLE_POP3.bats similarity index 96% rename from test/ENABLE_POP3.bats rename to test/env_ENABLE_POP3.bats index 9fba601c..b1e1fbcc 100644 --- a/test/ENABLE_POP3.bats +++ b/test/env_ENABLE_POP3.bats @@ -34,7 +34,7 @@ # #################################################################################################### -@test "checking process: process is not running" { +@test "checking pop: process is not running" { if [ "$ENABLE_POP3" = 1 ]; then skip fi diff --git a/test/SASL_PASSWD.bats b/test/env_SASL_PASSWD.bats similarity index 100% rename from test/SASL_PASSWD.bats rename to test/env_SASL_PASSWD.bats diff --git a/test/env_SA_XXX.bats b/test/env_SA_XXX.bats new file mode 100644 index 00000000..f486eab1 --- /dev/null +++ b/test/env_SA_XXX.bats @@ -0,0 +1,59 @@ +#################################################################################################### +# +# SA_XXX with default configuraton +# +#################################################################################################### + +@test "checking spamassassin: docker env variables are set correctly (default)" { + if [ -n "$SA_TAG" ]; then + skip + fi + run docker exec mail /bin/sh -c "grep '\$sa_tag_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 2.0'" + [ "$status" -eq 0 ] +} + +@test "checking spamassassin: docker env variables are set correctly (default)" { + if [ -n "$SA_TAG2" ]; then + skip + fi + run docker exec mail /bin/sh -c "grep '\$sa_tag2_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 6.31'" + [ "$status" -eq 0 ] +} + +@test "checking spamassassin: docker env variables are set correctly (default)" { + if [ -n "$SA_KILL" ]; then + skip + fi + run docker exec mail /bin/sh -c "grep '\$sa_kill_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 6.31'" + [ "$status" -eq 0 ] +} + +#################################################################################################### +# +# SA_XXX with custom configuraton +# +#################################################################################################### + +@test "checking spamassassin: docker env variables are set correctly (default)" { + if [ -z "$SA_TAG" ]; then + skip + fi + run docker exec mail /bin/sh -c "grep '\$sa_tag_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= $SA_TAG'" + [ "$status" -eq 0 ] +} + +@test "checking spamassassin: docker env variables are set correctly (default)" { + if [ -z "$SA_TAG" ]; then + skip + fi + run docker exec mail /bin/sh -c "grep '\$sa_tag2_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= $SA_TAG2'" + [ "$status" -eq 0 ] +} + +@test "checking spamassassin: docker env variables are set correctly (default)" { + if [ -z "$SA_TAG" ]; then + skip + fi + run docker exec mail /bin/sh -c "grep '\$sa_kill_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= $SA_KILL'" + [ "$status" -eq 0 ] +} diff --git a/test/env_SMTP_ONLY.bats b/test/env_SMTP_ONLY.bats new file mode 100644 index 00000000..7d90ea3c --- /dev/null +++ b/test/env_SMTP_ONLY.bats @@ -0,0 +1,4 @@ +@test "checking process: dovecot imaplogin (disabled using SMTP_ONLY)" { + run docker exec mail_smtponly /bin/bash -c "ps aux --forest | grep -v grep | grep '/usr/sbin/dovecot'" + [ "$status" -eq 1 ] +} \ No newline at end of file diff --git a/test/global.bats b/test/global.bats new file mode 100644 index 00000000..23204c78 --- /dev/null +++ b/test/global.bats @@ -0,0 +1,160 @@ +# +# imap +# + +@test "checking process: dovecot imaplogin (enabled in default configuration)" { + run docker exec mail /bin/bash -c "ps aux --forest | grep -v grep | grep '/usr/sbin/dovecot'" + [ "$status" -eq 0 ] +} + +@test "checking imap: server is ready with STARTTLS" { + run docker exec mail /bin/bash -c "nc -w 2 0.0.0.0 143 | grep '* OK' | grep 'STARTTLS' | grep 'ready'" + [ "$status" -eq 0 ] +} + +@test "checking imap: authentication works" { + run docker exec mail /bin/sh -c "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt" + [ "$status" -eq 0 ] +} + +# +# logs +# + +@test "checking logs: mail related logs should be located in a subdirectory" { + run docker exec mail /bin/sh -c "ls -1 /var/log/mail/ | grep -E 'clamav|freshclam|mail'|wc -l" + [ "$status" -eq 0 ] + [ "$output" = 3 ] +} + +# +# smtp +# + +@test "checking smtp: authentication works with good password (plain)" { + run docker exec mail /bin/sh -c "nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-plain.txt | grep 'Authentication successful'" + [ "$status" -eq 0 ] +} + +@test "checking smtp: authentication fails with wrong password (plain)" { + run docker exec mail /bin/sh -c "nc -w 20 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-plain-wrong.txt | grep 'authentication failed'" + [ "$status" -eq 0 ] +} + +@test "checking smtp: authentication works with good password (login)" { + run docker exec mail /bin/sh -c "nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt | grep 'Authentication successful'" + [ "$status" -eq 0 ] +} + +@test "checking smtp: authentication fails with wrong password (login)" { + run docker exec mail /bin/sh -c "nc -w 20 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login-wrong.txt | grep 'authentication failed'" + [ "$status" -eq 0 ] +} + +@test "checking smtp: delivers mail to existing account" { + run docker exec mail /bin/sh -c "grep 'status=sent (delivered via dovecot service)' /var/log/mail/mail.log | wc -l" + [ "$status" -eq 0 ] + [ "$output" -eq 6 ] +} + +@test "checking smtp: delivers mail to existing alias" { + run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 1 ] +} + +@test "checking smtp: delivers mail to existing catchall" { + run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 1 ] +} + +@test "checking smtp: delivers mail to regexp alias" { + run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 1 ] +} + +@test "checking smtp: user1 should have received 5 mails" { + run docker exec mail /bin/sh -c "ls -A /var/mail/localhost.localdomain/user1/new | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 5 ] +} + +@test "checking smtp: rejects mail to unknown user" { + run docker exec mail /bin/sh -c "grep ': Recipient address rejected: User unknown in virtual mailbox table' /var/log/mail/mail.log | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 1 ] +} + +@test "checking smtp: redirects mail to external aliases" { + run docker exec mail /bin/sh -c "grep -- '-> ' /var/log/mail/mail.log | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 2 ] +} + +@test "checking smtp: rejects spam" { + run docker exec mail /bin/sh -c "grep 'Blocked SPAM' /var/log/mail/mail.log | grep spam@external.tld | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 1 ] +} + +@test "checking smtp: rejects virus" { + run docker exec mail /bin/sh -c "grep 'Blocked INFECTED' /var/log/mail/mail.log | grep virus@external.tld | wc -l" + [ "$status" -eq 0 ] + [ "$output" = 1 ] +} + +# +# accounts +# + +@test "checking accounts: user accounts" { + run docker exec mail doveadm user '*' + [ "$status" -eq 0 ] + [ "${lines[0]}" = "user1@localhost.localdomain" ] + [ "${lines[1]}" = "user2@otherdomain.tld" ] +} + +@test "checking accounts: user mail folders for user1" { + run docker exec mail /bin/bash -c "ls -A /var/mail/localhost.localdomain/user1 | grep -E '.Drafts|.Sent|.Trash|cur|new|subscriptions|tmp' | wc -l" + [ "$status" -eq 0 ] + [ "$output" -eq 7 ] +} + +@test "checking accounts: user mail folders for user2" { + run docker exec mail /bin/bash -c "ls -A /var/mail/otherdomain.tld/user2 | grep -E '.Drafts|.Sent|.Trash|cur|new|subscriptions|tmp' | wc -l" + [ "$status" -eq 0 ] + [ "$output" -eq 7 ] +} + +# +# postfix +# + +@test "checking postfix: vhost file is correct" { + run docker exec mail cat /etc/postfix/vhost + [ "$status" -eq 0 ] + [ "${lines[0]}" = "localdomain2.com" ] + [ "${lines[1]}" = "localhost.localdomain" ] + [ "${lines[2]}" = "otherdomain.tld" ] +} + +@test "checking postfix: main.cf overrides" { + run docker exec mail grep -q 'max_idle = 600s' /tmp/docker-mailserver/postfix-main.cf + [ "$status" -eq 0 ] + run docker exec mail grep -q 'readme_directory = /tmp' /tmp/docker-mailserver/postfix-main.cf + [ "$status" -eq 0 ] +} + +# +# dovecot +# + +@test "checking dovecot: config additions" { + run docker exec mail grep -q 'mail_max_userip_connections = 69' /tmp/docker-mailserver/dovecot.cf + [ "$status" -eq 0 ] + run docker exec mail /bin/sh -c "doveconf | grep 'mail_max_userip_connections = 69'" + [ "$status" -eq 0 ] + [ "$output" = 'mail_max_userip_connections = 69' ] +} \ No newline at end of file diff --git a/test/tests._bats b/test/tests._bats index 27cbf331..5d24a7d8 100644 --- a/test/tests._bats +++ b/test/tests._bats @@ -60,220 +60,6 @@ fi [ "$status" -eq 1 ] } -# -# imap -# - -@test "checking process: dovecot imaplogin (enabled in default configuration)" { - run docker exec mail /bin/bash -c "ps aux --forest | grep -v grep | grep '/usr/sbin/dovecot'" - [ "$status" -eq 0 ] -} - -@test "checking process: dovecot imaplogin (disabled using SMTP_ONLY)" { - run docker exec mail_smtponly /bin/bash -c "ps aux --forest | grep -v grep | grep '/usr/sbin/dovecot'" - [ "$status" -eq 1 ] -} - -@test "checking imap: server is ready with STARTTLS" { - run docker exec mail /bin/bash -c "nc -w 2 0.0.0.0 143 | grep '* OK' | grep 'STARTTLS' | grep 'ready'" - [ "$status" -eq 0 ] -} - -@test "checking imap: authentication works" { - run docker exec mail /bin/sh -c "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt" - [ "$status" -eq 0 ] -} - -# -# logs -# - -@test "checking logs: mail related logs should be located in a subdirectory" { - run docker exec mail /bin/sh -c "ls -1 /var/log/mail/ | grep -E 'clamav|freshclam|mail'|wc -l" - [ "$status" -eq 0 ] - [ "$output" = 3 ] -} - -# -# smtp -# - -@test "checking smtp: authentication works with good password (plain)" { - run docker exec mail /bin/sh -c "nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-plain.txt | grep 'Authentication successful'" - [ "$status" -eq 0 ] -} - -@test "checking smtp: authentication fails with wrong password (plain)" { - run docker exec mail /bin/sh -c "nc -w 20 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-plain-wrong.txt | grep 'authentication failed'" - [ "$status" -eq 0 ] -} - -@test "checking smtp: authentication works with good password (login)" { - run docker exec mail /bin/sh -c "nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt | grep 'Authentication successful'" - [ "$status" -eq 0 ] -} - -@test "checking smtp: authentication fails with wrong password (login)" { - run docker exec mail /bin/sh -c "nc -w 20 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login-wrong.txt | grep 'authentication failed'" - [ "$status" -eq 0 ] -} - -@test "checking smtp: delivers mail to existing account" { - run docker exec mail /bin/sh -c "grep 'status=sent (delivered via dovecot service)' /var/log/mail/mail.log | wc -l" - [ "$status" -eq 0 ] - [ "$output" -eq 6 ] -} - -@test "checking smtp: delivers mail to existing alias" { - run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 1 ] -} - -@test "checking smtp: delivers mail to existing catchall" { - run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 1 ] -} - -@test "checking smtp: delivers mail to regexp alias" { - run docker exec mail /bin/sh -c "grep 'to=, orig_to=' /var/log/mail/mail.log | grep 'status=sent' | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 1 ] -} - -@test "checking smtp: user1 should have received 5 mails" { - run docker exec mail /bin/sh -c "ls -A /var/mail/localhost.localdomain/user1/new | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 5 ] -} - -@test "checking smtp: rejects mail to unknown user" { - run docker exec mail /bin/sh -c "grep ': Recipient address rejected: User unknown in virtual mailbox table' /var/log/mail/mail.log | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 1 ] -} - -@test "checking smtp: redirects mail to external aliases" { - run docker exec mail /bin/sh -c "grep -- '-> ' /var/log/mail/mail.log | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 2 ] -} - -@test "checking smtp: rejects spam" { - run docker exec mail /bin/sh -c "grep 'Blocked SPAM' /var/log/mail/mail.log | grep spam@external.tld | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 1 ] -} - -@test "checking smtp: rejects virus" { - run docker exec mail /bin/sh -c "grep 'Blocked INFECTED' /var/log/mail/mail.log | grep virus@external.tld | wc -l" - [ "$status" -eq 0 ] - [ "$output" = 1 ] -} - -# -# accounts -# - -@test "checking accounts: user accounts" { - run docker exec mail doveadm user '*' - [ "$status" -eq 0 ] - [ "${lines[0]}" = "user1@localhost.localdomain" ] - [ "${lines[1]}" = "user2@otherdomain.tld" ] -} - -@test "checking accounts: user mail folders for user1" { - run docker exec mail /bin/bash -c "ls -A /var/mail/localhost.localdomain/user1 | grep -E '.Drafts|.Sent|.Trash|cur|new|subscriptions|tmp' | wc -l" - [ "$status" -eq 0 ] - [ "$output" -eq 7 ] -} - -@test "checking accounts: user mail folders for user2" { - run docker exec mail /bin/bash -c "ls -A /var/mail/otherdomain.tld/user2 | grep -E '.Drafts|.Sent|.Trash|cur|new|subscriptions|tmp' | wc -l" - [ "$status" -eq 0 ] - [ "$output" -eq 7 ] -} - -# -# postfix -# - -@test "checking postfix: vhost file is correct" { - run docker exec mail cat /etc/postfix/vhost - [ "$status" -eq 0 ] - [ "${lines[0]}" = "localdomain2.com" ] - [ "${lines[1]}" = "localhost.localdomain" ] - [ "${lines[2]}" = "otherdomain.tld" ] -} - -@test "checking postfix: main.cf overrides" { - run docker exec mail grep -q 'max_idle = 600s' /tmp/docker-mailserver/postfix-main.cf - [ "$status" -eq 0 ] - run docker exec mail grep -q 'readme_directory = /tmp' /tmp/docker-mailserver/postfix-main.cf - [ "$status" -eq 0 ] -} - -# -# dovecot -# - -@test "checking dovecot: config additions" { - run docker exec mail grep -q 'mail_max_userip_connections = 69' /tmp/docker-mailserver/dovecot.cf - [ "$status" -eq 0 ] - run docker exec mail /bin/sh -c "doveconf | grep 'mail_max_userip_connections = 69'" - [ "$status" -eq 0 ] - [ "$output" = 'mail_max_userip_connections = 69' ] -} - -# -# spamassassin -# - -if [ -z $SA_TAG ]; then - @test "checking spamassassin: docker env variables are set correctly (default)" { - run docker exec mail_pop3 /bin/sh -c "grep '\$sa_tag_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 2.0'" - [ "$status" -eq 0 ] - } -fi - -if [ -z $SA_TAG2 ]; then - @test "checking spamassassin: docker env variables are set correctly (default)" { - run docker exec mail_pop3 /bin/sh -c "grep '\$sa_tag2_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 6.31'" - [ "$status" -eq 0 ] - } -fi - -if [ -z $SA_KILL ]; then - @test "checking spamassassin: docker env variables are set correctly (default)" { - run docker exec mail_pop3 /bin/sh -c "grep '\$sa_kill_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= 6.31'" - [ "$status" -eq 0 ] - } -fi - - -if [ -n $SA_TAG ]; then - @test "checking spamassassin: docker env variables are set correctly (default)" { - run docker exec mail_pop3 /bin/sh -c "grep '\$sa_tag_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= $SA_TAG'" - [ "$status" -eq 0 ] - } -fi - -if [ -n $SA_TAG2 ]; then - @test "checking spamassassin: docker env variables are set correctly (default)" { - run docker exec mail_pop3 /bin/sh -c "grep '\$sa_tag2_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= $SA_TAG2'" - [ "$status" -eq 0 ] - } -fi - -if [ -n $SA_KILL ]; then - @test "checking spamassassin: docker env variables are set correctly (default)" { - run docker exec mail_pop3 /bin/sh -c "grep '\$sa_kill_level_deflt' /etc/amavis/conf.d/20-debian_defaults | grep '= $SA_KILL'" - [ "$status" -eq 0 ] - } -fi - - # # opendkim #