trivial test_[db_]replicator cleanup
* add a self.temp_dir in setUp and remove in teraDown * be consistent in order of (expected, actual) args * assert the complete exception error line Related-Change: I289d3e9b6fe14159925786732ad748acd0459812 Change-Id: I185c8cd55db6df593bb3304c54c5160c1f662b86 Signed-off-by: Alistair Coles <alistairncoles@gmail.com>
This commit is contained in:
		| @@ -446,14 +446,16 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         self._patchers = [] |         self._patchers = [] | ||||||
|         # recon cache path |         # recon cache path | ||||||
|         self.recon_cache = mkdtemp() |         self.recon_cache = mkdtemp() | ||||||
|         rmtree(self.recon_cache, ignore_errors=1) |         rmtree(self.recon_cache, ignore_errors=True) | ||||||
|         os.mkdir(self.recon_cache) |         os.mkdir(self.recon_cache) | ||||||
|         self.logger = debug_logger('test-replicator') |         self.logger = debug_logger('test-replicator') | ||||||
|  |         self.temp_dir = mkdtemp() | ||||||
|  |  | ||||||
|     def tearDown(self): |     def tearDown(self): | ||||||
|         for patcher in self._patchers: |         for patcher in self._patchers: | ||||||
|             patcher.stop() |             patcher.stop() | ||||||
|         rmtree(self.recon_cache, ignore_errors=1) |         rmtree(self.recon_cache, ignore_errors=True) | ||||||
|  |         rmtree(self.temp_dir, ignore_errors=True) | ||||||
|  |  | ||||||
|     def _patch(self, patching_fn, *args, **kwargs): |     def _patch(self, patching_fn, *args, **kwargs): | ||||||
|         patcher = patching_fn(*args, **kwargs) |         patcher = patching_fn(*args, **kwargs) | ||||||
| @@ -1299,9 +1301,7 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         replicator._zero_stats() |         replicator._zero_stats() | ||||||
|         replicator.extract_device = lambda _: 'some_device' |         replicator.extract_device = lambda _: 'some_device' | ||||||
|  |  | ||||||
|         temp_dir = mkdtemp() |         temp_part_dir = os.path.join(self.temp_dir, '140') | ||||||
|         try: |  | ||||||
|             temp_part_dir = os.path.join(temp_dir, '140') |  | ||||||
|         os.mkdir(temp_part_dir) |         os.mkdir(temp_part_dir) | ||||||
|         temp_suf_dir = os.path.join(temp_part_dir, '16e') |         temp_suf_dir = os.path.join(temp_part_dir, '16e') | ||||||
|         os.mkdir(temp_suf_dir) |         os.mkdir(temp_suf_dir) | ||||||
| @@ -1315,7 +1315,7 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         temp_file2 = NamedTemporaryFile(dir=temp_hash_dir2, delete=False) |         temp_file2 = NamedTemporaryFile(dir=temp_hash_dir2, delete=False) | ||||||
|  |  | ||||||
|         # sanity-checks |         # sanity-checks | ||||||
|             self.assertTrue(os.path.exists(temp_dir)) |         self.assertTrue(os.path.exists(self.temp_dir)) | ||||||
|         self.assertTrue(os.path.exists(temp_part_dir)) |         self.assertTrue(os.path.exists(temp_part_dir)) | ||||||
|         self.assertTrue(os.path.exists(temp_suf_dir)) |         self.assertTrue(os.path.exists(temp_suf_dir)) | ||||||
|         self.assertTrue(os.path.exists(temp_hash_dir)) |         self.assertTrue(os.path.exists(temp_hash_dir)) | ||||||
| @@ -1327,7 +1327,7 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         temp_file.db_file = temp_file.name |         temp_file.db_file = temp_file.name | ||||||
|         replicator.delete_db(temp_file) |         replicator.delete_db(temp_file) | ||||||
|  |  | ||||||
|             self.assertTrue(os.path.exists(temp_dir)) |         self.assertTrue(os.path.exists(self.temp_dir)) | ||||||
|         self.assertTrue(os.path.exists(temp_part_dir)) |         self.assertTrue(os.path.exists(temp_part_dir)) | ||||||
|         self.assertTrue(os.path.exists(temp_suf_dir)) |         self.assertTrue(os.path.exists(temp_suf_dir)) | ||||||
|         self.assertFalse(os.path.exists(temp_hash_dir)) |         self.assertFalse(os.path.exists(temp_hash_dir)) | ||||||
| @@ -1342,7 +1342,7 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         temp_file2.db_file = temp_file2.name |         temp_file2.db_file = temp_file2.name | ||||||
|         replicator.delete_db(temp_file2) |         replicator.delete_db(temp_file2) | ||||||
|  |  | ||||||
|             self.assertTrue(os.path.exists(temp_dir)) |         self.assertTrue(os.path.exists(self.temp_dir)) | ||||||
|         self.assertFalse(os.path.exists(temp_part_dir)) |         self.assertFalse(os.path.exists(temp_part_dir)) | ||||||
|         self.assertFalse(os.path.exists(temp_suf_dir)) |         self.assertFalse(os.path.exists(temp_suf_dir)) | ||||||
|         self.assertFalse(os.path.exists(temp_hash_dir)) |         self.assertFalse(os.path.exists(temp_hash_dir)) | ||||||
| @@ -1353,8 +1353,6 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|             [(('removes.some_device',), {})] * 2, |             [(('removes.some_device',), {})] * 2, | ||||||
|             replicator.logger.statsd_client.calls['increment']) |             replicator.logger.statsd_client.calls['increment']) | ||||||
|         self.assertEqual(2, replicator.stats['remove']) |         self.assertEqual(2, replicator.stats['remove']) | ||||||
|         finally: |  | ||||||
|             rmtree(temp_dir) |  | ||||||
|  |  | ||||||
|     def test_extract_device(self): |     def test_extract_device(self): | ||||||
|         replicator = ConcreteReplicator({'devices': '/some/root'}) |         replicator = ConcreteReplicator({'devices': '/some/root'}) | ||||||
| @@ -1481,10 +1479,7 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         replicator = ConcreteReplicator({}, logger=self.logger) |         replicator = ConcreteReplicator({}, logger=self.logger) | ||||||
|         replicator._zero_stats() |         replicator._zero_stats() | ||||||
|         db_replicator.lock_parent_directory = lock_parent_directory |         db_replicator.lock_parent_directory = lock_parent_directory | ||||||
|  |         temp_part_dir = os.path.join(self.temp_dir, '140') | ||||||
|         temp_dir = mkdtemp() |  | ||||||
|         try: |  | ||||||
|             temp_part_dir = os.path.join(temp_dir, '140') |  | ||||||
|         os.mkdir(temp_part_dir) |         os.mkdir(temp_part_dir) | ||||||
|         suf_dir = os.path.join(temp_part_dir, '16e') |         suf_dir = os.path.join(temp_part_dir, '16e') | ||||||
|         os.mkdir(suf_dir) |         os.mkdir(suf_dir) | ||||||
| @@ -1505,22 +1500,20 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|             return os.rmdir(path) |             return os.rmdir(path) | ||||||
|  |  | ||||||
|         with mock.patch('swift.common.db_replicator.os', |         with mock.patch('swift.common.db_replicator.os', | ||||||
|                             new=mock.MagicMock(wraps=os)) as mock_os: |                         wraps=os) as mock_os: | ||||||
|             mock_os.rmdir.side_effect = rmdir_side_effect |             mock_os.rmdir.side_effect = rmdir_side_effect | ||||||
|             result = replicator.delete_db(broker) |             result = replicator.delete_db(broker) | ||||||
|         self.assertFalse(result) |         self.assertFalse(result) | ||||||
|  |         self.assertFalse(os.path.exists(hash_dir)) | ||||||
|  |         self.assertFalse(os.path.exists(object_file)) | ||||||
|  |  | ||||||
|             lines = [line.rstrip(': ') |         lines = self.logger.get_lines_for_level('error') | ||||||
|                      for line in self.logger.get_lines_for_level('error')] |  | ||||||
|         self.assertEqual( |         self.assertEqual( | ||||||
|                 ['ERROR while trying to clean up %s, path: %s, db: %s' % |             ['ERROR while trying to clean up %s, path: %s, db: %s: ' % | ||||||
|              (parent_dir, db_replicator.quote(broker.path), |              (parent_dir, db_replicator.quote(broker.path), | ||||||
|               broker.db_file)], |               broker.db_file)], | ||||||
|             lines) |             lines) | ||||||
|  |  | ||||||
|         finally: |  | ||||||
|             rmtree(temp_dir, ignore_errors=True) |  | ||||||
|  |  | ||||||
|     def test_rsync_then_merge_db_does_not_exist(self): |     def test_rsync_then_merge_db_does_not_exist(self): | ||||||
|         rpc = db_replicator.ReplicatorRpc('/', '/', FakeBroker, |         rpc = db_replicator.ReplicatorRpc('/', '/', FakeBroker, | ||||||
|                                           mount_check=False) |                                           mount_check=False) | ||||||
| @@ -1746,21 +1739,18 @@ class TestDBReplicator(unittest.TestCase): | |||||||
|         self.assertEqual(404, resp.status_int) |         self.assertEqual(404, resp.status_int) | ||||||
|  |  | ||||||
|     def test_complete_rsync(self): |     def test_complete_rsync(self): | ||||||
|         drive = mkdtemp() |         drive = self.temp_dir | ||||||
|         args = ['old_file'] |         args = ['old_file'] | ||||||
|         rpc = db_replicator.ReplicatorRpc('/', '/', FakeBroker, |         rpc = db_replicator.ReplicatorRpc('/', '/', FakeBroker, | ||||||
|                                           mount_check=False) |                                           mount_check=False) | ||||||
|         os.mkdir('%s/tmp' % drive) |         os.mkdir('%s/tmp' % drive) | ||||||
|         old_file = '%s/tmp/old_file' % drive |         old_file = '%s/tmp/old_file' % drive | ||||||
|         new_file = '%s/new_db_file' % drive |         new_file = '%s/new_db_file' % drive | ||||||
|         try: |  | ||||||
|         fp = open(old_file, 'w') |         fp = open(old_file, 'w') | ||||||
|         fp.write('void') |         fp.write('void') | ||||||
|         fp.close |         fp.close | ||||||
|         resp = rpc.complete_rsync(drive, new_file, args) |         resp = rpc.complete_rsync(drive, new_file, args) | ||||||
|         self.assertEqual(204, resp.status_int) |         self.assertEqual(204, resp.status_int) | ||||||
|         finally: |  | ||||||
|             rmtree(drive) |  | ||||||
|  |  | ||||||
|     @unit.with_tempdir |     @unit.with_tempdir | ||||||
|     def test_empty_suffix_and_hash_dirs_get_cleanedup(self, tempdir): |     def test_empty_suffix_and_hash_dirs_get_cleanedup(self, tempdir): | ||||||
|   | |||||||
| @@ -1334,7 +1334,7 @@ class TestReplicatorSync(test_db_replicator.TestReplicatorSync): | |||||||
|         expected = 'Failed to update sync_store, path: %s, db: %s: ' % ( |         expected = 'Failed to update sync_store, path: %s, db: %s: ' % ( | ||||||
|             quote(broker.path), broker.db_file |             quote(broker.path), broker.db_file | ||||||
|         ) |         ) | ||||||
|         self.assertEqual(lines, [expected]) |         self.assertEqual([expected], lines) | ||||||
|  |  | ||||||
|     def test_update_sync_store(self): |     def test_update_sync_store(self): | ||||||
|         klass = 'swift.container.sync_store.ContainerSyncStore' |         klass = 'swift.container.sync_store.ContainerSyncStore' | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Alistair Coles
					Alistair Coles