Ejemplo n.º 1
0
    def test_upload_volume_with_bps_limit(self, mock_stat):
        bps_limit = 1048576
        image_meta = {'id': 1, 'disk_format': 'qcow2'}
        TEST_RET = "image: qemu.qcow2\n"\
                   "file_format: qcow2 \n"\
                   "virtual_size: 50M (52428800 bytes)\n"\
                   "cluster_size: 65536\n"\
                   "disk_size: 196K (200704 bytes)"

        self.override_config('volume_copy_bps_limit', bps_limit)
        prefix = ('cgexec', '-g', 'blkio:test')

        cmd = prefix + ('qemu-img', 'convert', '-O', 'qcow2', mox.IgnoreArg(),
                        mox.IgnoreArg())

        m = self._mox
        m.StubOutWithMock(utils, 'execute')
        m.StubOutWithMock(volume_utils, 'setup_blkio_cgroup')
        m.StubOutWithMock(volume_utils, 'check_for_odirect_support')

        volume_utils.setup_blkio_cgroup(mox.IgnoreArg(), mox.IgnoreArg(),
                                        bps_limit).AndReturn(prefix)
        utils.execute(*cmd, run_as_root=True)
        utils.execute('env',
                      'LC_ALL=C',
                      'qemu-img',
                      'info',
                      mox.IgnoreArg(),
                      run_as_root=True).AndReturn((TEST_RET, 'ignored'))

        m.ReplayAll()
        image_utils.upload_volume(context, FakeImageService(), image_meta,
                                  '/dev/loop1')
        m.VerifyAll()
Ejemplo n.º 2
0
    def test_upload_volume(self, bps_limit=0):
        image_meta = {'id': 1, 'disk_format': 'qcow2'}
        TEST_RET = "image: qemu.qcow2\n"\
                   "file_format: qcow2 \n"\
                   "virtual_size: 50M (52428800 bytes)\n"\
                   "cluster_size: 65536\n"\
                   "disk_size: 196K (200704 bytes)"
        if bps_limit:
            CONF.set_override('volume_copy_bps_limit', bps_limit)
            prefix = ('cgexec', '-g', 'blkio:test')
            postfix = ('-t', 'none')
        else:
            prefix = postfix = ()
        cmd = prefix + ('qemu-img', 'convert', '-O', 'qcow2',
                        mox.IgnoreArg(), mox.IgnoreArg()) + postfix

        m = self._mox
        m.StubOutWithMock(utils, 'execute')
        m.StubOutWithMock(volume_utils, 'setup_blkio_cgroup')

        volume_utils.setup_blkio_cgroup(mox.IgnoreArg(), mox.IgnoreArg(),
                                        bps_limit).AndReturn(prefix)
        utils.execute(*cmd, run_as_root=True)
        utils.execute(
            'env', 'LC_ALL=C', 'qemu-img', 'info',
            mox.IgnoreArg(), run_as_root=True).AndReturn(
                (TEST_RET, 'ignored')
            )

        m.ReplayAll()

        image_utils.upload_volume(context, FakeImageService(),
                                  image_meta, '/dev/loop1')
        m.VerifyAll()
Ejemplo n.º 3
0
    def test_upload_volume_with_bps_limit(self, mock_stat):
        bps_limit = 1048576
        image_meta = {"id": 1, "disk_format": "qcow2"}
        TEST_RET = (
            "image: qemu.qcow2\n"
            "file_format: qcow2 \n"
            "virtual_size: 50M (52428800 bytes)\n"
            "cluster_size: 65536\n"
            "disk_size: 196K (200704 bytes)"
        )

        self.override_config("volume_copy_bps_limit", bps_limit)
        prefix = ("cgexec", "-g", "blkio:test")

        cmd = prefix + ("qemu-img", "convert", "-O", "qcow2", mox.IgnoreArg(), mox.IgnoreArg())

        m = self._mox
        m.StubOutWithMock(utils, "execute")
        m.StubOutWithMock(volume_utils, "setup_blkio_cgroup")
        m.StubOutWithMock(volume_utils, "check_for_odirect_support")

        volume_utils.setup_blkio_cgroup(mox.IgnoreArg(), mox.IgnoreArg(), bps_limit).AndReturn(prefix)
        utils.execute(*cmd, run_as_root=True)
        utils.execute("env", "LC_ALL=C", "qemu-img", "info", mox.IgnoreArg(), run_as_root=True).AndReturn(
            (TEST_RET, "ignored")
        )

        m.ReplayAll()
        image_utils.upload_volume(context, FakeImageService(), image_meta, "/dev/loop1")
        m.VerifyAll()
Ejemplo n.º 4
0
    def _test_fetch_to_raw(self, has_qemu=True, src_inf=None, dest_inf=None,
                           bps_limit=0):
        mox = self._mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')
        mox.StubOutWithMock(volume_utils, 'setup_blkio_cgroup')

        TEST_INFO = ("image: qemu.qcow2\n"
                     "file format: raw\n"
                     "virtual size: 0 (0 bytes)\n"
                     "disk size: 0")

        CONF.set_override('volume_copy_bps_limit', bps_limit)

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)

        test_qemu_img = utils.execute(
            'env', 'LC_ALL=C', 'qemu-img', 'info', self.TEST_DEV_PATH,
            run_as_root=True)

        if has_qemu:
            test_qemu_img.AndReturn((TEST_INFO, 'ignored'))
            image_utils.fetch(context, self._image_service, self.TEST_IMAGE_ID,
                              self.TEST_DEV_PATH, None, None)
        else:
            test_qemu_img.AndRaise(processutils.ProcessExecutionError())

        if has_qemu and src_inf:
            utils.execute(
                'env', 'LC_ALL=C', 'qemu-img', 'info',
                self.TEST_DEV_PATH, run_as_root=True).AndReturn(
                    (src_inf, 'ignored')
                )

        if has_qemu and dest_inf:
            if bps_limit:
                prefix = ('cgexec', '-g', 'blkio:test')
                postfix = ('-t', 'none')
            else:
                prefix = postfix = ()
            cmd = prefix + ('qemu-img', 'convert', '-O', 'raw',
                            self.TEST_DEV_PATH, self.TEST_DEV_PATH) + postfix

            volume_utils.setup_blkio_cgroup(
                self.TEST_DEV_PATH, self.TEST_DEV_PATH,
                bps_limit).AndReturn(prefix)

            utils.execute(*cmd, run_as_root=True)

            utils.execute(
                'env', 'LC_ALL=C', 'qemu-img', 'info',
                self.TEST_DEV_PATH, run_as_root=True).AndReturn(
                    (dest_inf, 'ignored')
                )

        self._mox.ReplayAll()
Ejemplo n.º 5
0
    def _test_fetch_to_raw(self, has_qemu=True, src_inf=None, dest_inf=None, bps_limit=0):
        mox = self._mox
        mox.StubOutWithMock(image_utils, "create_temporary_file")
        mox.StubOutWithMock(utils, "execute")
        mox.StubOutWithMock(image_utils, "fetch")
        mox.StubOutWithMock(volume_utils, "setup_blkio_cgroup")
        mox.StubOutWithMock(utils, "is_blk_device")

        TEST_INFO = "image: qemu.qcow2\n" "file format: raw\n" "virtual size: 0 (0 bytes)\n" "disk size: 0"

        utils.is_blk_device(self.TEST_DEV_PATH).AndReturn(True)
        self.override_config("volume_copy_bps_limit", bps_limit)

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)

        test_qemu_img = utils.execute("env", "LC_ALL=C", "qemu-img", "info", self.TEST_DEV_PATH, run_as_root=True)

        if has_qemu:
            test_qemu_img.AndReturn((TEST_INFO, "ignored"))
            image_utils.fetch(context, self._image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None)
        else:
            test_qemu_img.AndRaise(processutils.ProcessExecutionError())

        if has_qemu and src_inf:
            utils.execute("env", "LC_ALL=C", "qemu-img", "info", self.TEST_DEV_PATH, run_as_root=True).AndReturn(
                (src_inf, "ignored")
            )

        if has_qemu and dest_inf:
            if bps_limit:
                prefix = ("cgexec", "-g", "blkio:test")
            else:
                prefix = ()

            utils.execute(
                "dd", "count=0", "if=/dev/ether/fake_dev", "of=/dev/ether/fake_dev", "oflag=direct", run_as_root=True
            )

            cmd = prefix + ("qemu-img", "convert", "-t", "none", "-O", "raw", self.TEST_DEV_PATH, self.TEST_DEV_PATH)

            volume_utils.setup_blkio_cgroup(self.TEST_DEV_PATH, self.TEST_DEV_PATH, bps_limit).AndReturn(prefix)

            utils.execute(*cmd, run_as_root=True)

            utils.execute("env", "LC_ALL=C", "qemu-img", "info", self.TEST_DEV_PATH, run_as_root=True).AndReturn(
                (dest_inf, "ignored")
            )

        self._mox.ReplayAll()
Ejemplo n.º 6
0
    def test_setup_blkio_cgroup(self, mock_major_minor):

        def fake_get_blkdev_major_minor(path):
            return {'src_volume': "253:0", 'dst_volume': "253:1"}[path]

        mock_major_minor.side_effect = fake_get_blkdev_major_minor

        self.exec_cnt = 0

        def fake_utils_execute(*cmd, **kwargs):
            exec_cmds = [('cgcreate', '-g',
                          'blkio:' + CONF.volume_copy_blkio_cgroup_name),
                         ('cgset', '-r',
                          'blkio.throttle.read_bps_device=253:0 1024',
                          CONF.volume_copy_blkio_cgroup_name),
                         ('cgset', '-r',
                          'blkio.throttle.write_bps_device=253:1 1024',
                          CONF.volume_copy_blkio_cgroup_name)]
            self.assertEqual(exec_cmds[self.exec_cnt], cmd)
            self.exec_cnt += 1

        cmd = volume_utils.setup_blkio_cgroup('src_volume', 'dst_volume', 1024,
                                              execute=fake_utils_execute)
        self.assertEqual(['cgexec', '-g',
                          'blkio:' + CONF.volume_copy_blkio_cgroup_name], cmd)
Ejemplo n.º 7
0
    def test_setup_blkio_cgroup(self, mock_major_minor):
        def fake_get_blkdev_major_minor(path):
            return {'src_volume': "253:0", 'dst_volume': "253:1"}[path]

        mock_major_minor.side_effect = fake_get_blkdev_major_minor

        self.exec_cnt = 0

        def fake_utils_execute(*cmd, **kwargs):
            exec_cmds = [
                ('cgcreate', '-g',
                 'blkio:' + CONF.volume_copy_blkio_cgroup_name),
                ('cgset', '-r', 'blkio.throttle.read_bps_device=253:0 1024',
                 CONF.volume_copy_blkio_cgroup_name),
                ('cgset', '-r', 'blkio.throttle.write_bps_device=253:1 1024',
                 CONF.volume_copy_blkio_cgroup_name)
            ]
            self.assertEqual(exec_cmds[self.exec_cnt], cmd)
            self.exec_cnt += 1

        cmd = volume_utils.setup_blkio_cgroup('src_volume',
                                              'dst_volume',
                                              1024,
                                              execute=fake_utils_execute)
        self.assertEqual(
            ['cgexec', '-g', 'blkio:' + CONF.volume_copy_blkio_cgroup_name],
            cmd)
Ejemplo n.º 8
0
 def test_get_blkdev_error(self, mock_get_blkdev):
     mock_exec = mock.Mock()
     output = volume_utils.setup_blkio_cgroup('src', 'dst', 1,
                                              execute=mock_exec)
     self.assertIsNone(output)
     mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
     self.assertFalse(mock_exec.called)
Ejemplo n.º 9
0
def convert_image(source, dest, out_format, bps_limit=None):
    """Convert image to other format."""
    start_time = timeutils.utcnow()
    # Always set -t none. First it is needed for cgroup io/limiting
    # and it is needed to ensure that all data hit the device before
    # it gets unmapped remotely from the host
    cmd = ('qemu-img', 'convert',
           '-t', 'none',
           '-O', out_format, source, dest)
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
    utils.execute(*cmd, run_as_root=True)

    duration = timeutils.delta_seconds(start_time, timeutils.utcnow())

    # NOTE(jdg): use a default of 1, mostly for unit test, but in
    # some incredible event this is 0 (cirros image?) don't barf
    if duration < 1:
        duration = 1
    fsz_mb = os.stat(source).st_size / units.Mi
    mbps = (fsz_mb / duration)
    msg = ("Image conversion details: src %(src)s, size %(sz).2f MB, "
           "duration %(duration).2f sec, destination %(dest)s")
    LOG.debug(msg % {"src": source,
                     "sz": fsz_mb,
                     "duration": duration,
                     "dest": dest})

    msg = _("Converted %(sz).2f MB image at %(mbps).2f MB/s")
    LOG.info(msg % {"sz": fsz_mb, "mbps": mbps})
Ejemplo n.º 10
0
def convert_image(source, dest, out_format, bps_limit=None):
    """Convert image to other format."""
    start_time = timeutils.utcnow()
    # Always set -t none. First it is needed for cgroup io/limiting
    # and it is needed to ensure that all data hit the device before
    # it gets unmapped remotely from the host
    cmd = ('qemu-img', 'convert', '-t', 'none', '-O', out_format, source, dest)
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
    utils.execute(*cmd, run_as_root=True)

    duration = timeutils.delta_seconds(start_time, timeutils.utcnow())

    # NOTE(jdg): use a default of 1, mostly for unit test, but in
    # some incredible event this is 0 (cirros image?) don't barf
    if duration < 1:
        duration = 1
    fsz_mb = os.stat(source).st_size / units.Mi
    mbps = (fsz_mb / duration)
    msg = ("Image conversion details: src %(src)s, size %(sz).2f MB, "
           "duration %(duration).2f sec, destination %(dest)s")
    LOG.debug(msg % {
        "src": source,
        "sz": fsz_mb,
        "duration": duration,
        "dest": dest
    })

    msg = _("Converted %(sz).2f MB image at %(mbps).2f MB/s")
    LOG.info(msg % {"sz": fsz_mb, "mbps": mbps})
Ejemplo n.º 11
0
def convert_image(source, dest, out_format, bps_limit=None):
    """Convert image to other format."""
    start_time = timeutils.utcnow()
    cmd = ('qemu-img', 'convert', '-O', out_format, source, dest)
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
        cmd += ('-t', 'none')  # required to enable ratelimit by blkio cgroup
    utils.execute(*cmd, run_as_root=True)

    duration = timeutils.delta_seconds(start_time, timeutils.utcnow())

    # NOTE(jdg): use a default of 1, mostly for unit test, but in
    # some incredible event this is 0 (cirros image?) don't barf
    if duration < 1:
        duration = 1
    fsz_mb = os.stat(source).st_size / units.Mi
    mbps = (fsz_mb / duration)
    msg = ("Image conversion details: src %(src)s, size %(sz).2f MB, "
           "duration %(duration).2f sec, destination %(dest)s")
    LOG.debug(msg % {"src": source,
                     "sz": fsz_mb,
                     "duration": duration,
                     "dest": dest})

    msg = _("Converted %(sz).2f MB image at %(mbps).2f MB/s")
    LOG.info(msg % {"sz": fsz_mb, "mbps": mbps})
Ejemplo n.º 12
0
def convert_image(source, dest, out_format, bps_limit=None):
    """Convert image to other format."""
    start_time = timeutils.utcnow()
    cmd = ('qemu-img', 'convert', '-O', out_format, source, dest)
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
        cmd += ('-t', 'none')  # required to enable ratelimit by blkio cgroup
    utils.execute(*cmd, run_as_root=True)

    duration = timeutils.delta_seconds(start_time, timeutils.utcnow())

    # NOTE(jdg): use a default of 1, mostly for unit test, but in
    # some incredible event this is 0 (cirros image?) don't barf
    if duration < 1:
        duration = 1
    fsz_mb = os.stat(source).st_size / units.Mi
    mbps = (fsz_mb / duration)
    msg = ("Image conversion details: src %(src)s, size %(sz).2f MB, "
           "duration %(duration).2f sec, destination %(dest)s")
    LOG.debug(msg % {
        "src": source,
        "sz": fsz_mb,
        "duration": duration,
        "dest": dest
    })

    msg = _("Converted %(sz).2f MB image at %(mbps).2f MB/s")
    LOG.info(msg % {"sz": fsz_mb, "mbps": mbps})
Ejemplo n.º 13
0
def convert_image(source, dest, out_format, bps_limit=None):
    """Convert image to other format."""
    cmd = ('qemu-img', 'convert', '-O', out_format, source, dest)
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
        cmd += ('-t', 'none')  # required to enable ratelimit by blkio cgroup
    utils.execute(*cmd, run_as_root=True)
Ejemplo n.º 14
0
def convert_image(source, dest, out_format, bps_limit=None):
    """Convert image to other format."""
    cmd = ('qemu-img', 'convert', '-O', out_format, source, dest)
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
        cmd += ('-t', 'none')  # required to enable ratelimit by blkio cgroup
    utils.execute(*cmd, run_as_root=True)
Ejemplo n.º 15
0
 def test_bps_limit_zero(self):
     mock_exec = mock.Mock()
     output = volume_utils.setup_blkio_cgroup('src',
                                              'dst',
                                              0,
                                              execute=mock_exec)
     self.assertIsNone(output)
     self.assertFalse(mock_exec.called)
Ejemplo n.º 16
0
 def test_get_blkdev_error(self, mock_get_blkdev):
     mock_exec = mock.Mock()
     output = volume_utils.setup_blkio_cgroup('src',
                                              'dst',
                                              1,
                                              execute=mock_exec)
     self.assertIsNone(output)
     mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
     self.assertFalse(mock_exec.called)
Ejemplo n.º 17
0
 def test_cgcreate_fail(self, mock_conf, mock_get_blkdev):
     mock_conf.volume_copy_blkio_cgroup_name = 'test_group'
     mock_exec = mock.Mock()
     mock_exec.side_effect = processutils.ProcessExecutionError
     output = volume_utils.setup_blkio_cgroup('src', 'dst', 1,
                                              execute=mock_exec)
     self.assertIsNone(output)
     mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
     mock_exec.assert_called_once_with('cgcreate', '-g', 'blkio:test_group',
                                       run_as_root=True)
Ejemplo n.º 18
0
def convert_image(source, dest, out_format, bps_limit=None, is_qcow_compress=False):
    """Convert image to other format."""

    cmd = ('qemu-img', 'convert',
           '-O', out_format, source, dest)

    if is_qcow_compress and out_format=='qcow2':
        cmd = ('qemu-img', 'convert',
               '-c',
               '-O', out_format, source, dest)
    else:
        cmd = ('qemu-img', 'convert',
               '-O', out_format, source, dest)

    # Check whether O_DIRECT is supported and set '-t none' if it is
    # This is needed to ensure that all data hit the device before
    # it gets unmapped remotely from the host for some backends
    # Reference Bug: #1363016

    # NOTE(jdg): In the case of file devices qemu does the
    # flush properly and more efficiently than would be done
    # setting O_DIRECT, so check for that and skip the
    # setting for non BLK devs
    if (utils.is_blk_device(dest) and
            volume_utils.check_for_odirect_support(source,
                                                   dest,
                                                   'oflag=direct')):
        cmd = ('qemu-img', 'convert',
               '-t', 'none',
               '-O', out_format, source, dest)

    start_time = timeutils.utcnow()
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
    utils.execute(*cmd, run_as_root=True)

    duration = timeutils.delta_seconds(start_time, timeutils.utcnow())

    # NOTE(jdg): use a default of 1, mostly for unit test, but in
    # some incredible event this is 0 (cirros image?) don't barf
    if duration < 1:
        duration = 1
    fsz_mb = os.stat(source).st_size / units.Mi
    mbps = (fsz_mb / duration)
    msg = ("Image conversion details: src %(src)s, size %(sz).2f MB, "
           "duration %(duration).2f sec, destination %(dest)s")
    LOG.debug(msg % {"src": source,
                     "sz": fsz_mb,
                     "duration": duration,
                     "dest": dest})

    msg = _("Converted %(sz).2f MB image at %(mbps).2f MB/s")
    LOG.info(msg % {"sz": fsz_mb, "mbps": mbps})
Ejemplo n.º 19
0
def convert_image(source,
                  dest,
                  out_format,
                  bps_limit=None,
                  is_qcow_compress=False):
    """Convert image to other format."""

    cmd = ('qemu-img', 'convert', '-O', out_format, source, dest)

    if is_qcow_compress and out_format == 'qcow2':
        cmd = ('qemu-img', 'convert', '-c', '-O', out_format, source, dest)
    else:
        cmd = ('qemu-img', 'convert', '-O', out_format, source, dest)

    # Check whether O_DIRECT is supported and set '-t none' if it is
    # This is needed to ensure that all data hit the device before
    # it gets unmapped remotely from the host for some backends
    # Reference Bug: #1363016

    # NOTE(jdg): In the case of file devices qemu does the
    # flush properly and more efficiently than would be done
    # setting O_DIRECT, so check for that and skip the
    # setting for non BLK devs
    if (utils.is_blk_device(dest) and volume_utils.check_for_odirect_support(
            source, dest, 'oflag=direct')):
        cmd = ('qemu-img', 'convert', '-t', 'none', '-O', out_format, source,
               dest)

    start_time = timeutils.utcnow()
    cgcmd = volume_utils.setup_blkio_cgroup(source, dest, bps_limit)
    if cgcmd:
        cmd = tuple(cgcmd) + cmd
    utils.execute(*cmd, run_as_root=True)

    duration = timeutils.delta_seconds(start_time, timeutils.utcnow())

    # NOTE(jdg): use a default of 1, mostly for unit test, but in
    # some incredible event this is 0 (cirros image?) don't barf
    if duration < 1:
        duration = 1
    fsz_mb = os.stat(source).st_size / units.Mi
    mbps = (fsz_mb / duration)
    msg = ("Image conversion details: src %(src)s, size %(sz).2f MB, "
           "duration %(duration).2f sec, destination %(dest)s")
    LOG.debug(msg % {
        "src": source,
        "sz": fsz_mb,
        "duration": duration,
        "dest": dest
    })

    msg = _("Converted %(sz).2f MB image at %(mbps).2f MB/s")
    LOG.info(msg % {"sz": fsz_mb, "mbps": mbps})
Ejemplo n.º 20
0
 def test_setup_blkio_cgroup(self, mock_conf, mock_get_blkdev):
     mock_conf.volume_copy_blkio_cgroup_name = 'test_group'
     mock_exec = mock.Mock()
     output = volume_utils.setup_blkio_cgroup('src', 'dst', 1,
                                              execute=mock_exec)
     self.assertEqual(['cgexec', '-g', 'blkio:test_group'], output)
     mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
     mock_exec.assert_has_calls([
         mock.call('cgcreate', '-g', 'blkio:test_group', run_as_root=True),
         mock.call('cgset', '-r', 'blkio.throttle.read_bps_device=src 1',
                   'test_group', run_as_root=True),
         mock.call('cgset', '-r', 'blkio.throttle.write_bps_device=dst 1',
                   'test_group', run_as_root=True)])
Ejemplo n.º 21
0
 def test_cgcreate_fail(self, mock_conf, mock_get_blkdev):
     mock_conf.volume_copy_blkio_cgroup_name = 'test_group'
     mock_exec = mock.Mock()
     mock_exec.side_effect = processutils.ProcessExecutionError
     output = volume_utils.setup_blkio_cgroup('src',
                                              'dst',
                                              1,
                                              execute=mock_exec)
     self.assertIsNone(output)
     mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
     mock_exec.assert_called_once_with('cgcreate',
                                       '-g',
                                       'blkio:test_group',
                                       run_as_root=True)
Ejemplo n.º 22
0
    def test_cgset_fail(self, mock_conf, mock_get_blkdev):
        mock_conf.volume_copy_blkio_cgroup_name = 'test_group'
        mock_exec = mock.Mock()

        def cgset_exception(*args, **kwargs):
            if 'cgset' in args:
                raise processutils.ProcessExecutionError

        mock_exec.side_effect = cgset_exception
        output = volume_utils.setup_blkio_cgroup('src', 'dst', 1,
                                                 execute=mock_exec)
        self.assertIsNone(output)
        mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
        mock_exec.assert_has_calls([
            mock.call('cgcreate', '-g', 'blkio:test_group', run_as_root=True),
            mock.call('cgset', '-r', 'blkio.throttle.read_bps_device=src 1',
                      'test_group', run_as_root=True)])
Ejemplo n.º 23
0
    def test_setup_blkio_cgroup(self, mock_major_minor):
        def fake_get_blkdev_major_minor(path):
            return {"src_volume": "253:0", "dst_volume": "253:1"}[path]

        mock_major_minor.side_effect = fake_get_blkdev_major_minor

        self.exec_cnt = 0

        def fake_utils_execute(*cmd, **kwargs):
            exec_cmds = [
                ("cgcreate", "-g", "blkio:" + CONF.volume_copy_blkio_cgroup_name),
                ("cgset", "-r", "blkio.throttle.read_bps_device=253:0 1024", CONF.volume_copy_blkio_cgroup_name),
                ("cgset", "-r", "blkio.throttle.write_bps_device=253:1 1024", CONF.volume_copy_blkio_cgroup_name),
            ]
            self.assertEqual(exec_cmds[self.exec_cnt], cmd)
            self.exec_cnt += 1

        cmd = volume_utils.setup_blkio_cgroup("src_volume", "dst_volume", 1024, execute=fake_utils_execute)
        self.assertEqual(["cgexec", "-g", "blkio:" + CONF.volume_copy_blkio_cgroup_name], cmd)
Ejemplo n.º 24
0
 def test_setup_blkio_cgroup(self, mock_conf, mock_get_blkdev):
     mock_conf.volume_copy_blkio_cgroup_name = 'test_group'
     mock_exec = mock.Mock()
     output = volume_utils.setup_blkio_cgroup('src',
                                              'dst',
                                              1,
                                              execute=mock_exec)
     self.assertEqual(['cgexec', '-g', 'blkio:test_group'], output)
     mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
     mock_exec.assert_has_calls([
         mock.call('cgcreate', '-g', 'blkio:test_group', run_as_root=True),
         mock.call('cgset',
                   '-r',
                   'blkio.throttle.read_bps_device=src 1',
                   'test_group',
                   run_as_root=True),
         mock.call('cgset',
                   '-r',
                   'blkio.throttle.write_bps_device=dst 1',
                   'test_group',
                   run_as_root=True)
     ])
Ejemplo n.º 25
0
    def test_cgset_fail(self, mock_conf, mock_get_blkdev):
        mock_conf.volume_copy_blkio_cgroup_name = 'test_group'
        mock_exec = mock.Mock()

        def cgset_exception(*args, **kwargs):
            if 'cgset' in args:
                raise processutils.ProcessExecutionError

        mock_exec.side_effect = cgset_exception
        output = volume_utils.setup_blkio_cgroup('src',
                                                 'dst',
                                                 1,
                                                 execute=mock_exec)
        self.assertIsNone(output)
        mock_get_blkdev.assert_has_calls([mock.call('src'), mock.call('dst')])
        mock_exec.assert_has_calls([
            mock.call('cgcreate', '-g', 'blkio:test_group', run_as_root=True),
            mock.call('cgset',
                      '-r',
                      'blkio.throttle.read_bps_device=src 1',
                      'test_group',
                      run_as_root=True)
        ])
Ejemplo n.º 26
0
    def _test_fetch_to_raw(self,
                           has_qemu=True,
                           src_inf=None,
                           dest_inf=None,
                           bps_limit=0):
        mox = self._mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')
        mox.StubOutWithMock(volume_utils, 'setup_blkio_cgroup')

        TEST_INFO = ("image: qemu.qcow2\n"
                     "file format: raw\n"
                     "virtual size: 0 (0 bytes)\n"
                     "disk size: 0")

        CONF.set_override('volume_copy_bps_limit', bps_limit)

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)

        test_qemu_img = utils.execute('env',
                                      'LC_ALL=C',
                                      'qemu-img',
                                      'info',
                                      self.TEST_DEV_PATH,
                                      run_as_root=True)

        if has_qemu:
            test_qemu_img.AndReturn((TEST_INFO, 'ignored'))
            image_utils.fetch(context, self._image_service, self.TEST_IMAGE_ID,
                              self.TEST_DEV_PATH, None, None)
        else:
            test_qemu_img.AndRaise(processutils.ProcessExecutionError())

        if has_qemu and src_inf:
            utils.execute('env',
                          'LC_ALL=C',
                          'qemu-img',
                          'info',
                          self.TEST_DEV_PATH,
                          run_as_root=True).AndReturn((src_inf, 'ignored'))

        if has_qemu and dest_inf:
            if bps_limit:
                prefix = ('cgexec', '-g', 'blkio:test')
                postfix = ('-t', 'none')
            else:
                prefix = postfix = ()
            cmd = prefix + ('qemu-img', 'convert', '-O', 'raw',
                            self.TEST_DEV_PATH, self.TEST_DEV_PATH) + postfix

            volume_utils.setup_blkio_cgroup(self.TEST_DEV_PATH,
                                            self.TEST_DEV_PATH,
                                            bps_limit).AndReturn(prefix)

            utils.execute(*cmd, run_as_root=True)

            utils.execute('env',
                          'LC_ALL=C',
                          'qemu-img',
                          'info',
                          self.TEST_DEV_PATH,
                          run_as_root=True).AndReturn((dest_inf, 'ignored'))

        self._mox.ReplayAll()
Ejemplo n.º 27
0
 def test_bps_limit_zero(self):
     mock_exec = mock.Mock()
     output = volume_utils.setup_blkio_cgroup('src', 'dst', 0,
                                              execute=mock_exec)
     self.assertIsNone(output)
     self.assertFalse(mock_exec.called)