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
27
from nova.api import openstack
28
from nova.api.openstack import extensions
29
from nova.tests.api.openstack import fakes
33
def return_create_volume_type_extra_specs(context, volume_type_id,
35
return stub_volume_type_extra_specs()
38
def return_volume_type_extra_specs(context, volume_type_id):
39
return stub_volume_type_extra_specs()
42
def return_empty_volume_type_extra_specs(context, volume_type_id):
46
def delete_volume_type_extra_specs(context, volume_type_id, key):
50
def stub_volume_type_extra_specs():
60
class VolumeTypesExtraSpecsTest(test.TestCase):
63
super(VolumeTypesExtraSpecsTest, self).setUp()
64
fakes.stub_out_key_pair_funcs(self.stubs)
65
self.api_path = '/v1.1/123/os-volume-types/1/extra_specs'
68
self.stubs.Set(nova.db.api, 'volume_type_extra_specs_get',
69
return_volume_type_extra_specs)
70
request = webob.Request.blank(self.api_path)
71
res = request.get_response(fakes.wsgi_app())
72
self.assertEqual(200, res.status_int)
73
res_dict = json.loads(res.body)
74
self.assertEqual('application/json', res.headers['Content-Type'])
75
self.assertEqual('value1', res_dict['extra_specs']['key1'])
77
def test_index_no_data(self):
78
self.stubs.Set(nova.db.api, 'volume_type_extra_specs_get',
79
return_empty_volume_type_extra_specs)
80
req = webob.Request.blank(self.api_path)
81
res = req.get_response(fakes.wsgi_app())
82
res_dict = json.loads(res.body)
83
self.assertEqual(200, res.status_int)
84
self.assertEqual('application/json', res.headers['Content-Type'])
85
self.assertEqual(0, len(res_dict['extra_specs']))
88
self.stubs.Set(nova.db.api, 'volume_type_extra_specs_get',
89
return_volume_type_extra_specs)
90
req = webob.Request.blank(self.api_path + '/key5')
91
res = req.get_response(fakes.wsgi_app())
92
self.assertEqual(200, res.status_int)
93
res_dict = json.loads(res.body)
94
self.assertEqual('application/json', res.headers['Content-Type'])
95
self.assertEqual('value5', res_dict['key5'])
97
def test_show_spec_not_found(self):
98
self.stubs.Set(nova.db.api, 'volume_type_extra_specs_get',
99
return_empty_volume_type_extra_specs)
100
req = webob.Request.blank(self.api_path + '/key6')
101
res = req.get_response(fakes.wsgi_app())
102
res_dict = json.loads(res.body)
103
self.assertEqual(404, res.status_int)
105
def test_delete(self):
106
self.stubs.Set(nova.db.api, 'volume_type_extra_specs_delete',
107
delete_volume_type_extra_specs)
108
req = webob.Request.blank(self.api_path + '/key5')
109
req.method = 'DELETE'
110
res = req.get_response(fakes.wsgi_app())
111
self.assertEqual(200, res.status_int)
113
def test_create(self):
114
self.stubs.Set(nova.db.api,
115
'volume_type_extra_specs_update_or_create',
116
return_create_volume_type_extra_specs)
117
req = webob.Request.blank(self.api_path)
119
req.body = '{"extra_specs": {"key1": "value1"}}'
120
req.headers["content-type"] = "application/json"
121
res = req.get_response(fakes.wsgi_app())
122
res_dict = json.loads(res.body)
123
self.assertEqual(200, res.status_int)
124
self.assertEqual('application/json', res.headers['Content-Type'])
125
self.assertEqual('value1', res_dict['extra_specs']['key1'])
127
def test_create_empty_body(self):
128
self.stubs.Set(nova.db.api,
129
'volume_type_extra_specs_update_or_create',
130
return_create_volume_type_extra_specs)
131
req = webob.Request.blank(self.api_path)
133
req.headers["content-type"] = "application/json"
134
res = req.get_response(fakes.wsgi_app())
135
self.assertEqual(400, res.status_int)
137
def test_update_item(self):
138
self.stubs.Set(nova.db.api,
139
'volume_type_extra_specs_update_or_create',
140
return_create_volume_type_extra_specs)
141
req = webob.Request.blank(self.api_path + '/key1')
143
req.body = '{"key1": "value1"}'
144
req.headers["content-type"] = "application/json"
145
res = req.get_response(fakes.wsgi_app())
146
self.assertEqual(200, res.status_int)
147
self.assertEqual('application/json', res.headers['Content-Type'])
148
res_dict = json.loads(res.body)
149
self.assertEqual('value1', res_dict['key1'])
151
def test_update_item_empty_body(self):
152
self.stubs.Set(nova.db.api,
153
'volume_type_extra_specs_update_or_create',
154
return_create_volume_type_extra_specs)
155
req = webob.Request.blank(self.api_path + '/key1')
157
req.headers["content-type"] = "application/json"
158
res = req.get_response(fakes.wsgi_app())
159
self.assertEqual(400, res.status_int)
161
def test_update_item_too_many_keys(self):
162
self.stubs.Set(nova.db.api,
163
'volume_type_extra_specs_update_or_create',
164
return_create_volume_type_extra_specs)
165
req = webob.Request.blank(self.api_path + '/key1')
167
req.body = '{"key1": "value1", "key2": "value2"}'
168
req.headers["content-type"] = "application/json"
169
res = req.get_response(fakes.wsgi_app())
170
self.assertEqual(400, res.status_int)
172
def test_update_item_body_uri_mismatch(self):
173
self.stubs.Set(nova.db.api,
174
'volume_type_extra_specs_update_or_create',
175
return_create_volume_type_extra_specs)
176
req = webob.Request.blank(self.api_path + '/bad')
178
req.body = '{"key1": "value1"}'
179
req.headers["content-type"] = "application/json"
180
res = req.get_response(fakes.wsgi_app())
181
self.assertEqual(400, res.status_int)