From 644c2df8884623c36c6a7c4582058af883b4a504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kov=C3=A1cs=20Bence?= Date: Fri, 8 May 2020 22:17:09 +0200 Subject: [PATCH 1/3] unit test endpoint --- .drone.yml | 13 +++++++++++ curl-test.sh | 6 +++++ docker-compose.yml | 11 +++++++++ endpoints.py | 4 ++-- redisclient.py | 2 +- requirements.txt | 6 ++++- test.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 curl-test.sh create mode 100644 docker-compose.yml create mode 100644 test.py diff --git a/.drone.yml b/.drone.yml index c79412e..05488e7 100644 --- a/.drone.yml +++ b/.drone.yml @@ -27,6 +27,15 @@ steps: - find . -name "*.py" -exec python3 -m mccabe --min 3 '{}' + || if [ $? -eq 1 ]; then echo "you fail"; fi - bandit -r . + || if [ $? -eq 1 ]; then echo "you fail"; fi +- name: unit_test + image: python:3.8 + environment: + REDIS_URL: "redis://cache" + commands: + - pip3 install -r requirements.txt + - pytest test.py + + - name: rebuild-cache-with-filesystem image: meltwater/drone-cache pull: true @@ -65,6 +74,10 @@ steps: when: status: [ failure ] +services: + - name: cache + image: redis + volumes: - name: cache temp: {} diff --git a/curl-test.sh b/curl-test.sh new file mode 100644 index 0000000..cb8302e --- /dev/null +++ b/curl-test.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +curl --header "Content-Type: application/json" \ + --request POST \ + --data '{"uuid":"c959ad81-58f9-4445-aab4-8f3d68aee1ad", "ip":"127.1.2.3"}' \ + http://localhost:5000/ip diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..134cf5f --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' + +networks: + redis: + external: false + +services: + redis: + image: redis + ports: + - 6969:6379 diff --git a/endpoints.py b/endpoints.py index 34aa678..8afd295 100644 --- a/endpoints.py +++ b/endpoints.py @@ -1,6 +1,6 @@ import json from redisclient import redis_client -from flask import jsonify, request, Response +from flask import request, Response from flask_classful import FlaskView @@ -14,4 +14,4 @@ class IPEndpoint(FlaskView): strconsumer = json.dumps(consumer).encode('utf-8') redis_client.set('currentConsumer', strconsumer) - return Response(status=204) \ No newline at end of file + return Response(status=204) diff --git a/redisclient.py b/redisclient.py index 701805e..90e8358 100644 --- a/redisclient.py +++ b/redisclient.py @@ -1,3 +1,3 @@ from flask_redis import FlaskRedis -redis_client = FlaskRedis() \ No newline at end of file +redis_client = FlaskRedis() diff --git a/requirements.txt b/requirements.txt index a612a78..c8c4a3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,9 @@ flask flask-classful +redis flask-redis gunicorn -sentry-sdk[flask] \ No newline at end of file +sentry-sdk[flask] +pytest +pytest-flask +pytest-redis \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..cfb886a --- /dev/null +++ b/test.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +""" +Unit tests for the producer-endpoint module. +""" + +import os +import json +import pytest +import redis +from pytest_redis import factories +import app + + +generateduuid = 'c959ad81-58f9-4445-aab4-8f3d68aee1ad' +redis_proc = factories.redis_proc(host='cache', port=6379) +redis_db = factories.redisdb('redis_nooproc') +redstuff = redis.Redis.from_url(url=os.environ['REDIS_URL']) +redstuff.set('currentConsumer', json.dumps({'uuid': generateduuid, 'Host': "127.2.2.2"}).encode('utf-8')) + +@pytest.fixture +def client(): + """Client fixture""" + with app.app.test_client() as client: + yield client + + +def test_set_currentconsumer_success(client): + + r = client.post("/ip", json={'uuid': generateduuid, 'ip': "127.1.2.3"}) + + json_in_redis = json.loads(redstuff.get('currentConsumer')) + + assert r.status_code == 204 + assert json_in_redis == {'uuid': generateduuid, 'Host': "127.1.2.3"} + + +def test_set_currentconsumer_server_error(client): + + r = client.post("/ip", json="unexpected") + + assert r.status_code == 500 + + +def test_set_currentconsumer_notfound(client): + + r = client.post("/test") + + assert r.status_code == 404 + + +def test_set_currentconsumer_method_not_allowed(client): + + r = client.get("/ip") + + assert r.status_code == 405 From b282b6a3733ba5d1d31e5f2907d2fcc9f05870e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kov=C3=A1cs=20Bence?= Date: Fri, 8 May 2020 22:19:21 +0200 Subject: [PATCH 2/3] play with ci --- .drone.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 05488e7..0d2ce9c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -32,7 +32,7 @@ steps: environment: REDIS_URL: "redis://cache" commands: - - pip3 install -r requirements.txt + - pip3 install --cache-dir='./.pipcache' -r requirements.txt - pytest test.py @@ -80,4 +80,4 @@ services: volumes: - name: cache - temp: {} + host: /tmp/cache From beaff9f62bd52583044ca633e7894368b1467b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kov=C3=A1cs=20Bence?= Date: Fri, 8 May 2020 22:20:34 +0200 Subject: [PATCH 3/3] fix my mistake --- .drone.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 0d2ce9c..a53b9aa 100644 --- a/.drone.yml +++ b/.drone.yml @@ -80,4 +80,5 @@ services: volumes: - name: cache - host: /tmp/cache + host: + path: "/tmp/cache"