blob: 0b527d9af19d0bf0b9a76cd76a45f607b98006a3 [file] [log] [blame]
Jay Pipes13b479b2012-06-11 14:52:27 -04001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Jay Pipes13b479b2012-06-11 14:52:27 -04004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
David Kranzcf0040c2012-06-26 09:46:56 -040018import time
Jay Pipesf38eaac2012-06-21 13:37:35 -040019
Sean Dague1937d092013-05-17 16:36:38 -040020from tempest.api import compute
Matthew Treinish481466b2012-12-20 17:16:01 -050021from tempest import clients
Matthew Treinishb86cda92013-07-29 11:22:23 -040022from tempest.common import isolated_creds
Sean Dagued62bf1c2013-06-05 14:36:25 -040023from tempest.common.utils.data_utils import parse_image_id
Matthew Treinisha83a16e2012-12-07 13:44:02 -050024from tempest.common.utils.data_utils import rand_name
David Kranz33138312013-09-24 17:09:32 -040025from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040026from tempest.openstack.common import log as logging
Attila Fazekasdc216422013-01-29 15:12:14 +010027import tempest.test
Jay Pipesf38eaac2012-06-21 13:37:35 -040028
Tiago Melloeda03b52012-08-22 23:47:29 -030029
Jay Pipesf38eaac2012-06-21 13:37:35 -040030LOG = logging.getLogger(__name__)
Daryl Walleckc7251962012-03-12 17:26:54 -050031
32
Attila Fazekasdc216422013-01-29 15:12:14 +010033class BaseComputeTest(tempest.test.BaseTestCase):
Sean Daguef237ccb2013-01-04 15:19:14 -050034 """Base test case class for all Compute API tests."""
Daryl Walleckc7251962012-03-12 17:26:54 -050035
Attila Fazekas9a63c942013-01-29 21:46:02 +010036 conclusion = compute.generic_setup_package()
Chris Yeoh8a79b9d2013-01-18 19:32:47 +103037
Jay Pipesf38eaac2012-06-21 13:37:35 -040038 @classmethod
39 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020040 super(BaseComputeTest, cls).setUpClass()
Matthew Treinish6b41e242013-07-19 16:49:28 -040041 if not cls.config.service_available.nova:
42 skip_msg = ("%s skipped as nova is not available" % cls.__name__)
43 raise cls.skipException(skip_msg)
Matthew Treinishb86cda92013-07-29 11:22:23 -040044 cls.isolated_creds = isolated_creds.IsolatedCreds(cls.__name__)
Jay Pipesf38eaac2012-06-21 13:37:35 -040045
46 if cls.config.compute.allow_tenant_isolation:
Matthew Treinishb86cda92013-07-29 11:22:23 -040047 creds = cls.isolated_creds.get_primary_creds()
Jay Pipesf38eaac2012-06-21 13:37:35 -040048 username, tenant_name, password = creds
Matthew Treinish481466b2012-12-20 17:16:01 -050049 os = clients.Manager(username=username,
50 password=password,
James E. Blaire6d8ee12013-01-18 21:33:45 +000051 tenant_name=tenant_name,
52 interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050053 else:
James E. Blaire6d8ee12013-01-18 21:33:45 +000054 os = clients.Manager(interface=cls._interface)
Daryl Walleckc7251962012-03-12 17:26:54 -050055
Jay Pipesf38eaac2012-06-21 13:37:35 -040056 cls.os = os
57 cls.servers_client = os.servers_client
58 cls.flavors_client = os.flavors_client
59 cls.images_client = os.images_client
60 cls.extensions_client = os.extensions_client
61 cls.floating_ips_client = os.floating_ips_client
62 cls.keypairs_client = os.keypairs_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040063 cls.security_groups_client = os.security_groups_client
Rohit Karajgi07599c52012-11-02 05:35:16 -070064 cls.quotas_client = os.quotas_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040065 cls.limits_client = os.limits_client
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070066 cls.volumes_extensions_client = os.volumes_extensions_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040067 cls.volumes_client = os.volumes_client
Dan Smith8ad1c472013-02-26 13:03:16 -050068 cls.interfaces_client = os.interfaces_client
Mauro S. M. Rodriguesf5166402013-04-01 10:25:26 -040069 cls.fixed_ips_client = os.fixed_ips_client
Leo Toyodaa5278912013-04-16 15:40:12 +090070 cls.availability_zone_client = os.availability_zone_client
Mitsuhiko Yamazakiae8fc532013-04-22 11:17:35 +090071 cls.aggregates_client = os.aggregates_client
Leo Toyoda3ae31e12013-04-19 11:19:57 +090072 cls.services_client = os.services_client
Tony Yang3d5f1632013-06-06 14:17:57 +080073 cls.hypervisor_client = os.hypervisor_client
Jay Pipesf38eaac2012-06-21 13:37:35 -040074 cls.build_interval = cls.config.compute.build_interval
75 cls.build_timeout = cls.config.compute.build_timeout
76 cls.ssh_user = cls.config.compute.ssh_user
Ryan Hsucb2e1252013-09-03 21:44:49 -070077 cls.image_ssh_user = cls.config.compute.image_ssh_user
78 cls.image_ssh_password = cls.config.compute.image_ssh_password
Jay Pipesf38eaac2012-06-21 13:37:35 -040079 cls.image_ref = cls.config.compute.image_ref
80 cls.image_ref_alt = cls.config.compute.image_ref_alt
81 cls.flavor_ref = cls.config.compute.flavor_ref
82 cls.flavor_ref_alt = cls.config.compute.flavor_ref_alt
83 cls.servers = []
Sean Dagued62bf1c2013-06-05 14:36:25 -040084 cls.images = []
Daryl Walleckc7251962012-03-12 17:26:54 -050085
Brant Knudsonc7ca3342013-03-28 21:08:50 -050086 cls.servers_client_v3_auth = os.servers_client_v3_auth
87
Jay Pipesf38eaac2012-06-21 13:37:35 -040088 @classmethod
Jay Pipes444c3e62012-10-04 19:26:35 -040089 def clear_servers(cls):
90 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070091 try:
92 cls.servers_client.delete_server(server['id'])
93 except Exception:
94 pass
95
Jay Pipes444c3e62012-10-04 19:26:35 -040096 for server in cls.servers:
Dan Smith74e7bcb2012-08-21 09:18:26 -070097 try:
98 cls.servers_client.wait_for_server_termination(server['id'])
99 except Exception:
100 pass
101
102 @classmethod
Sean Dagued62bf1c2013-06-05 14:36:25 -0400103 def clear_images(cls):
104 for image_id in cls.images:
105 try:
106 cls.images_client.delete_image(image_id)
David Kranz33138312013-09-24 17:09:32 -0400107 except exceptions.NotFound:
108 # The image may have already been deleted which is OK.
109 pass
Sean Dagued62bf1c2013-06-05 14:36:25 -0400110 except Exception as exc:
111 LOG.info('Exception raised deleting image %s', image_id)
112 LOG.exception(exc)
113 pass
114
115 @classmethod
Jay Pipesf38eaac2012-06-21 13:37:35 -0400116 def tearDownClass(cls):
Sean Dagued62bf1c2013-06-05 14:36:25 -0400117 cls.clear_images()
Jay Pipes444c3e62012-10-04 19:26:35 -0400118 cls.clear_servers()
Matthew Treinishb86cda92013-07-29 11:22:23 -0400119 cls.isolated_creds.clear_isolated_creds()
120 super(BaseComputeTest, cls).tearDownClass()
Rohit Karajgidc300b22012-05-04 08:11:00 -0700121
Jay Pipes444c3e62012-10-04 19:26:35 -0400122 @classmethod
Sean Dague22897e12013-02-25 17:54:09 -0500123 def create_server(cls, **kwargs):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Wrapper utility that returns a test server."""
Sean Dague22897e12013-02-25 17:54:09 -0500125 name = rand_name(cls.__name__ + "-instance")
126 if 'name' in kwargs:
127 name = kwargs.pop('name')
128 flavor = kwargs.get('flavor', cls.flavor_ref)
129 image_id = kwargs.get('image_id', cls.image_ref)
Rohit Karajgidc300b22012-05-04 08:11:00 -0700130
Andrea Frittoli938d1512013-05-16 15:42:27 +0100131 resp, body = cls.servers_client.create_server(
Sean Dague22897e12013-02-25 17:54:09 -0500132 name, image_id, flavor, **kwargs)
Andrea Frittoli938d1512013-05-16 15:42:27 +0100133
134 # handle the case of multiple servers
135 servers = [body]
136 if 'min_count' in kwargs or 'max_count' in kwargs:
137 # Get servers created which name match with name param.
138 r, b = cls.servers_client.list_servers()
139 servers = [s for s in b['servers'] if s['name'].startswith(name)]
140
141 cls.servers.extend(servers)
David Kranzcf0040c2012-06-26 09:46:56 -0400142
Sean Dague22897e12013-02-25 17:54:09 -0500143 if 'wait_until' in kwargs:
Andrea Frittoli938d1512013-05-16 15:42:27 +0100144 for server in servers:
145 cls.servers_client.wait_for_server_status(
146 server['id'], kwargs['wait_until'])
Sean Dague9b669e32012-12-13 18:40:08 -0500147
Andrea Frittoli938d1512013-05-16 15:42:27 +0100148 return resp, body
Sean Dague9b669e32012-12-13 18:40:08 -0500149
Sean Dagued62bf1c2013-06-05 14:36:25 -0400150 @classmethod
151 def create_image_from_server(cls, server_id, **kwargs):
152 """Wrapper utility that returns a test server."""
153 name = rand_name(cls.__name__ + "-image")
154 if 'name' in kwargs:
155 name = kwargs.pop('name')
156
157 resp, image = cls.images_client.create_image(
158 server_id, name)
159 image_id = parse_image_id(resp['location'])
160 cls.images.append(image_id)
161
162 if 'wait_until' in kwargs:
163 cls.images_client.wait_for_image_status(image_id,
164 kwargs['wait_until'])
165 resp, image = cls.images_client.get_image(image_id)
166
167 return resp, image
168
David Kranzcf0040c2012-06-26 09:46:56 -0400169 def wait_for(self, condition):
Sean Daguef237ccb2013-01-04 15:19:14 -0500170 """Repeatedly calls condition() until a timeout."""
David Kranzcf0040c2012-06-26 09:46:56 -0400171 start_time = int(time.time())
172 while True:
173 try:
174 condition()
Matthew Treinish05d9fb92012-12-07 16:14:05 -0500175 except Exception:
David Kranzcf0040c2012-06-26 09:46:56 -0400176 pass
177 else:
178 return
179 if int(time.time()) - start_time >= self.build_timeout:
180 condition()
181 return
182 time.sleep(self.build_interval)
Jay Pipesf38eaac2012-06-21 13:37:35 -0400183
184
Attila Fazekas19044d52013-02-16 07:35:06 +0100185class BaseComputeAdminTest(BaseComputeTest):
Sean Daguef237ccb2013-01-04 15:19:14 -0500186 """Base test case class for all Compute Admin API tests."""
Jay Pipesf38eaac2012-06-21 13:37:35 -0400187
188 @classmethod
189 def setUpClass(cls):
Attila Fazekas4ba36582013-02-12 08:26:17 +0100190 super(BaseComputeAdminTest, cls).setUpClass()
191 admin_username = cls.config.compute_admin.username
192 admin_password = cls.config.compute_admin.password
193 admin_tenant = cls.config.compute_admin.tenant_name
Attila Fazekas4ba36582013-02-12 08:26:17 +0100194 if not (admin_username and admin_password and admin_tenant):
Jay Pipesf38eaac2012-06-21 13:37:35 -0400195 msg = ("Missing Compute Admin API credentials "
196 "in configuration.")
ivan-zhu1feeb382013-01-24 10:14:39 +0800197 raise cls.skipException(msg)
Matthew Treinish47ff7912013-07-22 17:09:55 -0400198 if cls.config.compute.allow_tenant_isolation:
Matthew Treinishb86cda92013-07-29 11:22:23 -0400199 creds = cls.isolated_creds.get_admin_creds()
Matthew Treinish47ff7912013-07-22 17:09:55 -0400200 admin_username, admin_tenant_name, admin_password = creds
201 cls.os_adm = clients.Manager(username=admin_username,
202 password=admin_password,
203 tenant_name=admin_tenant_name,
204 interface=cls._interface)
205 else:
206 cls.os_adm = clients.ComputeAdminManager(interface=cls._interface)