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
20
from nova import exception
21
from nova import context
23
from nova import log as logging
24
from nova.volume import volume_types
25
from nova.tests.api.openstack import fakes
27
LOG = logging.getLogger('nova.tests.api.openstack.test_volume_types')
32
def stub_volume_type(id):
39
return dict(id=id, name='vol_type_%s' % str(id), extra_specs=specs)
42
def return_volume_types_get_all_types(context):
43
return dict(vol_type_1=stub_volume_type(1),
44
vol_type_2=stub_volume_type(2),
45
vol_type_3=stub_volume_type(3))
48
def return_empty_volume_types_get_all_types(context):
52
def return_volume_types_get_volume_type(context, id):
54
raise exception.VolumeTypeNotFound(volume_type_id=id)
55
return stub_volume_type(int(id))
58
def return_volume_types_destroy(context, name):
60
raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
64
def return_volume_types_create(context, name, specs):
68
def return_volume_types_get_by_name(context, name):
70
raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
71
return stub_volume_type(int(name.split("_")[2]))
74
class VolumeTypesApiTest(test.TestCase):
76
super(VolumeTypesApiTest, self).setUp()
77
fakes.stub_out_key_pair_funcs(self.stubs)
81
super(VolumeTypesApiTest, self).tearDown()
83
def test_volume_types_index(self):
84
self.stubs.Set(volume_types, 'get_all_types',
85
return_volume_types_get_all_types)
86
req = webob.Request.blank('/v1.1/123/os-volume-types')
87
res = req.get_response(fakes.wsgi_app())
88
self.assertEqual(200, res.status_int)
89
res_dict = json.loads(res.body)
90
self.assertEqual('application/json', res.headers['Content-Type'])
92
self.assertEqual(3, len(res_dict))
93
for name in ['vol_type_1', 'vol_type_2', 'vol_type_3']:
94
self.assertEqual(name, res_dict[name]['name'])
95
self.assertEqual('value1', res_dict[name]['extra_specs']['key1'])
97
def test_volume_types_index_no_data(self):
98
self.stubs.Set(volume_types, 'get_all_types',
99
return_empty_volume_types_get_all_types)
100
req = webob.Request.blank('/v1.1/123/os-volume-types')
101
res = req.get_response(fakes.wsgi_app())
102
res_dict = json.loads(res.body)
103
self.assertEqual(200, res.status_int)
104
self.assertEqual('application/json', res.headers['Content-Type'])
105
self.assertEqual(0, len(res_dict))
107
def test_volume_types_show(self):
108
self.stubs.Set(volume_types, 'get_volume_type',
109
return_volume_types_get_volume_type)
110
req = webob.Request.blank('/v1.1/123/os-volume-types/1')
111
res = req.get_response(fakes.wsgi_app())
112
self.assertEqual(200, res.status_int)
113
res_dict = json.loads(res.body)
114
self.assertEqual('application/json', res.headers['Content-Type'])
115
self.assertEqual(1, len(res_dict))
116
self.assertEqual('vol_type_1', res_dict['volume_type']['name'])
118
def test_volume_types_show_not_found(self):
119
self.stubs.Set(volume_types, 'get_volume_type',
120
return_volume_types_get_volume_type)
121
req = webob.Request.blank('/v1.1/123/os-volume-types/777')
122
res = req.get_response(fakes.wsgi_app())
123
self.assertEqual(404, res.status_int)
125
def test_volume_types_delete(self):
126
self.stubs.Set(volume_types, 'get_volume_type',
127
return_volume_types_get_volume_type)
128
self.stubs.Set(volume_types, 'destroy',
129
return_volume_types_destroy)
130
req = webob.Request.blank('/v1.1/123/os-volume-types/1')
131
req.method = 'DELETE'
132
res = req.get_response(fakes.wsgi_app())
133
self.assertEqual(200, res.status_int)
135
def test_volume_types_delete_not_found(self):
136
self.stubs.Set(volume_types, 'get_volume_type',
137
return_volume_types_get_volume_type)
138
self.stubs.Set(volume_types, 'destroy',
139
return_volume_types_destroy)
140
req = webob.Request.blank('/v1.1/123/os-volume-types/777')
141
req.method = 'DELETE'
142
res = req.get_response(fakes.wsgi_app())
143
self.assertEqual(404, res.status_int)
145
def test_create(self):
146
self.stubs.Set(volume_types, 'create',
147
return_volume_types_create)
148
self.stubs.Set(volume_types, 'get_volume_type_by_name',
149
return_volume_types_get_by_name)
150
req = webob.Request.blank('/v1.1/123/os-volume-types')
152
req.body = '{"volume_type": {"name": "vol_type_1", '\
153
'"extra_specs": {"key1": "value1"}}}'
154
req.headers["content-type"] = "application/json"
155
res = req.get_response(fakes.wsgi_app())
156
self.assertEqual(200, res.status_int)
157
res_dict = json.loads(res.body)
158
self.assertEqual('application/json', res.headers['Content-Type'])
159
self.assertEqual(1, len(res_dict))
160
self.assertEqual('vol_type_1', res_dict['volume_type']['name'])
162
def test_create_empty_body(self):
163
self.stubs.Set(volume_types, 'create',
164
return_volume_types_create)
165
self.stubs.Set(volume_types, 'get_volume_type_by_name',
166
return_volume_types_get_by_name)
167
req = webob.Request.blank('/v1.1/123/os-volume-types')
169
req.headers["content-type"] = "application/json"
170
res = req.get_response(fakes.wsgi_app())
171
self.assertEqual(400, res.status_int)