Skip to content

Commit

Permalink
[filter-fdb] Call Filter FDB Main From Within Test Code (sonic-net#1051)
Browse files Browse the repository at this point in the history
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 <tamer.ahmed@microsoft.com>
  • Loading branch information
tahmed-dev committed Sep 1, 2020
1 parent 8e30ac1 commit 3ddea10
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 deletions.
Empty file added fdbutil/__init__.py
Empty file.
29 changes: 8 additions & 21 deletions scripts/filter_fdb_entries.py → fdbutil/filter_fdb_entries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#!/usr/bin/env python

import json
import sys
import os
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def get_test_suite():
'sfputil',
'pfc',
'psuutil',
'fdbutil',
'show',
'sonic_installer',
'sonic-utilities-tests',
Expand All @@ -70,7 +71,6 @@ def get_test_suite():
'scripts/fast-reboot-dump.py',
'scripts/fdbclear',
'scripts/fdbshow',
'scripts/filter_fdb_entries.py',
'scripts/generate_dump',
'scripts/intfutil',
'scripts/lldpshow',
Expand Down Expand Up @@ -102,6 +102,7 @@ def get_test_suite():
'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',
'pfc = pfc.main:cli',
Expand Down
9 changes: 5 additions & 4 deletions sonic-utilities-tests/filter_fdb_entries_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3ddea10

Please sign in to comment.