예제 #1
0
 def wrapper(self, ctx, snapshot_id, *args, **kwargs):
     try:
         res = method(self, ctx, snapshot_id, *args, **kwargs)
     except cinder_exception.ClientException:
         exc_type, exc_value, exc_trace = sys.exc_info()
         if isinstance(exc_value, cinder_exception.NotFound):
             exc_value = exception.SnapshotNotFound(snapshot_id=snapshot_id)
         raise exc_value, None, exc_trace
     return res
예제 #2
0
    def test_volume_create_bad_snapshot_id(self, mock_create, mock_get):
        vol = {"snapshot_id": '1', "size": 10}
        body = {"volume": vol}
        mock_get.side_effect = exception.SnapshotNotFound(snapshot_id='1')

        req = fakes.HTTPRequest.blank(self.url_prefix + '/os-volumes')
        self.assertRaises(webob.exc.HTTPNotFound,
                          volumes_v21.VolumeController().create, req,
                          body=body)
예제 #3
0
 def wrapper(self, ctx, snapshot_id, *args, **kwargs):
     try:
         res = method(self, ctx, snapshot_id, *args, **kwargs)
     except (cinder_exception.ClientException,
             keystone_exception.ClientException):
         exc_type, exc_value, exc_trace = sys.exc_info()
         if isinstance(
                 exc_value,
             (keystone_exception.NotFound, cinder_exception.NotFound)):
             exc_value = exception.SnapshotNotFound(snapshot_id=snapshot_id)
         six.reraise(exc_value, None, exc_trace)
     return res
예제 #4
0
    def rollback_to_snap(self, volume, name):
        """Revert an RBD volume to its contents at a snapshot.

        :volume: Name of RBD object
        :name: Name of snapshot
        """
        with RBDVolumeProxy(self, volume) as vol:
            if name in [snap.get('name', '') for snap in vol.list_snaps()]:
                LOG.debug('rolling back rbd image(%(img)s) to '
                          'snapshot(%(snap)s)', {'snap': name, 'img': volume})
                vol.rollback_to_snap(name)
            else:
                raise exception.SnapshotNotFound(snapshot_id=name)
예제 #5
0
 def wrapper(self, ctx, snapshot_id, *args, **kwargs):
     try:
         res = method(self, ctx, snapshot_id, *args, **kwargs)
     except (cinder_exception.ClientException,
             keystone_exception.ClientException):
         exc_type, exc_value, exc_trace = sys.exc_info()
         if isinstance(exc_value, (keystone_exception.NotFound,
                                   cinder_exception.NotFound)):
             exc_value = exception.SnapshotNotFound(snapshot_id=snapshot_id)
         raise exc_value, None, exc_trace
     except (cinder_exception.ConnectionError,
             keystone_exception.ConnectionError):
         exc_type, exc_value, exc_trace = sys.exc_info()
         exc_value = exception.CinderConnectionFailed(
                                               reason=exc_value.message)
         raise exc_value, None, exc_trace
     return res
예제 #6
0
 def not_found(context):
     raise exception.SnapshotNotFound(snapshot_id=5)
예제 #7
0
파일: cinder.py 프로젝트: woraser/nova
 def wrapper(self, ctx, snapshot_id, *args, **kwargs):
     try:
         res = method(self, ctx, snapshot_id, *args, **kwargs)
     except (keystone_exception.NotFound, cinder_exception.NotFound):
         _reraise(exception.SnapshotNotFound(snapshot_id=snapshot_id))
     return res
예제 #8
0
 def fake_delete_snapshot_not_exist(self, context, snapshot_id):
     raise exception.SnapshotNotFound(snapshot_id=snapshot_id)
예제 #9
0
 def fake_get_snapshot(self, context, id):
     raise exception.SnapshotNotFound(snapshot_id=id)
예제 #10
0
def stub_snapshot_get(self, context, snapshot_id):
    if snapshot_id == '-1':
        raise exc.SnapshotNotFound(snapshot_id=snapshot_id)
    return stub_snapshot(snapshot_id)
예제 #11
0
def stub_snapshot_delete(self, context, snapshot_id):
    if snapshot_id == '-1':
        raise exc.SnapshotNotFound(snapshot_id=snapshot_id)
예제 #12
0
class SnapshotApiTestV21(test.NoDBTestCase):
    controller = volumes_v21.SnapshotController()
    validation_error = exception.ValidationError

    def setUp(self):
        super(SnapshotApiTestV21, self).setUp()
        fakes.stub_out_networking(self.stubs)
        fakes.stub_out_rate_limiting(self.stubs)
        self.stubs.Set(cinder.API, "create_snapshot",
                       fakes.stub_snapshot_create)
        self.stubs.Set(cinder.API, "create_snapshot_force",
                       fakes.stub_snapshot_create)
        self.stubs.Set(cinder.API, "delete_snapshot",
                       fakes.stub_snapshot_delete)
        self.stubs.Set(cinder.API, "get_snapshot", fakes.stub_snapshot_get)
        self.stubs.Set(cinder.API, "get_all_snapshots",
                       fakes.stub_snapshot_get_all)
        self.stubs.Set(cinder.API, "get", fakes.stub_volume_get)
        self.req = fakes.HTTPRequest.blank('')

    def _test_snapshot_create(self, force):
        snapshot = {
            "volume_id": '12',
            "force": force,
            "display_name": "Snapshot Test Name",
            "display_description": "Snapshot Test Desc"
        }
        body = dict(snapshot=snapshot)
        resp_dict = self.controller.create(self.req, body=body)
        self.assertIn('snapshot', resp_dict)
        self.assertEqual(snapshot['display_name'],
                         resp_dict['snapshot']['displayName'])
        self.assertEqual(snapshot['display_description'],
                         resp_dict['snapshot']['displayDescription'])
        self.assertEqual(snapshot['volume_id'],
                         resp_dict['snapshot']['volumeId'])

    def test_snapshot_create(self):
        self._test_snapshot_create(False)

    def test_snapshot_create_force(self):
        self._test_snapshot_create(True)

    def test_snapshot_create_invalid_force_param(self):
        body = {'snapshot': {'volume_id': '1', 'force': '**&&^^%%$$##@@'}}
        self.assertRaises(self.validation_error,
                          self.controller.create,
                          self.req,
                          body=body)

    def test_snapshot_delete(self):
        snapshot_id = '123'
        result = self.controller.delete(self.req, snapshot_id)

        # NOTE: on v2.1, http status code is set as wsgi_code of API
        # method instead of status_int in a response object.
        if isinstance(self.controller, volumes_v21.SnapshotController):
            status_int = self.controller.delete.wsgi_code
        else:
            status_int = result.status_int
        self.assertEqual(202, status_int)

    @mock.patch.object(
        cinder.API,
        'delete_snapshot',
        side_effect=exception.SnapshotNotFound(snapshot_id=FAKE_UUID))
    def test_delete_snapshot_not_exists(self, mock_mr):
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
                          self.req, FAKE_UUID)

    def test_snapshot_delete_invalid_id(self):
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
                          self.req, '-1')

    def test_snapshot_show(self):
        snapshot_id = '123'
        resp_dict = self.controller.show(self.req, snapshot_id)
        self.assertIn('snapshot', resp_dict)
        self.assertEqual(resp_dict['snapshot']['id'], str(snapshot_id))

    def test_snapshot_show_invalid_id(self):
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
                          self.req, '-1')

    def test_snapshot_detail(self):
        resp_dict = self.controller.detail(self.req)
        self.assertIn('snapshots', resp_dict)
        resp_snapshots = resp_dict['snapshots']
        self.assertEqual(len(resp_snapshots), 3)

        resp_snapshot = resp_snapshots.pop()
        self.assertEqual(resp_snapshot['id'], 102)

    def test_snapshot_index(self):
        resp_dict = self.controller.index(self.req)
        self.assertIn('snapshots', resp_dict)
        resp_snapshots = resp_dict['snapshots']
        self.assertEqual(3, len(resp_snapshots))
예제 #13
0
class SnapshotApiTestV21(test.NoDBTestCase):
    controller = volumes_v21.SnapshotController()
    validation_error = exception.ValidationError

    def setUp(self):
        super(SnapshotApiTestV21, self).setUp()
        fakes.stub_out_networking(self)
        self.stub_out("nova.volume.cinder.API.create_snapshot",
                      fakes.stub_snapshot_create)
        self.stub_out("nova.volume.cinder.API.create_snapshot_force",
                      fakes.stub_snapshot_create)
        self.stub_out("nova.volume.cinder.API.delete_snapshot",
                      fakes.stub_snapshot_delete)
        self.stub_out("nova.volume.cinder.API.get_snapshot",
                      fakes.stub_snapshot_get)
        self.stub_out("nova.volume.cinder.API.get_all_snapshots",
                      fakes.stub_snapshot_get_all)
        self.stub_out("nova.volume.cinder.API.get", fakes.stub_volume_get)
        self.req = fakes.HTTPRequest.blank('')

    def _test_snapshot_create(self, force):
        snapshot = {
            "volume_id": '12',
            "force": force,
            "display_name": "Snapshot Test Name",
            "display_description": "Snapshot Test Desc"
        }
        body = dict(snapshot=snapshot)
        resp_dict = self.controller.create(self.req, body=body)
        self.assertIn('snapshot', resp_dict)
        self.assertEqual(snapshot['display_name'],
                         resp_dict['snapshot']['displayName'])
        self.assertEqual(snapshot['display_description'],
                         resp_dict['snapshot']['displayDescription'])
        self.assertEqual(snapshot['volume_id'],
                         resp_dict['snapshot']['volumeId'])

    def test_snapshot_create(self):
        self._test_snapshot_create(False)

    def test_snapshot_create_force(self):
        self._test_snapshot_create(True)

    def test_snapshot_create_invalid_force_param(self):
        body = {'snapshot': {'volume_id': '1', 'force': '**&&^^%%$$##@@'}}
        self.assertRaises(self.validation_error,
                          self.controller.create,
                          self.req,
                          body=body)

    def test_snapshot_delete(self):
        snapshot_id = '123'
        delete = self.controller.delete
        result = delete(self.req, snapshot_id)

        # NOTE: on v2.1, http status code is set as wsgi_code of API
        # method instead of status_int in a response object.
        if isinstance(self.controller, volumes_v21.SnapshotController):
            status_int = delete.wsgi_code
        else:
            status_int = result.status_int
        self.assertEqual(202, status_int)

    @mock.patch.object(
        cinder.API,
        'delete_snapshot',
        side_effect=exception.SnapshotNotFound(snapshot_id=FAKE_UUID))
    def test_delete_snapshot_not_exists(self, mock_mr):
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
                          self.req, FAKE_UUID)

    def test_snapshot_delete_invalid_id(self):
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.delete,
                          self.req, '-1')

    def test_snapshot_show(self):
        snapshot_id = '123'
        resp_dict = self.controller.show(self.req, snapshot_id)
        self.assertIn('snapshot', resp_dict)
        self.assertEqual(str(snapshot_id), resp_dict['snapshot']['id'])

    def test_snapshot_show_invalid_id(self):
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
                          self.req, '-1')

    def test_snapshot_detail(self):
        resp_dict = self.controller.detail(self.req)
        self.assertIn('snapshots', resp_dict)
        resp_snapshots = resp_dict['snapshots']
        self.assertEqual(3, len(resp_snapshots))

        resp_snapshot = resp_snapshots.pop()
        self.assertEqual(102, resp_snapshot['id'])

    def test_snapshot_detail_offset_and_limit(self):
        path = ('/v2/%s/os-snapshots/detail?offset=1&limit=1' %
                fakes.FAKE_PROJECT_ID)
        req = fakes.HTTPRequest.blank(path)
        resp_dict = self.controller.detail(req)
        self.assertIn('snapshots', resp_dict)
        resp_snapshots = resp_dict['snapshots']
        self.assertEqual(1, len(resp_snapshots))

        resp_snapshot = resp_snapshots.pop()
        self.assertEqual(101, resp_snapshot['id'])

    def test_snapshot_index(self):
        resp_dict = self.controller.index(self.req)
        self.assertIn('snapshots', resp_dict)
        resp_snapshots = resp_dict['snapshots']
        self.assertEqual(3, len(resp_snapshots))

    def test_snapshot_index_offset_and_limit(self):
        path = ('/v2/%s/os-snapshots?offset=1&limit=1' % fakes.FAKE_PROJECT_ID)
        req = fakes.HTTPRequest.blank(path)
        resp_dict = self.controller.index(req)
        self.assertIn('snapshots', resp_dict)
        resp_snapshots = resp_dict['snapshots']
        self.assertEqual(1, len(resp_snapshots))

    def _test_list_with_invalid_filter(self, url):
        prefix = '/os-snapshots'
        req = fakes.HTTPRequest.blank(prefix + url)
        controller_list = self.controller.index
        if 'detail' in url:
            controller_list = self.controller.detail
        self.assertRaises(exception.ValidationError, controller_list, req)

    def test_list_with_invalid_non_int_limit(self):
        self._test_list_with_invalid_filter('?limit=-9')

    def test_list_with_invalid_string_limit(self):
        self._test_list_with_invalid_filter('?limit=abc')

    def test_list_duplicate_query_with_invalid_string_limit(self):
        self._test_list_with_invalid_filter('?limit=1&limit=abc')

    def test_detail_list_with_invalid_non_int_limit(self):
        self._test_list_with_invalid_filter('/detail?limit=-9')

    def test_detail_list_with_invalid_string_limit(self):
        self._test_list_with_invalid_filter('/detail?limit=abc')

    def test_detail_list_duplicate_query_with_invalid_string_limit(self):
        self._test_list_with_invalid_filter('/detail?limit=1&limit=abc')

    def test_list_with_invalid_non_int_offset(self):
        self._test_list_with_invalid_filter('?offset=-9')

    def test_list_with_invalid_string_offset(self):
        self._test_list_with_invalid_filter('?offset=abc')

    def test_list_duplicate_query_with_invalid_string_offset(self):
        self._test_list_with_invalid_filter('?offset=1&offset=abc')

    def test_detail_list_with_invalid_non_int_offset(self):
        self._test_list_with_invalid_filter('/detail?offset=-9')

    def test_detail_list_with_invalid_string_offset(self):
        self._test_list_with_invalid_filter('/detail?offset=abc')

    def test_detail_list_duplicate_query_with_invalid_string_offset(self):
        self._test_list_with_invalid_filter('/detail?offset=1&offset=abc')

    def _test_list_duplicate_query_parameters_validation(self, url):
        params = {'limit': 1, 'offset': 1}
        controller_list = self.controller.index
        if 'detail' in url:
            controller_list = self.controller.detail
        for param, value in params.items():
            req = fakes.HTTPRequest.blank(url + '?%s=%s&%s=%s' %
                                          (param, value, param, value))
            controller_list(req)

    def test_list_duplicate_query_parameters_validation(self):
        self._test_list_duplicate_query_parameters_validation('/os-snapshots')

    def test_detail_list_duplicate_query_parameters_validation(self):
        self._test_list_duplicate_query_parameters_validation(
            '/os-snapshots/detail')

    def test_list_with_additional_filter(self):
        req = fakes.HTTPRequest.blank(
            '/os-snapshots?limit=1&offset=1&additional=something')
        self.controller.index(req)

    def test_detail_list_with_additional_filter(self):
        req = fakes.HTTPRequest.blank(
            '/os-snapshots/detail?limit=1&offset=1&additional=something')
        self.controller.detail(req)