]> tinc-vpn.org Git - tinc/commitdiff
Shorter paths to PID files in integration tests
authorKirill Isakov <bootctl@gmail.com>
Tue, 31 May 2022 19:30:58 +0000 (01:30 +0600)
committerKirill Isakov <bootctl@gmail.com>
Tue, 31 May 2022 19:30:58 +0000 (01:30 +0600)
Tests were running into the 108-char limit on UNIX socket path length.

Since we don't care about saving PID files and sockets for further
analysis if anything does wrong with the test, just shove them into a
temporary directory (which is likely to have a very short name).

test/integration/cmd_misc.py
test/integration/commandline.py
test/integration/testlib/proc.py

index 84c5b6b32256abb4f48bbad72afa805904047d65..5a8a973d11294b548ef9c17e7f089fc08d6e7624 100755 (executable)
@@ -121,7 +121,7 @@ def test_pid(foo: Tinc) -> None:
     check.is_in("Too many arguments", err)
 
     log.info("test pid without arguments")
-    pidfile = util.read_text(foo.sub("pid"))
+    pidfile = util.read_text(foo.pid_file)
     pid, _ = pidfile.split(maxsplit=1)
 
     out, _ = foo.cmd("pid")
index 6781545e7731d302ce2c21001b2132ad0cb1e263..5db01d3dfd7713a4d9cba5c3a59030482814bccd 100755 (executable)
@@ -3,8 +3,10 @@
 """Test supported and unsupported commandline flags."""
 
 import os
+import shutil
 import signal
 import subprocess as subp
+import tempfile
 import time
 
 from testlib import check, util, path
@@ -13,44 +15,6 @@ from testlib.proc import Tinc, Script
 from testlib.test import Test
 from testlib.feature import SANDBOX_LEVEL
 
-tinc_flags = (
-    (0, ("get", "name")),
-    (0, ("-n", "foo", "get", "name")),
-    (0, ("-nfoo", "get", "name")),
-    (0, ("--net=foo", "get", "name")),
-    (0, ("--net", "foo", "get", "name")),
-    (0, ("-c", "conf", "-c", "conf")),
-    (0, ("-n", "net", "-n", "net")),
-    (0, ("--pidfile=pid", "--pidfile=pid")),
-    (1, ("-n", "foo", "get", "somethingreallyunknown")),
-    (1, ("--net",)),
-    (1, ("--net", "get", "name")),
-    (1, ("foo",)),
-    (1, ("-c", "conf", "-n", "n/e\\t")),
-)
-
-tincd_flags = (
-    (0, ("-D",)),
-    (0, ("--no-detach",)),
-    (0, ("-D", "-d")),
-    (0, ("-D", "-d2")),
-    (0, ("-D", "-d", "2")),
-    (0, ("-D", "-n", "foo")),
-    (0, ("-D", "-nfoo")),
-    (0, ("-D", "--net=foo")),
-    (0, ("-D", "--net", "foo")),
-    (0, ("-D", "-c", ".", "-c", ".")),
-    (0, ("-D", "-n", "net", "-n", "net")),
-    (0, ("-D", "-n", "net", "-o", "FakeOpt=42")),
-    (0, ("-D", "--logfile=log", "--logfile=log")),
-    (0, ("-D", "--pidfile=pid", "--pidfile=pid")),
-    (1, ("foo",)),
-    (1, ("--pidfile",)),
-    (1, ("--foo",)),
-    (1, ("-n", "net", "-o", "Compression=")),
-    (1, ("-c", "fakedir", "-n", "n/e\\t")),
-)
-
 
 def init(ctx: Test) -> Tinc:
     """Initialize new test nodes."""
@@ -69,6 +33,29 @@ def init(ctx: Test) -> Tinc:
 
 with Test("commandline flags") as context:
     node = init(context)
+    pf = node.pid_file
+
+    tincd_flags = (
+        (0, ("-D",)),
+        (0, ("--no-detach",)),
+        (0, ("-D", "-d")),
+        (0, ("-D", "-d2")),
+        (0, ("-D", "-d", "2")),
+        (0, ("-D", "-n", "foo")),
+        (0, ("-D", "-nfoo")),
+        (0, ("-D", "--net=foo")),
+        (0, ("-D", "--net", "foo")),
+        (0, ("-D", "-c", ".", "-c", ".")),
+        (0, ("-D", "-n", "net", "-n", "net")),
+        (0, ("-D", "-n", "net", "-o", "FakeOpt=42")),
+        (0, ("-D", "--logfile=log", "--logfile=log")),
+        (0, ("-D", f"--pidfile={pf}", f"--pidfile={pf}")),
+        (1, ("foo",)),
+        (1, ("--pidfile",)),
+        (1, ("--foo",)),
+        (1, ("-n", "net", "-o", "Compression=")),
+        (1, ("-c", "fakedir", "-n", "n/e\\t")),
+    )
 
     for code, flags in tincd_flags:
         COOKIE = util.random_string(10)
@@ -88,6 +75,22 @@ with Test("commandline flags") as context:
         log.debug('got code %d, ("%s", "%s")', server.returncode, stdout, stderr)
         check.equals(code, server.returncode)
 
+    tinc_flags = (
+        (0, ("get", "name")),
+        (0, ("-n", "foo", "get", "name")),
+        (0, ("-nfoo", "get", "name")),
+        (0, ("--net=foo", "get", "name")),
+        (0, ("--net", "foo", "get", "name")),
+        (0, ("-c", "conf", "-c", "conf")),
+        (0, ("-n", "net", "-n", "net")),
+        (0, (f"--pidfile={pf}", f"--pidfile={pf}")),
+        (1, ("-n", "foo", "get", "somethingreallyunknown")),
+        (1, ("--net",)),
+        (1, ("--net", "get", "name")),
+        (1, ("foo",)),
+        (1, ("-c", "conf", "-n", "n/e\\t")),
+    )
+
     for code, flags in tinc_flags:
         node.cmd(*flags, code=code)
 
@@ -96,33 +99,32 @@ def test_relative_path(ctx: Test, chroot: bool) -> None:
     """Test tincd with relative paths."""
 
     foo = init(ctx)
+    confdir = os.path.realpath(foo.sub("."))
+
+    # Workaround for the 108-char limit on UNIX socket path length.
+    shortcut = tempfile.mkdtemp()
+    os.symlink(confdir, os.path.join(shortcut, "conf"))
 
-    conf_dir = os.path.realpath(foo.sub("."))
-    dirname = os.path.dirname(conf_dir)
-    basename = os.path.basename(conf_dir)
-    log.info("using confdir %s, dirname %s, basename %s", conf_dir, dirname, basename)
+    log.info("using paths: confdir '%s', shortcut '%s'", confdir, shortcut)
 
     args = [
         path.TINCD_PATH,
         "-D",
         "-c",
-        basename,
+        "conf",
         "--pidfile",
         "pid",
         "--logfile",
-        ".//./log",
+        "conf/.//./log",
     ]
 
     if chroot:
         args.append("-R")
 
-    pidfile = os.path.join(dirname, "pid")
-    util.remove_file(pidfile)
+    pidfile = os.path.join(shortcut, "pid")
+    logfile = os.path.join(confdir, "log")
 
-    logfile = os.path.join(dirname, "log")
-    util.remove_file(logfile)
-
-    with subp.Popen(args, stderr=subp.STDOUT, cwd=dirname) as tincd:
+    with subp.Popen(args, stderr=subp.STDOUT, cwd=shortcut) as tincd:
         foo[Script.TINC_UP].wait(10)
 
         log.info("pidfile and logfile must exist at expected paths")
@@ -146,6 +148,9 @@ def test_relative_path(ctx: Test, chroot: bool) -> None:
         foo.cmd("--pidfile", pidfile, "stop")
         check.equals(0, tincd.wait())
 
+    # Leave behind as debugging aid if there's an exception
+    shutil.rmtree(shortcut)
+
 
 with Test("relative path to tincd dir") as context:
     test_relative_path(context, chroot=False)
index ffa0a5f50c536d41478e7f537082a42af4dedc94..660b95549c9191421618730f9b0c543f7bb008c4 100755 (executable)
@@ -2,6 +2,7 @@
 
 import os
 import random
+import tempfile
 import typing as T
 import subprocess as subp
 from enum import Enum
@@ -16,6 +17,9 @@ from .util import random_string, random_port
 # Does the OS support all addresses in 127.0.0.0/8 without additional configuration?
 _FULL_LOCALHOST_SUBNET = system() in ("Linux", "Windows")
 
+# Path to the system temporary directory.
+_TEMPDIR = tempfile.gettempdir()
+
 
 def _make_wd(name: str) -> str:
     work_dir = os.path.join(path.TEST_WD, "data", name)
@@ -63,6 +67,7 @@ class Tinc:
     name: str
     address: str
     _work_dir: str
+    _pid_file: str
     _port: T.Optional[int]
     _scripts: T.Dict[str, TincScript]
     _procs: T.List[subp.Popen]
@@ -71,6 +76,8 @@ class Tinc:
         self.name = name if name else random_string(10)
         self.address = addr if addr else _rand_localhost()
         self._work_dir = _make_wd(self.name)
+        os.makedirs(self._work_dir, exist_ok=True)
+        self._pid_file = os.path.join(_TEMPDIR, f"tinc_{self.name}")
         self._port = None
         self._scripts = {}
         self._procs = []
@@ -80,12 +87,16 @@ class Tinc:
         self._port = random_port()
         return self._port
 
+    @property
+    def pid_file(self) -> str:
+        """Get the path to the pid file."""
+        return self._pid_file
+
     def read_port(self) -> int:
         """Read port used by tincd from its pidfile and update the _port field."""
-        pidfile = self.sub("pid")
-        log.debug("reading pidfile at %s", pidfile)
+        log.debug("reading pidfile at %s", self.pid_file)
 
-        with open(pidfile, "r", encoding="utf-8") as f:
+        with open(self.pid_file, "r", encoding="utf-8") as f:
             content = f.read()
         log.debug("found data %s", content)
 
@@ -140,9 +151,9 @@ class Tinc:
             "--net",
             self.name,
             "--config",
-            self._work_dir,
+            self.work_dir,
             "--pidfile",
-            self.sub("pid"),
+            self.pid_file,
         ]
 
     def sub(self, *paths: str) -> str: