import_export.py 2.5 KB

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