blob: 0e50ac33df3bf118368040dcfeeb891ae55db2da [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")
Steve Baker6bf3d632013-06-24 14:44:40 +120036 cls.build_timeout = cls.orchestration_cfg.build_timeout
37 cls.build_interval = cls.orchestration_cfg.build_interval
Steve Bakerd2525a92013-05-06 15:29:03 +120038
39 cls.os = os
40 cls.orchestration_client = os.orchestration_client
Steve Baker38d8f092013-06-12 12:47:16 +120041 cls.servers_client = os.servers_client
Steve Bakerd2525a92013-05-06 15:29:03 +120042 cls.keypairs_client = os.keypairs_client
43 cls.stacks = []
Steve Bakerb1f67b52013-06-24 14:42:30 +120044 cls.keypairs = []
Steve Bakerd2525a92013-05-06 15:29:03 +120045
46 @classmethod
47 def _get_identity_admin_client(cls):
48 """
49 Returns an instance of the Identity Admin API client
50 """
51 os = clients.AdminManager(interface=cls._interface)
52 admin_client = os.identity_client
53 return admin_client
54
55 @classmethod
56 def _get_client_args(cls):
57
58 return (
59 cls.config,
60 cls.config.identity.admin_username,
61 cls.config.identity.admin_password,
62 cls.config.identity.uri
63 )
64
65 def create_stack(self, stack_name, template_data, parameters={}):
66 resp, body = self.client.create_stack(
67 stack_name,
68 template=template_data,
69 parameters=parameters)
70 self.assertEqual('201', resp['status'])
71 stack_id = resp['location'].split('/')[-1]
72 stack_identifier = '%s/%s' % (stack_name, stack_id)
73 self.stacks.append(stack_identifier)
74 return stack_identifier
75
76 @classmethod
77 def clear_stacks(cls):
78 for stack_identifier in cls.stacks:
79 try:
80 cls.orchestration_client.delete_stack(stack_identifier)
81 except Exception:
82 pass
83
84 for stack_identifier in cls.stacks:
85 try:
86 cls.orchestration_client.wait_for_stack_status(
87 stack_identifier, 'DELETE_COMPLETE')
88 except Exception:
89 pass
90
91 def _create_keypair(self, namestart='keypair-heat-'):
92 kp_name = rand_name(namestart)
93 resp, body = self.keypairs_client.create_keypair(kp_name)
94 self.assertEqual(body['name'], kp_name)
Steve Bakerb1f67b52013-06-24 14:42:30 +120095 self.keypairs.append(kp_name)
Steve Bakerd2525a92013-05-06 15:29:03 +120096 return body
97
98 @classmethod
Steve Bakerb1f67b52013-06-24 14:42:30 +120099 def clear_keypairs(cls):
100 for kp_name in cls.keypairs:
101 try:
102 cls.keypairs_client.delete_keypair(kp_name)
103 except Exception:
104 pass
105
106 @classmethod
Steve Bakerd2525a92013-05-06 15:29:03 +1200107 def tearDownClass(cls):
108 cls.clear_stacks()
Steve Bakerb1f67b52013-06-24 14:42:30 +1200109 cls.clear_keypairs()
Steve Bakerd2525a92013-05-06 15:29:03 +1200110
111 def wait_for(self, condition):
112 """Repeatedly calls condition() until a timeout."""
113 start_time = int(time.time())
114 while True:
115 try:
116 condition()
117 except Exception:
118 pass
119 else:
120 return
121 if int(time.time()) - start_time >= self.build_timeout:
122 condition()
123 return
124 time.sleep(self.build_interval)
Steve Baker38d8f092013-06-12 12:47:16 +1200125
126 @staticmethod
127 def stack_output(stack, output_key):
128 """Return a stack output value for a give key."""
129 return next((o['output_value'] for o in stack['outputs']
130 if o['output_key'] == output_key), None)