dd82781a9cc561bed39e32aa622ae1be89c54673
[tinc] / test / integration / import_export.py
1 #!/usr/bin/env python3
2
3 """Test peer information import and export."""
4
5 import typing as T
6
7 from testlib import check, cmd
8 from testlib.log import log
9 from testlib.proc import Tinc, Script
10 from testlib.test import Test
11
12
13 def init(ctx: Test) -> T.Tuple[Tinc, Tinc, Tinc]:
14     """Initialize new test nodes."""
15     foo, bar, baz = ctx.node(), ctx.node(), ctx.node()
16
17     log.info("configure %s", foo.name)
18     stdin = f"""
19         init {foo}
20         set Port 0
21         set Address localhost
22         set DeviceType dummy
23     """
24     foo.cmd(stdin=stdin)
25
26     log.info("configure %s", bar.name)
27     stdin = f"""
28         init {bar}
29         set Port 0
30         set Address localhost
31         set DeviceType dummy
32     """
33     bar.cmd(stdin=stdin)
34
35     log.info("configure %s", baz.name)
36     stdin = f"""
37         init {baz}
38         set Port 0
39         set Address localhost
40         set DeviceType dummy
41     """
42     baz.cmd(stdin=stdin)
43
44     return foo, bar, baz
45
46
47 def run_tests(ctx: Test) -> None:
48     """Run all tests."""
49     foo, bar, baz = init(ctx)
50
51     tinc_up = f"""
52     bar, baz = Tinc('{bar}'), Tinc('{baz}')
53     bar.cmd('add', 'ConnectTo', this.name)
54     baz.cmd('add', 'ConnectTo', this.name)
55     """
56     foo.add_script(Script.TINC_UP, tinc_up)
57     foo.start()
58
59     log.info("run exchange")
60     cmd.exchange(foo, bar)
61
62     log.info("run exchange with export-all")
63     cmd.exchange(foo, baz, export_all=True)
64
65     log.info("run exchange-all")
66     out, err = foo.cmd("exchange-all", code=1)
67     check.is_in("No host configuration files imported", err)
68
69     log.info("run import")
70     bar.cmd("import", stdin=out)
71
72     for first, second in (
73         (foo.sub("hosts", foo.name), bar.sub("hosts", foo.name)),
74         (foo.sub("hosts", foo.name), baz.sub("hosts", foo.name)),
75         (foo.sub("hosts", bar.name), bar.sub("hosts", bar.name)),
76         (foo.sub("hosts", bar.name), baz.sub("hosts", bar.name)),
77         (foo.sub("hosts", baz.name), bar.sub("hosts", baz.name)),
78         (foo.sub("hosts", baz.name), baz.sub("hosts", baz.name)),
79     ):
80         log.info("comparing configs %s and %s", first, second)
81         check.files_eq(first, second)
82
83     log.info("create %s scripts", foo)
84     foo.add_script(bar.script_up)
85     foo.add_script(baz.script_up)
86
87     log.info("start nodes")
88     bar.cmd("start")
89     baz.cmd("start")
90
91     log.info("wait for up scripts")
92     foo[bar.script_up].wait()
93     foo[baz.script_up].wait()
94
95     for tinc in foo, bar, baz:
96         check.nodes(tinc, 3)
97
98
99 with Test("import-export") as context:
100     run_tests(context)