1
# Copyright 2011 OpenStack LLC.
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
18
from nova.api.openstack.volume.contrib import types_manage
19
from nova import exception
21
from nova.tests.api.openstack import fakes
22
from nova.volume import volume_types
25
def stub_volume_type(id):
32
return dict(id=id, name='vol_type_%s' % str(id), extra_specs=specs)
35
def return_volume_types_get_volume_type(context, id):
37
raise exception.VolumeTypeNotFound(volume_type_id=id)
38
return stub_volume_type(int(id))
41
def return_volume_types_destroy(context, name):
43
raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
47
def return_volume_types_create(context, name, specs):
51
def return_volume_types_get_by_name(context, name):
53
raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
54
return stub_volume_type(int(name.split("_")[2]))
57
class VolumeTypesManageApiTest(test.TestCase):
59
super(VolumeTypesManageApiTest, self).setUp()
60
self.controller = types_manage.VolumeTypesManageController()
62
def test_volume_types_delete(self):
63
self.stubs.Set(volume_types, 'get_volume_type',
64
return_volume_types_get_volume_type)
65
self.stubs.Set(volume_types, 'destroy',
66
return_volume_types_destroy)
68
req = fakes.HTTPRequest.blank('/v1/fake/types/1')
69
self.controller._delete(req, 1)
71
def test_volume_types_delete_not_found(self):
72
self.stubs.Set(volume_types, 'get_volume_type',
73
return_volume_types_get_volume_type)
74
self.stubs.Set(volume_types, 'destroy',
75
return_volume_types_destroy)
77
req = fakes.HTTPRequest.blank('/v1/fake/types/777')
78
self.assertRaises(webob.exc.HTTPNotFound, self.controller._delete,
81
def test_create(self):
82
self.stubs.Set(volume_types, 'create',
83
return_volume_types_create)
84
self.stubs.Set(volume_types, 'get_volume_type_by_name',
85
return_volume_types_get_by_name)
87
body = {"volume_type": {"name": "vol_type_1",
88
"extra_specs": {"key1": "value1"}}}
89
req = fakes.HTTPRequest.blank('/v1/fake/types')
90
res_dict = self.controller._create(req, body)
92
self.assertEqual(1, len(res_dict))
93
self.assertEqual('vol_type_1', res_dict['volume_type']['name'])
96
class VolumeTypesUnprocessableEntityTestCase(test.TestCase):
99
Tests of places we throw 422 Unprocessable Entity from
103
super(VolumeTypesUnprocessableEntityTestCase, self).setUp()
104
self.controller = types_manage.VolumeTypesManageController()
106
def _unprocessable_volume_type_create(self, body):
107
req = fakes.HTTPRequest.blank('/v2/fake/types')
110
self.assertRaises(webob.exc.HTTPUnprocessableEntity,
111
self.controller._create, req, body)
113
def test_create_no_body(self):
114
self._unprocessable_volume_type_create(body=None)
116
def test_create_missing_volume(self):
117
body = {'foo': {'a': 'b'}}
118
self._unprocessable_volume_type_create(body=body)
120
def test_create_malformed_entity(self):
121
body = {'volume_type': 'string'}
122
self._unprocessable_volume_type_create(body=body)