From 8768580aa055429bed9d31036afc2129ed1c0756 Mon Sep 17 00:00:00 2001 From: Tamer Ahmed Date: Sat, 15 Aug 2020 15:04:11 -0700 Subject: [PATCH] [filter-fdb] Call Filter FDB Main From Within Test Code (#1051) Code coverage requires that python code be run with the same process. Current test code was invoking filter fdb via shell which launches new process and so coverage is not available. This PR calls the main method from within test code. signed-off-by: Tamer Ahmed --- fdbutil/__init__.py | 0 {scripts => fdbutil}/filter_fdb_entries.py | 29 ++++++---------------- pytest.ini | 2 +- scripts/fast-reboot | 2 +- setup.py | 3 ++- tests/filter_fdb_entries_test.py | 9 ++++--- 6 files changed, 17 insertions(+), 28 deletions(-) create mode 100644 fdbutil/__init__.py rename {scripts => fdbutil}/filter_fdb_entries.py (93%) diff --git a/fdbutil/__init__.py b/fdbutil/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/scripts/filter_fdb_entries.py b/fdbutil/filter_fdb_entries.py similarity index 93% rename from scripts/filter_fdb_entries.py rename to fdbutil/filter_fdb_entries.py index 31d4204ec9..23380ed0ef 100755 --- a/scripts/filter_fdb_entries.py +++ b/fdbutil/filter_fdb_entries.py @@ -1,5 +1,3 @@ -#!/usr/bin/env python - import json import sys import os @@ -124,44 +122,33 @@ def file_exists_or_raise(filename): if not os.path.exists(filename): raise Exception("file '{0}' does not exist".format(filename)) -def main(): +def main(argv=sys.argv): parser = argparse.ArgumentParser() parser.add_argument('-f', '--fdb', type=str, default='/tmp/fdb.json', help='fdb file name') parser.add_argument('-a', '--arp', type=str, default='/tmp/arp.json', help='arp file name') parser.add_argument('-c', '--config_db', type=str, default='/tmp/config_db.json', help='config db file name') parser.add_argument('-b', '--backup_file', type=bool, default=True, help='Back up old fdb entries file') - args = parser.parse_args() + args = parser.parse_args(argv[1:]) fdb_filename = args.fdb arp_filename = args.arp config_db_filename = args.config_db backup_file = args.backup_file + res = 0 try: + syslog.openlog('filter_fdb_entries') file_exists_or_raise(fdb_filename) file_exists_or_raise(arp_filename) file_exists_or_raise(config_db_filename) except Exception as e: syslog.syslog(syslog.LOG_ERR, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc())) - else: - filter_fdb_entries(fdb_filename, arp_filename, config_db_filename, backup_file) - - return 0 - -if __name__ == '__main__': - res = 0 - try: - syslog.openlog('filter_fdb_entries') - res = main() except KeyboardInterrupt: syslog.syslog(syslog.LOG_NOTICE, "SIGINT received. Quitting") res = 1 - except Exception as e: - syslog.syslog(syslog.LOG_ERR, "Got an exception %s: Traceback: %s" % (str(e), traceback.format_exc())) - res = 2 + else: + filter_fdb_entries(fdb_filename, arp_filename, config_db_filename, backup_file) finally: syslog.closelog() - try: - sys.exit(res) - except SystemExit: - os._exit(res) + + return res diff --git a/pytest.ini b/pytest.ini index d1975890b9..40d110e3b0 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,4 +1,4 @@ [pytest] filterwarnings = ignore::DeprecationWarning -addopts = --cov=acl_loader --cov=clear --cov=config --cov=connect --cov=consutil --cov=counterpoll --cov=crm --cov=debug --cov=fwutil --cov=pcieutil --cov=pfcwd --cov=psuutil --cov=pddf_fanutil --cov=pddf_ledutil --cov=pddf_psuutil --cov=pddf_thermalutil --cov=scripts --cov=sfputil --cov=show --cov=sonic_installer --cov=ssdutil --cov=utilities_common --cov=watchdogutil --cov-report html --cov-report term --cov-report xml +addopts = --cov=acl_loader --cov=clear --cov=config --cov=connect --cov=consutil --cov=counterpoll --cov=crm --cov=debug --cov=fdbutil --cov=fwutil --cov=pcieutil --cov=pfcwd --cov=psuutil --cov=pddf_fanutil --cov=pddf_ledutil --cov=pddf_psuutil --cov=pddf_thermalutil --cov=scripts --cov=sfputil --cov=show --cov=sonic_installer --cov=ssdutil --cov=utilities_common --cov=watchdogutil --cov-report html --cov-report term --cov-report xml diff --git a/scripts/fast-reboot b/scripts/fast-reboot index 6ed44c8dde..b2333e2386 100755 --- a/scripts/fast-reboot +++ b/scripts/fast-reboot @@ -456,7 +456,7 @@ if [[ "$REBOOT_TYPE" = "fast-reboot" ]]; then FILTER_FDB_ENTRIES_RC=0 # Filter FDB entries using MAC addresses from ARP table - /usr/bin/filter_fdb_entries.py -f $DUMP_DIR/fdb.json -a $DUMP_DIR/arp.json -c $CONFIG_DB_FILE || FILTER_FDB_ENTRIES_RC=$? + /usr/bin/filter_fdb_entries -f $DUMP_DIR/fdb.json -a $DUMP_DIR/arp.json -c $CONFIG_DB_FILE || FILTER_FDB_ENTRIES_RC=$? if [[ FILTER_FDB_ENTRIES_RC -ne 0 ]]; then error "Failed to filter FDb entries. Exit code: $FILTER_FDB_ENTRIES_RC" unload_kernel diff --git a/setup.py b/setup.py index d0b74c2aea..37e55cba1e 100644 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ 'ssdutil', 'pfc', 'psuutil', + 'fdbutil', 'fwutil', 'pcieutil', 'pddf_fanutil', @@ -78,7 +79,6 @@ 'scripts/fast-reboot-dump.py', 'scripts/fdbclear', 'scripts/fdbshow', - 'scripts/filter_fdb_entries.py', 'scripts/gearboxutil', 'scripts/generate_dump', 'scripts/intfutil', @@ -124,6 +124,7 @@ 'counterpoll = counterpoll.main:cli', 'crm = crm.main:cli', 'debug = debug.main:cli', + 'filter_fdb_entries = fdbutil.filter_fdb_entries:main', 'pfcwd = pfcwd.main:cli', 'sfputil = sfputil.main:cli', 'ssdutil = ssdutil.main:ssdutil', diff --git a/tests/filter_fdb_entries_test.py b/tests/filter_fdb_entries_test.py index af1f7712c3..fbbff67ea9 100644 --- a/tests/filter_fdb_entries_test.py +++ b/tests/filter_fdb_entries_test.py @@ -7,6 +7,7 @@ from collections import defaultdict from filter_fdb_input.test_vectors import filterFdbEntriesTestVector +from fdbutil.filter_fdb_entries import main as filterFdbMain class TestFilterFdbEntries(object): """ @@ -162,16 +163,16 @@ def testFilterFdbEntries(self, testData): """ try: self.__setUp(testData) - - stdout, stderr, rc = self.__runCommand([ - "scripts/filter_fdb_entries.py", + argv = [ + "filter_fdb_entries", "-a", self.ARP_FILENAME, "-f", self.FDB_FILENAME, "-c", self.CONFIG_DB_FILENAME, - ]) + ] + rc = filterFdbMain(argv) assert rc == 0, "Filter_fdb_entries.py failed with '{0}'".format(stderr) assert self.__verifyOutput(), "Test failed for test data: {0}".format(testData) finally: