From f0fd33fb11513a3e74079eaed5f5c1b8bb4e561d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Thu, 21 Jun 2012 15:39:40 +1200 Subject: [PATCH] Log inspection and manipulation from tests. --- libpathod/app.py | 43 +++++++++++++------------------------------ libpathod/test.py | 20 ++++++++++++++++++++ test/test_pathod.py | 16 ---------------- test/test_test.py | 9 ++++++++- 4 files changed, 41 insertions(+), 47 deletions(-) diff --git a/libpathod/app.py b/libpathod/app.py index 95e7d169c..1e65bc4fc 100644 --- a/libpathod/app.py +++ b/libpathod/app.py @@ -9,37 +9,20 @@ def api_info(): ) +@app.route('/api/log') +def api_log(): + return jsonify( + log = app.config["pathod"].get_log() + ) + + +@app.route('/api/clear_log') +def api_clear_log(): + app.config["pathod"].clear_log() + return "OK" + + """ -class APILog(tornado.web.RequestHandler): - def get(self): - self.write( - dict( - d = self.application.get_log() - ) - ) - - -class APILogClear(tornado.web.RequestHandler): - def post(self): - self.application.clear_log() - self.write("OK") - - -class APIShutdown(tornado.web.RequestHandler): - def post(self): - tornado.ioloop.IOLoop.instance().stop() - 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) diff --git a/libpathod/test.py b/libpathod/test.py index 5fba17d53..943fe3c06 100644 --- a/libpathod/test.py +++ b/libpathod/test.py @@ -14,10 +14,30 @@ class Daemon: self.urlbase = "%s://%s:%s"%("https" if ssl else "http", IFACE, self.port) def info(self): + """ + Return some basic info about the remote daemon. + """ resp = requests.get("%s/api/info"%self.urlbase, verify=False) return resp.json + def log(self): + """ + Return the log buffer as a list of dictionaries. + """ + resp = requests.get("%s/api/log"%self.urlbase, verify=False) + return resp.json["log"] + + def clear_log(self): + """ + Clear the log. + """ + resp = requests.get("%s/api/clear_log"%self.urlbase, verify=False) + return resp.ok + def shutdown(self): + """ + Shut the daemon down, return after the thread has exited. + """ self.thread.server.shutdown() self.thread.join() diff --git a/test/test_pathod.py b/test/test_pathod.py index 9c16748d1..966ae12ec 100644 --- a/test/test_pathod.py +++ b/test/test_pathod.py @@ -12,22 +12,6 @@ class _TestApplication: a.remove_anchor("/oink", "400") assert a.get_anchors() == [("/foo", "200")] - def test_logs(self): - a = pathod.PathodApp(staticdir=None) - a.LOGBUF = 3 - a.add_log({}) - assert a.log[0]["id"] == 0 - a.add_log({}) - a.add_log({}) - assert a.log[0]["id"] == 2 - a.add_log({}) - assert len(a.log) == 3 - assert a.log[0]["id"] == 3 - assert a.log[-1]["id"] == 1 - - assert a.log_by_id(1)["id"] == 1 - assert not a.log_by_id(0) - class TestPathod: def test_instantiation(self): diff --git a/test/test_test.py b/test/test_test.py index c3743a42e..36d77fd5f 100644 --- a/test/test_test.py +++ b/test/test_test.py @@ -6,7 +6,7 @@ import tutils logging.disable(logging.CRITICAL) class TestDaemonManual: - def test_startstop(self): + def test_simple(self): d = test.Daemon() rsp = requests.get("http://localhost:%s/p/202"%d.port) assert rsp.ok @@ -46,3 +46,10 @@ class TestDaemon: def test_info(self): assert tuple(self.d.info()["version"]) == version.IVERSION + + def test_logs(self): + rsp = requests.get("http://localhost:%s/p/202"%self.d.port) + assert len(self.d.log()) == 1 + assert self.d.clear_log() + assert len(self.d.log()) == 0 +