def test__init_block_dev(self):
        """Create a backing block device for LVM's Treadmill volume group.
        """
        # pylint: disable=W0212

        localdiskutils.loop_dev_for.side_effect = [
            subproc.CalledProcessError(returncode=1, cmd='losetup'),
            '/dev/test'
        ]

        res = localdiskutils.init_block_dev('treadmill.img', '/bar', '2G')

        self.assertTrue(localdiskutils.init_loopback_devices.called)
        localdiskutils.create_image.assert_called_with(
            localdiskutils.TREADMILL_IMG, '/bar', '2G')
        self.assertEqual(res, '/dev/test')
Esempio n. 2
0
    def localdisk(img_location, img_size, block_dev, vg_name,
                  block_dev_configuration, block_dev_read_bps,
                  block_dev_write_bps, block_dev_read_iops,
                  block_dev_write_iops, default_read_bps, default_write_bps,
                  default_read_iops, default_write_iops):
        """Runs localdisk service."""

        root_dir = local_ctx['root-dir']
        watchdogs_dir = local_ctx['watchdogs-dir']

        svc = services.ResourceService(service_dir=os.path.join(
            root_dir, 'localdisk_svc'),
                                       impl='localdisk')

        block_dev_params = [
            block_dev_read_bps, block_dev_write_bps, block_dev_read_iops,
            block_dev_write_iops
        ]
        if img_location is None:
            img_location = root_dir

        # prepare block device
        if block_dev is not None:
            underlying_device_uuid = fs_linux.blk_uuid(block_dev)
        else:
            underlying_device_uuid = fs_linux.blk_uuid(
                fs_linux.maj_min_to_blk(
                    *fs_linux.maj_min_from_path(img_location)))
            block_dev = localdiskutils.init_block_dev(
                localdiskutils.TREADMILL_IMG, img_location, img_size)

        # prepare block device configuration
        read_bps = None
        write_bps = None
        read_iops = None
        write_iops = None

        # use block device config file
        if block_dev_configuration is not None and all(
                param is None for param in block_dev_params):
            try:
                current_benchmark = diskbenchmark.read(
                    block_dev_configuration)[underlying_device_uuid]
                read_bps = current_benchmark['read_bps']
                write_bps = current_benchmark['write_bps']
                read_iops = int(current_benchmark['read_iops'])
                write_iops = int(current_benchmark['write_iops'])
            except IOError:
                _LOGGER.error('No benchmark found : %s',
                              block_dev_configuration)
            except (KeyError, ValueError):
                _LOGGER.error('Incorrect disk benchmark for device %s in %s',
                              underlying_device_uuid, block_dev_configuration)

        # use block device config parameters
        if all(param is not None for param in
               block_dev_params) and block_dev_configuration is None:
            read_bps = block_dev_read_bps
            write_bps = block_dev_write_bps
            read_iops = block_dev_read_iops
            write_iops = block_dev_write_iops

        if None in [read_bps, write_bps, read_iops, write_iops]:
            _LOGGER.error('Bad block dev configuration')
            read_bps = '200M'
            write_bps = '200M'
            read_iops = 3000
            write_iops = 3000

        svc.run(
            watchdogs_dir=os.path.join(root_dir, watchdogs_dir),
            block_dev=block_dev,
            vg_name=vg_name,
            read_bps=read_bps,
            write_bps=write_bps,
            read_iops=read_iops,
            write_iops=write_iops,
            default_read_bps=default_read_bps,
            default_write_bps=default_write_bps,
            default_read_iops=default_read_iops,
            default_write_iops=default_write_iops,
        )