Changeset 451:53c0da7549b7

Show
Ignore:
Timestamp:
08/01/08 15:41:41 (5 months ago)
Author:
"Brian Warner <warner@lothar.com>"
branch:
default
Message:

test_logging.py: add incident-gathering/classification test

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • ChangeLog

    r450 r451  
    112008-08-01  Brian Warner  <warner@lothar.com> 
     2 
     3        * foolscap/test/test_logging.py (IncidentGatherer.test_emit): add 
     4        test of incident generation, publish, recording, and default 
     5        classification 
    26 
    37        * foolscap/logging/gatherer.py (IncidentGathererService): get 
  • foolscap/test/test_logging.py

    r450 r451  
    55from twisted.trial import unittest 
    66from twisted.application import service 
    7 from twisted.internet import defer, reactor 
     7from twisted.internet import defer 
    88from twisted.python import log as twisted_log 
    99from twisted.python import failure, runtime, usage 
     
    191191    TRAILING_DELAY = 1.0 
    192192    TRAILING_EVENT_LIMIT = 3 
     193 
     194class NoFollowUpReporter(incident.IncidentReporter): 
     195    TRAILING_DELAY = None 
    193196 
    194197class LogfileReaderMixin: 
     
    868871class MyIncidentGathererService(gatherer.IncidentGathererService): 
    869872    verbose = False 
     873    cb_new_incident = None 
    870874 
    871875    def remote_logport(self, nodeid, publisher): 
     
    874878        d.addCallback(lambda res: self.d.callback(publisher)) 
    875879        return d 
     880 
     881    def new_incident(self, abs_fn, rel_fn, nodeid_s, incident): 
     882        gatherer.IncidentGathererService.new_incident(self, abs_fn, rel_fn, 
     883                                                      nodeid_s, incident) 
     884        if self.cb_new_incident: 
     885            self.cb_new_incident((abs_fn, rel_fn)) 
    876886 
    877887class IncidentGatherer(unittest.TestCase, 
     
    881891        self.parent.startService() 
    882892        self.logger = log.FoolscapLogger() 
     893        self.logger.setIncidentReporterFactory(NoFollowUpReporter) 
    883894 
    884895    def tearDown(self): 
     
    888899        return d 
    889900 
    890     def test_incident_gatherer(self): 
    891         basedir = "logging/IncidentGatherer/incident_gatherer" 
    892         os.makedirs(basedir) 
    893         self.logger.setLogDir(basedir) 
    894  
     901    def create_incident_gatherer(self, basedir): 
    895902        # create an incident gatherer, which will make its own Tub 
    896903        ig_basedir = os.path.join(basedir, "ig") 
     
    901908        ig.d = defer.Deferred() 
    902909        ig.setServiceParent(self.parent) 
    903  
     910        return ig 
     911 
     912    def create_connected_tub(self, ig): 
    904913        t = GoodEnoughTub() 
    905914        t.logger = self.logger 
     
    909918        t.setOption("log-gatherer-furl", ig.my_furl) 
    910919 
     920    def test_connect(self): 
     921        basedir = "logging/IncidentGatherer/connect" 
     922        os.makedirs(basedir) 
     923        self.logger.setLogDir(basedir) 
     924 
     925        ig = self.create_incident_gatherer(basedir) 
     926        self.create_connected_tub(ig) 
     927 
    911928        d = ig.d 
    912  
    913929        # give the call to remote_logport a chance to retire 
    914930        d.addCallback(self.stall, 0.5) 
    915  
     931        return d 
     932 
     933    def test_emit(self): 
     934        basedir = "logging/IncidentGatherer/emit" 
     935        os.makedirs(basedir) 
     936        self.logger.setLogDir(basedir) 
     937 
     938        ig = self.create_incident_gatherer(basedir) 
     939        incident_d = defer.Deferred() 
     940        ig.cb_new_incident = incident_d.callback 
     941        self.create_connected_tub(ig) 
     942 
     943        d = ig.d 
     944        d.addCallback(lambda res: self.logger.msg("boom", level=log.WEIRD)) 
     945        d.addCallback(lambda res: incident_d) 
     946        def _new_incident((abs_fn, rel_fn)): 
     947            events = self._read_logfile(abs_fn) 
     948            header = events[0]["header"] 
     949            self.failUnless("trigger" in header) 
     950            self.failUnlessEqual(header["trigger"]["message"], "boom") 
     951            e = events[1]["d"] 
     952            self.failUnlessEqual(e["message"], "boom") 
     953 
     954            # it should have been classified as "unknown" 
     955            unknowns_fn = os.path.join(ig.basedir, "classified", "unknown") 
     956            unknowns = [fn.strip() for fn in open(unknowns_fn,"r").readlines()] 
     957            self.failUnlessEqual(len(unknowns), 1) 
     958            self.failUnlessEqual(unknowns[0], rel_fn) 
     959        d.addCallback(_new_incident) 
     960 
     961        # give the call to remote_logport a chance to retire 
     962        d.addCallback(self.stall, 0.5) 
    916963        return d 
    917964