]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: py: nft-tests.py: Add function for loading and removing kernel modules
authorManuel Johannes Messner <manuel.johannes.messner@hs-furtwangen.de>
Mon, 5 Sep 2016 18:20:12 +0000 (20:20 +0200)
committerFlorian Westphal <fw@strlen.de>
Tue, 6 Sep 2016 11:09:41 +0000 (13:09 +0200)
Some tests use the dummy kernel module. This commit adds a function to
automatically load that module and remove it afterwards.

Signed-off-by: Manuel Johannes Messner <manuel.johannes.messner@hs-furtwangen.de>
Signed-off-by: Florian Westphal <fw@strlen.de>
tests/py/nft-test.py

index fc7ae608b46b209ab5ff981ba2d3196a1abe2f61..c1217bbbe72b7d8d95f6a79fbc4a0b0661761a4c 100755 (executable)
@@ -28,6 +28,7 @@ table_list = []
 chain_list = []
 all_set = dict()
 signal_received = 0
+modules = []
 
 
 class Colors:
@@ -114,6 +115,30 @@ def print_differences_error(filename, lineno, cmd):
           str(lineno + 1) + ": '" + cmd + "': " + reason
 
 
+def cleanup_modules():
+    for i in modules:
+        modprobe(i, remove=True)
+
+
+def modprobe(name, remove=False):
+    '''
+    Loads or removes a kernel module
+    '''
+    if name is None:
+        return -1
+
+    cmds = {
+        'ins': ['modprobe', '--first-time', '--quiet', name],
+        'del': ['modprobe', '-r', name],
+    }
+
+    ret = subprocess.call(cmds['del' if remove else 'ins']) == 0
+    if ret and not remove:
+        # the kernel module has been loaded -> remove it afterwards
+        global modules
+        modules.append(name)
+
+
 def table_exist(table, filename, lineno):
     '''
     Exists a table.
@@ -686,6 +711,8 @@ def cleanup_on_exit():
             set_delete(table)
         table_delete(table)
 
+    cleanup_modules()
+
 
 def signal_handler(signal, frame):
     global signal_received
@@ -965,6 +992,8 @@ def main():
         print "The nft binary does not exist. You need to build the project."
         return
 
+    modprobe('dummy')
+
     test_files = files_ok = run_total = 0
     tests = passed = warnings = errors = 0
     global log_file
@@ -972,6 +1001,7 @@ def main():
         log_file = open(LOGFILE, 'w')
     except IOError:
         print "Cannot open log file %s" % LOGFILE
+        cleanup_modules()
         return
 
     file_list = []
@@ -1020,6 +1050,8 @@ def main():
                       "%d error, %d warning" \
                       % (test_files, files_ok, tests, errors, warnings)
 
+    cleanup_modules()
+
 
 if __name__ == '__main__':
     main()