blob: 7656ff3aba90af485eea80861d63180062fda955 [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
14
Steve Bakerd2525a92013-05-06 15:29:03 +120015from tempest import clients
Masayuki Igawa259c1132013-10-31 17:48:44 +090016from tempest.common.utils import data_utils
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000017from tempest import config
Steve Baker8ba9e2d2014-03-24 15:37:15 +130018from tempest import exceptions
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040019from tempest.openstack.common import log as logging
Steve Bakerd2525a92013-05-06 15:29:03 +120020import tempest.test
21
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000022CONF = config.CONF
Steve Bakerd2525a92013-05-06 15:29:03 +120023
24LOG = logging.getLogger(__name__)
25
26
27class BaseOrchestrationTest(tempest.test.BaseTestCase):
28 """Base test case class for all Orchestration API tests."""
29
30 @classmethod
31 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020032 super(BaseOrchestrationTest, cls).setUpClass()
Sean Daguee0a65d12014-03-25 15:59:16 -040033 cls.os = clients.OrchestrationManager()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000034 if not CONF.service_available.heat:
Steve Bakerd2525a92013-05-06 15:29:03 +120035 raise cls.skipException("Heat support is required")
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000036 cls.build_timeout = CONF.orchestration.build_timeout
37 cls.build_interval = CONF.orchestration.build_interval
Steve Bakerd2525a92013-05-06 15:29:03 +120038
Sean Daguee0a65d12014-03-25 15:59:16 -040039 cls.orchestration_client = cls.os.orchestration_client
40 cls.client = cls.orchestration_client
41 cls.servers_client = cls.os.servers_client
42 cls.keypairs_client = cls.os.keypairs_client
43 cls.network_client = cls.os.network_client
Steve Bakerd2525a92013-05-06 15:29:03 +120044 cls.stacks = []
Steve Bakerb1f67b52013-06-24 14:42:30 +120045 cls.keypairs = []
Steve Bakerd2525a92013-05-06 15:29:03 +120046
47 @classmethod
Steve Baker80252da2013-09-25 13:29:10 +120048 def _get_default_network(cls):
49 resp, networks = cls.network_client.list_networks()
50 for net in networks['networks']:
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000051 if net['name'] == CONF.compute.fixed_network_name:
Steve Baker80252da2013-09-25 13:29:10 +120052 return net
53
54 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +120055 def _get_identity_admin_client(cls):
Steven Hardy48ec7052014-03-28 14:06:27 +000056 """Returns an instance of the Identity Admin API client."""
Sean Daguee0a65d12014-03-25 15:59:16 -040057 manager = clients.AdminManager(interface=cls._interface)
58 admin_client = manager.identity_client
Steve Bakerd2525a92013-05-06 15:29:03 +120059 return admin_client
60
61 @classmethod
Steve Bakerb7942772013-06-27 10:23:28 +120062 def create_stack(cls, stack_name, template_data, parameters={}):
63 resp, body = cls.client.create_stack(
Steve Bakerd2525a92013-05-06 15:29:03 +120064 stack_name,
65 template=template_data,
66 parameters=parameters)
Steve Bakerd2525a92013-05-06 15:29:03 +120067 stack_id = resp['location'].split('/')[-1]
68 stack_identifier = '%s/%s' % (stack_name, stack_id)
Steve Bakerb7942772013-06-27 10:23:28 +120069 cls.stacks.append(stack_identifier)
Steve Bakerd2525a92013-05-06 15:29:03 +120070 return stack_identifier
71
72 @classmethod
Steven Hardy5be93e82014-04-02 21:24:05 +010073 def _clear_stacks(cls):
Steve Bakerd2525a92013-05-06 15:29:03 +120074 for stack_identifier in cls.stacks:
75 try:
Sean Daguead824912014-03-25 14:56:35 -040076 cls.client.delete_stack(stack_identifier)
Steve Baker8ba9e2d2014-03-24 15:37:15 +130077 except exceptions.NotFound:
Steve Bakerd2525a92013-05-06 15:29:03 +120078 pass
79
80 for stack_identifier in cls.stacks:
81 try:
Sean Daguead824912014-03-25 14:56:35 -040082 cls.client.wait_for_stack_status(
Steve Bakerd2525a92013-05-06 15:29:03 +120083 stack_identifier, 'DELETE_COMPLETE')
Steve Baker8ba9e2d2014-03-24 15:37:15 +130084 except exceptions.NotFound:
Steve Bakerd2525a92013-05-06 15:29:03 +120085 pass
86
Steve Bakerb7942772013-06-27 10:23:28 +120087 @classmethod
Bartosz Górskiab33b7e2013-06-27 00:39:47 -070088 def _create_keypair(cls, name_start='keypair-heat-'):
Masayuki Igawa259c1132013-10-31 17:48:44 +090089 kp_name = data_utils.rand_name(name_start)
Steve Bakerb7942772013-06-27 10:23:28 +120090 resp, body = cls.keypairs_client.create_keypair(kp_name)
91 cls.keypairs.append(kp_name)
Steve Bakerd2525a92013-05-06 15:29:03 +120092 return body
93
94 @classmethod
Steven Hardy5be93e82014-04-02 21:24:05 +010095 def _clear_keypairs(cls):
Steve Bakerb1f67b52013-06-24 14:42:30 +120096 for kp_name in cls.keypairs:
97 try:
98 cls.keypairs_client.delete_keypair(kp_name)
99 except Exception:
100 pass
101
102 @classmethod
Sean Daguee0a65d12014-03-25 15:59:16 -0400103 def load_template(cls, name, ext='yaml'):
104 loc = ["tempest", "api", "orchestration",
105 "stacks", "templates", "%s.%s" % (name, ext)]
106 fullpath = os.path.join(*loc)
107
108 with open(fullpath, "r") as f:
109 content = f.read()
110 return content
111
112 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +1200113 def tearDownClass(cls):
Steven Hardy5be93e82014-04-02 21:24:05 +0100114 cls._clear_stacks()
115 cls._clear_keypairs()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200116 super(BaseOrchestrationTest, cls).tearDownClass()
Steve Bakerd2525a92013-05-06 15:29:03 +1200117
Steve Baker38d8f092013-06-12 12:47:16 +1200118 @staticmethod
119 def stack_output(stack, output_key):
Steven Hardy48ec7052014-03-28 14:06:27 +0000120 """Return a stack output value for a given key."""
Steve Baker38d8f092013-06-12 12:47:16 +1200121 return next((o['output_value'] for o in stack['outputs']
122 if o['output_key'] == output_key), None)
Steven Hardye2a74442014-03-25 12:10:52 +0000123
124 def assert_fields_in_dict(self, obj, *fields):
125 for field in fields:
126 self.assertIn(field, obj)
127
128 def list_resources(self, stack_identifier):
129 """Get a dict mapping of resource names to types."""
130 resp, resources = self.client.list_resources(stack_identifier)
131 self.assertEqual('200', resp['status'])
132 self.assertIsInstance(resources, list)
133 for res in resources:
134 self.assert_fields_in_dict(res, 'logical_resource_id',
135 'resource_type', 'resource_status',
136 'updated_time')
137
138 return dict((r['resource_name'], r['resource_type'])
139 for r in resources)