ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 1 | # Copyright 2013 OpenStack Foundation |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 2 | # Copyright 2013 IBM Corp |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 3 | # All Rights Reserved. |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 4 | # |
| 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 | |
| 17 | import cStringIO as StringIO |
| 18 | import random |
| 19 | |
Sean Dague | 1937d09 | 2013-05-17 16:36:38 -0400 | [diff] [blame] | 20 | from tempest.api.image import base |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 21 | from tempest.common.utils import data_utils |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 22 | from tempest.test import attr |
| 23 | |
| 24 | |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 25 | class BasicOperationsImagesTest(base.BaseV2ImageTest): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 26 | """ |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 27 | Here we test the basic operations of images |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 28 | """ |
| 29 | |
Giampaolo Lauria | fd5f595 | 2013-05-15 09:44:24 -0400 | [diff] [blame] | 30 | @attr(type='gate') |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 31 | 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 Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 40 | container_format='bare', |
| 41 | disk_format='raw', |
| 42 | visibility='public') |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 43 | self.assertIn('id', body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 44 | image_id = body.get('id') |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 45 | self.assertIn('name', body) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 46 | self.assertEqual(image_name, body['name']) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 47 | self.assertIn('visibility', body) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 48 | self.assertEqual('public', body['visibility']) |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 49 | self.assertIn('status', body) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 50 | self.assertEqual('queued', body['status']) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 51 | |
| 52 | # Now try uploading an image file |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 53 | file_content = '*' * 1024 |
| 54 | image_file = StringIO.StringIO(file_content) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 55 | resp, body = self.client.store_image(image_id, image_file) |
| 56 | self.assertEqual(resp.status, 204) |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 57 | |
| 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 Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 63 | self.assertIn('size', body) |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 64 | self.assertEqual(1024, body.get('size')) |
| 65 | |
Hoisaleshwara Madan V S | e50a6f1 | 2013-10-23 18:01:01 +0530 | [diff] [blame] | 66 | # 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 | |
Hoisaleshwara Madan V S | 7cba108 | 2013-11-26 12:43:04 +0530 | [diff] [blame] | 71 | @attr(type='gate') |
| 72 | def test_delete_image(self): |
| 73 | # Deletes a image by image_id |
| 74 | |
| 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 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 93 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 94 | class ListImagesTest(base.BaseV2ImageTest): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 95 | """ |
| 96 | Here we test the listing of image information |
| 97 | """ |
| 98 | |
| 99 | @classmethod |
| 100 | def setUpClass(cls): |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 101 | super(ListImagesTest, cls).setUpClass() |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 102 | # We add a few images here to test the listing functionality of |
| 103 | # the images API |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 104 | cls._create_standard_image('bare', 'raw') |
| 105 | cls._create_standard_image('bare', 'raw') |
| 106 | cls._create_standard_image('ami', 'raw') |
| 107 | # Add some more for listing |
| 108 | cls._create_standard_image('ami', 'ami') |
| 109 | cls._create_standard_image('ari', 'ari') |
| 110 | cls._create_standard_image('aki', 'aki') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 111 | |
| 112 | @classmethod |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 113 | def _create_standard_image(cls, container_format, disk_format): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 114 | """ |
| 115 | Create a new standard image and return the ID of the newly-registered |
| 116 | image. Note that the size of the new image is a random number between |
| 117 | 1024 and 4096 |
| 118 | """ |
| 119 | image_file = StringIO.StringIO('*' * random.randint(1024, 4096)) |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 120 | name = data_utils.rand_name('image-') |
| 121 | resp, body = cls.create_image(name=name, |
| 122 | container_format=container_format, |
| 123 | disk_format=disk_format, |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 124 | visibility='public') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 125 | image_id = body['id'] |
| 126 | resp, body = cls.client.store_image(image_id, data=image_file) |
| 127 | |
| 128 | return image_id |
| 129 | |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 130 | def _list_by_param_value_and_assert(self, params): |
| 131 | """ |
| 132 | Perform list action with given params and validates result. |
| 133 | """ |
| 134 | resp, images_list = self.client.image_list(params=params) |
| 135 | self.assertEqual(200, resp.status) |
| 136 | # Validating params of fetched images |
| 137 | for image in images_list: |
| 138 | for key in params: |
| 139 | msg = "Failed to list images by %s" % key |
| 140 | self.assertEqual(params[key], image[key], msg) |
| 141 | |
Giampaolo Lauria | fd5f595 | 2013-05-15 09:44:24 -0400 | [diff] [blame] | 142 | @attr(type='gate') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 143 | def test_index_no_params(self): |
| 144 | # Simple test to see all fixture images returned |
| 145 | resp, images_list = self.client.image_list() |
| 146 | self.assertEqual(resp['status'], '200') |
| 147 | image_list = map(lambda x: x['id'], images_list) |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 148 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 149 | for image in self.created_images: |
Attila Fazekas | e191cb1 | 2013-07-29 06:41:52 +0200 | [diff] [blame] | 150 | self.assertIn(image, image_list) |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 151 | |
| 152 | @attr(type='gate') |
| 153 | def test_list_images_param_container_format(self): |
| 154 | # Test to get all images with container_format='bare' |
| 155 | params = {"container_format": "bare"} |
| 156 | self._list_by_param_value_and_assert(params) |
| 157 | |
| 158 | @attr(type='gate') |
| 159 | def test_list_images_param_disk_format(self): |
| 160 | # Test to get all images with disk_format = raw |
| 161 | params = {"disk_format": "raw"} |
| 162 | self._list_by_param_value_and_assert(params) |
| 163 | |
| 164 | @attr(type='gate') |
| 165 | def test_list_images_param_visibility(self): |
| 166 | # Test to get all images with visibility = public |
| 167 | params = {"visibility": "public"} |
| 168 | self._list_by_param_value_and_assert(params) |
| 169 | |
| 170 | @attr(type='gate') |
| 171 | def test_list_images_param_size(self): |
| 172 | # Test to get all images by size |
| 173 | image_id = self.created_images[1] |
| 174 | # Get image metadata |
| 175 | resp, image = self.client.get_image(image_id) |
| 176 | self.assertEqual(resp['status'], '200') |
| 177 | |
| 178 | params = {"size": image['size']} |
| 179 | self._list_by_param_value_and_assert(params) |
| 180 | |
| 181 | @attr(type='gate') |
| 182 | def test_list_images_param_min_max_size(self): |
| 183 | # Test to get all images with size between 2000 to 3000 |
| 184 | image_id = self.created_images[1] |
| 185 | # Get image metadata |
| 186 | resp, image = self.client.get_image(image_id) |
| 187 | self.assertEqual(resp['status'], '200') |
| 188 | |
| 189 | size = image['size'] |
| 190 | params = {"size_min": size - 500, "size_max": size + 500} |
| 191 | resp, images_list = self.client.image_list(params=params) |
| 192 | self.assertEqual(resp['status'], '200') |
| 193 | image_size_list = map(lambda x: x['size'], images_list) |
| 194 | |
| 195 | for image_size in image_size_list: |
| 196 | self.assertTrue(image_size >= params['size_min'] and |
| 197 | image_size <= params['size_max'], |
| 198 | "Failed to get images by size_min and size_max") |
| 199 | |
| 200 | @attr(type='gate') |
| 201 | def test_list_images_param_status(self): |
Anju Tiwari | ca2249d | 2014-01-23 17:33:02 +0530 | [diff] [blame] | 202 | # Test to get all active images |
| 203 | params = {"status": "active"} |
Abhijeet Malawade | f268d8e | 2013-09-17 06:20:23 -0700 | [diff] [blame] | 204 | self._list_by_param_value_and_assert(params) |
| 205 | |
| 206 | @attr(type='gate') |
| 207 | def test_list_images_param_limit(self): |
| 208 | # Test to get images by limit |
| 209 | params = {"limit": 2} |
| 210 | resp, images_list = self.client.image_list(params=params) |
| 211 | self.assertEqual(resp['status'], '200') |
| 212 | |
| 213 | self.assertEqual(len(images_list), params['limit'], |
| 214 | "Failed to get images by limit") |