blob: d7d2efe5e2b8771baed47813933de0f45d82dbf3 [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):
101 """
102 Wrapper utility for creating test chassis.
103
104 :param description: A description of the chassis. if not supplied,
105 a random value will be generated.
106 :return: Created chassis.
107
108 """
Ken'ichi Ohmichi823d3312015-03-23 00:27:53 +0000109 description = description or data_utils.rand_name('test-chassis')
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300110 resp, body = cls.client.create_chassis(description=description)
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530111 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300112
113 @classmethod
114 @creates('node')
Adam Gandelman3ea1eb82015-02-18 19:13:25 -0800115 def create_node(cls, chassis_id, cpu_arch='x86', cpus=8, local_gb=10,
116 memory_mb=4096):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300117 """
118 Wrapper utility for creating test baremetal nodes.
119
120 :param cpu_arch: CPU architecture of the node. Default: x86.
Adam Gandelman3ea1eb82015-02-18 19:13:25 -0800121 :param cpus: Number of CPUs. Default: 8.
122 :param local_gb: Disk size. Default: 10.
123 :param memory_mb: Available RAM. Default: 4096.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300124 :return: Created node.
125
126 """
127 resp, body = cls.client.create_node(chassis_id, cpu_arch=cpu_arch,
Adam Gandelman3ea1eb82015-02-18 19:13:25 -0800128 cpus=cpus, local_gb=local_gb,
129 memory_mb=memory_mb,
130 driver=cls.driver)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300131
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530132 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300133
134 @classmethod
135 @creates('port')
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400136 def create_port(cls, node_id, address, extra=None, uuid=None):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300137 """
138 Wrapper utility for creating test ports.
139
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400140 :param address: MAC address of the port.
141 :param extra: Meta data of the port. If not supplied, an empty
142 dictionary will be created.
143 :param uuid: UUID of the port.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300144 :return: Created port.
145
146 """
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400147 extra = extra or {}
148 resp, body = cls.client.create_port(address=address, node_id=node_id,
149 extra=extra, uuid=uuid)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300150
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530151 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300152
153 @classmethod
154 def delete_chassis(cls, chassis_id):
155 """
156 Deletes a chassis having the specified UUID.
157
158 :param uuid: The unique identifier of the chassis.
159 :return: Server response.
160
161 """
162
163 resp, body = cls.client.delete_chassis(chassis_id)
164
165 if chassis_id in cls.created_objects['chassis']:
166 cls.created_objects['chassis'].remove(chassis_id)
167
168 return resp
169
170 @classmethod
171 def delete_node(cls, node_id):
172 """
173 Deletes a node having the specified UUID.
174
175 :param uuid: The unique identifier of the node.
176 :return: Server response.
177
178 """
179
180 resp, body = cls.client.delete_node(node_id)
181
182 if node_id in cls.created_objects['node']:
183 cls.created_objects['node'].remove(node_id)
184
185 return resp
186
187 @classmethod
188 def delete_port(cls, port_id):
189 """
190 Deletes a port having the specified UUID.
191
192 :param uuid: The unique identifier of the port.
193 :return: Server response.
194
195 """
196
197 resp, body = cls.client.delete_port(port_id)
198
199 if port_id in cls.created_objects['port']:
200 cls.created_objects['port'].remove(port_id)
201
202 return resp
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400203
204 def validate_self_link(self, resource, uuid, link):
205 """Check whether the given self link formatted correctly."""
206 expected_link = "{base}/{pref}/{res}/{uuid}".format(
207 base=self.client.base_url,
208 pref=self.client.uri_prefix,
209 res=resource,
210 uuid=uuid)
211 self.assertEqual(expected_link, link)