test_reconnect.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. import copy
  3. import platform
  4. from time import sleep
  5. import pytest
  6. from flaky import flaky
  7. from conftest import CfdModes
  8. from constants import protocols
  9. from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected
  10. @flaky(max_runs=3, min_passes=1)
  11. class TestReconnect:
  12. default_ha_conns = 1
  13. default_reconnect_secs = 15
  14. extra_config = {
  15. "stdin-control": True,
  16. }
  17. def _extra_config(self, protocol):
  18. return {
  19. "stdin-control": True,
  20. "protocol": protocol,
  21. }
  22. @pytest.mark.skipif(platform.system() == "Windows", reason=f"Currently buggy on Windows TUN-4584")
  23. @pytest.mark.parametrize("protocol", protocols())
  24. def test_named_reconnect(self, tmp_path, component_tests_config, protocol):
  25. config = component_tests_config(self._extra_config(protocol))
  26. with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, allow_input=True, capture_output=False) as cloudflared:
  27. # Repeat the test multiple times because some issues only occur after multiple reconnects
  28. self.assert_reconnect(config, cloudflared, 5)
  29. def send_reconnect(self, cloudflared, secs):
  30. # Although it is recommended to use the Popen.communicate method, we cannot
  31. # use it because it blocks on reading stdout and stderr until EOF is reached
  32. cloudflared.stdin.write(f"reconnect {secs}s\n".encode())
  33. cloudflared.stdin.flush()
  34. def assert_reconnect(self, config, cloudflared, repeat):
  35. wait_tunnel_ready(tunnel_url=config.get_url(),
  36. require_min_connections=self.default_ha_conns)
  37. for _ in range(repeat):
  38. for _ in range(self.default_ha_conns):
  39. self.send_reconnect(cloudflared, self.default_reconnect_secs)
  40. check_tunnel_not_connected()
  41. sleep(self.default_reconnect_secs * 2)
  42. wait_tunnel_ready(tunnel_url=config.get_url(),
  43. require_min_connections=self.default_ha_conns)