9f7fc203dc1642bab78f55bbbb47e60490a9fe7c
[tinc] / test / integration / import_export.py
1 #!/usr/bin/env python3
2
3 """Test peer information import and export."""
4
5 from testlib import check, cmd
6 from testlib.log import log
7 from testlib.proc import Script
8 from testlib.test import Test
9
10
11 def run_tests(ctx: Test) -> None:
12     """Run all tests."""
13     foo, bar, baz = ctx.node(init=True), ctx.node(init=True), ctx.node(init=True)
14
15     tinc_up = f"""
16     bar, baz = Tinc('{bar}'), Tinc('{baz}')
17     bar.cmd('add', 'ConnectTo', this.name)
18     baz.cmd('add', 'ConnectTo', this.name)
19     """
20     foo.add_script(Script.TINC_UP, tinc_up)
21     foo.start()
22
23     log.info("run exchange")
24     cmd.exchange(foo, bar)
25
26     log.info("run exchange with export-all")
27     cmd.exchange(foo, baz, export_all=True)
28
29     log.info("run exchange-all")
30     out, err = foo.cmd("exchange-all", code=1)
31     check.is_in("No host configuration files imported", err)
32
33     log.info("run import")
34     bar.cmd("import", stdin=out)
35
36     for first, second in (
37         (foo.sub("hosts", foo.name), bar.sub("hosts", foo.name)),
38         (foo.sub("hosts", foo.name), baz.sub("hosts", foo.name)),
39         (foo.sub("hosts", bar.name), bar.sub("hosts", bar.name)),
40         (foo.sub("hosts", bar.name), baz.sub("hosts", bar.name)),
41         (foo.sub("hosts", baz.name), bar.sub("hosts", baz.name)),
42         (foo.sub("hosts", baz.name), baz.sub("hosts", baz.name)),
43     ):
44         log.info("comparing configs %s and %s", first, second)
45         check.files_eq(first, second)
46
47     log.info("create %s scripts", foo)
48     foo.add_script(bar.script_up)
49     foo.add_script(baz.script_up)
50
51     log.info("start nodes")
52     bar.cmd("start")
53     baz.cmd("start")
54
55     log.info("wait for up scripts")
56     foo[bar.script_up].wait()
57     foo[baz.script_up].wait()
58
59     for tinc in foo, bar, baz:
60         check.nodes(tinc, 3)
61
62
63 with Test("import-export") as context:
64     run_tests(context)