Esempio n. 1
0
 def test_disk_usage(self):
     """Mock test for disk usage."""
     os.statvfs.return_value = namedtuple(
         'statvfs', 'f_blocks f_bavail, f_frsize')(100, 20, 4)
     du = sysinfo.disk_usage('/var/tmp')
     os.statvfs.assert_called_with('/var/tmp')
     self.assertEqual(400, du.total)
     self.assertEqual(80, du.free)
def create_image(img_name, img_location, img_size):
    """Create a sparse file of the appropriate size.
    """
    fs.mkdir_safe(img_location)
    filename = os.path.join(img_location, img_name)

    retries = 10
    while retries > 0:
        retries -= 1
        try:
            stats = os.stat(filename)
            os.unlink(filename)
            _LOGGER.info('Disk image found and unlinked: %r; stat: %r',
                         filename, stats)
        except OSError as err:
            if err.errno == errno.ENOENT:
                pass
            else:
                raise

        available_size = sysinfo.disk_usage(img_location)
        img_size_bytes = utils.size_to_bytes(img_size)
        if img_size_bytes <= 0:
            real_img_size = available_size.free - abs(img_size_bytes)
        else:
            real_img_size = img_size_bytes

        if (real_img_size < TREADMILL_MIN_VG_SIZE or available_size.free <
                real_img_size + TREADMILL_MIN_RESERVE_SIZE):
            raise exc.NodeSetupError('Not enough free disk space')

        if fs.create_excl(filename, real_img_size):
            break

    if retries == 0:
        raise exc.NodeSetupError('Something is messing with '
                                 'disk image creation')