From 04d38feb65b06d33224d356f6db1b9202532e2ed Mon Sep 17 00:00:00 2001 From: Mark Washenberger Date: Mon, 13 Feb 2012 16:04:59 -0500 Subject: Use stubs in libvirt/utils get_fs_info test This test frequently breaks for me because of a race condition where some small change will happen to the file system between when df is called and when os.statvfs is called. By refactoring the test to stubs, we can eliminate the race condition. Note that I do not modify the code under test at all in this change, so the risk of introducing a defect is zero. Change-Id: I83e4b38b25fef7e5e27fcf81b5e5a566406b05dd --- nova/tests/test_libvirt.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'nova') diff --git a/nova/tests/test_libvirt.py b/nova/tests/test_libvirt.py index 2d50344d6..1f9dc3eb8 100644 --- a/nova/tests/test_libvirt.py +++ b/nova/tests/test_libvirt.py @@ -1920,17 +1920,34 @@ disk size: 4.4M''', '')) os.unlink(dst_path) def test_get_fs_info(self): - # Use a 1024-byte block size (df -k) because OS X does not support - # the -B flag - blocksize = 1024 - stdout, stderr = utils.execute('df', '-k', '/tmp') - info_line = ' '.join(stdout.split('\n')[1:]) - _dev, total, used, free, _percentage, _mntpnt = info_line.split() - - fs_info = libvirt_utils.get_fs_info('/tmp') - self.assertEquals(int(total) * blocksize, fs_info['total']) - self.assertEquals(int(free) * blocksize, fs_info['free']) - self.assertEquals(int(used) * blocksize, fs_info['used']) + + class FakeStatResult(object): + + def __init__(self): + self.f_bsize = 4096 + self.f_frsize = 4096 + self.f_blocks = 2000 + self.f_bfree = 1000 + self.f_bavail = 900 + self.f_files = 2000 + self.f_ffree = 1000 + self.f_favail = 900 + self.f_flag = 4096 + self.f_namemax = 255 + + self.path = None + + def fake_statvfs(path): + self.path = path + return FakeStatResult() + + self.stubs.Set(os, 'statvfs', fake_statvfs) + + fs_info = libvirt_utils.get_fs_info('/some/file/path') + self.assertEquals('/some/file/path', self.path) + self.assertEquals(8192000, fs_info['total']) + self.assertEquals(3686400, fs_info['free']) + self.assertEquals(4096000, fs_info['used']) def test_fetch_image(self): self.mox.StubOutWithMock(images, 'fetch') -- cgit