def test_events(self): pings = [] def starts(request): pings.append('starts') def ends(response): pings.append('ends') subscribe(REQUEST_STARTS, starts) subscribe(REQUEST_ENDS, ends) try: config = { 'global.heartbeat_page': '__heartbeat__', 'global.debug_page': '__debug__', 'auth.backend': 'services.auth.dummy.DummyAuth' } urls = [] controllers = {} app = SyncServerApp(urls, controllers, config, auth_class=self.auth_class) request = make_request("/user/__hearbeat__") app(request) finally: unsubscribe(REQUEST_STARTS, starts) unsubscribe(REQUEST_ENDS, ends) self.assertEquals(pings, ['starts', 'ends'])
def test_nosigclean(self): # check that we can deactivate sigterm/sigint hooks pings = [] def end(): pings.append('app ends') subscribe(APP_ENDS, end) try: config = { 'global.heartbeat_page': '__heartbeat__', 'global.debug_page': '__debug__', 'auth.backend': 'services.auth.dummy.DummyAuth', 'global.clean_shutdown': False } urls = [] controllers = {} app = SyncServerApp(urls, controllers, config, auth_class=self.auth_class) # heartbeat should work request = make_request("/__heartbeat__") app(request) finally: unsubscribe(APP_ENDS, end) # and we should have had no ping self.assertEquals(pings, [])
def test_heartbeat_debug_pages(self): config = { 'global.heartbeat_page': '__heartbeat__', 'global.debug_page': '__debug__', 'app.modules': ['metlog_loader'], 'metlog_loader.backend': 'services.metrics.MetlogLoader', 'metlog_loader.config': metlog_cfg_path, 'auth.backend': 'services.auth.dummy.DummyAuth' } urls = [] controllers = {} # testing the default configuration app = SyncServerApp(urls, controllers, config, auth_class=self.auth_class) # a heartbeat returns a 200 / empty body request = make_request("/__heartbeat__") res = app(request) self.assertEqual(res.status_int, 200) self.assertEqual(res.body, '') # we can get heartbeats with a HEAD call request = make_request("/__heartbeat__", method="HEAD") res = app(request) self.assertEqual(res.status_int, 200) self.assertEqual(res.body, '') # the debug page returns a 200 / info in the body request = make_request("/__debug__") res = app(request) self.assertEqual(res.status_int, 200) self.assertTrue("'REQUEST_METHOD': 'GET'" in res.body) # now let's create an app with extra heartbeating # and debug info class MyCoolApp(SyncServerApp): def _debug_server(self, request): return ['DEEBOOG'] def _check_server(self, request): raise HTTPServiceUnavailable() # testing that new app app = MyCoolApp(urls, controllers, config, auth_class=self.auth_class) # a heartbeat returns a 503 / empty body request = make_request("/__heartbeat__") self.assertRaises(HTTPServiceUnavailable, app, request) # the debug page returns a 200 / info in the body request = make_request("/__debug__") res = app(request) self.assertEqual(res.status_int, 200) self.assertTrue("DEEBOOG" in res.body)
def test_retry_after(self): config = {'global.retry_after': 60, 'auth.backend': 'dummy'} urls = [('GET', '/boom', 'foo', 'boom')] controllers = {'foo': _Foo} app = SyncServerApp(urls, controllers, config) request = _Request('GET', '/boom', 'localhost') try: app(request) except HTTPServiceUnavailable, error: self.assertEqual(error.headers['Retry-After'], '60')
def setUp(self): urls = [('POST', '/', 'foo', 'index'), ('GET', '/secret', 'foo', 'secret', { 'auth': True })] controllers = {'foo': _Foo} config = { 'host:here.one.two': 1, 'one.two': 2, 'auth.backend': 'dummy' } self.app = SyncServerApp(urls, controllers, config)
def test_heartbeat_debug_pages(self): config = { 'global.heartbeat_page': '__heartbeat__', 'global.debug_page': '__debug__', 'auth.backend': 'dummy' } urls = [] controllers = {} # testing the default configuration app = SyncServerApp(urls, controllers, config) # a heartbeat returns a 200 / empty body request = _Request('GET', '/__heartbeat__', 'localhost') res = app(request) self.assertEqual(res.status_int, 200) self.assertEqual(res.body, '') # the debug page returns a 200 / info in the body request = _Request('GET', '/__debug__', 'localhost') res = app(request) self.assertEqual(res.status_int, 200) self.assertTrue("'REQUEST_METHOD': 'GET'" in res.body) # now let's create an app with extra heartbeating # and debug info class MyCoolApp(SyncServerApp): def _debug_server(self, request): return ['DEEBOOG'] def _check_server(self, request): raise HTTPServiceUnavailable() # testing that new app app = MyCoolApp(urls, controllers, config) # a heartbeat returns a 503 / empty body request = _Request('GET', '/__heartbeat__', 'localhost') self.assertRaises(HTTPServiceUnavailable, app, request) # the debug page returns a 200 / info in the body request = _Request('GET', '/__debug__', 'localhost') res = app(request) self.assertEqual(res.status_int, 200) self.assertTrue("DEEBOOG" in res.body)
def test_retry_after(self): config = { 'global.retry_after': 60, 'auth.backend': 'services.auth.dummy.DummyAuth', 'app.modules': ['metlog_loader'], 'metlog_loader.backend': 'services.metrics.MetlogLoader', 'metlog_loader.config': metlog_cfg_path, } urls = [('GET', '/boom', 'foo', 'boom'), ('GET', '/boom2', 'foo', 'boom2'), ('GET', '/boom3', 'foo', 'boom3')] controllers = {'foo': _Foo} app = SyncServerApp(urls, controllers, config, auth_class=self.auth_class) request = make_request("/boom", method="GET", host="localhost") try: app(request) except HTTPServiceUnavailable, error: self.assertEqual(error.headers['Retry-After'], '10')
def test_graceful_shutdown(self): pings = [] def end(): pings.append('app ends') subscribe(APP_ENDS, end) try: config = { 'global.heartbeat_page': '__heartbeat__', 'global.debug_page': '__debug__', 'auth.backend': 'services.auth.dummy.DummyAuth', 'global.graceful_shutdown_interval': 1, 'global.hard_shutdown_interval': 1 } urls = [] controllers = {} app = SyncServerApp(urls, controllers, config, auth_class=self.auth_class) # heartbeat should work request = make_request("/__heartbeat__") app(request) # let's "kill it" in a thread class Killer(threading.Thread): def __init__(self, app): threading.Thread.__init__(self) self.app = app def run(self): self.app._sigterm(None, None) killer = Killer(app) killer.start() sleep(0.2) # in the meantime, /heartbeat should return a 503 request = make_request("/__heartbeat__") self.assertRaises(HTTPServiceUnavailable, app, request) # but regular requests should still work request = make_request("/") app(request) # sleeping sleep(1.) # now / should 503 too request = make_request("/") self.assertRaises(HTTPServiceUnavailable, app, request) killer.join() finally: unsubscribe(APP_ENDS, end) # and we should have had a event ping self.assertEquals(pings, ['app ends'])
def setUp(self): self.app = SyncServerApp(self.urls, self.controllers, self.config, auth_class=self.auth_class)