Skip to content
Snippets Groups Projects
Commit defa54fe authored by Jorrit Schaap's avatar Jorrit Schaap
Browse files

Task #8291 added initial version of flask based webservice and tests

parent 40c1c3c7
No related branches found
No related tags found
No related merge requests found
......@@ -2831,10 +2831,14 @@ LTA/ltastorageoverview/bin/ltastorageoverviewscraper -text
LTA/ltastorageoverview/lib/CMakeLists.txt -text
LTA/ltastorageoverview/lib/scraper.py -text
LTA/ltastorageoverview/lib/store.py -text
LTA/ltastorageoverview/lib/webservice/webservice.py -text
LTA/ltastorageoverview/test/CMakeLists.txt -text
LTA/ltastorageoverview/test/test_store.py -text
LTA/ltastorageoverview/test/test_store.run -text
LTA/ltastorageoverview/test/test_store.sh -text
LTA/ltastorageoverview/test/test_webservice.py -text
LTA/ltastorageoverview/test/test_webservice.run -text
LTA/ltastorageoverview/test/test_webservice.sh -text
MAC/APL/APLCommon/include/APL/APLCommon/AntennaField.h -text
MAC/APL/APLCommon/src/AntennaField.cc -text
MAC/APL/APLCommon/src/StartDaemon_Protocol.prot -text svneol=native#application/octet-stream
......
/.kdev4
CEP/Calibration/BBSControl/Makefile.common
CEP/Calibration/BBSControl/autoconf_share
CEP/Calibration/BBSControl/lofarconf
......@@ -255,3 +256,5 @@ SAS/OTB/nbproject
SAS/OTDB/Makefile.common
SAS/OTDB/autoconf_share
SAS/OTDB/lofarconf
/build
/wcblofar.kdev4
......@@ -3,5 +3,6 @@
python_install(
scraper.py
store.py
webservice/webservice.py
DESTINATION ltastorageoverview)
#!/usr/bin/python
# Copyright (C) 2012-2015 ASTRON (Netherlands Institute for Radio Astronomy)
# P.O. Box 2, 7990 AA Dwingeloo, The Netherlands
#
# This file is part of the LOFAR software suite.
# The LOFAR software suite is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# The LOFAR software suite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
import sys
import os
import os.path
import flask
import threading
from ltastorageoverview import store
app = flask.Flask('LTA storage overview')
db = None
def run():
app.run()
def runThreaded():
serviceThread = threading.Thread(target=run)
serviceThread.daemon = True
serviceThread.start()
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/json/sites/')
def get_sites():
sites = {'sites' : [ {'id':x[0], 'name':x[1], 'url':x[2] } for x in db.sites()]}
return flask.json.jsonify(sites)
@app.route('/json/sites/<int:site_id>')
def get_site(site_id):
site = db.site.site(site_id)
site_dict = {'id':site[0], 'name':site[1], 'url':site[2] }
return flask.json.jsonify(site_dict)
def main(argv):
db = store.LTAStorageDb(argv[0] if argv else 'ltastoragedb.sqlite')
run()
if __name__ == '__main__':
main(sys.argv[1:])
......@@ -2,8 +2,10 @@
include(LofarCTest)
lofar_add_test(test_store)
lofar_add_test(test_webservice)
python_install(
test_store.py
test_webservice.py
DESTINATION ltastorageoverview/test)
#!/usr/bin/env python
import unittest
import os
import time
import os.path
import tempfile
import urllib2
import json
from StringIO import StringIO
from flask.ext.testing import LiveServerTestCase as FlaskLiveTestCase
from ltastorageoverview import store
import ltastorageoverview.webservice.webservice as webservice
def setUpModule():
tmpfile = os.path.join(tempfile.gettempdir(), 'test.sqlite')
webservice.db = store.LTAStorageDb(tmpfile)
webservice.db.insertSite('siteA', 'srm://siteA.org')
webservice.db.insertSite('siteB', 'srm://siteB.org')
def tearDownModule():
if os.path.exists(webservice.db.db_filename):
os.remove(webservice.db.db_filename)
class TestLTAStorageWebService(FlaskLiveTestCase):
def create_app(self):
return webservice.app
def testSites(self):
response = urllib2.urlopen('http://localhost:5000/json/sites/')
self.assertEqual(200, response.code)
self.assertEqual('application/json', response.info()['Content-Type'])
content = json.load(StringIO(response.read()))
self.assertTrue('sites' in content)
sites = content['sites']
sitesDict = dict([(x['name'], x) for x in sites])
self.assertTrue('siteA' in sitesDict)
self.assertEqual('srm://siteA.org', sitesDict['siteA']['url'])
self.assertTrue('siteB' in sitesDict)
self.assertEqual('srm://siteB.org', sitesDict['siteB']['url'])
# run tests if main
if __name__ == '__main__':
unittest.main(verbosity=2)
#!/bin/bash
python test_webservice.py
#!/bin/sh
./runctest.sh test_webservice
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment