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_create_empty_body(self):
122
self.stubs.Set(nova.db,
123
'volume_type_extra_specs_update_or_create',
124
return_create_volume_type_extra_specs)
126
req = fakes.HTTPRequest.blank(self.api_path)
127
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create,
130
def test_update_item(self):
131
self.stubs.Set(nova.db,
132
'volume_type_extra_specs_update_or_create',
133
return_create_volume_type_extra_specs)
134
body = {"key1": "value1"}
136
req = fakes.HTTPRequest.blank(self.api_path + '/key1')
137
res_dict = self.controller.update(req, 1, 'key1', body)
139
self.assertEqual('value1', res_dict['key1'])
141
def test_update_item_empty_body(self):
142
self.stubs.Set(nova.db,
143
'volume_type_extra_specs_update_or_create',
144
return_create_volume_type_extra_specs)
146
req = fakes.HTTPRequest.blank(self.api_path + '/key1')
147
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
150
def test_update_item_too_many_keys(self):
151
self.stubs.Set(nova.db,
152
'volume_type_extra_specs_update_or_create',
153
return_create_volume_type_extra_specs)
154
body = {"key1": "value1", "key2": "value2"}
156
req = fakes.HTTPRequest.blank(self.api_path + '/key1')
157
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
158
req, 1, 'key1', body)
160
def test_update_item_body_uri_mismatch(self):
161
self.stubs.Set(nova.db,
162
'volume_type_extra_specs_update_or_create',
163
return_create_volume_type_extra_specs)
164
body = {"key1": "value1"}
166
req = fakes.HTTPRequest.blank(self.api_path + '/bad')
167
self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
171
class VolumeTypeExtraSpecsSerializerTest(test.TestCase):
172
def test_index_create_serializer(self):
173
serializer = types_extra_specs.VolumeTypeExtraSpecsTemplate()
175
# Just getting some input data
176
extra_specs = stub_volume_type_extra_specs()
177
text = serializer.serialize(dict(extra_specs=extra_specs))
180
tree = etree.fromstring(text)
182
self.assertEqual('extra_specs', tree.tag)
183
self.assertEqual(len(extra_specs), len(tree))
184
seen = set(extra_specs.keys())
186
self.assertTrue(child.tag in seen)
187
self.assertEqual(extra_specs[child.tag], child.text)
188
seen.remove(child.tag)
189
self.assertEqual(len(seen), 0)
191
def test_update_show_serializer(self):
192
serializer = types_extra_specs.VolumeTypeExtraSpecTemplate()
194
exemplar = dict(key1='value1')
195
text = serializer.serialize(exemplar)
198
tree = etree.fromstring(text)
200
self.assertEqual('key1', tree.tag)
201
self.assertEqual('value1', tree.text)
202
self.assertEqual(0, len(tree))