Improve and extend setup.sh (#295)

* Improve and extend setup.sh

Add subcommands 'debug show-mail-logs', 'inspect', 'login'. Add option
'-c' to specify the name of the running container. Add option '-i' to
specify the image name.

* Add tests for setup.sh
This commit is contained in:
Josef Friedrich 2016-09-01 12:10:23 +02:00 committed by Thomas VIAL
parent ee0c4244cc
commit ac36272d97
2 changed files with 162 additions and 19 deletions

118
setup.sh
View file

@ -2,14 +2,41 @@
##
# Wrapper for various setup scripts included in the docker-mailserver
##
#
if [ -z "$DOCKER_IMAGE" ]; then
DOCKER_IMAGE=tvial/docker-mailserver:latest
INFO=$(docker ps \
--no-trunc \
--format="{{.Image}}\t{{.Names}}\t{{.Command}}" | \
grep '/bin/sh -c /usr/local/bin/start-mailserver.sh')
IMAGE_NAME=$(echo $INFO | awk '{print $1}')
CONTAINER_NAME=$(echo $INFO | awk '{print $2}')
if [ -z "$IMAGE_NAME" ]; then
IMAGE_NAME=tvial/docker-mailserver:latest
fi
_inspect() {
if _docker_image_exists "$IMAGE_NAME"; then
echo "Image: $IMAGE_NAME"
else
echo "Image: '$IMAGE_NAME' cant be found."
fi
if [ -n "$CONTAINER_NAME" ]; then
echo "Container: $CONTAINER_NAME"
else
echo "Container: Not running, please start docker-mailserver."
fi
}
_usage() {
echo "Usage: $0 <subcommand> <subcommand> [args]
echo "Usage: $0 [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]
OPTIONS:
-i IMAGE_NAME The name of the docker-mailserver image, by default
'tvial/docker-mailserver:latest'.
-c CONTAINER_NAME The name of the running container.
SUBCOMMANDS:
@ -27,16 +54,57 @@ SUBCOMMANDS:
debug:
$0 debug fetchmail
$0 debug show-mail-logs
$0 debug inspect
$0 debug login <commands>
"
exit 1
}
_docker() {
docker run --rm \
-v "$(pwd)/config":/tmp/docker-mailserver \
-ti $DOCKER_IMAGE $@
_docker_image_exists() {
if docker history -q "$1" >/dev/null 2>&1; then
return 0
else
return 1
fi
}
_docker_image() {
if ! _docker_image_exists "$IMAGE_NAME"; then
echo "Image '$IMAGE_NAME' not found. Pulling ..."
docker pull "$IMAGE_NAME"
fi
docker run \
--rm \
-v "$(pwd)/config":/tmp/docker-mailserver \
-ti "$IMAGE_NAME" $@
}
_docker_container() {
if [ -n "$CONTAINER_NAME" ]; then
docker exec -ti "$CONTAINER_NAME" $@
else
echo "The docker-mailserver is not running!"
exit 1
fi
}
while getopts ":c:i:" OPT; do
case $OPT in
c)
CONTAINER_NAME="$OPTARG"
;;
i)
IMAGE_NAME="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
;;
esac
done
shift $((OPTIND-1))
case $1 in
email)
@ -45,17 +113,15 @@ case $1 in
add)
shift
_docker addmailuser $@
_docker_image addmailuser $@
;;
del)
shift
_docker delmailuser $@
_docker_image delmailuser $@
;;
list)
_docker listmailuser
_docker_image listmailuser
;;
*)
_usage
;;
@ -66,13 +132,10 @@ case $1 in
shift
case $1 in
dkim)
shift
_docker generate-dkim-config
_docker_image generate-dkim-config
;;
ssl)
shift
_docker generate-ssl-certificate
_docker_image generate-ssl-certificate
;;
*)
_usage
@ -84,7 +147,24 @@ case $1 in
shift
case $1 in
fetchmail)
_docker debug-fetchmail
_docker_image debug-fetchmail
;;
show-mail-logs)
_docker_container cat /var/log/mail/mail.log
;;
inspect)
_inspect
;;
login)
shift
if [ -z "$1" ]; then
_docker_container /bin/bash
else
_docker_container /bin/bash -c "$@"
fi
;;
*)
_usage
;;
esac
;;

View file

@ -660,3 +660,66 @@
run docker exec mail_pop3 /bin/sh -c "postconf | grep '^mynetworks =' | egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/32'"
[ "$status" -eq 0 ]
}
#
# setup.sh
#
# CLI interface
@test "checking setup.sh: Without arguments: status 1, show help text" {
run ./setup.sh
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Usage: ./setup.sh [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]" ]
}
@test "checking setup.sh: Wrong arguments" {
run ./setup.sh lol troll
[ "$status" -eq 1 ]
[ "${lines[0]}" = "Usage: ./setup.sh [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]" ]
}
# email
@test "checking setup.sh: setup.sh email add " {
run ./setup.sh -c mail email add lorem@impsum.org dolorsit
[ "$status" -eq 0 ]
value=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org | awk -F '|' '{print $1}')
[ "$value" = "lorem@impsum.org" ]
}
@test "checking setup.sh: setup.sh email list" {
run ./setup.sh -c mail email list
[ "$status" -eq 0 ]
}
@test "checking setup.sh: setup.sh email del" {
run ./setup.sh -c mail email del lorem@impsum.org
[ "$status" -eq 0 ]
run value=$(cat ./config/postfix-accounts.cf | grep lorem@impsum.org)
[ -z "$value" ]
}
# config
@test "checking setup.sh: setup.sh config dkim" {
run ./setup.sh -c mail config dkim
[ "$status" -eq 0 ]
}
# TODO: To create a test generate-ssl-certificate must be non interactive
#@test "checking setup.sh: setup.sh config ssl" {
# run ./setup.sh -c mail_ssl config ssl
# [ "$status" -eq 0 ]
#}
# debug
@test "checking setup.sh: setup.sh debug fetchmail" {
run ./setup.sh -c mail debug fetchmail
[ "$status" -eq 5 ]
# TODO: Fix output check
# [ "$output" = "fetchmail: no mailservers have been specified." ]
}
@test "checking setup.sh: setup.sh debug inspect" {
run ./setup.sh -c mail debug inspect
[ "$status" -eq 0 ]
[ "${lines[0]}" = "Image: tvial/docker-mailserver:testing" ]
[ "${lines[1]}" = "Container: mail" ]
}
@test "checking setup.sh: setup.sh debug login ls" {
run ./setup.sh -c mail debug login ls
[ "$status" -eq 0 ]
}