test_command.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import click
  2. import asyncio
  3. import sys
  4. import pathlib
  5. import dagger
  6. import uuid
  7. import httpx
  8. from nd import Distribution, NetdataInstaller, FeatureFlags, Endpoint, AgentContext
  9. def run_async(func):
  10. def wrapper(*args, **kwargs):
  11. return asyncio.run(func(*args, **kwargs))
  12. return wrapper
  13. @run_async
  14. async def simple_test():
  15. config = dagger.Config(log_output=sys.stdout)
  16. async with dagger.Connection(config) as client:
  17. platform = dagger.Platform("linux/x86_64")
  18. distro = Distribution("debian10")
  19. repo_root = pathlib.Path("/netdata")
  20. prefix_path = pathlib.Path("/opt/netdata")
  21. installer = NetdataInstaller(
  22. platform, distro, repo_root, prefix_path, FeatureFlags.DBEngine
  23. )
  24. api_key = uuid.uuid4()
  25. #
  26. # parent
  27. #
  28. parent_endpoint = Endpoint("parent1", 22000)
  29. parent_ctx = AgentContext(
  30. client, platform, distro, installer, parent_endpoint, api_key, True
  31. )
  32. parent_cmd = installer.prefix / "usr/sbin/netdata"
  33. parent_args = [
  34. parent_cmd.as_posix(),
  35. "-D",
  36. "-i",
  37. "0.0.0.0",
  38. "-p",
  39. str(parent_endpoint.port),
  40. ]
  41. parent_ctr = parent_ctx.build_container()
  42. parent_ctr = parent_ctr.with_exec(parent_args)
  43. parent_svc = parent_ctr.as_service()
  44. #
  45. # child
  46. #
  47. child_endpoint = Endpoint("child1", 21000)
  48. child_ctx = AgentContext(
  49. client, platform, distro, installer, child_endpoint, api_key, False
  50. )
  51. child_ctx.add_parent(parent_ctx)
  52. child_cmd = installer.prefix / "usr/sbin/netdata"
  53. child_args = [
  54. child_cmd.as_posix(),
  55. "-D",
  56. "-i",
  57. "0.0.0.0",
  58. "-p",
  59. str(child_endpoint.port),
  60. ]
  61. child_ctr = child_ctx.build_container()
  62. child_ctr = child_ctr.with_service_binding(parent_endpoint.hostname, parent_svc)
  63. child_ctr = child_ctr.with_exec(child_args)
  64. child_svc = child_ctr.as_service()
  65. #
  66. # endpoints
  67. #
  68. parent_tunnel, child_tunnel = await asyncio.gather(
  69. client.host().tunnel(parent_svc, native=True).start(),
  70. client.host().tunnel(child_svc, native=True).start(),
  71. )
  72. parent_endpoint, child_endpoint = await asyncio.gather(
  73. parent_tunnel.endpoint(),
  74. child_tunnel.endpoint(),
  75. )
  76. await asyncio.sleep(10)
  77. #
  78. # run tests
  79. #
  80. async with httpx.AsyncClient() as http:
  81. resp = await http.get(f"http://{parent_endpoint}/api/v1/info")
  82. #
  83. # Check that the child was connected
  84. #
  85. jd = resp.json()
  86. assert (
  87. "hosts-available" in jd
  88. ), "Could not find 'host-available' key in api/v1/info"
  89. assert jd["hosts-available"] == 2, "Child did not connect to parent"
  90. #
  91. # Check bearer protection
  92. #
  93. forbidden_urls = [
  94. f"http://{parent_endpoint}/api/v2/bearer_protection",
  95. f"http://{parent_endpoint}/api/v2/bearer_get_token",
  96. ]
  97. for url in forbidden_urls:
  98. async with httpx.AsyncClient() as http:
  99. resp = await http.get(url)
  100. assert (
  101. resp.status_code == httpx.codes.UNAVAILABLE_FOR_LEGAL_REASONS
  102. ), "Bearer protection is broken"
  103. @click.command(help="Run a simple parent/child test")
  104. def test():
  105. simple_test()