]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
tests: py: don't use a fixed filename
authorFlorian Westphal <fw@strlen.de>
Thu, 5 Mar 2026 22:52:48 +0000 (23:52 +0100)
committerFlorian Westphal <fw@strlen.de>
Wed, 15 Apr 2026 19:39:11 +0000 (21:39 +0200)
Using a predicatable filename in /tmp is not good practice.

This test runs with uid 0 and stray symlink could lead to unwanted
effects.  Use a temporary file and auto-delete it unless -k/--keep gets
passed to us.

Signed-off-by: Florian Westphal <fw@strlen.de>
tests/py/nft-test.py

index 53fd3f7ae6fea1b9f758cff64f48e57c93f564c4..c83a737a5f3b53c146fe949c6e0ae570226bfeaf 100755 (executable)
@@ -30,13 +30,13 @@ os.environ['TZ'] = 'UTC-2'
 from nftables import Nftables
 
 TESTS_DIRECTORY = ["any", "arp", "bridge", "inet", "ip", "ip6", "netdev"]
-LOGFILE = "/tmp/nftables-test.log"
 log_file = None
 table_list = []
 chain_list = []
 all_set = dict()
 obj_list = []
 signal_received = 0
+auto_delete = True
 
 
 class Colors:
@@ -1523,6 +1523,9 @@ def main():
     parser.add_argument('-l', '--library', default=None,
                         help='path to libntables.so.1, overrides --host')
 
+    parser.add_argument('-k', '--keep', default=False,
+                        help='keep log file around after tests')
+
     parser.add_argument('-N', '--no-netns', action='store_true',
                         dest='no_netns',
                         help='Do not run in own network namespace')
@@ -1574,6 +1577,11 @@ def main():
               "You need to build the project." % args.library)
         return 99
 
+    global auto_delete
+
+    if args.keep:
+        auto_delete = False
+
     if args.enable_schema and not args.enable_json:
         print_error("Option --schema requires option --json")
         return 99
@@ -1585,10 +1593,13 @@ def main():
     tests = passed = warnings = errors = 0
     global log_file
     try:
-        log_file = open(LOGFILE, 'w')
-        print_info("Log will be available at %s" % LOGFILE)
+        log_file = tempfile.NamedTemporaryFile(prefix="nftables-test-py-", suffix=".log", mode='w', delete=auto_delete)
+        if auto_delete:
+            print_info("Log file %s will not be retained.  Pass -k to keep it.")
+        else:
+            print_info("Log will be available at %s" % log_file.name)
     except IOError:
-        print_error("Cannot open log file %s" % LOGFILE)
+        print_error("Cannot create a temporary log file")
         return 99
 
     file_list = []