From 32799b65d55f19f1dca53b135d697ecc4f463d0f Mon Sep 17 00:00:00 2001 From: Benjamin Dweck Date: Mon, 25 Jan 2021 11:33:36 +0200 Subject: [PATCH] created a template python project with docker-compose integration tests --- Dockerfile | 12 ++++++++++++ Dockerfile.test | 9 +++++++++ app.py | 16 ++++++++++++++++ docker-compose.test.yml | 12 ++++++++++++ docker-compose.yml | 9 +++++++++ requirements.txt | 2 ++ test.sh | 8 ++++++++ 7 files changed, 68 insertions(+) create mode 100644 Dockerfile create mode 100644 Dockerfile.test create mode 100644 app.py create mode 100644 docker-compose.test.yml create mode 100644 docker-compose.yml create mode 100644 requirements.txt create mode 100644 test.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c79dd2e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM python:2.7 + +WORKDIR /app + +ADD requirements.txt /app/requirements.txt +RUN pip install -r requirements.txt + +ADD app.py /app/app.py + +EXPOSE 80 + +CMD ["python", "app.py"] diff --git a/Dockerfile.test b/Dockerfile.test new file mode 100644 index 0000000..5aabcc9 --- /dev/null +++ b/Dockerfile.test @@ -0,0 +1,9 @@ +FROM ubuntu:trusty + +RUN apt-get update && apt-get install -yq curl && apt-get clean + +WORKDIR /app + +ADD test.sh /app/test.sh + +CMD ["bash", "test.sh"] diff --git a/app.py b/app.py new file mode 100644 index 0000000..dcff02f --- /dev/null +++ b/app.py @@ -0,0 +1,16 @@ +from flask import Flask +from redis import Redis + +app = Flask(__name__) +redis = Redis(host="redis") + +@app.route("/") +def hello(): + visits = redis.incr('counter') + html = "

Hello World!

" \ + "Visits: {visits}" \ + "
" + return html.format(visits=visits) + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=80) \ No newline at end of file diff --git a/docker-compose.test.yml b/docker-compose.test.yml new file mode 100644 index 0000000..04d6185 --- /dev/null +++ b/docker-compose.test.yml @@ -0,0 +1,12 @@ +sut: + build: . + dockerfile: Dockerfile.test + links: + - web +web: + build: . + dockerfile: Dockerfile + links: + - redis +redis: + image: redis diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c6014fb --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +web: + build: . + dockerfile: Dockerfile + links: + - redis + ports: + - "80:80" +redis: + image: redis \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8862084 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +Flask +Redis \ No newline at end of file diff --git a/test.sh b/test.sh new file mode 100644 index 0000000..f14ae47 --- /dev/null +++ b/test.sh @@ -0,0 +1,8 @@ +sleep 5 +if curl web | grep -q 'Visits: '; then + echo "Tests passed!" + exit 0 +else + echo "Tests failed!" + exit 1 +fi