]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Cleanly override config in tests
authorAdrien Vergé <adrien.verge@numergy.com>
Tue, 28 Oct 2014 21:00:09 +0000 (22:00 +0100)
committerAdrien Vergé <adrien.verge@numergy.com>
Tue, 28 Oct 2014 21:35:42 +0000 (22:35 +0100)
CONF.set_override() is often called in tests but CONF.clear_override()
is never. Create a override_config() method in the base TestCase class
that restores previous conf value after test, as it is done in other
OpenStack projects.

Change-Id: Id57b4502a2a37ea12047985cf2ea6c38c6f965fd

cinder/test.py
cinder/tests/api/contrib/test_volume_actions.py
cinder/tests/test_backup.py
cinder/tests/test_backup_swift.py
cinder/tests/test_glusterfs.py
cinder/tests/test_image_utils.py
cinder/tests/test_nfs.py
cinder/tests/test_service.py
cinder/tests/test_volume.py

index 5f36942bcf18ae46ad3c3ec2d5ea0af0569a690e..46abeb90cf1c237d6a2d316c5519872157efc78c 100644 (file)
@@ -185,18 +185,18 @@ class TestCase(testtools.TestCase):
 
         fake_notifier.stub_notifier(self.stubs)
 
-        CONF.set_override('fatal_exception_format_errors', True)
+        self.override_config('fatal_exception_format_errors', True)
         # This will be cleaned up by the NestedTempfile fixture
-        CONF.set_override('lock_path', tempfile.mkdtemp())
-        CONF.set_override('policy_file',
-                          os.path.join(
-                              os.path.abspath(
-                                  os.path.join(
-                                      os.path.dirname(__file__),
-                                      '..',
-                                  )
-                              ),
-                              'cinder/tests/policy.json'))
+        self.override_config('lock_path', tempfile.mkdtemp())
+        self.override_config('policy_file',
+                             os.path.join(
+                                 os.path.abspath(
+                                     os.path.join(
+                                         os.path.dirname(__file__),
+                                         '..',
+                                     )
+                                 ),
+                                 'cinder/tests/policy.json'))
 
     def _common_cleanup(self):
         """Runs after each test method to tear down test environment."""
@@ -221,10 +221,15 @@ class TestCase(testtools.TestCase):
         for key in [k for k in self.__dict__.keys() if k[0] != '_']:
             del self.__dict__[key]
 
+    def override_config(self, name, override, group=None):
+        """Cleanly override CONF variables."""
+        CONF.set_override(name, override, group)
+        self.addCleanup(CONF.clear_override, name, group)
+
     def flags(self, **kw):
         """Override CONF variables for a test."""
         for k, v in kw.iteritems():
-            CONF.set_override(k, v)
+            self.override_config(k, v)
 
     def log_level(self, level):
         """Set logging level to the specified value."""
index c8c1feb3ed00aa9c3d46223e9a9629472c6b25d2..65c51c77f0befa507fafa4e6ad3e34e22b1a26da 100644 (file)
@@ -835,7 +835,7 @@ class VolumeImageActionsTest(test.TestCase):
                     mock_copy_volume_to_image.side_effect = \
                         self.fake_rpc_copy_volume_to_image
 
-                    CONF.set_override('glance_core_properties', [])
+                    self.override_config('glance_core_properties', [])
 
                     req = fakes.HTTPRequest.blank(
                         '/v2/tenant1/volumes/%s/action' % id)
index 2ebbb1099ada0601a415f37069b7ee65b0761273..02b948956483fd93842bc15a869795845e0fec3c 100644 (file)
@@ -422,7 +422,7 @@ class BackupTestCase(BaseBackupTest):
 
     def test_backup_manager_driver_name(self):
         """"Test mapping between backup services and backup drivers."""
-        cfg.CONF.set_override('backup_driver', "cinder.backup.services.swift")
+        self.override_config('backup_driver', "cinder.backup.services.swift")
         backup_mgr = \
             importutils.import_object(CONF.backup_manager)
         self.assertEqual('cinder.backup.drivers.swift',
@@ -546,8 +546,8 @@ class BackupTestCaseWithVerify(BaseBackupTest):
     """Test Case for backups."""
 
     def setUp(self):
-        CONF.set_override("backup_driver",
-                          "cinder.tests.backup.fake_service_with_verify")
+        self.override_config("backup_driver",
+                             "cinder.tests.backup.fake_service_with_verify")
         super(BackupTestCaseWithVerify, self).setUp()
 
     def test_import_record_with_verify(self):
index 87d1e018cd8b941d8ac0d76e6b54d0eb7b55c145..34c830f6a99c31d77412504f784c292f422aeffc 100644 (file)
@@ -100,14 +100,14 @@ class BackupSwiftTestCase(test.TestCase):
                                           u'adminURL': u'http://example.com'}]
                                       }]
         self.ctxt.project_id = "12345678"
-        CONF.set_override("backup_swift_url", "http://public.example.com/")
+        self.override_config("backup_swift_url", "http://public.example.com/")
         backup = SwiftBackupDriver(self.ctxt)
         self.assertEqual("%s%s" % (CONF.backup_swift_url,
                                    self.ctxt.project_id),
                          backup.swift_url)
 
     def test_backup_swift_info(self):
-        CONF.set_override("swift_catalog_info", "dummy")
+        self.override_config("swift_catalog_info", "dummy")
         self.assertRaises(exception.BackupDriverException,
                           SwiftBackupDriver,
                           self.ctxt)
index 0f93e3f8d2d88ae5ba878ed4000a0ca5b379a317..a25c91b237ff05111b7e589d2814ba83cd08028c 100644 (file)
@@ -152,8 +152,8 @@ class GlusterFsDriverTestCase(test.TestCase):
 
     def test_local_path(self):
         """local_path common use case."""
-        CONF.set_override("glusterfs_mount_point_base",
-                          self.TEST_MNT_POINT_BASE)
+        self.override_config("glusterfs_mount_point_base",
+                             self.TEST_MNT_POINT_BASE)
         drv = self._driver
 
         volume = DumbVolume()
@@ -259,8 +259,8 @@ class GlusterFsDriverTestCase(test.TestCase):
         mox.StubOutWithMock(brick.remotefs.remotefs.RemoteFsClient,
                             'get_mount_point')
 
-        CONF.set_override("glusterfs_mount_point_base",
-                          self.TEST_MNT_POINT_BASE)
+        self.override_config("glusterfs_mount_point_base",
+                             self.TEST_MNT_POINT_BASE)
 
         brick.remotefs.remotefs.RemoteFsClient.\
             get_mount_point(self.TEST_EXPORT1).AndReturn(hashed_path)
@@ -422,8 +422,8 @@ class GlusterFsDriverTestCase(test.TestCase):
         mox = self._mox
         drv = self._driver
 
-        CONF.set_override("glusterfs_shares_config",
-                          self.TEST_SHARES_CONFIG_FILE)
+        self.override_config("glusterfs_shares_config",
+                             self.TEST_SHARES_CONFIG_FILE)
 
         mox.StubOutWithMock(os.path, 'exists')
         os.path.exists(self.TEST_SHARES_CONFIG_FILE).AndReturn(True)
@@ -450,8 +450,8 @@ class GlusterFsDriverTestCase(test.TestCase):
         mox = self._mox
         drv = self._driver
 
-        CONF.set_override("glusterfs_shares_config",
-                          self.TEST_SHARES_CONFIG_FILE)
+        self.override_config("glusterfs_shares_config",
+                             self.TEST_SHARES_CONFIG_FILE)
 
         self.stubs.Set(drv, '_load_shares_config',
                        self._fake_load_shares_config)
@@ -561,7 +561,7 @@ class GlusterFsDriverTestCase(test.TestCase):
         drv = self._driver
         volume = self._simple_volume()
 
-        CONF.set_override('glusterfs_sparsed_volumes', True)
+        self.override_config('glusterfs_sparsed_volumes', True)
 
         mox.StubOutWithMock(drv, '_create_sparsed_file')
         mox.StubOutWithMock(drv, '_set_rw_permissions_for_all')
@@ -1475,8 +1475,8 @@ class GlusterFsDriverTestCase(test.TestCase):
     def test_get_backing_chain_for_path(self):
         (mox, drv) = self._mox, self._driver
 
-        CONF.set_override('glusterfs_mount_point_base',
-                          self.TEST_MNT_POINT_BASE)
+        self.override_config('glusterfs_mount_point_base',
+                             self.TEST_MNT_POINT_BASE)
 
         volume = self._simple_volume()
         vol_filename = volume['name']
index 393bc6e49f9633eda1778c02a3913982e5dbdc1f..4d54559aebb4b0076f94a45dbb3390d02c2919f5 100644 (file)
@@ -222,7 +222,7 @@ class TestUtils(test.TestCase):
                      "disk size: 0")
 
         utils.is_blk_device(self.TEST_DEV_PATH).AndReturn(True)
-        CONF.set_override('volume_copy_bps_limit', bps_limit)
+        self.override_config('volume_copy_bps_limit', bps_limit)
 
         image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)
 
@@ -449,7 +449,7 @@ class TestUtils(test.TestCase):
                    "disk_size: 196K (200704 bytes)"
 
         if bps_limit:
-            CONF.set_override('volume_copy_bps_limit', bps_limit)
+            self.override_config('volume_copy_bps_limit', bps_limit)
             prefix = ('cgexec', '-g', 'blkio:test')
         else:
             prefix = ()
@@ -486,7 +486,7 @@ class TestUtils(test.TestCase):
                    "cluster_size: 65536\n"\
                    "disk_size: 196K (200704 bytes)"
 
-        CONF.set_override('volume_copy_bps_limit', bps_limit)
+        self.override_config('volume_copy_bps_limit', bps_limit)
         prefix = ('cgexec', '-g', 'blkio:test')
 
         cmd = prefix + ('qemu-img', 'convert', '-O', 'qcow2',
index aa3d070d611eb7deb4761c26c11ac3415134fea1..9031e3ea7d46aee7db5e4277811107dc05e9b0df 100644 (file)
@@ -22,7 +22,6 @@ import mox as mox_lib
 from mox import IgnoreArg
 from mox import IsA
 from mox import stubout
-from oslo.config import cfg
 
 from cinder import context
 from cinder import exception
@@ -702,7 +701,7 @@ class NfsDriverTestCase(test.TestCase):
         drv = self._driver
         volume = self._simple_volume()
 
-        cfg.CONF.set_override('nfs_sparsed_volumes', True)
+        self.override_config('nfs_sparsed_volumes', True)
 
         mox.StubOutWithMock(drv, '_create_sparsed_file')
         mox.StubOutWithMock(drv, '_set_rw_permissions')
@@ -722,7 +721,7 @@ class NfsDriverTestCase(test.TestCase):
         self.configuration.nfs_sparsed_volumes = False
         volume = self._simple_volume()
 
-        cfg.CONF.set_override('nfs_sparsed_volumes', False)
+        self.override_config('nfs_sparsed_volumes', False)
 
         mox.StubOutWithMock(drv, '_create_regular_file')
         mox.StubOutWithMock(drv, '_set_rw_permissions')
index 1b8109012db804fa038a4e8bcf36689b7a59a44c..e7d1560df1bdb7dad5095e4e046718c24f90d209 100644 (file)
@@ -198,8 +198,8 @@ class ServiceTestCase(test.TestCase):
         self.assertFalse(serv.model_disconnected)
 
     def test_service_with_long_report_interval(self):
-        CONF.set_override('service_down_time', 10)
-        CONF.set_override('report_interval', 10)
+        self.override_config('service_down_time', 10)
+        self.override_config('report_interval', 10)
         service.Service.create(binary="test_service",
                                manager="cinder.tests.test_service.FakeManager")
         self.assertEqual(CONF.service_down_time, 25)
@@ -237,18 +237,18 @@ class TestWSGIService(test.TestCase):
         self.assertEqual(test_service.workers, processutils.get_worker_count())
 
     def test_workers_set_good_user_setting(self):
-        CONF.set_override('osapi_volume_workers', 8)
+        self.override_config('osapi_volume_workers', 8)
         test_service = service.WSGIService("osapi_volume")
         self.assertEqual(test_service.workers, 8)
 
     def test_workers_set_zero_user_setting(self):
-        CONF.set_override('osapi_volume_workers', 0)
+        self.override_config('osapi_volume_workers', 0)
         test_service = service.WSGIService("osapi_volume")
         # If a value less than 1 is used, defaults to number of procs available
         self.assertEqual(test_service.workers, processutils.get_worker_count())
 
     def test_workers_set_negative_user_setting(self):
-        CONF.set_override('osapi_volume_workers', -1)
+        self.override_config('osapi_volume_workers', -1)
         self.assertRaises(exception.InvalidInput,
                           service.WSGIService,
                           "osapi_volume")
index 70fa10169b05c5e8ba5d973d4b09744ef92b974d..4eaf8d47c1456d45566e32a140bdc5e3b4054591 100644 (file)
@@ -471,14 +471,14 @@ class VolumeTestCase(BaseVolumeTestCase):
                        fake_list_availability_zones)
 
         # Test backwards compatibility, default_availability_zone not set
-        CONF.set_override('storage_availability_zone', 'az2')
+        self.override_config('storage_availability_zone', 'az2')
         volume = volume_api.create(self.context,
                                    1,
                                    'name',
                                    'description')
         self.assertEqual(volume['availability_zone'], 'az2')
 
-        CONF.set_override('default_availability_zone', 'default-az')
+        self.override_config('default_availability_zone', 'default-az')
         volume = volume_api.create(self.context,
                                    1,
                                    'name',