From 34ffe46fa0474a47bc737669ccd58fe036e3a033 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 7 Jun 2012 11:39:37 +1200 Subject: [PATCH] Add /api/info, returning general info on the running pathod daemon. --- libpathod/pathod.py | 14 ++++++++++++-- libpathod/test.py | 16 ++++++++++------ test/test_test.py | 16 ++++++++++++++-- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 9b0f4ac17..6012abc1b 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -1,6 +1,6 @@ import urllib, pprint import tornado.web, tornado.template, tornado.ioloop, tornado.httpserver -import rparse, utils +import rparse, utils, version class APILog(tornado.web.RequestHandler): @@ -24,6 +24,15 @@ class APIShutdown(tornado.web.RequestHandler): self.write("OK") +class APIInfo(tornado.web.RequestHandler): + def get(self): + self.write( + dict( + version = version.IVERSION + ) + ) + + class _Page(tornado.web.RequestHandler): def render(self, name, **kwargs): tornado.web.RequestHandler.render(self, name + ".html", **kwargs) @@ -141,6 +150,7 @@ class PathodApp(tornado.web.Application): (r"/help", Help), (r"/preview", Preview), (r"/api/shutdown", APIShutdown), + (r"/api/info", APIInfo), (r"/api/log", APILog), (r"/api/log/clear", APILogClear), (r"/p/.*", RequestPathod, settings), @@ -228,7 +238,7 @@ def make_app(staticdir=None, anchors=()): def make_server(application, port, address, ssl_options): """ - Returns a (server, port) tuple. + Returns a (server, port) tuple. The returned port will match the passed port, unless the passed port was 0. In that case, an arbitrary empty port will be bound to, and this diff --git a/libpathod/test.py b/libpathod/test.py index 6728b60cd..23b399d9c 100644 --- a/libpathod/test.py +++ b/libpathod/test.py @@ -1,8 +1,9 @@ -import threading +import json, threading, Queue import requests -import Queue import pathod +IFACE = "127.0.0.1" + class PaThread(threading.Thread): def __init__(self, q, app): threading.Thread.__init__(self) @@ -11,7 +12,7 @@ class PaThread(threading.Thread): self.port = None def run(self): - self.server, self.port = pathod.make_server(self.app, 0, "127.0.0.1", None) + self.server, self.port = pathod.make_server(self.app, 0, IFACE, None) self.q.put(self.port) pathod.run(self.server) @@ -23,9 +24,12 @@ class Daemon: self.thread = PaThread(self.q, self.app) self.thread.start() self.port = self.q.get(True, 5) + self.urlbase = "http://%s:%s"%(IFACE, self.port) - def clear(self): - pass + def info(self): + resp = requests.get("%s/api/info"%self.urlbase) + if resp.ok: + return json.loads(resp.read()) def shutdown(self): - requests.post("http://localhost:%s/api/shutdown"%self.port) + requests.post("%s/api/shutdown"%self.urlbase) diff --git a/test/test_test.py b/test/test_test.py index 6e80dd772..c10cee4cd 100644 --- a/test/test_test.py +++ b/test/test_test.py @@ -1,10 +1,10 @@ import time import libpry import requests -from libpathod import test +from libpathod import test, version -class uDaemon(libpry.AutoTree): +class uDaemonManual(libpry.AutoTree): def test_startstop(self): d = test.Daemon() rsp = requests.get("http://localhost:%s/p/202"%d.port) @@ -15,7 +15,19 @@ class uDaemon(libpry.AutoTree): assert not rsp.ok +class uDaemon(libpry.AutoTree): + def setUpAll(self): + self.d = test.Daemon() + + def tearDownAll(self): + self.d.shutdown() + + def test_info(self): + assert tuple(self.d.info()["version"]) == version.IVERSION + + tests = [ + uDaemonManual(), uDaemon() ]