blob: 3b12b8e6b68584df734d0331e95edf698a97631d [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
14
15from tempest import clients
16from tempest.common.utils import data_utils
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000017from tempest import config
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030018from tempest import exceptions as exc
19from 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
55 @classmethod
Andrea Frittoliba240c32014-09-15 13:14:53 +010056 def resource_setup(cls):
57 super(BaseBaremetalTest, cls).resource_setup()
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030058
Matthew Treinishcb09bbb2014-01-29 18:20:25 +000059 if not CONF.service_available.ironic:
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030060 skip_msg = ('%s skipped as Ironic is not available' % cls.__name__)
61 raise cls.skipException(skip_msg)
62
Adam Gandelman3e8f7962014-06-25 13:18:29 -070063 if CONF.baremetal.driver not in SUPPORTED_DRIVERS:
64 skip_msg = ('%s skipped as Ironic driver %s is not supported for '
65 'testing.' %
66 (cls.__name__, CONF.baremetal.driver))
67 raise cls.skipException(skip_msg)
68 cls.driver = CONF.baremetal.driver
69
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030070 mgr = clients.AdminManager()
71 cls.client = mgr.baremetal_client
Mh Raiesf8ecf232014-04-17 12:43:55 +053072 cls.power_timeout = CONF.baremetal.power_timeout
Jim Rollenhagen92420f02014-09-19 12:04:07 -070073 cls.created_objects = {}
74 for resource in RESOURCE_TYPES:
75 cls.created_objects[resource] = set()
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030076
77 @classmethod
Andrea Frittoliba240c32014-09-15 13:14:53 +010078 def resource_cleanup(cls):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030079 """Ensure that all created objects get destroyed."""
80
81 try:
Jim Rollenhagen92420f02014-09-19 12:04:07 -070082 for resource in RESOURCE_TYPES:
83 uuids = cls.created_objects[resource]
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030084 delete_method = getattr(cls.client, 'delete_%s' % resource)
85 for u in uuids:
86 delete_method(u, ignore_errors=exc.NotFound)
87 finally:
Andrea Frittoliba240c32014-09-15 13:14:53 +010088 super(BaseBaremetalTest, cls).resource_cleanup()
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +030089
90 @classmethod
91 @creates('chassis')
92 def create_chassis(cls, description=None, expect_errors=False):
93 """
94 Wrapper utility for creating test chassis.
95
96 :param description: A description of the chassis. if not supplied,
97 a random value will be generated.
98 :return: Created chassis.
99
100 """
101 description = description or data_utils.rand_name('test-chassis-')
102 resp, body = cls.client.create_chassis(description=description)
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530103 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300104
105 @classmethod
106 @creates('node')
107 def create_node(cls, chassis_id, cpu_arch='x86', cpu_num=8, storage=1024,
Adam Gandelman3e8f7962014-06-25 13:18:29 -0700108 memory=4096):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300109 """
110 Wrapper utility for creating test baremetal nodes.
111
112 :param cpu_arch: CPU architecture of the node. Default: x86.
113 :param cpu_num: Number of CPUs. Default: 8.
114 :param storage: Disk size. Default: 1024.
115 :param memory: Available RAM. Default: 4096.
116 :return: Created node.
117
118 """
119 resp, body = cls.client.create_node(chassis_id, cpu_arch=cpu_arch,
120 cpu_num=cpu_num, storage=storage,
Adam Gandelman3e8f7962014-06-25 13:18:29 -0700121 memory=memory, driver=cls.driver)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300122
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530123 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300124
125 @classmethod
126 @creates('port')
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400127 def create_port(cls, node_id, address, extra=None, uuid=None):
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300128 """
129 Wrapper utility for creating test ports.
130
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400131 :param address: MAC address of the port.
132 :param extra: Meta data of the port. If not supplied, an empty
133 dictionary will be created.
134 :param uuid: UUID of the port.
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300135 :return: Created port.
136
137 """
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400138 extra = extra or {}
139 resp, body = cls.client.create_port(address=address, node_id=node_id,
140 extra=extra, uuid=uuid)
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300141
Mh Raiesa9bb79d2014-04-17 16:20:17 +0530142 return resp, body
Roman Prykhodchenko62b1ed12013-10-16 21:51:47 +0300143
144 @classmethod
145 def delete_chassis(cls, chassis_id):
146 """
147 Deletes a chassis having the specified UUID.
148
149 :param uuid: The unique identifier of the chassis.
150 :return: Server response.
151
152 """
153
154 resp, body = cls.client.delete_chassis(chassis_id)
155
156 if chassis_id in cls.created_objects['chassis']:
157 cls.created_objects['chassis'].remove(chassis_id)
158
159 return resp
160
161 @classmethod
162 def delete_node(cls, node_id):
163 """
164 Deletes a node having the specified UUID.
165
166 :param uuid: The unique identifier of the node.
167 :return: Server response.
168
169 """
170
171 resp, body = cls.client.delete_node(node_id)
172
173 if node_id in cls.created_objects['node']:
174 cls.created_objects['node'].remove(node_id)
175
176 return resp
177
178 @classmethod
179 def delete_port(cls, port_id):
180 """
181 Deletes a port having the specified UUID.
182
183 :param uuid: The unique identifier of the port.
184 :return: Server response.
185
186 """
187
188 resp, body = cls.client.delete_port(port_id)
189
190 if port_id in cls.created_objects['port']:
191 cls.created_objects['port'].remove(port_id)
192
193 return resp
Sergey Nikitin0d43eb52014-02-03 14:50:02 +0400194
195 def validate_self_link(self, resource, uuid, link):
196 """Check whether the given self link formatted correctly."""
197 expected_link = "{base}/{pref}/{res}/{uuid}".format(
198 base=self.client.base_url,
199 pref=self.client.uri_prefix,
200 res=resource,
201 uuid=uuid)
202 self.assertEqual(expected_link, link)