Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2013 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # Copyright 2013 IBM Corp |
| 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 | |
| 22 | from tempest import clients |
| 23 | from tempest import exceptions |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 24 | from tempest.test import attr |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 25 | from tempest.tests.image import base |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 26 | |
| 27 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 28 | class CreateRegisterImagesTest(base.BaseV2ImageTest): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 29 | |
| 30 | """ |
| 31 | Here we test the registration and creation of images |
| 32 | """ |
| 33 | |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 34 | @attr(type='negative') |
| 35 | def test_register_with_invalid_container_format(self): |
| 36 | # Negative tests for invalid data supplied to POST /images |
| 37 | self.assertRaises(exceptions.BadRequest, self.client.create_image, |
| 38 | 'test', 'wrong', 'vhd') |
| 39 | |
| 40 | @attr(type='negative') |
| 41 | def test_register_with_invalid_disk_format(self): |
| 42 | self.assertRaises(exceptions.BadRequest, self.client.create_image, |
| 43 | 'test', 'bare', 'wrong') |
| 44 | |
| 45 | @attr(type='image') |
| 46 | def test_register_then_upload(self): |
| 47 | # Register, then upload an image |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 48 | resp, body = self.create_image(name='New Name', |
| 49 | container_format='bare', |
| 50 | disk_format='raw', |
| 51 | visibility='public') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 52 | self.assertTrue('id' in body) |
| 53 | image_id = body.get('id') |
| 54 | self.created_images.append(image_id) |
| 55 | self.assertTrue('name' in body) |
| 56 | self.assertEqual('New Name', body.get('name')) |
| 57 | self.assertTrue('visibility' in body) |
| 58 | self.assertTrue(body.get('visibility') == 'public') |
| 59 | self.assertTrue('status' in body) |
| 60 | self.assertEqual('queued', body.get('status')) |
| 61 | |
| 62 | # Now try uploading an image file |
| 63 | image_file = StringIO.StringIO(('*' * 1024)) |
| 64 | resp, body = self.client.store_image(image_id, image_file) |
| 65 | self.assertEqual(resp.status, 204) |
| 66 | resp, body = self.client.get_image_metadata(image_id) |
| 67 | self.assertTrue('size' in body) |
| 68 | self.assertEqual(1024, body.get('size')) |
| 69 | |
| 70 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 71 | class ListImagesTest(base.BaseV2ImageTest): |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 72 | |
| 73 | """ |
| 74 | Here we test the listing of image information |
| 75 | """ |
| 76 | |
| 77 | @classmethod |
| 78 | def setUpClass(cls): |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 79 | super(ListImagesTest, cls).setUpClass() |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 80 | # We add a few images here to test the listing functionality of |
| 81 | # the images API |
| 82 | for x in xrange(0, 10): |
| 83 | cls.created_images.append(cls._create_standard_image(x)) |
| 84 | |
| 85 | @classmethod |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 86 | def _create_standard_image(cls, number): |
| 87 | """ |
| 88 | Create a new standard image and return the ID of the newly-registered |
| 89 | image. Note that the size of the new image is a random number between |
| 90 | 1024 and 4096 |
| 91 | """ |
| 92 | image_file = StringIO.StringIO('*' * random.randint(1024, 4096)) |
| 93 | name = 'New Standard Image %s' % number |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 94 | resp, body = cls.create_image(name=name, container_format='bare', |
| 95 | disk_format='raw', |
| 96 | visibility='public') |
Matthew Treinish | a62347f | 2013-03-01 16:37:30 -0500 | [diff] [blame] | 97 | image_id = body['id'] |
| 98 | resp, body = cls.client.store_image(image_id, data=image_file) |
| 99 | |
| 100 | return image_id |
| 101 | |
| 102 | @attr(type='image') |
| 103 | def test_index_no_params(self): |
| 104 | # Simple test to see all fixture images returned |
| 105 | resp, images_list = self.client.image_list() |
| 106 | self.assertEqual(resp['status'], '200') |
| 107 | image_list = map(lambda x: x['id'], images_list) |
| 108 | for image in self.created_images: |
| 109 | self.assertTrue(image in image_list) |