Skip to content
This repository has been archived by the owner on Jul 1, 2021. It is now read-only.

Commit

Permalink
tests(): add all 1.1 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hardware committed Aug 20, 2017
1 parent cbccfed commit 335b859
Show file tree
Hide file tree
Showing 37 changed files with 2,317 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
language: bash
sudo: required
services:
- docker
script:
- make all
102 changes: 102 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
NAME = hardware/mailserver:testing

all: build-no-cache init fixtures run clean
all-fast: build init fixtures run clean
no-build: init fixtures run clean

build-no-cache:
docker build --no-cache -t $(NAME) .

build:
docker build -t $(NAME) .

init:
# -docker rm -f mariadb redis mailserver_default mailserver_reverse
sleep 2

docker run \
-d \
--name mariadb \
-e MYSQL_ROOT_PASSWORD=testpasswd \
-e MYSQL_DATABASE=postfix \
-e MYSQL_USER=postfix \
-e MYSQL_PASSWORD=testpasswd \
-v "`pwd`/test/config/mariadb":/docker-entrypoint-initdb.d \
-t mariadb:10.1

docker run \
-d \
--name redis \
-t redis:3.2-alpine

sleep 10

docker run \
-d \
--name mailserver_default \
--link mariadb:mariadb \
--link redis:redis \
-e DBPASS=testpasswd \
-e RSPAMD_PASSWORD=testpasswd \
-e VMAILUID=`id -u` \
-e VMAILGID=`id -g` \
-e DISABLE_CLAMAV=true \
-e ADD_DOMAINS=domain2.tld,domain3.tld \
-e TESTING=true \
-v "`pwd`/test/share/tests":/tmp/tests \
-v "`pwd`/test/share/ssl":/var/mail/ssl \
-v "`pwd`/test/share/postfix":/var/mail/postfix \
-h mail.domain.tld \
-t $(NAME)

docker run \
-d \
--name mailserver_reverse \
--link mariadb:mariadb \
--link redis:redis \
-e FQDN=mail.domain.tld \
-e DOMAIN=domain.tld \
-e DBPASS=testpasswd \
-e RSPAMD_PASSWORD=testpasswd \
-e VMAILUID=`id -u` \
-e VMAILGID=`id -g` \
-e VMAIL_SUBDIR=subdir \
-e RECIPIENT_DELIMITER=: \
-e RELAY_NETWORKS="192.168.0.0/16 172.16.0.0/12 10.0.0.0/8" \
-e DISABLE_CLAMAV=true \
-e DISABLE_SIEVE=true \
-e DISABLE_SIGNING=true \
-e DISABLE_GREYLISTING=true \
-e DISABLE_RATELIMITING=true \
-e ENABLE_POP3=true \
-e OPENDKIM_KEY_LENGTH=4096 \
-e TESTING=true \
-v "`pwd`/test/share/tests":/tmp/tests \
-v "`pwd`/test/share/ssl":/var/mail/ssl \
-v "`pwd`/test/share/letsencrypt":/etc/letsencrypt \
-t $(NAME)

docker exec mailserver_default /bin/sh -c "apt-get update && apt-get install -y -q netcat"
docker exec mailserver_reverse /bin/sh -c "apt-get update && apt-get install -y -q netcat"

fixtures:
docker exec mailserver_default /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-existing-user.txt"
docker exec mailserver_default /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-valid-user-subaddress-with-default-separator.txt"
docker exec mailserver_default /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-non-existing-user.txt"
docker exec mailserver_default /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-existing-alias.txt"
docker exec mailserver_default /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-spam-to-existing-user.txt"
docker exec mailserver_default /bin/sh -c "openssl s_client -ign_eof -connect 0.0.0.0:587 -starttls smtp < /tmp/tests/email-templates/internal-user-to-existing-user.txt"

docker exec mailserver_reverse /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-existing-user.txt"
docker exec mailserver_reverse /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-valid-user-subaddress.txt"
docker exec mailserver_reverse /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-non-existing-user.txt"
docker exec mailserver_reverse /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-to-existing-alias.txt"
docker exec mailserver_reverse /bin/sh -c "nc 0.0.0.0 25 < /tmp/tests/email-templates/external-spam-to-existing-user.txt"
docker exec mailserver_reverse /bin/sh -c "openssl s_client -ign_eof -connect 0.0.0.0:587 -starttls smtp < /tmp/tests/email-templates/internal-user-to-existing-user.txt"
sleep 10

run:
./test/bin/bats test/tests.bats

clean:
docker rm -f mariadb mailserver_default mailserver_reverse
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ You can use this variable to allow other local containers to relay via the mails

#### Let's Encrypt certificate authority

This mail setup uses 3 domain names that should be covered by your new certificate :
This mail setup uses 4 domain names that should be covered by your new certificate :

* **mail.domain.tld** (mandatory)
* **postfixadmin.domain.tld** (recommended)
Expand Down Expand Up @@ -398,7 +398,7 @@ docker logs -f mailserver

- Postfix 3.1.4
- Dovecot 2.2.27
- Rspamd 1.6.1
- Rspamd 1.6.3
- Fetchmail 6.3.26
- ClamAV 0.99.2
- s6 2.6.0.0
Expand Down
5 changes: 2 additions & 3 deletions rootfs/usr/local/bin/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ sed -i -e "s/DOVECOT_MIN_PROCESS/${DOVECOT_MIN_PROCESS}/" \
# Disable virus check if asked
if [ "$DISABLE_CLAMAV" = true ]; then
echo "[INFO] ClamAV is disabled, service will not start."
sed -i 's|\(enabled.*=\).*|\1 false;|' /etc/rspamd/local.d/antivirus.conf
rm -f /etc/rspamd/local.d/antivirus.conf
fi

# Disable fetchmail forwarding
Expand Down Expand Up @@ -281,13 +281,12 @@ if [ "$ENABLE_POP3" = true ]; then
sed -i '/^protocols/s/$/ pop3/' /etc/dovecot/dovecot.conf
fi

# Virtual table may cause counting troubles during tests
# Disable fetchmail cron task during tests
if [ "$TESTING" = true ]; then
echo "[INFO] DOCKER IMAGE UNDER TESTING"
sed -i '/etc\/postfix\/virtual/ s/^/#/' /etc/postfix/main.cf
sed -i 's|\(sign_local.*=\).*|\1 false;|' /etc/rspamd/local.d/dkim_signing.conf
sed -i 's|\(sign_local.*=\).*|\1 false;|' /etc/rspamd/local.d/arc.conf
sed -i 's|\(ssl_dh_parameters_length.*=\).*|\1 512|' /etc/dovecot/conf.d/10-ssl.conf
rm -f /etc/cron.d/fetchmail
fi

Expand Down
142 changes: 142 additions & 0 deletions test/bin/bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#!/usr/bin/env bash
set -e

version() {
echo "Bats 0.4.0"
}

usage() {
version
echo "Usage: bats [-c] [-p | -t] <test> [<test> ...]"
}

help() {
usage
echo
echo " <test> is the path to a Bats test file, or the path to a directory"
echo " containing Bats test files."
echo
echo " -c, --count Count the number of test cases without running any tests"
echo " -h, --help Display this help message"
echo " -p, --pretty Show results in pretty format (default for terminals)"
echo " -t, --tap Show results in TAP format"
echo " -v, --version Display the version number"
echo
echo " For more information, see https://github.com/sstephenson/bats"
echo
}

resolve_link() {
$(type -p greadlink readlink | head -1) "$1"
}

abs_dirname() {
local cwd="$(pwd)"
local path="$1"

while [ -n "$path" ]; do
cd "${path%/*}"
local name="${path##*/}"
path="$(resolve_link "$name" || true)"
done

pwd
cd "$cwd"
}

expand_path() {
{ cd "$(dirname "$1")" 2>/dev/null
local dirname="$PWD"
cd "$OLDPWD"
echo "$dirname/$(basename "$1")"
} || echo "$1"
}

BATS_LIBEXEC="$(abs_dirname "$0")"
export BATS_PREFIX="$(abs_dirname "$BATS_LIBEXEC")"
export BATS_CWD="$(abs_dirname .)"
export PATH="$BATS_LIBEXEC:$PATH"

options=()
arguments=()
for arg in "$@"; do
if [ "${arg:0:1}" = "-" ]; then
if [ "${arg:1:1}" = "-" ]; then
options[${#options[*]}]="${arg:2}"
else
index=1
while option="${arg:$index:1}"; do
[ -n "$option" ] || break
options[${#options[*]}]="$option"
let index+=1
done
fi
else
arguments[${#arguments[*]}]="$arg"
fi
done

unset count_flag pretty
[ -t 0 ] && [ -t 1 ] && pretty="1"
[ -n "$CI" ] && pretty=""

for option in "${options[@]}"; do
case "$option" in
"h" | "help" )
help
exit 0
;;
"v" | "version" )
version
exit 0
;;
"c" | "count" )
count_flag="-c"
;;
"t" | "tap" )
pretty=""
;;
"p" | "pretty" )
pretty="1"
;;
* )
usage >&2
exit 1
;;
esac
done

if [ "${#arguments[@]}" -eq 0 ]; then
usage >&2
exit 1
fi

filenames=()
for filename in "${arguments[@]}"; do
if [ -d "$filename" ]; then
shopt -s nullglob
for suite_filename in "$(expand_path "$filename")"/*.bats; do
filenames["${#filenames[@]}"]="$suite_filename"
done
shopt -u nullglob
else
filenames["${#filenames[@]}"]="$(expand_path "$filename")"
fi
done

if [ "${#filenames[@]}" -eq 1 ]; then
command="bats-exec-test"
else
command="bats-exec-suite"
fi

if [ -n "$pretty" ]; then
extended_syntax_flag="-x"
formatter="bats-format-tap-stream"
else
extended_syntax_flag=""
formatter="cat"
fi

set -o pipefail execfail
exec "$command" $count_flag $extended_syntax_flag "${filenames[@]}" | "$formatter"
55 changes: 55 additions & 0 deletions test/bin/bats-exec-suite
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
set -e

count_only_flag=""
if [ "$1" = "-c" ]; then
count_only_flag=1
shift
fi

extended_syntax_flag=""
if [ "$1" = "-x" ]; then
extended_syntax_flag="-x"
shift
fi

trap "kill 0; exit 1" int

count=0
for filename in "$@"; do
let count+="$(bats-exec-test -c "$filename")"
done

if [ -n "$count_only_flag" ]; then
echo "$count"
exit
fi

echo "1..$count"
status=0
offset=0
for filename in "$@"; do
index=0
{
IFS= read -r # 1..n
while IFS= read -r line; do
case "$line" in
"begin "* )
let index+=1
echo "${line/ $index / $(($offset + $index)) }"
;;
"ok "* | "not ok "* )
[ -n "$extended_syntax_flag" ] || let index+=1
echo "${line/ $index / $(($offset + $index)) }"
[ "${line:0:6}" != "not ok" ] || status=1
;;
* )
echo "$line"
;;
esac
done
} < <( bats-exec-test $extended_syntax_flag "$filename" )
offset=$(($offset + $index))
done

exit "$status"
Loading

0 comments on commit 335b859

Please sign in to comment.