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 | |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 15 | import time |
| 16 | |
| 17 | from tempest import clients |
| 18 | from tempest.common.utils.data_utils import rand_name |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 19 | from tempest.openstack.common import log as logging |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 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): |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 31 | super(BaseOrchestrationTest, cls).setUpClass() |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 32 | os = clients.OrchestrationManager() |
| 33 | cls.orchestration_cfg = os.config.orchestration |
Matthew Treinish | a9d4388 | 2013-07-19 16:54:52 -0400 | [diff] [blame] | 34 | if not os.config.service_available.heat: |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 35 | raise cls.skipException("Heat support is required") |
Steve Baker | 6bf3d63 | 2013-06-24 14:44:40 +1200 | [diff] [blame] | 36 | cls.build_timeout = cls.orchestration_cfg.build_timeout |
| 37 | cls.build_interval = cls.orchestration_cfg.build_interval |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 38 | |
| 39 | cls.os = os |
| 40 | cls.orchestration_client = os.orchestration_client |
Steve Baker | 38d8f09 | 2013-06-12 12:47:16 +1200 | [diff] [blame] | 41 | cls.servers_client = os.servers_client |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 42 | cls.keypairs_client = os.keypairs_client |
| 43 | cls.stacks = [] |
Steve Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame] | 44 | cls.keypairs = [] |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 45 | |
| 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 | |
Steve Baker | b794277 | 2013-06-27 10:23:28 +1200 | [diff] [blame] | 65 | @classmethod |
| 66 | def create_stack(cls, stack_name, template_data, parameters={}): |
| 67 | resp, body = cls.client.create_stack( |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 68 | stack_name, |
| 69 | template=template_data, |
| 70 | parameters=parameters) |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 71 | stack_id = resp['location'].split('/')[-1] |
| 72 | stack_identifier = '%s/%s' % (stack_name, stack_id) |
Steve Baker | b794277 | 2013-06-27 10:23:28 +1200 | [diff] [blame] | 73 | cls.stacks.append(stack_identifier) |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 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 | |
Steve Baker | b794277 | 2013-06-27 10:23:28 +1200 | [diff] [blame] | 91 | @classmethod |
Bartosz Górski | ab33b7e | 2013-06-27 00:39:47 -0700 | [diff] [blame] | 92 | def _create_keypair(cls, name_start='keypair-heat-'): |
| 93 | kp_name = rand_name(name_start) |
Steve Baker | b794277 | 2013-06-27 10:23:28 +1200 | [diff] [blame] | 94 | resp, body = cls.keypairs_client.create_keypair(kp_name) |
| 95 | cls.keypairs.append(kp_name) |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 96 | return body |
| 97 | |
| 98 | @classmethod |
Steve Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame] | 99 | 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 Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 107 | def tearDownClass(cls): |
| 108 | cls.clear_stacks() |
Steve Baker | b1f67b5 | 2013-06-24 14:42:30 +1200 | [diff] [blame] | 109 | cls.clear_keypairs() |
Attila Fazekas | f86fa31 | 2013-07-30 19:56:39 +0200 | [diff] [blame] | 110 | super(BaseOrchestrationTest, cls).tearDownClass() |
Steve Baker | d2525a9 | 2013-05-06 15:29:03 +1200 | [diff] [blame] | 111 | |
| 112 | def wait_for(self, condition): |
| 113 | """Repeatedly calls condition() until a timeout.""" |
| 114 | start_time = int(time.time()) |
| 115 | while True: |
| 116 | try: |
| 117 | condition() |
| 118 | except Exception: |
| 119 | pass |
| 120 | else: |
| 121 | return |
| 122 | if int(time.time()) - start_time >= self.build_timeout: |
| 123 | condition() |
| 124 | return |
| 125 | time.sleep(self.build_interval) |
Steve Baker | 38d8f09 | 2013-06-12 12:47:16 +1200 | [diff] [blame] | 126 | |
| 127 | @staticmethod |
| 128 | def stack_output(stack, output_key): |
| 129 | """Return a stack output value for a give key.""" |
| 130 | return next((o['output_value'] for o in stack['outputs'] |
| 131 | if o['output_key'] == output_key), None) |