blob: 37dc1637cb3e88f315ada3d6747b6606c28c75c0 [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')
40 resp, body = self.create_image(name=image_name,
Matthew Treinishce3ef922013-03-11 14:02:46 -040041 container_format='bare',
42 disk_format='raw',
Aaron Rosenc7720622014-05-20 10:38:10 -070043 visibility='private',
Sean Daguec6ec4762014-05-29 08:54:21 -040044 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
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053055 file_content = '*' * 1024
56 image_file = StringIO.StringIO(file_content)
Matthew Treinisha62347f2013-03-01 16:37:30 -050057 resp, body = self.client.store_image(image_id, image_file)
58 self.assertEqual(resp.status, 204)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053059
60 # Now try to get image details
61 resp, body = self.client.get_image(image_id)
62 self.assertEqual(200, resp.status)
63 self.assertEqual(image_id, body['id'])
64 self.assertEqual(image_name, body['name'])
Sean Daguec6ec4762014-05-29 08:54:21 -040065 self.assertEqual(uuid, body['ramdisk_id'])
Attila Fazekase191cb12013-07-29 06:41:52 +020066 self.assertIn('size', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050067 self.assertEqual(1024, body.get('size'))
68
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053069 # Now try get image file
70 resp, body = self.client.get_image_file(image_id)
71 self.assertEqual(200, resp.status)
72 self.assertEqual(file_content, body)
73
Eiichi Aikawa9012f462014-03-05 16:43:32 +090074 @test.attr(type='gate')
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053075 def test_delete_image(self):
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040076 # Deletes an image by image_id
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053077
78 # Create image
79 image_name = data_utils.rand_name('image')
80 resp, body = self.client.create_image(name=image_name,
81 container_format='bare',
82 disk_format='raw',
Aaron Rosenc7720622014-05-20 10:38:10 -070083 visibility='private')
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053084 self.assertEqual(201, resp.status)
85 image_id = body['id']
86
87 # Delete Image
88 self.client.delete_image(image_id)
89 self.client.wait_for_resource_deletion(image_id)
90
91 # Verifying deletion
92 resp, images = self.client.image_list()
93 self.assertEqual(resp.status, 200)
94 self.assertNotIn(image_id, images)
95
Eiichi Aikawa9012f462014-03-05 16:43:32 +090096 @test.attr(type='gate')
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +040097 def test_update_image(self):
98 # Updates an image by image_id
99
100 # Create image
101 image_name = data_utils.rand_name('image')
102 resp, body = self.client.create_image(name=image_name,
103 container_format='bare',
104 disk_format='iso',
Aaron Rosenc7720622014-05-20 10:38:10 -0700105 visibility='private')
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400106 self.assertEqual(201, resp.status)
Masayuki Igawa930ac372014-03-11 18:50:26 +0900107 self.addCleanup(self.client.delete_image, body['id'])
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400108 self.assertEqual('queued', body['status'])
109 image_id = body['id']
110
111 # Now try uploading an image file
112 file_content = '*' * 1024
113 image_file = StringIO.StringIO(file_content)
114 resp, body = self.client.store_image(image_id, image_file)
115 self.assertEqual(204, resp.status)
116
117 # Update Image
118 new_image_name = data_utils.rand_name('new-image')
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400119 resp, body = self.client.update_image(image_id, [
Aaron Rosenc7720622014-05-20 10:38:10 -0700120 dict(replace='/name', value=new_image_name)])
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400121
122 self.assertEqual(200, resp.status)
123
124 # Verifying updating
125
126 resp, body = self.client.get_image(image_id)
127 self.assertEqual(200, resp.status)
128 self.assertEqual(image_id, body['id'])
129 self.assertEqual(new_image_name, body['name'])
Sergey Nikitinc6b2ee82014-02-03 17:13:50 +0400130
Matthew Treinisha62347f2013-03-01 16:37:30 -0500131
Matthew Treinishce3ef922013-03-11 14:02:46 -0400132class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500133 """
134 Here we test the listing of image information
135 """
136
137 @classmethod
Zhi Kun Liuca934a42014-03-24 01:30:31 -0500138 @test.safe_setup
Matthew Treinisha62347f2013-03-01 16:37:30 -0500139 def setUpClass(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -0400140 super(ListImagesTest, cls).setUpClass()
Matthew Treinisha62347f2013-03-01 16:37:30 -0500141 # We add a few images here to test the listing functionality of
142 # the images API
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700143 cls._create_standard_image('bare', 'raw')
144 cls._create_standard_image('bare', 'raw')
145 cls._create_standard_image('ami', 'raw')
146 # Add some more for listing
147 cls._create_standard_image('ami', 'ami')
148 cls._create_standard_image('ari', 'ari')
149 cls._create_standard_image('aki', 'aki')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500150
151 @classmethod
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700152 def _create_standard_image(cls, container_format, disk_format):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500153 """
154 Create a new standard image and return the ID of the newly-registered
155 image. Note that the size of the new image is a random number between
156 1024 and 4096
157 """
158 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700159 name = data_utils.rand_name('image-')
160 resp, body = cls.create_image(name=name,
161 container_format=container_format,
162 disk_format=disk_format,
Aaron Rosenc7720622014-05-20 10:38:10 -0700163 visibility='private')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500164 image_id = body['id']
165 resp, body = cls.client.store_image(image_id, data=image_file)
166
167 return image_id
168
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700169 def _list_by_param_value_and_assert(self, params):
170 """
171 Perform list action with given params and validates result.
172 """
173 resp, images_list = self.client.image_list(params=params)
174 self.assertEqual(200, resp.status)
175 # Validating params of fetched images
176 for image in images_list:
177 for key in params:
178 msg = "Failed to list images by %s" % key
179 self.assertEqual(params[key], image[key], msg)
180
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900181 @test.attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500182 def test_index_no_params(self):
183 # Simple test to see all fixture images returned
184 resp, images_list = self.client.image_list()
185 self.assertEqual(resp['status'], '200')
186 image_list = map(lambda x: x['id'], images_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700187
Matthew Treinisha62347f2013-03-01 16:37:30 -0500188 for image in self.created_images:
Attila Fazekase191cb12013-07-29 06:41:52 +0200189 self.assertIn(image, image_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700190
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_container_format(self):
193 # Test to get all images with container_format='bare'
194 params = {"container_format": "bare"}
195 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_disk_format(self):
199 # Test to get all images with disk_format = raw
200 params = {"disk_format": "raw"}
201 self._list_by_param_value_and_assert(params)
202
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900203 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700204 def test_list_images_param_visibility(self):
Aaron Rosenc7720622014-05-20 10:38:10 -0700205 # Test to get all images with visibility = private
206 params = {"visibility": "private"}
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700207 self._list_by_param_value_and_assert(params)
208
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900209 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700210 def test_list_images_param_size(self):
211 # Test to get all images by size
212 image_id = self.created_images[1]
213 # Get image metadata
214 resp, image = self.client.get_image(image_id)
215 self.assertEqual(resp['status'], '200')
216
217 params = {"size": image['size']}
218 self._list_by_param_value_and_assert(params)
219
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900220 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700221 def test_list_images_param_min_max_size(self):
222 # Test to get all images with size between 2000 to 3000
223 image_id = self.created_images[1]
224 # Get image metadata
225 resp, image = self.client.get_image(image_id)
226 self.assertEqual(resp['status'], '200')
227
228 size = image['size']
229 params = {"size_min": size - 500, "size_max": size + 500}
230 resp, images_list = self.client.image_list(params=params)
231 self.assertEqual(resp['status'], '200')
232 image_size_list = map(lambda x: x['size'], images_list)
233
234 for image_size in image_size_list:
235 self.assertTrue(image_size >= params['size_min'] and
236 image_size <= params['size_max'],
237 "Failed to get images by size_min and size_max")
238
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900239 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700240 def test_list_images_param_status(self):
Anju Tiwarica2249d2014-01-23 17:33:02 +0530241 # Test to get all active images
242 params = {"status": "active"}
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700243 self._list_by_param_value_and_assert(params)
244
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900245 @test.attr(type='gate')
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700246 def test_list_images_param_limit(self):
247 # Test to get images by limit
248 params = {"limit": 2}
249 resp, images_list = self.client.image_list(params=params)
250 self.assertEqual(resp['status'], '200')
251
252 self.assertEqual(len(images_list), params['limit'],
253 "Failed to get images by limit")
raiesmh08a1ce3542014-03-04 11:58:29 +0530254
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900255 @test.attr(type='gate')
raiesmh08a1ce3542014-03-04 11:58:29 +0530256 def test_get_image_schema(self):
257 # Test to get image schema
258 schema = "image"
259 resp, body = self.client.get_schema(schema)
260 self.assertEqual(200, resp.status)
261 self.assertEqual("image", body['name'])
262
Eiichi Aikawa9012f462014-03-05 16:43:32 +0900263 @test.attr(type='gate')
raiesmh08a1ce3542014-03-04 11:58:29 +0530264 def test_get_images_schema(self):
265 # Test to get images schema
266 schema = "images"
267 resp, body = self.client.get_schema(schema)
268 self.assertEqual(200, resp.status)
269 self.assertEqual("images", body['name'])