Skip to contentSkip to conversation

tests: py: don't use a fixed filename

PATCH New Florian Westphal 1 participant 1 message Apr 9 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 tests: py: don't use a fixed filename

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.

---
 tests/py/nft-test.py | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
tests/py/nft-test.py +15 -4
index 53fd3f7ae6fe..c83a737a5f3b 100755
@@ -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 = []
Signature · 1 line
2.52.0