2021-03-11 13:49:09 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
import copy
|
2021-06-18 11:21:11 +00:00
|
|
|
import platform
|
2021-03-11 13:49:09 +00:00
|
|
|
from time import sleep
|
|
|
|
|
2021-06-18 11:21:11 +00:00
|
|
|
import pytest
|
2021-03-24 12:52:01 +00:00
|
|
|
from flaky import flaky
|
|
|
|
|
2021-07-30 16:44:11 +00:00
|
|
|
from conftest import CfdModes
|
2022-01-04 19:20:43 +00:00
|
|
|
from constants import protocols
|
2021-03-24 12:52:01 +00:00
|
|
|
from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected
|
2021-03-11 13:49:09 +00:00
|
|
|
|
|
|
|
|
2021-03-16 13:55:41 +00:00
|
|
|
@flaky(max_runs=3, min_passes=1)
|
2021-06-08 10:01:53 +00:00
|
|
|
class TestReconnect:
|
2023-04-19 11:41:01 +00:00
|
|
|
default_ha_conns = 1
|
2021-06-08 10:01:53 +00:00
|
|
|
default_reconnect_secs = 15
|
2021-03-11 13:49:09 +00:00
|
|
|
extra_config = {
|
|
|
|
"stdin-control": True,
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:20:43 +00:00
|
|
|
def _extra_config(self, protocol):
|
|
|
|
return {
|
|
|
|
"stdin-control": True,
|
|
|
|
"protocol": protocol,
|
|
|
|
}
|
|
|
|
|
2021-06-18 11:21:11 +00:00
|
|
|
@pytest.mark.skipif(platform.system() == "Windows", reason=f"Currently buggy on Windows TUN-4584")
|
2022-01-04 19:20:43 +00:00
|
|
|
@pytest.mark.parametrize("protocol", protocols())
|
|
|
|
def test_named_reconnect(self, tmp_path, component_tests_config, protocol):
|
|
|
|
config = component_tests_config(self._extra_config(protocol))
|
2023-04-19 11:41:01 +00:00
|
|
|
with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, allow_input=True, capture_output=False) as cloudflared:
|
2021-03-11 13:49:09 +00:00
|
|
|
# Repeat the test multiple times because some issues only occur after multiple reconnects
|
|
|
|
self.assert_reconnect(config, cloudflared, 5)
|
|
|
|
|
|
|
|
def send_reconnect(self, cloudflared, secs):
|
|
|
|
# Although it is recommended to use the Popen.communicate method, we cannot
|
|
|
|
# use it because it blocks on reading stdout and stderr until EOF is reached
|
|
|
|
cloudflared.stdin.write(f"reconnect {secs}s\n".encode())
|
|
|
|
cloudflared.stdin.flush()
|
|
|
|
|
|
|
|
def assert_reconnect(self, config, cloudflared, repeat):
|
2022-08-31 19:52:44 +00:00
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url(),
|
|
|
|
require_min_connections=self.default_ha_conns)
|
2021-03-11 13:49:09 +00:00
|
|
|
for _ in range(repeat):
|
2022-08-31 19:52:44 +00:00
|
|
|
for _ in range(self.default_ha_conns):
|
2021-03-11 13:49:09 +00:00
|
|
|
self.send_reconnect(cloudflared, self.default_reconnect_secs)
|
2022-08-31 19:52:44 +00:00
|
|
|
check_tunnel_not_connected()
|
2021-06-08 10:01:53 +00:00
|
|
|
sleep(self.default_reconnect_secs * 2)
|
2022-08-31 19:52:44 +00:00
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url(),
|
|
|
|
require_min_connections=self.default_ha_conns)
|