address_cache.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python3
  2. """Test recent address cache."""
  3. import os
  4. import typing as T
  5. import shutil
  6. from testlib import check
  7. from testlib.log import log
  8. from testlib.proc import Tinc, Script
  9. from testlib.test import Test
  10. def init(ctx: Test) -> T.Tuple[Tinc, Tinc]:
  11. """Create test node."""
  12. foo, bar = ctx.node(), ctx.node()
  13. stdin = f"""
  14. init {foo}
  15. set Port 0
  16. set Address localhost
  17. set DeviceType dummy
  18. set AutoConnect no
  19. """
  20. foo.cmd(stdin=stdin)
  21. return foo, bar
  22. def connect_nodes(foo: Tinc, bar: Tinc) -> None:
  23. """Start second node and wait for connection."""
  24. log.info("connect nodes")
  25. bar.cmd("start")
  26. bar[foo.script_up].wait()
  27. foo[bar.script_up].wait()
  28. def run_tests(ctx: Test) -> None:
  29. """Run tests."""
  30. foo, bar = init(ctx)
  31. log.info("cache directory must exist after init")
  32. check.dir_exists(foo.sub("cache"))
  33. foo.add_script(Script.TINC_UP)
  34. foo.add_script(Script.INVITATION_ACCEPTED)
  35. foo.start()
  36. log.info("invite %s to %s", bar, foo)
  37. invite, _ = foo.cmd("invite", bar.name)
  38. invite = invite.strip()
  39. log.info("join %s to %s", bar, foo)
  40. bar.cmd("join", invite)
  41. log.info("cache directory must exist after join")
  42. check.dir_exists(bar.sub("cache"))
  43. log.info("invitee address must be cached after invitation is accepted")
  44. foo[Script.INVITATION_ACCEPTED].wait()
  45. check.file_exists(foo.sub(f"cache/{bar}"))
  46. os.remove(foo.sub(f"cache/{bar}"))
  47. log.info("configure %s", bar)
  48. bar.cmd("set", "DeviceType", "dummy")
  49. bar.cmd("set", "Port", "0")
  50. log.info("add host-up scripts")
  51. foo.add_script(bar.script_up)
  52. bar.add_script(foo.script_up)
  53. connect_nodes(foo, bar)
  54. log.info("%s must cache %s's public address", bar, foo)
  55. check.file_exists(bar.sub(f"cache/{foo}"))
  56. log.info("%s must not cache %s's outgoing address", foo, bar)
  57. assert not os.path.exists(foo.sub(f"cache/{bar}"))
  58. log.info("stop node %s", bar)
  59. bar.cmd("stop")
  60. log.info("remove %s cache directory", bar)
  61. shutil.rmtree(bar.sub("cache"))
  62. connect_nodes(foo, bar)
  63. log.info("make sure %s cache was recreated", bar)
  64. check.file_exists(bar.sub(f"cache/{foo}"))
  65. log.info("stop nodes")
  66. bar.cmd("stop")
  67. foo.cmd("stop")
  68. log.info("remove Address from all nodes")
  69. for node in foo, bar:
  70. node.cmd("del", "Address", code=None)
  71. for peer in foo, bar:
  72. node.cmd("del", f"{peer}.Address", code=None)
  73. bar.cmd("add", "ConnectTo", foo.name)
  74. log.info("make sure connection works using just the cached address")
  75. foo.cmd("start")
  76. connect_nodes(foo, bar)
  77. with Test("run address cache tests") as context:
  78. run_tests(context)