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

Commit 2e8ec75

Browse files
committed
feat(Makefile,rootfs): run unit and style tests in container
1 parent 90bd2c6 commit 2e8ec75

File tree

10 files changed

+106
-37
lines changed

10 files changed

+106
-37
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ _tests/example-*/
3232
# coverage reports
3333
.coverage
3434
coverage.out
35+
coverage.xml
3536
htmlcov/
3637

3738
# python virtual environments for testing

.travis.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@ branches:
55
only:
66
- master
77
services:
8-
- postgresql
98
- docker
109
env:
1110
- DOCKER_BUILD_FLAGS="--pull --no-cache"
1211
sudo: required
13-
addons:
14-
postgresql: "9.4"
1512
cache: pip
16-
before_install:
17-
- createdb -U postgres deis
18-
install:
19-
- pip install -r rootfs/requirements.txt
20-
- pip install -r rootfs/dev_requirements.txt
2113
script:
2214
- make test
2315
after_success:
24-
- pushd rootfs/ && codecov && popd
16+
- make upload-coverage

Makefile

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ SHORT_NAME ?= $(COMPONENT)
66

77
include versioning.mk
88

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

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

33+
docker-build-test: check-docker
34+
docker build ${DOCKER_BUILD_FLAGS} -t ${IMAGE}.test -f rootfs/Dockerfile.test rootfs
35+
3336
deploy: check-kubectl docker-build docker-push
3437
kubectl --namespace=deis patch deployment deis-$(COMPONENT) --type='json' -p='[{"op": "replace", "path": "/spec/template/spec/containers/0/image", "value":"$(IMAGE)"}]'
3538

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

45-
postgres: check-docker
46-
docker start postgres || docker run --restart="always" -d -p 5432:5432 --name postgres postgres:9.3
47-
docker exec postgres createdb -U postgres deis 2>/dev/null || true
48-
@echo "To use postgres for local development:"
49-
@echo " export DEIS_DATABASE_SERVICE_HOST=`docker-machine ip $$(docker-machine active) 2>/dev/null || echo 127.0.0.1`"
50-
@echo " export DEIS_DATABASE_SERVICE_PORT=5432"
51-
@echo " export DEIS_DATABASE_USER=postgres"
52-
53-
setup-venv:
54-
python3 -m venv venv
55-
venv/bin/pip3 install --disable-pip-version-check -q -r rootfs/requirements.txt -r rootfs/dev_requirements.txt
56-
57-
test: test-style test-check test-unit test-functional
48+
test: test-style test-unit test-functional
5849

59-
test-check:
60-
cd rootfs && python manage.py check
61-
62-
test-style:
63-
cd rootfs && flake8 --show-source
50+
test-style: docker-build-test
51+
docker run -v ${CURDIR}:/test -w /test/rootfs ${IMAGE}.test /test/rootfs/bin/test-style
6452
${SHELLCHECK_PREFIX} $(SHELL_SCRIPTS)
6553

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

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

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

77-
.PHONY: build clean commit-hook full-clean postgres setup-venv test test-style test-unit test-functional
63+
upload-coverage:
64+
$(eval CI_ENV := $(shell curl -s https://codecov.io/env | bash))
65+
docker run ${CI_ENV} -v ${CURDIR}:/test -w /test/rootfs ${IMAGE}.test codecov --required
66+
67+
.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

rootfs/.coveragerc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ exclude_lines =
2525
if __name__ == .__main__.:
2626

2727
[html]
28-
title = Deis Coverage Report
28+
title = Controller Coverage Report

rootfs/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ RUN adduser --system \
99

1010
COPY requirements.txt /app/requirements.txt
1111

12-
RUN buildDeps='gcc git libffi-dev libpq-dev libldap2-dev libsasl2-dev python3-dev python3-pip python3-wheel python3-setuptools'; \
12+
RUN buildDeps='gcc libffi-dev libpq-dev libldap2-dev libsasl2-dev python3-dev python3-pip python3-wheel python3-setuptools'; \
1313
apt-get update && \
14-
apt-get install -y --no-install-recommends \
14+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
1515
$buildDeps \
1616
sudo \
1717
libpq5 \

rootfs/Dockerfile.test

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
FROM quay.io/deis/base:v0.3.6
2+
3+
RUN adduser --system \
4+
--shell /bin/bash \
5+
--disabled-password \
6+
--home /app \
7+
--group \
8+
deis
9+
10+
COPY requirements.txt /app/requirements.txt
11+
COPY dev_requirements.txt /app/dev_requirements.txt
12+
13+
RUN buildDeps='gcc libffi-dev libpq-dev libldap2-dev libsasl2-dev python3-dev python3-pip python3-wheel python3-setuptools'; \
14+
apt-get update && \
15+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
16+
$buildDeps \
17+
sudo \
18+
libpq5 \
19+
libldap-2.4 \
20+
python3-minimal \
21+
# cryptography package needs pkg_resources
22+
python3-pkg-resources && \
23+
ln -s /usr/bin/python3 /usr/bin/python && \
24+
mkdir -p /configs && chown -R deis:deis /configs && \
25+
pip3 install --disable-pip-version-check --no-cache-dir -r /app/requirements.txt && \
26+
# cleanup
27+
apt-get purge -y --auto-remove $buildDeps && \
28+
apt-get autoremove -y && \
29+
apt-get clean -y && \
30+
# package up license files if any by appending to existing tar
31+
COPYRIGHT_TAR='/usr/share/copyrights.tar'; \
32+
gunzip -f $COPYRIGHT_TAR.gz; tar -rf $COPYRIGHT_TAR /usr/share/doc/*/copyright; gzip $COPYRIGHT_TAR && \
33+
rm -rf \
34+
/usr/share/doc \
35+
/usr/share/man \
36+
/usr/share/info \
37+
/usr/share/locale \
38+
/var/lib/apt/lists/* \
39+
/var/log/* \
40+
/var/cache/debconf/* \
41+
/etc/systemd \
42+
/lib/lsb \
43+
/lib/udev \
44+
/usr/lib/x86_64-linux-gnu/gconv/IBM* \
45+
/usr/lib/x86_64-linux-gnu/gconv/EBC* && \
46+
bash -c "mkdir -p /usr/share/man/man{1..8}"
47+
48+
# define execution environment
49+
WORKDIR /app
50+
51+
# test-unit additions to the main Dockerfile
52+
ENV PGBIN=/usr/lib/postgresql/9.5/bin PGDATA=/var/lib/postgresql/data
53+
RUN apt-get update && \
54+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
55+
git \
56+
postgresql \
57+
postgresql-contrib \
58+
python3-pip \
59+
python3-setuptools \
60+
python3-wheel && \
61+
pip3 install --disable-pip-version-check --no-cache-dir -r dev_requirements.txt && \
62+
sudo -u postgres -E $PGBIN/initdb
63+
64+
CMD ["/app/bin/test-unit"]

rootfs/api/settings/testing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
# randomize test database name so we can run multiple unit tests simultaneously
2626
DATABASES['default']['NAME'] = "unittest-{}".format(''.join(
2727
random.choice(string.ascii_letters + string.digits) for _ in range(8)))
28+
DATABASES['default']['USER'] = 'postgres'
2829

2930
# use DB name to isolate the data for each test run
3031
CACHES = {

rootfs/bin/test-style

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This script is designed to be run inside the container
4+
#
5+
6+
# fail hard and fast even on pipelines
7+
set -eou pipefail
8+
9+
flake8 --show-source

rootfs/bin/test-unit

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This script is designed to be run inside the container
4+
#
5+
6+
# fail hard and fast even on pipelines
7+
set -eou pipefail
8+
9+
sudo -u postgres "$PGBIN"/pg_ctl -D "$PGDATA" -l /tmp/logfile start
10+
python3 manage.py check
11+
coverage run manage.py test --settings=api.settings.testing --noinput registry api scheduler.tests
12+
coverage report -m

rootfs/dev_requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ codecov==2.0.5
99

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

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

0 commit comments

Comments
 (0)