def _makeOne(self, request=None, callback=None): if request is None: request = DummyRequest(None, None, None, None, None) if callback is None: callback = Dummy() callback.delay = 1 return self._getTargetClass()(request, callback)
def test_handle_request_stdout_logfile_missing(self): supervisor = DummySupervisor() supervisor.options.logfile = '/not/there' request = DummyRequest('/mainlogtail', None, None, None) handler = self._makeOne(supervisor) handler.handle_request(request) self.assertEqual(request._error, 410)
def test_more_not_done_yet(self): request = DummyRequest('/index.html', [], '', '') from docker_supervisor.http import NOT_DONE_YET callback = lambda *x: NOT_DONE_YET callback.delay = 1 producer = self._makeOne(request, callback) self.assertEqual(producer.more(), NOT_DONE_YET)
def test_continue_request_no_params_in_request(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = '<?xml version="1.0" encoding="UTF-8"?>' \ '<methodCall>' \ '<methodName>supervisor.getAPIVersion</methodName>' \ '</methodCall>' request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data from docker_supervisor.xmlrpc import loads if loads: expected = 2 else: expected = 3 self.assertEqual(len(logdata), expected) self.assertEqual(logdata[-2], u'XML-RPC method called: supervisor.getAPIVersion()') self.assertEqual( logdata[-1], u'XML-RPC method supervisor.getAPIVersion() returned successfully') self.assertEqual(len(request.producers), 1) xml_response = request.producers[0] import xmlrpclib response = xmlrpclib.loads(xml_response) from docker_supervisor.rpcinterface import API_VERSION self.assertEqual(response[0][0], API_VERSION) self.assertEqual(request._done, True) self.assertEqual(request.headers['Content-Type'], 'text/xml') self.assertEqual(request.headers['Content-Length'], len(xml_response))
def test_handle_request_stdout_logfile_missing(self): options = DummyOptions() pconfig = DummyPConfig(options, 'foo', 'foo', 'it/is/missing') supervisord = PopulatedDummySupervisor(options, 'foo', pconfig) handler = self._makeOne(supervisord) request = DummyRequest('/logtail/foo', None, None, None) handler.handle_request(request) self.assertEqual(request._error, 410)
def test_handle_request_authorizes_good_password_with_colon(self): request = DummyRequest('/logtail/process1', None, None, None) encoded = base64.b64encode("user:pass:word") # password contains colon request.header = ["Authorization: Basic %s" % encoded] handler = DummyHandler() auth_handler = self._makeOne({'user': '******'}, handler) auth_handler.handle_request(request) self.assertTrue(handler.handled_request)
def test_handle_request_does_not_authorize_bad_credentials(self): request = DummyRequest('/logtail/process1', None, None, None) encoded = base64.b64encode("wrong:wrong") request.header = ["Authorization: Basic %s" % encoded] handler = DummyHandler() auth_handler = self._makeOne({'user': '******'}, handler) auth_handler.handle_request(request) self.assertFalse(handler.handled_request)
def test_ctor(self): request = DummyRequest('/index.html', [], '', '') callback = lambda *x: None callback.delay = 1 producer = self._makeOne(request, callback) self.assertEqual(producer.callback, callback) self.assertEqual(producer.request, request) self.assertEqual(producer.finished, False) self.assertEqual(producer.delay, 1)
def test_handle_request_default(self): request = DummyRequest('/index.html', [], '', '', {'PATH_INFO':'/index.html'}) handler = self._makeOne() data = handler.handle_request(request) self.assertEqual(data, None) self.assertEqual(request.channel.producer.request, request) from docker_supervisor.web import StatusView self.assertEqual(request.channel.producer.callback.__class__,StatusView)
def test_handle_more_fd_closed(self): request = DummyRequest('/logtail/foo', None, None, None) f = tempfile.NamedTemporaryFile() f.write('a' * 80) f.flush() producer = self._makeOne(request, f.name, 80) producer.file.close() result = producer.more() self.assertEqual(result, producer.more())
def test_handle_request_ok_html(self): request = DummyRequest('/tail.html', [], '', '', {'PATH_INFO':'/ok.html'}) handler = self._makeOne() handler.handle_request(request) from docker_supervisor.web import OKView view = request.channel.producer.callback self.assertEqual(view.__class__, OKView) self.assertEqual(view.context.template, None)
def test_handle_request_index_html(self): request = DummyRequest('/index.html', [], '', '', {'PATH_INFO':'/index.html'}) handler = self._makeOne() handler.handle_request(request) from docker_supervisor.web import StatusView view = request.channel.producer.callback self.assertEqual(view.__class__, StatusView) self.assertEqual(view.context.template, 'ui/status.html')
def test_render_noaction(self): context = DummyContext() context.supervisord = DummySupervisor() context.template = 'ui/status.html' context.request = DummyRequest('/foo', [], '', '') context.form = {} context.response = {} view = self._makeOne(context) data = view.render() self.assertTrue(data.startswith('<!DOCTYPE html PUBLIC'), data)
def test_sendresponse_redirect(self): request = DummyRequest('/index.html', [], '', '') callback = lambda *arg: None callback.delay = 1 producer = self._makeOne(request, callback) response = {'headers': {'Location':'abc'}} result = producer.sendresponse(response) self.assertEqual(result, None) self.assertEqual(request._error, 301) self.assertEqual(request.headers['Content-Type'], 'text/plain') self.assertEqual(request.headers['Content-Length'], 0)
def test_sendresponse_withbody_and_content_type(self): request = DummyRequest('/index.html', [], '', '') callback = lambda *arg: None callback.delay = 1 producer = self._makeOne(request, callback) response = {'body': 'abc', 'headers':{'Content-Type':'text/html'}} result = producer.sendresponse(response) self.assertEqual(result, None) self.assertEqual(request.headers['Content-Type'], 'text/html') self.assertEqual(request.headers['Content-Length'], 3) self.assertEqual(request.producers[0], 'abc')
def test_handle_request_stdout_logfile_none(self): options = DummyOptions() pconfig = DummyPConfig(options, 'process1', '/bin/process1', priority=1, stdout_logfile='/tmp/process1.log') supervisord = PopulatedDummySupervisor(options, 'process1', pconfig) handler = self._makeOne(supervisord) request = DummyRequest('/logtail/process1', None, None, None) handler.handle_request(request) self.assertEqual(request._error, 410)
def test_more_exception_caught(self): request = DummyRequest('/index.html', [], '', '') def callback(*arg): raise ValueError('foo') callback.delay = 1 producer = self._makeOne(request, callback) self.assertEqual(producer.more(), None) logdata = request.channel.server.logger.logged self.assertEqual(len(logdata), 1) logged = logdata[0] self.assertEqual(logged[0], 'Web interface error') self.assertTrue(logged[1].startswith('Traceback'), logged[1]) self.assertEqual(producer.finished, True) self.assertEqual(request._error, 500)
def test_handle_request(self): supervisor = DummySupervisor() f = tempfile.NamedTemporaryFile() t = f.name supervisor.options.logfile = t handler = self._makeOne(supervisor) request = DummyRequest('/mainlogtail', None, None, None) handler.handle_request(request) self.assertEqual(request._error, None) from docker_supervisor.medusa import http_date self.assertEqual(request.headers['Last-Modified'], http_date.build_http_date(os.stat(t)[stat.ST_MTIME])) self.assertEqual(request.headers['Content-Type'], 'text/plain') self.assertEqual(len(request.producers), 1) self.assertEqual(request._done, True)
def test_handle_request(self): f = tempfile.NamedTemporaryFile() t = f.name options = DummyOptions() pconfig = DummyPConfig(options, 'foo', 'foo', stdout_logfile=t) supervisord = PopulatedDummySupervisor(options, 'foo', pconfig) handler = self._makeOne(supervisord) request = DummyRequest('/logtail/foo', None, None, None) handler.handle_request(request) self.assertEqual(request._error, None) from docker_supervisor.medusa import http_date self.assertEqual(request.headers['Last-Modified'], http_date.build_http_date(os.stat(t)[stat.ST_MTIME])) self.assertEqual(request.headers['Content-Type'], 'text/plain') self.assertEqual(len(request.producers), 1) self.assertEqual(request._done, True)
def test_handle_more_follow_file_recreated(self): request = DummyRequest('/logtail/foo', None, None, None) f = tempfile.NamedTemporaryFile() f.write('a' * 80) f.flush() producer = self._makeOne(request, f.name, 80) result = producer.more() self.assertEqual(result, 'a' * 80) f.close() f2 = open(f.name, 'w') try: f2.write('b' * 80) f2.close() result = producer.more() finally: os.unlink(f2.name) self.assertEqual(result, 'b' * 80)
def test_continue_request_400_if_method_name_is_empty(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) data = '<?xml version="1.0" encoding="UTF-8"?>' \ '<methodCall><methodName></methodName></methodCall>' request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data from docker_supervisor.xmlrpc import loads if loads: expected = 1 else: expected = 2 self.assertEqual(len(logdata), expected) self.assertEqual(logdata[-1], u'XML-RPC request received with no method name') self.assertEqual(len(request.producers), 0) self.assertEqual(request._error, 400)
def test_handle_more(self): request = DummyRequest('/logtail/foo', None, None, None) from docker_supervisor import http f = tempfile.NamedTemporaryFile() f.write('a' * 80) f.flush() producer = self._makeOne(request, f.name, 80) result = producer.more() self.assertEqual(result, 'a' * 80) f.write('w' * 100) f.flush() result = producer.more() self.assertEqual(result, 'w' * 100) result = producer.more() self.assertEqual(result, http.NOT_DONE_YET) f.truncate(0) f.flush() result = producer.more() self.assertEqual(result, '==> File truncated <==\n')
def test_handle_more_follow_file_gone(self): request = DummyRequest('/logtail/foo', None, None, None) filename = tempfile.mktemp() f = open(filename, 'wb') f.write('a' * 80) f.close() try: producer = self._makeOne(request, f.name, 80) finally: os.unlink(f.name) result = producer.more() self.assertEqual(result, 'a' * 80) f = open(filename, 'wb') f.write('b' * 80) f.close() try: result = producer.more() # should open in new file self.assertEqual(result, 'b' * 80) finally: os.unlink(f.name)
def test_continue_request_500(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) import xmlrpclib data = xmlrpclib.dumps((), 'supervisor.raiseError') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data from docker_supervisor.xmlrpc import loads if loads: expected = 2 else: expected = 3 self.assertEqual(len(logdata), expected) self.assertEqual(logdata[-2], u'XML-RPC method called: supervisor.raiseError()') self.assertTrue(logdata[-1].startswith('Traceback')) self.assertTrue(logdata[-1].endswith('ValueError: error\n')) self.assertEqual(len(request.producers), 0) self.assertEqual(request._error, 500)
def test_continue_request_nosuchmethod(self): supervisor = DummySupervisor() subinterfaces = [('supervisor', DummySupervisorRPCNamespace())] handler = self._makeOne(supervisor, subinterfaces) import xmlrpclib data = xmlrpclib.dumps(('a', 'b'), 'supervisor.noSuchMethod') request = DummyRequest('/what/ever', None, None, None) handler.continue_request(data, request) logdata = supervisor.options.logger.data from docker_supervisor.xmlrpc import loads if loads: expected = 2 else: expected = 3 self.assertEqual(len(logdata), expected) self.assertEqual(logdata[-2], u'XML-RPC method called: supervisor.noSuchMethod()') self.assertEqual( logdata[-1], (u'XML-RPC method supervisor.noSuchMethod() returned fault: ' '[1] UNKNOWN_METHOD')) self.assertEqual(len(request.producers), 1) xml_response = request.producers[0] self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, xml_response)
def test_handle_request_no_view_method(self): request = DummyRequest('/foo.css', [], '', '', {'PATH_INFO':'/foo.css'}) handler = self._makeOne() data = handler.handle_request(request) self.assertEqual(data, None)
def test_handle_request_stdout_logfile_none(self): supervisor = DummySupervisor() handler = self._makeOne(supervisor) request = DummyRequest('/mainlogtail', None, None, None) handler.handle_request(request) self.assertEqual(request._error, 410)