From 95588918ce3fd8460b7d5acfe0352e83ddef0e5d Mon Sep 17 00:00:00 2001 From: Julia Varlamova Date: Tue, 18 Feb 2014 15:17:44 +0400 Subject: [PATCH] Replace tearDown with addCleanup - Part 3 Replace tearDown with addCleanup in some cinder unit tests. Infra team has indicated that tearDown should not be used and should be replaced with addCleanup in all places. Implements blueprint replace-teardown-with-addcleanup Change-Id: I8469ffb5c896d40f9979cd6d1f8da81be763ec14 --- cinder/tests/api/v2/test_volumes.py | 6 +----- cinder/tests/test_backup_swift.py | 5 +---- cinder/tests/test_create_volume_flow.py | 4 ---- cinder/tests/test_emc_smis.py | 12 ++---------- cinder/tests/test_hds.py | 7 ++----- cinder/tests/test_iscsi.py | 17 ++++++++--------- cinder/tests/test_volume_utils.py | 5 +---- 7 files changed, 15 insertions(+), 41 deletions(-) diff --git a/cinder/tests/api/v2/test_volumes.py b/cinder/tests/api/v2/test_volumes.py index 7263eedcc..42c1acca2 100644 --- a/cinder/tests/api/v2/test_volumes.py +++ b/cinder/tests/api/v2/test_volumes.py @@ -59,6 +59,7 @@ def stub_snapshot_get(self, context, snapshot_id): class VolumeApiTest(test.TestCase): def setUp(self): super(VolumeApiTest, self).setUp() + self.addCleanup(fake_notifier.reset) self.ext_mgr = extensions.ExtensionManager() self.ext_mgr.extensions = {} fake_image.stub_out_image_service(self.stubs) @@ -66,17 +67,12 @@ class VolumeApiTest(test.TestCase): self.flags(host='fake', notification_driver=[fake_notifier.__name__]) - self.stubs.Set(db, 'volume_get_all', stubs.stub_volume_get_all) self.stubs.Set(volume_api.API, 'delete', stubs.stub_volume_delete) self.stubs.Set(db, 'service_get_all_by_topic', stubs.stub_service_get_all_by_topic) self.maxDiff = None - def tearDown(self): - super(VolumeApiTest, self).tearDown() - fake_notifier.reset() - def test_volume_create(self): self.stubs.Set(volume_api.API, 'get', stubs.stub_volume_get) self.stubs.Set(volume_api.API, "create", stubs.stub_volume_create) diff --git a/cinder/tests/test_backup_swift.py b/cinder/tests/test_backup_swift.py index 202d175e5..b94ffa6cd 100644 --- a/cinder/tests/test_backup_swift.py +++ b/cinder/tests/test_backup_swift.py @@ -71,13 +71,10 @@ class BackupSwiftTestCase(test.TestCase): self._create_volume_db_entry() self.volume_file = tempfile.NamedTemporaryFile() + self.addCleanup(self.volume_file.close) for i in xrange(0, 128): self.volume_file.write(os.urandom(1024)) - def tearDown(self): - self.volume_file.close() - super(BackupSwiftTestCase, self).tearDown() - def test_backup_uncompressed(self): self._create_backup_db_entry() self.flags(backup_compression_algorithm='none') diff --git a/cinder/tests/test_create_volume_flow.py b/cinder/tests/test_create_volume_flow.py index 13a3b8691..a4e4babd7 100644 --- a/cinder/tests/test_create_volume_flow.py +++ b/cinder/tests/test_create_volume_flow.py @@ -104,7 +104,3 @@ class CreateVolumeFlowTestCase(test.TestCase): fake_db()) task._cast_create_volume(self.ctxt, spec, props) - - def tearDown(self): - self.stubs.UnsetAll() - super(CreateVolumeFlowTestCase, self).tearDown() diff --git a/cinder/tests/test_emc_smis.py b/cinder/tests/test_emc_smis.py index 8946e6205..b3ab58a99 100644 --- a/cinder/tests/test_emc_smis.py +++ b/cinder/tests/test_emc_smis.py @@ -899,9 +899,11 @@ class EMCSMISISCSIDriverTestCase(test.TestCase): self.data = EMCSMISCommonData() self.tempdir = tempfile.mkdtemp() + self.addCleanup(shutil.rmtree, self.tempdir) super(EMCSMISISCSIDriverTestCase, self).setUp() self.config_file_path = None self.create_fake_config_file() + self.addCleanup(os.remove, self.config_file_path) configuration = mock.Mock() configuration.cinder_emc_config_file = self.config_file_path @@ -1089,16 +1091,6 @@ class EMCSMISISCSIDriverTestCase(test.TestCase): self.data.failed_extend_vol, '10') - def _cleanup(self): - bExists = os.path.exists(self.config_file_path) - if bExists: - os.remove(self.config_file_path) - shutil.rmtree(self.tempdir) - - def tearDown(self): - self._cleanup() - super(EMCSMISISCSIDriverTestCase, self).tearDown() - class EMCSMISFCDriverTestCase(test.TestCase): diff --git a/cinder/tests/test_hds.py b/cinder/tests/test_hds.py index 290da4b21..e2cd2d640 100644 --- a/cinder/tests/test_hds.py +++ b/cinder/tests/test_hds.py @@ -167,6 +167,7 @@ class HUSiSCSIDriverTest(test.TestCase): def setUp(self): super(HUSiSCSIDriverTest, self).setUp() (handle, self.config_file) = tempfile.mkstemp('.xml') + self.addCleanup(os.remove, self.config_file) os.write(handle, CONF) os.close(handle) SimulatedHusBackend.alloc_lun = [] @@ -179,11 +180,7 @@ class HUSiSCSIDriverTest(test.TestCase): self.configuration = mox.MockObject(conf.Configuration) self.configuration.hds_cinder_config_file = self.config_file self.driver = hds.HUSDriver(configuration=self.configuration) - - def tearDown(self): - os.remove(self.config_file) - self.mox.UnsetStubs() - super(HUSiSCSIDriverTest, self).tearDown() + self.addCleanup(self.mox.UnsetStubs) def test_get_volume_stats(self): stats = self.driver.get_volume_stats(True) diff --git a/cinder/tests/test_iscsi.py b/cinder/tests/test_iscsi.py index d24989be2..169738851 100644 --- a/cinder/tests/test_iscsi.py +++ b/cinder/tests/test_iscsi.py @@ -49,6 +49,8 @@ class TargetAdminTestCase(object): self.fake_verify_backing_lun) self.driver = driver.ISCSIDriver() self.flags(iscsi_target_prefix='iqn.2011-09.org.foo.bar:') + self.persist_tempdir = tempfile.mkdtemp() + self.addCleanup(self._cleanup, self.persist_tempdir) def fake_verify_backing_lun(obj, iqn, tid): return True @@ -103,13 +105,18 @@ class TargetAdminTestCase(object): self.run_commands() self.verify() + def _cleanup(self, persist_tempdir): + try: + shutil.rmtree(persist_tempdir) + except OSError: + pass + class TgtAdmTestCase(test.TestCase, TargetAdminTestCase): def setUp(self): super(TgtAdmTestCase, self).setUp() TargetAdminTestCase.setUp(self) - self.persist_tempdir = tempfile.mkdtemp() self.flags(iscsi_helper='tgtadm') self.flags(volumes_dir=self.persist_tempdir) self.script_template = "\n".join([ @@ -119,13 +126,6 @@ class TgtAdmTestCase(test.TestCase, TargetAdminTestCase): '--delete %(target_name)s', 'tgtadm --lld iscsi --op show --mode target']) - def tearDown(self): - try: - shutil.rmtree(self.persist_tempdir) - except OSError: - pass - super(TgtAdmTestCase, self).tearDown() - class IetAdmTestCase(test.TestCase, TargetAdminTestCase): @@ -196,7 +196,6 @@ class LioAdmTestCase(test.TestCase, TargetAdminTestCase): def setUp(self): super(LioAdmTestCase, self).setUp() TargetAdminTestCase.setUp(self) - self.persist_tempdir = tempfile.mkdtemp() self.flags(iscsi_helper='lioadm') self.script_template = "\n".join([ 'cinder-rtstool create ' diff --git a/cinder/tests/test_volume_utils.py b/cinder/tests/test_volume_utils.py index 5d0d68bc5..af5c59002 100644 --- a/cinder/tests/test_volume_utils.py +++ b/cinder/tests/test_volume_utils.py @@ -48,6 +48,7 @@ class UsageInfoTestCase(test.TestCase): def setUp(self): super(UsageInfoTestCase, self).setUp() + self.addCleanup(fake_notifier.reset) self.flags(host='fake', notification_driver=["test"]) self.volume = importutils.import_object(CONF.volume_manager) self.user_id = 'fake' @@ -56,10 +57,6 @@ class UsageInfoTestCase(test.TestCase): self.volume_size = 0 self.context = context.RequestContext(self.user_id, self.project_id) - def tearDown(self): - super(UsageInfoTestCase, self).tearDown() - fake_notifier.reset() - def _create_volume(self, params={}): """Create a test volume.""" vol = {} -- 2.45.2