test_tunnel.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/env python
  2. import requests
  3. from conftest import CfdModes
  4. from constants import METRICS_PORT, MAX_RETRIES, BACKOFF_SECS
  5. from retrying import retry
  6. from cli import CloudflaredCli
  7. from util import LOGGER, write_config, start_cloudflared, wait_tunnel_ready, send_requests
  8. import platform
  9. class TestTunnel:
  10. '''Test tunnels with no ingress rules from config.yaml but ingress rules from CLI only'''
  11. def test_tunnel_hello_world(self, tmp_path, component_tests_config):
  12. config = component_tests_config(cfd_mode=CfdModes.NAMED, run_proxy_dns=False, provide_ingress=False)
  13. LOGGER.debug(config)
  14. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], cfd_args=["run", "--hello-world"], new_process=True):
  15. wait_tunnel_ready(tunnel_url=config.get_url(),
  16. require_min_connections=1)
  17. def test_tunnel_url(self, tmp_path, component_tests_config):
  18. config = component_tests_config(cfd_mode=CfdModes.NAMED, run_proxy_dns=False, provide_ingress=False)
  19. LOGGER.debug(config)
  20. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], cfd_args=["run", "--url", f"http://localhost:{METRICS_PORT}/"], new_process=True):
  21. wait_tunnel_ready(require_min_connections=1)
  22. send_requests(config.get_url()+"/ready", 3, True)
  23. def test_tunnel_no_ingress(self, tmp_path, component_tests_config):
  24. '''
  25. Running a tunnel with no ingress rules provided from either config.yaml or CLI will still work but return 503
  26. for all incoming requests.
  27. '''
  28. config = component_tests_config(cfd_mode=CfdModes.NAMED, run_proxy_dns=False, provide_ingress=False)
  29. LOGGER.debug(config)
  30. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], cfd_args=["run"], new_process=True):
  31. wait_tunnel_ready(require_min_connections=1)
  32. resp = send_request(config.get_url()+"/")
  33. assert resp.status_code == 503, "Expected cloudflared to return 503 for all requests with no ingress defined"
  34. resp = send_request(config.get_url()+"/test")
  35. assert resp.status_code == 503, "Expected cloudflared to return 503 for all requests with no ingress defined"
  36. @retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=BACKOFF_SECS * 1000)
  37. def send_request(url, headers={}):
  38. with requests.Session() as s:
  39. return s.get(url, timeout=BACKOFF_SECS, headers=headers)