Skip to content

Commit

Permalink
[ecnconfig] Allow ecn unit test to run without sudo (sonic-net#1390)
Browse files Browse the repository at this point in the history
Allow ecn unit tests to run without root privileges

**- How I did it**
Included the UTILITIES_UNIT_TESTING' env variable also as one of the conditions to determine if the command needs root privileges for execution

**- How to verify it**
Ran utilities test using the command "python3 setup.py test" and ecn_test.py passed. Prior to the fix, most of the testcases were failing with the error 'Root privileged required for this operation'
  • Loading branch information
neethajohn authored Jan 29, 2021
1 parent 8a1109e commit a50b7a2
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions scripts/ecnconfig
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ OFF = "[]"

lossless_queues = ['3', '4']

def chk_exec_privilege():
if os.geteuid() != 0 and os.environ.get("UTILITIES_UNIT_TESTING", "0") != "2":
sys.exit("Root privileges required for this operation")

class EcnConfig(object):
"""
Process ecnconfig
Expand Down Expand Up @@ -166,8 +170,7 @@ class EcnConfig(object):
return result

def set_wred_threshold(self, profile, threshold, value):
if os.geteuid() != 0:
sys.exit("Root privileges required for this operation")
chk_exec_privilege()

field = WRED_CONFIG_FIELDS[threshold]
if self.verbose:
Expand All @@ -179,8 +182,7 @@ class EcnConfig(object):
json.dump(prof_table, fd)

def set_wred_prob(self, profile, drop_color, value):
if os.geteuid() != 0:
sys.exit("Root privileges required for this operation")
chk_exec_privilege()

field = WRED_CONFIG_FIELDS[drop_color]
if self.verbose:
Expand Down Expand Up @@ -227,8 +229,8 @@ class EcnQ(object):
)

def set(self, enable):
if os.geteuid() != 0:
sys.exit("Root privileges required for this operation")
chk_exec_privilege()

for queue in self.queues:
if self.verbose:
print("%s ECN on %s queue %s" % ("Enable" if enable else "Disable", ','.join(self.ports_key), queue))
Expand Down

0 comments on commit a50b7a2

Please sign in to comment.