Permalink
Browse files

Test unit for the endpoint server

  • Loading branch information...
hmalphettes committed Nov 26, 2017
1 parent f6661f4 commit e5618e65f25b2f265c9b64531356a960b558f7e4
Showing with 44 additions and 10 deletions.
  1. +4 −2 Dockerfile
  2. +6 −0 README.rst
  3. +2 −1 requirements.txt
  4. +12 −7 server.py
  5. +20 −0 test_s3_storage_analyzer.py
View
@@ -1,10 +1,12 @@
FROM python:3-slim
COPY requirements.txt /tmp/
RUN pip install --requirement /tmp/requirements.txt && \
pip install pytz s3cmd
RUN pip install --requirement /tmp/requirements.txt
COPY *.py /app/
WORKDIR /app
USER nobody
ENTRYPOINT [ "python3", "-m" ]
CMD [ "s3_storage_analyser" ]
View
@@ -110,6 +110,12 @@ To run the REST endpoint for development:
TOKEN=secret python3 -m server
Via docker:
::
docker run -e TOKEN=secret --name s3analyser_endpoint --net host -d hmalphettes/s3-storage-analyser server
Continuous Integration - Continuous Delivery
--------------------------------------------
The CI is graciously operated by Travis: https://travis-ci.org/hmalphettes/s3-storage-analyser
View
@@ -1,2 +1,3 @@
boto3>=1.4.7
tabulate>=0.8.1
tabulate>=0.8.1
pytz
View
@@ -25,7 +25,7 @@ def do_GET(self):
self.end_headers()
return
token = os.environ['TOKEN'] or 's3cr3t'
token = os.environ['TOKEN']
query_components = {
'token': None,
@@ -130,10 +130,15 @@ def _run_analysis(unit=None, prefix=None, conc='6', fmt=None, echo=False):
print('Exited RUNNING_ANALYSIS')
LOCK_ANALYSIS.release()
if __name__ == '__main__':
PORT = 8000
def make_server(do_print=False):
"""Main entrypoint"""
port = 8000
if 'S3ANALYSER_PORT' in os.environ:
PORT = int(os.environ['S3ANALYSER_PORT'])
SERVER = HTTPServer(('localhost', PORT), RequestHandler)
print(f'Starting s3analyser endpoint at http://localhost:{PORT}')
SERVER.serve_forever()
port = int(os.environ['S3ANALYSER_PORT'])
if do_print:
print(f'Starting s3analyser endpoint at http://localhost:{port}')
server = HTTPServer(('localhost', port), RequestHandler)
return server
if __name__ == '__main__':
make_server(do_print=True).serve_forever()
@@ -4,12 +4,16 @@
from datetime import datetime
from io import StringIO
import sys
import os
import threading
import http.client
from contextlib import redirect_stdout
from s3_storage_analyser import (
list_buckets, fold_metrics_data, convert_bytes,
main, list_metrics, get_metrics_data, _today)
import s3_storage_analyser
import server
from moto import mock_s3, mock_cloudwatch
import boto3
@@ -234,3 +238,19 @@ def test_main_wrong_prefix(monkeypatch):
assert 'Invalid prefix' in err.__str__()
return
raise Exception('No ValueError was raised although the prefix was wrong')
@mock_cloudwatch
@mock_s3
def test_server(monkeypatch):
"""Test whole server"""
_setup(monkeypatch)
http_server = server.make_server()
os.environ['TOKEN'] = 'hi'
threading.Thread(target=http_server.serve_forever).start()
conn = http.client.HTTPConnection('localhost:8000')
conn.request('GET', '/api/?token=hi&format=json')
res = conn.getresponse()
assert res.status == 200
data = res.read().decode()
assert data.startswith('{"Buckets": [{"Bucket": "hm.samples"')
http_server.shutdown()

0 comments on commit e5618e6

Please sign in to comment.