From 60b1798790c2cd789193d61310f230b0cb4172fd Mon Sep 17 00:00:00 2001 From: dcarrillo Date: Sun, 8 Mar 2020 13:27:44 +0100 Subject: [PATCH] Add testing and github workflow --- .github/workflows/main.yml | 40 ++++++++++++++++++++++++++++ Dockerfile | 7 ++++- build.sh | 34 ++++++++++++++++++++++++ conf.env | 2 ++ tests/test.sh | 54 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/main.yml create mode 100755 build.sh create mode 100644 conf.env create mode 100755 tests/test.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..ca3fc79 --- /dev/null +++ b/.github/workflows/main.yml @@ -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: ./test/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 diff --git a/Dockerfile b/Dockerfile index 37def95..16198be 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..c826333 --- /dev/null +++ b/build.sh @@ -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 diff --git a/conf.env b/conf.env new file mode 100644 index 0000000..abbd72b --- /dev/null +++ b/conf.env @@ -0,0 +1,2 @@ +POSTFIX_VERSION=3.4.9-r0 +DOCKER_IMAGE=dcarrillo/postfix diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..7aee497 --- /dev/null +++ b/tests/test.sh @@ -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 !"