blob: 80b69b91b94f59bfed0238537f82f920f13a5e19 [file] [log] [blame]
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +03001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import functools
Matthew Treinish01472ff2015-02-20 17:26:52 -050014
Masayuki Igawabfa07602015-01-20 18:47:17 +090015from tempest_lib import exceptions as lib_exc
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030016
Fei Long Wangd39431f2015-05-14 11:30:48 +120017from tempest.common.utils import data_utils
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000018from tempest import config
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030019from tempest import test
20
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000021CONF = config.CONF
22
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030023
Adam Gandelman3e8f7962014-06-25 13:18:29 -070024# NOTE(adam_g): The baremetal API tests exercise operations such as enroll
25# node, power on, power off, etc. Testing against real drivers (ie, IPMI)
26# will require passing driver-specific data to Tempest (addresses,
27# credentials, etc). Until then, only support testing against the fake driver,
28# which has no external dependencies.
29SUPPORTED_DRIVERS = ['fake']
30
Jim Rollenhagen92420f02014-09-19 12:04:07 -070031# NOTE(jroll): resources must be deleted in a specific order, this list
32# defines the resource types to clean up, and the correct order.
33RESOURCE_TYPES = ['port', 'node', 'chassis']
34
Adam Gandelman3e8f7962014-06-25 13:18:29 -070035
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030036def creates(resource):
37 """Decorator that adds resources to the appropriate cleanup list."""
38
39 def decorator(f):
40 @functools.wraps(f)
41 def wrapper(cls, *args, **kwargs):
Mh Raiesa9bb79d2014-04-17 16:20:17 +053042 resp, body = f(cls, *args, **kwargs)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030043
44 if 'uuid' in body:
45 cls.created_objects[resource].add(body['uuid'])
46
Mh Raiesa9bb79d2014-04-17 16:20:17 +053047 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030048 return wrapper
49 return decorator
50
51
52class BaseBaremetalTest(test.BaseTestCase):
53 """Base class for Baremetal API tests."""
54
Andrea Frittolib21de6c2015-02-06 20:12:38 +000055 credentials = ['admin']
56
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030057 @classmethod
Rohan Kanade7ed42f52015-02-03 13:00:29 +053058 def skip_checks(cls):
59 super(BaseBaremetalTest, cls).skip_checks()
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000060 if not CONF.service_available.ironic:
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030061 skip_msg = ('%s skipped as Ironic is not available' % cls.__name__)
62 raise cls.skipException(skip_msg)
63
Adam Gandelman3e8f7962014-06-25 13:18:29 -070064 if CONF.baremetal.driver not in SUPPORTED_DRIVERS:
65 skip_msg = ('%s skipped as Ironic driver %s is not supported for '
66 'testing.' %
67 (cls.__name__, CONF.baremetal.driver))
68 raise cls.skipException(skip_msg)
Adam Gandelman3e8f7962014-06-25 13:18:29 -070069
Rohan Kanade7ed42f52015-02-03 13:00:29 +053070 @classmethod
Rohan Kanade7ed42f52015-02-03 13:00:29 +053071 def setup_clients(cls):
72 super(BaseBaremetalTest, cls).setup_clients()
Andrea Frittolib21de6c2015-02-06 20:12:38 +000073 cls.client = cls.os_admin.baremetal_client
Rohan Kanade7ed42f52015-02-03 13:00:29 +053074
75 @classmethod
76 def resource_setup(cls):
77 super(BaseBaremetalTest, cls).resource_setup()
78
79 cls.driver = CONF.baremetal.driver
Mh Raiesf8ecf232014-04-17 12:43:55 +053080 cls.power_timeout = CONF.baremetal.power_timeout
Jim Rollenhagen92420f02014-09-19 12:04:07 -070081 cls.created_objects = {}
82 for resource in RESOURCE_TYPES:
83 cls.created_objects[resource] = set()
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030084
85 @classmethod
Andrea Frittoliba240c32014-09-15 13:14:53 +010086 def resource_cleanup(cls):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030087 """Ensure that all created objects get destroyed."""
88
89 try:
Jim Rollenhagen92420f02014-09-19 12:04:07 -070090 for resource in RESOURCE_TYPES:
91 uuids = cls.created_objects[resource]
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030092 delete_method = getattr(cls.client, 'delete_%s' % resource)
93 for u in uuids:
Masayuki Igawabfa07602015-01-20 18:47:17 +090094 delete_method(u, ignore_errors=lib_exc.NotFound)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030095 finally:
Andrea Frittoliba240c32014-09-15 13:14:53 +010096 super(BaseBaremetalTest, cls).resource_cleanup()
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030097
98 @classmethod
99 @creates('chassis')
100 def create_chassis(cls, description=None, expect_errors=False):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000101 """Wrapper utility for creating test chassis.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300102
103 :param description: A description of the chassis. if not supplied,
104 a random value will be generated.
105 :return: Created chassis.
106
107 """
Ken'ichi Ohmichi823d3312015-03-23 00:27:53 +0000108 description = description or data_utils.rand_name('test-chassis')
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300109 resp, body = cls.client.create_chassis(description=description)
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530110 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300111
112 @classmethod
113 @creates('node')
Adam Gandelman3ea1eb82015-02-18 19:13:25 -0800114 def create_node(cls, chassis_id, cpu_arch='x86', cpus=8, local_gb=10,
115 memory_mb=4096):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000116 """Wrapper utility for creating test baremetal nodes.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300117
118 :param cpu_arch: CPU architecture of the node. Default: x86.
Adam Gandelman3ea1eb82015-02-18 19:13:25 -0800119 :param cpus: Number of CPUs. Default: 8.
120 :param local_gb: Disk size. Default: 10.
121 :param memory_mb: Available RAM. Default: 4096.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300122 :return: Created node.
123
124 """
125 resp, body = cls.client.create_node(chassis_id, cpu_arch=cpu_arch,
Adam Gandelman3ea1eb82015-02-18 19:13:25 -0800126 cpus=cpus, local_gb=local_gb,
127 memory_mb=memory_mb,
128 driver=cls.driver)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300129
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530130 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300131
132 @classmethod
133 @creates('port')
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400134 def create_port(cls, node_id, address, extra=None, uuid=None):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000135 """Wrapper utility for creating test ports.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300136
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400137 :param address: MAC address of the port.
138 :param extra: Meta data of the port. If not supplied, an empty
139 dictionary will be created.
140 :param uuid: UUID of the port.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300141 :return: Created port.
142
143 """
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400144 extra = extra or {}
145 resp, body = cls.client.create_port(address=address, node_id=node_id,
146 extra=extra, uuid=uuid)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300147
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530148 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300149
150 @classmethod
151 def delete_chassis(cls, chassis_id):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000152 """Deletes a chassis having the specified UUID.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300153
154 :param uuid: The unique identifier of the chassis.
155 :return: Server response.
156
157 """
158
159 resp, body = cls.client.delete_chassis(chassis_id)
160
161 if chassis_id in cls.created_objects['chassis']:
162 cls.created_objects['chassis'].remove(chassis_id)
163
164 return resp
165
166 @classmethod
167 def delete_node(cls, node_id):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000168 """Deletes a node having the specified UUID.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300169
170 :param uuid: The unique identifier of the node.
171 :return: Server response.
172
173 """
174
175 resp, body = cls.client.delete_node(node_id)
176
177 if node_id in cls.created_objects['node']:
178 cls.created_objects['node'].remove(node_id)
179
180 return resp
181
182 @classmethod
183 def delete_port(cls, port_id):
Ken'ichi Ohmichi9e3dac02015-11-19 07:01:07 +0000184 """Deletes a port having the specified UUID.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300185
186 :param uuid: The unique identifier of the port.
187 :return: Server response.
188
189 """
190
191 resp, body = cls.client.delete_port(port_id)
192
193 if port_id in cls.created_objects['port']:
194 cls.created_objects['port'].remove(port_id)
195
196 return resp
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400197
198 def validate_self_link(self, resource, uuid, link):
199 """Check whether the given self link formatted correctly."""
200 expected_link = "{base}/{pref}/{res}/{uuid}".format(
201 base=self.client.base_url,
202 pref=self.client.uri_prefix,
203 res=resource,
204 uuid=uuid)
205 self.assertEqual(expected_link, link)