summaryrefslogtreecommitdiff
path: root/Mailman/Queue/tests
diff options
context:
space:
mode:
authorbwarsaw2006-07-16 21:54:12 +0000
committerbwarsaw2006-07-16 21:54:12 +0000
commit2863de361a5a597006d7ce57dc58f9c538021855 (patch)
treeee552e311c055eb336c11e9dc782ec1112ff4220 /Mailman/Queue/tests
parent337beba04cf052acb9f33cc4f1862b2841924973 (diff)
downloadmailman-2863de361a5a597006d7ce57dc58f9c538021855.tar.gz
mailman-2863de361a5a597006d7ce57dc58f9c538021855.tar.zst
mailman-2863de361a5a597006d7ce57dc58f9c538021855.zip
Added robustness to Switchboards and Runners so that if a runner crashes
uncleanly (e.g. segfaults the Python interpreter), messages being processed will not be lost. The vulnerability, ideas, and patches are credited to Richard Barrett and Mark Sapiro. Their original work was modified by Barry for this commit and any bugs are his fault. The basic idea is that instead of unlinking a .pck file in dequeue(), the file is renamed to a .bak file. The Switchboard grows a finish() method which then unlinks the .bak file. That class's constructor also grows a 'restore' argument (defaulting to false), which when true moves all .bak files it finds in its hash space to .pck, thereby restoring a file lost while "in flight". This relies on the fact that even with multiple qrunners, exactly one process will be responsible for one hash space slice, so it's never possible (under normal operation) for a .bak file to be renamed to .pck by some other process. Test cases for both the new Switchboard behavior and the use of that by Runner subclasses has been added. There are two things to watch out for, either of which may require some additional changes. There is some small potential to duplicate messages in various queues, if say 'mailmanctl' were improperly started more than once by a site admin. This usually won't happen unless an admin is overly eager with the mailmanctl -s switch, so we can chalk this one up to operator error. I'm not sure what more we can do about that. There's also a possibility that if we're processing a message that continually causes the Python interpreter to crash, we could end up duplicating messages endlessly. This is especially troublesome for the Outgoing runner which could conceivably cause a mail flood. I consider this the more critical issue to defend against, probably by adding a numbering scheme to the .bak file names and refusing to restore a .bak file more than say 3 times without human intervention.
Diffstat (limited to 'Mailman/Queue/tests')
-rw-r--r--Mailman/Queue/tests/Makefile.in71
-rw-r--r--Mailman/Queue/tests/__init__.py0
-rw-r--r--Mailman/Queue/tests/test_runners.py236
3 files changed, 307 insertions, 0 deletions
diff --git a/Mailman/Queue/tests/Makefile.in b/Mailman/Queue/tests/Makefile.in
new file mode 100644
index 000000000..fdeae5d86
--- /dev/null
+++ b/Mailman/Queue/tests/Makefile.in
@@ -0,0 +1,71 @@
+# Copyright (C) 2006 by the Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+# NOTE: Makefile.in is converted into Makefile by the configure script
+# in the parent directory. Once configure has run, you can recreate
+# the Makefile by running just config.status.
+
+# Variables set by configure
+
+VPATH= @srcdir@
+srcdir= @srcdir@
+bindir= @bindir@
+prefix= @prefix@
+exec_prefix= @exec_prefix@
+DESTDIR=
+
+CC= @CC@
+CHMOD= @CHMOD@
+INSTALL= @INSTALL@
+
+DEFS= @DEFS@
+
+# Customizable but not set by configure
+
+OPT= @OPT@
+CFLAGS= $(OPT) $(DEFS)
+PACKAGEDIR= $(prefix)/Mailman/Queue/tests
+SHELL= /bin/sh
+
+MODULES= *.py
+
+# Modes for directories and executables created by the install
+# process. Default to group-writable directories but
+# user-only-writable for executables.
+DIRMODE= 775
+EXEMODE= 755
+FILEMODE= 644
+INSTALL_PROGRAM=$(INSTALL) -m $(EXEMODE)
+
+
+# Rules
+
+all:
+
+install:
+ for f in $(MODULES); \
+ do \
+ $(INSTALL) -m $(FILEMODE) $(srcdir)/$$f $(DESTDIR)$(PACKAGEDIR); \
+ done
+
+finish:
+
+clean:
+
+distclean:
+ -rm *.pyc
+ -rm Makefile
diff --git a/Mailman/Queue/tests/__init__.py b/Mailman/Queue/tests/__init__.py
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/Mailman/Queue/tests/__init__.py
diff --git a/Mailman/Queue/tests/test_runners.py b/Mailman/Queue/tests/test_runners.py
new file mode 100644
index 000000000..48dd15db2
--- /dev/null
+++ b/Mailman/Queue/tests/test_runners.py
@@ -0,0 +1,236 @@
+# Copyright (C) 2001-2006 by the Free Software Foundation, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+# USA.
+
+"""Unit tests for the various Mailman qrunner modules."""
+
+import os
+import email
+import shutil
+import tempfile
+import unittest
+
+from Mailman.Message import Message
+from Mailman.Queue.NewsRunner import prepare_message
+from Mailman.Queue.Runner import Runner
+from Mailman.Queue.Switchboard import Switchboard
+from Mailman.testing.base import TestBase
+
+
+
+class TestPrepMessage(TestBase):
+ def test_remove_unacceptables(self):
+ eq = self.assertEqual
+ msg = email.message_from_string("""\
+From: aperson@dom.ain
+To: _xtest@dom.ain
+NNTP-Posting-Host: news.dom.ain
+NNTP-Posting-Date: today
+X-Trace: blah blah
+X-Complaints-To: abuse@dom.ain
+Xref: blah blah
+Xref: blah blah
+Date-Received: yesterday
+Posted: tomorrow
+Posting-Version: 99.99
+Relay-Version: 88.88
+Received: blah blah
+
+A message
+""")
+ msgdata = {}
+ prepare_message(self._mlist, msg, msgdata)
+ eq(msgdata.get('prepped'), 1)
+ eq(msg['from'], 'aperson@dom.ain')
+ eq(msg['to'], '_xtest@dom.ain')
+ eq(msg['nntp-posting-host'], None)
+ eq(msg['nntp-posting-date'], None)
+ eq(msg['x-trace'], None)
+ eq(msg['x-complaints-to'], None)
+ eq(msg['xref'], None)
+ eq(msg['date-received'], None)
+ eq(msg['posted'], None)
+ eq(msg['posting-version'], None)
+ eq(msg['relay-version'], None)
+ eq(msg['received'], None)
+
+ def test_munge_duplicates_no_duplicates(self):
+ eq = self.assertEqual
+ msg = email.message_from_string("""\
+From: aperson@dom.ain
+To: _xtest@dom.ain
+Cc: someother@dom.ain
+Content-Transfer-Encoding: yes
+
+A message
+""")
+ msgdata = {}
+ prepare_message(self._mlist, msg, msgdata)
+ eq(msgdata.get('prepped'), 1)
+ eq(msg['from'], 'aperson@dom.ain')
+ eq(msg['to'], '_xtest@dom.ain')
+ eq(msg['cc'], 'someother@dom.ain')
+ eq(msg['content-transfer-encoding'], 'yes')
+
+ def test_munge_duplicates(self):
+ eq = self.assertEqual
+ msg = email.message_from_string("""\
+From: aperson@dom.ain
+To: _xtest@dom.ain
+To: two@dom.ain
+Cc: three@dom.ain
+Cc: four@dom.ain
+Cc: five@dom.ain
+Content-Transfer-Encoding: yes
+Content-Transfer-Encoding: no
+Content-Transfer-Encoding: maybe
+
+A message
+""")
+ msgdata = {}
+ prepare_message(self._mlist, msg, msgdata)
+ eq(msgdata.get('prepped'), 1)
+ eq(msg.get_all('from'), ['aperson@dom.ain'])
+ eq(msg.get_all('to'), ['_xtest@dom.ain'])
+ eq(msg.get_all('cc'), ['three@dom.ain'])
+ eq(msg.get_all('content-transfer-encoding'), ['yes'])
+ eq(msg.get_all('x-original-to'), ['two@dom.ain'])
+ eq(msg.get_all('x-original-cc'), ['four@dom.ain', 'five@dom.ain'])
+ eq(msg.get_all('x-original-content-transfer-encoding'),
+ ['no', 'maybe'])
+
+
+
+class TestSwitchboard(TestBase):
+ def setUp(self):
+ TestBase.setUp(self)
+ self._tmpdir = tempfile.mkdtemp()
+ self._msg = email.message_from_string("""\
+From: aperson@dom.ain
+To: _xtest@dom.ain
+
+A test message.
+""")
+ self._sb = Switchboard(self._tmpdir)
+
+ def tearDown(self):
+ shutil.rmtree(self._tmpdir, True)
+ TestBase.tearDown(self)
+
+ def _count_qfiles(self):
+ files = {}
+ for qfile in os.listdir(self._tmpdir):
+ root, ext = os.path.splitext(qfile)
+ files[ext] = files.get(ext, 0) + 1
+ return files
+
+ def test_whichq(self):
+ self.assertEqual(self._sb.whichq(), self._tmpdir)
+
+ def test_enqueue(self):
+ eq = self.assertEqual
+ self._sb.enqueue(self._msg, listname='_xtest@dom.ain')
+ files = self._count_qfiles()
+ eq(len(files), 1)
+ eq(files['.pck'], 1)
+
+ def test_dequeue(self):
+ eq = self.assertEqual
+ self._sb.enqueue(self._msg, listname='_xtest@dom.ain', foo='yes')
+ count = 0
+ for filebase in self._sb.files():
+ msg, data = self._sb.dequeue(filebase)
+ self._sb.finish(filebase)
+ count += 1
+ eq(msg['from'], self._msg['from'])
+ eq(msg['to'], self._msg['to'])
+ eq(data['foo'], 'yes')
+ eq(count, 1)
+ files = self._count_qfiles()
+ eq(len(files), 0)
+
+ def test_bakfile(self):
+ eq = self.assertEqual
+ self._sb.enqueue(self._msg, listname='_xtest@dom.ain')
+ self._sb.enqueue(self._msg, listname='_xtest@dom.ain')
+ self._sb.enqueue(self._msg, listname='_xtest@dom.ain')
+ for filebase in self._sb.files():
+ self._sb.dequeue(filebase)
+ files = self._count_qfiles()
+ eq(len(files), 1)
+ eq(files['.bak'], 3)
+
+ def test_recover(self):
+ eq = self.assertEqual
+ self._sb.enqueue(self._msg, listname='_xtest@dom.ain')
+ for filebase in self._sb.files():
+ self._sb.dequeue(filebase)
+ # Not calling sb.finish() leaves .bak files
+ sb2 = Switchboard(self._tmpdir, recover=True)
+ files = self._count_qfiles()
+ eq(len(files), 1)
+ eq(files['.pck'], 1)
+
+
+
+class TestableRunner(Runner):
+ def _dispose(self, mlist, msg, msgdata):
+ self.msg = msg
+ self.data = msgdata
+ return False
+
+ def _doperiodic(self):
+ self.stop()
+
+ def _snooze(self, filecnt):
+ return
+
+
+class TestRunner(TestBase):
+ def setUp(self):
+ TestBase.setUp(self)
+ self._tmpdir = tempfile.mkdtemp()
+ self._msg = email.message_from_string("""\
+From: aperson@dom.ain
+To: _xtest@dom.ain
+
+A test message.
+""", Message)
+ class MyRunner(TestableRunner):
+ QDIR = self._tmpdir
+ self._runner = MyRunner()
+
+ def tearDown(self):
+ shutil.rmtree(self._tmpdir, True)
+ TestBase.tearDown(self)
+
+ def test_run_loop(self):
+ eq = self.assertEqual
+ sb = Switchboard(self._tmpdir)
+ sb.enqueue(self._msg, listname='_xtest@example.com', foo='yes')
+ self._runner.run()
+ eq(self._runner.msg['from'], self._msg['from'])
+ eq(self._runner.msg['to'], self._msg['to'])
+ eq(self._runner.data['foo'], 'yes')
+
+
+
+def test_suite():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(TestPrepMessage))
+ suite.addTest(unittest.makeSuite(TestSwitchboard))
+ suite.addTest(unittest.makeSuite(TestRunner))
+ return suite