cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
component-tests/test_reconnect.py
50lines · modecode
| 1 | #!/usr/bin/env python |
| 2 | import copy |
| 3 | |
| 4 | from retrying import retry |
| 5 | from time import sleep |
| 6 | |
| 7 | from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_ready, send_requests |
| 8 | |
| 9 | |
| 10 | class TestReconnect(): |
| 11 | default_ha_conns = 4 |
| 12 | default_reconnect_secs = 5 |
| 13 | extra_config = { |
| 14 | "stdin-control": True, |
| 15 | } |
| 16 | |
| 17 | def test_named_reconnect(self, tmp_path, component_tests_config): |
| 18 | config = component_tests_config(self.extra_config) |
| 19 | with start_cloudflared(tmp_path, config, new_process=True, allow_input=True) as cloudflared: |
| 20 | # Repeat the test multiple times because some issues only occur after multiple reconnects |
| 21 | self.assert_reconnect(config, cloudflared, 5) |
| 22 | |
| 23 | def test_classic_reconnect(self, tmp_path, component_tests_config): |
| 24 | extra_config = copy.copy(self.extra_config) |
| 25 | extra_config["hello-world"] = True |
| 26 | config = component_tests_config( |
| 27 | additional_config=extra_config, named_tunnel=False) |
| 28 | with start_cloudflared(tmp_path, config, cfd_args=[], new_process=True, allow_input=True) as cloudflared: |
| 29 | self.assert_reconnect(config, cloudflared, 1) |
| 30 | |
| 31 | def send_reconnect(self, cloudflared, secs): |
| 32 | # Although it is recommended to use the Popen.communicate method, we cannot |
| 33 | # use it because it blocks on reading stdout and stderr until EOF is reached |
| 34 | cloudflared.stdin.write(f"reconnect {secs}s\n".encode()) |
| 35 | cloudflared.stdin.flush() |
| 36 | |
| 37 | def assert_reconnect(self, config, cloudflared, repeat): |
| 38 | wait_tunnel_ready() |
| 39 | for _ in range(repeat): |
| 40 | for i in range(self.default_ha_conns): |
| 41 | self.send_reconnect(cloudflared, self.default_reconnect_secs) |
| 42 | expect_connections = self.default_ha_conns-i-1 |
| 43 | if expect_connections > 0: |
| 44 | wait_tunnel_ready(expect_connections=expect_connections) |
| 45 | else: |
| 46 | check_tunnel_not_ready() |
| 47 | |
| 48 | sleep(self.default_reconnect_secs + 10) |
| 49 | wait_tunnel_ready() |
| 50 | send_requests(config.get_url(), 1) |
| 51 | |