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_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_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_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)