Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 1 | # 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 | |
| 15 | import logging |
| 16 | import time |
| 17 | |
| 18 | from tempest import clients |
| 19 | from tempest.common.utils.data_utils import rand_name |
| 20 | import tempest.test |
| 21 | |
| 22 | |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | class 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 Baker | 38d8f09 | 2013-06-12 12:47:16 +1200 | [diff] [blame] | 39 | cls.servers_client = os.servers_client |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 40 | cls.keypairs_client = os.keypairs_client |
| 41 | cls.stacks = [] |
Steve Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame^] | 42 | cls.keypairs = [] |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 43 | |
| 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 Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame^] | 93 | self.keypairs.append(kp_name) |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 94 | return body |
| 95 | |
| 96 | @classmethod |
Steve Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame^] | 97 | 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 Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 105 | def tearDownClass(cls): |
| 106 | cls.clear_stacks() |
Steve Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame^] | 107 | cls.clear_keypairs() |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 108 | |
| 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 Baker | 38d8f09 | 2013-06-12 12:47:16 +1200 | [diff] [blame] | 123 | |
| 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) |