blob: d513b0c6e4050e1649cf7c01bc916d41cec8e6da [file] [log] [blame]
Matthew Treinishce3ef922013-03-11 14:02:46 -04001# 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 Fazekasa709b762013-10-08 11:52:44 +020015import cStringIO as StringIO
Matthew Treinish01472ff2015-02-20 17:26:52 -050016
Doug Hellmann583ce2c2015-03-11 14:55:46 +000017from oslo_log import log as logging
Matthew Treinish01472ff2015-02-20 17:26:52 -050018from tempest_lib.common.utils import data_utils
Masayuki Igawabfa07602015-01-20 18:47:17 +090019from tempest_lib import exceptions as lib_exc
Attila Fazekasa709b762013-10-08 11:52:44 +020020
Matthew Treinishce3ef922013-03-11 14:02:46 -040021from tempest import clients
Cyril Roelandtc1482a22014-11-13 16:55:04 +010022from tempest.common import credentials
Matthew Treinish5ba84e32014-01-29 16:52:57 +000023from tempest import config
Matthew Treinishce3ef922013-03-11 14:02:46 -040024import tempest.test
25
Matthew Treinish5ba84e32014-01-29 16:52:57 +000026CONF = config.CONF
27
Matthew Treinishce3ef922013-03-11 14:02:46 -040028LOG = logging.getLogger(__name__)
29
30
31class BaseImageTest(tempest.test.BaseTestCase):
32 """Base test class for Image API tests."""
33
34 @classmethod
Rohan Kanade3bbbed02015-02-05 18:18:28 +053035 def skip_checks(cls):
36 super(BaseImageTest, cls).skip_checks()
37 if not CONF.service_available.glance:
38 skip_msg = ("%s skipped as glance is not available" % cls.__name__)
39 raise cls.skipException(skip_msg)
40
41 @classmethod
42 def setup_credentials(cls):
43 super(BaseImageTest, cls).setup_credentials()
44 cls.isolated_creds = credentials.get_isolated_credentials(
45 cls.__name__, network_resources=cls.network_resources)
46 cls.os = clients.Manager(cls.isolated_creds.get_primary_creds())
47
48 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +010049 def resource_setup(cls):
Sylvain Afchain11b99d02014-01-16 00:42:33 +010050 cls.set_network_resources()
Andrea Frittoli69a6b632014-09-15 13:14:53 +010051 super(BaseImageTest, cls).resource_setup()
Matthew Treinishce3ef922013-03-11 14:02:46 -040052 cls.created_images = []
53
54 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +010055 def resource_cleanup(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -040056 for image_id in cls.created_images:
57 try:
58 cls.client.delete_image(image_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +090059 except lib_exc.NotFound:
Matthew Treinishce3ef922013-03-11 14:02:46 -040060 pass
61
62 for image_id in cls.created_images:
63 cls.client.wait_for_resource_deletion(image_id)
Andrea Frittoli69a6b632014-09-15 13:14:53 +010064 super(BaseImageTest, cls).resource_cleanup()
Matthew Treinishce3ef922013-03-11 14:02:46 -040065
66 @classmethod
67 def create_image(cls, **kwargs):
68 """Wrapper that returns a test image."""
Attila Fazekas689e2652013-10-07 18:06:57 +020069 name = data_utils.rand_name(cls.__name__ + "-instance")
Matthew Treinishce3ef922013-03-11 14:02:46 -040070
71 if 'name' in kwargs:
72 name = kwargs.pop('name')
73
74 container_format = kwargs.pop('container_format')
75 disk_format = kwargs.pop('disk_format')
76
David Kranz34f18782015-01-06 13:43:55 -050077 image = cls.client.create_image(name, container_format,
78 disk_format, **kwargs)
Matthew Treinishce3ef922013-03-11 14:02:46 -040079 cls.created_images.append(image['id'])
David Kranz34f18782015-01-06 13:43:55 -050080 return image
Matthew Treinishce3ef922013-03-11 14:02:46 -040081
82
83class BaseV1ImageTest(BaseImageTest):
84
85 @classmethod
Rohan Kanade3bbbed02015-02-05 18:18:28 +053086 def skip_checks(cls):
87 super(BaseV1ImageTest, cls).skip_checks()
Matthew Treinish5ba84e32014-01-29 16:52:57 +000088 if not CONF.image_feature_enabled.api_v1:
Matthew Treinishc0f768f2013-03-11 14:24:16 -040089 msg = "Glance API v1 not supported"
90 raise cls.skipException(msg)
Matthew Treinishce3ef922013-03-11 14:02:46 -040091
Rohan Kanade3bbbed02015-02-05 18:18:28 +053092 @classmethod
93 def setup_clients(cls):
94 super(BaseV1ImageTest, cls).setup_clients()
95 cls.client = cls.os.image_client
96
Matthew Treinishce3ef922013-03-11 14:02:46 -040097
Attila Fazekasa709b762013-10-08 11:52:44 +020098class BaseV1ImageMembersTest(BaseV1ImageTest):
Rohan Kanade3bbbed02015-02-05 18:18:28 +053099
100 @classmethod
101 def setup_credentials(cls):
102 super(BaseV1ImageMembersTest, cls).setup_credentials()
103 cls.os_alt = clients.Manager(cls.isolated_creds.get_alt_creds())
104
105 @classmethod
106 def setup_clients(cls):
107 super(BaseV1ImageMembersTest, cls).setup_clients()
108 cls.alt_img_cli = cls.os_alt.image_client
109
Attila Fazekasa709b762013-10-08 11:52:44 +0200110 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +0100111 def resource_setup(cls):
112 super(BaseV1ImageMembersTest, cls).resource_setup()
Andrea Frittoli9612e812014-03-13 10:57:26 +0000113 cls.alt_tenant_id = cls.alt_img_cli.tenant_id
Attila Fazekasa709b762013-10-08 11:52:44 +0200114
115 def _create_image(self):
Mark Washenberger5c3b6fe2014-07-29 13:40:34 -0700116 image_file = StringIO.StringIO(data_utils.random_bytes())
David Kranz34f18782015-01-06 13:43:55 -0500117 image = self.create_image(container_format='bare',
118 disk_format='raw',
119 is_public=False,
120 data=image_file)
Attila Fazekasa709b762013-10-08 11:52:44 +0200121 image_id = image['id']
122 return image_id
123
124
Matthew Treinishce3ef922013-03-11 14:02:46 -0400125class BaseV2ImageTest(BaseImageTest):
126
127 @classmethod
Rohan Kanade3bbbed02015-02-05 18:18:28 +0530128 def skip_checks(cls):
129 super(BaseV2ImageTest, cls).skip_checks()
Matthew Treinish5ba84e32014-01-29 16:52:57 +0000130 if not CONF.image_feature_enabled.api_v2:
Matthew Treinishc0f768f2013-03-11 14:24:16 -0400131 msg = "Glance API v2 not supported"
132 raise cls.skipException(msg)
Attila Fazekas689e2652013-10-07 18:06:57 +0200133
Rohan Kanade3bbbed02015-02-05 18:18:28 +0530134 @classmethod
135 def setup_clients(cls):
136 super(BaseV2ImageTest, cls).setup_clients()
137 cls.client = cls.os.image_client_v2
138
Attila Fazekas689e2652013-10-07 18:06:57 +0200139
JordanP236da422014-01-16 14:38:07 +0000140class BaseV2MemberImageTest(BaseV2ImageTest):
Attila Fazekas689e2652013-10-07 18:06:57 +0200141
142 @classmethod
Rohan Kanade3bbbed02015-02-05 18:18:28 +0530143 def setup_credentials(cls):
144 super(BaseV2MemberImageTest, cls).setup_credentials()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100145 creds = cls.isolated_creds.get_alt_creds()
146 cls.os_alt = clients.Manager(creds)
Rohan Kanade3bbbed02015-02-05 18:18:28 +0530147
148 @classmethod
149 def setup_clients(cls):
150 super(BaseV2MemberImageTest, cls).setup_clients()
Attila Fazekas689e2652013-10-07 18:06:57 +0200151 cls.os_img_client = cls.os.image_client_v2
152 cls.alt_img_client = cls.os_alt.image_client_v2
Rohan Kanade3bbbed02015-02-05 18:18:28 +0530153
154 @classmethod
155 def resource_setup(cls):
156 super(BaseV2MemberImageTest, cls).resource_setup()
Zhi Kun Liud752c602014-05-16 13:25:28 +0800157 cls.alt_tenant_id = cls.alt_img_client.tenant_id
Attila Fazekas689e2652013-10-07 18:06:57 +0200158
159 def _list_image_ids_as_alt(self):
David Kranz34f18782015-01-06 13:43:55 -0500160 image_list = self.alt_img_client.image_list()
Attila Fazekas689e2652013-10-07 18:06:57 +0200161 image_ids = map(lambda x: x['id'], image_list)
162 return image_ids
163
164 def _create_image(self):
165 name = data_utils.rand_name('image')
David Kranz34f18782015-01-06 13:43:55 -0500166 image = self.os_img_client.create_image(name,
167 container_format='bare',
168 disk_format='raw')
Attila Fazekas689e2652013-10-07 18:06:57 +0200169 image_id = image['id']
170 self.addCleanup(self.os_img_client.delete_image, image_id)
171 return image_id