2021-03-11 20:43:31 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from contextlib import contextmanager
|
2021-03-23 17:24:36 +00:00
|
|
|
import platform
|
2021-03-11 20:43:31 +00:00
|
|
|
import signal
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
import pytest
|
|
|
|
import requests
|
|
|
|
|
2022-01-04 19:20:43 +00:00
|
|
|
from constants import protocols
|
2021-03-23 17:24:36 +00:00
|
|
|
from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected
|
|
|
|
|
|
|
|
|
|
|
|
def supported_signals():
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
return [signal.SIGTERM]
|
|
|
|
return [signal.SIGTERM, signal.SIGINT]
|
2021-03-11 20:43:31 +00:00
|
|
|
|
|
|
|
|
2022-01-04 19:20:43 +00:00
|
|
|
class TestTermination:
|
2021-03-11 20:43:31 +00:00
|
|
|
grace_period = 5
|
|
|
|
timeout = 10
|
|
|
|
sse_endpoint = "/sse?freq=1s"
|
|
|
|
|
2022-01-04 19:20:43 +00:00
|
|
|
def _extra_config(self, protocol):
|
|
|
|
return {
|
|
|
|
"grace-period": f"{self.grace_period}s",
|
|
|
|
"protocol": protocol,
|
|
|
|
}
|
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
@pytest.mark.parametrize("signal", supported_signals())
|
2022-01-04 19:20:43 +00:00
|
|
|
@pytest.mark.parametrize("protocol", protocols())
|
|
|
|
def test_graceful_shutdown(self, tmp_path, component_tests_config, signal, protocol):
|
|
|
|
config = component_tests_config(self._extra_config(protocol))
|
2021-03-23 17:24:36 +00:00
|
|
|
with start_cloudflared(
|
2023-04-19 11:41:01 +00:00
|
|
|
tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, capture_output=False) as cloudflared:
|
2021-03-23 17:24:36 +00:00
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url())
|
2021-03-11 20:43:31 +00:00
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
connected = threading.Condition()
|
|
|
|
in_flight_req = threading.Thread(
|
|
|
|
target=self.stream_request, args=(config, connected, False, ))
|
|
|
|
in_flight_req.start()
|
2021-03-11 20:43:31 +00:00
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
with connected:
|
|
|
|
connected.wait(self.timeout)
|
|
|
|
# Send signal after the SSE connection is established
|
2024-08-30 11:51:20 +00:00
|
|
|
with self.within_grace_period():
|
|
|
|
self.terminate_by_signal(cloudflared, signal)
|
|
|
|
self.wait_eyeball_thread(
|
|
|
|
in_flight_req, self.grace_period + self.timeout)
|
2021-03-11 20:43:31 +00:00
|
|
|
|
|
|
|
# test cloudflared terminates before grace period expires when all eyeball
|
|
|
|
# connections are drained
|
2021-03-23 17:24:36 +00:00
|
|
|
@pytest.mark.parametrize("signal", supported_signals())
|
2022-01-04 19:20:43 +00:00
|
|
|
@pytest.mark.parametrize("protocol", protocols())
|
|
|
|
def test_shutdown_once_no_connection(self, tmp_path, component_tests_config, signal, protocol):
|
|
|
|
config = component_tests_config(self._extra_config(protocol))
|
2021-03-23 17:24:36 +00:00
|
|
|
with start_cloudflared(
|
2023-04-19 11:41:01 +00:00
|
|
|
tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, capture_output=False) as cloudflared:
|
2021-03-23 17:24:36 +00:00
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url())
|
2021-03-11 20:43:31 +00:00
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
connected = threading.Condition()
|
|
|
|
in_flight_req = threading.Thread(
|
|
|
|
target=self.stream_request, args=(config, connected, True, ))
|
|
|
|
in_flight_req.start()
|
2021-03-11 20:43:31 +00:00
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
with connected:
|
|
|
|
connected.wait(self.timeout)
|
2024-08-30 11:51:20 +00:00
|
|
|
with self.within_grace_period(has_connection=False):
|
2021-03-23 17:24:36 +00:00
|
|
|
# Send signal after the SSE connection is established
|
|
|
|
self.terminate_by_signal(cloudflared, signal)
|
|
|
|
self.wait_eyeball_thread(in_flight_req, self.grace_period)
|
2021-03-11 20:43:31 +00:00
|
|
|
|
2021-03-23 17:24:36 +00:00
|
|
|
@pytest.mark.parametrize("signal", supported_signals())
|
2022-01-04 19:20:43 +00:00
|
|
|
@pytest.mark.parametrize("protocol", protocols())
|
|
|
|
def test_no_connection_shutdown(self, tmp_path, component_tests_config, signal, protocol):
|
|
|
|
config = component_tests_config(self._extra_config(protocol))
|
2021-03-23 17:24:36 +00:00
|
|
|
with start_cloudflared(
|
2023-04-19 11:41:01 +00:00
|
|
|
tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, capture_output=False) as cloudflared:
|
2021-03-23 17:24:36 +00:00
|
|
|
wait_tunnel_ready(tunnel_url=config.get_url())
|
2024-08-30 11:51:20 +00:00
|
|
|
with self.within_grace_period(has_connection=False):
|
2021-03-23 17:24:36 +00:00
|
|
|
self.terminate_by_signal(cloudflared, signal)
|
2021-03-11 20:43:31 +00:00
|
|
|
|
|
|
|
def terminate_by_signal(self, cloudflared, sig):
|
|
|
|
cloudflared.send_signal(sig)
|
|
|
|
check_tunnel_not_connected()
|
|
|
|
cloudflared.wait()
|
|
|
|
|
|
|
|
def wait_eyeball_thread(self, thread, timeout):
|
|
|
|
thread.join(timeout)
|
|
|
|
assert thread.is_alive() == False, "eyeball thread is still alive"
|
|
|
|
|
|
|
|
# Using this context asserts logic within the context is executed within grace period
|
|
|
|
@contextmanager
|
2024-08-30 11:51:20 +00:00
|
|
|
def within_grace_period(self, has_connection=True):
|
2021-03-11 20:43:31 +00:00
|
|
|
try:
|
|
|
|
start = time.time()
|
|
|
|
yield
|
|
|
|
finally:
|
2024-08-30 11:51:20 +00:00
|
|
|
|
|
|
|
# If the request takes longer than the grace period then we need to wait at most the grace period.
|
|
|
|
# If the request fell within the grace period cloudflared can close earlier, but to ensure that it doesn't
|
|
|
|
# close immediately we add a minimum boundary. If cloudflared shutdown in less than 1s it's likely that
|
|
|
|
# it shutdown as soon as it received SIGINT. The only way cloudflared can close immediately is if it has no
|
|
|
|
# in-flight requests
|
|
|
|
minimum = 1 if has_connection else 0
|
2021-03-11 20:43:31 +00:00
|
|
|
duration = time.time() - start
|
2024-08-30 11:51:20 +00:00
|
|
|
# Here we truncate to ensure that we don't fail on minute differences like 10.1 instead of 10
|
|
|
|
assert minimum <= int(duration) <= self.grace_period
|
2021-03-11 20:43:31 +00:00
|
|
|
|
|
|
|
def stream_request(self, config, connected, early_terminate):
|
|
|
|
expected_terminate_message = "502 Bad Gateway"
|
|
|
|
url = config.get_url() + self.sse_endpoint
|
|
|
|
|
|
|
|
with requests.get(url, timeout=5, stream=True) as resp:
|
|
|
|
with connected:
|
|
|
|
connected.notifyAll()
|
|
|
|
lines = 0
|
|
|
|
for line in resp.iter_lines():
|
|
|
|
if expected_terminate_message.encode() == line:
|
|
|
|
break
|
|
|
|
lines += 1
|
|
|
|
if early_terminate and lines == 2:
|
|
|
|
return
|
|
|
|
# /sse returns count followed by 2 new lines
|
|
|
|
assert lines >= (self.grace_period * 2)
|