Debian Codegraph @

ironic_tempest_plugin/tests/api/admin/test_deploy_templates.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 import copy
14
15 from tempest.lib.common.utils import data_utils
16 from tempest.lib import decorators
17 from tempest.lib import exceptions as lib_exc
18
19 from ironic_tempest_plugin.tests.api.admin import api_microversion_fixture
20 from ironic_tempest_plugin.tests.api import base
21
22
23 EXAMPLE_STEPS = [{
24 'interface': 'bios',
25 'step': 'apply_configuration',
26 'args': {},
27 'priority': 10
28 }]
29
30
31 def _get_random_trait():
32 return data_utils.rand_name('CUSTOM', '').replace('-', '_')
33
34
35 class TestDeployTemplates(base.BaseBaremetalTest):
36 """Tests for deploy templates."""
37
38 min_microversion = '1.55'
39
40 def setUp(self):
41 super(TestDeployTemplates, self).setUp()
42 self.name = _get_random_trait()
43 self.steps = copy.deepcopy(EXAMPLE_STEPS)
44 _, self.template = self.create_deploy_template(self.name,
45 steps=self.steps)
46
47 @decorators.idempotent_id('8f39c794-4e6f-42fc-bfcb-e1f6eafff2e9')
48 def test_create_deploy_template_specifying_uuid(self):
49 name = _get_random_trait()
50 uuid = data_utils.rand_uuid()
51
52 _, template = self.create_deploy_template(name=name, steps=self.steps,
53 uuid=uuid)
54
55 _, body = self.client.show_deploy_template(uuid)
56 self._assertExpected(template, body)
57
58 @decorators.idempotent_id('7ac8fdd5-928b-4fbc-8341-3ebaad9def5e')
59 def test_delete_deploy_template(self):
60 self.delete_deploy_template(self.template['uuid'])
61
62 self.assertRaises(lib_exc.NotFound, self.client.show_deploy_template,
63 self.template['uuid'])
64
65 @decorators.idempotent_id('f424dd67-46c3-4169-b8f0-7e0c18b70437')
66 def test_show_deploy_template(self):
67 _, template = self.client.show_deploy_template(self.template['uuid'])
68 self._assertExpected(self.template, template)
69 self.assertEqual(self.name, template['name'])
70 self.assertEqual(self.steps, template['steps'])
71 self.assertIn('uuid', template)
72 self.assertEqual({}, template['extra'])
73
74 @decorators.idempotent_id('2fd98e9a-10ce-405a-a32c-0d6079766183')
75 def test_show_deploy_template_with_links(self):
76 _, template = self.client.show_deploy_template(self.template['uuid'])
77 self.assertIn('links', template)
78 self.assertEqual(2, len(template['links']))
79 self.assertIn(template['uuid'], template['links'][0]['href'])
80
81 @decorators.idempotent_id('cec2a01d-07af-4062-a8b0-9a1703f65bcf')
82 def test_list_deploy_templates(self):
83 _, body = self.client.list_deploy_templates()
84 self.assertIn(self.template['uuid'],
85 [i['uuid'] for i in body['deploy_templates']])
86 # Verify self links.
87 for template in body['deploy_templates']:
88 self.validate_self_link('deploy_templates', template['uuid'],
89 template['links'][0]['href'])
90
91 @decorators.idempotent_id('89aea2bf-c094-445f-b869-9fd56d1dfe5a')
92 def test_list_with_limit(self):
93 for i in range(2):
94 name = _get_random_trait()
95 self.create_deploy_template(name, steps=self.steps)
96
97 _, body = self.client.list_deploy_templates(limit=3)
98
99 next_marker = body['deploy_templates'][-1]['uuid']
100 self.assertIn(next_marker, body['next'])
101
102 @decorators.idempotent_id('c09c917e-c4d2-4148-9b6a-459bd126ed7c')
103 def test_list_deploy_templates_detail(self):
104 uuids = [
105 self.create_deploy_template(_get_random_trait(), steps=self.steps)
106 [1]['uuid'] for i in range(0, 5)]
107
108 _, body = self.client.list_deploy_templates(detail=True)
109
110 templates_dict = dict((template['uuid'], template)
111 for template in body['deploy_templates']
112 if template['uuid'] in uuids)
113
114 for uuid in uuids:
115 self.assertIn(uuid, templates_dict)
116 template = templates_dict[uuid]
117 self.assertIn('name', template)
118 self.assertEqual(self.steps, template['steps'])
119 self.assertIn('uuid', template)
120 self.assertEqual({}, template['extra'])
121 # Verify self link.
122 self.validate_self_link('deploy_templates', template['uuid'],
123 template['links'][0]['href'])
124
125 @decorators.idempotent_id('a6cf1ade-e19a-41e2-b151-13ecf0d8f08c')
126 def test_update_deploy_template_replace(self):
127 new_name = _get_random_trait()
128 new_steps = [{
129 'interface': 'raid',
130 'step': 'create_configuration',
131 'args': {},
132 'priority': 10,
133 }]
134
135 patch = [{'path': '/name', 'op': 'replace', 'value': new_name},
136 {'path': '/steps', 'op': 'replace', 'value': new_steps}]
137
138 self.client.update_deploy_template(self.template['uuid'], patch)
139
140 _, body = self.client.show_deploy_template(self.template['uuid'])
141 self.assertEqual(new_name, body['name'])
142 self.assertEqual(new_steps, body['steps'])
143
144 @decorators.idempotent_id('bb168c63-452b-4065-9a81-77853ca9540a')
145 def test_update_deploy_template_add(self):
146 new_steps = [
147 {
148 'interface': 'bios',
149 'step': 'cache_bios_settings',
150 'args': {},
151 'priority': 20
152 },
153 {
154 'interface': 'bios',
155 'step': 'factory_reset',
156 'args': {},
157 'priority': 30
158 },
159 ]
160
161 patch = [{'path': '/steps/1', 'op': 'add', 'value': new_steps[0]},
162 {'path': '/steps/2', 'op': 'add', 'value': new_steps[1]}]
163
164 self.client.update_deploy_template(self.template['uuid'], patch)
165
166 _, body = self.client.show_deploy_template(self.template['uuid'])
167 self.assertEqual(self.steps + new_steps, body['steps'])
168
169 @decorators.idempotent_id('2aa204a2-1d50-48fd-8b76-d2ed15586d50')
170 def test_update_deploy_template_mixed_ops(self):
171 new_name = _get_random_trait()
172 new_steps = [
173 {
174 'interface': 'bios',
175 'step': 'apply_configuration',
176 'args': {},
177 'priority': 20
178 },
179 {
180 'interface': 'bios',
181 'step': 'apply_configuration',
182 'args': {},
183 'priority': 30
184 },
185 ]
186
187 patch = [{'path': '/name', 'op': 'replace', 'value': new_name},
188 {'path': '/steps/0', 'op': 'replace', 'value': new_steps[0]},
189 {'path': '/steps/0', 'op': 'remove'},
190 {'path': '/steps/0', 'op': 'add', 'value': new_steps[1]}]
191
192 self.client.update_deploy_template(self.template['uuid'], patch)
193
194 _, body = self.client.show_deploy_template(self.template['uuid'])
195 self.assertEqual(new_name, body['name'])
196 self.assertEqual([new_steps[1]], body['steps'])
197
198
199 class TestDeployTemplatesWithJsonExtSupport(base.BaseBaremetalTest):
200 """Tests for deploy templates to validate appending .json extension to a
201
202 template's `name` or `uuid` in API versions 1.90 and prior works.
203 """
204
205 max_microversion = '1.90' # Max API version allowed for testing: 1.90
206 min_microversion = '1.90' # Min API version allowed for testing: 1.90
207
208 def setUp(self):
209 super(TestDeployTemplatesWithJsonExtSupport, self).setUp()
210 self.name = _get_random_trait()
211 self.steps = copy.deepcopy(EXAMPLE_STEPS)
212 _, self.template = self.create_deploy_template(self.name,
213 steps=self.steps)
214
215 @decorators.idempotent_id('df2fabbd-3c8b-4979-8f13-c58c96122adb')
216 def test_delete_deploy_template_with_json(self):
217 """Delete a template while appending .json extension to its uuid"""
218 self.delete_deploy_template('%s.json' % self.template['uuid'])
219
220 self.assertRaises(lib_exc.NotFound, self.client.show_deploy_template,
221 self.template['uuid'])
222
223 @decorators.idempotent_id('8b64a54d-ead7-4b3f-8fae-a1b8bdefe3ee')
224 def test_show_deploy_template_with_json(self):
225 """Show a template while appending .json extension to its uuid"""
226 _, template = self.client.show_deploy_template('%s.json' %
227 self.template['uuid'])
228 self._assertExpected(self.template, template)
229 self.assertEqual(self.name, template['name'])
230 self.assertEqual(self.steps, template['steps'])
231 self.assertIn('uuid', template)
232 self.assertEqual({}, template['extra'])
233
234
235 class TestDeployTemplatesWithoutJsonExtSupport(base.BaseBaremetalTest):
236 """Tests for deploy templates to validate appending .json extension to a
237
238 template's `name` or `uuid` in API versions later than 1.90 is disabled.
239 """
240
241 min_microversion = '1.91' # Min API version allowed for testing: 1.91
242
243 def setUp(self):
244 super(TestDeployTemplatesWithoutJsonExtSupport, self).setUp()
245 self.name = _get_random_trait()
246 self.steps = copy.deepcopy(EXAMPLE_STEPS)
247 _, self.template = self.create_deploy_template(self.name,
248 steps=self.steps)
249
250 @decorators.idempotent_id('61d05189-9f84-4973-835b-a860b655dfe3')
251 def test_delete_deploy_template_with_json(self):
252 """Trying to delete a template while appending .json ext 404s"""
253 self.assertRaises(lib_exc.NotFound, self.client.delete_deploy_template,
254 '%s.json' % self.template['uuid'])
255
256 @decorators.idempotent_id('432429dd-b964-4133-aa46-cd43a7cd9c37')
257 def test_show_deploy_template_with_json(self):
258 """Trying to show a template while appending .json ext 404s"""
259 self.assertRaises(lib_exc.NotFound, self.client.show_deploy_template,
260 '%s.json' % self.template['uuid'])
261
262
263 class TestDeployTemplatesOldAPI(base.BaseBaremetalTest):
264 """Negative tests for deploy templates using an old API version."""
265
266 @decorators.attr(type=['negative'])
267 @decorators.idempotent_id('e9481a0d-23e0-4757-bc11-c3c9ab9d3839')
268 def test_create_deploy_template_old_api(self):
269 # With deploy templates support, ironic returns 404. Without, 405.
270 self.assertRaises((lib_exc.NotFound, lib_exc.UnexpectedResponseCode),
271 self.create_deploy_template,
272 name=_get_random_trait(), steps=EXAMPLE_STEPS)
273
274 @decorators.attr(type=['negative'])
275 @decorators.idempotent_id('0d3af2aa-ba53-4c8a-92d4-91f9b4179fe7')
276 def test_update_deploy_template_old_api(self):
277 patch = [{'path': '/name', 'op': 'replace',
278 'value': _get_random_trait()}]
279
280 # With deploy templates support, ironic returns 404. Without, 405.
281 self.assertRaises((lib_exc.NotFound, lib_exc.UnexpectedResponseCode),
282 self.client.update_deploy_template,
283 _get_random_trait(), patch)
284
285 @decorators.attr(type=['negative'])
286 @decorators.idempotent_id('1646b1e5-ab81-45a8-9ea0-30444a4dcaa2')
287 def test_delete_deploy_template_old_api(self):
288 # With deploy templates support, ironic returns 404. Without, 405.
289 self.assertRaises((lib_exc.NotFound, lib_exc.UnexpectedResponseCode),
290 self.client.delete_deploy_template,
291 _get_random_trait())
292
293 @decorators.attr(type=['negative'])
294 @decorators.idempotent_id('819480ac-f36a-4402-b1d5-504d7cf55b1f')
295 def test_list_deploy_templates_old_api(self):
296 self.assertRaises(lib_exc.NotFound,
297 self.client.list_deploy_templates)
298
299
300 class TestDeployTemplatesNegative(base.BaseBaremetalTest):
301 """Negative tests for deploy templates."""
302
303 min_microversion = '1.55'
304
305 def setUp(self):
306 super(TestDeployTemplatesNegative, self).setUp()
307 self.useFixture(
308 api_microversion_fixture.APIMicroversionFixture(
309 self.min_microversion)
310 )
311 self.steps = EXAMPLE_STEPS
312
313 @decorators.attr(type=['negative'])
314 @decorators.idempotent_id('a4085c08-e718-4c2f-a796-0e115b659243')
315 def test_create_deploy_template_invalid_name(self):
316 name = 'invalid-name'
317 self.assertRaises(lib_exc.BadRequest,
318 self.create_deploy_template, name=name,
319 steps=self.steps)
320
321 @decorators.attr(type=['negative'])
322 @decorators.idempotent_id('6390acc4-9490-4b23-8b4c-41888a78c9b7')
323 def test_create_deploy_template_duplicated_deploy_template_name(self):
324 name = _get_random_trait()
325 self.create_deploy_template(name=name, steps=self.steps)
326 self.assertRaises(lib_exc.Conflict, self.create_deploy_template,
327 name=name, steps=self.steps)
328
329 @decorators.attr(type=['negative'])
330 @decorators.idempotent_id('ed3f0cec-13e8-4175-9fdb-d129e7b7fe10')
331 def test_create_deploy_template_no_mandatory_field_name(self):
332 self.assertRaises(lib_exc.BadRequest, self.create_deploy_template,
333 name=None, steps=self.steps)
334
335 @decorators.attr(type=['negative'])
336 @decorators.idempotent_id('af5dd0df-d903-463f-9535-9e4e9d6fd576')
337 def test_create_deploy_template_no_mandatory_field_steps(self):
338 self.assertRaises(lib_exc.BadRequest, self.create_deploy_template,
339 name=_get_random_trait())
340
341 @decorators.attr(type=['negative'])
342 @decorators.idempotent_id('cbd33bc5-7602-40b7-943e-3e92217567a3')
343 def test_create_deploy_template_malformed_steps(self):
344 steps = {'key': 'value'}
345 self.assertRaises(lib_exc.BadRequest, self.create_deploy_template,
346 name=_get_random_trait(), steps=steps)
347
348 @decorators.attr(type=['negative'])
349 @decorators.idempotent_id('2a562fca-f377-4a6e-b332-37ee82d3a983')
350 def test_create_deploy_template_malformed_deploy_template_uuid(self):
351 uuid = 'malformed:uuid'
352 self.assertRaises(lib_exc.BadRequest, self.create_deploy_template,
353 name=_get_random_trait(), steps=self.steps,
354 uuid=uuid)
355
356 @decorators.attr(type=['negative'])
357 @decorators.idempotent_id('2c006994-88ca-43b7-b605-897d479229d9')
358 def test_show_deploy_template_nonexistent(self):
359 self.assertRaises(lib_exc.NotFound, self.client.show_deploy_template,
360 data_utils.rand_uuid())
361
362 @decorators.attr(type=['negative'])
363 @decorators.idempotent_id('5a815f37-f015-4d68-9b22-099504f74805')
364 def test_update_deploy_template_remove_mandatory_field_steps(self):
365 name = _get_random_trait()
366 _, template = self.create_deploy_template(name=name, steps=self.steps)
367
368 # Removing one item from the collection
369 self.assertRaises(lib_exc.BadRequest,
370 self.client.update_deploy_template,
371 template['uuid'],
372 [{'path': '/steps/0', 'op': 'remove'}])
373
374 # Removing the collection
375 self.assertRaises(lib_exc.BadRequest,
376 self.client.update_deploy_template,
377 template['uuid'],
378 [{'path': '/steps', 'op': 'remove'}])
379
380 @decorators.attr(type=['negative'])
381 @decorators.idempotent_id('ee852ebb-a601-4593-9d59-063fcbc8f964')
382 def test_update_deploy_template_remove_mandatory_field_name(self):
383 name = _get_random_trait()
384 _, template = self.create_deploy_template(name=name, steps=self.steps)
385 self.assertRaises(lib_exc.BadRequest,
386 self.client.update_deploy_template,
387 template['uuid'],
388 [{'path': '/name', 'op': 'remove'}])
389
390 @decorators.attr(type=['negative'])
391 @decorators.idempotent_id('e59bf38d-272f-4490-b21e-9db217f11378')
392 def test_update_deploy_template_replace_empty_name(self):
393 name = _get_random_trait()
394 _, template = self.create_deploy_template(name=name, steps=self.steps)
395 self.assertRaises(lib_exc.BadRequest,
396 self.client.update_deploy_template,
397 template['uuid'],
398 [{'path': '/name', 'op': 'replace', 'value': ''}])