From: Florian Westphal Date: Thu, 5 Mar 2026 22:52:48 +0000 (+0100) Subject: tests: py: don't use a fixed filename X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f2b5d6fc26b9aa94eddb065628858e79069b5012;p=thirdparty%2Fnftables.git 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. Signed-off-by: Florian Westphal --- diff --git a/tests/py/nft-test.py b/tests/py/nft-test.py index 53fd3f7a..c83a737a 100755 --- a/tests/py/nft-test.py +++ b/tests/py/nft-test.py @@ -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 = []