1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright (c) 2011 Zadara Storage Inc.
4
# Copyright (c) 2011 OpenStack LLC.
5
# Copyright 2011 University of Southern California
8
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
# not use this file except in compliance with the License. You may obtain
10
# a copy of the License at
12
# http://www.apache.org/licenses/LICENSE-2.0
14
# Unless required by applicable law or agreed to in writing, software
15
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
# License for the specific language governing permissions and limitations
20
from lxml import etree
23
from nova.api.openstack.volume.contrib import types_extra_specs
25
from nova.tests.api.openstack import fakes
29
def return_create_volume_type_extra_specs(context, volume_type_id,
31
return stub_volume_type_extra_specs()
34
def return_volume_type_extra_specs(context, volume_type_id):
35
return stub_volume_type_extra_specs()
38
def return_empty_volume_type_extra_specs(context, volume_type_id):
42
def delete_volume_type_extra_specs(context, volume_type_id, key):
46
def stub_volume_type_extra_specs():
56
def volume_type_get(context, volume_type_id):
60
class VolumeTypesExtraSpecsTest(test.TestCase):
63
super(VolumeTypesExtraSpecsTest, self).setUp()
64
self.stubs.Set(nova.db, 'volume_type_get', volume_type_get)
65
self.api_path = '/v1/fake/os-volume-types/1/extra_specs'
66
self.controller = types_extra_specs.VolumeTypeExtraSpecsController()
69
self.stubs.Set(nova.db, 'volume_type_extra_specs_get',
70
return_volume_type_extra_specs)
72
req = fakes.HTTPRequest.blank(self.api_path)
73
res_dict = self.controller.index(req, 1)
75
self.assertEqual('value1', res_dict['extra_specs']['key1'])
77
def test_index_no_data(self):
78
self.stubs.Set(nova.db, 'volume_type_extra_specs_get',
79
return_empty_volume_type_extra_specs)
81
req = fakes.HTTPRequest.blank(self.api_path)
82
res_dict = self.controller.index(req, 1)
84
self.assertEqual(0, len(res_dict['extra_specs']))
87
self.stubs.Set(nova.db, 'volume_type_extra_specs_get',
88
return_volume_type_extra_specs)
90
req = fakes.HTTPRequest.blank(self.api_path + '/key5')
91
res_dict = self.controller.show(req, 1, 'key5')
93
self.assertEqual('value5', res_dict['key5'])
95
def test_show_spec_not_found(self):
96
self.stubs.Set(nova.db, 'volume_type_extra_specs_get',
97
return_empty_volume_type_extra_specs)
99
req = fakes.HTTPRequest.blank(self.api_path + '/key6')
100
self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
103
def test_delete(self):
104
self.stubs.Set(nova.db, 'volume_type_extra_specs_delete',
105
delete_volume_type_extra_specs)
107
req = fakes.HTTPRequest.blank(self.api_path + '/key5')
108
self.controller.delete(req, 1, 'key5')
110
def test_create(self):
111
self.stubs.Set(nova.db,
112
'volume_type_extra_specs_update_or_create',
113
return_create_volume_type_extra_specs)
114
body = {"extra_specs": {"key1": "value1"}}
116
req = fakes.HTTPRequest.blank(self.api_path)
117
res_dict = self.controller.create(req, 1, body)
119
self.assertEqual('value1', res_dict['extra_specs']['key1'])
121
def test_update_item(self):
122
self.stubs.Set(nova.db,
123
'volume_type_extra_specs_update_or_create',
124
return_create_volume_type_extra_specs)
125
body = {"key1": "value1"}
127
req = fakes.HTTPRequest.blank(self.api_path + '/key1')
128
res_dict = self.controller.update(req, 1, 'key1', body)
130
self.assertEqual('value1', res_dict['key1'])
132
def test_update_item_too_many_keys(self):
133
self.stubs.Set(nova.db,
134
'volume_type_extra_specs_update_or_create',
135
return_create_volume_type_extra_specs)
136
body = {"key1": "value1", "key2": "value2"}
138
req = fakes.HTTPRequest.blank(self.api_path + '/key1')
139
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
140
req, 1, 'key1', body)
142
def test_update_item_body_uri_mismatch(self):
143
self.stubs.Set(nova.db,
144
'volume_type_extra_specs_update_or_create',
145
return_create_volume_type_extra_specs)
146
body = {"key1": "value1"}
148
req = fakes.HTTPRequest.blank(self.api_path + '/bad')
149
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
153
class VolumeTypeExtraSpecsSerializerTest(test.TestCase):
154
def test_index_create_serializer(self):
155
serializer = types_extra_specs.VolumeTypeExtraSpecsTemplate()
157
# Just getting some input data
158
extra_specs = stub_volume_type_extra_specs()
159
text = serializer.serialize(dict(extra_specs=extra_specs))
162
tree = etree.fromstring(text)
164
self.assertEqual('extra_specs', tree.tag)
165
self.assertEqual(len(extra_specs), len(tree))
166
seen = set(extra_specs.keys())
168
self.assertTrue(child.tag in seen)
169
self.assertEqual(extra_specs[child.tag], child.text)
170
seen.remove(child.tag)
171
self.assertEqual(len(seen), 0)
173
def test_update_show_serializer(self):
174
serializer = types_extra_specs.VolumeTypeExtraSpecTemplate()
176
exemplar = dict(key1='value1')
177
text = serializer.serialize(exemplar)
180
tree = etree.fromstring(text)
182
self.assertEqual('key1', tree.tag)
183
self.assertEqual('value1', tree.text)
184
self.assertEqual(0, len(tree))
187
class VolumeTypeExtraSpecsUnprocessableEntityTestCase(test.TestCase):
190
Tests of places we throw 422 Unprocessable Entity from
194
super(VolumeTypeExtraSpecsUnprocessableEntityTestCase, self).setUp()
195
self.controller = types_extra_specs.VolumeTypeExtraSpecsController()
197
def _unprocessable_extra_specs_create(self, body):
198
req = fakes.HTTPRequest.blank('/v2/fake/types/1/extra_specs')
201
self.assertRaises(webob.exc.HTTPUnprocessableEntity,
202
self.controller.create, req, '1', body)
204
def test_create_no_body(self):
205
self._unprocessable_extra_specs_create(body=None)
207
def test_create_missing_volume(self):
208
body = {'foo': {'a': 'b'}}
209
self._unprocessable_extra_specs_create(body=body)
211
def test_create_malformed_entity(self):
212
body = {'extra_specs': 'string'}
213
self._unprocessable_extra_specs_create(body=body)
215
def _unprocessable_extra_specs_update(self, body):
216
req = fakes.HTTPRequest.blank('/v2/fake/types/1/extra_specs')
219
self.assertRaises(webob.exc.HTTPUnprocessableEntity,
220
self.controller.update, req, '1', body)
222
def test_update_no_body(self):
223
self._unprocessable_extra_specs_update(body=None)
225
def test_update_empty_body(self):
226
self._unprocessable_extra_specs_update(body={})