Rewrite integration test suite in Python
[tinc] / test / integration / invite.py
1 #!/usr/bin/env python3
2 # pylint: disable=import-outside-toplevel
3
4 """Test tinc peer invitations."""
5
6 from testlib import check, util
7 from testlib.log import log
8 from testlib.test import Test
9
10
11 def run_invite_test(ctx: Test, start_before_invite: bool) -> None:
12     """Run tests. If start_before_invite is True,
13     tincd is started *before* creating invitation, and vice versa.
14     """
15     foo, bar = ctx.node(), ctx.node()
16
17     stdin = f"""
18         init {foo}
19         set Port 0
20         set Address localhost
21         set DeviceType dummy
22         set Mode switch
23         set Broadcast no
24     """
25     foo.cmd(stdin=stdin)
26
27     if start_before_invite:
28         port = foo.start()
29
30     log.info("create invitation")
31     foo_invite, _ = foo.cmd("invite", bar.name)
32     assert foo_invite
33     foo_invite = foo_invite.strip()
34
35     if not start_before_invite:
36         port = foo.start()
37         foo_invite = foo_invite.replace(":0/", f":{port}/")
38
39     log.info("join second node with %s", foo_invite)
40     bar.cmd("join", foo_invite)
41     bar.cmd("set", "Port", "0")
42
43     if not start_before_invite:
44         log.info("%s thinks %s is using port 0, updating", bar, foo)
45         bar.cmd("set", f"{foo}.Port", str(port))
46
47     log.info("compare configs")
48     check.files_eq(foo.sub("hosts", foo.name), bar.sub("hosts", foo.name))
49
50     log.info("compare keys")
51
52     prefix = "Ed25519PublicKey"
53     foo_key = util.find_line(foo.sub("hosts", bar.name), prefix)
54     bar_key = util.find_line(bar.sub("hosts", bar.name), prefix)
55     check.equals(foo_key, bar_key)
56
57     log.info("checking Mode")
58     bar_mode, _ = bar.cmd("get", "Mode")
59     check.equals("switch", bar_mode.strip())
60
61     log.info("checking Broadcast")
62     bar_bcast, _ = bar.cmd("get", "Broadcast")
63     check.equals("no", bar_bcast.strip())
64
65     log.info("checking ConnectTo")
66     bar_conn, _ = bar.cmd("get", "ConnectTo")
67     check.equals(foo.name, bar_conn.strip())
68
69     log.info("configuring %s", bar.name)
70     bar.cmd("set", "DeviceType", "dummy")
71
72     log.info("adding scripts")
73     foo.add_script(bar.script_up)
74     bar.add_script(foo.script_up)
75
76     log.info("starting %s", bar.name)
77     bar.cmd("start")
78
79     log.info("waiting for nodes to come up")
80     foo[bar.script_up].wait()
81     bar[foo.script_up].wait()
82
83     log.info("checking required nodes")
84     check.nodes(foo, 2)
85     check.nodes(bar, 2)
86
87
88 with Test("offline mode") as context:
89     run_invite_test(context, start_before_invite=False)
90
91 with Test("online mode") as context:
92     run_invite_test(context, start_before_invite=True)