Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
feat(Makefile,rootfs): run unit and style tests in container
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Apr 3, 2017
1 parent 90bd2c6 commit 2e8ec75
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ _tests/example-*/
# coverage reports
.coverage
coverage.out
coverage.xml
htmlcov/

# python virtual environments for testing
Expand Down
10 changes: 1 addition & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ branches:
only:
- master
services:
- postgresql
- docker
env:
- DOCKER_BUILD_FLAGS="--pull --no-cache"
sudo: required
addons:
postgresql: "9.4"
cache: pip
before_install:
- createdb -U postgres deis
install:
- pip install -r rootfs/requirements.txt
- pip install -r rootfs/dev_requirements.txt
script:
- make test
after_success:
- pushd rootfs/ && codecov && popd
- make upload-coverage
38 changes: 14 additions & 24 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SHORT_NAME ?= $(COMPONENT)

include versioning.mk

SHELLCHECK_PREFIX := docker run --rm -v ${CURDIR}:/workdir -w /workdir quay.io/deis/shell-dev shellcheck
SHELLCHECK_PREFIX := docker run -v ${CURDIR}:/workdir -w /workdir quay.io/deis/shell-dev shellcheck
SHELL_SCRIPTS = $(wildcard rootfs/bin/*) $(shell find "rootfs" -name '*.sh') $(wildcard _scripts/*.sh)

# Test processes used in quick unit testing
Expand All @@ -30,6 +30,9 @@ docker-build: check-docker
docker build ${DOCKER_BUILD_FLAGS} -t ${IMAGE} rootfs
docker tag ${IMAGE} ${MUTABLE_IMAGE}

docker-build-test: check-docker
docker build ${DOCKER_BUILD_FLAGS} -t ${IMAGE}.test -f rootfs/Dockerfile.test rootfs

deploy: check-kubectl docker-build docker-push
kubectl --namespace=deis patch deployment deis-$(COMPONENT) --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"$(IMAGE)"}]'

Expand All @@ -42,36 +45,23 @@ commit-hook:
full-clean: check-docker
docker images -q $(IMAGE_PREFIX)$(COMPONENT) | xargs docker rmi -f

postgres: check-docker
docker start postgres || docker run --restart="always" -d -p 5432:5432 --name postgres postgres:9.3
docker exec postgres createdb -U postgres deis 2>/dev/null || true
@echo "To use postgres for local development:"
@echo " export DEIS_DATABASE_SERVICE_HOST=`docker-machine ip $$(docker-machine active) 2>/dev/null || echo 127.0.0.1`"
@echo " export DEIS_DATABASE_SERVICE_PORT=5432"
@echo " export DEIS_DATABASE_USER=postgres"

setup-venv:
python3 -m venv venv
venv/bin/pip3 install --disable-pip-version-check -q -r rootfs/requirements.txt -r rootfs/dev_requirements.txt

test: test-style test-check test-unit test-functional
test: test-style test-unit test-functional

test-check:
cd rootfs && python manage.py check

test-style:
cd rootfs && flake8 --show-source
test-style: docker-build-test
docker run -v ${CURDIR}:/test -w /test/rootfs ${IMAGE}.test /test/rootfs/bin/test-style
${SHELLCHECK_PREFIX} $(SHELL_SCRIPTS)

test-unit:
cd rootfs \
&& coverage run manage.py test --settings=api.settings.testing --noinput registry api scheduler.tests \
&& coverage report -m
test-unit: docker-build-test
docker run -v ${CURDIR}:/test -w /test/rootfs ${IMAGE}.test /test/rootfs/bin/test-unit

test-functional:
@echo "Implement functional tests in _tests directory"

test-integration:
@echo "Check https://github.com/deis/workflow-e2e for the complete integration test suite"

.PHONY: build clean commit-hook full-clean postgres setup-venv test test-style test-unit test-functional
upload-coverage:
$(eval CI_ENV := $(shell curl -s https://codecov.io/env | bash))
docker run ${CI_ENV} -v ${CURDIR}:/test -w /test/rootfs ${IMAGE}.test codecov --required

.PHONY: check-kubectl check-docker build docker-build docker-build-test deploy clean commit-hook full-clean test test-style test-unit test-functional test-integration upload-coverage
2 changes: 1 addition & 1 deletion rootfs/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ exclude_lines =
if __name__ == .__main__.:

[html]
title = Deis Coverage Report
title = Controller Coverage Report
4 changes: 2 additions & 2 deletions rootfs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ RUN adduser --system \

COPY requirements.txt /app/requirements.txt

RUN buildDeps='gcc git libffi-dev libpq-dev libldap2-dev libsasl2-dev python3-dev python3-pip python3-wheel python3-setuptools'; \
RUN buildDeps='gcc libffi-dev libpq-dev libldap2-dev libsasl2-dev python3-dev python3-pip python3-wheel python3-setuptools'; \
apt-get update && \
apt-get install -y --no-install-recommends \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
$buildDeps \
sudo \
libpq5 \
Expand Down
64 changes: 64 additions & 0 deletions rootfs/Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
FROM quay.io/deis/base:v0.3.6

RUN adduser --system \
--shell /bin/bash \
--disabled-password \
--home /app \
--group \
deis

COPY requirements.txt /app/requirements.txt
COPY dev_requirements.txt /app/dev_requirements.txt

RUN buildDeps='gcc libffi-dev libpq-dev libldap2-dev libsasl2-dev python3-dev python3-pip python3-wheel python3-setuptools'; \
apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
$buildDeps \
sudo \
libpq5 \
libldap-2.4 \
python3-minimal \
# cryptography package needs pkg_resources
python3-pkg-resources && \
ln -s /usr/bin/python3 /usr/bin/python && \
mkdir -p /configs && chown -R deis:deis /configs && \
pip3 install --disable-pip-version-check --no-cache-dir -r /app/requirements.txt && \
# cleanup
apt-get purge -y --auto-remove $buildDeps && \
apt-get autoremove -y && \
apt-get clean -y && \
# package up license files if any by appending to existing tar
COPYRIGHT_TAR='/usr/share/copyrights.tar'; \
gunzip -f $COPYRIGHT_TAR.gz; tar -rf $COPYRIGHT_TAR /usr/share/doc/*/copyright; gzip $COPYRIGHT_TAR && \
rm -rf \
/usr/share/doc \
/usr/share/man \
/usr/share/info \
/usr/share/locale \
/var/lib/apt/lists/* \
/var/log/* \
/var/cache/debconf/* \
/etc/systemd \
/lib/lsb \
/lib/udev \
/usr/lib/x86_64-linux-gnu/gconv/IBM* \
/usr/lib/x86_64-linux-gnu/gconv/EBC* && \
bash -c "mkdir -p /usr/share/man/man{1..8}"

# define execution environment
WORKDIR /app

# test-unit additions to the main Dockerfile
ENV PGBIN=/usr/lib/postgresql/9.5/bin PGDATA=/var/lib/postgresql/data
RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
git \
postgresql \
postgresql-contrib \
python3-pip \
python3-setuptools \
python3-wheel && \
pip3 install --disable-pip-version-check --no-cache-dir -r dev_requirements.txt && \
sudo -u postgres -E $PGBIN/initdb

CMD ["/app/bin/test-unit"]
1 change: 1 addition & 0 deletions rootfs/api/settings/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# randomize test database name so we can run multiple unit tests simultaneously
DATABASES['default']['NAME'] = "unittest-{}".format(''.join(
random.choice(string.ascii_letters + string.digits) for _ in range(8)))
DATABASES['default']['USER'] = 'postgres'

# use DB name to isolate the data for each test run
CACHES = {
Expand Down
9 changes: 9 additions & 0 deletions rootfs/bin/test-style
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
#
# This script is designed to be run inside the container
#

# fail hard and fast even on pipelines
set -eou pipefail

flake8 --show-source
12 changes: 12 additions & 0 deletions rootfs/bin/test-unit
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash
#
# This script is designed to be run inside the container
#

# fail hard and fast even on pipelines
set -eou pipefail

sudo -u postgres "$PGBIN"/pg_ctl -D "$PGDATA" -l /tmp/logfile start
python3 manage.py check
coverage run manage.py test --settings=api.settings.testing --noinput registry api scheduler.tests
coverage report -m
2 changes: 1 addition & 1 deletion rootfs/dev_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ codecov==2.0.5

# mock out python-requests, mostly k8s
# requests-mock==1.0.0
-e git+https://github.com/helgi/requests-mock.git@class_adapter#egg=request_mock
git+https://github.com/helgi/requests-mock.git@class_adapter#egg=request_mock

# tail a log and pipe into tbgrep to find all tracebacks
tbgrep==0.3.0

0 comments on commit 2e8ec75

Please sign in to comment.