2022-09-18 15:17:23 +01:00
|
|
|
# import asyncio
|
2020-06-29 06:55:32 +01:00
|
|
|
import logging
|
|
|
|
import os
|
2022-12-15 09:49:26 +00:00
|
|
|
import sys
|
2022-12-07 12:42:17 +00:00
|
|
|
from asyncio import AbstractEventLoop, sleep
|
2021-12-21 20:56:12 +00:00
|
|
|
from string import ascii_lowercase
|
|
|
|
|
2020-09-27 00:58:36 +01:00
|
|
|
import httpcore
|
2020-06-29 06:55:32 +01:00
|
|
|
import httpx
|
|
|
|
import pytest
|
2022-07-24 20:07:54 +01:00
|
|
|
from pytest import LogCaptureFixture
|
|
|
|
|
2020-06-29 06:55:32 +01:00
|
|
|
from sanic import Sanic
|
2022-12-15 09:49:26 +00:00
|
|
|
from sanic.compat import use_context
|
2022-07-24 20:07:54 +01:00
|
|
|
from sanic.request import Request
|
2020-06-29 06:55:32 +01:00
|
|
|
from sanic.response import text
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
# import platform
|
|
|
|
# import subprocess
|
|
|
|
# import sys
|
|
|
|
|
|
|
|
|
2020-06-29 06:55:32 +01:00
|
|
|
pytestmark = pytest.mark.skipif(os.name != "posix", reason="UNIX only")
|
|
|
|
SOCKPATH = "/tmp/sanictest.sock"
|
|
|
|
SOCKPATH2 = "/tmp/sanictest2.sock"
|
2023-10-25 04:13:13 +01:00
|
|
|
httpx_version = tuple(map(int, httpx.__version__.strip(ascii_lowercase).split(".")))
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
def socket_cleanup():
|
|
|
|
try:
|
|
|
|
os.unlink(SOCKPATH)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
os.unlink(SOCKPATH2)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
# Run test function
|
|
|
|
yield
|
|
|
|
try:
|
|
|
|
os.unlink(SOCKPATH2)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
try:
|
|
|
|
os.unlink(SOCKPATH)
|
|
|
|
except FileNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@pytest.mark.xfail(
|
|
|
|
reason="Flaky Test on Non Linux Infra",
|
|
|
|
)
|
2022-07-24 20:07:54 +01:00
|
|
|
def test_unix_socket_creation(caplog: LogCaptureFixture):
|
2020-06-29 06:55:32 +01:00
|
|
|
from socket import AF_UNIX, socket
|
|
|
|
|
|
|
|
with socket(AF_UNIX) as sock:
|
|
|
|
sock.bind(SOCKPATH)
|
|
|
|
assert os.path.exists(SOCKPATH)
|
|
|
|
ino = os.stat(SOCKPATH).st_ino
|
|
|
|
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2020-06-29 06:55:32 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.after_server_start
|
|
|
|
def running(app: Sanic):
|
2020-06-29 06:55:32 +01:00
|
|
|
assert os.path.exists(SOCKPATH)
|
|
|
|
assert ino != os.stat(SOCKPATH).st_ino
|
|
|
|
app.stop()
|
|
|
|
|
|
|
|
with caplog.at_level(logging.INFO):
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(unix=SOCKPATH, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
assert (
|
|
|
|
"sanic.root",
|
|
|
|
logging.INFO,
|
|
|
|
f"Goin' Fast @ {SOCKPATH} http://...",
|
|
|
|
) in caplog.record_tuples
|
|
|
|
assert not os.path.exists(SOCKPATH)
|
|
|
|
|
|
|
|
|
2022-01-16 07:03:04 +00:00
|
|
|
@pytest.mark.parametrize("path", (".", "no-such-directory/sanictest.sock"))
|
2022-07-24 20:07:54 +01:00
|
|
|
def test_invalid_paths(path: str):
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2022-09-18 15:17:23 +01:00
|
|
|
#
|
2022-01-16 07:03:04 +00:00
|
|
|
with pytest.raises((FileExistsError, FileNotFoundError)):
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(unix=path, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dont_replace_file():
|
|
|
|
with open(SOCKPATH, "w") as f:
|
|
|
|
f.write("File, not socket")
|
|
|
|
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2020-06-29 06:55:32 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.after_server_start
|
|
|
|
def stop(app: Sanic):
|
2020-06-29 06:55:32 +01:00
|
|
|
app.stop()
|
|
|
|
|
|
|
|
with pytest.raises(FileExistsError):
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(unix=SOCKPATH, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_dont_follow_symlink():
|
|
|
|
from socket import AF_UNIX, socket
|
|
|
|
|
|
|
|
with socket(AF_UNIX) as sock:
|
|
|
|
sock.bind(SOCKPATH2)
|
|
|
|
os.symlink(SOCKPATH2, SOCKPATH)
|
|
|
|
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2020-06-29 06:55:32 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.after_server_start
|
|
|
|
def stop(app: Sanic):
|
2020-06-29 06:55:32 +01:00
|
|
|
app.stop()
|
|
|
|
|
|
|
|
with pytest.raises(FileExistsError):
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(unix=SOCKPATH, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_socket_deleted_while_running():
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2020-06-29 06:55:32 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.after_server_start
|
|
|
|
async def hack(app: Sanic):
|
2020-06-29 06:55:32 +01:00
|
|
|
os.unlink(SOCKPATH)
|
|
|
|
app.stop()
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(host="myhost.invalid", unix=SOCKPATH, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_socket_replaced_with_file():
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2020-06-29 06:55:32 +01:00
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.after_server_start
|
|
|
|
async def hack(app: Sanic):
|
2020-06-29 06:55:32 +01:00
|
|
|
os.unlink(SOCKPATH)
|
|
|
|
with open(SOCKPATH, "w") as f:
|
|
|
|
f.write("Not a socket")
|
|
|
|
app.stop()
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(host="myhost.invalid", unix=SOCKPATH, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_unix_connection():
|
2022-06-27 09:19:26 +01:00
|
|
|
app = Sanic(name="test")
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
@app.get("/")
|
2022-07-24 20:07:54 +01:00
|
|
|
def handler(request: Request):
|
2020-06-29 06:55:32 +01:00
|
|
|
return text(f"{request.conn_info.server}")
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
@app.after_server_start
|
|
|
|
async def client(app: Sanic):
|
2021-12-21 20:56:12 +00:00
|
|
|
if httpx_version >= (0, 20):
|
|
|
|
transport = httpx.AsyncHTTPTransport(uds=SOCKPATH)
|
|
|
|
else:
|
|
|
|
transport = httpcore.AsyncConnectionPool(uds=SOCKPATH)
|
2020-06-29 06:55:32 +01:00
|
|
|
try:
|
2020-09-27 00:58:36 +01:00
|
|
|
async with httpx.AsyncClient(transport=transport) as client:
|
2020-06-29 06:55:32 +01:00
|
|
|
r = await client.get("http://myhost.invalid/")
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.text == os.path.abspath(SOCKPATH)
|
|
|
|
finally:
|
|
|
|
app.stop()
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
app.run(host="myhost.invalid", unix=SOCKPATH, single_process=True)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
2022-07-24 20:07:54 +01:00
|
|
|
def handler(request: Request):
|
2020-06-29 06:55:32 +01:00
|
|
|
return text(f"{request.conn_info.server}")
|
|
|
|
|
|
|
|
|
2022-07-24 20:07:54 +01:00
|
|
|
async def client(app: Sanic, loop: AbstractEventLoop):
|
2020-06-29 06:55:32 +01:00
|
|
|
try:
|
2022-12-15 09:49:26 +00:00
|
|
|
transport = httpx.AsyncHTTPTransport(uds=SOCKPATH)
|
|
|
|
async with httpx.AsyncClient(transport=transport) as client:
|
2020-06-29 06:55:32 +01:00
|
|
|
r = await client.get("http://myhost.invalid/")
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.text == os.path.abspath(SOCKPATH)
|
|
|
|
finally:
|
2022-12-07 12:42:17 +00:00
|
|
|
await sleep(0.2)
|
2020-06-29 06:55:32 +01:00
|
|
|
app.stop()
|
|
|
|
|
|
|
|
|
2022-12-15 09:49:26 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform not in ("linux", "darwin"),
|
|
|
|
reason="This test requires fork context",
|
|
|
|
)
|
2020-06-29 06:55:32 +01:00
|
|
|
def test_unix_connection_multiple_workers():
|
2022-12-15 09:49:26 +00:00
|
|
|
with use_context("fork"):
|
|
|
|
app_multi = Sanic(name="test")
|
|
|
|
app_multi.get("/")(handler)
|
|
|
|
app_multi.listener("after_server_start")(client)
|
|
|
|
app_multi.run(host="myhost.invalid", unix=SOCKPATH, workers=2)
|
2020-06-29 06:55:32 +01:00
|
|
|
|
|
|
|
|
2022-09-18 15:17:23 +01:00
|
|
|
# @pytest.mark.xfail(
|
|
|
|
# condition=platform.system() != "Linux",
|
|
|
|
# reason="Flaky Test on Non Linux Infra",
|
|
|
|
# )
|
|
|
|
# async def test_zero_downtime():
|
|
|
|
# """Graceful server termination and socket replacement on restarts"""
|
|
|
|
# from signal import SIGINT
|
|
|
|
# from time import monotonic as current_time
|
|
|
|
|
|
|
|
# async def client():
|
|
|
|
# if httpx_version >= (0, 20):
|
|
|
|
# transport = httpx.AsyncHTTPTransport(uds=SOCKPATH)
|
|
|
|
# else:
|
|
|
|
# transport = httpcore.AsyncConnectionPool(uds=SOCKPATH)
|
|
|
|
# for _ in range(40):
|
|
|
|
# async with httpx.AsyncClient(transport=transport) as client:
|
|
|
|
# r = await client.get("http://localhost/sleep/0.1")
|
|
|
|
# assert r.status_code == 200, r.text
|
|
|
|
# assert r.text == "Slept 0.1 seconds.\n"
|
|
|
|
|
|
|
|
# def spawn():
|
|
|
|
# command = [
|
|
|
|
# sys.executable,
|
|
|
|
# "-m",
|
|
|
|
# "sanic",
|
|
|
|
# "--debug",
|
|
|
|
# "--unix",
|
|
|
|
# SOCKPATH,
|
|
|
|
# "examples.delayed_response.app",
|
|
|
|
# ]
|
|
|
|
# DN = subprocess.DEVNULL
|
|
|
|
# return subprocess.Popen(
|
|
|
|
# command, stdin=DN, stdout=DN, stderr=subprocess.PIPE
|
|
|
|
# )
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# processes = [spawn()]
|
|
|
|
# while not os.path.exists(SOCKPATH):
|
|
|
|
# if processes[0].poll() is not None:
|
|
|
|
# raise Exception(
|
|
|
|
# "Worker did not start properly. "
|
|
|
|
# f"stderr: {processes[0].stderr.read()}"
|
|
|
|
# )
|
|
|
|
# await asyncio.sleep(0.0001)
|
|
|
|
# ino = os.stat(SOCKPATH).st_ino
|
|
|
|
# task = asyncio.get_event_loop().create_task(client())
|
|
|
|
# start_time = current_time()
|
|
|
|
# while current_time() < start_time + 6:
|
|
|
|
# # Start a new one and wait until the socket is replaced
|
|
|
|
# processes.append(spawn())
|
|
|
|
# while ino == os.stat(SOCKPATH).st_ino:
|
|
|
|
# await asyncio.sleep(0.001)
|
|
|
|
# ino = os.stat(SOCKPATH).st_ino
|
|
|
|
# # Graceful termination of the previous one
|
|
|
|
# processes[-2].send_signal(SIGINT)
|
|
|
|
# # Wait until client has completed all requests
|
|
|
|
# await task
|
|
|
|
# processes[-1].send_signal(SIGINT)
|
|
|
|
# for worker in processes:
|
|
|
|
# try:
|
|
|
|
# worker.wait(1.0)
|
|
|
|
# except subprocess.TimeoutExpired:
|
|
|
|
# raise Exception(
|
|
|
|
# f"Worker would not terminate:\n{worker.stderr}"
|
|
|
|
# )
|
|
|
|
# finally:
|
|
|
|
# for worker in processes:
|
|
|
|
# worker.kill()
|
|
|
|
# # Test for clean run and termination
|
|
|
|
# return_codes = [worker.poll() for worker in processes]
|
|
|
|
|
|
|
|
# # Removing last process which seems to be flappy
|
|
|
|
# return_codes.pop()
|
|
|
|
# assert len(processes) > 5
|
|
|
|
# assert all(code == 0 for code in return_codes)
|
|
|
|
|
|
|
|
# # Removing this check that seems to be flappy
|
|
|
|
# # assert not os.path.exists(SOCKPATH)
|