From 150642e1130654c6b543924de34a2e83f23782f0 Mon Sep 17 00:00:00 2001 From: Danny Allen Date: Tue, 22 Dec 2020 09:56:24 -0800 Subject: [PATCH] [dropcounters] Fix clear for non-root users (#1253) Signed-off-by: Danny Allen --- scripts/dropstat | 17 ++++++++++++----- tests/drops_group_test.py | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/scripts/dropstat b/scripts/dropstat index bddd3fa0b237..a80269fa480a 100755 --- a/scripts/dropstat +++ b/scripts/dropstat @@ -30,7 +30,8 @@ try: sys.path.insert(0, modules_path) sys.path.insert(0, test_path) import mock_tables.dbconnector - socket.gethostname = lambda : 'sonic_drops_test' + socket.gethostname = lambda: 'sonic_drops_test' + os.getuid = lambda: 27 except KeyError: pass @@ -75,8 +76,10 @@ std_port_headers_map = { # Standard Switch-Level Headers std_switch_description_header = ['DEVICE'] -# Bookkeeping Files -dropstat_dir = '/tmp/dropstat/' + +def get_dropstat_dir(): + dropstat_dir_prefix = '/tmp/dropstat' + return "{}-{}/".format(dropstat_dir_prefix, os.getuid()) class DropStat(object): @@ -89,8 +92,9 @@ class DropStat(object): self.db.connect(self.db.ASIC_DB) self.db.connect(self.db.APPL_DB) - self.port_drop_stats_file = os.path.join(dropstat_dir, 'port-stats-{}'.format(os.getuid())) - self.switch_drop_stats_file = os.path.join(dropstat_dir + 'switch-stats-{}'.format(os.getuid())) + dropstat_dir = get_dropstat_dir() + self.port_drop_stats_file = os.path.join(dropstat_dir, 'port-stats') + self.switch_drop_stats_file = os.path.join(dropstat_dir + 'switch-stats') self.stat_lookup = {} self.reverse_stat_lookup = {} @@ -405,6 +409,8 @@ Examples: group = args.group counter_type = args.type + dropstat_dir = get_dropstat_dir() + # Create the directory to hold clear results if not os.path.exists(dropstat_dir): try: @@ -422,5 +428,6 @@ Examples: else: print("Command not recognized") + if __name__ == '__main__': main() diff --git a/tests/drops_group_test.py b/tests/drops_group_test.py index 637ed09e18be..d374275a4874 100644 --- a/tests/drops_group_test.py +++ b/tests/drops_group_test.py @@ -82,7 +82,7 @@ sonic_drops_test 0 0 """ -dropstat_path = "/tmp/dropstat" +dropstat_path = "/tmp/dropstat-27" class TestDropCounters(object): @classmethod