Exemplo n.º 1
0
    def test_fetch_to_raw_on_error_backing_file(self):
        TEST_RET = "image: qemu.qcow2\n"\
                   "backing_file: qemu.qcow2 (actual path: qemu.qcow2)\n"\
                   "file_format: qcow2 \n"\
                   "virtual_size: 50M (52428800 bytes)\n"\
                   "cluster_size: 65536\n"\
                   "disk_size: 196K (200704 bytes)"

        fake_image_service = FakeImageService()
        mox = self._mox

        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)
        image_utils.fetch(context, fake_image_service, self.TEST_IMAGE_ID,
                          self.TEST_DEV_PATH, None, None)

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

        mox.ReplayAll()
        self.assertRaises(exception.ImageUnacceptable,
                          image_utils.fetch_to_raw, context,
                          fake_image_service, self.TEST_IMAGE_ID,
                          self.TEST_DEV_PATH)
Exemplo n.º 2
0
    def test_fetch_to_raw(self):
        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)"
        TEST_RETURN_RAW = "image: qemu.raw\n"\
                          "file_format: raw\n"\
                          "virtual_size: 50M (52428800 bytes)\n"\
                          "cluster_size: 65536\n"\
                          "disk_size: 196K (200704 bytes)\n"\

        fake_image_service = FakeImageService()
        mox = self._mox

        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)
        image_utils.fetch(context, fake_image_service, self.TEST_IMAGE_ID,
                          self.TEST_DEV_PATH, None, None)

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

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

        utils.execute('qemu-img',
                      'convert',
                      '-O',
                      'raw',
                      self.TEST_DEV_PATH,
                      self.TEST_DEV_PATH,
                      run_as_root=True)

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

        mox.ReplayAll()

        image_utils.fetch_to_raw(context, fake_image_service,
                                 self.TEST_IMAGE_ID, self.TEST_DEV_PATH)
        mox.VerifyAll()
Exemplo n.º 3
0
    def test_fetch_to_raw_on_error_backing_file(self):
        TEST_RET = "image: qemu.qcow2\n"\
                   "backing_file: qemu.qcow2 (actual path: qemu.qcow2)\n"\
                   "file_format: qcow2 \n"\
                   "virtual_size: 50M (52428800 bytes)\n"\
                   "cluster_size: 65536\n"\
                   "disk_size: 196K (200704 bytes)"

        fake_image_service = FakeImageService()
        mox = self._mox

        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)
        image_utils.fetch(context, fake_image_service,
                          self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None)

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

        mox.ReplayAll()
        self.assertRaises(exception.ImageUnacceptable,
                          image_utils.fetch_to_raw,
                          context, fake_image_service,
                          self.TEST_IMAGE_ID, self.TEST_DEV_PATH)
Exemplo n.º 4
0
    def _test_fetch_to_raw(self, has_qemu=True, src_inf=None, dest_inf=None):
        mox = self._mox
        mox.StubOutWithMock(image_utils, "create_temporary_file")
        mox.StubOutWithMock(utils, "execute")
        mox.StubOutWithMock(image_utils, "fetch")

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

        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:
            utils.execute("qemu-img", "convert", "-O", "raw", self.TEST_DEV_PATH, self.TEST_DEV_PATH, 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()
Exemplo 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')

        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()
Exemplo n.º 6
0
    def test_file_unlinked(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(image_utils.os, 'unlink')

        image_utils.create_temporary_file().AndReturn('somefile')
        image_utils.os.unlink('somefile')

        mox.ReplayAll()

        with image_utils.temporary_file():
            pass
Exemplo n.º 7
0
    def test_file_unlinked(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(image_utils.os, 'unlink')

        image_utils.create_temporary_file().AndReturn('somefile')
        image_utils.os.unlink('somefile')

        mox.ReplayAll()

        with image_utils.temporary_file():
            pass
Exemplo n.º 8
0
    def test_file_unlinked(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(fileutils, 'delete_if_exists')

        image_utils.create_temporary_file().AndReturn('somefile')
        fileutils.delete_if_exists('somefile')

        mox.ReplayAll()

        with image_utils.temporary_file():
            pass
Exemplo n.º 9
0
    def test_file_unlinked(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(fileutils, 'delete_if_exists')

        image_utils.create_temporary_file().AndReturn('somefile')
        fileutils.delete_if_exists('somefile')

        mox.ReplayAll()

        with image_utils.temporary_file():
            pass
    def _test_fetch_to_raw(self, has_qemu=True, src_inf=None, dest_inf=None):
        mox = self._mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')

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

        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:
            utils.execute('qemu-img',
                          'convert',
                          '-O',
                          'raw',
                          self.TEST_DEV_PATH,
                          self.TEST_DEV_PATH,
                          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()
Exemplo n.º 11
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()
Exemplo n.º 12
0
    def test_fetch_to_raw(self):
        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)"
        TEST_RETURN_RAW = "image: qemu.raw\n"\
                          "file_format: raw\n"\
                          "virtual_size: 50M (52428800 bytes)\n"\
                          "cluster_size: 65536\n"\
                          "disk_size: 196K (200704 bytes)\n"\

        fake_image_service = FakeImageService()
        mox = self._mox

        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)
        image_utils.fetch(context, fake_image_service,
                          self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None)

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

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

        utils.execute('qemu-img', 'convert', '-O', 'raw',
                      self.TEST_DEV_PATH, self.TEST_DEV_PATH, run_as_root=True)

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

        mox.ReplayAll()

        image_utils.fetch_to_raw(context, fake_image_service,
                                 self.TEST_IMAGE_ID, self.TEST_DEV_PATH)
        mox.VerifyAll()
Exemplo n.º 13
0
    def test_file_unlinked_on_error(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(image_utils.os, 'unlink')

        image_utils.create_temporary_file().AndReturn('somefile')
        image_utils.os.unlink('somefile')

        mox.ReplayAll()

        def sut():
            with image_utils.temporary_file():
                raise test.TestingException()

        self.assertRaises(test.TestingException, sut)
Exemplo n.º 14
0
    def test_file_unlinked_on_error(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, "create_temporary_file")
        mox.StubOutWithMock(fileutils, "delete_if_exists")

        image_utils.create_temporary_file().AndReturn("somefile")
        fileutils.delete_if_exists("somefile")

        mox.ReplayAll()

        def sut():
            with image_utils.temporary_file():
                raise test.TestingException()

        self.assertRaises(test.TestingException, sut)
Exemplo n.º 15
0
    def test_file_unlinked_on_error(self):
        mox = self.mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(image_utils.os, 'unlink')

        image_utils.create_temporary_file().AndReturn('somefile')
        image_utils.os.unlink('somefile')

        mox.ReplayAll()

        def sut():
            with image_utils.temporary_file():
                raise test.TestingException()

        self.assertRaises(test.TestingException, sut)
Exemplo n.º 16
0
    def test_copy_image_to_volume(self):
        """resize_image common case usage."""
        drv = self._driver

        volume = db_fakes.get_fake_volume_info()

        fake_get_supported_type = lambda x: constants.VHD_TYPE_FIXED
        self.stubs.Set(drv, 'local_path', self.fake_local_path)
        self.stubs.Set(windows_utils.WindowsUtils, 'get_supported_vhd_type',
                       fake_get_supported_type)

        self.mox.StubOutWithMock(os, 'makedirs')
        self.mox.StubOutWithMock(os, 'unlink')
        self.mox.StubOutWithMock(image_utils, 'create_temporary_file')
        self.mox.StubOutWithMock(image_utils, 'fetch_to_vhd')
        self.mox.StubOutWithMock(vhdutils.VHDUtils, 'convert_vhd')
        self.mox.StubOutWithMock(vhdutils.VHDUtils, 'resize_vhd')
        self.mox.StubOutWithMock(windows_utils.WindowsUtils,
                                 'change_disk_status')

        fake_temp_path = r'C:\fake\temp\file'
        if (CONF.image_conversion_dir and not
                os.path.exists(CONF.image_conversion_dir)):
            os.makedirs(CONF.image_conversion_dir)
        image_utils.create_temporary_file(suffix='.vhd').AndReturn(
            fake_temp_path)

        fake_volume_path = self.fake_local_path(volume)

        image_utils.fetch_to_vhd(None, None, None,
                                 fake_temp_path,
                                 mox.IgnoreArg())
        windows_utils.WindowsUtils.change_disk_status(volume['name'],
                                                      mox.IsA(bool))
        os.unlink(mox.IsA(str))
        vhdutils.VHDUtils.convert_vhd(fake_temp_path,
                                      fake_volume_path,
                                      constants.VHD_TYPE_FIXED)
        vhdutils.VHDUtils.resize_vhd(fake_volume_path,
                                     volume['size'] << 30)
        windows_utils.WindowsUtils.change_disk_status(volume['name'],
                                                      mox.IsA(bool))
        os.unlink(mox.IsA(str))

        self.mox.ReplayAll()

        drv.copy_image_to_volume(None, volume, None, None)
Exemplo n.º 17
0
    def test_copy_image_to_volume(self):
        """resize_image common case usage."""
        drv = self._driver

        volume = db_fakes.get_fake_volume_info()

        fake_get_supported_type = lambda x: constants.VHD_TYPE_FIXED
        self.stubs.Set(drv, 'local_path', self.fake_local_path)
        self.stubs.Set(windows_utils.WindowsUtils, 'get_supported_vhd_type',
                       fake_get_supported_type)

        self.mox.StubOutWithMock(os, 'makedirs')
        self.mox.StubOutWithMock(os, 'unlink')
        self.mox.StubOutWithMock(image_utils, 'create_temporary_file')
        self.mox.StubOutWithMock(image_utils, 'fetch_to_vhd')
        self.mox.StubOutWithMock(vhdutils.VHDUtils, 'convert_vhd')
        self.mox.StubOutWithMock(vhdutils.VHDUtils, 'resize_vhd')
        self.mox.StubOutWithMock(windows_utils.WindowsUtils,
                                 'change_disk_status')

        fake_temp_path = r'C:\fake\temp\file'
        if (CONF.image_conversion_dir and not
                os.path.exists(CONF.image_conversion_dir)):
            os.makedirs(CONF.image_conversion_dir)
        image_utils.create_temporary_file(suffix='.vhd').AndReturn(
            fake_temp_path)

        fake_volume_path = self.fake_local_path(volume)

        image_utils.fetch_to_vhd(None, None, None,
                                 fake_temp_path,
                                 mox.IgnoreArg())
        windows_utils.WindowsUtils.change_disk_status(volume['name'],
                                                      mox.IsA(bool))
        os.unlink(mox.IsA(str))
        vhdutils.VHDUtils.convert_vhd(fake_temp_path,
                                      fake_volume_path,
                                      constants.VHD_TYPE_FIXED)
        vhdutils.VHDUtils.resize_vhd(fake_volume_path,
                                     volume['size'] << 30)
        windows_utils.WindowsUtils.change_disk_status(volume['name'],
                                                      mox.IsA(bool))
        os.unlink(mox.IsA(str))

        self.mox.ReplayAll()

        drv.copy_image_to_volume(None, volume, None, None)
Exemplo n.º 18
0
    def _test_fetch_to_raw(self, has_qemu=True, src_inf=None, dest_inf=None):
        mox = self._mox
        mox.StubOutWithMock(image_utils, 'create_temporary_file')
        mox.StubOutWithMock(utils, 'execute')
        mox.StubOutWithMock(image_utils, 'fetch')

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

        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:
            utils.execute(
                'qemu-img', 'convert', '-O', 'raw',
                self.TEST_DEV_PATH, self.TEST_DEV_PATH, 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()
Exemplo n.º 19
0
    def test_fetch_to_raw_on_error_not_convert_to_raw(self):
        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)"
        )

        fake_image_service = FakeImageService()
        mox = self._mox

        mox.StubOutWithMock(image_utils, "create_temporary_file")
        mox.StubOutWithMock(utils, "execute")
        mox.StubOutWithMock(image_utils, "fetch")

        image_utils.create_temporary_file().AndReturn(self.TEST_DEV_PATH)
        image_utils.fetch(context, fake_image_service, self.TEST_IMAGE_ID, self.TEST_DEV_PATH, None, None)

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

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

        utils.execute("qemu-img", "convert", "-O", "raw", self.TEST_DEV_PATH, self.TEST_DEV_PATH, run_as_root=True)

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

        mox.ReplayAll()

        self.assertRaises(
            exception.ImageUnacceptable,
            image_utils.fetch_to_raw,
            context,
            fake_image_service,
            self.TEST_IMAGE_ID,
            self.TEST_DEV_PATH,
        )
Exemplo n.º 20
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()