blob: e307c5cffd546b607e793892b9a75a7b4151f450 [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
Sean Daguec6ec4762014-05-29 08:54:21 -040038 uuid = '00000000-1111-2222-3333-444455556666'
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053039 image_name = data_utils.rand_name('image')
David Kranz34f18782015-01-06 13:43:55 -050040 body = self.create_image(name=image_name,
41 container_format='bare',
42 disk_format='raw',
43 visibility='private',
44 ramdisk_id=uuid)
Attila Fazekase191cb12013-07-29 06:41:52 +020045 self.assertIn('id', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050046 image_id = body.get('id')
Attila Fazekase191cb12013-07-29 06:41:52 +020047 self.assertIn('name', body)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053048 self.assertEqual(image_name, body['name'])
Attila Fazekase191cb12013-07-29 06:41:52 +020049 self.assertIn('visibility', body)
Aaron Rosenc7720622014-05-20 10:38:10 -070050 self.assertEqual('private', body['visibility'])
Attila Fazekase191cb12013-07-29 06:41:52 +020051 self.assertIn('status', body)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053052 self.assertEqual('queued', body['status'])
Matthew Treinisha62347f2013-03-01 16:37:30 -050053
54 # Now try uploading an image file
Mark Washenberger5c3b6fe2014-07-29 13:40:34 -070055 file_content = data_utils.random_bytes()
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053056 image_file = StringIO.StringIO(file_content)
David Kranz9c3b3b62014-06-19 16:05:53 -040057 self.client.store_image(image_id, image_file)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053058
59 # Now try to get image details
David Kranz34f18782015-01-06 13:43:55 -050060 body = self.client.get_image(image_id)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053061 self.assertEqual(image_id, body['id'])
62 self.assertEqual(image_name, body['name'])
Sean Daguec6ec4762014-05-29 08:54:21 -040063 self.assertEqual(uuid, body['ramdisk_id'])
Attila Fazekase191cb12013-07-29 06:41:52 +020064 self.assertIn('size', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050065 self.assertEqual(1024, body.get('size'))
66
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053067 # Now try get image file
David Kranz9c3b3b62014-06-19 16:05:53 -040068 _, body = self.client.get_image_file(image_id)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053069 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')
David Kranz34f18782015-01-06 13:43:55 -050077 body = self.client.create_image(name=image_name,
78 container_format='bare',
79 disk_format='raw',
80 visibility='private')
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053081 image_id = body['id']
82
83 # Delete Image
84 self.client.delete_image(image_id)
85 self.client.wait_for_resource_deletion(image_id)
86
87 # Verifying deletion
David Kranz34f18782015-01-06 13:43:55 -050088 images = self.client.image_list()
Sunil G29856a32014-07-17 23:17:58 +053089 images_id = [item['id'] for item in images]
90 self.assertNotIn(image_id, images_id)
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053091
Eiichi Aikawa9012f462014-03-05 16:43:32 +090092 @test.attr(type='gate')
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040093 def test_update_image(self):
94 # Updates an image by image_id
95
96 # Create image
97 image_name = data_utils.rand_name('image')
David Kranz34f18782015-01-06 13:43:55 -050098 body = self.client.create_image(name=image_name,
99 container_format='bare',
100 disk_format='iso',
101 visibility='private')
Masayuki Igawa930ac372014-03-11 18:50:26 +0900102 self.addCleanup(self.client.delete_image, body['id'])
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400103 self.assertEqual('queued', body['status'])
104 image_id = body['id']
105
106 # Now try uploading an image file
Mark Washenberger5c3b6fe2014-07-29 13:40:34 -0700107 image_file = StringIO.StringIO(data_utils.random_bytes())
David Kranz9c3b3b62014-06-19 16:05:53 -0400108 self.client.store_image(image_id, image_file)
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400109
110 # Update Image
111 new_image_name = data_utils.rand_name('new-image')
David Kranz34f18782015-01-06 13:43:55 -0500112 body = self.client.update_image(image_id, [
Aaron Rosenc7720622014-05-20 10:38:10 -0700113 dict(replace='/name', value=new_image_name)])
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400114
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400115 # Verifying updating
116
David Kranz34f18782015-01-06 13:43:55 -0500117 body = self.client.get_image(image_id)
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400118 self.assertEqual(image_id, body['id'])
119 self.assertEqual(new_image_name, body['name'])
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400120
Matthew Treinisha62347f2013-03-01 16:37:30 -0500121
Matthew Treinishce3ef922013-03-11 14:02:46 -0400122class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500123 """
124 Here we test the listing of image information
125 """
126
127 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +0100128 def resource_setup(cls):
129 super(ListImagesTest, cls).resource_setup()
Matthew Treinisha62347f2013-03-01 16:37:30 -0500130 # We add a few images here to test the listing functionality of
131 # the images API
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700132 cls._create_standard_image('bare', 'raw')
133 cls._create_standard_image('bare', 'raw')
134 cls._create_standard_image('ami', 'raw')
135 # Add some more for listing
136 cls._create_standard_image('ami', 'ami')
137 cls._create_standard_image('ari', 'ari')
138 cls._create_standard_image('aki', 'aki')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500139
140 @classmethod
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700141 def _create_standard_image(cls, container_format, disk_format):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500142 """
143 Create a new standard image and return the ID of the newly-registered
144 image. Note that the size of the new image is a random number between
145 1024 and 4096
146 """
Mark Washenberger5c3b6fe2014-07-29 13:40:34 -0700147 size = random.randint(1024, 4096)
148 image_file = StringIO.StringIO(data_utils.random_bytes(size))
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700149 name = data_utils.rand_name('image-')
David Kranz34f18782015-01-06 13:43:55 -0500150 body = cls.create_image(name=name,
151 container_format=container_format,
152 disk_format=disk_format,
153 visibility='private')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500154 image_id = body['id']
David Kranz9c3b3b62014-06-19 16:05:53 -0400155 cls.client.store_image(image_id, data=image_file)
Matthew Treinisha62347f2013-03-01 16:37:30 -0500156
157 return image_id
158
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700159 def _list_by_param_value_and_assert(self, params):
160 """
161 Perform list action with given params and validates result.
162 """
David Kranz34f18782015-01-06 13:43:55 -0500163 images_list = self.client.image_list(params=params)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700164 # Validating params of fetched images
165 for image in images_list:
166 for key in params:
167 msg = "Failed to list images by %s" % key
168 self.assertEqual(params[key], image[key], msg)
169
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900170 @test.attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500171 def test_index_no_params(self):
172 # Simple test to see all fixture images returned
David Kranz34f18782015-01-06 13:43:55 -0500173 images_list = self.client.image_list()
Matthew Treinisha62347f2013-03-01 16:37:30 -0500174 image_list = map(lambda x: x['id'], images_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700175
Matthew Treinisha62347f2013-03-01 16:37:30 -0500176 for image in self.created_images:
Attila Fazekase191cb12013-07-29 06:41:52 +0200177 self.assertIn(image, image_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700178
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900179 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700180 def test_list_images_param_container_format(self):
181 # Test to get all images with container_format='bare'
182 params = {"container_format": "bare"}
183 self._list_by_param_value_and_assert(params)
184
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900185 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700186 def test_list_images_param_disk_format(self):
187 # Test to get all images with disk_format = raw
188 params = {"disk_format": "raw"}
189 self._list_by_param_value_and_assert(params)
190
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900191 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700192 def test_list_images_param_visibility(self):
Aaron Rosenc7720622014-05-20 10:38:10 -0700193 # Test to get all images with visibility = private
194 params = {"visibility": "private"}
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700195 self._list_by_param_value_and_assert(params)
196
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900197 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700198 def test_list_images_param_size(self):
199 # Test to get all images by size
200 image_id = self.created_images[1]
201 # Get image metadata
David Kranz34f18782015-01-06 13:43:55 -0500202 image = self.client.get_image(image_id)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700203
204 params = {"size": image['size']}
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_min_max_size(self):
209 # Test to get all images with size between 2000 to 3000
210 image_id = self.created_images[1]
211 # Get image metadata
David Kranz34f18782015-01-06 13:43:55 -0500212 image = self.client.get_image(image_id)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700213
214 size = image['size']
215 params = {"size_min": size - 500, "size_max": size + 500}
David Kranz34f18782015-01-06 13:43:55 -0500216 images_list = self.client.image_list(params=params)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700217 image_size_list = map(lambda x: x['size'], images_list)
218
219 for image_size in image_size_list:
220 self.assertTrue(image_size >= params['size_min'] and
221 image_size <= params['size_max'],
222 "Failed to get images by size_min and size_max")
223
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900224 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700225 def test_list_images_param_status(self):
Anju Tiwarica2249d2014-01-23 17:33:02 +0530226 # Test to get all active images
227 params = {"status": "active"}
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700228 self._list_by_param_value_and_assert(params)
229
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900230 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700231 def test_list_images_param_limit(self):
232 # Test to get images by limit
233 params = {"limit": 2}
David Kranz34f18782015-01-06 13:43:55 -0500234 images_list = self.client.image_list(params=params)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700235
236 self.assertEqual(len(images_list), params['limit'],
237 "Failed to get images by limit")
raiesmh08a1ce3542014-03-04 11:58:29 +0530238
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900239 @test.attr(type='gate')
raiesmh08a1ce3542014-03-04 11:58:29 +0530240 def test_get_image_schema(self):
241 # Test to get image schema
242 schema = "image"
David Kranz34f18782015-01-06 13:43:55 -0500243 body = self.client.get_schema(schema)
raiesmh08a1ce3542014-03-04 11:58:29 +0530244 self.assertEqual("image", body['name'])
245
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900246 @test.attr(type='gate')
raiesmh08a1ce3542014-03-04 11:58:29 +0530247 def test_get_images_schema(self):
248 # Test to get images schema
249 schema = "images"
David Kranz34f18782015-01-06 13:43:55 -0500250 body = self.client.get_schema(schema)
raiesmh08a1ce3542014-03-04 11:58:29 +0530251 self.assertEqual("images", body['name'])