blob: ce11911e24789d0c331d64dc82fc66422c3c8930 [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
Eiichi Aikawa9012f462014-03-05 16:43:32 +090022from tempest import test
Matthew Treinisha62347f2013-03-01 16:37:30 -050023
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
Eiichi Aikawa9012f462014-03-05 16:43:32 +090030 @test.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
Eiichi Aikawa9012f462014-03-05 16:43:32 +090071 @test.attr(type='gate')
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053072 def test_delete_image(self):
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040073 # Deletes an image by image_id
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053074
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
Eiichi Aikawa9012f462014-03-05 16:43:32 +090093 @test.attr(type='gate')
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040094 def test_update_image(self):
95 # Updates an image by image_id
96
97 # Create image
98 image_name = data_utils.rand_name('image')
99 resp, body = self.client.create_image(name=image_name,
100 container_format='bare',
101 disk_format='iso',
102 visibility='public')
103 self.assertEqual(201, resp.status)
104 self.assertEqual('queued', body['status'])
105 image_id = body['id']
106
107 # Now try uploading an image file
108 file_content = '*' * 1024
109 image_file = StringIO.StringIO(file_content)
110 resp, body = self.client.store_image(image_id, image_file)
111 self.assertEqual(204, resp.status)
112
113 # Update Image
114 new_image_name = data_utils.rand_name('new-image')
115 new_visibility = 'private'
116 resp, body = self.client.update_image(image_id, [
117 dict(replace='/name', value=new_image_name),
118 dict(replace='/visibility', value=new_visibility)])
119
120 self.assertEqual(200, resp.status)
121
122 # Verifying updating
123
124 resp, body = self.client.get_image(image_id)
125 self.assertEqual(200, resp.status)
126 self.assertEqual(image_id, body['id'])
127 self.assertEqual(new_image_name, body['name'])
128 self.assertEqual(new_visibility, body['visibility'])
129
Matthew Treinisha62347f2013-03-01 16:37:30 -0500130
Matthew Treinishce3ef922013-03-11 14:02:46 -0400131class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500132 """
133 Here we test the listing of image information
134 """
135
136 @classmethod
137 def setUpClass(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -0400138 super(ListImagesTest, cls).setUpClass()
Matthew Treinisha62347f2013-03-01 16:37:30 -0500139 # We add a few images here to test the listing functionality of
140 # the images API
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700141 cls._create_standard_image('bare', 'raw')
142 cls._create_standard_image('bare', 'raw')
143 cls._create_standard_image('ami', 'raw')
144 # Add some more for listing
145 cls._create_standard_image('ami', 'ami')
146 cls._create_standard_image('ari', 'ari')
147 cls._create_standard_image('aki', 'aki')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500148
149 @classmethod
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700150 def _create_standard_image(cls, container_format, disk_format):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500151 """
152 Create a new standard image and return the ID of the newly-registered
153 image. Note that the size of the new image is a random number between
154 1024 and 4096
155 """
156 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700157 name = data_utils.rand_name('image-')
158 resp, body = cls.create_image(name=name,
159 container_format=container_format,
160 disk_format=disk_format,
Matthew Treinishce3ef922013-03-11 14:02:46 -0400161 visibility='public')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500162 image_id = body['id']
163 resp, body = cls.client.store_image(image_id, data=image_file)
164
165 return image_id
166
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700167 def _list_by_param_value_and_assert(self, params):
168 """
169 Perform list action with given params and validates result.
170 """
171 resp, images_list = self.client.image_list(params=params)
172 self.assertEqual(200, resp.status)
173 # Validating params of fetched images
174 for image in images_list:
175 for key in params:
176 msg = "Failed to list images by %s" % key
177 self.assertEqual(params[key], image[key], msg)
178
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900179 @test.attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500180 def test_index_no_params(self):
181 # Simple test to see all fixture images returned
182 resp, images_list = self.client.image_list()
183 self.assertEqual(resp['status'], '200')
184 image_list = map(lambda x: x['id'], images_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700185
Matthew Treinisha62347f2013-03-01 16:37:30 -0500186 for image in self.created_images:
Attila Fazekase191cb12013-07-29 06:41:52 +0200187 self.assertIn(image, image_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700188
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900189 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700190 def test_list_images_param_container_format(self):
191 # Test to get all images with container_format='bare'
192 params = {"container_format": "bare"}
193 self._list_by_param_value_and_assert(params)
194
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900195 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700196 def test_list_images_param_disk_format(self):
197 # Test to get all images with disk_format = raw
198 params = {"disk_format": "raw"}
199 self._list_by_param_value_and_assert(params)
200
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900201 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700202 def test_list_images_param_visibility(self):
203 # Test to get all images with visibility = public
204 params = {"visibility": "public"}
205 self._list_by_param_value_and_assert(params)
206
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900207 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700208 def test_list_images_param_size(self):
209 # Test to get all images by size
210 image_id = self.created_images[1]
211 # Get image metadata
212 resp, image = self.client.get_image(image_id)
213 self.assertEqual(resp['status'], '200')
214
215 params = {"size": image['size']}
216 self._list_by_param_value_and_assert(params)
217
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900218 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700219 def test_list_images_param_min_max_size(self):
220 # Test to get all images with size between 2000 to 3000
221 image_id = self.created_images[1]
222 # Get image metadata
223 resp, image = self.client.get_image(image_id)
224 self.assertEqual(resp['status'], '200')
225
226 size = image['size']
227 params = {"size_min": size - 500, "size_max": size + 500}
228 resp, images_list = self.client.image_list(params=params)
229 self.assertEqual(resp['status'], '200')
230 image_size_list = map(lambda x: x['size'], images_list)
231
232 for image_size in image_size_list:
233 self.assertTrue(image_size >= params['size_min'] and
234 image_size <= params['size_max'],
235 "Failed to get images by size_min and size_max")
236
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900237 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700238 def test_list_images_param_status(self):
Anju Tiwarica2249d2014-01-23 17:33:02 +0530239 # Test to get all active images
240 params = {"status": "active"}
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700241 self._list_by_param_value_and_assert(params)
242
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900243 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700244 def test_list_images_param_limit(self):
245 # Test to get images by limit
246 params = {"limit": 2}
247 resp, images_list = self.client.image_list(params=params)
248 self.assertEqual(resp['status'], '200')
249
250 self.assertEqual(len(images_list), params['limit'],
251 "Failed to get images by limit")
raiesmh08a1ce3542014-03-04 11:58:29 +0530252
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900253 @test.attr(type='gate')
raiesmh08a1ce3542014-03-04 11:58:29 +0530254 def test_get_image_schema(self):
255 # Test to get image schema
256 schema = "image"
257 resp, body = self.client.get_schema(schema)
258 self.assertEqual(200, resp.status)
259 self.assertEqual("image", body['name'])
260
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900261 @test.attr(type='gate')
raiesmh08a1ce3542014-03-04 11:58:29 +0530262 def test_get_images_schema(self):
263 # Test to get images schema
264 schema = "images"
265 resp, body = self.client.get_schema(schema)
266 self.assertEqual(200, resp.status)
267 self.assertEqual("images", body['name'])