test_proxy_dns.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/env python
  2. import socket
  3. from time import sleep
  4. import constants
  5. from conftest import CfdModes
  6. from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected
  7. # Sanity checks that test that we only run Proxy DNS and Tunnel when we really expect them to be there.
  8. class TestProxyDns:
  9. def test_proxy_dns_with_named_tunnel(self, tmp_path, component_tests_config):
  10. run_test_scenario(tmp_path, component_tests_config, CfdModes.NAMED, run_proxy_dns=True)
  11. def test_proxy_dns_alone(self, tmp_path, component_tests_config):
  12. run_test_scenario(tmp_path, component_tests_config, CfdModes.PROXY_DNS, run_proxy_dns=True)
  13. def test_named_tunnel_alone(self, tmp_path, component_tests_config):
  14. run_test_scenario(tmp_path, component_tests_config, CfdModes.NAMED, run_proxy_dns=False)
  15. def run_test_scenario(tmp_path, component_tests_config, cfd_mode, run_proxy_dns):
  16. expect_proxy_dns = run_proxy_dns
  17. expect_tunnel = False
  18. if cfd_mode == CfdModes.NAMED:
  19. expect_tunnel = True
  20. pre_args = ["tunnel", "--ha-connections", "1"]
  21. args = ["run"]
  22. elif cfd_mode == CfdModes.PROXY_DNS:
  23. expect_proxy_dns = True
  24. pre_args = []
  25. args = ["proxy-dns", "--port", str(constants.PROXY_DNS_PORT)]
  26. else:
  27. assert False, f"Unknown cfd_mode {cfd_mode}"
  28. config = component_tests_config(cfd_mode=cfd_mode, run_proxy_dns=run_proxy_dns)
  29. with start_cloudflared(tmp_path, config, cfd_pre_args=pre_args, cfd_args=args, new_process=True, capture_output=False):
  30. if expect_tunnel:
  31. wait_tunnel_ready()
  32. else:
  33. check_tunnel_not_connected()
  34. verify_proxy_dns(expect_proxy_dns)
  35. def verify_proxy_dns(should_be_running):
  36. # Wait for the Proxy DNS listener to come up.
  37. sleep(constants.BACKOFF_SECS)
  38. had_failure = False
  39. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  40. try:
  41. sock.connect(('localhost', constants.PROXY_DNS_PORT))
  42. sock.send(b"anything")
  43. except:
  44. if should_be_running:
  45. assert False, "Expected Proxy DNS to be running, but it was not."
  46. had_failure = True
  47. finally:
  48. sock.close()
  49. if not should_be_running and not had_failure:
  50. assert False, "Proxy DNS should not have been running, but it was."