blob: 266f726d90ff8d51cc2d76367ab95910fcaa729a [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
Doug Hellmann583ce2c2015-03-11 14:55:46 +000015from oslo_log import log as logging
Masayuki Igawabfa07602015-01-20 18:47:17 +090016from tempest_lib import exceptions as lib_exc
Ghanshyam961ea1a2014-06-09 10:56:00 +090017import yaml
Sean Daguee0a65d12014-03-25 15:59:16 -040018
Fei Long Wangd39431f2015-05-14 11:30:48 +120019from tempest.common.utils import data_utils
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000020from tempest import config
Steve Bakerd2525a92013-05-06 15:29:03 +120021import tempest.test
22
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000023CONF = config.CONF
Steve Bakerd2525a92013-05-06 15:29:03 +120024
25LOG = logging.getLogger(__name__)
26
27
28class BaseOrchestrationTest(tempest.test.BaseTestCase):
29 """Base test case class for all Orchestration API tests."""
30
Andrea Frittolib21de6c2015-02-06 20:12:38 +000031 credentials = ['primary']
32
Steve Bakerd2525a92013-05-06 15:29:03 +120033 @classmethod
Rohan Kanade80b938a2015-02-07 10:58:56 +053034 def skip_checks(cls):
35 super(BaseOrchestrationTest, cls).skip_checks()
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")
38
Rohan Kanade80b938a2015-02-07 10:58:56 +053039 @classmethod
40 def setup_credentials(cls):
41 super(BaseOrchestrationTest, cls).setup_credentials()
Matthew Treinishdb9721d2015-03-18 14:21:28 -040042 stack_owner_role = CONF.orchestration.stack_owner_role
Andrea Frittoli (andreaf)737fac92015-05-12 16:14:35 +010043 cls.os = cls.get_client_manager(roles=[stack_owner_role])
Rohan Kanade80b938a2015-02-07 10:58:56 +053044
45 @classmethod
46 def setup_clients(cls):
47 super(BaseOrchestrationTest, cls).setup_clients()
Sean Daguee0a65d12014-03-25 15:59:16 -040048 cls.orchestration_client = cls.os.orchestration_client
49 cls.client = cls.orchestration_client
50 cls.servers_client = cls.os.servers_client
51 cls.keypairs_client = cls.os.keypairs_client
52 cls.network_client = cls.os.network_client
Steven Hardyb1f91982014-04-04 15:35:55 +010053 cls.volumes_client = cls.os.volumes_client
huangtianhua01cba0a2014-04-30 16:18:03 +080054 cls.images_v2_client = cls.os.image_client_v2
Rohan Kanade80b938a2015-02-07 10:58:56 +053055
56 @classmethod
57 def resource_setup(cls):
58 super(BaseOrchestrationTest, cls).resource_setup()
59 cls.build_timeout = CONF.orchestration.build_timeout
60 cls.build_interval = CONF.orchestration.build_interval
Steve Bakerd2525a92013-05-06 15:29:03 +120061 cls.stacks = []
Steve Bakerb1f67b52013-06-24 14:42:30 +120062 cls.keypairs = []
huangtianhua01cba0a2014-04-30 16:18:03 +080063 cls.images = []
Steve Bakerd2525a92013-05-06 15:29:03 +120064
65 @classmethod
Ghanshyam2a180b82014-06-16 13:54:22 +090066 def create_stack(cls, stack_name, template_data, parameters=None,
Steven Hardy1b25fe02014-05-07 16:21:28 +010067 environment=None, files=None):
Ghanshyam2a180b82014-06-16 13:54:22 +090068 if parameters is None:
69 parameters = {}
David Kranz8ad924b2015-01-16 16:50:18 -050070 body = cls.client.create_stack(
Steve Bakerd2525a92013-05-06 15:29:03 +120071 stack_name,
72 template=template_data,
Steven Hardy00de7582014-05-07 15:18:52 +010073 parameters=parameters,
Steven Hardy1b25fe02014-05-07 16:21:28 +010074 environment=environment,
75 files=files)
David Kranz8ad924b2015-01-16 16:50:18 -050076 stack_id = body.response['location'].split('/')[-1]
Steve Bakerd2525a92013-05-06 15:29:03 +120077 stack_identifier = '%s/%s' % (stack_name, stack_id)
Steve Bakerb7942772013-06-27 10:23:28 +120078 cls.stacks.append(stack_identifier)
Steve Bakerd2525a92013-05-06 15:29:03 +120079 return stack_identifier
80
81 @classmethod
Steven Hardy5be93e82014-04-02 21:24:05 +010082 def _clear_stacks(cls):
Steve Bakerd2525a92013-05-06 15:29:03 +120083 for stack_identifier in cls.stacks:
84 try:
Sean Daguead824912014-03-25 14:56:35 -040085 cls.client.delete_stack(stack_identifier)
Masayuki Igawabfa07602015-01-20 18:47:17 +090086 except lib_exc.NotFound:
Steve Bakerd2525a92013-05-06 15:29:03 +120087 pass
88
89 for stack_identifier in cls.stacks:
Zhi Kun Liu03aec1d2014-08-12 16:57:05 +080090 try:
91 cls.client.wait_for_stack_status(
92 stack_identifier, 'DELETE_COMPLETE')
Masayuki Igawabfa07602015-01-20 18:47:17 +090093 except lib_exc.NotFound:
Zhi Kun Liu03aec1d2014-08-12 16:57:05 +080094 pass
Steve Bakerd2525a92013-05-06 15:29:03 +120095
Steve Bakerb7942772013-06-27 10:23:28 +120096 @classmethod
Bartosz Górskiab33b7e2013-06-27 00:39:47 -070097 def _create_keypair(cls, name_start='keypair-heat-'):
Masayuki Igawa259c1132013-10-31 17:48:44 +090098 kp_name = data_utils.rand_name(name_start)
David Kranz173f0e02015-02-06 13:47:57 -050099 body = cls.keypairs_client.create_keypair(kp_name)
Steve Bakerb7942772013-06-27 10:23:28 +1200100 cls.keypairs.append(kp_name)
Steve Bakerd2525a92013-05-06 15:29:03 +1200101 return body
102
103 @classmethod
Steven Hardy5be93e82014-04-02 21:24:05 +0100104 def _clear_keypairs(cls):
Steve Bakerb1f67b52013-06-24 14:42:30 +1200105 for kp_name in cls.keypairs:
106 try:
107 cls.keypairs_client.delete_keypair(kp_name)
108 except Exception:
109 pass
110
111 @classmethod
huangtianhua01cba0a2014-04-30 16:18:03 +0800112 def _create_image(cls, name_start='image-heat-', container_format='bare',
113 disk_format='iso'):
114 image_name = data_utils.rand_name(name_start)
David Kranz34f18782015-01-06 13:43:55 -0500115 body = cls.images_v2_client.create_image(image_name,
116 container_format,
117 disk_format)
huangtianhua01cba0a2014-04-30 16:18:03 +0800118 image_id = body['id']
119 cls.images.append(image_id)
120 return body
121
122 @classmethod
123 def _clear_images(cls):
124 for image_id in cls.images:
125 try:
126 cls.images_v2_client.delete_image(image_id)
Masayuki Igawabfa07602015-01-20 18:47:17 +0900127 except lib_exc.NotFound:
huangtianhua01cba0a2014-04-30 16:18:03 +0800128 pass
129
130 @classmethod
Ghanshyam961ea1a2014-06-09 10:56:00 +0900131 def read_template(cls, name, ext='yaml'):
Qiu Hua Qiaof6368772014-04-01 01:12:39 -0500132 loc = ["stacks", "templates", "%s.%s" % (name, ext)]
133 fullpath = os.path.join(os.path.dirname(__file__), *loc)
Sean Daguee0a65d12014-03-25 15:59:16 -0400134
135 with open(fullpath, "r") as f:
136 content = f.read()
137 return content
138
139 @classmethod
Ghanshyam961ea1a2014-06-09 10:56:00 +0900140 def load_template(cls, name, ext='yaml'):
141 loc = ["stacks", "templates", "%s.%s" % (name, ext)]
142 fullpath = os.path.join(os.path.dirname(__file__), *loc)
143
144 with open(fullpath, "r") as f:
145 return yaml.safe_load(f)
146
147 @classmethod
Andrea Frittoli556f7962014-09-15 13:14:54 +0100148 def resource_cleanup(cls):
Steven Hardy5be93e82014-04-02 21:24:05 +0100149 cls._clear_stacks()
150 cls._clear_keypairs()
huangtianhua01cba0a2014-04-30 16:18:03 +0800151 cls._clear_images()
Andrea Frittoli556f7962014-09-15 13:14:54 +0100152 super(BaseOrchestrationTest, cls).resource_cleanup()
Steve Bakerd2525a92013-05-06 15:29:03 +1200153
Steve Baker38d8f092013-06-12 12:47:16 +1200154 @staticmethod
155 def stack_output(stack, output_key):
Steven Hardy48ec7052014-03-28 14:06:27 +0000156 """Return a stack output value for a given key."""
Steve Baker38d8f092013-06-12 12:47:16 +1200157 return next((o['output_value'] for o in stack['outputs']
158 if o['output_key'] == output_key), None)
Steven Hardye2a74442014-03-25 12:10:52 +0000159
160 def assert_fields_in_dict(self, obj, *fields):
161 for field in fields:
162 self.assertIn(field, obj)
163
164 def list_resources(self, stack_identifier):
165 """Get a dict mapping of resource names to types."""
David Kranz8ad924b2015-01-16 16:50:18 -0500166 resources = self.client.list_resources(stack_identifier)
Steven Hardye2a74442014-03-25 12:10:52 +0000167 self.assertIsInstance(resources, list)
168 for res in resources:
169 self.assert_fields_in_dict(res, 'logical_resource_id',
170 'resource_type', 'resource_status',
171 'updated_time')
172
173 return dict((r['resource_name'], r['resource_type'])
174 for r in resources)
Steven Hardy8b54fc52014-03-28 16:15:51 +0000175
176 def get_stack_output(self, stack_identifier, output_key):
Ken'ichi Ohmichie06f0e72015-04-06 02:52:11 +0000177 body = self.client.show_stack(stack_identifier)
Steven Hardy8b54fc52014-03-28 16:15:51 +0000178 return self.stack_output(body, output_key)