Skip to content

Commit

Permalink
[portconfig] Validate duplicate speed value and interface type value (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
Junchao-Mellanox authored Sep 14, 2021
1 parent f1086ee commit 47a9a0f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions scripts/portconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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)))
Expand Down
6 changes: 6 additions & 0 deletions tests/config_an_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 47a9a0f

Please sign in to comment.