Skip to content

Commit 0ba77e6

Browse files
committed
[Neutron] Fix the "port show" command for trunk details
In [1], the "port list --long" command received a new column, showing the trunk subports related to a parent port. The problem of this patch is that the ``_formatters`` definition, that is shared with the "port show" command too, is changed. The "trunk_details" information presented in both commands differ: * In the "port list" command, only the subports are presented, in order to print a list of dictionaries without showing the ``trunk_id``. * In the "port show" command, it is presented all the trunk information, including the ``trunk_id``. This patch includes functional tests to validate the fix. [1]https://review.opendev.org/c/openstack/python-openstackclient/+/926611 Closes-Bug: #2098950 Change-Id: Ib1107fb3dbb025b39a7c55f90f5fe51ae433a72f
1 parent 3412147 commit 0ba77e6

3 files changed

Lines changed: 83 additions & 3 deletions

File tree

openstackclient/network/v2/port.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ def machine_readable(self):
6565
'fixed_ips': format_columns.ListDictColumn,
6666
'security_group_ids': format_columns.ListColumn,
6767
'tags': format_columns.ListColumn,
68-
'trunk_details': SubPortColumn,
6968
}
69+
_list_formatters = copy.deepcopy(_formatters)
70+
_list_formatters.update({'trunk_details': SubPortColumn})
7071

7172

7273
def _get_columns(item):
@@ -952,7 +953,7 @@ def take_action(self, parsed_args):
952953
utils.get_item_properties(
953954
s,
954955
attrs,
955-
formatters=_formatters,
956+
formatters=_list_formatters,
956957
)
957958
for s in data
958959
),

openstackclient/tests/functional/network/v2/test_port.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,82 @@ def _create_resource_for_tag_test(self, name, args):
289289
f'{self.base_command} create --network {self.NETWORK_NAME} {args} {name}',
290290
parse_output=True,
291291
)
292+
293+
def _trunk_creation(self):
294+
pport = uuid.uuid4().hex
295+
sport1 = uuid.uuid4().hex
296+
sport2 = uuid.uuid4().hex
297+
trunk = uuid.uuid4().hex
298+
json_output = self.openstack(
299+
'port create ' f'--network {self.NETWORK_NAME} {pport}',
300+
parse_output=True,
301+
)
302+
pport_id = json_output.get('id')
303+
json_output = self.openstack(
304+
'port create ' f'--network {self.NETWORK_NAME} {sport1}',
305+
parse_output=True,
306+
)
307+
sport1_id = json_output.get('id')
308+
json_output = self.openstack(
309+
'port create ' f'--network {self.NETWORK_NAME} {sport2}',
310+
parse_output=True,
311+
)
312+
sport2_id = json_output.get('id')
313+
314+
self.openstack(
315+
f'network trunk create --parent-port {pport} {trunk}',
316+
)
317+
self.openstack(
318+
f'network trunk set --subport port={sport1},'
319+
f'segmentation-type=vlan,segmentation-id=100 {trunk}',
320+
)
321+
self.openstack(
322+
f'network trunk set --subport port={sport2},'
323+
f'segmentation-type=vlan,segmentation-id=101 {trunk}',
324+
)
325+
326+
# NOTE(ralonsoh): keep this order to first delete the trunk and then
327+
# the ports.
328+
self.addCleanup(self.openstack, f'port delete {pport_id}')
329+
self.addCleanup(self.openstack, f'port delete {sport1_id}')
330+
self.addCleanup(self.openstack, f'port delete {sport2_id}')
331+
self.addCleanup(self.openstack, f'network trunk delete {trunk}')
332+
333+
return pport_id, sport1_id, sport2_id
334+
335+
def check_subports(self, subports, pport_id, sport1_id, sport2_id):
336+
self.assertEqual(2, len(subports))
337+
for subport in subports:
338+
if subport['port_id'] == sport1_id:
339+
self.assertEqual(100, subport['segmentation_id'])
340+
elif subport['port_id'] == sport2_id:
341+
self.assertEqual(101, subport['segmentation_id'])
342+
else:
343+
self.fail(
344+
f'Port {pport_id} does not have subport '
345+
f'{subport["port_id"]}'
346+
)
347+
self.assertEqual('vlan', subport['segmentation_type'])
348+
349+
def test_port_list_with_trunk(self):
350+
pport_id, sport1_id, sport2_id = self._trunk_creation()
351+
352+
# List all ports with "--long" flag to retrieve the trunk details
353+
json_output = self.openstack(
354+
'port list --long',
355+
parse_output=True,
356+
)
357+
port = next(port for port in json_output if port['ID'] == pport_id)
358+
subports = port['Trunk subports']
359+
self.check_subports(subports, pport_id, sport1_id, sport2_id)
360+
361+
def test_port_show_with_trunk(self):
362+
pport_id, sport1_id, sport2_id = self._trunk_creation()
363+
364+
# List all ports with "--long" flag to retrieve the trunk details
365+
port = self.openstack(
366+
f'port show {pport_id}',
367+
parse_output=True,
368+
)
369+
subports = port['trunk_details']['sub_ports']
370+
self.check_subports(subports, pport_id, sport1_id, sport2_id)

openstackclient/tests/unit/network/v2/test_port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _get_common_cols_data(fake_port):
122122
fake_port.status,
123123
format_columns.ListColumn(fake_port.tags),
124124
fake_port.trusted,
125-
port.SubPortColumn(fake_port.trunk_details),
125+
fake_port.trunk_details,
126126
fake_port.updated_at,
127127
)
128128

0 commit comments

Comments
 (0)