From a50b7a2f07dc0dfac945dc92bd3f4ee0f3a587ea Mon Sep 17 00:00:00 2001 From: Neetha John Date: Fri, 29 Jan 2021 14:52:02 -0800 Subject: [PATCH] [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' --- scripts/ecnconfig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/scripts/ecnconfig b/scripts/ecnconfig index cdebf21e799a..62a14b9106f3 100755 --- a/scripts/ecnconfig +++ b/scripts/ecnconfig @@ -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 @@ -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: @@ -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: @@ -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))