blob: 02d391b36480f192dac2db8f6fb2353459b49714 [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
Matthew Treinishb86cda92013-07-29 11:22:23 -040018from tempest.common import isolated_creds
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
34 def setUpClass(cls):
Sylvain Afchain11b99d02014-01-16 00:42:33 +010035 cls.set_network_resources()
Attila Fazekasf86fa312013-07-30 19:56:39 +020036 super(BaseImageTest, cls).setUpClass()
Matthew Treinishce3ef922013-03-11 14:02:46 -040037 cls.created_images = []
Matthew Treinishe09b3c22013-07-23 16:49:36 -040038 cls._interface = 'json'
Matthew Treinish9f756a02014-01-15 10:26:07 -050039 cls.isolated_creds = isolated_creds.IsolatedCreds(
40 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)
Matthew Treinish5ba84e32014-01-29 16:52:57 +000044 if CONF.compute.allow_tenant_isolation:
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000045 cls.os = clients.Manager(cls.isolated_creds.get_primary_creds())
Matthew Treinishe09b3c22013-07-23 16:49:36 -040046 else:
47 cls.os = clients.Manager()
Matthew Treinishce3ef922013-03-11 14:02:46 -040048
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 Treinishb86cda92013-07-29 11:22:23 -040059 cls.isolated_creds.clear_isolated_creds()
60 super(BaseImageTest, cls).tearDownClass()
Matthew Treinishce3ef922013-03-11 14:02:46 -040061
62 @classmethod
63 def create_image(cls, **kwargs):
64 """Wrapper that returns a test image."""
Attila Fazekas689e2652013-10-07 18:06:57 +020065 name = data_utils.rand_name(cls.__name__ + "-instance")
Matthew Treinishce3ef922013-03-11 14:02:46 -040066
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
79class BaseV1ImageTest(BaseImageTest):
80
81 @classmethod
82 def setUpClass(cls):
83 super(BaseV1ImageTest, cls).setUpClass()
84 cls.client = cls.os.image_client
Matthew Treinish5ba84e32014-01-29 16:52:57 +000085 if not CONF.image_feature_enabled.api_v1:
Matthew Treinishc0f768f2013-03-11 14:24:16 -040086 msg = "Glance API v1 not supported"
87 raise cls.skipException(msg)
Matthew Treinishce3ef922013-03-11 14:02:46 -040088
89
Attila Fazekasa709b762013-10-08 11:52:44 +020090class BaseV1ImageMembersTest(BaseV1ImageTest):
91 @classmethod
92 def setUpClass(cls):
93 super(BaseV1ImageMembersTest, cls).setUpClass()
Matthew Treinish5ba84e32014-01-29 16:52:57 +000094 if CONF.compute.allow_tenant_isolation:
Andrea Frittoli422fbdf2014-03-20 10:05:18 +000095 cls.os_alt = clients.Manager(cls.isolated_creds.get_alt_creds())
Attila Fazekasa709b762013-10-08 11:52:44 +020096 else:
97 cls.os_alt = clients.AltManager()
Attila Fazekasa709b762013-10-08 11:52:44 +020098
99 cls.alt_img_cli = cls.os_alt.image_client
Andrea Frittoli9612e812014-03-13 10:57:26 +0000100 cls.alt_tenant_id = cls.alt_img_cli.tenant_id
Attila Fazekasa709b762013-10-08 11:52:44 +0200101
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 Treinishce3ef922013-03-11 14:02:46 -0400113class BaseV2ImageTest(BaseImageTest):
114
115 @classmethod
116 def setUpClass(cls):
117 super(BaseV2ImageTest, cls).setUpClass()
118 cls.client = cls.os.image_client_v2
Matthew Treinish5ba84e32014-01-29 16:52:57 +0000119 if not CONF.image_feature_enabled.api_v2:
Matthew Treinishc0f768f2013-03-11 14:24:16 -0400120 msg = "Glance API v2 not supported"
121 raise cls.skipException(msg)
Attila Fazekas689e2652013-10-07 18:06:57 +0200122
123
JordanP236da422014-01-16 14:38:07 +0000124class BaseV2MemberImageTest(BaseV2ImageTest):
Attila Fazekas689e2652013-10-07 18:06:57 +0200125
126 @classmethod
127 def setUpClass(cls):
JordanP236da422014-01-16 14:38:07 +0000128 super(BaseV2MemberImageTest, cls).setUpClass()
Matthew Treinish5ba84e32014-01-29 16:52:57 +0000129 if CONF.compute.allow_tenant_isolation:
Attila Fazekas689e2652013-10-07 18:06:57 +0200130 creds = cls.isolated_creds.get_alt_creds()
Andrea Frittoli422fbdf2014-03-20 10:05:18 +0000131 cls.os_alt = clients.Manager(creds)
Attila Fazekas689e2652013-10-07 18:06:57 +0200132 else:
133 cls.os_alt = clients.AltManager()
Attila Fazekas689e2652013-10-07 18:06:57 +0200134 cls.os_img_client = cls.os.image_client_v2
135 cls.alt_img_client = cls.os_alt.image_client_v2
Zhi Kun Liud752c602014-05-16 13:25:28 +0800136 cls.alt_tenant_id = cls.alt_img_client.tenant_id
Attila Fazekas689e2652013-10-07 18:06:57 +0200137
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 Kranz9c3b3b62014-06-19 16:05:53 -0400145 _, image = self.os_img_client.create_image(name,
146 container_format='bare',
147 disk_format='raw')
Attila Fazekas689e2652013-10-07 18:06:57 +0200148 image_id = image['id']
149 self.addCleanup(self.os_img_client.delete_image, image_id)
150 return image_id