~ubuntu-branches/ubuntu/trusty/swift/trusty-updates

« back to all changes in this revision

Viewing changes to test/unit/obj/test_auditor.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2013-08-13 10:37:13 UTC
  • mfrom: (1.2.21)
  • Revision ID: package-import@ubuntu.com-20130813103713-1ctbx4zifyljs2aq
Tags: 1.9.1-0ubuntu1
[ James Page ]
* d/control: Update VCS fields for new branch locations.

[ Chuck Short ]
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 
16
16
from test import unit
17
17
import unittest
 
18
import mock
18
19
import os
19
20
import time
20
21
from shutil import rmtree
22
23
from tempfile import mkdtemp
23
24
from test.unit import FakeLogger
24
25
from swift.obj import auditor
25
 
from swift.obj import server as object_server
26
 
from swift.obj.server import DiskFile, write_metadata, DATADIR
 
26
from swift.obj.diskfile import DiskFile, write_metadata, invalidate_hash
 
27
from swift.obj.server import DATADIR
27
28
from swift.common.utils import hash_path, mkdirs, normalize_timestamp, \
28
29
    storage_directory
29
 
from swift.obj.base import invalidate_hash
30
30
 
31
31
 
32
32
class TestAuditor(unittest.TestCase):
50
50
 
51
51
        self.conf = dict(
52
52
            devices=self.devices,
53
 
            mount_check='false')
 
53
            mount_check='false',
 
54
            object_size_stats='10,100,1024,10240')
54
55
        self.disk_file = DiskFile(self.devices, 'sda', '0', 'a', 'c', 'o',
55
56
                                  self.logger)
56
57
 
140
141
            'sda', '0')
141
142
        self.assertEquals(self.auditor.quarantines, pre_quarantines + 1)
142
143
 
143
 
    def test_object_audit_bad_args(self):
 
144
    def test_generic_exception_handling(self):
144
145
        self.auditor = auditor.AuditorWorker(self.conf, self.logger)
 
146
        timestamp = str(normalize_timestamp(time.time()))
145
147
        pre_errors = self.auditor.errors
146
 
        self.auditor.object_audit(5, 'sda', '0')
 
148
        data = '0' * 1024
 
149
        etag = md5()
 
150
        with self.disk_file.writer() as writer:
 
151
            writer.write(data)
 
152
            etag.update(data)
 
153
            etag = etag.hexdigest()
 
154
            metadata = {
 
155
                'ETag': etag,
 
156
                'X-Timestamp': timestamp,
 
157
                'Content-Length': str(os.fstat(writer.fd).st_size),
 
158
            }
 
159
            writer.put(metadata)
 
160
        with mock.patch('swift.obj.diskfile.DiskFile',
 
161
                        lambda *_: 1 / 0):
 
162
            self.auditor.audit_all_objects()
147
163
        self.assertEquals(self.auditor.errors, pre_errors + 1)
148
 
        pre_errors = self.auditor.errors
149
 
        self.auditor.object_audit('badpath', 'sda', '0')
150
 
        self.assertEquals(self.auditor.errors, pre_errors)  # just returns
151
164
 
152
165
    def test_object_run_once_pass(self):
153
166
        self.auditor = auditor.AuditorWorker(self.conf, self.logger)
168
181
            writer.put(metadata)
169
182
        self.auditor.audit_all_objects()
170
183
        self.assertEquals(self.auditor.quarantines, pre_quarantines)
 
184
        self.assertEquals(self.auditor.stats_buckets[1024], 1)
 
185
        self.assertEquals(self.auditor.stats_buckets[10240], 0)
171
186
 
172
187
    def test_object_run_once_no_sda(self):
173
188
        self.auditor = auditor.AuditorWorker(self.conf, self.logger)
322
337
                rat[0] = True
323
338
                DiskFile.close(self, verify_file=verify_file)
324
339
        self.setup_bad_zero_byte()
325
 
        was_df = object_server.DiskFile
 
340
        was_df = auditor.diskfile.DiskFile
326
341
        try:
327
 
            object_server.DiskFile = FakeFile
 
342
            auditor.diskfile.DiskFile = FakeFile
328
343
            self.auditor.run_once(zero_byte_fps=50)
329
344
            quarantine_path = os.path.join(self.devices,
330
345
                                           'sda', 'quarantined', 'objects')
331
346
            self.assertTrue(os.path.isdir(quarantine_path))
332
347
            self.assertTrue(rat[0])
333
348
        finally:
334
 
            object_server.DiskFile = was_df
 
349
            auditor.diskfile.DiskFile = was_df
335
350
 
336
351
    def test_run_forever(self):
337
352