531868f63d87eb59ae400de7689a00d19b87f4a3
[tinc] / test / integration / commandline.py
1 #!/usr/bin/env python3
2
3 """Test supported and unsupported commandline flags."""
4
5 from testlib import check, util
6 from testlib.log import log
7 from testlib.proc import Tinc, Script
8 from testlib.test import Test
9
10 tinc_flags = (
11     (0, ("get", "name")),
12     (0, ("-n", "foo", "get", "name")),
13     (0, ("-nfoo", "get", "name")),
14     (0, ("--net=foo", "get", "name")),
15     (0, ("--net", "foo", "get", "name")),
16     (0, ("-c", "conf", "-c", "conf")),
17     (0, ("-n", "net", "-n", "net")),
18     (0, ("--pidfile=pid", "--pidfile=pid")),
19     (1, ("-n", "foo", "get", "somethingreallyunknown")),
20     (1, ("--net",)),
21     (1, ("--net", "get", "name")),
22     (1, ("foo",)),
23     (1, ("-c", "conf", "-n", "n/e\\t")),
24 )
25
26 tincd_flags = (
27     (0, ("-D",)),
28     (0, ("--no-detach",)),
29     (0, ("-D", "-d")),
30     (0, ("-D", "-d2")),
31     (0, ("-D", "-d", "2")),
32     (0, ("-D", "-n", "foo")),
33     (0, ("-D", "-nfoo")),
34     (0, ("-D", "--net=foo")),
35     (0, ("-D", "--net", "foo")),
36     (0, ("-D", "-c", ".", "-c", ".")),
37     (0, ("-D", "-n", "net", "-n", "net")),
38     (0, ("-D", "-n", "net", "-o", "FakeOpt=42")),
39     (0, ("-D", "--logfile=log", "--logfile=log")),
40     (0, ("-D", "--pidfile=pid", "--pidfile=pid")),
41     (1, ("foo",)),
42     (1, ("--pidfile",)),
43     (1, ("--foo",)),
44     (1, ("-n", "net", "-o", "Compression=")),
45     (1, ("-c", "fakedir", "-n", "n/e\\t")),
46 )
47
48
49 def init(ctx: Test) -> Tinc:
50     """Initialize new test nodes."""
51     tinc = ctx.node()
52     stdin = f"""
53         init {tinc}
54         set Port 0
55         set Address localhost
56         set DeviceType dummy
57     """
58     tinc.cmd(stdin=stdin)
59     tinc.add_script(Script.TINC_UP)
60     return tinc
61
62
63 with Test("commandline flags") as context:
64     node = init(context)
65
66     for code, flags in tincd_flags:
67         COOKIE = util.random_string(10)
68         server = node.tincd(*flags, env={"COOKIE": COOKIE})
69
70         if not code:
71             log.info("waiting for tincd to come up")
72             env = node[Script.TINC_UP].wait().env
73             check.equals(COOKIE, env["COOKIE"])
74
75         log.info("stopping tinc")
76         node.cmd("stop", code=code)
77
78         log.info("reading tincd output")
79         stdout, stderr = server.communicate()
80
81         log.debug('got code %d, ("%s", "%s")', server.returncode, stdout, stderr)
82         check.equals(code, server.returncode)
83
84     for code, flags in tinc_flags:
85         node.cmd(*flags, code=code)