blob: 090f31f6e161f535a96bca0ffe4e19f044fe5f9b [file] [log] [blame]
Matthew Treinisha62347f2013-03-01 16:37:30 -05001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2013 OpenStack Foundation
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +05304# Copyright 2013 IBM Corp
Matthew Treinisha62347f2013-03-01 16:37:30 -05005# All Rights Reserved.
Matthew Treinisha62347f2013-03-01 16:37:30 -05006#
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
Sean Dague1937d092013-05-17 16:36:38 -040022from tempest.api.image import base
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053023from tempest.common.utils import data_utils
Matthew Treinisha62347f2013-03-01 16:37:30 -050024from tempest.test import attr
25
26
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053027class BasicOperationsImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050028 """
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053029 Here we test the basic operations of images
Matthew Treinisha62347f2013-03-01 16:37:30 -050030 """
31
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -040032 @attr(type='gate')
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053033 def test_register_upload_get_image_file(self):
34
35 """
36 Here we test these functionalities - Register image,
37 upload the image file, get image and get image file api's
38 """
39
40 image_name = data_utils.rand_name('image')
41 resp, body = self.create_image(name=image_name,
Matthew Treinishce3ef922013-03-11 14:02:46 -040042 container_format='bare',
43 disk_format='raw',
44 visibility='public')
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)
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053050 self.assertEqual('public', 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'])
Attila Fazekase191cb12013-07-29 06:41:52 +020065 self.assertIn('size', body)
Matthew Treinisha62347f2013-03-01 16:37:30 -050066 self.assertEqual(1024, body.get('size'))
67
Hoisaleshwara Madan V Se50a6f12013-10-23 18:01:01 +053068 # Now try get image file
69 resp, body = self.client.get_image_file(image_id)
70 self.assertEqual(200, resp.status)
71 self.assertEqual(file_content, body)
72
Hoisaleshwara Madan V S7cba1082013-11-26 12:43:04 +053073 @attr(type='gate')
74 def test_delete_image(self):
75 # Deletes a image by image_id
76
77 # Create image
78 image_name = data_utils.rand_name('image')
79 resp, body = self.client.create_image(name=image_name,
80 container_format='bare',
81 disk_format='raw',
82 visibility='public')
83 self.assertEqual(201, resp.status)
84 image_id = body['id']
85
86 # Delete Image
87 self.client.delete_image(image_id)
88 self.client.wait_for_resource_deletion(image_id)
89
90 # Verifying deletion
91 resp, images = self.client.image_list()
92 self.assertEqual(resp.status, 200)
93 self.assertNotIn(image_id, images)
94
Matthew Treinisha62347f2013-03-01 16:37:30 -050095
Matthew Treinishce3ef922013-03-11 14:02:46 -040096class ListImagesTest(base.BaseV2ImageTest):
Matthew Treinisha62347f2013-03-01 16:37:30 -050097 """
98 Here we test the listing of image information
99 """
100
101 @classmethod
102 def setUpClass(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -0400103 super(ListImagesTest, cls).setUpClass()
Matthew Treinisha62347f2013-03-01 16:37:30 -0500104 # We add a few images here to test the listing functionality of
105 # the images API
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700106 cls._create_standard_image('bare', 'raw')
107 cls._create_standard_image('bare', 'raw')
108 cls._create_standard_image('ami', 'raw')
109 # Add some more for listing
110 cls._create_standard_image('ami', 'ami')
111 cls._create_standard_image('ari', 'ari')
112 cls._create_standard_image('aki', 'aki')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500113
114 @classmethod
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700115 def _create_standard_image(cls, container_format, disk_format):
Matthew Treinisha62347f2013-03-01 16:37:30 -0500116 """
117 Create a new standard image and return the ID of the newly-registered
118 image. Note that the size of the new image is a random number between
119 1024 and 4096
120 """
121 image_file = StringIO.StringIO('*' * random.randint(1024, 4096))
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700122 name = data_utils.rand_name('image-')
123 resp, body = cls.create_image(name=name,
124 container_format=container_format,
125 disk_format=disk_format,
Matthew Treinishce3ef922013-03-11 14:02:46 -0400126 visibility='public')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500127 image_id = body['id']
128 resp, body = cls.client.store_image(image_id, data=image_file)
129
130 return image_id
131
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700132 def _list_by_param_value_and_assert(self, params):
133 """
134 Perform list action with given params and validates result.
135 """
136 resp, images_list = self.client.image_list(params=params)
137 self.assertEqual(200, resp.status)
138 # Validating params of fetched images
139 for image in images_list:
140 for key in params:
141 msg = "Failed to list images by %s" % key
142 self.assertEqual(params[key], image[key], msg)
143
Giampaolo Lauriafd5f5952013-05-15 09:44:24 -0400144 @attr(type='gate')
Matthew Treinisha62347f2013-03-01 16:37:30 -0500145 def test_index_no_params(self):
146 # Simple test to see all fixture images returned
147 resp, images_list = self.client.image_list()
148 self.assertEqual(resp['status'], '200')
149 image_list = map(lambda x: x['id'], images_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700150
Matthew Treinisha62347f2013-03-01 16:37:30 -0500151 for image in self.created_images:
Attila Fazekase191cb12013-07-29 06:41:52 +0200152 self.assertIn(image, image_list)
Abhijeet Malawadef268d8e2013-09-17 06:20:23 -0700153
154 @attr(type='gate')
155 def test_list_images_param_container_format(self):
156 # Test to get all images with container_format='bare'
157 params = {"container_format": "bare"}
158 self._list_by_param_value_and_assert(params)
159
160 @attr(type='gate')
161 def test_list_images_param_disk_format(self):
162 # Test to get all images with disk_format = raw
163 params = {"disk_format": "raw"}
164 self._list_by_param_value_and_assert(params)
165
166 @attr(type='gate')
167 def test_list_images_param_visibility(self):
168 # Test to get all images with visibility = public
169 params = {"visibility": "public"}
170 self._list_by_param_value_and_assert(params)
171
172 @attr(type='gate')
173 def test_list_images_param_size(self):
174 # Test to get all images by size
175 image_id = self.created_images[1]
176 # Get image metadata
177 resp, image = self.client.get_image(image_id)
178 self.assertEqual(resp['status'], '200')
179
180 params = {"size": image['size']}
181 self._list_by_param_value_and_assert(params)
182
183 @attr(type='gate')
184 def test_list_images_param_min_max_size(self):
185 # Test to get all images with size between 2000 to 3000
186 image_id = self.created_images[1]
187 # Get image metadata
188 resp, image = self.client.get_image(image_id)
189 self.assertEqual(resp['status'], '200')
190
191 size = image['size']
192 params = {"size_min": size - 500, "size_max": size + 500}
193 resp, images_list = self.client.image_list(params=params)
194 self.assertEqual(resp['status'], '200')
195 image_size_list = map(lambda x: x['size'], images_list)
196
197 for image_size in image_size_list:
198 self.assertTrue(image_size >= params['size_min'] and
199 image_size <= params['size_max'],
200 "Failed to get images by size_min and size_max")
201
202 @attr(type='gate')
203 def test_list_images_param_status(self):
204 # Test to get all available images
205 params = {"status": "available"}
206 self._list_by_param_value_and_assert(params)
207
208 @attr(type='gate')
209 def test_list_images_param_limit(self):
210 # Test to get images by limit
211 params = {"limit": 2}
212 resp, images_list = self.client.image_list(params=params)
213 self.assertEqual(resp['status'], '200')
214
215 self.assertEqual(len(images_list), params['limit'],
216 "Failed to get images by limit")