From 47a9a0f56db95265c15c74c4c8dc6a3998bfd2d3 Mon Sep 17 00:00:00 2001 From: Junchao-Mellanox <57339448+Junchao-Mellanox@users.noreply.github.com> Date: Tue, 14 Sep 2021 15:55:30 +0800 Subject: [PATCH] [portconfig] Validate duplicate speed value and interface type value (#1745) #### What I did Validate input parameter for command `config interface advertised-speeds` and `config interface advertised-types`, make sure there is no duplicate speeds or types #### How I did it Split input speeds/types with "," and check if there is a duplication, if yes, print an error. #### How to verify it 1. new unit test case 2. manual test --- scripts/portconfig | 12 ++++++++++-- tests/config_an_test.py | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/scripts/portconfig b/scripts/portconfig index bf730ea5c917..fde1d436d015 100755 --- a/scripts/portconfig +++ b/scripts/portconfig @@ -141,7 +141,11 @@ class portconfig(object): supported_speeds_str = self.get_supported_speeds(port) if supported_speeds_str: supported_speeds = set(supported_speeds_str.split(',')) - config_speeds = set(adv_speeds.split(',')) + config_speed_list = [x.strip() for x in adv_speeds.split(',')] + config_speeds = set(config_speed_list) + if len(config_speeds) < len(config_speed_list): + print('Invalid speed specified: {}. Please remove duplicate speed'.format(adv_speeds)) + exit(1) invalid_speeds = config_speeds - supported_speeds if invalid_speeds: print('Invalid speed specified: {}'.format(','.join(invalid_speeds))) @@ -164,7 +168,11 @@ class portconfig(object): print("Setting adv_interface_types %s on port %s" % (adv_interface_types, port)) if adv_interface_types != 'all': - config_interface_types = set(adv_interface_types.split(',')) + config_interface_type_list = [x.strip() for x in adv_interface_types.split(',')] + config_interface_types = set(config_interface_type_list) + if len(config_interface_types) < len(config_interface_type_list): + print("Invalid interface type specified: {}. Please remove duplicate interface type".format(adv_interface_types)) + exit(1) invalid_interface_types = config_interface_types - VALID_INTERFACE_TYPE_SET if invalid_interface_types: print("Invalid interface type specified: {}".format(','.join(invalid_interface_types))) diff --git a/tests/config_an_test.py b/tests/config_an_test.py index cfec47a5d1dc..8b5c1178df8b 100644 --- a/tests/config_an_test.py +++ b/tests/config_an_test.py @@ -50,6 +50,9 @@ def test_config_adv_speeds(self, ctx): result = self.basic_check("advertised-speeds", ["Ethernet0", "50000,100000"], ctx, operator.ne) assert 'Invalid speed' in result.output assert 'Valid speeds:' in result.output + result = self.basic_check("advertised-speeds", ["Ethernet0", "50000,50000"], ctx, operator.ne) + assert 'Invalid speed' in result.output + assert 'duplicate' in result.output def test_config_type(self, ctx): self.basic_check("type", ["Ethernet0", "CR4"], ctx) @@ -66,6 +69,9 @@ def test_config_adv_types(self, ctx): result = self.basic_check("advertised-types", ["Ethernet0", "CR4,Invalid"], ctx, operator.ne) assert 'Invalid interface type specified' in result.output assert 'Valid interface types:' in result.output + result = self.basic_check("advertised-types", ["Ethernet0", "CR4,CR4"], ctx, operator.ne) + assert 'Invalid interface type specified' in result.output + assert 'duplicate' in result.output self.basic_check("advertised-types", ["Ethernet0", ""], ctx, operator.ne) def basic_check(self, command_name, para_list, ctx, op=operator.eq, expect_result=0):