Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 15 | import cStringIO as StringIO |
| 16 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 17 | from tempest import clients |
Cyril Roelandt | c1482a2 | 2014-11-13 16:55:04 +0100 | [diff] [blame] | 18 | from tempest.common import credentials |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 19 | from tempest.common.utils import data_utils |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 20 | from tempest import config |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 21 | from tempest import exceptions |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 22 | from tempest.openstack.common import log as logging |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 23 | import tempest.test |
| 24 | |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 25 | CONF = config.CONF |
| 26 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 27 | LOG = logging.getLogger(__name__) |
| 28 | |
| 29 | |
| 30 | class BaseImageTest(tempest.test.BaseTestCase): |
| 31 | """Base test class for Image API tests.""" |
| 32 | |
| 33 | @classmethod |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 34 | def resource_setup(cls): |
Sylvain Afchain | 11b99d0 | 2014-01-16 00:42:33 +0100 | [diff] [blame] | 35 | cls.set_network_resources() |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 36 | super(BaseImageTest, cls).resource_setup() |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 37 | cls.created_images = [] |
Matthew Treinish | e09b3c2 | 2013-07-23 16:49:36 -0400 | [diff] [blame] | 38 | cls._interface = 'json' |
Cyril Roelandt | c1482a2 | 2014-11-13 16:55:04 +0100 | [diff] [blame] | 39 | cls.isolated_creds = credentials.get_isolated_credentials( |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 40 | cls.__name__, network_resources=cls.network_resources) |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 41 | if not CONF.service_available.glance: |
Matthew Treinish | 853ae44 | 2013-07-19 16:36:07 -0400 | [diff] [blame] | 42 | skip_msg = ("%s skipped as glance is not available" % cls.__name__) |
| 43 | raise cls.skipException(skip_msg) |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 44 | cls.os = clients.Manager(cls.isolated_creds.get_primary_creds()) |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 45 | |
| 46 | @classmethod |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 47 | def resource_cleanup(cls): |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 48 | for image_id in cls.created_images: |
| 49 | try: |
| 50 | cls.client.delete_image(image_id) |
| 51 | except exceptions.NotFound: |
| 52 | pass |
| 53 | |
| 54 | for image_id in cls.created_images: |
| 55 | cls.client.wait_for_resource_deletion(image_id) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 56 | cls.isolated_creds.clear_isolated_creds() |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 57 | super(BaseImageTest, cls).resource_cleanup() |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 58 | |
| 59 | @classmethod |
| 60 | def create_image(cls, **kwargs): |
| 61 | """Wrapper that returns a test image.""" |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 62 | name = data_utils.rand_name(cls.__name__ + "-instance") |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 63 | |
| 64 | if 'name' in kwargs: |
| 65 | name = kwargs.pop('name') |
| 66 | |
| 67 | container_format = kwargs.pop('container_format') |
| 68 | disk_format = kwargs.pop('disk_format') |
| 69 | |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 70 | image = cls.client.create_image(name, container_format, |
| 71 | disk_format, **kwargs) |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 72 | cls.created_images.append(image['id']) |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 73 | return image |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 74 | |
| 75 | |
| 76 | class BaseV1ImageTest(BaseImageTest): |
| 77 | |
| 78 | @classmethod |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 79 | def resource_setup(cls): |
| 80 | super(BaseV1ImageTest, cls).resource_setup() |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 81 | cls.client = cls.os.image_client |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 82 | if not CONF.image_feature_enabled.api_v1: |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 83 | msg = "Glance API v1 not supported" |
| 84 | raise cls.skipException(msg) |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 85 | |
| 86 | |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 87 | class BaseV1ImageMembersTest(BaseV1ImageTest): |
| 88 | @classmethod |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 89 | def resource_setup(cls): |
| 90 | super(BaseV1ImageMembersTest, cls).resource_setup() |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 91 | cls.os_alt = clients.Manager(cls.isolated_creds.get_alt_creds()) |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 92 | |
| 93 | cls.alt_img_cli = cls.os_alt.image_client |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 94 | cls.alt_tenant_id = cls.alt_img_cli.tenant_id |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 95 | |
| 96 | def _create_image(self): |
Mark Washenberger | 5c3b6fe | 2014-07-29 13:40:34 -0700 | [diff] [blame] | 97 | image_file = StringIO.StringIO(data_utils.random_bytes()) |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 98 | image = self.create_image(container_format='bare', |
| 99 | disk_format='raw', |
| 100 | is_public=False, |
| 101 | data=image_file) |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 102 | image_id = image['id'] |
| 103 | return image_id |
| 104 | |
| 105 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 106 | class BaseV2ImageTest(BaseImageTest): |
| 107 | |
| 108 | @classmethod |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 109 | def resource_setup(cls): |
| 110 | super(BaseV2ImageTest, cls).resource_setup() |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 111 | cls.client = cls.os.image_client_v2 |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 112 | if not CONF.image_feature_enabled.api_v2: |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 113 | msg = "Glance API v2 not supported" |
| 114 | raise cls.skipException(msg) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 115 | |
| 116 | |
JordanP | 236da42 | 2014-01-16 14:38:07 +0000 | [diff] [blame] | 117 | class BaseV2MemberImageTest(BaseV2ImageTest): |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 118 | |
| 119 | @classmethod |
Andrea Frittoli | 69a6b63 | 2014-09-15 13:14:53 +0100 | [diff] [blame] | 120 | def resource_setup(cls): |
| 121 | super(BaseV2MemberImageTest, cls).resource_setup() |
Andrea Frittoli | 8283b4e | 2014-07-17 13:28:58 +0100 | [diff] [blame] | 122 | creds = cls.isolated_creds.get_alt_creds() |
| 123 | cls.os_alt = clients.Manager(creds) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 124 | cls.os_img_client = cls.os.image_client_v2 |
| 125 | cls.alt_img_client = cls.os_alt.image_client_v2 |
Zhi Kun Liu | d752c60 | 2014-05-16 13:25:28 +0800 | [diff] [blame] | 126 | cls.alt_tenant_id = cls.alt_img_client.tenant_id |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 127 | |
| 128 | def _list_image_ids_as_alt(self): |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 129 | image_list = self.alt_img_client.image_list() |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 130 | image_ids = map(lambda x: x['id'], image_list) |
| 131 | return image_ids |
| 132 | |
| 133 | def _create_image(self): |
| 134 | name = data_utils.rand_name('image') |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 135 | image = self.os_img_client.create_image(name, |
| 136 | container_format='bare', |
| 137 | disk_format='raw') |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 138 | image_id = image['id'] |
| 139 | self.addCleanup(self.os_img_client.delete_image, image_id) |
| 140 | return image_id |