1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 OpenStack, LLC
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
23
from glance.common import exception
24
from glance.registry import server
25
from tests import stubs
28
class TestImageController(unittest.TestCase):
30
"""Establish a clean test environment"""
31
self.stubs = stubout.StubOutForTesting()
32
stubs.stub_out_registry_db_image_api(self.stubs)
35
"""Clear the test environment"""
38
def test_get_root(self):
39
"""Tests that the root registry API returns "index",
40
which is a list of public images
44
'name': 'fake image #2'}
45
req = webob.Request.blank('/')
46
res = req.get_response(server.API())
47
res_dict = json.loads(res.body)
48
self.assertEquals(res.status_int, 200)
50
images = res_dict['images']
51
self.assertEquals(len(images), 1)
53
for k, v in fixture.iteritems():
54
self.assertEquals(v, images[0][k])
56
def test_get_index(self):
57
"""Tests that the /images registry API returns list of
62
'name': 'fake image #2'}
63
req = webob.Request.blank('/images')
64
res = req.get_response(server.API())
65
res_dict = json.loads(res.body)
66
self.assertEquals(res.status_int, 200)
68
images = res_dict['images']
69
self.assertEquals(len(images), 1)
71
for k, v in fixture.iteritems():
72
self.assertEquals(v, images[0][k])
74
def test_get_details(self):
75
"""Tests that the /images/detail registry API returns
76
a mapping containing a list of detailed image information
80
'name': 'fake image #2',
85
req = webob.Request.blank('/images/detail')
86
res = req.get_response(server.API())
87
res_dict = json.loads(res.body)
88
self.assertEquals(res.status_int, 200)
90
images = res_dict['images']
91
self.assertEquals(len(images), 1)
93
for k, v in fixture.iteritems():
94
self.assertEquals(v, images[0][k])
96
def test_create_image(self):
97
"""Tests that the /images POST registry API creates the image"""
98
fixture = {'name': 'fake public image',
103
req = webob.Request.blank('/images')
106
req.body = json.dumps(dict(image=fixture))
108
res = req.get_response(server.API())
110
self.assertEquals(res.status_int, 200)
112
res_dict = json.loads(res.body)
114
for k, v in fixture.iteritems():
115
self.assertEquals(v, res_dict['image'][k])
117
# Test ID auto-assigned properly
118
self.assertEquals(3, res_dict['image']['id'])
120
# Test status was updated properly
121
self.assertEquals('active', res_dict['image']['status'])
123
def test_create_image_with_bad_status(self):
124
"""Tests proper exception is raised if a bad status is set"""
126
'name': 'fake public image',
129
'status': 'bad status'
132
req = webob.Request.blank('/images')
135
req.body = json.dumps(dict(image=fixture))
137
# TODO(jaypipes): Port Nova's Fault infrastructure
138
# over to Glance to support exception catching into
139
# standard HTTP errors.
140
res = req.get_response(server.API())
141
self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
143
def test_update_image(self):
144
"""Tests that the /images PUT registry API updates the image"""
145
fixture = {'name': 'fake public image #2',
149
req = webob.Request.blank('/images/2')
152
req.body = json.dumps(dict(image=fixture))
154
res = req.get_response(server.API())
156
self.assertEquals(res.status_int, 200)
158
res_dict = json.loads(res.body)
160
for k, v in fixture.iteritems():
161
self.assertEquals(v, res_dict['image'][k])
163
def test_update_image_not_existing(self):
164
"""Tests proper exception is raised if attempt to update non-existing
167
'name': 'fake public image',
170
'status': 'bad status'
173
req = webob.Request.blank('/images/3')
176
req.body = json.dumps(dict(image=fixture))
178
# TODO(jaypipes): Port Nova's Fault infrastructure
179
# over to Glance to support exception catching into
180
# standard HTTP errors.
181
res = req.get_response(server.API())
182
self.assertEquals(res.status_int,
183
webob.exc.HTTPNotFound.code)
185
def test_delete_image(self):
186
"""Tests that the /images DELETE registry API deletes the image"""
188
# Grab the original number of images
189
req = webob.Request.blank('/images')
190
res = req.get_response(server.API())
191
res_dict = json.loads(res.body)
192
self.assertEquals(res.status_int, 200)
194
orig_num_images = len(res_dict['images'])
197
req = webob.Request.blank('/images/2')
199
req.method = 'DELETE'
201
res = req.get_response(server.API())
203
self.assertEquals(res.status_int, 200)
205
# Verify one less image
206
req = webob.Request.blank('/images')
207
res = req.get_response(server.API())
208
res_dict = json.loads(res.body)
209
self.assertEquals(res.status_int, 200)
211
new_num_images = len(res_dict['images'])
212
self.assertEquals(new_num_images, orig_num_images - 1)
214
def test_delete_image_not_existing(self):
215
"""Tests proper exception is raised if attempt to delete non-existing
218
req = webob.Request.blank('/images/3')
220
req.method = 'DELETE'
222
# TODO(jaypipes): Port Nova's Fault infrastructure
223
# over to Glance to support exception catching into
224
# standard HTTP errors.
225
res = req.get_response(server.API())
226
self.assertEquals(res.status_int,
227
webob.exc.HTTPNotFound.code)