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 |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 18 | from tempest.common import isolated_creds |
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 |
| 34 | def setUpClass(cls): |
Sylvain Afchain | 11b99d0 | 2014-01-16 00:42:33 +0100 | [diff] [blame] | 35 | cls.set_network_resources() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 36 | super(BaseImageTest, cls).setUpClass() |
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' |
Matthew Treinish | 9f756a0 | 2014-01-15 10:26:07 -0500 | [diff] [blame] | 39 | cls.isolated_creds = isolated_creds.IsolatedCreds( |
| 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) |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 44 | if CONF.compute.allow_tenant_isolation: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 45 | cls.os = clients.Manager(cls.isolated_creds.get_primary_creds()) |
Matthew Treinish | e09b3c2 | 2013-07-23 16:49:36 -0400 | [diff] [blame] | 46 | else: |
| 47 | cls.os = clients.Manager() |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 48 | |
| 49 | @classmethod |
| 50 | def tearDownClass(cls): |
| 51 | for image_id in cls.created_images: |
| 52 | try: |
| 53 | cls.client.delete_image(image_id) |
| 54 | except exceptions.NotFound: |
| 55 | pass |
| 56 | |
| 57 | for image_id in cls.created_images: |
| 58 | cls.client.wait_for_resource_deletion(image_id) |
Matthew Treinish | b86cda9 | 2013-07-29 11:22:23 -0400 | [diff] [blame] | 59 | cls.isolated_creds.clear_isolated_creds() |
| 60 | super(BaseImageTest, cls).tearDownClass() |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 61 | |
| 62 | @classmethod |
| 63 | def create_image(cls, **kwargs): |
| 64 | """Wrapper that returns a test image.""" |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 65 | name = data_utils.rand_name(cls.__name__ + "-instance") |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 66 | |
| 67 | if 'name' in kwargs: |
| 68 | name = kwargs.pop('name') |
| 69 | |
| 70 | container_format = kwargs.pop('container_format') |
| 71 | disk_format = kwargs.pop('disk_format') |
| 72 | |
| 73 | resp, image = cls.client.create_image(name, container_format, |
| 74 | disk_format, **kwargs) |
| 75 | cls.created_images.append(image['id']) |
| 76 | return resp, image |
| 77 | |
| 78 | |
| 79 | class BaseV1ImageTest(BaseImageTest): |
| 80 | |
| 81 | @classmethod |
| 82 | def setUpClass(cls): |
| 83 | super(BaseV1ImageTest, cls).setUpClass() |
| 84 | cls.client = cls.os.image_client |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 85 | if not CONF.image_feature_enabled.api_v1: |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 86 | msg = "Glance API v1 not supported" |
| 87 | raise cls.skipException(msg) |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 88 | |
| 89 | |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 90 | class BaseV1ImageMembersTest(BaseV1ImageTest): |
| 91 | @classmethod |
| 92 | def setUpClass(cls): |
| 93 | super(BaseV1ImageMembersTest, cls).setUpClass() |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 94 | if CONF.compute.allow_tenant_isolation: |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 95 | cls.os_alt = clients.Manager(cls.isolated_creds.get_alt_creds()) |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 96 | else: |
| 97 | cls.os_alt = clients.AltManager() |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 98 | |
| 99 | cls.alt_img_cli = cls.os_alt.image_client |
Andrea Frittoli | 9612e81 | 2014-03-13 10:57:26 +0000 | [diff] [blame] | 100 | cls.alt_tenant_id = cls.alt_img_cli.tenant_id |
Attila Fazekas | a709b76 | 2013-10-08 11:52:44 +0200 | [diff] [blame] | 101 | |
| 102 | def _create_image(self): |
| 103 | image_file = StringIO.StringIO('*' * 1024) |
| 104 | resp, image = self.create_image(container_format='bare', |
| 105 | disk_format='raw', |
| 106 | is_public=False, |
| 107 | data=image_file) |
| 108 | self.assertEqual(201, resp.status) |
| 109 | image_id = image['id'] |
| 110 | return image_id |
| 111 | |
| 112 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 113 | class BaseV2ImageTest(BaseImageTest): |
| 114 | |
| 115 | @classmethod |
| 116 | def setUpClass(cls): |
| 117 | super(BaseV2ImageTest, cls).setUpClass() |
| 118 | cls.client = cls.os.image_client_v2 |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 119 | if not CONF.image_feature_enabled.api_v2: |
Matthew Treinish | c0f768f | 2013-03-11 14:24:16 -0400 | [diff] [blame] | 120 | msg = "Glance API v2 not supported" |
| 121 | raise cls.skipException(msg) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 122 | |
| 123 | |
JordanP | 236da42 | 2014-01-16 14:38:07 +0000 | [diff] [blame] | 124 | class BaseV2MemberImageTest(BaseV2ImageTest): |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 125 | |
| 126 | @classmethod |
| 127 | def setUpClass(cls): |
JordanP | 236da42 | 2014-01-16 14:38:07 +0000 | [diff] [blame] | 128 | super(BaseV2MemberImageTest, cls).setUpClass() |
Matthew Treinish | 5ba84e3 | 2014-01-29 16:52:57 +0000 | [diff] [blame] | 129 | if CONF.compute.allow_tenant_isolation: |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 130 | creds = cls.isolated_creds.get_alt_creds() |
Andrea Frittoli | 422fbdf | 2014-03-20 10:05:18 +0000 | [diff] [blame] | 131 | cls.os_alt = clients.Manager(creds) |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 132 | else: |
| 133 | cls.os_alt = clients.AltManager() |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 134 | cls.os_img_client = cls.os.image_client_v2 |
| 135 | cls.alt_img_client = cls.os_alt.image_client_v2 |
Zhi Kun Liu | d752c60 | 2014-05-16 13:25:28 +0800 | [diff] [blame] | 136 | cls.alt_tenant_id = cls.alt_img_client.tenant_id |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 137 | |
| 138 | def _list_image_ids_as_alt(self): |
| 139 | _, image_list = self.alt_img_client.image_list() |
| 140 | image_ids = map(lambda x: x['id'], image_list) |
| 141 | return image_ids |
| 142 | |
| 143 | def _create_image(self): |
| 144 | name = data_utils.rand_name('image') |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 145 | _, image = self.os_img_client.create_image(name, |
| 146 | container_format='bare', |
| 147 | disk_format='raw') |
Attila Fazekas | 689e265 | 2013-10-07 18:06:57 +0200 | [diff] [blame] | 148 | image_id = image['id'] |
| 149 | self.addCleanup(self.os_img_client.delete_image, image_id) |
| 150 | return image_id |