~ubuntu-branches/ubuntu/saucy/nova/saucy-proposed

« back to all changes in this revision

Viewing changes to nova/tests/api/openstack/volume/contrib/test_types_extra_specs.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2012-05-24 13:12:53 UTC
  • mfrom: (1.1.55)
  • Revision ID: package-import@ubuntu.com-20120524131253-ommql08fg1en06ut
Tags: 2012.2~f1-0ubuntu1
* New upstream release.
* Prepare for quantal:
  - Dropped debian/patches/upstream/0006-Use-project_id-in-ec2.cloud._format_image.patch
  - Dropped debian/patches/upstream/0005-Populate-image-properties-with-project_id-again.patch
  - Dropped debian/patches/upstream/0004-Fixed-bug-962840-added-a-test-case.patch
  - Dropped debian/patches/upstream/0003-Allow-unprivileged-RADOS-users-to-access-rbd-volumes.patch
  - Dropped debian/patches/upstream/0002-Stop-libvirt-test-from-deleting-instances-dir.patch
  - Dropped debian/patches/upstream/0001-fix-bug-where-nova-ignores-glance-host-in-imageref.patch 
  - Dropped debian/patches/0001-fix-useexisting-deprecation-warnings.patch
* debian/control: Add python-keystone as a dependency. (LP: #907197)
* debian/patches/kombu_tests_timeout.patch: Refreshed.
* debian/nova.conf, debian/nova-common.postinst: Convert to new ini
  file configuration
* debian/patches/nova-manage_flagfile_location.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright (c) 2011 Zadara Storage Inc.
 
4
# Copyright (c) 2011 OpenStack LLC.
 
5
# Copyright 2011 University of Southern California
 
6
# All Rights Reserved.
 
7
#
 
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
 
11
#
 
12
#         http://www.apache.org/licenses/LICENSE-2.0
 
13
#
 
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
 
18
#    under the License.
 
19
 
 
20
from lxml import etree
 
21
import webob
 
22
 
 
23
from nova.api.openstack.volume.contrib import types_extra_specs
 
24
from nova import test
 
25
from nova.tests.api.openstack import fakes
 
26
import nova.wsgi
 
27
 
 
28
 
 
29
def return_create_volume_type_extra_specs(context, volume_type_id,
 
30
                                          extra_specs):
 
31
    return stub_volume_type_extra_specs()
 
32
 
 
33
 
 
34
def return_volume_type_extra_specs(context, volume_type_id):
 
35
    return stub_volume_type_extra_specs()
 
36
 
 
37
 
 
38
def return_empty_volume_type_extra_specs(context, volume_type_id):
 
39
    return {}
 
40
 
 
41
 
 
42
def delete_volume_type_extra_specs(context, volume_type_id, key):
 
43
    pass
 
44
 
 
45
 
 
46
def stub_volume_type_extra_specs():
 
47
    specs = {
 
48
            "key1": "value1",
 
49
            "key2": "value2",
 
50
            "key3": "value3",
 
51
            "key4": "value4",
 
52
            "key5": "value5"}
 
53
    return specs
 
54
 
 
55
 
 
56
def volume_type_get(context, volume_type_id):
 
57
    pass
 
58
 
 
59
 
 
60
class VolumeTypesExtraSpecsTest(test.TestCase):
 
61
 
 
62
    def setUp(self):
 
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()
 
67
 
 
68
    def test_index(self):
 
69
        self.stubs.Set(nova.db, 'volume_type_extra_specs_get',
 
70
                       return_volume_type_extra_specs)
 
71
 
 
72
        req = fakes.HTTPRequest.blank(self.api_path)
 
73
        res_dict = self.controller.index(req, 1)
 
74
 
 
75
        self.assertEqual('value1', res_dict['extra_specs']['key1'])
 
76
 
 
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)
 
80
 
 
81
        req = fakes.HTTPRequest.blank(self.api_path)
 
82
        res_dict = self.controller.index(req, 1)
 
83
 
 
84
        self.assertEqual(0, len(res_dict['extra_specs']))
 
85
 
 
86
    def test_show(self):
 
87
        self.stubs.Set(nova.db, 'volume_type_extra_specs_get',
 
88
                       return_volume_type_extra_specs)
 
89
 
 
90
        req = fakes.HTTPRequest.blank(self.api_path + '/key5')
 
91
        res_dict = self.controller.show(req, 1, 'key5')
 
92
 
 
93
        self.assertEqual('value5', res_dict['key5'])
 
94
 
 
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)
 
98
 
 
99
        req = fakes.HTTPRequest.blank(self.api_path + '/key6')
 
100
        self.assertRaises(webob.exc.HTTPNotFound, self.controller.show,
 
101
                          req, 1, 'key6')
 
102
 
 
103
    def test_delete(self):
 
104
        self.stubs.Set(nova.db, 'volume_type_extra_specs_delete',
 
105
                       delete_volume_type_extra_specs)
 
106
 
 
107
        req = fakes.HTTPRequest.blank(self.api_path + '/key5')
 
108
        self.controller.delete(req, 1, 'key5')
 
109
 
 
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"}}
 
115
 
 
116
        req = fakes.HTTPRequest.blank(self.api_path)
 
117
        res_dict = self.controller.create(req, 1, body)
 
118
 
 
119
        self.assertEqual('value1', res_dict['extra_specs']['key1'])
 
120
 
 
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)
 
125
 
 
126
        req = fakes.HTTPRequest.blank(self.api_path)
 
127
        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.create,
 
128
                          req, 1, '')
 
129
 
 
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"}
 
135
 
 
136
        req = fakes.HTTPRequest.blank(self.api_path + '/key1')
 
137
        res_dict = self.controller.update(req, 1, 'key1', body)
 
138
 
 
139
        self.assertEqual('value1', res_dict['key1'])
 
140
 
 
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)
 
145
 
 
146
        req = fakes.HTTPRequest.blank(self.api_path + '/key1')
 
147
        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
 
148
                          req, 1, 'key1', '')
 
149
 
 
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"}
 
155
 
 
156
        req = fakes.HTTPRequest.blank(self.api_path + '/key1')
 
157
        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
 
158
                          req, 1, 'key1', body)
 
159
 
 
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"}
 
165
 
 
166
        req = fakes.HTTPRequest.blank(self.api_path + '/bad')
 
167
        self.assertRaises(webob.exc.HTTPBadRequest, self.controller.update,
 
168
                          req, 1, 'bad', body)
 
169
 
 
170
 
 
171
class VolumeTypeExtraSpecsSerializerTest(test.TestCase):
 
172
    def test_index_create_serializer(self):
 
173
        serializer = types_extra_specs.VolumeTypeExtraSpecsTemplate()
 
174
 
 
175
        # Just getting some input data
 
176
        extra_specs = stub_volume_type_extra_specs()
 
177
        text = serializer.serialize(dict(extra_specs=extra_specs))
 
178
 
 
179
        print text
 
180
        tree = etree.fromstring(text)
 
181
 
 
182
        self.assertEqual('extra_specs', tree.tag)
 
183
        self.assertEqual(len(extra_specs), len(tree))
 
184
        seen = set(extra_specs.keys())
 
185
        for child in tree:
 
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)
 
190
 
 
191
    def test_update_show_serializer(self):
 
192
        serializer = types_extra_specs.VolumeTypeExtraSpecTemplate()
 
193
 
 
194
        exemplar = dict(key1='value1')
 
195
        text = serializer.serialize(exemplar)
 
196
 
 
197
        print text
 
198
        tree = etree.fromstring(text)
 
199
 
 
200
        self.assertEqual('key1', tree.tag)
 
201
        self.assertEqual('value1', tree.text)
 
202
        self.assertEqual(0, len(tree))