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-09-10 15:47:36 +00:00
|
|
|
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-09-10 15:47:36 +00:00
|
|
|
with self.within_grace_period():
|
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-09-10 15:47:36 +00:00
|
|
|
with self.within_grace_period():
|
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-09-10 15:47:36 +00:00
|
|
|
def within_grace_period(self):
|
2021-03-11 20:43:31 +00:00
|
|
|
try:
|
|
|
|
start = time.time()
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
duration = time.time() - start
|
2024-09-10 15:47:36 +00:00
|
|
|
assert 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)
|