3 """Test supported and unsupported commandline flags."""
7 import subprocess as subp
10 from testlib import check, util, path
11 from testlib.log import log
12 from testlib.proc import Tinc, Script
13 from testlib.test import Test
17 (0, ("-n", "foo", "get", "name")),
18 (0, ("-nfoo", "get", "name")),
19 (0, ("--net=foo", "get", "name")),
20 (0, ("--net", "foo", "get", "name")),
21 (0, ("-c", "conf", "-c", "conf")),
22 (0, ("-n", "net", "-n", "net")),
23 (0, ("--pidfile=pid", "--pidfile=pid")),
24 (1, ("-n", "foo", "get", "somethingreallyunknown")),
26 (1, ("--net", "get", "name")),
28 (1, ("-c", "conf", "-n", "n/e\\t")),
33 (0, ("--no-detach",)),
36 (0, ("-D", "-d", "2")),
37 (0, ("-D", "-n", "foo")),
39 (0, ("-D", "--net=foo")),
40 (0, ("-D", "--net", "foo")),
41 (0, ("-D", "-c", ".", "-c", ".")),
42 (0, ("-D", "-n", "net", "-n", "net")),
43 (0, ("-D", "-n", "net", "-o", "FakeOpt=42")),
44 (0, ("-D", "--logfile=log", "--logfile=log")),
45 (0, ("-D", "--pidfile=pid", "--pidfile=pid")),
49 (1, ("-n", "net", "-o", "Compression=")),
50 (1, ("-c", "fakedir", "-n", "n/e\\t")),
54 def init(ctx: Test) -> Tinc:
55 """Initialize new test nodes."""
64 tinc.add_script(Script.TINC_UP)
68 with Test("commandline flags") as context:
71 for code, flags in tincd_flags:
72 COOKIE = util.random_string(10)
73 server = node.tincd(*flags, env={"COOKIE": COOKIE})
76 log.info("waiting for tincd to come up")
77 env = node[Script.TINC_UP].wait().env
78 check.equals(COOKIE, env["COOKIE"])
80 log.info("stopping tinc")
81 node.cmd("stop", code=code)
83 log.info("reading tincd output")
84 stdout, stderr = server.communicate()
86 log.debug('got code %d, ("%s", "%s")', server.returncode, stdout, stderr)
87 check.equals(code, server.returncode)
89 for code, flags in tinc_flags:
90 node.cmd(*flags, code=code)
93 def test_relative_path(ctx: Test, chroot: bool) -> None:
94 """Test tincd with relative paths."""
98 conf_dir = os.path.realpath(foo.sub("."))
99 dirname = os.path.dirname(conf_dir)
100 basename = os.path.basename(conf_dir)
101 log.info("using confdir %s, dirname %s, basename %s", conf_dir, dirname, basename)
117 pidfile = os.path.join(dirname, "pid")
118 util.remove_file(pidfile)
120 logfile = os.path.join(dirname, "log")
121 util.remove_file(logfile)
123 with subp.Popen(args, stderr=subp.STDOUT, cwd=dirname) as tincd:
124 foo[Script.TINC_UP].wait(10)
126 log.info("pidfile and logfile must exist at expected paths")
127 check.file_exists(pidfile)
128 check.file_exists(logfile)
130 # chrooted tincd won't be able to reopen its log since in this
131 # test we put the log outside tinc's configuration directory.
132 if os.name != "nt" and not chroot:
133 log.info("test log file rotation")
135 util.remove_file(logfile)
136 os.kill(tincd.pid, signal.SIGHUP)
139 log.info("pidfile and logfile must still exist")
140 check.file_exists(pidfile)
141 check.file_exists(logfile)
143 log.info("stopping tinc through '%s'", pidfile)
144 foo.cmd("--pidfile", pidfile, "stop")
145 check.equals(0, tincd.wait())
148 with Test("relative path to tincd dir") as context:
149 test_relative_path(context, chroot=False)
151 if os.name != "nt" and not os.getuid():
152 with Test("relative path to tincd dir (chroot)") as context:
153 test_relative_path(context, chroot=True)