~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/services/apachelogparser/tests/test_apachelogparser.py

[r=gmb][ui=none][bug=588288] log parser should not read entire
        remaining file contents into memory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import os
7
7
from StringIO import StringIO
8
8
import tempfile
 
9
import textwrap
9
10
import unittest
10
11
 
11
12
from zope.component import getUtility
12
13
 
 
14
from canonical.config import config
13
15
from canonical.launchpad.scripts.logger import BufferLogger
14
16
from canonical.launchpad.webapp.interfaces import (
15
17
    IStoreSelector, MAIN_STORE, DEFAULT_FLAVOR)
215
217
        self.assertEqual(self.logger.buffer.getvalue(), '')
216
218
 
217
219
        date = datetime(2008, 6, 13)
218
 
        self.assertEqual(downloads, 
 
220
        self.assertEqual(downloads,
219
221
            {'/15018215/ul_logo_64x64.png':
220
222
                {datetime(2008, 6, 13): {'US': 1}}})
221
223
 
222
224
        self.assertEqual(parsed_bytes, fd.tell())
223
225
 
 
226
    def test_max_parsed_lines(self):
 
227
        # The max_parsed_lines config option limits the number of parsed
 
228
        # lines.
 
229
        config.push(
 
230
            'log_parser config',
 
231
            textwrap.dedent('''\
 
232
                [launchpad]
 
233
                logparser_max_parsed_lines: 2
 
234
                '''))
 
235
        fd = open(os.path.join(
 
236
            here, 'apache-log-files', 'launchpadlibrarian.net.access-log'))
 
237
        downloads, parsed_bytes = parse_file(
 
238
            fd, start_position=0, logger=self.logger,
 
239
            get_download_key=get_path_download_key)
 
240
        config.pop("log_parser config")
 
241
 
 
242
        self.assertEqual(self.logger.buffer.getvalue(), '')
 
243
        date = datetime(2008, 6, 13)
 
244
        self.assertContentEqual(
 
245
            downloads.items(),
 
246
            [('/12060796/me-tv-icon-64x64.png', {date: {'AU': 1}}),
 
247
             ('/9096290/me-tv-icon-14x14.png', {date: {'AU': 1}})])
 
248
 
 
249
        # We should have parsed only the first two lines of data.
 
250
        fd.seek(0)
 
251
        lines = fd.readlines()
 
252
        self.assertEqual(parsed_bytes, len(lines[0]) + len(lines[1]))
 
253
 
224
254
 
225
255
class TestParsedFilesDetection(TestCase):
226
256
    """Test the detection of already parsed logs."""
263
293
 
264
294
    def test_different_files_with_same_name(self):
265
295
        # Thanks to log rotation, two runs of our script may see files with
266
 
        # the same name but completely different content.  If we see a file 
 
296
        # the same name but completely different content.  If we see a file
267
297
        # with a name matching that of an already parsed file but with content
268
298
        # differing from the last file with that name parsed, we know we need
269
299
        # to parse the file from the start.