Debian Codegraph @

ironic_tempest_plugin/tests/api/admin/test_allocations.py

raw
1 # 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
13 from oslo_utils import uuidutils
14 from tempest import config
15 from tempest.lib import decorators
16 from tempest.lib import exceptions as lib_exc
17
18 from ironic_tempest_plugin.common import waiters
19 from ironic_tempest_plugin.tests.api import base
20
21 CONF = config.CONF
22
23
24 class Base(base.BaseBaremetalTest):
25 @classmethod
26 def provide_and_power_off_node(cls, node_id, cleaning_timeout=None):
27 cls.provide_node(node_id, cleaning_timeout)
28 # Force non-empty power state, otherwise allocation API won't pick it
29 cls.client.set_node_power_state(node_id, 'power off')
30 waiters.wait_for_bm_node_status(cls.client, node_id,
31 'power_state', 'power off')
32
33 def setUp(self):
34 super(Base, self).setUp()
35
36 # Generate a resource class to prevent parallel tests from clashing
37 # with each other.
38 self.resource_class = uuidutils.generate_uuid()
39
40 _, self.chassis = self.create_chassis()
41 _, self.node = self.create_node(
42 self.chassis['uuid'],
43 resource_class=self.resource_class,
44 # Fake deploy interface to avoid stop when automated
45 # cleaning is on.
46 deploy_interface='fake',
47 # noop network interface in case a cleaning/provisioning network
48 # is not defined.
49 network_interface='noop')
50 self.provide_and_power_off_node(self.node['uuid'])
51
52
53 class TestAllocations(Base):
54 """Tests for baremetal allocations."""
55
56 min_microversion = '1.52'
57
58 @decorators.idempotent_id('9203ea28-3c61-4108-8498-22247b654ff6')
59 def test_create_show_allocation(self):
60 self.assertIsNone(self.node['allocation_uuid'])
61 _, body = self.create_allocation(self.resource_class)
62 uuid = body['uuid']
63
64 self.assertTrue(uuid)
65 self.assertEqual('allocating', body['state'])
66 self.assertEqual(self.resource_class, body['resource_class'])
67 self.assertIsNone(body['last_error'])
68 self.assertIsNone(body['node_uuid'])
69
70 _, body = waiters.wait_for_allocation(self.client, uuid)
71 self.assertEqual('active', body['state'])
72 self.assertEqual(self.resource_class, body['resource_class'])
73 self.assertIsNone(body['last_error'])
74 self.assertEqual(self.node['uuid'], body['node_uuid'])
75
76 _, body2 = self.client.show_node_allocation(body['node_uuid'])
77 self.assertEqual(body, body2)
78
79 _, node = self.client.show_node(self.node['uuid'])
80 self.assertEqual(uuid, node['allocation_uuid'])
81
82 @decorators.idempotent_id('eb074d06-e5f4-4fb4-b992-c9929db488ae')
83 def test_create_allocation_with_traits(self):
84 _, node2 = self.create_node(self.chassis['uuid'],
85 resource_class=self.resource_class,
86 deploy_interface='fake')
87 self.client.set_node_traits(node2['uuid'], ['CUSTOM_MEOW'])
88 self.provide_and_power_off_node(node2['uuid'])
89
90 _, body = self.create_allocation(self.resource_class,
91 traits=['CUSTOM_MEOW'])
92 uuid = body['uuid']
93
94 self.assertTrue(uuid)
95 self.assertEqual('allocating', body['state'])
96 self.assertEqual(['CUSTOM_MEOW'], body['traits'])
97 self.assertIsNone(body['last_error'])
98
99 _, body = waiters.wait_for_allocation(self.client, uuid)
100 self.assertEqual('active', body['state'])
101 self.assertEqual(['CUSTOM_MEOW'], body['traits'])
102 self.assertIsNone(body['last_error'])
103 self.assertEqual(node2['uuid'], body['node_uuid'])
104
105 @decorators.idempotent_id('12d19297-f35a-408a-8b1e-3cd244e30abe')
106 def test_create_allocation_candidate_node(self):
107 node_name = 'allocation-test-1'
108 _, node2 = self.create_node(self.chassis['uuid'],
109 resource_class=self.resource_class,
110 name=node_name,
111 deploy_interface='fake',
112 network_interface='noop')
113 self.provide_and_power_off_node(node2['uuid'])
114
115 _, body = self.create_allocation(self.resource_class,
116 candidate_nodes=[node_name])
117 uuid = body['uuid']
118
119 self.assertTrue(uuid)
120 self.assertEqual('allocating', body['state'])
121 self.assertEqual([node2['uuid']], body['candidate_nodes'])
122 self.assertIsNone(body['last_error'])
123
124 _, body = waiters.wait_for_allocation(self.client, uuid)
125 self.assertEqual('active', body['state'])
126 self.assertEqual([node2['uuid']], body['candidate_nodes'])
127 self.assertIsNone(body['last_error'])
128 self.assertEqual(node2['uuid'], body['node_uuid'])
129
130 @decorators.idempotent_id('84eb3c21-4e16-4f33-9551-dce0f8689462')
131 def test_delete_allocation(self):
132 _, body = self.create_allocation(self.resource_class)
133 self.client.delete_allocation(body['uuid'])
134 self.assertRaises(lib_exc.NotFound, self.client.show_allocation,
135 body['uuid'])
136
137 @decorators.idempotent_id('5e30452d-ee92-4342-82c1-5eea5e55c937')
138 def test_delete_allocation_by_name(self):
139 name = 'alloc-%s' % uuidutils.generate_uuid()
140 _, body = self.create_allocation(self.resource_class, name=name)
141 self.client.delete_allocation(name)
142 self.assertRaises(lib_exc.NotFound, self.client.show_allocation, name)
143
144 @decorators.idempotent_id('fbbc13bc-86da-438b-af01-d1bc1bab57d6')
145 def test_show_by_name(self):
146 name = 'alloc-%s' % uuidutils.generate_uuid()
147 _, body = self.create_allocation(self.resource_class, name=name)
148 _, loaded_body = self.client.show_allocation(name)
149 # The allocation will likely have been processed by this time, so do
150 # not compare the whole body.
151 for field in ('name', 'uuid', 'resource_class'):
152 self.assertEqual(body[field], loaded_body[field])
153
154 @decorators.idempotent_id('4ca123c4-160d-4d8d-a3f7-15feda812263')
155 def test_list_allocations(self):
156 _, body = self.create_allocation(self.resource_class)
157
158 _, listing = self.client.list_allocations()
159 self.assertIn(body['uuid'],
160 [i['uuid'] for i in listing['allocations']])
161
162 _, listing = self.client.list_allocations(
163 resource_class=self.resource_class)
164 self.assertEqual([body['uuid']],
165 [i['uuid'] for i in listing['allocations']])
166
167 @decorators.idempotent_id('092b7148-9ff0-4107-be57-2cfcd21eb5d7')
168 def test_list_allocations_by_state(self):
169 _, body = self.create_allocation(self.resource_class)
170 _, body2 = self.create_allocation(self.resource_class + 'foo2')
171
172 waiters.wait_for_allocation(self.client, body['uuid'])
173 waiters.wait_for_allocation(self.client, body2['uuid'],
174 expect_error=True)
175
176 _, listing = self.client.list_allocations(state='active')
177 uuids = [i['uuid'] for i in listing['allocations']]
178 self.assertIn(body['uuid'], uuids)
179 self.assertNotIn(body2['uuid'], uuids)
180
181 _, listing = self.client.list_allocations(state='error')
182 uuids = [i['uuid'] for i in listing['allocations']]
183 self.assertNotIn(body['uuid'], uuids)
184 self.assertIn(body2['uuid'], uuids)
185
186 _, listing = self.client.list_allocations(state='allocating')
187 uuids = [i['uuid'] for i in listing['allocations']]
188 self.assertNotIn(body['uuid'], uuids)
189 self.assertNotIn(body2['uuid'], uuids)
190
191 @decorators.attr(type=['negative'])
192 @decorators.idempotent_id('bf7e1375-019a-466a-a294-9c1052827ada')
193 def test_create_allocation_resource_class_mismatch(self):
194 _, body = self.create_allocation(self.resource_class + 'foo')
195
196 _, body = waiters.wait_for_allocation(self.client, body['uuid'],
197 expect_error=True)
198 self.assertEqual('error', body['state'])
199 self.assertTrue(body['last_error'])
200
201 @decorators.attr(type=['negative'])
202 @decorators.idempotent_id('b4eeddee-ca34-44f9-908b-490b78b18486')
203 def test_create_allocation_traits_mismatch(self):
204 _, body = self.create_allocation(
205 self.resource_class, traits=['CUSTOM_DOES_NOT_EXIST'])
206
207 _, body = waiters.wait_for_allocation(self.client, body['uuid'],
208 expect_error=True)
209 self.assertEqual('error', body['state'])
210 self.assertTrue(body['last_error'])
211
212 @decorators.attr(type=['negative'])
213 @decorators.idempotent_id('2378727f-77c3-4289-9562-bd2f3b147a60')
214 def test_create_allocation_node_mismatch(self):
215 _, node2 = self.create_node(self.chassis['uuid'],
216 resource_class=self.resource_class + 'alt',
217 deploy_interface='fake')
218 # Mismatch between the resource class and the candidate node
219 _, body = self.create_allocation(
220 self.resource_class, candidate_nodes=[node2['uuid']])
221
222 _, body = waiters.wait_for_allocation(self.client, body['uuid'],
223 expect_error=True)
224 self.assertEqual('error', body['state'])
225 self.assertTrue(body['last_error'])
226
227
228 class TestAllocationsWithJsonExtSupport(Base):
229 """Tests for baremetal allocations to validate appending .json extension
230
231 to an allocation's `name` or `uuid` in API versions 1.90 and prior works.
232 """
233
234 max_microversion = '1.90' # Max API version allowed for testing: 1.90
235 min_microversion = '1.90' # Min API version allowed for testing: 1.90
236
237 @decorators.idempotent_id('d111b85b-a169-440e-9dcd-9b8b6ad8c917')
238 def test_create_show_allocation_with_json(self):
239 """Show an allocation while appending .json extension to its uuid"""
240 self.assertIsNone(self.node['allocation_uuid'])
241 _, body = self.create_allocation(self.resource_class)
242 uuid = body['uuid']
243
244 self.assertTrue(uuid)
245 self.assertEqual('allocating', body['state'])
246 self.assertEqual(self.resource_class, body['resource_class'])
247 self.assertIsNone(body['last_error'])
248 self.assertIsNone(body['node_uuid'])
249
250 _, body = waiters.wait_for_allocation(self.client, uuid)
251 self.assertEqual('active', body['state'])
252 self.assertEqual(self.resource_class, body['resource_class'])
253 self.assertIsNone(body['last_error'])
254 self.assertEqual(self.node['uuid'], body['node_uuid'])
255
256 _, body2 = self.client.show_node_allocation(body['node_uuid'])
257 self.assertEqual(body, body2)
258
259 _, node = self.client.show_node('%s.json' % self.node['uuid'])
260 self.assertEqual(uuid, node['allocation_uuid'])
261
262 @decorators.idempotent_id('b94643f4-f789-4c04-a806-bf769e0bc181')
263 def test_delete_allocation_with_json(self):
264 """Delete an allocation while appending .json extension to its uuid"""
265 _, body = self.create_allocation(self.resource_class)
266
267 self.client.delete_allocation('%s.json' % body['uuid'])
268
269 self.assertRaises(lib_exc.NotFound, self.client.show_allocation,
270 '%s.json' % body['uuid'])
271
272
273 class TestAllocationsWithoutJsonExtSupport(Base):
274 """Tests for baremetal allocations to validate appending .json extension
275
276 to an allocation's `name` or `uuid` in API versions later than 1.90 is
277
278 disabled.
279 """
280
281 min_microversion = '1.91' # Min API version allowed for testing: 1.91
282
283 @decorators.idempotent_id('6b851ef4-e364-4a3e-af1d-e6b73c1adec6')
284 def test_create_show_allocation_with_json(self):
285 """Trying to show an allocation while appending .json ext 404s"""
286 self.assertIsNone(self.node['allocation_uuid'])
287 _, body = self.create_allocation(self.resource_class)
288 uuid = body['uuid']
289
290 self.assertTrue(uuid)
291 self.assertEqual('allocating', body['state'])
292 self.assertEqual(self.resource_class, body['resource_class'])
293 self.assertIsNone(body['last_error'])
294 self.assertIsNone(body['node_uuid'])
295
296 _, body = waiters.wait_for_allocation(self.client, uuid)
297 self.assertEqual('active', body['state'])
298 self.assertEqual(self.resource_class, body['resource_class'])
299 self.assertIsNone(body['last_error'])
300 self.assertEqual(self.node['uuid'], body['node_uuid'])
301
302 _, body2 = self.client.show_node_allocation(body['node_uuid'])
303 self.assertEqual(body, body2)
304
305 self.assertRaises(lib_exc.NotFound, self.client.show_allocation,
306 '%s.json' % body['uuid'])
307
308 @decorators.idempotent_id('cdf36c46-3da7-4306-b265-bcea3ab3bc3f')
309 def test_delete_allocation_with_json(self):
310 """Trying to delete an allocation while appending .json ext 404s"""
311 _, body = self.create_allocation(self.resource_class)
312 self.assertRaises(lib_exc.NotFound, self.client.delete_allocation,
313 '%s.json' % body['uuid'])
314
315
316 class TestBackfill(Base):
317 """Tests for backfilling baremetal allocations."""
318
319 min_microversion = '1.58'
320
321 @decorators.idempotent_id('10774c1d-6b79-453a-8e26-9bf04ab580a4')
322 def test_backfill_allocation(self):
323 self.deploy_node(self.node['uuid'])
324
325 _, body = self.client.create_allocation(self.resource_class,
326 node=self.node['uuid'])
327 uuid = body['uuid']
328 self.assertEqual(self.node['uuid'], body['node_uuid'])
329 self.assertEqual('active', body['state'])
330 self.assertIsNone(body['last_error'])
331
332 _, body2 = self.client.show_node_allocation(body['node_uuid'])
333 self.assertEqual(self.node['uuid'], body2['node_uuid'])
334 self.assertEqual('active', body2['state'])
335 self.assertIsNone(body2['last_error'])
336
337 _, node = self.client.show_node(self.node['uuid'])
338 self.assertEqual(uuid, node['allocation_uuid'])
339
340 @decorators.idempotent_id('c33d4b65-1232-4a3f-9aad-942e32f6f7b0')
341 def test_backfill_without_resource_class(self):
342 self.deploy_node(self.node['uuid'])
343
344 _, body = self.client.create_allocation(None, node=self.node['uuid'])
345 uuid = body['uuid']
346 self.assertEqual(self.node['uuid'], body['node_uuid'])
347 self.assertEqual('active', body['state'])
348 self.assertIsNone(body['last_error'])
349 # Resource class is copied from node
350 self.assertEqual(self.node['resource_class'], body['resource_class'])
351
352 _, body2 = self.client.show_node_allocation(body['node_uuid'])
353 self.assertEqual(self.node['uuid'], body2['node_uuid'])
354 self.assertEqual('active', body2['state'])
355 self.assertIsNone(body2['last_error'])
356 self.assertEqual(self.node['resource_class'], body2['resource_class'])
357
358 _, node = self.client.show_node(self.node['uuid'])
359 self.assertEqual(uuid, node['allocation_uuid'])