Skip to content

Commit

Permalink
Test unit for the endpoint server
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalphettes committed Nov 26, 2017
1 parent f6661f4 commit e5618e6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Dockerfile
@@ -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" ]
6 changes: 6 additions & 0 deletions README.rst
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
@@ -1,2 +1,3 @@
boto3>=1.4.7
tabulate>=0.8.1
tabulate>=0.8.1
pytz
19 changes: 12 additions & 7 deletions server.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
20 changes: 20 additions & 0 deletions test_s3_storage_analyzer.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.