Commit 1e5cd195 authored by Daniel Widerin's avatar Daniel Widerin

Merge branch 'flask' into 'master'

Flask



See merge request !1
parents bfdfcd9d e84401fe
bin
include
lib
.Python
pip-selfcheck.json
__pycache__
.cache
.git
*.py[c|o]
.cache
.Python
__pycache__
/bin
/include
/lib
/.Python
/pip-selfcheck.json
/__pycache__
/.cache
image: docker:latest
services:
- docker:dind
variables:
DOCKER_TAG: "registry.gitlab.com/widerin/vlbg-webdev-1016"
stages:
- build
- test
- deploy
before_script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN ${DOCKER_TAG}
build:
.build: &build_template
stage: build
variables:
CONTAINER_BUILD_IMAGE: ${DOCKER_TAG}:latest
tags:
- dind
script:
- docker build -t $CONTAINER_BUILD_IMAGE -f docker/Dockerfile .
- docker push $CONTAINER_BUILD_IMAGE
- echo "Building ${CONTAINER_BUILD_IMAGE}..."
- docker build -t ${CONTAINER_BUILD_IMAGE} -f ${APP_DIRECTORY}/Dockerfile .
- docker push ${CONTAINER_BUILD_IMAGE}
test:
stage: test
build-example-1:
<<: *build_template
variables:
APP_DIRECTORY: example-1
CONTAINER_BUILD_IMAGE: ${DOCKER_TAG}:${APP_DIRECTORY}
build-example-2:
<<: *build_template
variables:
CONTAINER_TEST_IMAGE: ${DOCKER_TAG}:latest
APP_DIRECTORY: example-2
CONTAINER_BUILD_IMAGE: ${DOCKER_TAG}:${APP_DIRECTORY}
.test: &test_template
stage: test
tags:
- dind
script:
- docker pull $CONTAINER_TEST_IMAGE
- docker run $CONTAINER_TEST_IMAGE pytest
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run ${CONTAINER_TEST_IMAGE} pytest
test-example-1:
<<: *test_template
variables:
CONTAINER_TEST_IMAGE: ${DOCKER_TAG}:example-1
test-example-2:
<<: *test_template
services:
- docker:dind
variables:
CONTAINER_TEST_IMAGE: ${DOCKER_TAG}:example-2
script:
- env
- docker run -d --name redis-${CI_BUILD_ID} redis
- docker pull ${CONTAINER_TEST_IMAGE}
- docker run --link redis-${CI_BUILD_ID}:redis ${CONTAINER_TEST_IMAGE} pytest
# VLBG WEBDEV Oct. 2016
Sample pyramid application dockerized
Sample Flask application dockerized
* [example-1](./example-1) - Flask sample ping application
* [example-2](./example-2) - Flask sample ping application with connected redis to count pings per remote ip.
## Install
git clone https://gitlab.com/widerin/vlbg-webdev-1016.git
cd vlbg-webdev-1016
virtualenv -p python3 .
source bin/activate
### Example 1
cd example-1
pip install -r requirements.txt
FLASK_APP=server.py flask run
Run application in browser using http://localhost:5000/ping
### Example 2
This app requires a running redis server, you can start on using docker:
docker run -d --name redis redis
Now you're ready to launch example 2
cd example-2
pip install -r requirements.txt
FLASK_APP=server.py flask run
Run application in browser using http://localhost:5000/ping. Each time you
refresh your browser, it will increment the counter stored in Redis:
REMOTE_ADDR => value++
FROM python:3-alpine
COPY ["requirements.txt", "server.py", "tests.py", "pytest.ini", "/app/"]
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
CMD ["python", "server.py" ]
FROM python:3-alpine
ENV FLASK_APP=server.py
COPY ["example-1/requirements.txt", \
"example-1/server.py", \
"example-1/conftest.py", \
"example-1/tests.py", \
"example-1/pytest.ini", "/app/"]
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
CMD ["flask", "run", "--host=0.0.0.0"]
import pytest
@pytest.fixture
def app():
from server import app
return app
from flask import Flask
app = Flask(__name__)
@app.route('/ping')
def ping():
return '<h1>Pong!</h1>'
def test_ping_success_via_get(client):
response = client.get('/ping')
assert 200 == response.status_code
def test_ping_failure_via_post(client):
response = client.post('/ping')
assert 405 == response.status_code
def test_ping_answer_is_pong(client):
response = client.get('/ping')
assert 200 == response.status_code
assert 'Pong!' in str(response.data)
FROM python:3-alpine
ENV FLASK_APP=server.py
COPY ["example-2/requirements.txt", \
"example-2/server.py", \
"example-2/conftest.py", \
"example-2/tests.py", \
"example-2/pytest.ini", "/app/"]
WORKDIR /app
RUN pip install --no-cache-dir -r requirements.txt
CMD ["flask", "run", "--host=0.0.0.0"]
import pytest
@pytest.fixture
def app():
from server import app
return app
[pytest]
norecursedirs = include lib
testpaths = tests.py
flask
flask-redis
pytest-flask
from flask import Flask
from flask import request
from flask_redis import FlaskRedis
import os
app = Flask(__name__)
REDIS_URL = os.environ.get('REDIS_URL', os.environ.get('REDIS_PORT'))
if REDIS_URL:
app.config.update({'REDIS_URL': REDIS_URL})
redis_store = FlaskRedis(app)
@app.route('/ping')
def ping_advanced():
key = request.environ.get('REMOTE_ADDR', 'test')
try:
value = int(redis_store.get(key)) + 1
except TypeError:
value = 1
redis_store.set(key, value)
return '<h1>Pong!</h1><h2>You sent %d pings</h2>' % value
import pytest
def test_ping_success_via_get(client):
response = client.get('/ping')
assert 200 == response.status_code
def test_ping_failure_via_post(client):
response = client.post('/ping')
assert 405 == response.status_code
def test_ping_answer_is_pong(client):
response = client.get('/ping')
assert 200 == response.status_code
assert 'Pong!' in str(response.data)
@pytest.fixture
def empty_redis():
from server import redis_store
redis_store.set('test', 0)
@pytest.mark.parametrize('requests,pings', [
(1, 1),
(2, 2),
(10, 10),
])
def test_ping_answer_is_pong_with_counter(client, empty_redis, requests, pings): # NOQA
for i in range(0, requests):
response = client.get('/ping')
assert 200 == response.status_code
assert '%d pings' % (pings) in str(response.data)
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response
def hello_world(request):
return Response('Hello %(name)s!' % request.matchdict)
if __name__ == '__main__':
config = Configurator()
config.add_route('hello', '/hello/{name}')
config.add_view(hello_world, route_name='hello')
app = config.make_wsgi_app()
server = make_server('0.0.0.0', 8080, app)
server.serve_forever()
from pyramid import testing
import pytest
@pytest.fixture
def req():
req = testing.DummyRequest(path='/hello/daniel')
req.matchdict = {'name': 'daniel'}
return req
def test_hello_world(req):
from server import hello_world
response = hello_world(req)
assert 200 == response.status_code
def test_hello_world_to_daniel(req):
from server import hello_world
response = hello_world(req)
assert 'daniel' in str(response.body)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment