Shorter paths to PID files in integration tests
[tinc] / test / integration / commandline.py
index 931b6d4..5db01d3 100755 (executable)
@@ -3,52 +3,17 @@
 """Test supported and unsupported commandline flags."""
 
 import os
 """Test supported and unsupported commandline flags."""
 
 import os
+import shutil
 import signal
 import subprocess as subp
 import signal
 import subprocess as subp
+import tempfile
 import time
 
 from testlib import check, util, path
 from testlib.log import log
 from testlib.proc import Tinc, Script
 from testlib.test import Test
 import time
 
 from testlib import check, util, path
 from testlib.log import log
 from testlib.proc import Tinc, Script
 from testlib.test import Test
-
-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")),
-)
+from testlib.feature import SANDBOX_LEVEL
 
 
 def init(ctx: Test) -> Tinc:
 
 
 def init(ctx: Test) -> Tinc:
@@ -59,6 +24,7 @@ def init(ctx: Test) -> Tinc:
         set Port 0
         set Address localhost
         set DeviceType dummy
         set Port 0
         set Address localhost
         set DeviceType dummy
+        set Sandbox {SANDBOX_LEVEL}
     """
     tinc.cmd(stdin=stdin)
     tinc.add_script(Script.TINC_UP)
     """
     tinc.cmd(stdin=stdin)
     tinc.add_script(Script.TINC_UP)
@@ -67,6 +33,29 @@ def init(ctx: Test) -> Tinc:
 
 with Test("commandline flags") as context:
     node = init(context)
 
 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)
 
     for code, flags in tincd_flags:
         COOKIE = util.random_string(10)
@@ -86,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)
 
         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)
 
     for code, flags in tinc_flags:
         node.cmd(*flags, code=code)
 
@@ -94,33 +99,32 @@ def test_relative_path(ctx: Test, chroot: bool) -> None:
     """Test tincd with relative paths."""
 
     foo = init(ctx)
     """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",
 
     args = [
         path.TINCD_PATH,
         "-D",
         "-c",
-        basename,
+        "conf",
         "--pidfile",
         "pid",
         "--logfile",
         "--pidfile",
         "pid",
         "--logfile",
-        ".//./log",
+        "conf/.//./log",
     ]
 
     if chroot:
         args.append("-R")
 
     ]
 
     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")
         foo[Script.TINC_UP].wait(10)
 
         log.info("pidfile and logfile must exist at expected paths")
@@ -144,6 +148,9 @@ def test_relative_path(ctx: Test, chroot: bool) -> None:
         foo.cmd("--pidfile", pidfile, "stop")
         check.equals(0, tincd.wait())
 
         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)
 
 with Test("relative path to tincd dir") as context:
     test_relative_path(context, chroot=False)