def _ebConnect(self, f): # probably because the server wasn't listening in time # but who knows, just try again log.msg('ERROR CONNECTING TO %s' % self.port) log.err(f) log.flushErrors() reactor.callLater(1, self._connect)
def openFailed(self, reason): """ good .... good """ log.msg('unknown open failed') log.flushErrors() self.conn.addResult()
def _flushErrors(self, *types): # This bit about log flushing seems to be necessary with twisted < 2.5. try: self.flushLoggedErrors(*types) except AttributeError: from twisted.python import log as tlog tlog.flushErrors(*types)
def testRefusedAnonymousClientConnection(self): onServerLost = defer.Deferred() onClientLost = defer.Deferred() self.loopback(sslverify.OpenSSLCertificateOptions(privateKey=self.sKey, certificate=self.sCert, verify=True, caCerts=[self.sCert], requireCertificate=True), sslverify.OpenSSLCertificateOptions(requireCertificate=False), onServerLost=onServerLost, onClientLost=onClientLost) d = defer.DeferredList([onClientLost, onServerLost], consumeErrors=True) def afterLost(((cSuccess, cResult), (sSuccess, sResult))): self.failIf(cSuccess) self.failIf(sSuccess) # XXX Twisted doesn't report SSL errors as SSL errors, but in the # future it will. # cResult.trap(SSL.Error) # sResult.trap(SSL.Error) # Twisted trunk will do the correct thing here, and not log any # errors. Twisted 2.1 will do the wrong thing. We're flushing # errors until the buildbot is updated to a reasonable facsimilie # of 2.2. log.flushErrors(SSL.Error)
def testLink(self): linkRes = self._getCmdResult('ln testLink testfile1') self.failIf(linkRes) lslRes = self._getCmdResult('ls -l testLink') log.flushErrors() self.failUnless(lslRes.startswith('l'), 'link failed') self.failIf(self._getCmdResult('rm testLink'))
def testFailedRETR(self): try: f = protocol.Factory() f.noisy = 0 port = reactor.listenTCP(0, f, interface="127.0.0.1") portNum = port.getHost().port responses = ['220 ready, dude (vsFTPd 1.0.0: beat me, break me)', '331 Please specify the password.', '230 Login successful. Have fun.', '200 Binary it is, then.', '227 Entering Passive Mode (127,0,0,1,%d,%d)' % (portNum >> 8, portNum & 0xff), '550 Failed to open file.'] f.buildProtocol = lambda addr: PrintLines(responses) client = ftp.FTPClient(passive=1) cc = protocol.ClientCreator(reactor, ftp.FTPClient, passive=1) client = wait(cc.connectTCP('127.0.0.1', portNum)) p = protocol.Protocol() d = client.retrieveFile('/file/that/doesnt/exist', p) d.addCallback(lambda r, self=self: self.fail('Callback incorrectly called: %r' % r)) def p(failure): failure.trap(ftp.CommandFailed) d.addErrback(p) wait(d) log.flushErrors(ftp.FTPError) finally: d = port.stopListening() if d is not None: wait(d)
def testErrorLog(self): c = self.c defer.Deferred().addCallback(lambda x: 1/0).callback(1) c2 = [e for e in c if e["isError"]] self.assertEquals(len(c2), 2) c2[1]["failure"].trap(ZeroDivisionError) log.flushErrors(ZeroDivisionError)
def testErrors(self): for e, ig in [("hello world", "hello world"), (KeyError(), KeyError), (failure.Failure(RuntimeError()), RuntimeError)]: log.err(e) i = self.catcher.pop() self.assertEquals(i['isError'], 1) log.flushErrors(ig)
def testReadNotificationRaises(self): self.f.protocol.readConnectionLost = self.aBug self.client.transport.loseWriteConnection() spinUntil(lambda :self.f.protocol.closed) # XXX client won't be closed?! why isn't server sending RST? # or maybe it is and we have a bug here. self.client.transport.loseConnection() log.flushErrors(RuntimeError)
def testDeferredStopService(self): ms = app.MultiService("MultiService") self.s1 = StoppingService("testService", 0) self.s2 = StoppingService("testService2", 1) ms.addService(self.s1) ms.addService(self.s2) ms.stopService().addCallback(self.woohoo) log.flushErrors (StopError)
def testErrors(self): for e, ig in [("hello world","hello world"), (KeyError(), KeyError), (failure.Failure(RuntimeError()), RuntimeError)]: log.err(e) i = self.catcher.pop() self.assertEquals(i['isError'], 1) log.flushErrors(ig)
def testPBFailures(self): auth = authorizer.DefaultAuthorizer() appl = app.Application("pbfailure", authorizer=auth) SimpleService("pbfailure",auth,appl,self).getPerspectiveNamed("guest").makeIdentity("guest") p = reactor.listenTCP(0, pb.BrokerFactory(pb.AuthRoot(auth)), interface="127.0.0.1") self.n = p.getHost()[2] appl.run(save=0) log.flushErrors(PoopError, FailError, DieError)
def testChAttrs(self): lsRes = self._getCmdResult('ls -l testfile1') self.failUnless(lsRes.startswith('-rw-r--r--'), lsRes) self.failIf(self._getCmdResult('chmod 0 testfile1')) lsRes = self._getCmdResult('ls -l testfile1') self.failUnless(lsRes.startswith('----------'), lsRes) self.failIf(self._getCmdResult('chmod 644 testfile1')) log.flushErrors()
def testStopServiceNone(self): """MultiService.stopService returns Deferred when service returns None. """ ms = app.MultiService("MultiService") self.s1 = StoppingServiceII("testService") ms.addService(self.s1) d = ms.stopService() d.addCallback(self.cb_nonetest) log.flushErrors (StopError)
def do_logErrCheck(cls): if log._keptErrors: L = [] for err in log._keptErrors: if isinstance(err, failure.Failure): L.append(err) else: L.append(repr(err)) log.flushErrors() raise LoggedErrors(L)
def tearDown(self): try: self.flushLoggedErrors(errors.NotAuthenticatedError) except AttributeError: tlog.flushErrors(errors.NotAuthenticatedError) log._getTheFluLogObserver().clearIgnores() d = self.feedServer.shutdown() d.addCallback(lambda _: self._bouncer.stop()) d.addCallback(lambda _: self.assertAdditionalFDsOpen(0, 'tearDown')) return d
def testErrors(self): for code, methodName in [(666, "fail"), (666, "deferFail"), (12, "fault"), (23, "noSuchMethod"), (17, "deferFault")]: l = [] d = self.proxy().callRemote(methodName).addErrback(l.append) while not l: reactor.iterate() l[0].trap(xmlrpc.Fault) self.assertEquals(l[0].value.faultCode, code) log.flushErrors(RuntimeError, ValueError)
def testRegeneratingIt(self): for mod in self.removedModules: self.failIf(mod.__name__ in sys.modules, 'Started with %r loaded: %r' % (mod.__name__, sys.path)) _regeneratePluginCache(['axiom', 'xmantissa']) log.flushErrors(ImportError) # This is necessary since there are Axiom # plugins that depend on Mantissa, so when # Axiom is installed, Mantissa-dependent # powerups are, but Mantissa isn't some # harmless tracebacks are printed. for mod in self.removedModules: self.failIf(mod.__name__ in sys.modules, 'Loaded %r: %r' % (mod.__name__, sys.path))
def testErrorLog(self): c = self.c defer.Deferred().addCallback(lambda x: 1/0).callback(1) # do you think it is rad to have memory leaks glyph ## d = defer.Deferred() ## d.addCallback(lambda x: 1/0) ## d.callback(1) ## del d c2 = [e for e in c if e["isError"]] self.assertEquals(len(c2), 2) c2[1]["failure"].trap(ZeroDivisionError) log.flushErrors(ZeroDivisionError)
def testBadLogin(self): factory = pb.PBClientFactory() for username, password in [("nosuchuser", "pass"), ("user", "wrongpass")]: d = factory.login(credentials.UsernamePassword(username, password), "BRAINS!") c = reactor.connectTCP("127.0.0.1", self.portno, factory) p = unittest.deferredError(d) self.failUnless(p.check("twisted.cred.error.UnauthorizedLogin")) c.disconnect() reactor.iterate() reactor.iterate() reactor.iterate() from twisted.cred.error import UnauthorizedLogin log.flushErrors(UnauthorizedLogin)
def testRegeneratingIt(self): for mod in self.removedModules: self.failIf( mod.__name__ in sys.modules, 'Started with %r loaded: %r' % (mod.__name__, sys.path)) _regeneratePluginCache(['axiom', 'xmantissa']) log.flushErrors(ImportError) # This is necessary since there are Axiom # plugins that depend on Mantissa, so when # Axiom is installed, Mantissa-dependent # powerups are, but Mantissa isn't some # harmless tracebacks are printed. for mod in self.removedModules: self.failIf(mod.__name__ in sys.modules, 'Loaded %r: %r' % (mod.__name__, sys.path))
def testBrokenTimer(self): t = internet.TimerService(1, lambda: 1 / 0) t.startService() util.spinWhile(lambda :t._loop.running, timeout=30) t.stopService() self.assertEquals([ZeroDivisionError], [o.value.__class__ for o in log.flushErrors(ZeroDivisionError)])
def later(result): errs = log.flushErrors(SpecialError) self.assertEquals(len(errs), 1) self.assertEquals( self.store.query(TimedEventFailureLog).count(), 0) self.failUnless(spec.procd) self.failIf(spec.broken)
def testErrors(self): p = TestErrorIdentServer() p.makeConnection(StringTransport()) L = [] p.sendLine = L.append p.exceptionType = ident.IdentError p.lineReceived('123, 345') self.assertEquals(L[0], '123, 345 : ERROR : UNKNOWN-ERROR') p.exceptionType = ident.NoUser p.lineReceived('432, 210') self.assertEquals(L[1], '432, 210 : ERROR : NO-USER') p.exceptionType = ident.InvalidPort p.lineReceived('987, 654') self.assertEquals(L[2], '987, 654 : ERROR : INVALID-PORT') p.exceptionType = ident.HiddenUser p.lineReceived('756, 827') self.assertEquals(L[3], '756, 827 : ERROR : HIDDEN-USER') p.exceptionType = NewException p.lineReceived('987, 789') self.assertEquals(L[4], '987, 789 : ERROR : UNKNOWN-ERROR') errs = log.flushErrors(NewException) self.assertEquals(len(errs), 1)
def testBadLogin(self): d = defer.succeed(None) for username, password in [("nosuchuser", "pass"), ("user", "wrongpass")]: d.addCallback(self._testBadLogin_once, username, password) d.addBoth(lambda res: log.flushErrors(UnauthorizedLogin)) return d
def cbCompleted(ign): """ Flush the exceptions which the reactor should have logged and make sure they're actually there. """ errs = log.flushErrors(BadClientError) self.assertEquals(len(errs), 2, "Incorrectly found %d errors, expected 2" % (len(errs),))
def testFailedRETR(self): try: f = protocol.Factory() f.noisy = 0 port = reactor.listenTCP(0, f, interface="127.0.0.1") portNum = port.getHost().port # This test data derived from a bug report by ranty on #twisted responses = ['220 ready, dude (vsFTPd 1.0.0: beat me, break me)', # USER anonymous '331 Please specify the password.', # PASS [email protected] '230 Login successful. Have fun.', # TYPE I '200 Binary it is, then.', # PASV '227 Entering Passive Mode (127,0,0,1,%d,%d)' % (portNum >> 8, portNum & 0xff), # RETR /file/that/doesnt/exist '550 Failed to open file.'] f.buildProtocol = lambda addr: PrintLines(responses) client = ftp.FTPClient(passive=1) cc = protocol.ClientCreator(reactor, ftp.FTPClient, passive=1) client = wait(cc.connectTCP('127.0.0.1', portNum)) p = protocol.Protocol() d = client.retrieveFile('/file/that/doesnt/exist', p) # This callback should not be called d.addCallback(lambda r, self=self: self.fail('Callback incorrectly called: %r' % r)) def p(failure): # Yow! Nested DeferredLists! failure.trap(defer.FirstError) failure.value.subFailure.trap(defer.FirstError) subsubFailure = failure.value.subFailure.value.subFailure subsubFailure.trap(ftp.CommandFailed) # If we got through all that, we got the right failure. d.addErrback(p) wait(d) log.flushErrors(ftp.FTPError) finally: d = port.stopListening() if d is not None: wait(d)
def testFailedRETR(self): try: f = protocol.Factory() f.noisy = 0 port = reactor.listenTCP(0, f, interface="127.0.0.1") portNum = port.getHost().port # This test data derived from a bug report by ranty on #twisted responses = [ '220 ready, dude (vsFTPd 1.0.0: beat me, break me)', # USER anonymous '331 Please specify the password.', # PASS [email protected] '230 Login successful. Have fun.', # TYPE I '200 Binary it is, then.', # PASV '227 Entering Passive Mode (127,0,0,1,%d,%d)' % (portNum >> 8, portNum & 0xff), # RETR /file/that/doesnt/exist '550 Failed to open file.' ] f.buildProtocol = lambda addr: PrintLines(responses) client = ftp.FTPClient(passive=1) cc = protocol.ClientCreator(reactor, ftp.FTPClient, passive=1) client = wait(cc.connectTCP('127.0.0.1', portNum)) p = protocol.Protocol() d = client.retrieveFile('/file/that/doesnt/exist', p) # This callback should not be called, because we're expecting a # failure. d.addCallback(lambda r, self=self: self.fail( 'Callback incorrectly called: %r' % r)) def p(failure): # Make sure we got the failure we were expecting. failure.trap(ftp.CommandFailed) d.addErrback(p) wait(d) log.flushErrors(ftp.FTPError) finally: d = port.stopListening() if d is not None: wait(d)
def testBrokenTimer(self): t = internet.TimerService(1, lambda: 1 / 0) t.startService() util.spinWhile(lambda: t._loop.running, timeout=30) t.stopService() self.assertEquals( [ZeroDivisionError], [o.value.__class__ for o in log.flushErrors(ZeroDivisionError)])
def cbCompleted(ign): """ Flush the exceptions which the reactor should have logged and make sure they're actually there. """ errs = log.flushErrors(BadClientError) self.assertEquals( len(errs), 2, "Incorrectly found %d errors, expected 2" % (len(errs), ))
def testFailedRETR(self): try: f = Factory() f.noisy = 0 f.protocol = Protocol port = reactor.listenTCP(0, f, interface="127.0.0.1") n = port.getHost()[2] # This test data derived from a bug report by ranty on #twisted responses = ['220 ready, dude (vsFTPd 1.0.0: beat me, break me)', # USER anonymous '331 Please specify the password.', # PASS [email protected] '230 Login successful. Have fun.', # TYPE I '200 Binary it is, then.', # PASV '227 Entering Passive Mode (127,0,0,1,%d,%d)' % (n>>8, n&0xff), # RETR /file/that/doesnt/exist '550 Failed to open file.'] b = StringIOWithoutClosing() client = ftp.FTPClient(passive=1) client.makeConnection(FileWrapper(b)) self.writeResponses(client, responses) p = Protocol() d = client.retrieveFile('/file/that/doesnt/exist', p) d.addCallback(lambda r, self=self: self.fail('Callback incorrectly called: %r' % r)) d.addBoth(lambda ignored,r=reactor: r.crash()) id = reactor.callLater(2, self.timeout) reactor.run() log.flushErrors(ftp.FTPError) try: id.cancel() except: pass finally: try: port.stopListening() reactor.iterate() except: pass
def testFailedRETR(self): try: f = protocol.Factory() f.noisy = 0 port = reactor.listenTCP(0, f, interface="127.0.0.1") portNum = port.getHost().port # This test data derived from a bug report by ranty on #twisted responses = [ "220 ready, dude (vsFTPd 1.0.0: beat me, break me)", # USER anonymous "331 Please specify the password.", # PASS [email protected] "230 Login successful. Have fun.", # TYPE I "200 Binary it is, then.", # PASV "227 Entering Passive Mode (127,0,0,1,%d,%d)" % (portNum >> 8, portNum & 0xFF), # RETR /file/that/doesnt/exist "550 Failed to open file.", ] f.buildProtocol = lambda addr: PrintLines(responses) client = ftp.FTPClient(passive=1) cc = protocol.ClientCreator(reactor, ftp.FTPClient, passive=1) client = wait(cc.connectTCP("127.0.0.1", portNum)) p = protocol.Protocol() d = client.retrieveFile("/file/that/doesnt/exist", p) # This callback should not be called, because we're expecting a # failure. d.addCallback(lambda r, self=self: self.fail("Callback incorrectly called: %r" % r)) def p(failure): # Make sure we got the failure we were expecting. failure.trap(ftp.CommandFailed) d.addErrback(p) wait(d) log.flushErrors(ftp.FTPError) finally: d = port.stopListening() if d is not None: wait(d)
def test_inputerror(self): p = self.runCode("import sys\nsys.stdout.write(sys.stdin.read())", TestStreamer(["hello", defer.fail(ZeroDivisionError())])) l = [] d = stream.readStream(p.outStream, l.append) d2 = p.run() def verify(_): self.assertEquals("".join(l), "hello") return d2 return d.addCallback(verify).addCallback(lambda _: log.flushErrors(ZeroDivisionError))
def testFailedCertificateVerification(self): onServerLost = defer.Deferred() onClientLost = defer.Deferred() self.loopback(sslverify.OpenSSLCertificateOptions(privateKey=self.sKey, certificate=self.sCert, verify=False, requireCertificate=False), sslverify.OpenSSLCertificateOptions(verify=True, requireCertificate=False, caCerts=[self.cCert]), onServerLost=onServerLost, onClientLost=onClientLost) d = defer.DeferredList([onClientLost, onServerLost], consumeErrors=True) def afterLost(((cSuccess, cResult), (sSuccess, sResult))): self.failIf(cSuccess) self.failIf(sSuccess) # Twisted trunk will do the correct thing here, and not log any # errors. Twisted 2.1 will do the wrong thing. We're flushing # errors until the buildbot is updated to a reasonable facsimilie # of 2.2. log.flushErrors(SSL.Error)
def testErrors(self): dl = [] for code, methodName in [(666, "fail"), (666, "deferFail"), (12, "fault"), (23, "noSuchMethod"), (17, "deferFault"), (42, "SESSION_TEST")]: d = self.proxy().callRemote(methodName) d = self.assertFailure(d, xmlrpc.Fault) d.addCallback(lambda exc, code=code: self.assertEquals(exc.faultCode, code)) dl.append(d) d = defer.DeferredList(dl, fireOnOneErrback=True) d.addCallback(lambda ign: log.flushErrors(TestRuntimeError, TestValueError)) return d
def cleanUp(self): """I clean up after the test is run. This includes making sure there are no pending calls lying around, garbage collecting and flushing errors. This is all to ensure that any errors caused by this tests are caught by this test. """ self._runPhase(util.reactorCleanUp) if gc: gc.collect() for e in log.flushErrors(): self.failures.append((self.error, e))
def testErrors(self): dl = [] for code, methodName in [(666, "fail"), (666, "deferFail"), (12, "fault"), (23, "noSuchMethod"), (17, "deferFault"), (42, "SESSION_TEST")]: d = self.proxy().callRemote(methodName) d = self.assertFailure(d, xmlrpc.Fault) d.addCallback( lambda exc, code=code: self.assertEquals(exc.faultCode, code)) dl.append(d) d = defer.DeferredList(dl, fireOnOneErrback=True) d.addCallback( lambda ign: log.flushErrors(TestRuntimeError, TestValueError)) return d
def testBrokenListener(self): """ Test that if a listener's processItem method raises an exception, processing continues beyond that item and that an error marker is created for that item. """ errmsg = "This reliable listener is not very reliable!" processedItems = [] def listener(item): if item.information == 1: raise RuntimeError(errmsg) processedItems.append(item.information) proc = self.procType(store=self.store) listener = WorkListener(store=self.store, listener=listener) proc.addReliableListener(listener) # Make some work, step the processor, and fake the error handling # behavior the Scheduler actually provides. for i in range(3): TestWorkUnit(store=self.store, information=i) try: proc.step() except batch._ProcessingFailure: proc.timedEventErrorHandler( (u"Oh crap, I do not have a TimedEvent, " "I sure hope that never becomes a problem."), failure.Failure()) self.assertEquals(processedItems, [0, 2]) errors = list(proc.getFailedItems()) self.assertEquals(len(errors), 1) self.assertEquals(errors[0][0], listener) self.assertEquals(errors[0][1].information, 1) loggedErrors = log.flushErrors(RuntimeError) self.assertEquals(len(loggedErrors), 1) self.assertEquals(loggedErrors[0].getErrorMessage(), errmsg)
def testProcessingServiceStepsOverErrors(self): """ If any processor raises an unexpected exception, the work unit which was being processed should be marked as having had an error and processing should move on to the next item. Make sure that this actually happens when L{BatchProcessingService} is handling those errors. """ BATCH_WORK_UNITS = 3 dbdir = self.mktemp() st = store.Store(dbdir) Scheduler(store=st).installOn(st) source = BatchWorkSource(store=st) for i in range(BATCH_WORK_UNITS): BatchWorkItem(store=st) source.addReliableListener(BrokenReliableListener(store=st), iaxiom.REMOTE) source.addReliableListener(WorkingReliableListener(store=st), iaxiom.REMOTE) svc = batch.BatchProcessingService(st, iaxiom.REMOTE) task = svc.step() # Loop 6 (BATCH_WORK_UNITS * 2) times - three items times two # listeners, it should not take any more than six iterations to # completely process all work. for i in xrange(BATCH_WORK_UNITS * 2): task.next() self.assertEquals( len(log.flushErrors(BrokenException)), BATCH_WORK_UNITS) self.assertEquals( st.query(BatchWorkItem, BatchWorkItem.value == u"processed").count(), BATCH_WORK_UNITS)
def testErroneousErrors(self): L1 = [] L2 = [] log.addObserver(lambda events: events['isError'] or L1.append(events)) log.addObserver(lambda events: 1 / 0) log.addObserver(lambda events: events['isError'] or L2.append(events)) log.msg("Howdy, y'all.") excs = [f.type for f in log.flushErrors(ZeroDivisionError)] self.assertEquals([ZeroDivisionError], excs) self.assertEquals(len(L1), 2) self.assertEquals(len(L2), 2) self.assertEquals(L1[1]['message'], ("Howdy, y'all.", )) self.assertEquals(L2[0]['message'], ("Howdy, y'all.", )) # The observer has been removed, there should be no exception log.msg("Howdy, y'all.") self.assertEquals(len(L1), 3) self.assertEquals(len(L2), 3) self.assertEquals(L1[2]['message'], ("Howdy, y'all.", )) self.assertEquals(L2[2]['message'], ("Howdy, y'all.", ))