Debian Codegraph @

ironic_tempest_plugin/tests/api/admin/test_ports.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 tempest.lib.common.utils import data_utils
14 from tempest.lib import decorators
15 from tempest.lib import exceptions as lib_exc
16
17 from ironic_tempest_plugin.tests.api.admin import api_microversion_fixture
18 from ironic_tempest_plugin.tests.api import base
19
20
21 class TestPorts(base.BaseBaremetalTest):
22 """Tests for ports."""
23
24 def setUp(self):
25 super(TestPorts, self).setUp()
26
27 _, self.chassis = self.create_chassis()
28 # NOTE(TheJulia): noop is required for the network interface for
29 # these tests, as newer versions of ironic can invoke network
30 # logic, and if is not configured with full integration configuration,
31 # can then result in failed tests.
32 _, self.node = self.create_node(self.chassis['uuid'])
33 _, self.port = self.create_port(self.node['uuid'],
34 data_utils.rand_mac_address())
35 self.useFixture(
36 api_microversion_fixture.APIMicroversionFixture('1.31'))
37 # Now with a 1.31 microversion, swap the network interfaces
38 # into place so the test doesn't break depending on
39 # the environment's default state.
40 self.client.update_node(self.node['uuid'],
41 [{'path': '/network_interface',
42 'op': 'replace',
43 'value': 'noop'}])
44 self.useFixture(api_microversion_fixture.APIMicroversionFixture('1.1'))
45
46 @decorators.idempotent_id('83975898-2e50-42ed-b5f0-e510e36a0b56')
47 def test_create_port(self):
48 node_id = self.node['uuid']
49 address = data_utils.rand_mac_address()
50
51 _, port = self.create_port(node_id=node_id, address=address)
52
53 _, body = self.client.show_port(port['uuid'])
54
55 self._assertExpected(port, body)
56
57 @decorators.idempotent_id('d1f6b249-4cf6-4fe6-9ed6-a6e84b1bf67b')
58 def test_create_port_specifying_uuid(self):
59 node_id = self.node['uuid']
60 address = data_utils.rand_mac_address()
61 uuid = data_utils.rand_uuid()
62
63 _, port = self.create_port(node_id=node_id,
64 address=address, uuid=uuid)
65
66 _, body = self.client.show_port(uuid)
67 self._assertExpected(port, body)
68
69 @decorators.idempotent_id('4a02c4b0-6573-42a4-a513-2e36ad485b62')
70 def test_create_port_with_extra(self):
71 node_id = self.node['uuid']
72 address = data_utils.rand_mac_address()
73 extra = {'str': 'value', 'int': 123, 'float': 0.123,
74 'bool': True, 'list': [1, 2, 3], 'dict': {'foo': 'bar'}}
75
76 _, port = self.create_port(node_id=node_id, address=address,
77 extra=extra)
78
79 _, body = self.client.show_port(port['uuid'])
80 self._assertExpected(port, body)
81
82 @decorators.idempotent_id('1bf257a9-aea3-494e-89c0-63f657ab4fdd')
83 def test_delete_port(self):
84 node_id = self.node['uuid']
85 address = data_utils.rand_mac_address()
86 _, port = self.create_port(node_id=node_id, address=address)
87
88 self.delete_port(port['uuid'])
89
90 self.assertRaises(lib_exc.NotFound, self.client.show_port,
91 port['uuid'])
92
93 @decorators.idempotent_id('9fa77ab5-ce59-4f05-baac-148904ba1597')
94 def test_show_port(self):
95 _, port = self.client.show_port(self.port['uuid'])
96 self._assertExpected(self.port, port)
97
98 @decorators.idempotent_id('7c1114ff-fc3f-47bb-bc2f-68f61620ba8b')
99 def test_show_port_by_address(self):
100 _, port = self.client.show_port_by_address(self.port['address'])
101 self._assertExpected(self.port, port['ports'][0])
102
103 @decorators.idempotent_id('bd773405-aea5-465d-b576-0ab1780069e5')
104 def test_show_port_with_links(self):
105 _, port = self.client.show_port(self.port['uuid'])
106 self.assertIn('links', port.keys())
107 self.assertEqual(2, len(port['links']))
108 self.assertIn(port['uuid'], port['links'][0]['href'])
109
110 @decorators.idempotent_id('b5e91854-5cd7-4a8e-bb35-3e0a1314606d')
111 def test_list_ports(self):
112 _, body = self.client.list_ports()
113 self.assertIn(self.port['uuid'],
114 [i['uuid'] for i in body['ports']])
115 # Verify self links.
116 for port in body['ports']:
117 self.validate_self_link('ports', port['uuid'],
118 port['links'][0]['href'])
119
120 @decorators.idempotent_id('324a910e-2f80-4258-9087-062b5ae06240')
121 def test_list_with_limit(self):
122 for i in range(2):
123 self.create_port(self.node['uuid'], data_utils.rand_mac_address())
124
125 _, body = self.client.list_ports(limit=3)
126
127 next_marker = body['ports'][-1]['uuid']
128 self.assertIn(next_marker, body['next'])
129
130 @decorators.idempotent_id('8a94b50f-9895-4a63-a574-7ecff86e5875')
131 def test_list_ports_details(self):
132 node_id = self.node['uuid']
133
134 uuids = [
135 self.create_port(node_id=node_id,
136 address=data_utils.rand_mac_address())
137 [1]['uuid'] for i in range(0, 5)]
138
139 _, body = self.client.list_ports_detail()
140
141 ports_dict = dict((port['uuid'], port) for port in body['ports']
142 if port['uuid'] in uuids)
143
144 for uuid in uuids:
145 self.assertIn(uuid, ports_dict)
146 port = ports_dict[uuid]
147 self.assertIn('extra', port)
148 self.assertIn('node_uuid', port)
149 # never expose the node_id
150 self.assertNotIn('node_id', port)
151 # Verify self link.
152 self.validate_self_link('ports', port['uuid'],
153 port['links'][0]['href'])
154
155 @decorators.idempotent_id('8a03f688-7d75-4ecd-8cbc-e06b8f346738')
156 def test_list_ports_details_with_address(self):
157 node_id = self.node['uuid']
158 address = data_utils.rand_mac_address()
159 self.create_port(node_id=node_id, address=address)
160 for i in range(0, 5):
161 self.create_port(node_id=node_id,
162 address=data_utils.rand_mac_address())
163
164 _, body = self.client.list_ports_detail(address=address)
165 self.assertEqual(1, len(body['ports']))
166 self.assertEqual(address, body['ports'][0]['address'])
167
168 @decorators.idempotent_id('9c26298b-1bcb-47b7-9b9e-8bdd6e3c4aba')
169 def test_update_port_replace(self):
170 node_id = self.node['uuid']
171 address = data_utils.rand_mac_address()
172 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
173
174 _, port = self.create_port(node_id=node_id, address=address,
175 extra=extra)
176
177 new_address = data_utils.rand_mac_address()
178 new_extra = {'key1': 'new-value1', 'key2': 'new-value2',
179 'key3': 'new-value3'}
180
181 patch = [{'path': '/address',
182 'op': 'replace',
183 'value': new_address},
184 {'path': '/extra/key1',
185 'op': 'replace',
186 'value': new_extra['key1']},
187 {'path': '/extra/key2',
188 'op': 'replace',
189 'value': new_extra['key2']},
190 {'path': '/extra/key3',
191 'op': 'replace',
192 'value': new_extra['key3']}]
193
194 self.client.update_port(port['uuid'], patch)
195
196 _, body = self.client.show_port(port['uuid'])
197 self.assertEqual(new_address, body['address'])
198 self.assertEqual(new_extra, body['extra'])
199
200 @decorators.idempotent_id('d7e7fece-6ed9-460a-9ebe-9267217e8580')
201 def test_update_port_remove(self):
202 node_id = self.node['uuid']
203 address = data_utils.rand_mac_address()
204 extra = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
205
206 _, port = self.create_port(node_id=node_id, address=address,
207 extra=extra)
208
209 # Removing one item from the collection
210 self.client.update_port(port['uuid'],
211 [{'path': '/extra/key2',
212 'op': 'remove'}])
213 extra.pop('key2')
214 _, body = self.client.show_port(port['uuid'])
215 self.assertEqual(extra, body['extra'])
216
217 # Removing the collection
218 self.client.update_port(port['uuid'], [{'path': '/extra',
219 'op': 'remove'}])
220 _, body = self.client.show_port(port['uuid'])
221 self.assertEqual({}, body['extra'])
222
223 # Assert nothing else was changed
224 self.assertEqual(node_id, body['node_uuid'])
225 self.assertEqual(address, body['address'])
226
227 @decorators.idempotent_id('241288b3-e98a-400f-a4d7-d1f716146361')
228 def test_update_port_add(self):
229 node_id = self.node['uuid']
230 address = data_utils.rand_mac_address()
231
232 _, port = self.create_port(node_id=node_id, address=address)
233
234 extra = {'key1': 'value1', 'key2': 'value2'}
235
236 patch = [{'path': '/extra/key1',
237 'op': 'add',
238 'value': extra['key1']},
239 {'path': '/extra/key2',
240 'op': 'add',
241 'value': extra['key2']}]
242
243 self.client.update_port(port['uuid'], patch)
244
245 _, body = self.client.show_port(port['uuid'])
246 self.assertEqual(extra, body['extra'])
247
248 @decorators.idempotent_id('5309e897-0799-4649-a982-0179b04c3876')
249 def test_update_port_mixed_ops(self):
250 node_id = self.node['uuid']
251 address = data_utils.rand_mac_address()
252 extra = {'key1': 'value1', 'key2': 'value2'}
253
254 _, port = self.create_port(node_id=node_id, address=address,
255 extra=extra)
256
257 new_address = data_utils.rand_mac_address()
258 new_extra = {'key1': 0.123, 'key3': {'cat': 'meow'}}
259
260 patch = [{'path': '/address',
261 'op': 'replace',
262 'value': new_address},
263 {'path': '/extra/key1',
264 'op': 'replace',
265 'value': new_extra['key1']},
266 {'path': '/extra/key2',
267 'op': 'remove'},
268 {'path': '/extra/key3',
269 'op': 'add',
270 'value': new_extra['key3']}]
271
272 self.client.update_port(port['uuid'], patch)
273
274 _, body = self.client.show_port(port['uuid'])
275 self.assertEqual(new_address, body['address'])
276 self.assertEqual(new_extra, body['extra'])
277
278
279 class TestPortsWithPhysicalNetwork(base.BaseBaremetalTest):
280 """Tests for ports with physical network information."""
281
282 min_microversion = '1.34'
283
284 def setUp(self):
285 super(TestPortsWithPhysicalNetwork, self).setUp()
286
287 self.useFixture(
288 api_microversion_fixture.APIMicroversionFixture(
289 TestPortsWithPhysicalNetwork.min_microversion)
290 )
291 _, self.chassis = self.create_chassis()
292 _, self.node = self.create_node(self.chassis['uuid'],
293 network_interface='noop')
294
295 @decorators.idempotent_id('f1a5d279-c456-4311-ad31-fea09f61c22b')
296 def test_create_port_with_physical_network(self):
297 node_id = self.node['uuid']
298 address = data_utils.rand_mac_address()
299
300 _, port = self.create_port(node_id=node_id, address=address,
301 physical_network='physnet1')
302
303 _, body = self.client.show_port(port['uuid'])
304
305 self._assertExpected(port, body)
306 self.assertEqual('physnet1', port['physical_network'])
307
308 @decorators.idempotent_id('9c26298b-1bcb-47b7-9b9e-8bdd6e3c4aba')
309 def test_update_port_replace_physical_network(self):
310 node_id = self.node['uuid']
311 address = data_utils.rand_mac_address()
312
313 _, port = self.create_port(node_id=node_id, address=address,
314 physical_network='physnet1')
315
316 new_physnet = 'physnet2'
317
318 patch = [{'path': '/physical_network',
319 'op': 'replace',
320 'value': new_physnet}]
321
322 self.client.update_port(port['uuid'], patch)
323
324 _, body = self.client.show_port(port['uuid'])
325 self.assertEqual(new_physnet, body['physical_network'])
326
327 @decorators.idempotent_id('6503309c-b2c7-4f59-b15a-0d92b5de9210')
328 def test_update_port_remove_physical_network(self):
329 node_id = self.node['uuid']
330 address = data_utils.rand_mac_address()
331
332 _, port = self.create_port(node_id=node_id, address=address,
333 physical_network='physnet1')
334
335 patch = [{'path': '/physical_network',
336 'op': 'remove'}]
337
338 self.client.update_port(port['uuid'], patch)
339
340 _, body = self.client.show_port(port['uuid'])
341 self.assertIsNone(body['physical_network'])
342
343 @decorators.idempotent_id('4155c24d-8474-4b53-a320-aee475f85a68')
344 def test_create_ports_in_portgroup_with_physical_network(self):
345 node_id = self.node['uuid']
346 address = data_utils.rand_mac_address()
347
348 _, portgroup = self.create_portgroup(node_id, address=address)
349
350 _, port1 = self.create_port(node_id=node_id, address=address,
351 portgroup_uuid=portgroup['uuid'],
352 physical_network='physnet1')
353
354 address = data_utils.rand_mac_address()
355 _, port2 = self.create_port(node_id=node_id, address=address,
356 portgroup_uuid=portgroup['uuid'],
357 physical_network='physnet1')
358
359 _, body = self.client.show_port(port1['uuid'])
360 self.assertEqual('physnet1', body['physical_network'])
361 self.assertEqual(portgroup['uuid'], body['portgroup_uuid'])
362
363 _, body = self.client.show_port(port2['uuid'])
364 self.assertEqual('physnet1', body['physical_network'])
365 self.assertEqual(portgroup['uuid'], body['portgroup_uuid'])
366
367 @decorators.idempotent_id('cf05a3ef-3bc4-4db7-bb4c-4eb871eb9f81')
368 def test_update_ports_in_portgroup_with_physical_network(self):
369 node_id = self.node['uuid']
370 address = data_utils.rand_mac_address()
371
372 _, portgroup = self.create_portgroup(node_id, address=address)
373
374 _, port1 = self.create_port(node_id=node_id, address=address,
375 portgroup_uuid=portgroup['uuid'],
376 physical_network='physnet1')
377
378 address = data_utils.rand_mac_address()
379 _, port2 = self.create_port(node_id=node_id, address=address,
380 physical_network='physnet1')
381
382 patch = [{'path': '/portgroup_uuid',
383 'op': 'replace',
384 'value': portgroup['uuid']}]
385
386 self.client.update_port(port2['uuid'], patch)
387
388 _, body = self.client.show_port(port1['uuid'])
389 self.assertEqual('physnet1', body['physical_network'])
390 self.assertEqual(portgroup['uuid'], body['portgroup_uuid'])
391
392 _, body = self.client.show_port(port2['uuid'])
393 self.assertEqual('physnet1', body['physical_network'])
394 self.assertEqual(portgroup['uuid'], body['portgroup_uuid'])