~sambuddhabasu1/mailman/fix_mailman_run_error

« back to all changes in this revision

Viewing changes to src/mailman/runners/tests/test_archiver.py

  • Committer: Barry Warsaw
  • Date: 2012-03-26 12:04:00 UTC
  • Revision ID: barry@list.org-20120326120400-jfezy6cg60ygod7k
Architecture
------------
 * Internally, all datetimes are kept in the UTC timezone, however because of
   LP: #280708, they are stored in the database in naive format.
 * `received_time` is now added to the message metadata by the LMTP runner
   instead of by `Switchboard.enqueue()`.  This latter no longer depends on
   `received_time` in the metadata.
 * The `ArchiveRunner` no longer acquires a lock before it calls the
   individual archiver implementations, since not all of them need a lock.  If
   they do, the implementations must acquire said lock themselves.

Configuration
-------------
 * New configuration variables `clobber_date` and `clobber_skew` supported in
   every `[archiver.<name>]` section.  These are used to determine under what
   circumstances a message destined for a specific archiver should have its
   `Date:` header clobbered.

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    make_testable_runner,
40
40
    specialized_message_from_string as mfs)
41
41
from mailman.testing.layers import ConfigLayer
 
42
from mailman.utilities.datetime import RFC822_DATE_FMT, factory, now
 
43
 
 
44
 
 
45
 
 
46
# This helper will set up a specific archiver as appropriate for a specific
 
47
# test.  It assumes the setUp() will just disable all archivers.
 
48
def archiver(name, enable=False, clobber=None, skew=None):
 
49
    def decorator(func):
 
50
        def wrapper(*args, **kws):
 
51
            config_name = 'archiver {0}'.format(name)
 
52
            section = """
 
53
            [archiver.{0}]
 
54
            enable: {1}
 
55
            clobber_date: {2}
 
56
            clobber_skew: {3}
 
57
            """.format(name,
 
58
                       'yes' if enable else 'no',
 
59
                       clobber, skew)
 
60
            config.push(config_name, section)
 
61
            try:
 
62
                return func(*args, **kws)
 
63
            finally:
 
64
                config.pop(config_name)
 
65
        return wrapper
 
66
    return decorator
42
67
 
43
68
 
44
69
 
54
79
    def permalink(mlist, msg):
55
80
        filename = msg['x-message-id-hash']
56
81
        return 'http://archive.example.com/' + filename
57
 
    
 
82
 
58
83
    @staticmethod
59
84
    def archive_message(mlist, msg):
60
85
        filename = msg['x-message-id-hash']
73
98
 
74
99
    def setUp(self):
75
100
        self._mlist = create_list('test@example.com')
 
101
        self._now = now()
76
102
        # Enable just the dummy archiver.
77
103
        config.push('dummy', """
78
104
        [archiver.dummy]
79
105
        class: mailman.runners.tests.test_archiver.DummyArchiver
80
 
        enable: yes
 
106
        enable: no
81
107
        [archiver.prototype]
82
108
        enable: no
83
109
        [archiver.mhonarc]
100
126
    def tearDown(self):
101
127
        config.pop('dummy')
102
128
 
 
129
    @archiver('dummy', enable=True)
103
130
    def test_archive_runner(self):
104
131
        # Ensure that the archive runner ends up archiving the message.
105
132
        self._archiveq.enqueue(
106
 
            self._msg, {}, listname=self._mlist.fqdn_listname)
 
133
            self._msg, {},
 
134
            listname=self._mlist.fqdn_listname,
 
135
            received_time=now())
107
136
        self._runner.run()
108
137
        # There should now be a copy of the message in the file system.
109
138
        filename = os.path.join(
112
141
            archived = message_from_file(fp)
113
142
        self.assertEqual(archived['message-id'], '<first>')
114
143
 
 
144
    @archiver('dummy', enable=True)
115
145
    def test_archive_runner_with_dated_message(self):
116
 
        # LP: #963612 FIXME
117
 
        self._msg['Date'] = 'Sat, 11 Mar 2011 03:19:38 -0500'
118
 
        self._archiveq.enqueue(
119
 
            self._msg, {}, listname=self._mlist.fqdn_listname)
120
 
        self._runner.run()
121
 
        # There should now be a copy of the message in the file system.
122
 
        filename = os.path.join(
123
 
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
124
 
        with open(filename) as fp:
125
 
            archived = message_from_file(fp)
126
 
        self.assertEqual(archived['message-id'], '<first>')
 
146
        # Date headers don't throw off the archiver runner.
 
147
        self._msg['Date'] = now(strip_tzinfo=False).strftime(RFC822_DATE_FMT)
 
148
        self._archiveq.enqueue(
 
149
            self._msg, {},
 
150
            listname=self._mlist.fqdn_listname,
 
151
            received_time=now())
 
152
        self._runner.run()
 
153
        # There should now be a copy of the message in the file system.
 
154
        filename = os.path.join(
 
155
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
 
156
        with open(filename) as fp:
 
157
            archived = message_from_file(fp)
 
158
        self.assertEqual(archived['message-id'], '<first>')
 
159
        self.assertEqual(archived['date'], 'Mon, 01 Aug 2005 07:49:23 +0000')
 
160
 
 
161
    @archiver('dummy', enable=True, clobber='never')
 
162
    def test_clobber_date_never(self):
 
163
        # Even if the Date header is insanely off from the received time of
 
164
        # the message, if clobber_date is 'never', the header is not clobbered.
 
165
        self._msg['Date'] = now(strip_tzinfo=False).strftime(RFC822_DATE_FMT)
 
166
        self._archiveq.enqueue(
 
167
            self._msg, {},
 
168
            listname=self._mlist.fqdn_listname,
 
169
            received_time=now())
 
170
        self._runner.run()
 
171
        # There should now be a copy of the message in the file system.
 
172
        filename = os.path.join(
 
173
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
 
174
        with open(filename) as fp:
 
175
            archived = message_from_file(fp)
 
176
        self.assertEqual(archived['message-id'], '<first>')
 
177
        self.assertEqual(archived['date'], 'Mon, 01 Aug 2005 07:49:23 +0000')
 
178
 
 
179
    @archiver('dummy', enable=True)
 
180
    def test_clobber_dateless(self):
 
181
        # A message with no Date header will always get clobbered.
 
182
        self.assertEqual(self._msg['date'], None)
 
183
        # Now, before enqueuing the message (well, really, calling 'now()'
 
184
        # again), fast forward a few days.
 
185
        self._archiveq.enqueue(
 
186
            self._msg, {},
 
187
            listname=self._mlist.fqdn_listname,
 
188
            received_time=now(strip_tzinfo=False))
 
189
        self._runner.run()
 
190
        # There should now be a copy of the message in the file system.
 
191
        filename = os.path.join(
 
192
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
 
193
        with open(filename) as fp:
 
194
            archived = message_from_file(fp)
 
195
        self.assertEqual(archived['message-id'], '<first>')
 
196
        self.assertEqual(archived['date'], 'Mon, 01 Aug 2005 07:49:23 +0000')
 
197
 
 
198
    @archiver('dummy', enable=True, clobber='always')
 
199
    def test_clobber_date_always(self):
 
200
        # The date always gets clobbered with the current received time.
 
201
        self._msg['Date'] = now(strip_tzinfo=False).strftime(RFC822_DATE_FMT)
 
202
        # Now, before enqueuing the message (well, really, calling 'now()'
 
203
        # again as will happen in the runner), fast forward a few days.
 
204
        self._archiveq.enqueue(
 
205
            self._msg, {},
 
206
            listname=self._mlist.fqdn_listname)
 
207
        factory.fast_forward(days=4)
 
208
        self._runner.run()
 
209
        # There should now be a copy of the message in the file system.
 
210
        filename = os.path.join(
 
211
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
 
212
        with open(filename) as fp:
 
213
            archived = message_from_file(fp)
 
214
        self.assertEqual(archived['message-id'], '<first>')
 
215
        self.assertEqual(archived['date'], 'Fri, 05 Aug 2005 07:49:23 +0000')
 
216
        self.assertEqual(archived['x-original-date'],
 
217
                         'Mon, 01 Aug 2005 07:49:23 +0000')
 
218
 
 
219
    @archiver('dummy', enable=True, clobber='maybe', skew='1d')
 
220
    def test_clobber_date_maybe_when_insane(self):
 
221
        # The date is clobbered if it's farther off from now than its skew
 
222
        # period.
 
223
        self._msg['Date'] = now(strip_tzinfo=False).strftime(RFC822_DATE_FMT)
 
224
        # Now, before enqueuing the message (well, really, calling 'now()'
 
225
        # again as will happen in the runner), fast forward a few days.
 
226
        self._archiveq.enqueue(
 
227
            self._msg, {},
 
228
            listname=self._mlist.fqdn_listname)
 
229
        factory.fast_forward(days=4)
 
230
        self._runner.run()
 
231
        # There should now be a copy of the message in the file system.
 
232
        filename = os.path.join(
 
233
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
 
234
        with open(filename) as fp:
 
235
            archived = message_from_file(fp)
 
236
        self.assertEqual(archived['message-id'], '<first>')
 
237
        self.assertEqual(archived['date'], 'Fri, 05 Aug 2005 07:49:23 +0000')
 
238
        self.assertEqual(archived['x-original-date'],
 
239
                         'Mon, 01 Aug 2005 07:49:23 +0000')
 
240
 
 
241
    @archiver('dummy', enable=True, clobber='maybe', skew='10d')
 
242
    def test_clobber_date_maybe_when_sane(self):
 
243
        # The date is not clobbered if it's nearer to now than its skew
 
244
        # period.
 
245
        self._msg['Date'] = now(strip_tzinfo=False).strftime(RFC822_DATE_FMT)
 
246
        # Now, before enqueuing the message (well, really, calling 'now()'
 
247
        # again as will happen in the runner), fast forward a few days.
 
248
        self._archiveq.enqueue(
 
249
            self._msg, {},
 
250
            listname=self._mlist.fqdn_listname)
 
251
        factory.fast_forward(days=4)
 
252
        self._runner.run()
 
253
        # There should now be a copy of the message in the file system.
 
254
        filename = os.path.join(
 
255
            config.MESSAGES_DIR, '4CMWUN6BHVCMHMDAOSJZ2Q72G5M32MWB')
 
256
        with open(filename) as fp:
 
257
            archived = message_from_file(fp)
 
258
        self.assertEqual(archived['message-id'], '<first>')
 
259
        self.assertEqual(archived['date'], 'Mon, 01 Aug 2005 07:49:23 +0000')
 
260
        self.assertEqual(archived['x-original-date'], None)