← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~allenap/launchpad/librarian-log-bug-828151 into lp:launchpad

 

Gavin Panella has proposed merging lp:~allenap/launchpad/librarian-log-bug-828151 into lp:launchpad with lp:~allenap/launchpad/native-sync-real-names-in-emails-bug-827526 as a prerequisite.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #828151 in Launchpad itself: "The librarian-log test detail contains the whole librarian log, not just the log from the currently running test"
  https://bugs.launchpad.net/launchpad/+bug/828151

For more details, see:
https://code.launchpad.net/~allenap/launchpad/librarian-log-bug-828151/+merge/72097

This adds a truncateLog() method to TacTestSetup - upon with
LibrarianServerFixture is based - and gets _check_and_reset() in
LibrarianLayer to call it.
-- 
https://code.launchpad.net/~allenap/launchpad/librarian-log-bug-828151/+merge/72097
Your team Launchpad code reviewers is requested to review the proposed merge of lp:~allenap/launchpad/librarian-log-bug-828151 into lp:launchpad.
=== modified file 'lib/canonical/launchpad/daemons/tachandler.py'
--- lib/canonical/launchpad/daemons/tachandler.py	2011-06-21 17:13:47 +0000
+++ lib/canonical/launchpad/daemons/tachandler.py	2011-08-18 20:01:26 +0000
@@ -12,6 +12,7 @@
 
 
 import os
+import os.path
 import subprocess
 import sys
 import time
@@ -144,6 +145,24 @@
             return
         os.kill(pid, sig)
 
+    def truncateLog(self):
+        """Truncate the log file.
+
+        Leaves everything up to and including the `LOG_MAGIC` marker in
+        place. If the `LOG_MAGIC` marker is not found the log is truncated to
+        0 bytes.
+        """
+        if os.path.exists(self.logfile):
+            with open(self.logfile, "r+b") as logfile:
+                position = 0
+                for line in logfile:
+                    position += len(line)
+                    if readyservice.LOG_MAGIC in line:
+                        logfile.truncate(position)
+                        break
+                else:
+                    logfile.truncate(0)
+
     def setUpRoot(self):
         """Override this.
 

=== modified file 'lib/canonical/launchpad/daemons/tests/test_tachandler.py'
--- lib/canonical/launchpad/daemons/tests/test_tachandler.py	2011-08-12 11:19:40 +0000
+++ lib/canonical/launchpad/daemons/tests/test_tachandler.py	2011-08-18 20:01:26 +0000
@@ -7,15 +7,18 @@
 
 import os.path
 import tempfile
-import unittest
-
+
+from fixtures import TempDir
+import testtools
+
+from canonical.launchpad.daemons.readyservice import LOG_MAGIC
 from canonical.launchpad.daemons.tachandler import (
     TacException,
     TacTestSetup,
     )
 
 
-class TacTestSetupTestCase(unittest.TestCase):
+class TacTestSetupTestCase(testtools.TestCase):
     """Some tests for the error handling of TacTestSetup."""
 
     def test_missingTac(self):
@@ -43,3 +46,36 @@
                 pass
 
         self.assertRaises(TacException, CouldNotListenTac().setUp)
+
+    def test_truncateLog(self):
+        """truncateLog truncates the log. What did you expect?"""
+        tempdir = self.useFixture(TempDir()).path
+
+        class DoNothingTac(TacTestSetup):
+            logfile = os.path.join(tempdir, 'nothing.log')
+
+        # Put something in the log file.
+        fixture = DoNothingTac()
+        with open(fixture.logfile, "wb") as logfile:
+            logfile.write("Hello\n")
+
+        # Truncating the log does not remove the log file.
+        fixture.truncateLog()
+        self.assertTrue(os.path.exists(fixture.logfile))
+        with open(fixture.logfile, "rb") as logfile:
+            self.assertEqual("", logfile.read())
+
+        # Put something in the log again, along with LOG_MAGIC.
+        with open(fixture.logfile, "wb") as logfile:
+            logfile.write("One\n")
+            logfile.write("Two\n")
+            logfile.write("Three, %s\n" % LOG_MAGIC)
+            logfile.write("Four\n")
+
+        # Truncating the log leaves everything up to and including the line
+        # containing LOG_MAGIC.
+        fixture.truncateLog()
+        with open(fixture.logfile, "rb") as logfile:
+            self.assertEqual(
+                "One\nTwo\nThree, %s\n" % LOG_MAGIC,
+                logfile.read())

=== modified file 'lib/canonical/testing/layers.py'
--- lib/canonical/testing/layers.py	2011-08-12 14:39:51 +0000
+++ lib/canonical/testing/layers.py	2011-08-18 20:01:26 +0000
@@ -927,6 +927,7 @@
                     "the Librarian is restarted if it absolutely must be "
                     "shutdown: " + str(e))
         cls.librarian_fixture.clear()
+        cls.librarian_fixture.truncateLog()
 
     @classmethod
     @profiled


Follow ups