Roman Prykhodchenko | 62b1ed1 | 2013-10-16 21:51:47 +0300 | [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 functools |
| 16 | |
| 17 | from tempest import clients |
| 18 | from tempest.common.utils import data_utils |
| 19 | from tempest import exceptions as exc |
| 20 | from tempest import test |
| 21 | |
| 22 | |
| 23 | def creates(resource): |
| 24 | """Decorator that adds resources to the appropriate cleanup list.""" |
| 25 | |
| 26 | def decorator(f): |
| 27 | @functools.wraps(f) |
| 28 | def wrapper(cls, *args, **kwargs): |
| 29 | result = f(cls, *args, **kwargs) |
| 30 | body = result[resource] |
| 31 | |
| 32 | if 'uuid' in body: |
| 33 | cls.created_objects[resource].add(body['uuid']) |
| 34 | |
| 35 | return result |
| 36 | return wrapper |
| 37 | return decorator |
| 38 | |
| 39 | |
| 40 | class BaseBaremetalTest(test.BaseTestCase): |
| 41 | """Base class for Baremetal API tests.""" |
| 42 | |
| 43 | @classmethod |
| 44 | def setUpClass(cls): |
| 45 | super(BaseBaremetalTest, cls).setUpClass() |
| 46 | |
| 47 | if not cls.config.service_available.ironic: |
| 48 | skip_msg = ('%s skipped as Ironic is not available' % cls.__name__) |
| 49 | raise cls.skipException(skip_msg) |
| 50 | |
| 51 | mgr = clients.AdminManager() |
| 52 | cls.client = mgr.baremetal_client |
| 53 | |
| 54 | cls.created_objects = {'chassis': set(), |
| 55 | 'port': set(), |
| 56 | 'node': set()} |
| 57 | |
| 58 | @classmethod |
| 59 | def tearDownClass(cls): |
| 60 | """Ensure that all created objects get destroyed.""" |
| 61 | |
| 62 | try: |
| 63 | for resource, uuids in cls.created_objects.iteritems(): |
| 64 | delete_method = getattr(cls.client, 'delete_%s' % resource) |
| 65 | for u in uuids: |
| 66 | delete_method(u, ignore_errors=exc.NotFound) |
| 67 | finally: |
| 68 | super(BaseBaremetalTest, cls).tearDownClass() |
| 69 | |
| 70 | @classmethod |
| 71 | @creates('chassis') |
| 72 | def create_chassis(cls, description=None, expect_errors=False): |
| 73 | """ |
| 74 | Wrapper utility for creating test chassis. |
| 75 | |
| 76 | :param description: A description of the chassis. if not supplied, |
| 77 | a random value will be generated. |
| 78 | :return: Created chassis. |
| 79 | |
| 80 | """ |
| 81 | description = description or data_utils.rand_name('test-chassis-') |
| 82 | resp, body = cls.client.create_chassis(description=description) |
| 83 | |
| 84 | return {'chassis': body, 'response': resp} |
| 85 | |
| 86 | @classmethod |
| 87 | @creates('node') |
| 88 | def create_node(cls, chassis_id, cpu_arch='x86', cpu_num=8, storage=1024, |
| 89 | memory=4096, driver='fake'): |
| 90 | """ |
| 91 | Wrapper utility for creating test baremetal nodes. |
| 92 | |
| 93 | :param cpu_arch: CPU architecture of the node. Default: x86. |
| 94 | :param cpu_num: Number of CPUs. Default: 8. |
| 95 | :param storage: Disk size. Default: 1024. |
| 96 | :param memory: Available RAM. Default: 4096. |
| 97 | :return: Created node. |
| 98 | |
| 99 | """ |
| 100 | resp, body = cls.client.create_node(chassis_id, cpu_arch=cpu_arch, |
| 101 | cpu_num=cpu_num, storage=storage, |
| 102 | memory=memory, driver=driver) |
| 103 | |
| 104 | return {'node': body, 'response': resp} |
| 105 | |
| 106 | @classmethod |
| 107 | @creates('port') |
| 108 | def create_port(cls, node_id, address=None): |
| 109 | """ |
| 110 | Wrapper utility for creating test ports. |
| 111 | |
| 112 | :param address: MAC address of the port. If not supplied, a random |
| 113 | value will be generated. |
| 114 | :return: Created port. |
| 115 | |
| 116 | """ |
| 117 | address = address or data_utils.rand_mac_address() |
| 118 | resp, body = cls.client.create_port(address=address, node_id=node_id) |
| 119 | |
| 120 | return {'port': body, 'response': resp} |
| 121 | |
| 122 | @classmethod |
| 123 | def delete_chassis(cls, chassis_id): |
| 124 | """ |
| 125 | Deletes a chassis having the specified UUID. |
| 126 | |
| 127 | :param uuid: The unique identifier of the chassis. |
| 128 | :return: Server response. |
| 129 | |
| 130 | """ |
| 131 | |
| 132 | resp, body = cls.client.delete_chassis(chassis_id) |
| 133 | |
| 134 | if chassis_id in cls.created_objects['chassis']: |
| 135 | cls.created_objects['chassis'].remove(chassis_id) |
| 136 | |
| 137 | return resp |
| 138 | |
| 139 | @classmethod |
| 140 | def delete_node(cls, node_id): |
| 141 | """ |
| 142 | Deletes a node having the specified UUID. |
| 143 | |
| 144 | :param uuid: The unique identifier of the node. |
| 145 | :return: Server response. |
| 146 | |
| 147 | """ |
| 148 | |
| 149 | resp, body = cls.client.delete_node(node_id) |
| 150 | |
| 151 | if node_id in cls.created_objects['node']: |
| 152 | cls.created_objects['node'].remove(node_id) |
| 153 | |
| 154 | return resp |
| 155 | |
| 156 | @classmethod |
| 157 | def delete_port(cls, port_id): |
| 158 | """ |
| 159 | Deletes a port having the specified UUID. |
| 160 | |
| 161 | :param uuid: The unique identifier of the port. |
| 162 | :return: Server response. |
| 163 | |
| 164 | """ |
| 165 | |
| 166 | resp, body = cls.client.delete_port(port_id) |
| 167 | |
| 168 | if port_id in cls.created_objects['port']: |
| 169 | cls.created_objects['port'].remove(port_id) |
| 170 | |
| 171 | return resp |