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
16
from lxml import etree
19
from nova.api.openstack.volume import types
20
from nova.api.openstack.volume.views import types as views_types
21
from nova import exception
22
from nova.openstack.common import timeutils
24
from nova.tests.api.openstack import fakes
25
from nova.volume import volume_types
28
def stub_volume_type(id):
35
return dict(id=id, name='vol_type_%s' % str(id), extra_specs=specs)
38
def return_volume_types_get_all_types(context):
39
return dict(vol_type_1=stub_volume_type(1),
40
vol_type_2=stub_volume_type(2),
41
vol_type_3=stub_volume_type(3))
44
def return_empty_volume_types_get_all_types(context):
48
def return_volume_types_get_volume_type(context, id):
50
raise exception.VolumeTypeNotFound(volume_type_id=id)
51
return stub_volume_type(int(id))
54
def return_volume_types_get_by_name(context, name):
56
raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
57
return stub_volume_type(int(name.split("_")[2]))
60
class VolumeTypesApiTest(test.TestCase):
62
super(VolumeTypesApiTest, self).setUp()
63
self.controller = types.VolumeTypesController()
65
def test_volume_types_index(self):
66
self.stubs.Set(volume_types, 'get_all_types',
67
return_volume_types_get_all_types)
69
req = fakes.HTTPRequest.blank('/v1/fake/types')
70
res_dict = self.controller.index(req)
72
self.assertEqual(3, len(res_dict['volume_types']))
74
expected_names = ['vol_type_1', 'vol_type_2', 'vol_type_3']
75
actual_names = map(lambda e: e['name'], res_dict['volume_types'])
76
self.assertEqual(set(actual_names), set(expected_names))
77
for entry in res_dict['volume_types']:
78
self.assertEqual('value1', entry['extra_specs']['key1'])
80
def test_volume_types_index_no_data(self):
81
self.stubs.Set(volume_types, 'get_all_types',
82
return_empty_volume_types_get_all_types)
84
req = fakes.HTTPRequest.blank('/v1/fake/types')
85
res_dict = self.controller.index(req)
87
self.assertEqual(0, len(res_dict['volume_types']))
89
def test_volume_types_show(self):
90
self.stubs.Set(volume_types, 'get_volume_type',
91
return_volume_types_get_volume_type)
93
req = fakes.HTTPRequest.blank('/v1/fake/types/1')
94
res_dict = self.controller.show(req, 1)
96
self.assertEqual(1, len(res_dict))
97
self.assertEqual('1', res_dict['volume_type']['id'])
98
self.assertEqual('vol_type_1', res_dict['volume_type']['name'])
100
def test_volume_types_show_not_found(self):
101
self.stubs.Set(volume_types, 'get_volume_type',
102
return_volume_types_get_volume_type)
104
req = fakes.HTTPRequest.blank('/v1/fake/types/777')
105
self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
108
def test_view_builder_show(self):
109
view_builder = views_types.ViewBuilder()
111
now = timeutils.isotime()
112
raw_volume_type = dict(name='new_type',
120
request = fakes.HTTPRequest.blank("/v1")
121
output = view_builder.show(request, raw_volume_type)
123
self.assertTrue('volume_type' in output)
124
expected_volume_type = dict(name='new_type',
127
self.assertDictMatch(output['volume_type'], expected_volume_type)
129
def test_view_builder_list(self):
130
view_builder = views_types.ViewBuilder()
132
now = timeutils.isotime()
133
raw_volume_types = []
134
for i in range(0, 10):
135
raw_volume_types.append(dict(name='new_type',
143
request = fakes.HTTPRequest.blank("/v1")
144
output = view_builder.index(request, raw_volume_types)
146
self.assertTrue('volume_types' in output)
147
for i in range(0, 10):
148
expected_volume_type = dict(name='new_type',
151
self.assertDictMatch(output['volume_types'][i],
152
expected_volume_type)
155
class VolumeTypesSerializerTest(test.TestCase):
156
def _verify_volume_type(self, vtype, tree):
157
self.assertEqual('volume_type', tree.tag)
158
self.assertEqual(vtype['name'], tree.get('name'))
159
self.assertEqual(str(vtype['id']), tree.get('id'))
160
self.assertEqual(1, len(tree))
161
extra_specs = tree[0]
162
self.assertEqual('extra_specs', extra_specs.tag)
163
seen = set(vtype['extra_specs'].keys())
164
for child in extra_specs:
165
self.assertTrue(child.tag in seen)
166
self.assertEqual(vtype['extra_specs'][child.tag], child.text)
167
seen.remove(child.tag)
168
self.assertEqual(len(seen), 0)
170
def test_index_serializer(self):
171
serializer = types.VolumeTypesTemplate()
173
# Just getting some input data
174
vtypes = return_volume_types_get_all_types(None)
175
text = serializer.serialize({'volume_types': vtypes.values()})
177
tree = etree.fromstring(text)
179
self.assertEqual('volume_types', tree.tag)
180
self.assertEqual(len(vtypes), len(tree))
182
name = child.get('name')
183
self.assertTrue(name in vtypes)
184
self._verify_volume_type(vtypes[name], child)
186
def test_voltype_serializer(self):
187
serializer = types.VolumeTypeTemplate()
189
vtype = stub_volume_type(1)
190
text = serializer.serialize(dict(volume_type=vtype))
192
tree = etree.fromstring(text)
194
self._verify_volume_type(vtype, tree)