Skip to content

Commit a50b7a2

Browse files
authored
[ecnconfig] Allow ecn unit test to run without sudo (#1390)
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'
1 parent 8a1109e commit a50b7a2

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

scripts/ecnconfig

+8-6
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ OFF = "[]"
8989

9090
lossless_queues = ['3', '4']
9191

92+
def chk_exec_privilege():
93+
if os.geteuid() != 0 and os.environ.get("UTILITIES_UNIT_TESTING", "0") != "2":
94+
sys.exit("Root privileges required for this operation")
95+
9296
class EcnConfig(object):
9397
"""
9498
Process ecnconfig
@@ -166,8 +170,7 @@ class EcnConfig(object):
166170
return result
167171

168172
def set_wred_threshold(self, profile, threshold, value):
169-
if os.geteuid() != 0:
170-
sys.exit("Root privileges required for this operation")
173+
chk_exec_privilege()
171174

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

181184
def set_wred_prob(self, profile, drop_color, value):
182-
if os.geteuid() != 0:
183-
sys.exit("Root privileges required for this operation")
185+
chk_exec_privilege()
184186

185187
field = WRED_CONFIG_FIELDS[drop_color]
186188
if self.verbose:
@@ -227,8 +229,8 @@ class EcnQ(object):
227229
)
228230

229231
def set(self, enable):
230-
if os.geteuid() != 0:
231-
sys.exit("Root privileges required for this operation")
232+
chk_exec_privilege()
233+
232234
for queue in self.queues:
233235
if self.verbose:
234236
print("%s ECN on %s queue %s" % ("Enable" if enable else "Disable", ','.join(self.ports_key), queue))

0 commit comments

Comments
 (0)