~0x44/nova/bug838466

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/test_volume_types.py

  • Committer: Tarmac
  • Author(s): vladimir.p
  • Date: 2011-08-25 16:14:44 UTC
  • mfrom: (1453.3.16 nova)
  • Revision ID: tarmac-20110825161444-oj48iwhpq7c5d6j7
Added:
- volume metadata
- volume types
- volume types extra_specs

Volume create API receives volume types & metadata.

Work in progress on Openstack APIs for volume types & extra_specs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2011 OpenStack LLC.
 
2
# All Rights Reserved.
 
3
#
 
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
 
7
#
 
8
#         http://www.apache.org/licenses/LICENSE-2.0
 
9
#
 
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
 
14
#    under the License.
 
15
 
 
16
import json
 
17
import stubout
 
18
import webob
 
19
 
 
20
from nova import exception
 
21
from nova import context
 
22
from nova import test
 
23
from nova import log as logging
 
24
from nova.volume import volume_types
 
25
from nova.tests.api.openstack import fakes
 
26
 
 
27
LOG = logging.getLogger('nova.tests.api.openstack.test_volume_types')
 
28
 
 
29
last_param = {}
 
30
 
 
31
 
 
32
def stub_volume_type(id):
 
33
    specs = {
 
34
            "key1": "value1",
 
35
            "key2": "value2",
 
36
            "key3": "value3",
 
37
            "key4": "value4",
 
38
            "key5": "value5"}
 
39
    return dict(id=id, name='vol_type_%s' % str(id), extra_specs=specs)
 
40
 
 
41
 
 
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))
 
46
 
 
47
 
 
48
def return_empty_volume_types_get_all_types(context):
 
49
    return {}
 
50
 
 
51
 
 
52
def return_volume_types_get_volume_type(context, id):
 
53
    if id == "777":
 
54
        raise exception.VolumeTypeNotFound(volume_type_id=id)
 
55
    return stub_volume_type(int(id))
 
56
 
 
57
 
 
58
def return_volume_types_destroy(context, name):
 
59
    if name == "777":
 
60
        raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
 
61
    pass
 
62
 
 
63
 
 
64
def return_volume_types_create(context, name, specs):
 
65
    pass
 
66
 
 
67
 
 
68
def return_volume_types_get_by_name(context, name):
 
69
    if name == "777":
 
70
        raise exception.VolumeTypeNotFoundByName(volume_type_name=name)
 
71
    return stub_volume_type(int(name.split("_")[2]))
 
72
 
 
73
 
 
74
class VolumeTypesApiTest(test.TestCase):
 
75
    def setUp(self):
 
76
        super(VolumeTypesApiTest, self).setUp()
 
77
        fakes.stub_out_key_pair_funcs(self.stubs)
 
78
 
 
79
    def tearDown(self):
 
80
        self.stubs.UnsetAll()
 
81
        super(VolumeTypesApiTest, self).tearDown()
 
82
 
 
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'])
 
91
 
 
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'])
 
96
 
 
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))
 
106
 
 
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'])
 
117
 
 
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)
 
124
 
 
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)
 
134
 
 
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)
 
144
 
 
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')
 
151
        req.method = 'POST'
 
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'])
 
161
 
 
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')
 
168
        req.method = 'POST'
 
169
        req.headers["content-type"] = "application/json"
 
170
        res = req.get_response(fakes.wsgi_app())
 
171
        self.assertEqual(400, res.status_int)