Files
unslothai--unsloth/unsloth_cli/tests/test_studio_cloudflare_flag.py
T
wehub-resource-sync e93507a09c
Lockfile supply-chain audit / lockfile supply-chain audit (push) Has been cancelled
Windows Studio GGUF CI / GPU prebuilt resolves without Visual Studio (push) Has been cancelled
Windows Studio GGUF CI / setup.ps1 unit tests (VS 2026 / CMake guard) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2022) (push) Has been cancelled
Windows Studio GGUF CI / real-VS detection (VS 2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-2025-vs2026) (push) Has been cancelled
Windows Studio GGUF CI / VC++ runtime detect + install round-trip (windows-latest) (push) Has been cancelled
Windows Studio Update CI / Studio Updating Tests (push) Has been cancelled
Wheel CI / Wheel build + content sanity + import smoke (push) Has been cancelled
Lint CI / Source lint (Python + shell + YAML + JSON + safety nets) (push) Has been cancelled
MLX CI on Mac M1 / dispatch (push) Has been cancelled
Security audit / advisory audit (pip + npm + cargo) (push) Has been cancelled
Security audit / pip scan-packages :: extras (push) Has been cancelled
Security audit / pip scan-packages :: studio (push) Has been cancelled
Security audit / pip scan-packages :: hf-stack (push) Has been cancelled
Security audit / npm scan-packages (Studio frontend tarballs) (push) Has been cancelled
Security audit / workflow-trigger lint (pull_request_target / cache-poisoning) (push) Has been cancelled
Security audit / pytest tests/security (push) Has been cancelled
Security audit / npm provenance + new install-script diff (push) Has been cancelled
Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Backend CI / (Python 3.10) (push) Has been cancelled
Backend CI / (Python 3.11) (push) Has been cancelled
Backend CI / (Python 3.12) (push) Has been cancelled
Backend CI / (Python 3.13) (push) Has been cancelled
Backend CI / Repo tests (CPU) (push) Has been cancelled
Frontend CI / Frontend build + bundle sanity (push) Has been cancelled
Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Mac Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Mac Studio GGUF CI / JSON, images (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-14) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26) (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-15-intel) (push) Has been cancelled
Mac Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Mac Studio Install Matrix CI / Install + load (macos-26-intel) (push) Has been cancelled
Mac Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Tauri CI / Tauri Linux debug build (no codesign) (push) Has been cancelled
Mac Studio Update CI / Studio Updating Tests (push) Has been cancelled
Studio UI CI / Chat UI Tests (push) Has been cancelled
Windows Studio API CI / Studio API & Auth Tests (push) Has been cancelled
Windows Studio UI CI / Chat UI Tests (push) Has been cancelled
Studio Update CI / Studio Updating Tests (push) Has been cancelled
Core / Core (HF=default + TRL=default) (push) Has been cancelled
Core / Core (HF=4.57.6 + TRL<1) (push) Has been cancelled
Core / Core (HF=latest + TRL=latest) (push) Has been cancelled
Core / llama.cpp build + smoke (push) Has been cancelled
Windows Studio GGUF CI / OpenAI, Anthropic API tests (push) Has been cancelled
Windows Studio GGUF CI / Tool calling Tests (push) Has been cancelled
Windows Studio GGUF CI / JSON, images (push) Has been cancelled
Windows Studio GGUF CI / Studio install + inference without Visual Studio (push) Has been cancelled
Studio export capability / capability (macos-latest) (push) Has been cancelled
Studio export capability / capability (ubuntu-latest) (push) Has been cancelled
Studio export capability / capability (windows-latest) (push) Has been cancelled
Cross-platform parity / parity (macos-latest) (push) Has been cancelled
Cross-platform parity / parity (windows-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
Studio load-orchestrator CI / test (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:59:56 +08:00

477 lines
16 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
"""Tests for the `--cloudflare/--no-cloudflare` Studio flag.
Pins the typer Option (default on) on both `unsloth studio` and
`unsloth studio run`, and that the chosen polarity reaches the re-exec'd
child and run_server. Modeled on test_studio_run_parallel_flag.py.
"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
from typer.testing import CliRunner
_REPO_ROOT = Path(__file__).resolve().parents[2]
if str(_REPO_ROOT) not in sys.path:
sys.path.insert(0, str(_REPO_ROOT))
def _studio():
from unsloth_cli.commands import studio as _studio_mod
return _studio_mod
_BASE = ["--model", "unsloth/Qwen3-1.7B-GGUF"]
# ── option registration ──────────────────────────────────────────────
def test_run_exposes_cloudflare_option_default_on():
import inspect
sig = inspect.signature(_studio().run)
assert "cloudflare" in sig.parameters
opt = sig.parameters["cloudflare"].default
decls = set(getattr(opt, "param_decls", []) or [])
assert "--cloudflare/--no-cloudflare" in decls
assert getattr(opt, "default", None) is True
def test_studio_default_exposes_cloudflare_option_default_on():
import inspect
sig = inspect.signature(_studio().studio_default)
assert "cloudflare" in sig.parameters
opt = sig.parameters["cloudflare"].default
assert getattr(opt, "default", None) is True
# ── re-exec forwarding: `unsloth studio run` ─────────────────────────
class _ExecCaptured(SystemExit):
def __init__(self, argv):
super().__init__(0)
self.argv = list(argv)
def _install_run_reexec_capture(monkeypatch, *, platform = "linux"):
studio_mod = _studio()
captured = []
monkeypatch.setattr(sys, "prefix", "/nonexistent/outer/venv")
fake_venv = Path("/fake/studio/venv/unsloth_studio")
monkeypatch.setattr(studio_mod, "_studio_venv_python", lambda: fake_venv / "bin" / "python")
fake_bin = fake_venv / "bin" / "unsloth"
real_is_file = Path.is_file
monkeypatch.setattr(
Path,
"is_file",
lambda self: True if str(self) == str(fake_bin) else real_is_file(self),
)
from unsloth_cli import _tool_policy as _tp_mod
monkeypatch.setattr(
_tp_mod,
"resolve_tool_policy",
lambda host, flag, yes, silent: False if flag is None else bool(flag),
)
monkeypatch.setattr(sys, "platform", platform)
def fake_execvp(file, argv):
captured.append(list(argv))
raise _ExecCaptured(argv)
monkeypatch.setattr(studio_mod.os, "execvp", fake_execvp)
return captured
def _invoke_run(monkeypatch, args):
import typer as _typer
studio_mod = _studio()
captured = _install_run_reexec_capture(monkeypatch)
app = _typer.Typer()
app.command(
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
)(studio_mod.run)
CliRunner().invoke(app, args, catch_exceptions = True)
return captured
@pytest.mark.parametrize(
"user_flag,expected,unexpected",
[
(None, "--cloudflare", "--no-cloudflare"), # default on
("--cloudflare", "--cloudflare", "--no-cloudflare"),
("--no-cloudflare", "--no-cloudflare", "--cloudflare"),
],
)
def test_run_reexec_forwards_cloudflare_polarity(monkeypatch, user_flag, expected, unexpected):
extras = [user_flag] if user_flag else []
captured = _invoke_run(monkeypatch, _BASE + extras)
assert len(captured) == 1, captured
argv = captured[0]
assert expected in argv, f"expected {expected} in child argv; got {argv}"
assert unexpected not in argv, f"unexpected {unexpected} in child argv; got {argv}"
# ── re-exec forwarding: plain `unsloth studio` ───────────────────────
def _invoke_studio_default(
monkeypatch,
args,
*,
platform = "linux",
):
import typer as _typer
studio_mod = _studio()
captured = []
monkeypatch.setattr(sys, "prefix", "/nonexistent/outer/venv")
monkeypatch.setattr(studio_mod, "_ensure_studio_env_exported", lambda: None)
fake_venv = Path("/fake/studio/venv/unsloth_studio")
monkeypatch.setattr(studio_mod, "_studio_venv_python", lambda: fake_venv / "bin" / "python")
monkeypatch.setattr(studio_mod, "_find_run_py", lambda: Path("/fake/studio/run.py"))
monkeypatch.setattr(studio_mod, "_find_frontend_dist", lambda: None)
monkeypatch.setattr(sys, "platform", platform)
def fake_execvp(file, argv):
captured.append(list(argv))
raise _ExecCaptured(argv)
monkeypatch.setattr(studio_mod.os, "execvp", fake_execvp)
app = _typer.Typer()
app.command()(studio_mod.studio_default)
CliRunner().invoke(app, args, catch_exceptions = True)
return captured
@pytest.mark.parametrize(
"user_flag,expected,unexpected",
[
(None, "--cloudflare", "--no-cloudflare"),
("--no-cloudflare", "--no-cloudflare", "--cloudflare"),
],
)
def test_studio_default_reexec_forwards_cloudflare(monkeypatch, user_flag, expected, unexpected):
extras = [user_flag] if user_flag else []
captured = _invoke_studio_default(monkeypatch, ["-H", "0.0.0.0"] + extras)
assert len(captured) == 1, captured
argv = captured[0]
assert expected in argv, f"expected {expected}; got {argv}"
assert unexpected not in argv, f"unexpected {unexpected}; got {argv}"
# ── in-venv path forwards cloudflare into run_server ─────────────────
class _RunServerCaptured(SystemExit):
def __init__(self, kwargs):
super().__init__(0)
self.kwargs = dict(kwargs)
@pytest.mark.parametrize("user_flag,expected", [(None, True), ("--no-cloudflare", False)])
def test_run_in_venv_passes_cloudflare_to_run_server(monkeypatch, user_flag, expected):
import types
studio_mod = _studio()
fake_venv = Path("/fake/studio/venv/unsloth_studio")
monkeypatch.setattr(sys, "prefix", str(fake_venv))
monkeypatch.setattr(studio_mod, "STUDIO_HOME", fake_venv.parent)
from unsloth_cli import _tool_policy as _tp_mod
monkeypatch.setattr(
_tp_mod,
"resolve_tool_policy",
lambda host, flag, yes, silent: False if flag is None else bool(flag),
)
captured: dict = {}
def fake_run_server(**kwargs):
captured.update(kwargs)
raise _RunServerCaptured(kwargs)
fake_backend_run = sys.modules.setdefault(
"studio.backend.run", types.ModuleType("studio.backend.run")
)
fake_backend_run.run_server = fake_run_server
fake_backend_run._resolve_external_ip = lambda: "127.0.0.1"
# run() loads the backend via _load_run_module() (by file path); inject the
# mock as the cached run module so the stubbed run_server is used.
monkeypatch.setattr(studio_mod, "_RUN_MODULE", fake_backend_run)
state_mod = types.ModuleType("state")
tp_mod = types.ModuleType("state.tool_policy")
tp_mod.set_tool_policy = lambda *a, **k: None
state_mod.tool_policy = tp_mod
monkeypatch.setitem(sys.modules, "state", state_mod)
monkeypatch.setitem(sys.modules, "state.tool_policy", tp_mod)
import typer as _typer
app = _typer.Typer()
app.command(
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
)(studio_mod.run)
extras = [user_flag] if user_flag else []
CliRunner().invoke(app, _BASE + extras, catch_exceptions = True)
assert captured.get("cloudflare") is expected, captured
def test_run_display_host_and_url_helpers_cover_ipv6_wildcard():
import types
studio_mod = _studio()
run_mod = types.SimpleNamespace(_resolve_external_ip = lambda: "198.51.100.7")
assert studio_mod._display_host_for_bind(run_mod, "0.0.0.0") == "198.51.100.7"
assert studio_mod._display_host_for_bind(run_mod, "::") == "198.51.100.7"
assert studio_mod._url_host("2001:db8::7") == "[2001:db8::7]"
assert studio_mod._url_host("127.0.0.1") == "127.0.0.1"
def test_run_cloudflare_notice_uses_external_host_policy():
import types
studio_mod = _studio()
calls = []
run_mod = types.SimpleNamespace(
_verify_global_reachability = lambda host, port: calls.append(("verify", host, port)),
_print_cloudflare_line = lambda **kw: calls.append(("print", kw)),
)
studio_mod._emit_run_cloudflare_notice(run_mod, "0.0.0.0", "198.51.100.7", 8888, False)
assert calls == [
("verify", "198.51.100.7", 8888),
("print", {"secure": False, "loopback_host": "127.0.0.1"}),
]
calls.clear()
studio_mod._emit_run_cloudflare_notice(run_mod, "::", "198.51.100.7", 8888, False)
assert calls == [
("verify", "198.51.100.7", 8888),
("print", {"secure": False, "loopback_host": "::1"}),
]
calls.clear()
studio_mod._emit_run_cloudflare_notice(run_mod, "127.0.0.1", "127.0.0.1", 8888, False)
assert calls == []
def test_run_silent_emits_cloudflare_notice_for_external_bind(monkeypatch):
import types
studio_mod = _studio()
fake_venv = Path("/fake/studio/venv/unsloth_studio")
monkeypatch.setattr(sys, "prefix", str(fake_venv))
monkeypatch.setattr(studio_mod, "STUDIO_HOME", fake_venv.parent)
from unsloth_cli import _tool_policy as _tp_mod
monkeypatch.setattr(
_tp_mod,
"resolve_tool_policy",
lambda host, flag, yes, silent: False if flag is None else bool(flag),
)
class _Done(SystemExit):
pass
class _ShutdownEvent:
def is_set(self):
raise _Done(0)
def wait(self, timeout = None):
return None
class _App:
class state:
server_port = 8888
cloudflare_url = "https://x.trycloudflare.com"
calls = []
backend = types.ModuleType("studio.backend.run")
backend.run_server = lambda **_kwargs: _App()
backend._resolve_external_ip = lambda: "198.51.100.7"
backend._verify_global_reachability = lambda host, port: calls.append(("verify", host, port))
backend._print_cloudflare_line = lambda **kw: calls.append(("print", kw))
backend._server = object()
backend._shutdown_event = _ShutdownEvent()
backend._graceful_shutdown = lambda server: calls.append(("shutdown", server))
monkeypatch.setitem(sys.modules, "studio.backend.run", backend)
monkeypatch.setattr(studio_mod, "_RUN_MODULE", backend)
state_mod = types.ModuleType("state")
tp_mod = types.ModuleType("state.tool_policy")
tp_mod.set_tool_policy = lambda *a, **k: None
state_mod.tool_policy = tp_mod
monkeypatch.setitem(sys.modules, "state", state_mod)
monkeypatch.setitem(sys.modules, "state.tool_policy", tp_mod)
monkeypatch.setattr(studio_mod, "_wait_for_server", lambda *a, **k: True)
monkeypatch.setattr(studio_mod, "_create_api_key_inprocess", lambda name: "sk-test")
monkeypatch.setattr(
studio_mod,
"_load_model_via_http",
lambda **_kwargs: {"model": "unsloth/Qwen3-1.7B-GGUF", "context_length": 4096},
)
import typer as _typer
app = _typer.Typer()
app.command(
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
)(studio_mod.run)
result = CliRunner().invoke(
app,
_BASE + ["--silent", "-H", "0.0.0.0"],
catch_exceptions = True,
)
assert result.exit_code == 0, result.output
assert ("verify", "198.51.100.7", 8888) in calls
assert ("print", {"secure": False, "loopback_host": "127.0.0.1"}) in calls
# ── parent-level --no-cloudflare with a subcommand is rejected ───────
def test_studio_default_rejects_no_cloudflare_with_subcommand(monkeypatch):
# `unsloth studio --no-cloudflare run ...` would not reach the subcommand,
# so it must error (mirrors --parallel) rather than silently still tunnel.
import typer as _typer
studio_mod = _studio()
app = _typer.Typer()
app.add_typer(studio_mod.studio_app, name = "studio")
result = CliRunner().invoke(app, ["studio", "--no-cloudflare", "run", "--model", "X"])
assert result.exit_code == 2, result.output
combined = (result.output or "") + (getattr(result, "stderr", "") or "")
assert "--no-cloudflare" in combined, combined
# ── run() tears the server + tunnel down if startup aborts ───────────
def test_run_in_venv_shuts_down_on_startup_abort(monkeypatch):
import types
studio_mod = _studio()
fake_venv = Path("/fake/studio/venv/unsloth_studio")
monkeypatch.setattr(sys, "prefix", str(fake_venv))
monkeypatch.setattr(studio_mod, "STUDIO_HOME", fake_venv.parent)
from unsloth_cli import _tool_policy as _tp_mod
monkeypatch.setattr(
_tp_mod,
"resolve_tool_policy",
lambda host, flag, yes, silent: False if flag is None else bool(flag),
)
class _App:
class state:
server_port = 8888
shutdown_calls = []
backend = sys.modules.setdefault("studio.backend.run", types.ModuleType("studio.backend.run"))
backend.run_server = lambda **k: _App()
backend._resolve_external_ip = lambda: "1.2.3.4"
backend._server = object()
backend._shutdown_event = None
backend._graceful_shutdown = lambda server: shutdown_calls.append(server)
# run() loads the backend via _load_run_module() (by file path); inject the
# mock as the cached run module so the stubbed symbols are used.
monkeypatch.setattr(studio_mod, "_RUN_MODULE", backend)
# set_tool_policy is imported as `from state.tool_policy import set_tool_policy`.
state_mod = sys.modules.setdefault("state", types.ModuleType("state"))
tp_mod = sys.modules.setdefault("state.tool_policy", types.ModuleType("state.tool_policy"))
tp_mod.set_tool_policy = lambda *a, **k: None
state_mod.tool_policy = tp_mod
# Force the health check to fail so startup aborts after run_server().
monkeypatch.setattr(studio_mod, "_wait_for_server", lambda *a, **k: False)
import typer as _typer
app = _typer.Typer()
app.command(
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
)(studio_mod.run)
result = CliRunner().invoke(app, _BASE + ["-H", "0.0.0.0"], catch_exceptions = True)
assert result.exit_code == 1, result.output
assert len(shutdown_calls) == 1, "startup abort must call _graceful_shutdown"
def test_run_in_venv_sets_tool_policy_before_server_start(monkeypatch):
import types
studio_mod = _studio()
fake_venv = Path("/fake/studio/venv/unsloth_studio")
monkeypatch.setattr(sys, "prefix", str(fake_venv))
monkeypatch.setattr(studio_mod, "STUDIO_HOME", fake_venv.parent)
from unsloth_cli import _tool_policy as _tp_mod
monkeypatch.setattr(
_tp_mod,
"resolve_tool_policy",
lambda host, flag, yes, silent: False,
)
calls = []
class _App:
class state:
server_port = 8888
def _run_server(**_kwargs):
calls.append(("run_server", None))
return _App()
backend = types.ModuleType("studio.backend.run")
backend.run_server = _run_server
backend._resolve_external_ip = lambda: "1.2.3.4"
backend._server = object()
backend._shutdown_event = None
backend._graceful_shutdown = lambda server: calls.append(("shutdown", server))
monkeypatch.setitem(sys.modules, "studio.backend.run", backend)
monkeypatch.setattr(studio_mod, "_RUN_MODULE", backend)
state_mod = types.ModuleType("state")
tp_mod = types.ModuleType("state.tool_policy")
tp_mod.set_tool_policy = lambda value: calls.append(("policy", value))
state_mod.tool_policy = tp_mod
monkeypatch.setitem(sys.modules, "state", state_mod)
monkeypatch.setitem(sys.modules, "state.tool_policy", tp_mod)
monkeypatch.setattr(studio_mod, "_wait_for_server", lambda *a, **k: False)
import typer as _typer
app = _typer.Typer()
app.command(
context_settings = {"allow_extra_args": True, "ignore_unknown_options": True},
)(studio_mod.run)
result = CliRunner().invoke(app, _BASE + ["--disable-tools"], catch_exceptions = True)
assert result.exit_code == 1, result.output
assert calls[:2] == [("policy", False), ("run_server", None)]