cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cd4af5696d0db81caae903b33e4bbddfb5aff9ff

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

component-tests/test_reconnect.py

55lines · modecode

1#!/usr/bin/env python
2import copy
3import platform
4from time import sleep
5
6import pytest
7from flaky import flaky
8
9from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected
10
11
12@flaky(max_runs=3, min_passes=1)
13class TestReconnect:
14 default_ha_conns = 4
15 default_reconnect_secs = 15
16 extra_config = {
17 "stdin-control": True,
18 }
19
20 @pytest.mark.skipif(platform.system() == "Windows", reason=f"Currently buggy on Windows TUN-4584")
21 def test_named_reconnect(self, tmp_path, component_tests_config):
22 config = component_tests_config(self.extra_config)
23 with start_cloudflared(tmp_path, config, new_process=True, allow_input=True, capture_output=False) as cloudflared:
24 # Repeat the test multiple times because some issues only occur after multiple reconnects
25 self.assert_reconnect(config, cloudflared, 5)
26
27 def test_classic_reconnect(self, tmp_path, component_tests_config):
28 extra_config = copy.copy(self.extra_config)
29 extra_config["hello-world"] = True
30 config = component_tests_config(
31 additional_config=extra_config, named_tunnel=False)
32 with start_cloudflared(tmp_path, config, cfd_args=[], new_process=True, allow_input=True, capture_output=False) as cloudflared:
33 self.assert_reconnect(config, cloudflared, 1)
34
35 def send_reconnect(self, cloudflared, secs):
36 # Although it is recommended to use the Popen.communicate method, we cannot
37 # use it because it blocks on reading stdout and stderr until EOF is reached
38 cloudflared.stdin.write(f"reconnect {secs}s\n".encode())
39 cloudflared.stdin.flush()
40
41 def assert_reconnect(self, config, cloudflared, repeat):
42 wait_tunnel_ready(tunnel_url=config.get_url(), require_min_connections=self.default_ha_conns)
43 for _ in range(repeat):
44 for i in range(self.default_ha_conns):
45 self.send_reconnect(cloudflared, self.default_reconnect_secs)
46 expect_connections = self.default_ha_conns-i-1
47 if expect_connections > 0:
48 # Don't check if tunnel returns 200 here because there is a race condition between wait_tunnel_ready
49 # retrying to get 200 response and reconnecting
50 wait_tunnel_ready(require_min_connections=expect_connections)
51 else:
52 check_tunnel_not_connected()
53
54 sleep(self.default_reconnect_secs * 2)
55 wait_tunnel_ready(tunnel_url=config.get_url(), require_min_connections=self.default_ha_conns)
56