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 authored Aug 15, 2020
1 parent 3297b7a commit 8768580
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 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 pytest.ini
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion scripts/fast-reboot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'ssdutil',
'pfc',
'psuutil',
'fdbutil',
'fwutil',
'pcieutil',
'pddf_fanutil',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions 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 8768580

Please sign in to comment.