2
0

invite.py 3.0 KB

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