blob: fa8190a546a0a3ad90eb5e19c8adc19ebf45eaa1 [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
15import logging
16import time
17
18from tempest import clients
19from tempest.common.utils.data_utils import rand_name
20import 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):
31
32 os = clients.OrchestrationManager()
33 cls.orchestration_cfg = os.config.orchestration
34 if not cls.orchestration_cfg.heat_available:
35 raise cls.skipException("Heat support is required")
36
37 cls.os = os
38 cls.orchestration_client = os.orchestration_client
Steve Baker38d8f092013-06-12 12:47:16 +120039 cls.servers_client = os.servers_client
Steve Bakerd2525a92013-05-06 15:29:03 +120040 cls.keypairs_client = os.keypairs_client
41 cls.stacks = []
Steve Bakerb1f67b52013-06-24 14:42:30 +120042 cls.keypairs = []
Steve Bakerd2525a92013-05-06 15:29:03 +120043
44 @classmethod
45 def _get_identity_admin_client(cls):
46 """
47 Returns an instance of the Identity Admin API client
48 """
49 os = clients.AdminManager(interface=cls._interface)
50 admin_client = os.identity_client
51 return admin_client
52
53 @classmethod
54 def _get_client_args(cls):
55
56 return (
57 cls.config,
58 cls.config.identity.admin_username,
59 cls.config.identity.admin_password,
60 cls.config.identity.uri
61 )
62
63 def create_stack(self, stack_name, template_data, parameters={}):
64 resp, body = self.client.create_stack(
65 stack_name,
66 template=template_data,
67 parameters=parameters)
68 self.assertEqual('201', resp['status'])
69 stack_id = resp['location'].split('/')[-1]
70 stack_identifier = '%s/%s' % (stack_name, stack_id)
71 self.stacks.append(stack_identifier)
72 return stack_identifier
73
74 @classmethod
75 def clear_stacks(cls):
76 for stack_identifier in cls.stacks:
77 try:
78 cls.orchestration_client.delete_stack(stack_identifier)
79 except Exception:
80 pass
81
82 for stack_identifier in cls.stacks:
83 try:
84 cls.orchestration_client.wait_for_stack_status(
85 stack_identifier, 'DELETE_COMPLETE')
86 except Exception:
87 pass
88
89 def _create_keypair(self, namestart='keypair-heat-'):
90 kp_name = rand_name(namestart)
91 resp, body = self.keypairs_client.create_keypair(kp_name)
92 self.assertEqual(body['name'], kp_name)
Steve Bakerb1f67b52013-06-24 14:42:30 +120093 self.keypairs.append(kp_name)
Steve Bakerd2525a92013-05-06 15:29:03 +120094 return body
95
96 @classmethod
Steve Bakerb1f67b52013-06-24 14:42:30 +120097 def clear_keypairs(cls):
98 for kp_name in cls.keypairs:
99 try:
100 cls.keypairs_client.delete_keypair(kp_name)
101 except Exception:
102 pass
103
104 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +1200105 def tearDownClass(cls):
106 cls.clear_stacks()
Steve Bakerb1f67b52013-06-24 14:42:30 +1200107 cls.clear_keypairs()
Steve Bakerd2525a92013-05-06 15:29:03 +1200108
109 def wait_for(self, condition):
110 """Repeatedly calls condition() until a timeout."""
111 start_time = int(time.time())
112 while True:
113 try:
114 condition()
115 except Exception:
116 pass
117 else:
118 return
119 if int(time.time()) - start_time >= self.build_timeout:
120 condition()
121 return
122 time.sleep(self.build_interval)
Steve Baker38d8f092013-06-12 12:47:16 +1200123
124 @staticmethod
125 def stack_output(stack, output_key):
126 """Return a stack output value for a give key."""
127 return next((o['output_value'] for o in stack['outputs']
128 if o['output_key'] == output_key), None)