~ubuntu-branches/ubuntu/oneiric/ubuntuone-client/oneiric

« back to all changes in this revision

Viewing changes to tests/syncdaemon/test_tritcask.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-02-23 18:34:09 UTC
  • mfrom: (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110223183409-535o7yo165wbjmca
Tags: 1.5.5-0ubuntu1
* New upstream release.
  - Subscribing to a RO share will not download content (LP: #712528)
  - Can't synchronize "~/Ubuntu One Music" (LP: #714976)
  - Syncdaemon needs to show progress in Unity launcher (LP: #702116)
  - Notifications say "your cloud" (LP: #715887)
  - No longer requires python-libproxy
  - Recommend unity and indicator libs by default

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    INACTIVE,
39
39
    DEAD,
40
40
    BadCrc,
 
41
    BadHeader,
41
42
    DataFile,
42
43
    TempDataFile,
43
44
    DeadDataFile,
166
167
        self.assertTrue(db.live_file.has_bad_crc, 'has_bad_crc should be True.')
167
168
        db.shutdown()
168
169
 
 
170
    def test_iter_entries_bad_header_unpack(self):
 
171
        """Test that unpack error during iter_entries is the same as EOF."""
 
172
        db = Tritcask(self.base_dir)
 
173
        for i in range(10):
 
174
            db.put(i, 'foo%d' % (i,), 'bar%s' % (i,))
 
175
        # truncate the file at the start of the header of the last value.
 
176
        curr_pos = db.live_file.fd.tell()
 
177
        db.live_file.fd.seek(curr_pos - header_size+4)
 
178
        db.live_file.fd.truncate()
 
179
        entries = []
 
180
        for i, entry in enumerate(db.live_file.iter_entries()):
 
181
            entries.append(entry)
 
182
            self.assertEqual(entry[4], i)
 
183
            self.assertEqual(entry[5], 'foo%d' % (i,))
 
184
            self.assertEqual(entry[6], 'bar%d' % (i,))
 
185
        self.assertEqual(9, len(entries))
 
186
        self.assertTrue(db.live_file.has_bad_data, 'has_bad_data should be True.')
 
187
        self.assertTrue(self.memento.check_warning('Found corrupted header on'))
 
188
        self.assertTrue(self.memento.check_warning(
 
189
            'the rest of the file will be ignored.'))
 
190
        db.shutdown()
 
191
 
169
192
    def test__getitem__(self):
170
193
        """Test our slicing support."""
171
194
        data_file = self.file_class(self.base_dir)
247
270
            with contextlib.closing(fmap):
248
271
                self.assertRaises(BadCrc, data_file.read, fmap, 0)
249
272
 
 
273
    def test_read_bad_header(self):
 
274
        """Test for read method with a bad header/unpack error."""
 
275
        data_file = self.file_class(self.base_dir)
 
276
        orig_tstamp, _, _ = data_file.write(0, 'foo', 'bar')
 
277
        # mess with the data on disk to make this entry crc32 invalid
 
278
        # seek to the end of crc32+header+key
 
279
        data_file.close()
 
280
        with open(data_file.filename, 'r+b') as fd:
 
281
            fd.read(crc32_size+4)
 
282
            fd.truncate()
 
283
            # write a different value -> random bytes
 
284
            fd.write(os.urandom(header_size/2))
 
285
            fd.flush()
 
286
            fmap = mmap.mmap(fd.fileno(), 0, access=mmap.ACCESS_READ)
 
287
            with contextlib.closing(fmap):
 
288
                self.assertRaises(BadHeader, data_file.read, fmap, 0)
 
289
 
250
290
 
251
291
class TempDataFileTest(DataFileTest):
252
292
    """Tests for TempDataFile."""
394
434
            'the rest of the file will be ignored.'))
395
435
        db.shutdown()
396
436
 
 
437
    def test_iter_entries_bad_header_unpack(self):
 
438
        """Test that unpack error during iter_entries is the same as EOF."""
 
439
        db = Tritcask(self.base_dir)
 
440
        for i in range(10):
 
441
            db.put(i, 'foo%d' % (i,), 'bar%s' % (i,))
 
442
        # truncate the file at the start of the header of the last value.
 
443
        db.live_file.fd.seek(crc32_size+header_size+8+crc32_size+4)
 
444
        db.live_file.fd.truncate()
 
445
        file_id = db.live_file.file_id
 
446
        db.rotate()
 
447
        db.shutdown()
 
448
        db = Tritcask(self.base_dir)
 
449
        entries = []
 
450
        for i, entry in enumerate(db._immutable[file_id].iter_entries()):
 
451
            entries.append(entry)
 
452
            self.assertEqual(entry[4], i)
 
453
            self.assertEqual(entry[5], 'foo%d' % (i,))
 
454
            self.assertEqual(entry[6], 'bar%d' % (i,))
 
455
        self.assertEqual(1, len(entries))
 
456
        self.assertTrue(db._immutable[file_id].has_bad_data,
 
457
                        'has_bad_data should be True.')
 
458
        self.assertTrue(self.memento.check_warning('Found corrupted header on'))
 
459
        self.assertTrue(self.memento.check_warning(
 
460
            'the rest of the file will be ignored.'))
 
461
 
 
462
        db.shutdown()
 
463
 
397
464
    def test__getitem__(self):
398
465
        """Test our slicing support."""
399
466
        rw_file = DataFile(self.base_dir)
461
528
        self.assertRaises(NotImplementedError, dead_file.read)
462
529
 
463
530
    test_read_bad_crc = test_read
 
531
    test_read_bad_header = test_read
464
532
 
465
533
    def test__open(self):
466
534
        """Test that always fails with NotImplementedError."""