~jaypipes/glance/api-image-format

« back to all changes in this revision

Viewing changes to tests/unit/test_api.py

  • Committer: jaypipes at gmail
  • Date: 2011-02-25 14:55:26 UTC
  • Revision ID: jaypipes@gmail.com-20110225145526-o8zfvh31fl0n79ie
Adds lots of unit tests for verifying exceptions are raised
properly with invalid or mismatched disk and container formats.

Adds documentation on disk and container formats. Updates
existing documentation to remove references to the now-gone
type column and replaces these references with disk_format
and container_format.

Reworked the validates_image() method in the registry.db.api
to be like what Rick was describing in reviews.

Show diffs side-by-side

added added

removed removed

Lines of Context:
127
127
        # Test status was updated properly
128
128
        self.assertEquals('active', res_dict['image']['status'])
129
129
 
 
130
    def test_create_image_with_bad_container_format(self):
 
131
        """Tests proper exception is raised if a bad disk_format is set"""
 
132
        fixture = {'id': 3,
 
133
                   'name': 'fake public image',
 
134
                   'is_public': True,
 
135
                   'disk_format': 'vhd',
 
136
                   'container_format': 'invalid'}
 
137
 
 
138
        req = webob.Request.blank('/images')
 
139
 
 
140
        req.method = 'POST'
 
141
        req.body = json.dumps(dict(image=fixture))
 
142
 
 
143
        res = req.get_response(self.api)
 
144
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
145
        self.assertTrue('Invalid container format' in res.body)
 
146
 
 
147
    def test_create_image_with_bad_disk_format(self):
 
148
        """Tests proper exception is raised if a bad disk_format is set"""
 
149
        fixture = {'id': 3,
 
150
                   'name': 'fake public image',
 
151
                   'is_public': True,
 
152
                   'disk_format': 'invalid',
 
153
                   'container_format': 'ovf'}
 
154
 
 
155
        req = webob.Request.blank('/images')
 
156
 
 
157
        req.method = 'POST'
 
158
        req.body = json.dumps(dict(image=fixture))
 
159
 
 
160
        res = req.get_response(self.api)
 
161
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
162
        self.assertTrue('Invalid disk format' in res.body)
 
163
 
 
164
    def test_create_image_with_mismatched_formats(self):
 
165
        """Tests that exception raised for bad matching disk and container
 
166
        formats"""
 
167
        fixture = {'name': 'fake public image #3',
 
168
                   'container_format': 'aki',
 
169
                   'disk_format': 'ari'}
 
170
 
 
171
        req = webob.Request.blank('/images')
 
172
 
 
173
        req.method = 'POST'
 
174
        req.body = json.dumps(dict(image=fixture))
 
175
 
 
176
        res = req.get_response(self.api)
 
177
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
178
        self.assertTrue('Invalid mix of disk and container formats'
 
179
                        in res.body)
 
180
 
130
181
    def test_create_image_with_bad_status(self):
131
182
        """Tests proper exception is raised if a bad status is set"""
132
183
        fixture = {'id': 3,
143
194
 
144
195
        res = req.get_response(self.api)
145
196
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
197
        self.assertTrue('Invalid image status' in res.body)
146
198
 
147
199
    def test_update_image(self):
148
200
        """Tests that the /images PUT registry API updates the image"""
166
218
    def test_update_image_not_existing(self):
167
219
        """Tests proper exception is raised if attempt to update non-existing
168
220
        image"""
169
 
        fixture = {'id': 3,
170
 
                   'name': 'fake public image',
171
 
                   'is_public': True,
172
 
                   'disk_format': 'vhd',
173
 
                   'container_format': 'ovf',
174
 
                   'status': 'bad status'}
 
221
        fixture = {'status': 'killed'}
175
222
 
176
223
        req = webob.Request.blank('/images/3')
177
224
 
182
229
        self.assertEquals(res.status_int,
183
230
                          webob.exc.HTTPNotFound.code)
184
231
 
 
232
    def test_update_image_with_bad_status(self):
 
233
        """Tests that exception raised trying to set a bad status"""
 
234
        fixture = {'status': 'invalid'}
 
235
 
 
236
        req = webob.Request.blank('/images/2')
 
237
 
 
238
        req.method = 'PUT'
 
239
        req.body = json.dumps(dict(image=fixture))
 
240
 
 
241
        res = req.get_response(self.api)
 
242
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
243
        self.assertTrue('Invalid image status' in res.body)
 
244
 
 
245
    def test_update_image_with_bad_disk_format(self):
 
246
        """Tests that exception raised trying to set a bad disk_format"""
 
247
        fixture = {'disk_format': 'invalid'}
 
248
 
 
249
        req = webob.Request.blank('/images/2')
 
250
 
 
251
        req.method = 'PUT'
 
252
        req.body = json.dumps(dict(image=fixture))
 
253
 
 
254
        res = req.get_response(self.api)
 
255
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
256
        self.assertTrue('Invalid disk format' in res.body)
 
257
 
 
258
    def test_update_image_with_bad_container_format(self):
 
259
        """Tests that exception raised trying to set a bad container_format"""
 
260
        fixture = {'container_format': 'invalid'}
 
261
 
 
262
        req = webob.Request.blank('/images/2')
 
263
 
 
264
        req.method = 'PUT'
 
265
        req.body = json.dumps(dict(image=fixture))
 
266
 
 
267
        res = req.get_response(self.api)
 
268
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
269
        self.assertTrue('Invalid container format' in res.body)
 
270
 
 
271
    def test_update_image_with_mismatched_formats(self):
 
272
        """Tests that exception raised for bad matching disk and container
 
273
        formats"""
 
274
        fixture = {'container_format': 'ari'}
 
275
 
 
276
        req = webob.Request.blank('/images/2')  # Image 2 has disk format 'vhd'
 
277
 
 
278
        req.method = 'PUT'
 
279
        req.body = json.dumps(dict(image=fixture))
 
280
 
 
281
        res = req.get_response(self.api)
 
282
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
 
283
        self.assertTrue('Invalid mix of disk and container formats'
 
284
                        in res.body)
 
285
 
185
286
    def test_delete_image(self):
186
287
        """Tests that the /images DELETE registry API deletes the image"""
187
288
 
261
362
        fixture_headers = {'x-image-meta-store': 'bad',
262
363
                   'x-image-meta-name': 'bogus',
263
364
                   'x-image-meta-location': 'http://example.com/image.tar.gz',
264
 
                   'x-image-meta-disk-format': 'invalid'}
 
365
                   'x-image-meta-disk-format': 'invalid',
 
366
                   'x-image-meta-container-format': 'ami'}
265
367
 
266
368
        req = webob.Request.blank("/images")
267
369
        req.method = 'POST'
270
372
 
271
373
        res = req.get_response(self.api)
272
374
        self.assertEquals(res.status_int, webob.exc.HTTPBadRequest.code)
273
 
        self.assertTrue('Invalid disk format' in res.body)
 
375
        self.assertTrue('Invalid disk format' in res.body, res.body)
274
376
 
275
377
    def test_missing_container_format(self):
276
378
        fixture_headers = {'x-image-meta-store': 'bad',