blob: 0b22de596a2b775f3543d12d74b69177ce7c0c59 [file] [log] [blame]
Steve Bakerd2525a92013-05-06 15:29:03 +12001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
Sean Daguee0a65d12014-03-25 15:59:16 -040013import os.path
Matthew Treinish96e9e882014-06-09 18:37:19 -040014
Ghanshyam961ea1a2014-06-09 10:56:00 +090015import yaml
Sean Daguee0a65d12014-03-25 15:59:16 -040016
Steve Bakerd2525a92013-05-06 15:29:03 +120017from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090018from tempest.common.utils import data_utils
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000019from tempest import config
Steve Baker8ba9e2d2014-03-24 15:37:15 +130020from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040021from tempest.openstack.common import log as logging
Steve Bakerd2525a92013-05-06 15:29:03 +120022import tempest.test
23
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000024CONF = config.CONF
Steve Bakerd2525a92013-05-06 15:29:03 +120025
26LOG = logging.getLogger(__name__)
27
28
29class BaseOrchestrationTest(tempest.test.BaseTestCase):
30 """Base test case class for all Orchestration API tests."""
31
32 @classmethod
33 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020034 super(BaseOrchestrationTest, cls).setUpClass()
Steve Baker61d8c442014-04-16 12:03:37 +120035 cls.os = clients.Manager()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000036 if not CONF.service_available.heat:
Steve Bakerd2525a92013-05-06 15:29:03 +120037 raise cls.skipException("Heat support is required")
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000038 cls.build_timeout = CONF.orchestration.build_timeout
39 cls.build_interval = CONF.orchestration.build_interval
Steve Bakerd2525a92013-05-06 15:29:03 +120040
Sean Daguee0a65d12014-03-25 15:59:16 -040041 cls.orchestration_client = cls.os.orchestration_client
42 cls.client = cls.orchestration_client
43 cls.servers_client = cls.os.servers_client
44 cls.keypairs_client = cls.os.keypairs_client
45 cls.network_client = cls.os.network_client
Steven Hardyb1f91982014-04-04 15:35:55 +010046 cls.volumes_client = cls.os.volumes_client
huangtianhua01cba0a2014-04-30 16:18:03 +080047 cls.images_v2_client = cls.os.image_client_v2
Steve Bakerd2525a92013-05-06 15:29:03 +120048 cls.stacks = []
Steve Bakerb1f67b52013-06-24 14:42:30 +120049 cls.keypairs = []
huangtianhua01cba0a2014-04-30 16:18:03 +080050 cls.images = []
Steve Bakerd2525a92013-05-06 15:29:03 +120051
52 @classmethod
Steve Baker80252da2013-09-25 13:29:10 +120053 def _get_default_network(cls):
Joseph Lanoux2707a0f2014-08-22 11:05:21 +000054 _, networks = cls.network_client.list_networks()
Steve Baker80252da2013-09-25 13:29:10 +120055 for net in networks['networks']:
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000056 if net['name'] == CONF.compute.fixed_network_name:
Steve Baker80252da2013-09-25 13:29:10 +120057 return net
58
59 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +120060 def _get_identity_admin_client(cls):
Steven Hardy48ec7052014-03-28 14:06:27 +000061 """Returns an instance of the Identity Admin API client."""
Sean Daguee0a65d12014-03-25 15:59:16 -040062 manager = clients.AdminManager(interface=cls._interface)
63 admin_client = manager.identity_client
Steve Bakerd2525a92013-05-06 15:29:03 +120064 return admin_client
65
66 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +090067 def create_stack(cls, stack_name, template_data, parameters=None,
Steven Hardy1b25fe02014-05-07 16:21:28 +010068 environment=None, files=None):
Ghanshyam2a180b82014-06-16 13:54:22 +090069 if parameters is None:
70 parameters = {}
Steve Bakerb7942772013-06-27 10:23:28 +120071 resp, body = cls.client.create_stack(
Steve Bakerd2525a92013-05-06 15:29:03 +120072 stack_name,
73 template=template_data,
Steven Hardy00de7582014-05-07 15:18:52 +010074 parameters=parameters,
Steven Hardy1b25fe02014-05-07 16:21:28 +010075 environment=environment,
76 files=files)
Steve Bakerd2525a92013-05-06 15:29:03 +120077 stack_id = resp['location'].split('/')[-1]
78 stack_identifier = '%s/%s' % (stack_name, stack_id)
Steve Bakerb7942772013-06-27 10:23:28 +120079 cls.stacks.append(stack_identifier)
Steve Bakerd2525a92013-05-06 15:29:03 +120080 return stack_identifier
81
82 @classmethod
Steven Hardy5be93e82014-04-02 21:24:05 +010083 def _clear_stacks(cls):
Steve Bakerd2525a92013-05-06 15:29:03 +120084 for stack_identifier in cls.stacks:
85 try:
Sean Daguead824912014-03-25 14:56:35 -040086 cls.client.delete_stack(stack_identifier)
Steve Baker8ba9e2d2014-03-24 15:37:15 +130087 except exceptions.NotFound:
Steve Bakerd2525a92013-05-06 15:29:03 +120088 pass
89
90 for stack_identifier in cls.stacks:
Zhi Kun Liu03aec1d2014-08-12 16:57:05 +080091 try:
92 cls.client.wait_for_stack_status(
93 stack_identifier, 'DELETE_COMPLETE')
94 except exceptions.NotFound:
95 pass
Steve Bakerd2525a92013-05-06 15:29:03 +120096
Steve Bakerb7942772013-06-27 10:23:28 +120097 @classmethod
Bartosz Górskiab33b7e2013-06-27 00:39:47 -070098 def _create_keypair(cls, name_start='keypair-heat-'):
Masayuki Igawa259c1132013-10-31 17:48:44 +090099 kp_name = data_utils.rand_name(name_start)
Joseph Lanoux2707a0f2014-08-22 11:05:21 +0000100 _, body = cls.keypairs_client.create_keypair(kp_name)
Steve Bakerb7942772013-06-27 10:23:28 +1200101 cls.keypairs.append(kp_name)
Steve Bakerd2525a92013-05-06 15:29:03 +1200102 return body
103
104 @classmethod
Steven Hardy5be93e82014-04-02 21:24:05 +0100105 def _clear_keypairs(cls):
Steve Bakerb1f67b52013-06-24 14:42:30 +1200106 for kp_name in cls.keypairs:
107 try:
108 cls.keypairs_client.delete_keypair(kp_name)
109 except Exception:
110 pass
111
112 @classmethod
huangtianhua01cba0a2014-04-30 16:18:03 +0800113 def _create_image(cls, name_start='image-heat-', container_format='bare',
114 disk_format='iso'):
115 image_name = data_utils.rand_name(name_start)
Joseph Lanoux2707a0f2014-08-22 11:05:21 +0000116 _, body = cls.images_v2_client.create_image(image_name,
117 container_format,
118 disk_format)
huangtianhua01cba0a2014-04-30 16:18:03 +0800119 image_id = body['id']
120 cls.images.append(image_id)
121 return body
122
123 @classmethod
124 def _clear_images(cls):
125 for image_id in cls.images:
126 try:
127 cls.images_v2_client.delete_image(image_id)
128 except exceptions.NotFound:
129 pass
130
131 @classmethod
Ghanshyam961ea1a2014-06-09 10:56:00 +0900132 def read_template(cls, name, ext='yaml'):
Qiu Hua Qiaof6368772014-04-01 01:12:39 -0500133 loc = ["stacks", "templates", "%s.%s" % (name, ext)]
134 fullpath = os.path.join(os.path.dirname(__file__), *loc)
Sean Daguee0a65d12014-03-25 15:59:16 -0400135
136 with open(fullpath, "r") as f:
137 content = f.read()
138 return content
139
140 @classmethod
Ghanshyam961ea1a2014-06-09 10:56:00 +0900141 def load_template(cls, name, ext='yaml'):
142 loc = ["stacks", "templates", "%s.%s" % (name, ext)]
143 fullpath = os.path.join(os.path.dirname(__file__), *loc)
144
145 with open(fullpath, "r") as f:
146 return yaml.safe_load(f)
147
148 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +1200149 def tearDownClass(cls):
Steven Hardy5be93e82014-04-02 21:24:05 +0100150 cls._clear_stacks()
151 cls._clear_keypairs()
huangtianhua01cba0a2014-04-30 16:18:03 +0800152 cls._clear_images()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200153 super(BaseOrchestrationTest, cls).tearDownClass()
Steve Bakerd2525a92013-05-06 15:29:03 +1200154
Steve Baker38d8f092013-06-12 12:47:16 +1200155 @staticmethod
156 def stack_output(stack, output_key):
Steven Hardy48ec7052014-03-28 14:06:27 +0000157 """Return a stack output value for a given key."""
Steve Baker38d8f092013-06-12 12:47:16 +1200158 return next((o['output_value'] for o in stack['outputs']
159 if o['output_key'] == output_key), None)
Steven Hardye2a74442014-03-25 12:10:52 +0000160
161 def assert_fields_in_dict(self, obj, *fields):
162 for field in fields:
163 self.assertIn(field, obj)
164
165 def list_resources(self, stack_identifier):
166 """Get a dict mapping of resource names to types."""
Joseph Lanoux2707a0f2014-08-22 11:05:21 +0000167 _, resources = self.client.list_resources(stack_identifier)
Steven Hardye2a74442014-03-25 12:10:52 +0000168 self.assertIsInstance(resources, list)
169 for res in resources:
170 self.assert_fields_in_dict(res, 'logical_resource_id',
171 'resource_type', 'resource_status',
172 'updated_time')
173
174 return dict((r['resource_name'], r['resource_type'])
175 for r in resources)
Steven Hardy8b54fc52014-03-28 16:15:51 +0000176
177 def get_stack_output(self, stack_identifier, output_key):
Joseph Lanoux2707a0f2014-08-22 11:05:21 +0000178 _, body = self.client.get_stack(stack_identifier)
Steven Hardy8b54fc52014-03-28 16:15:51 +0000179 return self.stack_output(body, output_key)