Replace stubs.Set with stub_out (os)

As part of the ongoing effort to stop using mox, start replacing
the stubs.Set calls with stub_out.

Limit the scope of this patch to the ~50 os stubs.

Part of bp:remove-mox

Change-Id: I192a84c0db46c6a73d2529c45f121854a659c5fc
This commit is contained in:
Diana Clarke 2016-01-14 22:35:42 -05:00
parent a53d0eef4a
commit 36037f3de4
7 changed files with 48 additions and 49 deletions

View File

@ -748,7 +748,7 @@ class LinuxNetworkTestCase(test.NoDBTestCase):
self.stubs.Set(linux_net, '_add_dhcp_mangle_rule',
fake_add_dhcp_mangle_rule)
self.stubs.Set(os, 'chmod', lambda *a, **kw: None)
self.stub_out('os.chmod', lambda *a, **kw: None)
self.stubs.Set(linux_net, 'write_to_file', lambda *a, **kw: None)
self.stubs.Set(linux_net, '_dnsmasq_pid_for', lambda *a, **kw: None)
dev = 'br100'

View File

@ -404,7 +404,7 @@ class CacheConcurrencyTestCase(test.NoDBTestCase):
def fake_extend(image, size, use_cow=False):
pass
self.stubs.Set(os.path, 'exists', fake_exists)
self.stub_out('os.path.exists', fake_exists)
self.stubs.Set(utils, 'execute', fake_execute)
self.stubs.Set(imagebackend.disk, 'extend', fake_extend)
self.useFixture(fixtures.MonkeyPatch(
@ -5589,7 +5589,7 @@ class LibvirtConnTestCase(test.NoDBTestCase):
flags &= ~os.O_DIRECT
return os_open(path, flags, *args, **kwargs)
self.stubs.Set(os, 'open', os_open_stub)
self.stub_out('os.open', os_open_stub)
@staticmethod
def connection_supports_direct_io_stub(dirpath):
@ -7482,7 +7482,7 @@ class LibvirtConnTestCase(test.NoDBTestCase):
fallback_from_host=None)
self.mox.ReplayAll()
self.stubs.Set(os.path, 'exists', lambda *args: False)
self.stub_out('os.path.exists', lambda *args: False)
drvr._create_images_and_backing(self.context, self.test_instance,
"/fake/instance/dir", [disk_info])
@ -12984,7 +12984,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
self.stubs.Set(self.drvr, '_is_storage_shared_with',
fake_is_storage_shared)
self.stubs.Set(utils, 'execute', fake_execute)
self.stubs.Set(os.path, 'exists', fake_os_path_exists)
self.stub_out('os.path.exists', fake_os_path_exists)
ins_ref = self._create_instance()
flavor = {'root_gb': 10, 'ephemeral_gb': 20}
@ -13668,7 +13668,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
def fake_os_path_exists(path):
return True
self.stubs.Set(os.path, 'exists', fake_os_path_exists)
self.stub_out('os.path.exists', fake_os_path_exists)
self.mox.StubOutWithMock(libvirt_utils, 'get_instance_path')
self.mox.StubOutWithMock(utils, 'execute')
@ -13699,7 +13699,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
def fake_unfilter_instance(instance, network_info):
pass
self.stubs.Set(os.path, 'exists', fake_os_path_exists)
self.stub_out('os.path.exists', fake_os_path_exists)
self.stubs.Set(self.drvr, '_undefine_domain',
fake_undefine_domain)
self.stubs.Set(self.drvr, 'unplug_vifs',

View File

@ -107,8 +107,8 @@ class _ImageTestCase(object):
return
self.stubs.Set(image, 'get_disk_size', lambda _: self.SIZE)
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stubs.Set(os, 'access', lambda p, w: True)
self.stub_out('os.path.exists', lambda _: True)
self.stub_out('os.access', lambda p, w: True)
# Call twice to verify testing fallocate is only called once.
image.cache(fake_fetch, self.TEMPLATE_PATH, self.SIZE)
@ -132,8 +132,8 @@ class _ImageTestCase(object):
self.stubs.Set(image, 'check_image_exists', lambda: True)
self.stubs.Set(image, '_can_fallocate', lambda: True)
self.stubs.Set(image, 'get_disk_size', lambda _: self.SIZE)
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stubs.Set(os, 'access', lambda p, w: False)
self.stub_out('os.path.exists', lambda _: True)
self.stub_out('os.access', lambda p, w: False)
# Testing fallocate is only called when user has write access.
image.cache(fake_fetch, self.TEMPLATE_PATH, self.SIZE)
@ -765,7 +765,7 @@ class LvmTestCase(_ImageTestCase, test.NoDBTestCase):
def fake_fetch(target, *args, **kwargs):
return
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(image, 'check_image_exists', lambda: True)
self.stubs.Set(image, 'get_disk_size', lambda _: self.SIZE)
@ -1138,7 +1138,7 @@ class EncryptedLvmTestCase(_ImageTestCase, test.NoDBTestCase):
def fake_fetch(target, *args, **kwargs):
return
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(image, 'check_image_exists', lambda: True)
self.stubs.Set(image, 'get_disk_size', lambda _: self.SIZE)
@ -1325,7 +1325,7 @@ class RbdTestCase(_ImageTestCase, test.NoDBTestCase):
def fake_fetch(target, *args, **kwargs):
return
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(image, 'check_image_exists', lambda: True)
self.stubs.Set(image, 'get_disk_size', lambda _: self.SIZE)
@ -1504,7 +1504,7 @@ class PloopTestCase(_ImageTestCase, test.NoDBTestCase):
def fake_fetch(target, *args, **kwargs):
return
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(image, 'check_image_exists', lambda: True)
self.stubs.Set(image, 'get_disk_size', lambda _: self.SIZE)

View File

@ -66,7 +66,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
'banana-42-hamster'])
def test_read_stored_checksum_missing(self):
self.stubs.Set(os.path, 'exists', lambda x: False)
self.stub_out('os.path.exists', lambda x: False)
csum = imagecache.read_stored_checksum('/tmp/foo', timestamped=False)
self.assertIsNone(csum)
@ -139,8 +139,8 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
'17d1b00b81642842e514494a78e804e9a511637c_10737418240']
listing.extend(images)
self.stubs.Set(os, 'listdir', lambda x: listing)
self.stubs.Set(os.path, 'isfile', lambda x: True)
self.stub_out('os.listdir', lambda x: listing)
self.stub_out('os.path.isfile', lambda x: True)
base_dir = '/var/lib/nova/instances/_base'
self.flags(instances_path='/var/lib/nova/instances')
@ -184,10 +184,10 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.assertIn('swap_1000', image_cache_manager.back_swap_images)
def test_list_backing_images_small(self):
self.stubs.Set(os, 'listdir',
self.stub_out('os.listdir',
lambda x: ['_base', 'instance-00000001',
'instance-00000002', 'instance-00000003'])
self.stubs.Set(os.path, 'exists',
self.stub_out('os.path.exists',
lambda x: x.find('instance-') != -1)
self.stubs.Set(libvirt_utils, 'get_disk_backing_file',
lambda x: 'e97222e91fc4241f49a7f520d1dcf446751129b3_sm')
@ -206,10 +206,10 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.assertEqual(len(image_cache_manager.unexplained_images), 0)
def test_list_backing_images_resized(self):
self.stubs.Set(os, 'listdir',
self.stub_out('os.listdir',
lambda x: ['_base', 'instance-00000001',
'instance-00000002', 'instance-00000003'])
self.stubs.Set(os.path, 'exists',
self.stub_out('os.path.exists',
lambda x: x.find('instance-') != -1)
self.stubs.Set(libvirt_utils, 'get_disk_backing_file',
lambda x: ('e97222e91fc4241f49a7f520d1dcf446751129b3_'
@ -230,9 +230,9 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.assertEqual(len(image_cache_manager.unexplained_images), 0)
def test_list_backing_images_instancename(self):
self.stubs.Set(os, 'listdir',
self.stub_out('os.listdir',
lambda x: ['_base', 'banana-42-hamster'])
self.stubs.Set(os.path, 'exists',
self.stub_out('os.path.exists',
lambda x: x.find('banana-42-hamster') != -1)
self.stubs.Set(libvirt_utils, 'get_disk_backing_file',
lambda x: 'e97222e91fc4241f49a7f520d1dcf446751129b3_sm')
@ -251,9 +251,9 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.assertEqual(len(image_cache_manager.unexplained_images), 0)
def test_list_backing_images_disk_notexist(self):
self.stubs.Set(os, 'listdir',
self.stub_out('os.listdir',
lambda x: ['_base', 'banana-42-hamster'])
self.stubs.Set(os.path, 'exists',
self.stub_out('os.path.exists',
lambda x: x.find('banana-42-hamster') != -1)
def fake_get_disk(disk_path):
@ -269,7 +269,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
image_cache_manager._list_backing_images)
def test_find_base_file_nothing(self):
self.stubs.Set(os.path, 'exists', lambda x: False)
self.stub_out('os.path.exists', lambda x: False)
base_dir = '/var/lib/nova/instances/_base'
fingerprint = '549867354867'
@ -280,7 +280,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
def test_find_base_file_small(self):
fingerprint = '968dd6cc49e01aaa044ed11c0cce733e0fa44a6a'
self.stubs.Set(os.path, 'exists',
self.stub_out('os.path.exists',
lambda x: x.endswith('%s_sm' % fingerprint))
base_dir = '/var/lib/nova/instances/_base'
@ -297,10 +297,10 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
'968dd6cc49e01aaa044ed11c0cce733e0fa44a6a_10737418240',
'00000004']
self.stubs.Set(os, 'listdir', lambda x: listing)
self.stubs.Set(os.path, 'exists',
self.stub_out('os.listdir', lambda x: listing)
self.stub_out('os.path.exists',
lambda x: x.endswith('%s_10737418240' % fingerprint))
self.stubs.Set(os.path, 'isfile', lambda x: True)
self.stub_out('os.path.isfile', lambda x: True)
base_dir = '/var/lib/nova/instances/_base'
image_cache_manager = imagecache.ImageCacheManager()
@ -318,9 +318,9 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
'968dd6cc49e01aaa044ed11c0cce733e0fa44a6a_10737418240',
'00000004']
self.stubs.Set(os, 'listdir', lambda x: listing)
self.stubs.Set(os.path, 'exists', lambda x: True)
self.stubs.Set(os.path, 'isfile', lambda x: True)
self.stub_out('os.listdir', lambda x: listing)
self.stub_out('os.path.exists', lambda x: True)
self.stub_out('os.path.isfile', lambda x: True)
base_dir = '/var/lib/nova/instances/_base'
image_cache_manager = imagecache.ImageCacheManager()
@ -606,12 +606,12 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.fail('Unexpected path existence check: %s' % path)
self.stubs.Set(os.path, 'exists', lambda x: exists(x))
self.stub_out('os.path.exists', lambda x: exists(x))
self.stubs.Set(libvirt_utils, 'chown', lambda x, y: None)
# We need to stub utime as well
self.stubs.Set(os, 'utime', lambda x, y: None)
self.stub_out('os.utime', lambda x, y: None)
# Fake up some instances in the instances directory
orig_listdir = os.listdir
@ -629,7 +629,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.fail('Unexpected directory listed: %s' % path)
self.stubs.Set(os, 'listdir', lambda x: listdir(x))
self.stub_out('os.listdir', lambda x: listdir(x))
# Fake isfile for these faked images in _base
orig_isfile = os.path.isfile
@ -645,7 +645,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
self.fail('Unexpected isfile call: %s' % path)
self.stubs.Set(os.path, 'isfile', lambda x: isfile(x))
self.stub_out('os.path.isfile', lambda x: isfile(x))
# Fake the database call which lists running instances
instances = [{'image_ref': '1',
@ -689,7 +689,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
return 1000000
self.stubs.Set(os.path, 'getmtime', lambda x: getmtime(x))
self.stub_out('os.path.getmtime', lambda x: getmtime(x))
# Make sure we don't accidentally remove a real file
orig_remove = os.remove
@ -701,7 +701,7 @@ class ImageCacheManagerTestCase(test.NoDBTestCase):
# Don't try to remove fake files
return
self.stubs.Set(os, 'remove', lambda x: remove(x))
self.stub_out('os.remove', lambda x: remove(x))
self.mox.StubOutWithMock(objects.block_device.BlockDeviceMappingList,
'get_by_instance_uuid')

View File

@ -527,7 +527,7 @@ disk size: 4.4M
self.path = path
return FakeStatResult()
self.stubs.Set(os, 'statvfs', fake_statvfs)
self.stub_out('os.statvfs', fake_statvfs)
fs_info = libvirt_utils.get_fs_info('/some/file/path')
self.assertEqual('/some/file/path', self.path)
@ -606,8 +606,8 @@ disk size: 4.4M
return FakeImgInfo()
self.stubs.Set(utils, 'execute', fake_execute)
self.stubs.Set(os, 'rename', fake_rename)
self.stubs.Set(os, 'unlink', fake_unlink)
self.stub_out('os.rename', fake_rename)
self.stub_out('os.unlink', fake_unlink)
self.stubs.Set(images, 'fetch', lambda *_, **__: None)
self.stubs.Set(images, 'qemu_img_info', fake_qemu_img_info)
self.stubs.Set(fileutils, 'delete_if_exists', fake_rm_on_error)
@ -677,7 +677,7 @@ disk size: 4.4M
return True
self.stubs.Set(utils, 'execute', fake_execute)
self.stubs.Set(os.path, 'exists', return_true)
self.stub_out('os.path.exists', return_true)
out = libvirt_utils.get_disk_backing_file('')
self.assertEqual(out, 'baz')

View File

@ -52,7 +52,7 @@ class LibvirtScalityVolumeDriverTestCase(
else:
return os.access(path, flags)
self.stubs.Set(os, 'access', _access_wrapper)
self.stub_out('os.access', _access_wrapper)
with mock.patch.object(self.drv, '_mount_sofs'):
self.drv.connect_volume(TEST_CONN_INFO, self.disk_info)

View File

@ -14,7 +14,6 @@
# under the License.
import io
import os
import mock
import six
@ -203,7 +202,7 @@ class TestVirtDisk(test.NoDBTestCase):
def fake_instance_for_format(image, mountdir, partition):
return FakeMount(image, mountdir, partition)
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(disk_api._DiskImage, '_device_for_path', proc_mounts)
self.stubs.Set(mount.Mount, 'instance_for_format',
staticmethod(fake_instance_for_format))
@ -222,7 +221,7 @@ class TestVirtDisk(test.NoDBTestCase):
}
return mount_points[mount_point]
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(disk_api._DiskImage, '_device_for_path', proc_mounts)
expected_commands = []
@ -266,7 +265,7 @@ class TestVirtDisk(test.NoDBTestCase):
def proc_mounts(self, mount_point):
return None
self.stubs.Set(os.path, 'exists', lambda _: True)
self.stub_out('os.path.exists', lambda _: True)
self.stubs.Set(disk_api._DiskImage, '_device_for_path', proc_mounts)
expected_commands = []