From cfa4f9497d8903d340f05f8314f40c814e3dde1c Mon Sep 17 00:00:00 2001 From: Pete Zaitcev Date: Fri, 13 Sep 2013 13:55:10 -0600 Subject: [PATCH] Use a local variable auditor_worker This patch is only a cleanup, but our code makes me rage every time I read it. In short, we have a class with a variable self.auditor, and we assign ObjectAuditor to it at some ties, AuditorWorker at other times. So, whenever there's a mismerge or whatever, you cannot tell if self.auditor.broker_class makes sense or not. Since all cases of using self.auditor as AuditorWorker are purely local, just evict it to a local variable auditor_worker. Leave self.auditor to represent ObjectAuditor, because it's used as a class variable in places. Change-Id: I811b80ac6c69a4daacfed7a5918bc0b15761d72e --- test/unit/obj/test_auditor.py | 82 +++++++++++++++++------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/test/unit/obj/test_auditor.py b/test/unit/obj/test_auditor.py index 94a6c5b57b..bbd7070ba8 100644 --- a/test/unit/obj/test_auditor.py +++ b/test/unit/obj/test_auditor.py @@ -60,7 +60,7 @@ class TestAuditor(unittest.TestCase): unit.xattr_data = {} def test_object_audit_extra_data(self): - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) data = '0' * 1024 etag = md5() with self.disk_file.create() as writer: @@ -74,21 +74,21 @@ class TestAuditor(unittest.TestCase): 'Content-Length': str(os.fstat(writer.fd).st_size), } writer.put(metadata) - pre_quarantines = self.auditor.quarantines + pre_quarantines = auditor_worker.quarantines - self.auditor.object_audit( + auditor_worker.object_audit( os.path.join(self.disk_file.datadir, timestamp + '.data'), 'sda', '0') - self.assertEquals(self.auditor.quarantines, pre_quarantines) + self.assertEquals(auditor_worker.quarantines, pre_quarantines) os.write(writer.fd, 'extra_data') - self.auditor.object_audit( + auditor_worker.object_audit( os.path.join(self.disk_file.datadir, timestamp + '.data'), 'sda', '0') - self.assertEquals(self.auditor.quarantines, pre_quarantines + 1) + self.assertEquals(auditor_worker.quarantines, pre_quarantines + 1) def test_object_audit_diff_data(self): - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) data = '0' * 1024 etag = md5() timestamp = str(normalize_timestamp(time.time())) @@ -102,16 +102,16 @@ class TestAuditor(unittest.TestCase): 'Content-Length': str(os.fstat(writer.fd).st_size), } writer.put(metadata) - pre_quarantines = self.auditor.quarantines + pre_quarantines = auditor_worker.quarantines # remake so it will have metadata self.disk_file = DiskFile(self.devices, 'sda', '0', 'a', 'c', 'o', self.logger) - self.auditor.object_audit( + auditor_worker.object_audit( os.path.join(self.disk_file.datadir, timestamp + '.data'), 'sda', '0') - self.assertEquals(self.auditor.quarantines, pre_quarantines) + self.assertEquals(auditor_worker.quarantines, pre_quarantines) etag = md5() etag.update('1' + '0' * 1023) etag = etag.hexdigest() @@ -121,10 +121,10 @@ class TestAuditor(unittest.TestCase): writer.write(data) writer.put(metadata) - self.auditor.object_audit( + auditor_worker.object_audit( os.path.join(self.disk_file.datadir, timestamp + '.data'), 'sda', '0') - self.assertEquals(self.auditor.quarantines, pre_quarantines + 1) + self.assertEquals(auditor_worker.quarantines, pre_quarantines + 1) def test_object_audit_no_meta(self): timestamp = str(normalize_timestamp(time.time())) @@ -134,12 +134,12 @@ class TestAuditor(unittest.TestCase): fp.write('0' * 1024) fp.close() invalidate_hash(os.path.dirname(self.disk_file.datadir)) - self.auditor = auditor.AuditorWorker(self.conf, self.logger) - pre_quarantines = self.auditor.quarantines - self.auditor.object_audit( + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) + pre_quarantines = auditor_worker.quarantines + auditor_worker.object_audit( os.path.join(self.disk_file.datadir, timestamp + '.data'), 'sda', '0') - self.assertEquals(self.auditor.quarantines, pre_quarantines + 1) + self.assertEquals(auditor_worker.quarantines, pre_quarantines + 1) def test_object_audit_will_not_swallow_errors_in_tests(self): timestamp = str(normalize_timestamp(time.time())) @@ -147,13 +147,13 @@ class TestAuditor(unittest.TestCase): mkdirs(self.disk_file.datadir) with open(path, 'w') as f: write_metadata(f, {'name': '/a/c/o'}) - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) def blowup(*args): raise NameError('tpyo') with mock.patch('swift.obj.diskfile.DiskFile', blowup): - self.assertRaises(NameError, self.auditor.object_audit, + self.assertRaises(NameError, auditor_worker.object_audit, path, 'sda', '0') def test_failsafe_object_audit_will_swallow_errors_in_tests(self): @@ -162,19 +162,19 @@ class TestAuditor(unittest.TestCase): mkdirs(self.disk_file.datadir) with open(path, 'w') as f: write_metadata(f, {'name': '/a/c/o'}) - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) def blowup(*args): raise NameError('tpyo') with mock.patch('swift.obj.diskfile.DiskFile', blowup): - self.auditor.failsafe_object_audit(path, 'sda', '0') - self.assertEquals(self.auditor.errors, 1) + auditor_worker.failsafe_object_audit(path, 'sda', '0') + self.assertEquals(auditor_worker.errors, 1) def test_generic_exception_handling(self): - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) timestamp = str(normalize_timestamp(time.time())) - pre_errors = self.auditor.errors + pre_errors = auditor_worker.errors data = '0' * 1024 etag = md5() with self.disk_file.create() as writer: @@ -189,14 +189,14 @@ class TestAuditor(unittest.TestCase): writer.put(metadata) with mock.patch('swift.obj.diskfile.DiskFile', lambda *_: 1 / 0): - self.auditor.audit_all_objects() - self.assertEquals(self.auditor.errors, pre_errors + 1) + auditor_worker.audit_all_objects() + self.assertEquals(auditor_worker.errors, pre_errors + 1) def test_object_run_once_pass(self): - self.auditor = auditor.AuditorWorker(self.conf, self.logger) - self.auditor.log_time = 0 + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker.log_time = 0 timestamp = str(normalize_timestamp(time.time())) - pre_quarantines = self.auditor.quarantines + pre_quarantines = auditor_worker.quarantines data = '0' * 1024 etag = md5() with self.disk_file.create() as writer: @@ -209,15 +209,15 @@ class TestAuditor(unittest.TestCase): 'Content-Length': str(os.fstat(writer.fd).st_size), } writer.put(metadata) - self.auditor.audit_all_objects() - self.assertEquals(self.auditor.quarantines, pre_quarantines) - self.assertEquals(self.auditor.stats_buckets[1024], 1) - self.assertEquals(self.auditor.stats_buckets[10240], 0) + auditor_worker.audit_all_objects() + self.assertEquals(auditor_worker.quarantines, pre_quarantines) + self.assertEquals(auditor_worker.stats_buckets[1024], 1) + self.assertEquals(auditor_worker.stats_buckets[10240], 0) def test_object_run_once_no_sda(self): - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) timestamp = str(normalize_timestamp(time.time())) - pre_quarantines = self.auditor.quarantines + pre_quarantines = auditor_worker.quarantines data = '0' * 1024 etag = md5() with self.disk_file.create() as writer: @@ -231,13 +231,13 @@ class TestAuditor(unittest.TestCase): } writer.put(metadata) os.write(writer.fd, 'extra_data') - self.auditor.audit_all_objects() - self.assertEquals(self.auditor.quarantines, pre_quarantines + 1) + auditor_worker.audit_all_objects() + self.assertEquals(auditor_worker.quarantines, pre_quarantines + 1) def test_object_run_once_multi_devices(self): - self.auditor = auditor.AuditorWorker(self.conf, self.logger) + auditor_worker = auditor.AuditorWorker(self.conf, self.logger) timestamp = str(normalize_timestamp(time.time())) - pre_quarantines = self.auditor.quarantines + pre_quarantines = auditor_worker.quarantines data = '0' * 10 etag = md5() with self.disk_file.create() as writer: @@ -250,7 +250,7 @@ class TestAuditor(unittest.TestCase): 'Content-Length': str(os.fstat(writer.fd).st_size), } writer.put(metadata) - self.auditor.audit_all_objects() + auditor_worker.audit_all_objects() self.disk_file = DiskFile(self.devices, 'sdb', '0', 'a', 'c', 'ob', self.logger) data = '1' * 10 @@ -266,8 +266,8 @@ class TestAuditor(unittest.TestCase): } writer.put(metadata) os.write(writer.fd, 'extra_data') - self.auditor.audit_all_objects() - self.assertEquals(self.auditor.quarantines, pre_quarantines + 1) + auditor_worker.audit_all_objects() + self.assertEquals(auditor_worker.quarantines, pre_quarantines + 1) def test_object_run_fast_track_non_zero(self): self.auditor = auditor.ObjectAuditor(self.conf)