Debian Codegraph @

ironic_tempest_plugin/tests/api/admin/test_runbooks.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 'order': 1
28 }]
29
30
31 def _get_random_trait():
32 return data_utils.rand_name('CUSTOM', '').replace('-', '_')
33
34
35 class TestRunbooks(base.BaseBaremetalTest):
36 """Tests for runbooks."""
37
38 min_microversion = '1.92'
39
40 def setUp(self):
41 super(TestRunbooks, self).setUp()
42 self.name = _get_random_trait()
43 self.steps = copy.deepcopy(EXAMPLE_STEPS)
44 _, self.runbook = self.create_runbook(self.name,
45 steps=self.steps)
46
47 _, self.chassis = self.create_chassis()
48 _, self.test_node = self.create_node(self.chassis['uuid'])
49
50 @decorators.idempotent_id('5a0cfe27-b7e8-d4f0-d3c5-e6e1eef63241')
51 def test_create_runbook_specifying_uuid(self):
52 name = _get_random_trait()
53 uuid = data_utils.rand_uuid()
54
55 _, runbook = self.create_runbook(name=name, steps=self.steps,
56 uuid=uuid)
57
58 _, body = self.client.show_runbook(uuid)
59 self._assertExpected(runbook, body)
60
61 @decorators.idempotent_id('b8bfb388-97b0-aa6e-5130-1a1ed34f94db')
62 def test_delete_runbook(self):
63 self.delete_runbook(self.runbook['uuid'])
64 self.assertRaises(lib_exc.NotFound, self.client.show_runbook,
65 self.runbook['uuid'])
66
67 @decorators.idempotent_id('f1cba93a-2894-7296-0f4b-58867359480b')
68 def test_show_runbook(self):
69 _, runbook = self.client.show_runbook(self.runbook['uuid'])
70 self._assertExpected(self.runbook, runbook)
71 self.assertEqual(self.name, runbook['name'])
72 self.assertEqual(self.steps, runbook['steps'])
73 self.assertIn('uuid', runbook)
74 self.assertEqual({}, runbook['extra'])
75
76 @decorators.idempotent_id('c95e2631-24e2-914b-db00-c8dafc35a677')
77 def test_show_runbook_with_links(self):
78 _, runbook = self.client.show_runbook(self.runbook['uuid'])
79 self.assertIn('links', runbook)
80 self.assertEqual(2, len(runbook['links']))
81 self.assertIn(runbook['uuid'], runbook['links'][0]['href'])
82
83 @decorators.idempotent_id('7b7951b3-e177-21d7-933a-1f29891dea52')
84 def test_list_runbooks(self):
85 _, body = self.client.list_runbooks()
86 self.assertIn(self.runbook['uuid'],
87 [i['uuid'] for i in body['runbooks']])
88
89 for runbook in body['runbooks']:
90 self.validate_self_link('runbooks', runbook['uuid'],
91 runbook['links'][0]['href'])
92
93 @decorators.idempotent_id('6aafc619-0d98-5341-7d94-b293e194dcf7')
94 def test_list_with_limit(self):
95 for _ in range(2):
96 name = _get_random_trait()
97 self.create_runbook(name, steps=self.steps)
98
99 _, body = self.client.list_runbooks(limit=3)
100
101 next_marker = body['runbooks'][-1]['uuid']
102 self.assertIn(next_marker, body['next'])
103
104 @decorators.idempotent_id('ebd762c3-c10e-b71f-5efd-af14ac9f6092')
105 def test_list_runbooks_detail(self):
106 uuids = [
107 self.create_runbook(_get_random_trait(), steps=self.steps)
108 [1]['uuid'] for _ in range(0, 5)]
109
110 _, body = self.client.list_runbooks(detail=True)
111
112 runbooks_dict = dict((runbook['uuid'], runbook)
113 for runbook in body['runbooks']
114 if runbook['uuid'] in uuids)
115
116 for uuid in uuids:
117 self.assertIn(uuid, runbooks_dict)
118 runbook = runbooks_dict[uuid]
119 self.assertIn('name', runbook)
120 self.assertEqual(self.steps, runbook['steps'])
121 self.assertIn('uuid', runbook)
122 self.assertEqual({}, runbook['extra'])
123 self.validate_self_link('runbooks', runbook['uuid'],
124 runbook['links'][0]['href'])
125
126 @decorators.idempotent_id('fb192fdd-ea6a-c637-a36e-390c46a7663b')
127 def test_update_runbook_replace(self):
128 new_name = _get_random_trait()
129 new_steps = [{
130 'interface': 'raid',
131 'step': 'create_configuration',
132 'args': {},
133 'order': 2,
134 }]
135
136 patch = [{'path': '/name', 'op': 'replace', 'value': new_name},
137 {'path': '/steps', 'op': 'replace', 'value': new_steps}]
138
139 self.client.update_runbook(self.runbook['uuid'], patch)
140
141 _, body = self.client.show_runbook(self.runbook['uuid'])
142 self.assertEqual(new_name, body['name'])
143 self.assertEqual(new_steps, body['steps'])
144
145 @decorators.idempotent_id('99f52546-9906-6ded-9186-7262591b99ec')
146 def test_update_runbook_add(self):
147 new_steps = [
148 {
149 'interface': 'bios',
150 'step': 'cache_bios_settings',
151 'args': {},
152 'order': 2
153 },
154 {
155 'interface': 'bios',
156 'step': 'factory_reset',
157 'args': {},
158 'order': 3
159 },
160 ]
161
162 patch = [{'path': '/steps/1', 'op': 'add', 'value': new_steps[0]},
163 {'path': '/steps/2', 'op': 'add', 'value': new_steps[1]}]
164
165 self.client.update_runbook(self.runbook['uuid'], patch)
166
167 _, body = self.client.show_runbook(self.runbook['uuid'])
168 self.assertEqual(self.steps + new_steps, body['steps'])
169
170 @decorators.idempotent_id('ec1550c3-264e-fcce-b131-d2815fdb733b')
171 def test_update_runbook_mixed_ops(self):
172 new_name = _get_random_trait()
173 new_steps = [
174 {
175 'interface': 'bios',
176 'step': 'apply_configuration',
177 'args': {},
178 'order': 2
179 },
180 {
181 'interface': 'bios',
182 'step': 'apply_configuration',
183 'args': {},
184 'order': 3
185 },
186 ]
187
188 patch = [{'path': '/name', 'op': 'replace', 'value': new_name},
189 {'path': '/steps/0', 'op': 'replace', 'value': new_steps[0]},
190 {'path': '/steps/0', 'op': 'remove'},
191 {'path': '/steps/0', 'op': 'add', 'value': new_steps[1]}]
192
193 self.client.update_runbook(self.runbook['uuid'], patch)
194
195 _, body = self.client.show_runbook(self.runbook['uuid'])
196 self.assertEqual(new_name, body['name'])
197 self.assertEqual([new_steps[1]], body['steps'])
198
199 @decorators.idempotent_id('5c7f0aca-cee3-d083-ef2a-e33a8dc467c5')
200 def test_combining_runbook_and_explicit_steps(self):
201 explicit_steps = [{'interface': 'deploy', 'step': 'deploy',
202 'args': {}}]
203
204 self.assertRaises(lib_exc.BadRequest,
205 self.client.set_node_provision_state,
206 self.test_node['uuid'], 'active',
207 runbook=self.runbook['uuid'],
208 clean_steps=explicit_steps)
209
210 @decorators.idempotent_id('ddf4a9b7-144d-386a-fd3fcf8960d77199')
211 def test_create_runbook_with_invalid_step_format(self):
212 name = _get_random_trait()
213 invalid_steps = [{'invalid_key': 'value'}]
214
215 self.assertRaises(lib_exc.BadRequest, self.create_runbook,
216 name, steps=invalid_steps)
217
218
219 class TestRunbooksOldAPI(base.BaseBaremetalTest):
220 """Negative tests for runbooks using an old API version."""
221
222 @decorators.attr(type=['negative'])
223 @decorators.idempotent_id('e9481a0d-23e0-4757-bc11-c3c9ab9d3839')
224 def test_create_runbook_old_api(self):
225 # With runbooks support, ironic returns 404. Without, 405.
226 self.assertRaises((lib_exc.NotFound, lib_exc.UnexpectedResponseCode),
227 self.create_runbook,
228 name=_get_random_trait(), steps=EXAMPLE_STEPS)
229
230 @decorators.attr(type=['negative'])
231 @decorators.idempotent_id('0d3af2aa-ba53-4c8a-92d4-91f9b4179fe7')
232 def test_update_runbook_old_api(self):
233 patch = [{'path': '/name', 'op': 'replace',
234 'value': _get_random_trait()}]
235
236 # With runbooks support, ironic returns 404. Without, 405.
237 self.assertRaises((lib_exc.NotFound, lib_exc.UnexpectedResponseCode),
238 self.client.update_runbook,
239 _get_random_trait(), patch)
240
241 @decorators.attr(type=['negative'])
242 @decorators.idempotent_id('1646b1e5-ab81-45a8-9ea0-30444a4dcaa2')
243 def test_delete_runbook_old_api(self):
244 # With runbooks support, ironic returns 404. Without, 405.
245 self.assertRaises((lib_exc.NotFound, lib_exc.UnexpectedResponseCode),
246 self.client.delete_runbook,
247 _get_random_trait())
248
249 @decorators.attr(type=['negative'])
250 @decorators.idempotent_id('819480ac-f36a-4402-b1d5-504d7cf55b1f')
251 def test_list_runbooks_old_api(self):
252 self.assertRaises(lib_exc.NotFound,
253 self.client.list_runbooks)
254
255
256 class TestRunbooksNegative(base.BaseBaremetalTest):
257 """Negative tests for runbooks."""
258
259 min_microversion = '1.92'
260
261 def setUp(self):
262 super(TestRunbooksNegative, self).setUp()
263 self.useFixture(
264 api_microversion_fixture.APIMicroversionFixture(
265 self.min_microversion)
266 )
267 self.steps = EXAMPLE_STEPS
268
269 @decorators.attr(type=['negative'])
270 @decorators.idempotent_id('a4085c08-e718-4c2f-a796-0e115b659243')
271 def test_create_runbook_invalid_name(self):
272 name = 'invalid-name'
273 self.assertRaises(lib_exc.BadRequest,
274 self.create_runbook, name=name,
275 steps=self.steps)
276
277 @decorators.attr(type=['negative'])
278 @decorators.idempotent_id('6390acc4-9490-4b23-8b4c-41888a78c9b7')
279 def test_create_runbook_duplicated_name(self):
280 name = _get_random_trait()
281 self.create_runbook(name=name, steps=self.steps)
282 self.assertRaises(lib_exc.Conflict, self.create_runbook,
283 name=name, steps=self.steps)
284
285 @decorators.attr(type=['negative'])
286 @decorators.idempotent_id('ed3f0cec-13e8-4175-9fdb-d129e7b7fe10')
287 def test_create_runbook_no_mandatory_field_name(self):
288 self.assertRaises(lib_exc.BadRequest, self.create_runbook,
289 name=None, steps=self.steps)
290
291 @decorators.attr(type=['negative'])
292 @decorators.idempotent_id('af5dd0df-d903-463f-9535-9e4e9d6fd576')
293 def test_create_runbook_no_mandatory_field_steps(self):
294 self.assertRaises(lib_exc.BadRequest, self.create_runbook,
295 name=_get_random_trait())
296
297 @decorators.attr(type=['negative'])
298 @decorators.idempotent_id('cbd33bc5-7602-40b7-943e-3e92217567a3')
299 def test_create_runbook_malformed_steps(self):
300 steps = {'key': 'value'}
301 self.assertRaises(lib_exc.BadRequest, self.create_runbook,
302 name=_get_random_trait(), steps=steps)
303
304 @decorators.attr(type=['negative'])
305 @decorators.idempotent_id('2a562fca-f377-4a6e-b332-37ee82d3a983')
306 def test_create_runbook_malformed_runbook_uuid(self):
307 uuid = 'malformed:uuid'
308 self.assertRaises(lib_exc.BadRequest, self.create_runbook,
309 name=_get_random_trait(), steps=self.steps,
310 uuid=uuid)
311
312 @decorators.attr(type=['negative'])
313 @decorators.idempotent_id('2c006994-88ca-43b7-b605-897d479229d9')
314 def test_show_runbook_nonexistent(self):
315 self.assertRaises(lib_exc.NotFound, self.client.show_runbook,
316 data_utils.rand_uuid())
317
318 @decorators.attr(type=['negative'])
319 @decorators.idempotent_id('5a815f37-f015-4d68-9b22-099504f74805')
320 def test_update_runbook_remove_mandatory_field_steps(self):
321 name = _get_random_trait()
322 _, runbook = self.create_runbook(name=name, steps=self.steps)
323
324 self.assertRaises(lib_exc.BadRequest,
325 self.client.update_runbook,
326 runbook['uuid'],
327 [{'path': '/steps/0', 'op': 'remove'}])
328
329 self.assertRaises(lib_exc.BadRequest,
330 self.client.update_runbook,
331 runbook['uuid'],
332 [{'path': '/steps', 'op': 'remove'}])
333
334 @decorators.attr(type=['negative'])
335 @decorators.idempotent_id('ee852ebb-a601-4593-9d59-063fcbc8f964')
336 def test_update_runbook_remove_mandatory_field_name(self):
337 name = _get_random_trait()
338 _, runbook = self.create_runbook(name=name, steps=self.steps)
339 self.assertRaises(lib_exc.BadRequest,
340 self.client.update_runbook,
341 runbook['uuid'],
342 [{'path': '/name', 'op': 'remove'}])
343
344 @decorators.attr(type=['negative'])
345 @decorators.idempotent_id('e59bf38d-272f-4490-b21e-9db217f11378')
346 def test_update_runbook_replace_empty_name(self):
347 name = _get_random_trait()
348 _, runbook = self.create_runbook(name=name, steps=self.steps)
349 self.assertRaises(lib_exc.BadRequest,
350 self.client.update_runbook,
351 runbook['uuid'],
352 [{'path': '/name', 'op': 'replace', 'value': ''}])