def test_check_already_managed_with_id_vol_pattern(self, conf_mock):
        mock_db = mock.Mock()
        conf_mock.volume_name_template = '%s-volume'

        result = volume_utils.check_already_managed_volume(
            mock_db, 'd8cd1feb-2dcc-404d-9b15-b86fe3bec0a1-volume')
        self.assertTrue(result)
Ejemplo n.º 2
0
    def test_check_managed_volume_not_managed_id_like_uuid(self):
        mock_db = mock.Mock()

        result = volume_utils.check_already_managed_volume(
            mock_db, 'volume-d8cd1fe')

        self.assertFalse(result)
    def test_check_managed_volume_not_managed_id_like_uuid(self):
        mock_db = mock.Mock()

        result = volume_utils.check_already_managed_volume(
            mock_db, 'volume-d8cd1fe')

        self.assertFalse(result)
    def test_check_managed_volume_not_managed(self):
        mock_db = mock.Mock()

        result = volume_utils.check_already_managed_volume(
            mock_db, 'test-volume')

        self.assertFalse(result)
Ejemplo n.º 5
0
 def test_check_managed_volume_not_managed_cinder_like_name(self,
                                                            exists_mock):
     id_ = 'd8cd1feb-2dcc-404d-9b15-b86fe3bec0a1'
     vol_id = 'volume-' + id_
     result = volume_utils.check_already_managed_volume(vol_id)
     self.assertFalse(result)
     exists_mock.assert_called_once_with(mock.ANY, models.Volume, id_)
Ejemplo n.º 6
0
    def test_check_managed_volume_not_managed(self):
        mock_db = mock.Mock()

        result = volume_utils.check_already_managed_volume(
            mock_db, 'test-volume')

        self.assertFalse(result)
Ejemplo n.º 7
0
    def test_check_already_managed_with_id_vol_pattern(self, conf_mock):
        mock_db = mock.Mock()
        conf_mock.volume_name_template = '%s-volume'

        result = volume_utils.check_already_managed_volume(
            mock_db, 'd8cd1feb-2dcc-404d-9b15-b86fe3bec0a1-volume')
        self.assertTrue(result)
Ejemplo n.º 8
0
    def test_check_managed_volume_not_managed_cinder_like_name(self):
        mock_db = mock.Mock()
        mock_db.volume_get = mock.Mock(
            side_effect=exception.VolumeNotFound("volume-d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1")
        )

        result = volume_utils.check_already_managed_volume(mock_db, "volume-d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1")

        self.assertFalse(result)
    def test_check_managed_volume_not_managed_cinder_like_name(self):
        mock_db = mock.Mock()
        mock_db.volume_get = mock.Mock(side_effect=exception.VolumeNotFound(
            'volume-d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1'))

        result = volume_utils.check_already_managed_volume(
            mock_db, 'volume-d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1')

        self.assertFalse(result)
Ejemplo n.º 10
0
    def test_check_already_managed_with_id_vol_pattern(self, exists_mock):
        template = '%s-volume'
        self.override_config('volume_name_template', template)
        id_ = 'd8cd1feb-2dcc-404d-9b15-b86fe3bec0a1'
        vol_id = template % id_

        result = volume_utils.check_already_managed_volume(vol_id)
        self.assertTrue(result)
        exists_mock.assert_called_once_with(mock.ANY, models.Volume, id_)
Ejemplo n.º 11
0
    def manage_existing(self, volume, existing_vol_ref):
        """Manages an existing volume.

        The specified Cinder volume is to be taken into Cinder management.
        The driver will verify its existence and then rename it to the
        new Cinder volume name. It is expected that the existing volume
        reference is an NFS share point and some [/path]/volume;
        e.g., 10.10.32.1:/openstack/vol_to_manage
        or 10.10.32.1:/openstack/some_directory/vol_to_manage

        :param volume: cinder volume to manage
        :param existing_vol_ref: driver-specific information used to identify a
                                 volume
        :returns: the provider location
        :raises VolumeBackendAPIException:
        """

        # Attempt to find NFS share, NFS mount, and volume path from vol_ref.
        (nfs_share, nfs_mount, vol_name
         ) = self._get_share_mount_and_vol_from_vol_ref(existing_vol_ref)

        LOG.info("Asked to manage NFS volume %(vol)s, "
                 "with vol ref %(ref)s.",
                 {'vol': volume.id,
                  'ref': existing_vol_ref['source-name']})

        vol_id = utils.extract_id_from_volume_name(vol_name)
        if utils.check_already_managed_volume(vol_id):
            raise exception.ManageExistingAlreadyManaged(volume_ref=vol_name)

        self._check_pool_and_share(volume, nfs_share)

        if vol_name == volume.name:
            LOG.debug("New Cinder volume %(vol)s name matches reference name: "
                      "no need to rename.", {'vol': volume.name})
        else:
            src_vol = os.path.join(nfs_mount, vol_name)
            dst_vol = os.path.join(nfs_mount, volume.name)
            try:
                self._try_execute("mv", src_vol, dst_vol, run_as_root=False,
                                  check_exit_code=True)
                LOG.debug("Setting newly managed Cinder volume name "
                          "to %(vol)s.", {'vol': volume.name})
                self._set_rw_permissions_for_all(dst_vol)
            except (OSError, processutils.ProcessExecutionError) as err:
                msg = (_("Failed to manage existing volume "
                         "%(name)s, because rename operation "
                         "failed: Error msg: %(msg)s.") %
                       {'name': existing_vol_ref['source-name'],
                        'msg': six.text_type(err)})
                LOG.error(msg)
                raise exception.VolumeBackendAPIException(data=msg)
        return {'provider_location': nfs_share}
Ejemplo n.º 12
0
    def manage_existing(self, volume, existing_vol_ref):
        """Manages an existing volume.

        The specified Cinder volume is to be taken into Cinder management.
        The driver will verify its existence and then rename it to the
        new Cinder volume name. It is expected that the existing volume
        reference is an NFS share point and some [/path]/volume;
        e.g., 10.10.32.1:/openstack/vol_to_manage
        or 10.10.32.1:/openstack/some_directory/vol_to_manage

        :param volume: cinder volume to manage
        :param existing_vol_ref: driver-specific information used to identify a
        volume
        :returns: the provider location
        :raises: VolumeBackendAPIException
        """

        # Attempt to find NFS share, NFS mount, and volume path from vol_ref.
        (nfs_share, nfs_mount, vol_name
         ) = self._get_share_mount_and_vol_from_vol_ref(existing_vol_ref)

        LOG.info(_LI("Asked to manage NFS volume %(vol)s, "
                     "with vol ref %(ref)s."),
                 {'vol': volume.id,
                  'ref': existing_vol_ref['source-name']})

        vol_id = utils.extract_id_from_volume_name(vol_name)
        if utils.check_already_managed_volume(vol_id):
            raise exception.ManageExistingAlreadyManaged(volume_ref=vol_name)

        self._check_pool_and_share(volume, nfs_share)

        if vol_name == volume.name:
            LOG.debug("New Cinder volume %(vol)s name matches reference name: "
                      "no need to rename.", {'vol': volume.name})
        else:
            src_vol = os.path.join(nfs_mount, vol_name)
            dst_vol = os.path.join(nfs_mount, volume.name)
            try:
                self._try_execute("mv", src_vol, dst_vol, run_as_root=False,
                                  check_exit_code=True)
                LOG.debug("Setting newly managed Cinder volume name "
                          "to %(vol)s.", {'vol': volume.name})
                self._set_rw_permissions_for_all(dst_vol)
            except (OSError, processutils.ProcessExecutionError) as err:
                msg = (_("Failed to manage existing volume "
                         "%(name)s, because rename operation "
                         "failed: Error msg: %(msg)s.") %
                       {'name': existing_vol_ref['source-name'],
                        'msg': six.text_type(err)})
                LOG.error(msg)
                raise exception.VolumeBackendAPIException(data=msg)
        return {'provider_location': nfs_share}
Ejemplo n.º 13
0
    def manage_existing(self, volume, existing_ref):
        """Manages an existing ZVOL.

        Renames the ZVOL to match the expected name for the volume.
        Error checking done by manage_existing_get_size is not repeated.
        """
        root_helper = utils.get_root_helper()
        zvol_name = existing_ref['source-name']
        zvol_src = self._zfs_volume({'name': zvol_name})
        zvol_dst = self._zfs_volume(volume)

        try:
            self._execute('zfs',
                          'list',
                          '-H',
                          zvol_src,
                          root_helper=root_helper,
                          run_as_root=True)
        except processutils.ProcessExecutionError as exc:
            exception_message = (_("Failed to retrive zvol %(name)s, "
                                   "error message was: %(err_msg)s") % {
                                       'name': zvol_name,
                                       'err_msg': exc.stderr
                                   })
            raise exception.VolumeBackendAPIException(data=exception_message)

        vol_id = volutils.extract_id_from_volume_name(zvol_name)
        if volutils.check_already_managed_volume(vol_id):
            raise exception.ManageExistingAlreadyManaged(volume_ref=zvol_name)

        # Attempt to rename the ZVOL to match the OpenStack internal name.
        try:
            self._execute('zfs',
                          'rename',
                          zvol_src,
                          zvol_dst,
                          root_helper=root_helper,
                          run_as_root=True)
        except processutils.ProcessExecutionError as exc:
            exception_message = (_("Failed to rename logical volume %(name)s, "
                                   "error message was: %(err_msg)s") % {
                                       'name': zvol_name,
                                       'err_msg': exc.stderr
                                   })
            raise exception.VolumeBackendAPIException(data=exception_message)
Ejemplo n.º 14
0
Archivo: lvm.py Proyecto: Datera/cinder
    def manage_existing(self, volume, existing_ref):
        """Manages an existing LV.

        Renames the LV to match the expected name for the volume.
        Error checking done by manage_existing_get_size is not repeated.
        """
        lv_name = existing_ref['source-name']
        self.vg.get_volume(lv_name)

        if volutils.check_already_managed_volume(self.db, lv_name):
            raise exception.ManageExistingAlreadyManaged(volume_ref=lv_name)

        # Attempt to rename the LV to match the OpenStack internal name.
        try:
            self.vg.rename_volume(lv_name, volume['name'])
        except processutils.ProcessExecutionError as exc:
            exception_message = (_("Failed to rename logical volume %(name)s, "
                                   "error message was: %(err_msg)s")
                                 % {'name': lv_name,
                                    'err_msg': exc.stderr})
            raise exception.VolumeBackendAPIException(
                data=exception_message)
Ejemplo n.º 15
0
    def manage_existing(self, volume, existing_ref):
        """Manages an existing LV.

        Renames the LV to match the expected name for the volume.
        Error checking done by manage_existing_get_size is not repeated.
        """
        lv_name = existing_ref['source-name']
        self.vg.get_volume(lv_name)

        if volutils.check_already_managed_volume(self.db, lv_name):
            raise exception.ManageExistingAlreadyManaged(volume_ref=lv_name)

        # Attempt to rename the LV to match the OpenStack internal name.
        try:
            self.vg.rename_volume(lv_name, volume['name'])
        except processutils.ProcessExecutionError as exc:
            exception_message = (_("Failed to rename logical volume %(name)s, "
                                   "error message was: %(err_msg)s") % {
                                       'name': lv_name,
                                       'err_msg': exc.stderr
                                   })
            raise exception.VolumeBackendAPIException(data=exception_message)
Ejemplo n.º 16
0
    def manage_existing_get_size(self, volume, existing_vol_ref):
        """Gets the size to manage_existing.

        Returns the size of volume to be managed by manage_existing.

        :param volume: cinder volume to manage
        :param existing_vol_ref: existing volume to take under management
        :returns: the size of the volume to be managed or raises error
        :raises: ManageExistingInvalidReference
        """
        # Check if the reference is valid.
        if 'source-name' not in existing_vol_ref:
            reason = _('Reference must contain source-name element.')
            raise exception.ManageExistingInvalidReference(
                existing_ref=existing_vol_ref, reason=reason)

        fs_label, vol_name = (self._get_info_from_vol_ref(
            existing_vol_ref['source-name']))

        LOG.debug("File System: %(fs_label)s "
                  "Volume name: %(vol_name)s.", {
                      'fs_label': fs_label,
                      'vol_name': vol_name
                  })

        if utils.check_already_managed_volume(vol_name):
            raise exception.ManageExistingAlreadyManaged(volume_ref=vol_name)

        lu_info = self.backend.get_existing_lu_info(vol_name, fs_label)

        if lu_info != {}:
            return lu_info['size']
        else:
            raise exception.ManageExistingInvalidReference(
                existing_ref=existing_vol_ref,
                reason=_('Volume not found on configured storage backend. '
                         'If your volume name contains "/", please rename it '
                         'and try to manage again.'))
Ejemplo n.º 17
0
    def manage_existing_get_size(self, volume, existing_vol_ref):
        """Gets the size to manage_existing.

        Returns the size of volume to be managed by manage_existing.

        :param volume: cinder volume to manage
        :param existing_vol_ref: existing volume to take under management
        :returns: the size of the volume to be managed or raises error
        :raises: ManageExistingInvalidReference
        """
        # Check if the reference is valid.
        if 'source-name' not in existing_vol_ref:
            reason = _('Reference must contain source-name element.')
            raise exception.ManageExistingInvalidReference(
                existing_ref=existing_vol_ref, reason=reason)

        fs_label, vol_name = (
            self._get_info_from_vol_ref(existing_vol_ref['source-name']))

        LOG.debug("File System: %(fs_label)s "
                  "Volume name: %(vol_name)s.",
                  {'fs_label': fs_label, 'vol_name': vol_name})

        if utils.check_already_managed_volume(vol_name):
            raise exception.ManageExistingAlreadyManaged(volume_ref=vol_name)

        lu_info = self.backend.get_existing_lu_info(vol_name, fs_label)

        if lu_info != {}:
            return lu_info['size']
        else:
            raise exception.ManageExistingInvalidReference(
                existing_ref=existing_vol_ref,
                reason=_('Volume not found on configured storage backend. '
                         'If your volume name contains "/", please rename it '
                         'and try to manage again.'))
Ejemplo n.º 18
0
    def manage_existing_get_size(self, volume, existing_vol_ref):
        """Returns the size of volume to be managed by manage_existing.

        When calculating the size, round up to the next GB.

        :param volume: cinder volume to manage
        :param existing_vol_ref: existing volume to take under management
        :returns: the size of the volume or raise error
        :raises: VolumeBackendAPIException
        """

        # Attempt to find NFS share, NFS mount, and volume path from vol_ref.
        (nfs_share, nfs_mount, vol_name
         ) = self._get_share_mount_and_vol_from_vol_ref(existing_vol_ref)

        LOG.debug("Asked to get size of NFS vol_ref %(ref)s.",
                  {'ref': existing_vol_ref['source-name']})

        if utils.check_already_managed_volume(vol_name):
            raise exception.ManageExistingAlreadyManaged(volume_ref=vol_name)

        try:
            file_path = os.path.join(nfs_mount, vol_name)
            file_size = float(cutils.get_file_size(file_path)) / units.Gi
            vol_size = int(math.ceil(file_size))
        except (OSError, ValueError):
            exception_message = (_("Failed to manage existing volume "
                                   "%(name)s, because of error in getting "
                                   "volume size."),
                                 {'name': existing_vol_ref['source-name']})
            raise exception.VolumeBackendAPIException(data=exception_message)

        LOG.debug("Reporting size of NFS volume ref %(ref)s as %(size)d GB.",
                  {'ref': existing_vol_ref['source-name'], 'size': vol_size})

        return vol_size
Ejemplo n.º 19
0
    def test_check_managed_volume_already_managed(self):
        mock_db = mock.Mock()

        result = volume_utils.check_already_managed_volume(
            mock_db, 'volume-d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1')
        self.assertTrue(result)
Ejemplo n.º 20
0
 def test_check_managed_volume_not_managed(self):
     result = volume_utils.check_already_managed_volume('test-volume')
     self.assertFalse(result)
Ejemplo n.º 21
0
 def test_check_managed_volume_not_managed_proper_uuid(self, exists_mock):
     id_ = 'd8cd1feb-2dcc-404d-9b15-b86fe3bec0a1'
     result = volume_utils.check_already_managed_volume(id_)
     self.assertFalse(result)
     exists_mock.assert_called_once_with(mock.ANY, models.Volume, id_)
Ejemplo n.º 22
0
 def test_check_managed_volume_not_managed_invalid_id(self):
     result = volume_utils.check_already_managed_volume(1)
     self.assertFalse(result)
     result = volume_utils.check_already_managed_volume('not-a-uuid')
     self.assertFalse(result)
    def test_check_managed_volume_already_managed(self):
        mock_db = mock.Mock()

        result = volume_utils.check_already_managed_volume(
            mock_db, 'volume-d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1')
        self.assertTrue(result)
Ejemplo n.º 24
0
 def test_check_managed_volume_not_managed_proper_uuid(self, exists_mock):
     id_ = 'd8cd1feb-2dcc-404d-9b15-b86fe3bec0a1'
     result = volume_utils.check_already_managed_volume(id_)
     self.assertFalse(result)
     exists_mock.assert_called_once_with(mock.ANY, models.Volume, id_)
Ejemplo n.º 25
0
 def test_check_managed_volume_not_managed_invalid_id(self):
     result = volume_utils.check_already_managed_volume(1)
     self.assertFalse(result)
     result = volume_utils.check_already_managed_volume('not-a-uuid')
     self.assertFalse(result)
Ejemplo n.º 26
0
 def test_check_managed_volume_already_managed(self, exists_mock):
     id_ = "d8cd1feb-2dcc-404d-9b15-b86fe3bec0a1"
     result = volume_utils.check_already_managed_volume(id_)
     self.assertTrue(result)
     exists_mock.assert_called_once_with(mock.ANY, models.Volume, id_)