blob: 2a5401f07c7757e311a945da33f603c0f43942b6 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2013 OpenStack Foundation
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +05302# Copyright 2013 IBM Corp
Matthew Treinisha62347f2013-03-01 16:37:30 -05003# All Rights Reserved.
Matthew Treinisha62347f2013-03-01 16:37:30 -05004#
5# Licensed under the Apache License, Version 2.0 (the "License"); you may
6# not use this file except in compliance with the License. You may obtain
7# a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14# License for the specific language governing permissions and limitations
15# under the License.
16
17import cStringIO as StringIO
18import random
19
Sean Dague1937d092013-05-17 16:36:38 -040020from tempest.api.image import base
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053021from tempest.common.utils import data_utils
Matthew Treinisha62347f2013-03-01 16:37:30 -050022from tempest.test import attr
23
24
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053025class BasicOperationsImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050026 """
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053027 Here we test the basic operations of images
Matthew Treinisha62347f2013-03-01 16:37:30 -050028 """
29
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -040030 @attr(type='gate')
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053031 def test_register_upload_get_image_file(self):
32
33 """
34 Here we test these functionalities - Register image,
35 upload the image file, get image and get image file api's
36 """
37
38 image_name = data_utils.rand_name('image')
39 resp, body = self.create_image(name=image_name,
Matthew Treinishce3ef922013-03-11 14:02:46 -040040 container_format='bare',
41 disk_format='raw',
42 visibility='public')
Attila Fazekase191cb12013-07-29 06:41:52 +020043 self.assertIn('id', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050044 image_id = body.get('id')
Attila Fazekase191cb12013-07-29 06:41:52 +020045 self.assertIn('name', body)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053046 self.assertEqual(image_name, body['name'])
Attila Fazekase191cb12013-07-29 06:41:52 +020047 self.assertIn('visibility', body)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053048 self.assertEqual('public', body['visibility'])
Attila Fazekase191cb12013-07-29 06:41:52 +020049 self.assertIn('status', body)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053050 self.assertEqual('queued', body['status'])
Matthew Treinisha62347f2013-03-01 16:37:30 -050051
52 # Now try uploading an image file
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053053 file_content = '*' * 1024
54 image_file = StringIO.StringIO(file_content)
Matthew Treinisha62347f2013-03-01 16:37:30 -050055 resp, body = self.client.store_image(image_id, image_file)
56 self.assertEqual(resp.status, 204)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053057
58 # Now try to get image details
59 resp, body = self.client.get_image(image_id)
60 self.assertEqual(200, resp.status)
61 self.assertEqual(image_id, body['id'])
62 self.assertEqual(image_name, body['name'])
Attila Fazekase191cb12013-07-29 06:41:52 +020063 self.assertIn('size', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050064 self.assertEqual(1024, body.get('size'))
65
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053066 # Now try get image file
67 resp, body = self.client.get_image_file(image_id)
68 self.assertEqual(200, resp.status)
69 self.assertEqual(file_content, body)
70
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053071 @attr(type='gate')
72 def test_delete_image(self):
73 # Deletes a image by image_id
74
75 # Create image
76 image_name = data_utils.rand_name('image')
77 resp, body = self.client.create_image(name=image_name,
78 container_format='bare',
79 disk_format='raw',
80 visibility='public')
81 self.assertEqual(201, resp.status)
82 image_id = body['id']
83
84 # Delete Image
85 self.client.delete_image(image_id)
86 self.client.wait_for_resource_deletion(image_id)
87
88 # Verifying deletion
89 resp, images = self.client.image_list()
90 self.assertEqual(resp.status, 200)
91 self.assertNotIn(image_id, images)
92
Matthew Treinisha62347f2013-03-01 16:37:30 -050093
Matthew Treinishce3ef922013-03-11 14:02:46 -040094class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050095 """
96 Here we test the listing of image information
97 """
98
99 @classmethod
100 def setUpClass(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -0400101 super(ListImagesTest, cls).setUpClass()
Matthew Treinisha62347f2013-03-01 16:37:30 -0500102 # We add a few images here to test the listing functionality of
103 # the images API
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700104 cls._create_standard_image('bare', 'raw')
105 cls._create_standard_image('bare', 'raw')
106 cls._create_standard_image('ami', 'raw')
107 # Add some more for listing
108 cls._create_standard_image('ami', 'ami')
109 cls._create_standard_image('ari', 'ari')
110 cls._create_standard_image('aki', 'aki')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500111
112 @classmethod
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700113 def _create_standard_image(cls, container_format, disk_format):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500114 """
115 Create a new standard image and return the ID of the newly-registered
116 image. Note that the size of the new image is a random number between
117 1024 and 4096
118 """
119 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700120 name = data_utils.rand_name('image-')
121 resp, body = cls.create_image(name=name,
122 container_format=container_format,
123 disk_format=disk_format,
Matthew Treinishce3ef922013-03-11 14:02:46 -0400124 visibility='public')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500125 image_id = body['id']
126 resp, body = cls.client.store_image(image_id, data=image_file)
127
128 return image_id
129
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700130 def _list_by_param_value_and_assert(self, params):
131 """
132 Perform list action with given params and validates result.
133 """
134 resp, images_list = self.client.image_list(params=params)
135 self.assertEqual(200, resp.status)
136 # Validating params of fetched images
137 for image in images_list:
138 for key in params:
139 msg = "Failed to list images by %s" % key
140 self.assertEqual(params[key], image[key], msg)
141
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -0400142 @attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500143 def test_index_no_params(self):
144 # Simple test to see all fixture images returned
145 resp, images_list = self.client.image_list()
146 self.assertEqual(resp['status'], '200')
147 image_list = map(lambda x: x['id'], images_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700148
Matthew Treinisha62347f2013-03-01 16:37:30 -0500149 for image in self.created_images:
Attila Fazekase191cb12013-07-29 06:41:52 +0200150 self.assertIn(image, image_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700151
152 @attr(type='gate')
153 def test_list_images_param_container_format(self):
154 # Test to get all images with container_format='bare'
155 params = {"container_format": "bare"}
156 self._list_by_param_value_and_assert(params)
157
158 @attr(type='gate')
159 def test_list_images_param_disk_format(self):
160 # Test to get all images with disk_format = raw
161 params = {"disk_format": "raw"}
162 self._list_by_param_value_and_assert(params)
163
164 @attr(type='gate')
165 def test_list_images_param_visibility(self):
166 # Test to get all images with visibility = public
167 params = {"visibility": "public"}
168 self._list_by_param_value_and_assert(params)
169
170 @attr(type='gate')
171 def test_list_images_param_size(self):
172 # Test to get all images by size
173 image_id = self.created_images[1]
174 # Get image metadata
175 resp, image = self.client.get_image(image_id)
176 self.assertEqual(resp['status'], '200')
177
178 params = {"size": image['size']}
179 self._list_by_param_value_and_assert(params)
180
181 @attr(type='gate')
182 def test_list_images_param_min_max_size(self):
183 # Test to get all images with size between 2000 to 3000
184 image_id = self.created_images[1]
185 # Get image metadata
186 resp, image = self.client.get_image(image_id)
187 self.assertEqual(resp['status'], '200')
188
189 size = image['size']
190 params = {"size_min": size - 500, "size_max": size + 500}
191 resp, images_list = self.client.image_list(params=params)
192 self.assertEqual(resp['status'], '200')
193 image_size_list = map(lambda x: x['size'], images_list)
194
195 for image_size in image_size_list:
196 self.assertTrue(image_size >= params['size_min'] and
197 image_size <= params['size_max'],
198 "Failed to get images by size_min and size_max")
199
200 @attr(type='gate')
201 def test_list_images_param_status(self):
Anju Tiwarica2249d2014-01-23 17:33:02 +0530202 # Test to get all active images
203 params = {"status": "active"}
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700204 self._list_by_param_value_and_assert(params)
205
206 @attr(type='gate')
207 def test_list_images_param_limit(self):
208 # Test to get images by limit
209 params = {"limit": 2}
210 resp, images_list = self.client.image_list(params=params)
211 self.assertEqual(resp['status'], '200')
212
213 self.assertEqual(len(images_list), params['limit'],
214 "Failed to get images by limit")