Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2013 OpenStack Foundation |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 4 | # Copyright 2013 IBM Corp |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 5 | # All Rights Reserved. |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 6 | # |
| 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 | |
| 19 | import cStringIO as StringIO |
| 20 | import random |
| 21 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 22 | from tempest.api.image import base |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 23 | from tempest.common.utils import data_utils |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 24 | from tempest.test import attr |
| 25 | |
| 26 | |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 27 | class BasicOperationsImagesTest(base.BaseV2ImageTest): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 28 | """ |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 29 | Here we test the basic operations of images |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 30 | """ |
| 31 | |
Giampaolo Lauria | fd5f595 | 2013-05-15 09:44:24 -0400 | [diff] [blame] | 32 | @attr(type='gate') |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 33 | 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 Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 42 | container_format='bare', |
| 43 | disk_format='raw', |
| 44 | visibility='public') |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 45 | self.assertIn('id', body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 46 | image_id = body.get('id') |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 47 | self.assertIn('name', body) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 48 | self.assertEqual(image_name, body['name']) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 49 | self.assertIn('visibility', body) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 50 | self.assertEqual('public', body['visibility']) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 51 | self.assertIn('status', body) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 52 | self.assertEqual('queued', body['status']) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 53 | |
| 54 | # Now try uploading an image file |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 55 | file_content = '*' * 1024 |
| 56 | image_file = StringIO.StringIO(file_content) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 57 | resp, body = self.client.store_image(image_id, image_file) |
| 58 | self.assertEqual(resp.status, 204) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 59 | |
| 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 Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 65 | self.assertIn('size', body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 66 | self.assertEqual(1024, body.get('size')) |
| 67 | |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 68 | # 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 S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 73 | @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 Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 95 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 96 | class ListImagesTest(base.BaseV2ImageTest): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 97 | """ |
| 98 | Here we test the listing of image information |
| 99 | """ |
| 100 | |
| 101 | @classmethod |
| 102 | def setUpClass(cls): |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 103 | super(ListImagesTest, cls).setUpClass() |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 104 | # We add a few images here to test the listing functionality of |
| 105 | # the images API |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame^] | 106 | 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 Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 113 | |
| 114 | @classmethod |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame^] | 115 | def _create_standard_image(cls, container_format, disk_format): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 116 | """ |
| 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 Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame^] | 122 | 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 Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 126 | visibility='public') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 127 | image_id = body['id'] |
| 128 | resp, body = cls.client.store_image(image_id, data=image_file) |
| 129 | |
| 130 | return image_id |
| 131 | |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame^] | 132 | 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 Lauria | fd5f595 | 2013-05-15 09:44:24 -0400 | [diff] [blame] | 144 | @attr(type='gate') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 145 | 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 Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame^] | 150 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 151 | for image in self.created_images: |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 152 | self.assertIn(image, image_list) |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame^] | 153 | |
| 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") |