blob: 12f3fdd9be2e59f0a887d1007caad690dabe2b5e [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
16
Matthew Treinishce3ef922013-03-11 14:02:46 -040017from tempest import clients
Cyril Roelandtc1482a22014-11-13 16:55:04 +010018from tempest.common import credentials
Attila Fazekas689e2652013-10-07 18:06:57 +020019from tempest.common.utils import data_utils
Matthew Treinish5ba84e32014-01-29 16:52:57 +000020from tempest import config
Matthew Treinishce3ef922013-03-11 14:02:46 -040021from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040022from tempest.openstack.common import log as logging
Matthew Treinishce3ef922013-03-11 14:02:46 -040023import tempest.test
24
Matthew Treinish5ba84e32014-01-29 16:52:57 +000025CONF = config.CONF
26
Matthew Treinishce3ef922013-03-11 14:02:46 -040027LOG = logging.getLogger(__name__)
28
29
30class BaseImageTest(tempest.test.BaseTestCase):
31 """Base test class for Image API tests."""
32
33 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +010034 def resource_setup(cls):
Sylvain Afchain11b99d02014-01-16 00:42:33 +010035 cls.set_network_resources()
Andrea Frittoli69a6b632014-09-15 13:14:53 +010036 super(BaseImageTest, cls).resource_setup()
Matthew Treinishce3ef922013-03-11 14:02:46 -040037 cls.created_images = []
Matthew Treinishe09b3c22013-07-23 16:49:36 -040038 cls._interface = 'json'
Cyril Roelandtc1482a22014-11-13 16:55:04 +010039 cls.isolated_creds = credentials.get_isolated_credentials(
Matthew Treinish9f756a02014-01-15 10:26:07 -050040 cls.__name__, network_resources=cls.network_resources)
Matthew Treinish5ba84e32014-01-29 16:52:57 +000041 if not CONF.service_available.glance:
Matthew Treinish853ae442013-07-19 16:36:07 -040042 skip_msg = ("%s skipped as glance is not available" % cls.__name__)
43 raise cls.skipException(skip_msg)
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010044 cls.os = clients.Manager(cls.isolated_creds.get_primary_creds())
Matthew Treinishce3ef922013-03-11 14:02:46 -040045
46 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +010047 def resource_cleanup(cls):
Matthew Treinishce3ef922013-03-11 14:02:46 -040048 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 Treinishb86cda92013-07-29 11:22:23 -040056 cls.isolated_creds.clear_isolated_creds()
Andrea Frittoli69a6b632014-09-15 13:14:53 +010057 super(BaseImageTest, cls).resource_cleanup()
Matthew Treinishce3ef922013-03-11 14:02:46 -040058
59 @classmethod
60 def create_image(cls, **kwargs):
61 """Wrapper that returns a test image."""
Attila Fazekas689e2652013-10-07 18:06:57 +020062 name = data_utils.rand_name(cls.__name__ + "-instance")
Matthew Treinishce3ef922013-03-11 14:02:46 -040063
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 Kranz34f18782015-01-06 13:43:55 -050070 image = cls.client.create_image(name, container_format,
71 disk_format, **kwargs)
Matthew Treinishce3ef922013-03-11 14:02:46 -040072 cls.created_images.append(image['id'])
David Kranz34f18782015-01-06 13:43:55 -050073 return image
Matthew Treinishce3ef922013-03-11 14:02:46 -040074
75
76class BaseV1ImageTest(BaseImageTest):
77
78 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +010079 def resource_setup(cls):
80 super(BaseV1ImageTest, cls).resource_setup()
Matthew Treinishce3ef922013-03-11 14:02:46 -040081 cls.client = cls.os.image_client
Matthew Treinish5ba84e32014-01-29 16:52:57 +000082 if not CONF.image_feature_enabled.api_v1:
Matthew Treinishc0f768f2013-03-11 14:24:16 -040083 msg = "Glance API v1 not supported"
84 raise cls.skipException(msg)
Matthew Treinishce3ef922013-03-11 14:02:46 -040085
86
Attila Fazekasa709b762013-10-08 11:52:44 +020087class BaseV1ImageMembersTest(BaseV1ImageTest):
88 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +010089 def resource_setup(cls):
90 super(BaseV1ImageMembersTest, cls).resource_setup()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +010091 cls.os_alt = clients.Manager(cls.isolated_creds.get_alt_creds())
Attila Fazekasa709b762013-10-08 11:52:44 +020092
93 cls.alt_img_cli = cls.os_alt.image_client
Andrea Frittoli9612e812014-03-13 10:57:26 +000094 cls.alt_tenant_id = cls.alt_img_cli.tenant_id
Attila Fazekasa709b762013-10-08 11:52:44 +020095
96 def _create_image(self):
Mark Washenberger5c3b6fe2014-07-29 13:40:34 -070097 image_file = StringIO.StringIO(data_utils.random_bytes())
David Kranz34f18782015-01-06 13:43:55 -050098 image = self.create_image(container_format='bare',
99 disk_format='raw',
100 is_public=False,
101 data=image_file)
Attila Fazekasa709b762013-10-08 11:52:44 +0200102 image_id = image['id']
103 return image_id
104
105
Matthew Treinishce3ef922013-03-11 14:02:46 -0400106class BaseV2ImageTest(BaseImageTest):
107
108 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +0100109 def resource_setup(cls):
110 super(BaseV2ImageTest, cls).resource_setup()
Matthew Treinishce3ef922013-03-11 14:02:46 -0400111 cls.client = cls.os.image_client_v2
Matthew Treinish5ba84e32014-01-29 16:52:57 +0000112 if not CONF.image_feature_enabled.api_v2:
Matthew Treinishc0f768f2013-03-11 14:24:16 -0400113 msg = "Glance API v2 not supported"
114 raise cls.skipException(msg)
Attila Fazekas689e2652013-10-07 18:06:57 +0200115
116
JordanP236da422014-01-16 14:38:07 +0000117class BaseV2MemberImageTest(BaseV2ImageTest):
Attila Fazekas689e2652013-10-07 18:06:57 +0200118
119 @classmethod
Andrea Frittoli69a6b632014-09-15 13:14:53 +0100120 def resource_setup(cls):
121 super(BaseV2MemberImageTest, cls).resource_setup()
Andrea Frittoli8283b4e2014-07-17 13:28:58 +0100122 creds = cls.isolated_creds.get_alt_creds()
123 cls.os_alt = clients.Manager(creds)
Attila Fazekas689e2652013-10-07 18:06:57 +0200124 cls.os_img_client = cls.os.image_client_v2
125 cls.alt_img_client = cls.os_alt.image_client_v2
Zhi Kun Liud752c602014-05-16 13:25:28 +0800126 cls.alt_tenant_id = cls.alt_img_client.tenant_id
Attila Fazekas689e2652013-10-07 18:06:57 +0200127
128 def _list_image_ids_as_alt(self):
David Kranz34f18782015-01-06 13:43:55 -0500129 image_list = self.alt_img_client.image_list()
Attila Fazekas689e2652013-10-07 18:06:57 +0200130 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 Kranz34f18782015-01-06 13:43:55 -0500135 image = self.os_img_client.create_image(name,
136 container_format='bare',
137 disk_format='raw')
Attila Fazekas689e2652013-10-07 18:06:57 +0200138 image_id = image['id']
139 self.addCleanup(self.os_img_client.delete_image, image_id)
140 return image_id