blob: 19a7a95ac26ebcd2f010ec4e201c0455fbb7846c [file] [log] [blame]
Matthew Treinisha62347f2013-03-01 16:37:30 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
3# Copyright 2013 OpenStack, LLC
4# All Rights Reserved.
5# Copyright 2013 IBM Corp
6#
7# Licensed under the Apache License, Version 2.0 (the "License"); you may
8# not use this file except in compliance with the License. You may obtain
9# a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16# License for the specific language governing permissions and limitations
17# under the License.
18
19import cStringIO as StringIO
20import random
21
22from tempest import clients
23from tempest import exceptions
Matthew Treinisha62347f2013-03-01 16:37:30 -050024from tempest.test import attr
Matthew Treinishce3ef922013-03-11 14:02:46 -040025from tempest.tests.image import base
Matthew Treinisha62347f2013-03-01 16:37:30 -050026
27
Matthew Treinishce3ef922013-03-11 14:02:46 -040028class CreateRegisterImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050029
30 """
31 Here we test the registration and creation of images
32 """
33
Matthew Treinisha62347f2013-03-01 16:37:30 -050034 @attr(type='negative')
35 def test_register_with_invalid_container_format(self):
36 # Negative tests for invalid data supplied to POST /images
37 self.assertRaises(exceptions.BadRequest, self.client.create_image,
38 'test', 'wrong', 'vhd')
39
40 @attr(type='negative')
41 def test_register_with_invalid_disk_format(self):
42 self.assertRaises(exceptions.BadRequest, self.client.create_image,
43 'test', 'bare', 'wrong')
44
45 @attr(type='image')
46 def test_register_then_upload(self):
47 # Register, then upload an image
Matthew Treinishce3ef922013-03-11 14:02:46 -040048 resp, body = self.create_image(name='New Name',
49 container_format='bare',
50 disk_format='raw',
51 visibility='public')
Matthew Treinisha62347f2013-03-01 16:37:30 -050052 self.assertTrue('id' in body)
53 image_id = body.get('id')
54 self.created_images.append(image_id)
55 self.assertTrue('name' in body)
56 self.assertEqual('New Name', body.get('name'))
57 self.assertTrue('visibility' in body)
58 self.assertTrue(body.get('visibility') == 'public')
59 self.assertTrue('status' in body)
60 self.assertEqual('queued', body.get('status'))
61
62 # Now try uploading an image file
63 image_file = StringIO.StringIO(('*' * 1024))
64 resp, body = self.client.store_image(image_id, image_file)
65 self.assertEqual(resp.status, 204)
66 resp, body = self.client.get_image_metadata(image_id)
67 self.assertTrue('size' in body)
68 self.assertEqual(1024, body.get('size'))
69
70
Matthew Treinishce3ef922013-03-11 14:02:46 -040071class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050072
73 """
74 Here we test the listing of image information
75 """
76
77 @classmethod
78 def setUpClass(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -040079 super(ListImagesTest, cls).setUpClass()
Matthew Treinisha62347f2013-03-01 16:37:30 -050080 # We add a few images here to test the listing functionality of
81 # the images API
82 for x in xrange(0, 10):
83 cls.created_images.append(cls._create_standard_image(x))
84
85 @classmethod
Matthew Treinisha62347f2013-03-01 16:37:30 -050086 def _create_standard_image(cls, number):
87 """
88 Create a new standard image and return the ID of the newly-registered
89 image. Note that the size of the new image is a random number between
90 1024 and 4096
91 """
92 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
93 name = 'New Standard Image %s' % number
Matthew Treinishce3ef922013-03-11 14:02:46 -040094 resp, body = cls.create_image(name=name, container_format='bare',
95 disk_format='raw',
96 visibility='public')
Matthew Treinisha62347f2013-03-01 16:37:30 -050097 image_id = body['id']
98 resp, body = cls.client.store_image(image_id, data=image_file)
99
100 return image_id
101
102 @attr(type='image')
103 def test_index_no_params(self):
104 # Simple test to see all fixture images returned
105 resp, images_list = self.client.image_list()
106 self.assertEqual(resp['status'], '200')
107 image_list = map(lambda x: x['id'], images_list)
108 for image in self.created_images:
109 self.assertTrue(image in image_list)