blob: 7c72991ea62bbf10f1a6aae7d530e89275eff83b [file] [log] [blame]
Steve Bakerd2525a92013-05-06 15:29:03 +12001# vim: tabstop=4 shiftwidth=4 softtabstop=4
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
Steve Bakerd2525a92013-05-06 15:29:03 +120015import time
16
17from tempest import clients
18from tempest.common.utils.data_utils import rand_name
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
22
23LOG = logging.getLogger(__name__)
24
25
26class BaseOrchestrationTest(tempest.test.BaseTestCase):
27 """Base test case class for all Orchestration API tests."""
28
29 @classmethod
30 def setUpClass(cls):
Attila Fazekasf86fa312013-07-30 19:56:39 +020031 super(BaseOrchestrationTest, cls).setUpClass()
Steve Bakerd2525a92013-05-06 15:29:03 +120032 os = clients.OrchestrationManager()
33 cls.orchestration_cfg = os.config.orchestration
Steve Baker80252da2013-09-25 13:29:10 +120034 cls.compute_cfg = os.config.compute
Matthew Treinisha9d43882013-07-19 16:54:52 -040035 if not os.config.service_available.heat:
Steve Bakerd2525a92013-05-06 15:29:03 +120036 raise cls.skipException("Heat support is required")
Steve Baker6bf3d632013-06-24 14:44:40 +120037 cls.build_timeout = cls.orchestration_cfg.build_timeout
38 cls.build_interval = cls.orchestration_cfg.build_interval
Steve Bakerd2525a92013-05-06 15:29:03 +120039
40 cls.os = os
41 cls.orchestration_client = os.orchestration_client
Steve Baker38d8f092013-06-12 12:47:16 +120042 cls.servers_client = os.servers_client
Steve Bakerd2525a92013-05-06 15:29:03 +120043 cls.keypairs_client = os.keypairs_client
Steve Baker80252da2013-09-25 13:29:10 +120044 cls.network_client = os.network_client
Steve Bakerd2525a92013-05-06 15:29:03 +120045 cls.stacks = []
Steve Bakerb1f67b52013-06-24 14:42:30 +120046 cls.keypairs = []
Steve Bakerd2525a92013-05-06 15:29:03 +120047
48 @classmethod
Steve Baker80252da2013-09-25 13:29:10 +120049 def _get_default_network(cls):
50 resp, networks = cls.network_client.list_networks()
51 for net in networks['networks']:
52 if net['name'] == cls.compute_cfg.fixed_network_name:
53 return net
54
55 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +120056 def _get_identity_admin_client(cls):
57 """
58 Returns an instance of the Identity Admin API client
59 """
60 os = clients.AdminManager(interface=cls._interface)
61 admin_client = os.identity_client
62 return admin_client
63
64 @classmethod
65 def _get_client_args(cls):
66
67 return (
68 cls.config,
69 cls.config.identity.admin_username,
70 cls.config.identity.admin_password,
71 cls.config.identity.uri
72 )
73
Steve Bakerb7942772013-06-27 10:23:28 +120074 @classmethod
75 def create_stack(cls, stack_name, template_data, parameters={}):
76 resp, body = cls.client.create_stack(
Steve Bakerd2525a92013-05-06 15:29:03 +120077 stack_name,
78 template=template_data,
79 parameters=parameters)
Steve Bakerd2525a92013-05-06 15:29:03 +120080 stack_id = resp['location'].split('/')[-1]
81 stack_identifier = '%s/%s' % (stack_name, stack_id)
Steve Bakerb7942772013-06-27 10:23:28 +120082 cls.stacks.append(stack_identifier)
Steve Bakerd2525a92013-05-06 15:29:03 +120083 return stack_identifier
84
85 @classmethod
86 def clear_stacks(cls):
87 for stack_identifier in cls.stacks:
88 try:
89 cls.orchestration_client.delete_stack(stack_identifier)
90 except Exception:
91 pass
92
93 for stack_identifier in cls.stacks:
94 try:
95 cls.orchestration_client.wait_for_stack_status(
96 stack_identifier, 'DELETE_COMPLETE')
97 except Exception:
98 pass
99
Steve Bakerb7942772013-06-27 10:23:28 +1200100 @classmethod
Bartosz Górskiab33b7e2013-06-27 00:39:47 -0700101 def _create_keypair(cls, name_start='keypair-heat-'):
102 kp_name = rand_name(name_start)
Steve Bakerb7942772013-06-27 10:23:28 +1200103 resp, body = cls.keypairs_client.create_keypair(kp_name)
104 cls.keypairs.append(kp_name)
Steve Bakerd2525a92013-05-06 15:29:03 +1200105 return body
106
107 @classmethod
Steve Bakerb1f67b52013-06-24 14:42:30 +1200108 def clear_keypairs(cls):
109 for kp_name in cls.keypairs:
110 try:
111 cls.keypairs_client.delete_keypair(kp_name)
112 except Exception:
113 pass
114
115 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +1200116 def tearDownClass(cls):
117 cls.clear_stacks()
Steve Bakerb1f67b52013-06-24 14:42:30 +1200118 cls.clear_keypairs()
Attila Fazekasf86fa312013-07-30 19:56:39 +0200119 super(BaseOrchestrationTest, cls).tearDownClass()
Steve Bakerd2525a92013-05-06 15:29:03 +1200120
121 def wait_for(self, condition):
122 """Repeatedly calls condition() until a timeout."""
123 start_time = int(time.time())
124 while True:
125 try:
126 condition()
127 except Exception:
128 pass
129 else:
130 return
131 if int(time.time()) - start_time >= self.build_timeout:
132 condition()
133 return
134 time.sleep(self.build_interval)
Steve Baker38d8f092013-06-12 12:47:16 +1200135
136 @staticmethod
137 def stack_output(stack, output_key):
138 """Return a stack output value for a give key."""
139 return next((o['output_value'] for o in stack['outputs']
140 if o['output_key'] == output_key), None)