Merge pull request #1 from dcarrillo/linting

Add testing and github workflow
This commit is contained in:
Daniel Carrillo 2020-03-08 13:31:11 +01:00 committed by GitHub
commit b1046c0178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 139 additions and 3 deletions

40
.github/workflows/main.yml vendored Normal file
View File

@ -0,0 +1,40 @@
name: CI
on:
push:
branches:
- master
pull_request:
jobs:
tests:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v1
- name: shellcheck
uses: azohra/shell-linter@v0.2.0
- name: hadolint
uses: brpaz/hadolint-action@master
- name: Build image
run: ./build.sh
- name: Run tests
run: ./tests/test.sh
deploy:
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v1
- name: Log in to dockerhub
run: echo "${{ secrets.DOCKERHUB_TOKEN }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
- name: Deploy image
run: ./build.sh --push --latest

View File

@ -1,7 +1,12 @@
FROM alpine:3.11
ARG ARG_POSTFIX_VERSION
ENV POSTFIX_VERSION $ARG_POSTFIX_VERSION
# hadolint ignore=DL3018
RUN apk add --no-cache \
postfix \
postfix=$POSTFIX_VERSION \
rsyslog \
runit

34
build.sh Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env sh
set -e
# shellcheck disable=SC1090
. "$(dirname "$0")"/conf.env
while [ $# -gt 0 ]; do
case $1 in
--push)
PUSH=true
shift
;;
--latest)
LATEST=true
shift
;;
*)
shift
;;
esac
done
docker build --build-arg=ARG_POSTFIX_VERSION="$POSTFIX_VERSION" \
-t "$DOCKER_IMAGE":"$POSTFIX_VERSION" .
if [ x$PUSH = "xtrue" ]; then
docker push "$DOCKER_IMAGE":"$POSTFIX_VERSION"
fi
if [ x$LATEST = "xtrue" ]; then
docker tag "$DOCKER_IMAGE":"$POSTFIX_VERSION" "$DOCKER_IMAGE":latest
[ x$PUSH = "xtrue" ] && docker push "$DOCKER_IMAGE":latest
fi

2
conf.env Normal file
View File

@ -0,0 +1,2 @@
POSTFIX_VERSION=3.4.9-r0
DOCKER_IMAGE=dcarrillo/postfix

View File

@ -18,10 +18,11 @@ if [ -n "$MY_NETWORKS" ]; then
fi
if [ -n "$MY_ROOT_ALIAS" ]; then
echo "root: $MY_ROOT_ALIAS" > /etc/aliases
newaliases
sed -iE "s/^#root:.*/root: $MY_ROOT_ALIAS/" /etc/postfix/aliases
fi
newaliases -v
# Ensure rsyslog is up
sv start rsyslog || exit 1

54
tests/test.sh Executable file
View File

@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -e
if [ x"$DEBUG" = xtrue ]; then
set -x
fi
# shellcheck disable=SC2039
trap _catch_err ERR
trap _cleanup EXIT
LOCAL_DIR="$(cd "$(dirname "$0")" ; pwd -P)"
# shellcheck disable=SC1090
. "$LOCAL_DIR"/../conf.env
_catch_err()
{
echo "Test FAILED"
}
_cleanup()
{
echo "Cleaning up..."
docker rm -f "${POSTFIX_VERSION}"_test > /dev/null 2>&1
}
echo "Running container to be tested..."
docker run --name "${POSTFIX_VERSION}"_test --rm \
-e MY_ROOT_ALIAS=mail_receiver \
-e MY_HOSTNAME=test_localhost \
-e MY_DOMAIN=test_localhost \
-e MY_NETWORKS=127.0.0.1 \
-d "${DOCKER_IMAGE}":"${POSTFIX_VERSION}" > /dev/null
sleep 2
DOCKER_EXEC="docker exec -i ${POSTFIX_VERSION}_test"
echo "Preparing container to be tested..."
$DOCKER_EXEC sh -c "adduser -S mail_receiver -s /bin/bash"
$DOCKER_EXEC sh -c "apk -q add mailx"
## Test 1 check receiving at aliased account
echo "+++ Sending test mail to root user aliased as mail_receiver"
$DOCKER_EXEC sh -c "echo 'This is a test...' | mail -s 'Testing postfix configuration' root@test_localhost"
$DOCKER_EXEC sh -c "grep -q 'Subject: Testing postfix configuration' /var/spool/mail/mail_receiver"
## Test 2 check rsyslog is logging to stdout
echo "+++ Testing rsyslog is logging to stdout"
$DOCKER_EXEC sh -c "logger -t test 'testing logging...'"
docker logs "${POSTFIX_VERSION}"_test 2>/dev/null | tail -1 | grep -q 'testing logging...'
echo "All tests succeeded !"