Exemplo n.º 1
0
def deserialize_remote_exception(data, allowed_remote_exmods):
    failure = jsonutils.loads(six.text_type(data))

    trace = failure.get('tb', [])
    message = failure.get('message', "") + "\n" + "\n".join(trace)
    name = failure.get('class')
    module = failure.get('module')

    # the remote service which raised the given exception might have a
    # different python version than the caller. For example, the caller might
    # run python 2.7, while the remote service might run python 3.4. Thus,
    # the exception module will be "builtins" instead of "exceptions".
    if module in _EXCEPTIONS_MODULES:
        module = _EXCEPTIONS_MODULE

    # NOTE(ameade): We DO NOT want to allow just any module to be imported, in
    # order to prevent arbitrary code execution.
    if module != _EXCEPTIONS_MODULE and module not in allowed_remote_exmods:
        return oslo_messaging.RemoteError(name, failure.get('message'), trace)

    try:
        __import__(module)
        mod = sys.modules[module]
        klass = getattr(mod, name)
        if not issubclass(klass, Exception):
            raise TypeError("Can only deserialize Exceptions")

        failure = klass(*failure.get('args', []), **failure.get('kwargs', {}))
    except (AttributeError, TypeError, ImportError) as error:
        LOG.warning(_LW("Failed to rebuild remote exception due to error: %s"),
                    six.text_type(error))
        return oslo_messaging.RemoteError(name, failure.get('message'), trace)

    ex_type = type(failure)
    str_override = lambda self: message
    new_ex_type = type(ex_type.__name__ + _REMOTE_POSTFIX, (ex_type, ), {
        '__str__': str_override,
        '__unicode__': str_override
    })
    new_ex_type.__module__ = '%s%s' % (module, _REMOTE_POSTFIX)
    try:
        # NOTE(ameade): Dynamically create a new exception type and swap it in
        # as the new type for the exception. This only works on user defined
        # Exceptions and not core Python exceptions. This is important because
        # we cannot necessarily change an exception message so we must override
        # the __str__ method.
        failure.__class__ = new_ex_type
    except TypeError:
        # NOTE(ameade): If a core exception then just add the traceback to the
        # first exception argument.
        failure.args = (message, ) + failure.args[1:]
    return failure
Exemplo n.º 2
0
 def test_volume_force_detach_raises_db_error(self):
     # In case of DB error 500 error code is returned to user
     # admin context
     ctx = context.RequestContext('admin', 'fake', True)
     # current status is available
     volume = db.volume_create(
         ctx, {
             'status': 'available',
             'host': 'test',
             'provider_location': '',
             'size': 1
         })
     connector = {'initiator': 'iqn.2012-07.org.fake:01'}
     # start service to handle rpc messages for attach requests
     svc = self.start_service('volume', host='test')
     self.volume_api.reserve_volume(ctx, volume)
     mountpoint = '/dev/vbd'
     attachment = self.volume_api.attach(ctx, volume, stubs.FAKE_UUID, None,
                                         mountpoint, 'rw')
     # volume is attached
     volume = db.volume_get(ctx, volume['id'])
     self.assertEqual('in-use', volume['status'])
     self.assertEqual(stubs.FAKE_UUID, attachment['instance_uuid'])
     self.assertEqual(mountpoint, attachment['mountpoint'])
     self.assertEqual('attached', attachment['attach_status'])
     admin_metadata = volume['volume_admin_metadata']
     self.assertEqual(2, len(admin_metadata))
     self.assertEqual('readonly', admin_metadata[0]['key'])
     self.assertEqual('False', admin_metadata[0]['value'])
     self.assertEqual('attached_mode', admin_metadata[1]['key'])
     self.assertEqual('rw', admin_metadata[1]['value'])
     conn_info = self.volume_api.initialize_connection(
         ctx, volume, connector)
     self.assertEqual('rw', conn_info['data']['access_mode'])
     # build request to force detach
     volume_remote_error = \
         messaging.RemoteError(exc_type='DBError')
     with mock.patch.object(volume_api.API,
                            'detach',
                            side_effect=volume_remote_error):
         req = webob.Request.blank('/v2/fake/volumes/%s/action' %
                                   volume['id'])
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
         body = {
             'os-force_detach': {
                 'attachment_id': 'fake',
                 'connector': connector
             }
         }
         req.body = jsonutils.dump_as_bytes(body)
         # attach admin context to request
         req.environ['cinder.context'] = ctx
         # make request
         self.assertRaises(messaging.RemoteError, req.get_response, app())
     # cleanup
     svc.stop()
Exemplo n.º 3
0
    def __init__(self, pika_engine, channel, method, properties, body):
        """Defines default values of result and failure fields, call
        super.__init__ method and then construct Exception object if failure is
        not None

        :param pika_engine: PikaEngine, shared object with configuration and
            shared driver functionality
        :param channel: Channel, RabbitMQ channel which was used for
            this message delivery, used for sending ack back.
            If None - ack is not required
        :param method: Method, RabbitMQ message method
        :param properties: Properties, RabbitMQ message properties
        :param body: Bytes, RabbitMQ message body
        """
        super(RpcReplyPikaIncomingMessage, self).__init__(
            pika_engine, channel, method, properties, body
        )

        self.msg_id = properties.correlation_id

        self.result = self.message.get("s", None)
        self.failure = self.message.get("e", None)

        if self.failure is not None:
            trace = self.failure.get('t', [])
            message = self.failure.get('s', "")
            class_name = self.failure.get('c')
            module_name = self.failure.get('m')

            res_exc = None

            if module_name in pika_engine.allowed_remote_exmods:
                try:
                    module = importutils.import_module(module_name)
                    klass = getattr(module, class_name)

                    ex_type = type(
                        klass.__name__,
                        (RemoteExceptionMixin, klass),
                        {}
                    )

                    res_exc = ex_type(module_name, class_name, message, trace)
                except ImportError as e:
                    LOG.warning(
                        "Can not deserialize remote exception [module:%s, "
                        "class:%s]. %s", module_name, class_name, e
                    )

            # if we have not processed failure yet, use RemoteError class
            if res_exc is None:
                res_exc = oslo_messaging.RemoteError(
                    class_name, message, trace
                )
            self.failure = res_exc
Exemplo n.º 4
0
 def test_volume_detach_raises_remote_error(self):
     volume_remote_error = \
         messaging.RemoteError(exc_type='VolumeAttachmentNotFound')
     with mock.patch.object(volume_api.API,
                            'detach',
                            side_effect=volume_remote_error):
         id = 1
         vol = {"attachment_id": self.UUID}
         body = {"os-detach": vol}
         req = fakes.HTTPRequest.blank('/v2/tenant1/volumes/%s/action' % id)
         self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller._detach, req, id, body)
Exemplo n.º 5
0
 def test_volume_force_detach_raises_remote_error(self):
     # admin context
     ctx = context.RequestContext('admin', 'fake', True)
     # current status is available
     kwargs = {
         'status': 'available',
         'host': 'test',
         'provider_location': '',
         'size': 1,
         'availability_zone': 'fake_availability_zone',
         'attach_status': 'detached'
     }
     volume = self._create_volume(ctx, kwargs)
     connector = {'initiator': 'iqn.2012-07.org.fake:01'}
     # start service to handle rpc messages for attach requests
     svc = self.start_service('volume', host='test')
     self.volume_api.reserve_volume(ctx, volume)
     mountpoint = '/dev/vbd'
     attachment = self.volume_api.attach(ctx, volume, stubs.FAKE_UUID, None,
                                         mountpoint, 'rw')
     # volume is attached
     volume.refresh()
     self.assertEqual('in-use', volume.status)
     self.assertEqual(stubs.FAKE_UUID, attachment['instance_uuid'])
     self.assertEqual(mountpoint, attachment['mountpoint'])
     self.assertEqual('attached', attachment['attach_status'])
     admin_metadata = volume.admin_metadata
     self.assertEqual(2, len(admin_metadata))
     self.assertEqual('False', admin_metadata['readonly'])
     self.assertEqual('rw', admin_metadata['attached_mode'])
     conn_info = self.volume_api.initialize_connection(
         ctx, volume, connector)
     self.assertEqual('rw', conn_info['data']['access_mode'])
     # build request to force detach
     volume_remote_error = \
         messaging.RemoteError(exc_type='VolumeAttachmentNotFound')
     with mock.patch.object(volume_api.API,
                            'detach',
                            side_effect=volume_remote_error):
         req = webob.Request.blank('/v2/fake/volumes/%s/action' % volume.id)
         req.method = 'POST'
         req.headers['content-type'] = 'application/json'
         req.body = jsonutils.dumps(
             {'os-force_detach': {
                 'attachment_id': 'fake'
             }})
         # attach admin context to request
         req.environ['cinder.context'] = ctx
         # make request
         resp = req.get_response(app())
         self.assertEqual(400, resp.status_int)
     # cleanup
     svc.stop()
Exemplo n.º 6
0
 def test_volume_detach_raises_db_error(self):
     # In case of DB error 500 error code is returned to user
     volume_remote_error = \
         messaging.RemoteError(exc_type='DBError')
     with mock.patch.object(volume_api.API,
                            'detach',
                            side_effect=volume_remote_error):
         id = 1
         vol = {"attachment_id": self.UUID}
         body = {"os-detach": vol}
         req = fakes.HTTPRequest.blank('/v2/tenant1/volumes/%s/action' % id)
         self.assertRaises(messaging.RemoteError, self.controller._detach,
                           req, id, body)
Exemplo n.º 7
0
 def test_find_destination_with_remoteError(
         self, m_from_primitives, m_setup_instance_group,
         m_build_request_spec, m_get_image_from_system_metadata):
     m_get_image_from_system_metadata.return_value = {'properties': {}}
     m_build_request_spec.return_value = {}
     fake_spec = objects.RequestSpec()
     m_from_primitives.return_value = fake_spec
     with mock.patch.object(self.task.scheduler_client,
                            'select_destinations') as m_select_destinations:
         error = messaging.RemoteError()
         m_select_destinations.side_effect = error
         self.assertRaises(exception.MigrationSchedulerRPCError,
                           self.task._find_destination)
Exemplo n.º 8
0
    def test_volume_force_detach_raises_db_error(self):
        # In case of DB error 500 error code is returned to user
        # current status is available
        volume = self._create_volume(self.ctx, {
            'provider_location': '',
            'size': 1
        })
        connector = {'initiator': 'iqn.2012-07.org.fake:01'}

        self.volume_api.reserve_volume(self.ctx, volume)
        mountpoint = '/dev/vbd'
        attachment = self.volume_api.attach(self.ctx, volume, fake.INSTANCE_ID,
                                            None, mountpoint, 'rw')
        # volume is attached
        volume = db.volume_get(self.ctx, volume['id'])
        self.assertEqual('in-use', volume['status'])
        self.assertEqual(fake.INSTANCE_ID, attachment['instance_uuid'])
        self.assertEqual(mountpoint, attachment['mountpoint'])
        self.assertEqual('attached', attachment['attach_status'])
        admin_metadata = volume['volume_admin_metadata']
        self.assertEqual(2, len(admin_metadata))
        self.assertEqual('readonly', admin_metadata[0]['key'])
        self.assertEqual('False', admin_metadata[0]['value'])
        self.assertEqual('attached_mode', admin_metadata[1]['key'])
        self.assertEqual('rw', admin_metadata[1]['value'])
        conn_info = self.volume_api.initialize_connection(
            self.ctx, volume, connector)
        self.assertEqual('rw', conn_info['data']['access_mode'])
        # build request to force detach
        volume_remote_error = messaging.RemoteError(exc_type='DBError')
        with mock.patch.object(volume_api.API,
                               'detach',
                               side_effect=volume_remote_error):
            req = webob.Request.blank('/v2/%s/volumes/%s/action' %
                                      (fake.PROJECT_ID, volume['id']))
            req.method = 'POST'
            req.headers['content-type'] = 'application/json'
            body = {
                'os-force_detach': {
                    'attachment_id': fake.ATTACHMENT_ID,
                    'connector': connector
                }
            }
            req.body = jsonutils.dump_as_bytes(body)
            # attach admin context to request
            req.environ['cinder.context'] = self.ctx
            # make request
            self.assertRaises(messaging.RemoteError, req.get_response, app())
Exemplo n.º 9
0
 def test_volume_attach_to_instance_raises_remote_error(self):
     volume_remote_error = \
         messaging.RemoteError(exc_type='InvalidUUID')
     with mock.patch.object(volume_api.API,
                            'attach',
                            side_effect=volume_remote_error):
         id = 1
         vol = {
             "instance_uuid": self.UUID,
             "mountpoint": "/dev/vdc",
             "mode": "rw"
         }
         body = {"os-attach": vol}
         req = fakes.HTTPRequest.blank('/v2/tenant1/volumes/%s/action' % id)
         self.assertRaises(webob.exc.HTTPBadRequest,
                           self.controller._attach, req, id, body)
Exemplo n.º 10
0
 def test_volume_attach_to_instance_raises_db_error(self):
     # In case of DB error 500 error code is returned to user
     volume_remote_error = \
         messaging.RemoteError(exc_type='DBError')
     with mock.patch.object(volume_api.API,
                            'attach',
                            side_effect=volume_remote_error):
         id = 1
         vol = {
             "instance_uuid": self.UUID,
             "mountpoint": "/dev/vdc",
             "mode": "rw"
         }
         body = {"os-attach": vol}
         req = fakes.HTTPRequest.blank('/v2/tenant1/volumes/%s/action' % id)
         self.assertRaises(messaging.RemoteError, self.controller._attach,
                           req, id, body)
Exemplo n.º 11
0
class VolumeManageTest(test.TestCase):
    """Test cases for cinder/api/contrib/volume_manage.py

    The API extension adds a POST /os-volume-manage API that is passed a cinder
    host name, and a driver-specific reference parameter.  If everything
    is passed correctly, then the cinder.volume.api.API.manage_existing method
    is invoked to manage an existing storage object on the host.

    In this set of test cases, we are ensuring that the code correctly parses
    the request structure and raises the correct exceptions when things are not
    right, and calls down into cinder.volume.api.API.manage_existing with the
    correct arguments.
    """

    def setUp(self):
        super(VolumeManageTest, self).setUp()
        self._admin_ctxt = context.RequestContext(fake.USER_ID,
                                                  fake.PROJECT_ID,
                                                  is_admin=True)
        self._non_admin_ctxt = context.RequestContext(fake.USER_ID,
                                                      fake.PROJECT_ID,
                                                      is_admin=False)
        self.controller = volume_manage.VolumeManageController()

    def _get_resp_post(self, body):
        """Helper to execute a POST os-volume-manage API call."""
        req = webob.Request.blank('/v2/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = self._admin_ctxt
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(app())
        return res

    def _get_resp_post_v3(self, body, version):
        """Helper to execute a POST os-volume-manage API call."""
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = self._admin_ctxt
        req.headers["OpenStack-API-Version"] = "volume " + version
        req.api_version_request = api_version.APIVersionRequest(version)
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(app_v3())
        return res

    @ddt.data(False, True)
    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage)
    @mock.patch(
        'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
    def test_manage_volume_ok(self, cluster, mock_validate, mock_api_manage):
        """Test successful manage volume execution.

        Tests for correct operation when valid arguments are passed in the
        request body.  We ensure that cinder.volume.api.API.manage_existing got
        called with the correct arguments, and that we return the correct HTTP
        code to the caller.
        """
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref'}}
        # This will be ignored
        if cluster:
            body['volume']['cluster'] = 'cluster'
        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int)

        # Check that the manage API was called with the correct arguments.
        self.assertEqual(1, mock_api_manage.call_count)
        args = mock_api_manage.call_args[0]
        self.assertEqual(body['volume']['host'], args[1])
        self.assertIsNone(args[2])  # Cluster argument
        self.assertEqual(body['volume']['ref'], args[3])
        self.assertTrue(mock_validate.called)

    def _get_resp_create(self, body, version='3.0'):
        url = '/v3/%s/os-volume-manage' % fake.PROJECT_ID
        req = webob.Request.blank(url, base_url='http://localhost.com' + url)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = self._admin_ctxt
        req.body = jsonutils.dump_as_bytes(body)
        req.headers = {'OpenStack-API-Version': 'volume %s' % version}
        req.api_version_request = api_version.APIVersionRequest(version)
        res = self.controller.create(req, body)
        return res

    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage)
    @mock.patch(
        'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
    def test_manage_volume_ok_cluster(self, mock_validate, mock_api_manage):
        body = {'volume': {'cluster': 'cluster',
                           'ref': 'fake_ref'}}
        res = self._get_resp_create(body, '3.16')
        self.assertEqual(['volume'], list(res.keys()))

        # Check that the manage API was called with the correct arguments.
        self.assertEqual(1, mock_api_manage.call_count)
        args = mock_api_manage.call_args[0]
        self.assertIsNone(args[1])
        self.assertEqual(body['volume']['cluster'], args[2])
        self.assertEqual(body['volume']['ref'], args[3])
        self.assertTrue(mock_validate.called)

    @mock.patch(
        'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
    def test_manage_volume_fail_host_cluster(self, mock_validate):
        body = {'volume': {'host': 'host_ok',
                           'cluster': 'cluster',
                           'ref': 'fake_ref'}}
        self.assertRaises(exception.InvalidInput,
                          self._get_resp_create, body, '3.16')

    def test_manage_volume_missing_host(self):
        """Test correct failure when host is not specified."""
        body = {'volume': {'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    @mock.patch('cinder.objects.Service.get_by_args')
    def test_manage_volume_service_not_found_on_host(self, mock_service):
        """Test correct failure when host having no volume service on it."""
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref'}}
        mock_service.side_effect = exception.ServiceNotFound(
            service_id='cinder-volume',
            host='host_ok')
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def test_manage_volume_missing_ref(self):
        """Test correct failure when the ref is not specified."""
        body = {'volume': {'host': 'host_ok'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def test_manage_volume_with_invalid_bootable(self):
        """Test correct failure when invalid bool value is specified."""
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref',
                           'bootable': 'InvalidBool'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    @mock.patch('cinder.objects.service.Service.is_up', return_value=True,
                new_callable=mock.PropertyMock)
    def test_manage_volume_disabled(self, mock_is_up):
        """Test manage volume failure due to disabled service."""
        body = {'volume': {'host': 'host_disabled', 'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_is_up.assert_not_called()

    @mock.patch('cinder.objects.service.Service.is_up', return_value=False,
                new_callable=mock.PropertyMock)
    def test_manage_volume_is_down(self, mock_is_up):
        """Test manage volume failure due to down service."""
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        self.assertTrue(mock_is_up.called)

    @mock.patch('cinder.volume.api.API.manage_existing', api_manage)
    @mock.patch(
        'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
    def test_manage_volume_volume_type_by_uuid(self, mock_validate):
        """Tests for correct operation when a volume type is specified by ID.

        We wrap cinder.volume.api.API.manage_existing so that managing is not
        actually attempted.
        """
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref',
                           'volume_type': fake.VOLUME_TYPE_ID,
                           'bootable': True}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int)
        self.assertTrue(mock_validate.called)

    @mock.patch('cinder.volume.api.API.manage_existing', api_manage)
    @mock.patch(
        'cinder.api.openstack.wsgi.Controller.validate_name_and_description')
    def test_manage_volume_volume_type_by_name(self, mock_validate):
        """Tests for correct operation when a volume type is specified by name.

        We wrap cinder.volume.api.API.manage_existing so that managing is not
        actually attempted.
        """
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref',
                           'volume_type': 'good_fakevt'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int)
        self.assertTrue(mock_validate.called)

    def test_manage_volume_bad_volume_type_by_uuid(self):
        """Test failure on nonexistent volume type specified by ID."""
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref',
                           'volume_type': fake.WILL_NOT_BE_FOUND_ID}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def test_manage_volume_bad_volume_type_by_name(self):
        """Test failure on nonexistent volume type specified by name."""
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref',
                           'volume_type': 'bad_fakevt'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def _get_resp_get(self, host, detailed, paging, admin=True):
        """Helper to execute a GET os-volume-manage API call."""
        params = {'host': host}
        if paging:
            params.update({'marker': '1234', 'limit': 10,
                           'offset': 4, 'sort': 'reference:asc'})
        query_string = "?%s" % urlencode(params)
        detail = ""
        if detailed:
            detail = "/detail"
        url = "/v2/%s/os-volume-manage%s%s" % (fake.PROJECT_ID, detail,
                                               query_string)
        req = webob.Request.blank(url)
        req.method = 'GET'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = (self._admin_ctxt if admin
                                         else self._non_admin_ctxt)
        res = req.get_response(app())
        return res

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                wraps=api_get_manageable_volumes)
    def test_get_manageable_volumes_non_admin(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', False, False, admin=False)
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
        mock_api_manageable.assert_not_called()
        res = self._get_resp_get('fakehost', True, False, admin=False)
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
        mock_api_manageable.assert_not_called()

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                wraps=api_get_manageable_volumes)
    def test_get_manageable_volumes_ok(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', False, True)
        exp = {'manageable-volumes':
               [{'reference':
                 {'source-name':
                  'volume-%s' % fake.VOLUME_ID},
                 'size': 4, 'safe_to_manage': False},
                {'reference': {'source-name': 'myvol'},
                 'size': 5, 'safe_to_manage': True}]}
        self.assertEqual(http_client.OK, res.status_int)
        self.assertEqual(exp, jsonutils.loads(res.body))
        mock_api_manageable.assert_called_once_with(
            self._admin_ctxt, 'fakehost', None, limit=10, marker='1234',
            offset=4, sort_dirs=['asc'], sort_keys=['reference'])

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                side_effect=messaging.RemoteError(
                    exc_type='InvalidInput', value='marker not found: 1234'))
    def test_get_manageable_volumes_non_existent_marker(self,
                                                        mock_api_manageable):
        res = self._get_resp_get('fakehost', detailed=False, paging=True)
        self.assertEqual(400, res.status_int)
        self.assertTrue(mock_api_manageable.called)

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                wraps=api_get_manageable_volumes)
    def test_get_manageable_volumes_detailed_ok(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', True, False)
        exp = {'manageable-volumes':
               [{'reference': {'source-name': 'volume-%s' % fake.VOLUME_ID},
                 'size': 4, 'reason_not_safe': 'volume in use',
                 'cinder_id': fake.VOLUME_ID, 'safe_to_manage': False,
                 'extra_info': 'qos_setting:high'},
                {'reference': {'source-name': 'myvol'}, 'cinder_id': None,
                 'size': 5, 'reason_not_safe': None, 'safe_to_manage': True,
                 'extra_info': 'qos_setting:low'}]}
        self.assertEqual(http_client.OK, res.status_int)
        self.assertEqual(exp, jsonutils.loads(res.body))
        mock_api_manageable.assert_called_once_with(
            self._admin_ctxt, 'fakehost', None, limit=CONF.osapi_max_limit,
            marker=None, offset=0, sort_dirs=['desc'],
            sort_keys=['reference'])

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                side_effect=messaging.RemoteError(
                    exc_type='InvalidInput', value='marker not found: 1234'))
    def test_get_manageable_volumes_non_existent_marker_detailed(
            self, mock_api_manageable):
        res = self._get_resp_get('fakehost', detailed=True, paging=True)
        self.assertEqual(400, res.status_int)
        self.assertTrue(mock_api_manageable.called)

    @ddt.data({'a' * 256: 'a'},
              {'a': 'a' * 256},
              {'': 'a'},
              )
    def test_manage_volume_with_invalid_metadata(self, value):
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref',
                           "metadata": value}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    @mock.patch('cinder.objects.service.Service.is_up', return_value=True,
                new_callable=mock.PropertyMock)
    def test_get_manageable_volumes_disabled(self, mock_is_up):
        res = self._get_resp_get('host_disabled', False, True)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_is_up.assert_not_called()

    @mock.patch('cinder.objects.service.Service.is_up', return_value=False,
                new_callable=mock.PropertyMock)
    def test_get_manageable_volumes_is_down(self, mock_is_up):
        res = self._get_resp_get('host_ok', False, True)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        self.assertTrue(mock_is_up.called)

    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage_new)
    def test_manage_volume_with_creating_status_in_v3(self, mock_api_manage):
        """Test managing volume to return 'creating' status in V3 API."""
        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref'}}
        res = self._get_resp_post_v3(body, '3.15')
        self.assertEqual(http_client.ACCEPTED, res.status_int)
        self.assertEqual(1, mock_api_manage.call_count)
        self.assertEqual('creating',
                         jsonutils.loads(res.body)['volume']['status'])

    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage_new)
    def test_manage_volume_with_creating_status_in_v2(self, mock_api_manage):
        """Test managing volume to return 'creating' status in V2 API."""

        body = {'volume': {'host': 'host_ok',
                           'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int)
        self.assertEqual(1, mock_api_manage.call_count)
        self.assertEqual('creating',
                         jsonutils.loads(res.body)['volume']['status'])
Exemplo n.º 12
0
 def test_fetch_trunk_plugin_other_error(self):
     self.stub.get_trunk_details.side_effect = (
         oslo_messaging.RemoteError('vacuum full'))
     with testtools.ExpectedException(oslo_messaging.RemoteError):
         self.tapi._fetch_trunk('ctx', 'port')
Exemplo n.º 13
0
 def test_fetch_trunk_plugin_disabled(self):
     self.stub.get_trunk_details.side_effect = (
         oslo_messaging.RemoteError('CallbackNotFound'))
     self.assertIsNone(self.tapi._fetch_trunk('ctx', 'port'))
Exemplo n.º 14
0
    def test_fetch_trunk_resource_remote_error(self):
        self.trunk_driver._trunk_rpc.get_trunk_details.side_effect = (
            oslo_messaging.RemoteError('expected CallbackNotFound'))

        trunk = self.trunk_driver._fetch_trunk(mock.sentinel.port_id)
        self.assertIsNone(trunk)
Exemplo n.º 15
0
class SnapshotManageTest(test.TestCase):
    """Test cases for cinder/api/contrib/snapshot_manage.py

    The API extension adds a POST /os-snapshot-manage API that is passed a
    cinder volume id, and a driver-specific reference parameter.
    If everything is passed correctly,
    then the cinder.volume.api.API.manage_existing_snapshot method
    is invoked to manage an existing storage object on the host.

    In this set of test cases, we are ensuring that the code correctly parses
    the request structure and raises the correct exceptions when things are not
    right, and calls down into cinder.volume.api.API.manage_existing_snapshot
    with the correct arguments.
    """

    def setUp(self):
        super(SnapshotManageTest, self).setUp()
        self._admin_ctxt = context.RequestContext(fake.USER_ID,
                                                  fake.PROJECT_ID,
                                                  is_admin=True)
        self._non_admin_ctxt = context.RequestContext(fake.USER_ID,
                                                      fake.PROJECT_ID,
                                                      is_admin=False)

    def _get_resp_post(self, body):
        """Helper to execute an os-snapshot-manage API call."""
        req = webob.Request.blank('/v2/%s/os-snapshot-manage' %
                                  fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = self._admin_ctxt
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(app())
        return res

    @mock.patch('cinder.volume.rpcapi.VolumeAPI.manage_existing_snapshot')
    @mock.patch('cinder.volume.api.API.create_snapshot_in_db')
    @mock.patch('cinder.db.sqlalchemy.api.service_get')
    def test_manage_snapshot_ok(self, mock_db,
                                mock_create_snapshot, mock_rpcapi):
        """Test successful manage snapshot execution.

        Tests for correct operation when valid arguments are passed in the
        request body. We ensure that cinder.volume.api.API.manage_existing got
        called with the correct arguments, and that we return the correct HTTP
        code to the caller.
        """
        mock_db.return_value = fake_service.fake_service_obj(
            self._admin_ctxt,
            binary=constants.VOLUME_BINARY)

        body = {'snapshot': {'volume_id': fake.VOLUME_ID,
                             'ref': {'fake_key': 'fake_ref'}}}

        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int, res)

        # Check the db.service_get was called with correct arguments.
        mock_db.assert_called_once_with(
            mock.ANY, None, host='fake_host', binary=constants.VOLUME_BINARY,
            cluster_name=None)

        # Check the create_snapshot_in_db was called with correct arguments.
        self.assertEqual(1, mock_create_snapshot.call_count)
        args = mock_create_snapshot.call_args[0]
        named_args = mock_create_snapshot.call_args[1]
        self.assertEqual(fake.VOLUME_ID, args[1].get('id'))
        self.assertTrue(named_args['commit_quota'])

        # Check the volume_rpcapi.manage_existing_snapshot was called with
        # correct arguments.
        self.assertEqual(1, mock_rpcapi.call_count)
        args = mock_rpcapi.call_args[0]
        self.assertEqual({u'fake_key': u'fake_ref'}, args[2])

    @mock.patch('cinder.volume.rpcapi.VolumeAPI.manage_existing_snapshot')
    @mock.patch('cinder.volume.api.API.create_snapshot_in_db')
    @mock.patch('cinder.objects.service.Service.get_by_id')
    def test_manage_snapshot_ok_with_metadata_null(
            self, mock_db, mock_create_snapshot, mock_rpcapi):
        mock_db.return_value = fake_service.fake_service_obj(
            self._admin_ctxt,
            binary=constants.VOLUME_BINARY)
        body = {'snapshot': {'volume_id': fake.VOLUME_ID,
                             'ref': {'fake_key': 'fake_ref'},
                             'name': 'test',
                             'description': 'test',
                             'metadata': None}}

        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int)
        args = mock_create_snapshot.call_args[0]
        # 5th argument of args is metadata.
        self.assertIsNone(args[5])

    @mock.patch('cinder.volume.rpcapi.VolumeAPI.manage_existing_snapshot')
    @mock.patch('cinder.volume.api.API.create_snapshot_in_db')
    @mock.patch('cinder.db.sqlalchemy.api.service_get')
    def test_manage_snapshot_ok_ref_as_string(self, mock_db,
                                              mock_create_snapshot,
                                              mock_rpcapi):

        mock_db.return_value = fake_service.fake_service_obj(
            self._admin_ctxt,
            binary=constants.VOLUME_BINARY)

        body = {'snapshot': {'volume_id': fake.VOLUME_ID,
                             'ref': "string"}}

        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int, res)

        # Check the volume_rpcapi.manage_existing_snapshot was called with
        # correct arguments.
        self.assertEqual(1, mock_rpcapi.call_count)
        args = mock_rpcapi.call_args[0]
        self.assertEqual(body['snapshot']['ref'], args[2])

    @mock.patch('cinder.objects.service.Service.is_up',
                return_value=True,
                new_callable=mock.PropertyMock)
    @mock.patch('cinder.volume.rpcapi.VolumeAPI.manage_existing_snapshot')
    @mock.patch('cinder.volume.api.API.create_snapshot_in_db')
    @mock.patch('cinder.db.sqlalchemy.api.service_get')
    def test_manage_snapshot_disabled(self, mock_db, mock_create_snapshot,
                                      mock_rpcapi, mock_is_up):
        """Test manage snapshot failure due to disabled service."""
        mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt,
                                                             disabled=True)
        body = {'snapshot': {'volume_id': fake.VOLUME_ID, 'ref': {
            'fake_key': 'fake_ref'}}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_create_snapshot.assert_not_called()
        mock_rpcapi.assert_not_called()
        mock_is_up.assert_not_called()

    @mock.patch('cinder.objects.service.Service.is_up', return_value=False,
                new_callable=mock.PropertyMock)
    @mock.patch('cinder.volume.rpcapi.VolumeAPI.manage_existing_snapshot')
    @mock.patch('cinder.volume.api.API.create_snapshot_in_db')
    @mock.patch('cinder.db.sqlalchemy.api.service_get')
    def test_manage_snapshot_is_down(self, mock_db, mock_create_snapshot,
                                     mock_rpcapi, mock_is_up):
        """Test manage snapshot failure due to down service."""
        mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt)
        body = {'snapshot': {'volume_id': fake.VOLUME_ID,
                             'ref': {'fake_key': 'fake_ref'}}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_create_snapshot.assert_not_called()
        mock_rpcapi.assert_not_called()
        self.assertTrue(mock_is_up.called)

    def test_manage_snapshot_missing_volume_id(self):
        """Test correct failure when volume_id is not specified."""
        body = {'snapshot': {'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def test_manage_snapshot_missing_ref(self):
        """Test correct failure when the ref is not specified."""
        body = {'snapshot': {'volume_id': fake.VOLUME_ID}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def test_manage_snapshot_error_body(self):
        """Test correct failure when body is invaild."""
        body = {'error_snapshot': {'volume_id': fake.VOLUME_ID}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def test_manage_snapshot_error_volume_id(self):
        """Test correct failure when volume can't be found."""
        body = {'snapshot': {'volume_id': 'error_volume_id', 'ref': {}}}
        res = self._get_resp_post(body)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)

    def _get_resp_get(self, host, detailed, paging, admin=True):
        """Helper to execute a GET os-snapshot-manage API call."""
        params = {'host': host}
        if paging:
            params.update({'marker': '1234', 'limit': 10,
                           'offset': 4, 'sort': 'reference:asc'})
        query_string = "?%s" % urlencode(params)
        detail = ""
        if detailed:
            detail = "/detail"
        url = "/v2/%s/os-snapshot-manage%s%s" % (fake.PROJECT_ID, detail,
                                                 query_string)
        req = webob.Request.blank(url)
        req.method = 'GET'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = (self._admin_ctxt if admin
                                         else self._non_admin_ctxt)
        res = req.get_response(app())
        return res

    @mock.patch('cinder.volume.api.API.get_manageable_snapshots',
                wraps=api_get_manageable_snapshots)
    def test_get_manageable_snapshots_non_admin(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', False, False, admin=False)
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
        self.assertEqual(False, mock_api_manageable.called)
        res = self._get_resp_get('fakehost', True, False, admin=False)
        self.assertEqual(http_client.FORBIDDEN, res.status_int)
        self.assertEqual(False, mock_api_manageable.called)

    @mock.patch('cinder.volume.api.API.get_manageable_snapshots',
                wraps=api_get_manageable_snapshots)
    def test_get_manageable_snapshots_ok(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', False, False)
        snap_name = 'snapshot-ffffffff-0000-ffff-0000-ffffffffffff'
        exp = {'manageable-snapshots':
               [{'reference': {'source-name': snap_name}, 'size': 4,
                 'safe_to_manage': False,
                 'source_reference':
                 {'source-name': 'volume-00000000-ffff-0000-ffff-000000'}},
                {'reference': {'source-name': 'mysnap'}, 'size': 5,
                 'safe_to_manage': True,
                 'source_reference': {'source-name': 'myvol'}}]}
        self.assertEqual(http_client.OK, res.status_int)
        self.assertEqual(jsonutils.loads(res.body), exp)
        mock_api_manageable.assert_called_once_with(
            self._admin_ctxt, 'fakehost', None, limit=CONF.osapi_max_limit,
            marker=None, offset=0, sort_dirs=['desc'],
            sort_keys=['reference'])

    @mock.patch('cinder.volume.api.API.get_manageable_snapshots',
                side_effect=messaging.RemoteError(
                    exc_type='InvalidInput', value='marker not found: 1234'))
    def test_get_manageable_snapshots_non_existent_marker(
            self, mock_api_manageable):
        res = self._get_resp_get('fakehost', detailed=False, paging=True)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)
        self.assertTrue(mock_api_manageable.called)

    @mock.patch('cinder.volume.api.API.get_manageable_snapshots',
                wraps=api_get_manageable_snapshots)
    def test_get_manageable_snapshots_detailed_ok(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', True, True)
        snap_id = 'ffffffff-0000-ffff-0000-ffffffffffff'
        exp = {'manageable-snapshots':
               [{'reference': {'source-name': 'snapshot-%s' % snap_id},
                 'size': 4, 'safe_to_manage': False, 'cinder_id': snap_id,
                 'reason_not_safe': 'snapshot in use',
                 'extra_info': 'qos_setting:high',
                 'source_reference':
                 {'source-name': 'volume-00000000-ffff-0000-ffff-000000'}},
                {'reference': {'source-name': 'mysnap'}, 'size': 5,
                 'cinder_id': None, 'safe_to_manage': True,
                 'reason_not_safe': None, 'extra_info': 'qos_setting:low',
                 'source_reference': {'source-name': 'myvol'}}]}
        self.assertEqual(http_client.OK, res.status_int)
        self.assertEqual(jsonutils.loads(res.body), exp)
        mock_api_manageable.assert_called_once_with(
            self._admin_ctxt, 'fakehost', None, limit=10, marker='1234',
            offset=4, sort_dirs=['asc'], sort_keys=['reference'])

    @mock.patch('cinder.volume.api.API.get_manageable_snapshots',
                side_effect=messaging.RemoteError(
                    exc_type='InvalidInput', value='marker not found: 1234'))
    def test_get_manageable_snapshots_non_existent_marker_detailed(
            self, mock_api_manageable):
        res = self._get_resp_get('fakehost', detailed=True, paging=True)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int)
        self.assertTrue(mock_api_manageable.called)

    @mock.patch('cinder.objects.service.Service.is_up', return_value=True)
    @mock.patch('cinder.db.sqlalchemy.api.service_get')
    def test_get_manageable_snapshots_disabled(self, mock_db, mock_is_up):
        mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt,
                                                             disabled=True)
        res = self._get_resp_get('host_ok', False, True)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_is_up.assert_not_called()

    @mock.patch('cinder.objects.service.Service.is_up', return_value=False,
                new_callable=mock.PropertyMock)
    @mock.patch('cinder.db.sqlalchemy.api.service_get')
    def test_get_manageable_snapshots_is_down(self, mock_db, mock_is_up):
        mock_db.return_value = fake_service.fake_service_obj(self._admin_ctxt)
        res = self._get_resp_get('host_ok', False, True)
        self.assertEqual(http_client.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        self.assertTrue(mock_is_up.called)

    @mock.patch('cinder.volume.rpcapi.VolumeAPI.manage_existing_snapshot')
    @mock.patch('cinder.volume.api.API.create_snapshot_in_db')
    @mock.patch('cinder.objects.service.Service.get_by_id')
    def test_manage_snapshot_with_null_validate(
            self, mock_db, mock_create_snapshot, mock_rpcapi):
        mock_db.return_value = fake_service.fake_service_obj(
            self._admin_ctxt,
            binary=constants.VOLUME_BINARY)
        body = {'snapshot': {'volume_id': fake.VOLUME_ID,
                             'ref': {'fake_key': 'fake_ref'},
                             'name': None,
                             'description': None}}

        res = self._get_resp_post(body)
        self.assertEqual(http_client.ACCEPTED, res.status_int, res)
        self.assertIn('snapshot', jsonutils.loads(res.body))
Exemplo n.º 16
0
class VolumeManageTest(test.TestCase):
    """Test cases for cinder/api/contrib/volume_manage.py

    The API extension adds a POST /os-volume-manage API that is passed a cinder
    host name, and a driver-specific reference parameter.  If everything
    is passed correctly, then the cinder.volume.api.API.manage_existing method
    is invoked to manage an existing storage object on the host.

    In this set of test cases, we are ensuring that the code correctly parses
    the request structure and raises the correct exceptions when things are not
    right, and calls down into cinder.volume.api.API.manage_existing with the
    correct arguments.
    """
    def setUp(self):
        super(VolumeManageTest, self).setUp()
        self._admin_ctxt = context.RequestContext(fake.USER_ID,
                                                  fake.PROJECT_ID,
                                                  is_admin=True)
        self._non_admin_ctxt = context.RequestContext(fake.USER_ID,
                                                      fake.PROJECT_ID,
                                                      is_admin=False)
        self.controller = volume_manage.VolumeManageController()

    def _get_resp_post(self, body, version='3.11'):
        """Helper to execute a POST os-volume-manage API call."""
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = self._admin_ctxt
        req.headers["OpenStack-API-Version"] = "volume " + version
        req.api_version_request = api_version.APIVersionRequest(version)
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(app())
        return res

    @ddt.data({'host': 'host_ok'}, {'host': 'user@host#backend:/vol_path'},
              {'host': 'host@backend#parts+of+pool'},
              {'host': 'host@backend#[dead:beef::cafe]:/vol01'})
    @ddt.unpack
    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage)
    def test_manage_volume_ok(self, mock_api_manage, host):
        """Test successful manage volume execution.

        Tests for correct operation when valid arguments are passed in the
        request body.  We ensure that cinder.volume.api.API.manage_existing got
        called with the correct arguments, and that we return the correct HTTP
        code to the caller.
        """
        body = {'volume': {'host': host, 'ref': 'fake_ref'}}

        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.ACCEPTED, res.status_int)

        # Check that the manage API was called with the correct arguments.
        self.assertEqual(1, mock_api_manage.call_count)
        args = mock_api_manage.call_args[0]
        self.assertEqual(body['volume']['host'], args[1])
        self.assertEqual(body['volume']['ref'], args[3])

    def test_manage_volume_not_ok(self):
        """Test not successful manage volume execution.

        Tests for error raised when invalid arguments are passed in the
        request body.
        """
        body = {'volume': {'host': 'host not ok', 'ref': 'fake_ref'}}

        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def _get_resp_create(self, body, version=mv.BASE_VERSION):
        url = '/v3/%s/os-volume-manage' % fake.PROJECT_ID
        req = webob.Request.blank(url, base_url='http://localhost.com' + url)
        req.method = 'POST'
        req.headers = mv.get_mv_header(version)
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = self._admin_ctxt
        req.body = jsonutils.dump_as_bytes(body)
        req.api_version_request = mv.get_api_version(version)
        res = self.controller.create(req, body=body)
        return res

    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage)
    def test_manage_volume_ok_cluster(self, mock_api_manage):
        body = {'volume': {'cluster': 'cluster', 'ref': 'fake_ref'}}
        res = self._get_resp_create(body, mv.VOLUME_MIGRATE_CLUSTER)
        self.assertEqual(['volume'], list(res.keys()))

        # Check that the manage API was called with the correct arguments.
        self.assertEqual(1, mock_api_manage.call_count)
        args = mock_api_manage.call_args[0]
        self.assertIsNone(args[1])
        self.assertEqual(body['volume']['cluster'], args[2])
        self.assertEqual(body['volume']['ref'], args[3])

    def test_manage_volume_fail_host_cluster(self):
        body = {
            'volume': {
                'host': 'host_ok',
                'cluster': 'cluster',
                'ref': 'fake_ref'
            }
        }
        self.assertRaises(exception.InvalidInput, self._get_resp_create, body,
                          mv.VOLUME_MIGRATE_CLUSTER)

    def test_manage_volume_missing_host(self):
        """Test correct failure when host is not specified."""
        body = {'volume': {'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    @mock.patch('cinder.objects.Service.get_by_args')
    def test_manage_volume_service_not_found_on_host(self, mock_service):
        """Test correct failure when host having no volume service on it."""
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        mock_service.side_effect = exception.ServiceNotFound(
            service_id='cinder-volume', host='host_ok')
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def test_manage_volume_missing_ref(self):
        """Test correct failure when the ref is not specified."""
        body = {'volume': {'host': 'host_ok'}}
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def test_manage_volume_with_invalid_bootable(self):
        """Test correct failure when invalid bool value is specified."""
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'bootable': 'InvalidBool'
            }
        }
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    @mock.patch('cinder.objects.service.Service.is_up',
                return_value=True,
                new_callable=mock.PropertyMock)
    def test_manage_volume_disabled(self, mock_is_up):
        """Test manage volume failure due to disabled service."""
        body = {'volume': {'host': 'host_disabled', 'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_is_up.assert_not_called()

    @mock.patch('cinder.objects.service.Service.is_up',
                return_value=False,
                new_callable=mock.PropertyMock)
    def test_manage_volume_is_down(self, mock_is_up):
        """Test manage volume failure due to down service."""
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        self.assertTrue(mock_is_up.called)

    @mock.patch('cinder.volume.api.API.manage_existing', api_manage)
    def test_manage_volume_volume_type_by_uuid(self):
        """Tests for correct operation when a volume type is specified by ID.

        We wrap cinder.volume.api.API.manage_existing so that managing is not
        actually attempted.
        """
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'volume_type': fake.VOLUME_TYPE_ID,
                'bootable': True
            }
        }
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.ACCEPTED, res.status_int)

    @mock.patch('cinder.volume.api.API.manage_existing', api_manage)
    def test_manage_volume_volume_type_by_name(self):
        """Tests for correct operation when a volume type is specified by name.

        We wrap cinder.volume.api.API.manage_existing so that managing is not
        actually attempted.
        """
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'volume_type': 'good_fakevt'
            }
        }
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.ACCEPTED, res.status_int)

    def test_manage_volume_bad_volume_type_by_uuid(self):
        """Test failure on nonexistent volume type specified by ID."""
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'volume_type': fake.WILL_NOT_BE_FOUND_ID
            }
        }
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def test_manage_volume_bad_volume_type_by_name(self):
        """Test failure on nonexistent volume type specified by name."""
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'volume_type': 'bad_fakevt'
            }
        }
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def _get_resp_get(self, host, detailed, paging, admin=True):
        """Helper to execute a GET os-volume-manage API call."""
        params = {'host': host}
        if paging:
            params.update({
                'marker': '1234',
                'limit': 10,
                'offset': 4,
                'sort': 'reference:asc'
            })
        query_string = "?%s" % urlencode(params)
        detail = ""
        if detailed:
            detail = "/detail"
        url = "/v3/%s/os-volume-manage%s%s" % (fake.PROJECT_ID, detail,
                                               query_string)
        req = webob.Request.blank(url)
        req.method = 'GET'
        req.headers['Content-Type'] = 'application/json'
        req.environ['cinder.context'] = (self._admin_ctxt
                                         if admin else self._non_admin_ctxt)
        res = req.get_response(app())
        return res

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                wraps=api_get_manageable_volumes)
    def test_get_manageable_volumes_non_admin(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', False, False, admin=False)
        self.assertEqual(HTTPStatus.FORBIDDEN, res.status_int)
        mock_api_manageable.assert_not_called()
        res = self._get_resp_get('fakehost', True, False, admin=False)
        self.assertEqual(HTTPStatus.FORBIDDEN, res.status_int)
        mock_api_manageable.assert_not_called()

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                wraps=api_get_manageable_volumes)
    def test_get_manageable_volumes_ok(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', False, True)
        exp = {
            'manageable-volumes': [{
                'reference': {
                    'source-name': 'volume-%s' % fake.VOLUME_ID
                },
                'size': 4,
                'safe_to_manage': False
            }, {
                'reference': {
                    'source-name': 'myvol'
                },
                'size': 5,
                'safe_to_manage': True
            }]
        }
        self.assertEqual(HTTPStatus.OK, res.status_int)
        self.assertEqual(exp, jsonutils.loads(res.body))
        mock_api_manageable.assert_called_once_with(self._admin_ctxt,
                                                    'fakehost',
                                                    None,
                                                    limit=10,
                                                    marker='1234',
                                                    offset=4,
                                                    sort_dirs=['asc'],
                                                    sort_keys=['reference'])

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                side_effect=messaging.RemoteError(
                    exc_type='InvalidInput', value='marker not found: 1234'))
    def test_get_manageable_volumes_non_existent_marker(
            self, mock_api_manageable):
        res = self._get_resp_get('fakehost', detailed=False, paging=True)
        self.assertEqual(400, res.status_int)
        self.assertTrue(mock_api_manageable.called)

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                wraps=api_get_manageable_volumes)
    def test_get_manageable_volumes_detailed_ok(self, mock_api_manageable):
        res = self._get_resp_get('fakehost', True, False)
        exp = {
            'manageable-volumes': [{
                'reference': {
                    'source-name': 'volume-%s' % fake.VOLUME_ID
                },
                'size': 4,
                'reason_not_safe': 'volume in use',
                'cinder_id': fake.VOLUME_ID,
                'safe_to_manage': False,
                'extra_info': 'qos_setting:high'
            }, {
                'reference': {
                    'source-name': 'myvol'
                },
                'cinder_id': None,
                'size': 5,
                'reason_not_safe': None,
                'safe_to_manage': True,
                'extra_info': 'qos_setting:low'
            }]
        }
        self.assertEqual(HTTPStatus.OK, res.status_int)
        self.assertEqual(exp, jsonutils.loads(res.body))
        mock_api_manageable.assert_called_once_with(self._admin_ctxt,
                                                    'fakehost',
                                                    None,
                                                    limit=CONF.osapi_max_limit,
                                                    marker=None,
                                                    offset=0,
                                                    sort_dirs=['desc'],
                                                    sort_keys=['reference'])

    @mock.patch('cinder.volume.api.API.get_manageable_volumes',
                side_effect=messaging.RemoteError(
                    exc_type='InvalidInput', value='marker not found: 1234'))
    def test_get_manageable_volumes_non_existent_marker_detailed(
            self, mock_api_manageable):
        res = self._get_resp_get('fakehost', detailed=True, paging=True)
        self.assertEqual(400, res.status_int)
        self.assertTrue(mock_api_manageable.called)

    @ddt.data(
        {'a' * 256: 'a'},
        {'a': 'a' * 256},
        {'': 'a'},
        {'a': None},
    )
    def test_manage_volume_with_invalid_metadata(self, value):
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                "metadata": value
            }
        }
        res = self._get_resp_post(body)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    @mock.patch('cinder.objects.service.Service.is_up',
                return_value=True,
                new_callable=mock.PropertyMock)
    def test_get_manageable_volumes_disabled(self, mock_is_up):
        res = self._get_resp_get('host_disabled', False, True)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        mock_is_up.assert_not_called()

    @mock.patch('cinder.objects.service.Service.is_up',
                return_value=False,
                new_callable=mock.PropertyMock)
    def test_get_manageable_volumes_is_down(self, mock_is_up):
        res = self._get_resp_get('host_ok', False, True)
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int, res)
        self.assertEqual(exception.ServiceUnavailable.message,
                         res.json['badRequest']['message'])
        self.assertTrue(mock_is_up.called)

    @mock.patch('cinder.volume.api.API.manage_existing', wraps=api_manage_new)
    def test_manage_volume_with_creating_status(self, mock_api_manage):
        """Test managing volume to return 'creating' status in V3 API."""
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        res = self._get_resp_post(body, mv.ETAGS)
        self.assertEqual(HTTPStatus.ACCEPTED, res.status_int)
        self.assertEqual(1, mock_api_manage.call_count)
        self.assertEqual('creating',
                         jsonutils.loads(res.body)['volume']['status'])

    def test_negative_manage_to_encrypted_type(self):
        """Not allowed to manage a volume to an encrypted volume type."""
        ctxt = context.RequestContext(fake.USER_ID,
                                      fake.PROJECT_ID,
                                      is_admin=True)
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'volume_type': fake_encrypted_vt['name']
            }
        }
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def test_negative_manage_to_encrypted_default_type(self):
        """Fail if no vol type in request and default vol type is encrypted."""

        self.flags(default_volume_type=fake_encrypted_vt['name'])
        ctxt = context.RequestContext(fake.USER_ID,
                                      fake.PROJECT_ID,
                                      is_admin=True)
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))
        self.assertEqual(HTTPStatus.BAD_REQUEST, res.status_int)

    def test_negative_no_volume_type(self):
        """Fail when no volume type is available for the managed volume."""
        self.flags(default_volume_type=None)
        ctxt = context.RequestContext(fake.USER_ID,
                                      fake.PROJECT_ID,
                                      is_admin=True)
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))
        self.assertEqual(HTTPStatus.INTERNAL_SERVER_ERROR, res.status_int)

    @mock.patch('cinder.group.API')
    @mock.patch('cinder.flow_utils')
    @mock.patch('cinder.volume.flows.api.manage_existing.get_flow')
    @mock.patch('cinder.volume.api.API._get_service_by_host_cluster')
    def test_manage_when_default_type_is_encrypted(self, mock_get_cluster,
                                                   mock_get_flow,
                                                   mock_flow_utils,
                                                   mock_group_api):
        """Default type doesn't matter if non-encrypted type is in request."""

        # make an encrypted type the default volume type
        self.flags(default_volume_type=fake_encrypted_vt['name'])
        ctxt = context.RequestContext(fake.USER_ID,
                                      fake.PROJECT_ID,
                                      is_admin=True)

        # pass a non-encrypted volume type in the request
        requested_vt = fake_vt
        body = {
            'volume': {
                'host': 'host_ok',
                'ref': 'fake_ref',
                'volume_type': requested_vt['name']
            }
        }
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))

        # request should be accepted
        self.assertEqual(HTTPStatus.ACCEPTED, res.status_int)

        # make sure the volume type passed through is the specified one
        called_with = mock_get_flow.call_args.args[2]
        self.assertEqual(requested_vt['name'],
                         called_with['volume_type']['name'])
        self.assertEqual(requested_vt['id'], called_with['volume_type']['id'])

    @mock.patch('cinder.group.API')
    @mock.patch('cinder.flow_utils')
    @mock.patch('cinder.volume.flows.api.manage_existing.get_flow')
    @mock.patch('cinder.volume.api.API._get_service_by_host_cluster')
    def test_manage_with_default_type(self, mock_get_cluster, mock_get_flow,
                                      mock_flow_utils, mock_group_api):
        """A non-encrypted default volume type should cause no problems."""

        # make an non-encrypted type the default volume type
        default_vt = fake_vt
        self.flags(default_volume_type=default_vt['name'])
        ctxt = context.RequestContext(fake.USER_ID,
                                      fake.PROJECT_ID,
                                      is_admin=True)

        # don't pass a volume type in the request
        body = {'volume': {'host': 'host_ok', 'ref': 'fake_ref'}}
        req = webob.Request.blank('/v3/%s/os-volume-manage' % fake.PROJECT_ID)
        req.method = 'POST'
        req.headers['Content-Type'] = 'application/json'
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(fakes.wsgi_app(fake_auth_context=ctxt))

        # request should be accepted
        self.assertEqual(HTTPStatus.ACCEPTED, res.status_int)

        # make sure the volume type passed through is the default
        called_with = mock_get_flow.call_args.args[2]
        self.assertEqual(default_vt['name'],
                         called_with['volume_type']['name'])
        self.assertEqual(default_vt['id'], called_with['volume_type']['id'])
Exemplo n.º 17
0
    def test_volume_force_detach_raises_remote_error(self):
        # current status is available
        volume = self._create_volume(self.ctx, {
            'provider_location': '',
            'size': 1
        })
        connector = {'initiator': 'iqn.2012-07.org.fake:01'}

        self.volume_api.reserve_volume(self.ctx, volume)
        mountpoint = '/dev/vbd'
        attachment = self.volume_api.attach(self.ctx, volume, fake.INSTANCE_ID,
                                            None, mountpoint, 'rw')
        # volume is attached
        volume.refresh()
        self.assertEqual('in-use', volume.status)
        self.assertEqual(fake.INSTANCE_ID, attachment['instance_uuid'])
        self.assertEqual(mountpoint, attachment['mountpoint'])
        self.assertEqual('attached', attachment['attach_status'])
        admin_metadata = volume.admin_metadata
        self.assertEqual(2, len(admin_metadata))
        self.assertEqual('False', admin_metadata['readonly'])
        self.assertEqual('rw', admin_metadata['attached_mode'])
        conn_info = self.volume_api.initialize_connection(
            self.ctx, volume, connector)
        self.assertEqual('rw', conn_info['data']['access_mode'])
        # build request to force detach
        volume_remote_error = \
            messaging.RemoteError(exc_type='VolumeAttachmentNotFound')
        with mock.patch.object(volume_api.API,
                               'detach',
                               side_effect=volume_remote_error):
            req = webob.Request.blank('/v2/%s/volumes/%s/action' %
                                      (fake.PROJECT_ID, volume.id))
            req.method = 'POST'
            req.headers['content-type'] = 'application/json'
            body = {'os-force_detach': {'attachment_id': fake.ATTACHMENT_ID}}
            req.body = jsonutils.dump_as_bytes(body)
            # attach admin context to request
            req.environ['cinder.context'] = self.ctx
            # make request
            resp = req.get_response(app())
            self.assertEqual(400, resp.status_int)

        # test for KeyError when missing connector
        volume_remote_error = (messaging.RemoteError(exc_type='KeyError'))
        with mock.patch.object(volume_api.API,
                               'detach',
                               side_effect=volume_remote_error):
            req = webob.Request.blank('/v2/%s/volumes/%s/action' %
                                      (fake.PROJECT_ID, volume.id))
            req.method = 'POST'
            req.headers['content-type'] = 'application/json'
            body = {'os-force_detach': {'attachment_id': fake.ATTACHMENT_ID}}
            req.body = jsonutils.dump_as_bytes(body)
            # attach admin context to request
            req.environ['cinder.context'] = self.ctx
            # make request
            self.assertRaises(messaging.RemoteError, req.get_response, app())

        # test for VolumeBackendAPIException
        volume_remote_error = (messaging.RemoteError(
            exc_type='VolumeBackendAPIException'))
        with mock.patch.object(volume_api.API,
                               'detach',
                               side_effect=volume_remote_error):
            req = webob.Request.blank('/v2/%s/volumes/%s/action' %
                                      (fake.PROJECT_ID, volume.id))
            req.method = 'POST'
            req.headers['content-type'] = 'application/json'
            body = {
                'os-force_detach': {
                    'attachment_id': fake.ATTACHMENT_ID,
                    'connector': connector
                }
            }
            req.body = jsonutils.dump_as_bytes(body)

            # attach admin context to request
            req.environ['cinder.context'] = self.ctx
            # make request
            self.assertRaises(messaging.RemoteError, req.get_response, app())