Add tests for some device & address variables
[tinc] / test / integration / testlib / util.py
index 8102bca..fdba760 100755 (executable)
@@ -7,6 +7,8 @@ import random
 import string
 import socket
 import typing as T
+import tempfile
+from pathlib import Path
 
 from . import check
 from .log import log
@@ -31,6 +33,23 @@ def random_port() -> int:
             log.debug("could not bind to random port %d", port, exc_info=ex)
 
 
+def temp_file(content: str) -> str:
+    """Create a temporary file and write text content into it."""
+    file = tempfile.mktemp()
+    with open(file, "w", encoding="utf-8") as f:
+        f.write(content)
+    return file
+
+
+def remove_file(path: T.Union[str, Path]) -> bool:
+    """Try to remove file without failing if it does not exist."""
+    try:
+        os.remove(path)
+        return True
+    except FileNotFoundError:
+        return False
+
+
 def random_string(k: int) -> str:
     """Generate a random alphanumeric string of length k."""
     return "".join(random.choices(_ALPHA_NUMERIC, k=k))
@@ -60,9 +79,13 @@ def require_command(*args: str) -> None:
     """Check that command args runs with exit code 0.
     Exit with code 77 otherwise.
     """
-    if subp.run(args, check=False).returncode:
-        log.info('this test requires command "%s" to work', " ".join(args))
-        sys.exit(EXIT_SKIP)
+    try:
+        if subp.run(args, check=False).returncode == 0:
+            return
+    except FileNotFoundError:
+        pass
+    log.info('this test requires command "%s" to work', " ".join(args))
+    sys.exit(EXIT_SKIP)
 
 
 def require_path(path: str) -> None: