The MHonArc archiver must set stdin=PIPE when calling the subprocess. Given
[mailman.git] / src / mailman / archiving / tests / test_mhonarc.py
blob515d3ca49125d6348ccb3a33ea24ac7e0bcaabb1
1 # Copyright (C) 2015 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """Test the MHonArc archiver."""
20 __all__ = [
21 'TestMhonarc',
25 import os
26 import sys
27 import shutil
28 import tempfile
29 import unittest
31 from mailman.archiving.mhonarc import MHonArc
32 from mailman.app.lifecycle import create_list
33 from mailman.database.transaction import transaction
34 from mailman.testing.helpers import (
35 configuration, specialized_message_from_string as mfs)
36 from mailman.testing.layers import ConfigLayer
37 from pkg_resources import resource_filename
40 class TestMhonarc(unittest.TestCase):
41 """Test the MHonArc archiver."""
43 layer = ConfigLayer
45 def setUp(self):
46 # Create a fake mailing list and message object.
47 self._msg = mfs("""\
48 To: test@example.com
49 From: anne@example.com
50 Subject: Testing the test list
51 Message-ID: <ant>
52 Message-ID-Hash: MS6QLWERIJLGCRF44J7USBFDELMNT2BW
54 Tests are better than no tests
55 but the water deserves to be swum.
56 """)
57 with transaction():
58 self._mlist = create_list('test@example.com')
59 tempdir = tempfile.mkdtemp()
60 self.addCleanup(shutil.rmtree, tempdir)
61 # Here's the command to execute our fake MHonArc process.
62 shutil.copy(
63 resource_filename('mailman.archiving.tests', 'fake_mhonarc.py'),
64 tempdir)
65 self._output_file = os.path.join(tempdir, 'output.txt')
66 command = '{} {} {}'.format(
67 sys.executable,
68 os.path.join(tempdir, 'fake_mhonarc.py'),
69 self._output_file)
70 # Write an external configuration file which points the command at our
71 # fake MHonArc process.
72 self._cfg = os.path.join(tempdir, 'mhonarc.cfg')
73 with open(self._cfg, 'w', encoding='utf-8') as fp:
74 print("""\
75 [general]
76 base_url: http://$hostname/archives/$fqdn_listname
77 command: {command}
78 """.format(command=command), file=fp)
80 def test_mhonarc(self):
81 # The archiver properly sends stdin to the subprocess.
82 with configuration('archiver.mhonarc',
83 configuration=self._cfg,
84 enable='yes'):
85 MHonArc().archive_message(self._mlist, self._msg)
86 with open(self._output_file, 'r', encoding='utf-8') as fp:
87 results = fp.read().splitlines()
88 self.assertEqual(results[0], '<ant>')
89 self.assertEqual(results[1], 'MS6QLWERIJLGCRF44J7USBFDELMNT2BW')