Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[portconfig] Validate duplicate speed value and interface type value #1745

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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