Skip to content

Commit

Permalink
Improving unittests and refactoring server
Browse files Browse the repository at this point in the history
  • Loading branch information
geonexus committed Jul 28, 2015
1 parent 6dc60e6 commit 6032724
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ git clone git@github.com:telefonicaid/fiware-facts.git
Usage
=====

Execute command "gunicorn --check-config server.py" inside the folder where you downloaded fiware-facts
Execute command "gunicorn facts.server:app" inside the folder where you downloaded fiware-facts

Changelog
=========
Expand Down
1 change: 0 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ else
echo "Building with travis"
fi

python server.py &
export PYTHONPATH=$PWD
nosetests -s -v --cover-package=facts --with-cover --cover-xml-file=target/site/cobertura/coverage.xml --cover-xml
sudo /sbin/service rabbitmq-server stop
Expand Down
36 changes: 27 additions & 9 deletions server.py → facts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@
from gevent.pywsgi import WSGIServer
from keystoneclient.exceptions import NotFound
from facts.config import fact_attributes
from facts import cloto_db_client
import logging.config
import sys
import datetime
import gevent.monkey
import os
import httplib
import gevent


gevent.monkey.patch_all()
Expand All @@ -60,6 +62,12 @@
"""
mredis = myredis()

"""
Initialize the mysql connection library
"""

myClotoDBClient = cloto_db_client.cloto_db_client()

"""
Initialize the pid of the process
"""
Expand Down Expand Up @@ -93,9 +101,13 @@ def facts(tenantid, serverid):
if request.headers['content-type'] == content_type:
try:
# Ensure that received data is a valid JSON
print "Esto es request: %s" % request.data

user_submission = json.loads(request.data) # @UnusedVariable
except ValueError:
except ValueError as v:
# Data is not a well-formed json
print "ESTO ES EL ERROR: %s" % v

message = "[{}] received {} from ip {}:{}"\
.format("-", json, request.environ['REMOTE_ADDR'], request.environ['REMOTE_PORT'])

Expand Down Expand Up @@ -135,9 +147,12 @@ def process_request(request, tenantid, serverid):
:return: True
"""
json = request.json
message = "[{}] received {} from ip {}:{}"\
.format("-", json, request.environ['REMOTE_ADDR'], request.environ['REMOTE_PORT'])

if request.remote_addr:
message = "[{}] received {} from ip {}:{}"\
.format("-", json, request.environ['REMOTE_ADDR'], request.environ['REMOTE_PORT'])
else:
message = "[{}] received {} from test client"\
.format("-", json)
logging.info(message)

key = ['contextResponses', 'contextElement', 'attributes']
Expand Down Expand Up @@ -179,8 +194,6 @@ def process_request(request, tenantid, serverid):
# Get the windowsize for the tenant from a redis queue
windowsize = mredis.get_windowsize(tenantid)
if windowsize == []:
from facts import cloto_db_client
myClotoDBClient = cloto_db_client.cloto_db_client()
windowsize = myClotoDBClient.get_window_size(tenantid)
mredis.insert_window_size(tenantid, windowsize)

Expand All @@ -194,7 +207,7 @@ def process_request(request, tenantid, serverid):
rabbit = myqueue()

message = "{\"serverId\": \"%s\", \"cpu\": %s, \"mem\": %s, \"hdd\": %s, \"net\": %s, \"time\": \"%s\"}" \
% (lo.data[0], lo.data[1], lo.data[2], lo.data[3], lo.data[4], lo.data[5])
% (lo.data[0][1:-1], lo.data[1], lo.data[2], lo.data[3], lo.data[4], lo.data[5])

logging_message = "[{}] sending message {}".format("-", message)

Expand Down Expand Up @@ -286,6 +299,11 @@ def callback(ch, method, properties, body):
else:
connection.close()

import gevent
gevent.spawn(windowsize_updater)
http.serve_forever()


def start_server():
http.serve_forever()

if __name__ == '__main__':
start_server()
113 changes: 100 additions & 13 deletions tests/test_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,75 @@

from flask.ext.testing import TestCase
from flask import Flask
from mockito import *
from mock import patch, MagicMock
import urllib2
import json
import unittest
from facts import server as server


class MyCursor(MagicMock):

# Mock of a mysql cursor
result = None

def execute(self, query):
# Generates mocked results depending of the MYSQL query content
if query.startswith("SELECT * FROM ") \
and query.__contains__("cloto.cloto_tenantinfo")\
and query.__contains__("WHERE tenantId=\"tenantId\""):
self.result = [["tenantId", 5]]

def fetchall(self):
# returns the result of the query
return self.result


class MyAppTest(unittest.TestCase):

@classmethod
def setUpClass(self):
# Put Flask into TESTING mode for the TestClient
server.app.config['TESTING'] = True
# Disable CSRF checking for WTForms
server.app.config['WTF_CSRF_ENABLED'] = False
# Point SQLAlchemy to a test database location
# (set in virtualenv normally, but fall back to sqlite if not defined)
self.app = server.app.test_client()
self.app.post()


"""Class to test the flask, gevent process
"""


class MyTest(TestCase):
def create_app(self):
app = Flask(__name__)
app.config['TESTING'] = True
self.url = 'http://127.0.0.1:5000/v1.0/33/servers/44'
self.url2 = 'http://127.0.0.1:5000/v1.0'
return app
class MyTest(MyAppTest):

def setUp(self):
self.tenantId = "tenantId"
self.mockedClient = mock()
mockedCursor = MyCursor()
when(self.mockedClient).cursor().thenReturn(mockedCursor)
server.myClotoDBClient.conn = self.mockedClient
self.url = '/v1.0/33/servers/44'
self.url2 = '/v1.0'
self.url3 = '/v1.0/tenantId/servers/44'

def test_server_is_up_and_running(self):
""" Test that a fiware-facts is up and running and return
information of the GET operation
:return 200 Ok
"""
response = urllib2.urlopen(self.url2)
self.assertEqual(response.code, 200)
response = self.app.get(self.url2)
self.assertEqual(response.status_code, 200)

def test_some_json(self):
""" Test that the POST operation over the API returns a error if
content-type is not application/json.
:return 500 Ok
:return 500 Internal Server error
"""
data = {'ids': [12, 3, 4, 5, 6]}

Expand All @@ -61,8 +101,55 @@ def test_some_json(self):
req.add_header('Content-Type', 'application/json')

try:
response = urllib2.urlopen(req, json.dumps(data))
response = self.app.post(self.url, json.dumps(data))

except (urllib2.HTTPError), err:
self.assertEqual(err.code, 500)
self.assertEqual(err.msg, "INTERNAL SERVER ERROR")
self.assertEqual(err.status_code, 500)
self.assertEqual(err.data, "INTERNAL SERVER ERROR")

def test_context_broker_message(self):
""" Test that the POST operation over the API returns a valid response if message is built correctly.
:return 200 Ok
"""
data = {'ids': [12, 3, 4, 5, 6]}
data2 = {"contextResponses": [
{
"contextElement": {
"attributes": [
{
"value": "99.12",
"name": "usedMemPct",
"type": "string"
},
{
"value": "99.14",
"name": "cpuLoadPct",
"type": "string"
},
{
"value": "99.856240",
"name": "freeSpacePct",
"type": "string"
},
{
"value": "99.8122",
"name": "netLoadPct",
"type": "string"
}
],
"id": "Trento:193.205.211.69",
"isPattern": "false",
"type": "host"
},
"statusCode": {
"code": "200",
"reasonPhrase": "OK"
}

}
]
}

response = self.app.post(self.url3, data=json.dumps(data2), headers={'Content-Type': 'application/json'})
self.assertEqual(response.status_code, 200)

0 comments on commit 6032724

Please sign in to comment.