feat(cli): add remote build support for langgraph deploy (#7234)

**Description:** 

 - extend host_backend client with remote build support
- refactored `cli.py` for better clarity
- update tests
This commit is contained in:
Asamu David
2026-04-02 16:39:30 +01:00
committed by GitHub
parent 57b314c5d7
commit b3eeb4fa1d
17 changed files with 2490 additions and 1268 deletions
+9 -9
View File
@@ -9,7 +9,7 @@ from pathlib import Path
from click.testing import CliRunner
import langgraph_cli.cli as cli_module
import langgraph_cli.deploy as deploy_module
from langgraph_cli.cli import cli, prepare_args_and_stdin
from langgraph_cli.config import Config, _get_pip_cleanup_lines, validate_config
from langgraph_cli.docker import DEFAULT_POSTGRES_URI, DockerCapabilities, Version
@@ -346,7 +346,7 @@ def test_deploy_list_command(monkeypatch) -> None:
]
}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -386,7 +386,7 @@ def test_deploy_list_command_no_results(monkeypatch) -> None:
def list_deployments(self, name_contains: str = ""):
return {"resources": []}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -432,7 +432,7 @@ def test_deploy_revisions_list_command(monkeypatch) -> None:
]
}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -473,7 +473,7 @@ def test_deploy_revisions_list_command_no_results(monkeypatch) -> None:
def list_revisions(self, deployment_id: str, limit: int = 1):
return {"resources": []}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -506,7 +506,7 @@ def test_deploy_revisions_list_command_with_explicit_limit(monkeypatch) -> None:
captured["limit"] = str(limit)
return {"resources": []}
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -552,7 +552,7 @@ def test_deploy_delete_command(monkeypatch) -> None:
captured["deployment_id"] = deployment_id
return None
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -594,7 +594,7 @@ def test_deploy_delete_command_cancelled(monkeypatch) -> None:
deleted = True
return None
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
@@ -629,7 +629,7 @@ def test_deploy_delete_command_force(monkeypatch) -> None:
captured["deployment_id"] = deployment_id
return None
monkeypatch.setattr(cli_module, "HostBackendClient", FakeClient)
monkeypatch.setattr(deploy_module, "HostBackendClient", FakeClient)
result = runner.invoke(
cli,
+299
View File
@@ -0,0 +1,299 @@
import os
import tarfile
from unittest.mock import patch
import click
import pytest
from langgraph_cli.archive import (
_add_directory,
_build_ignore_spec,
_tar_filter,
create_archive,
)
# ---------------------------------------------------------------------------
# _tar_filter
# ---------------------------------------------------------------------------
class TestTarFilter:
def _make_info(self, name: str, *, type_: int = tarfile.REGTYPE) -> tarfile.TarInfo:
info = tarfile.TarInfo(name=name)
info.type = type_
return info
def test_regular_file_passes(self):
info = self._make_info("src/main.py")
assert _tar_filter(info) is info
def test_symlink_rejected(self):
info = self._make_info("link", type_=tarfile.SYMTYPE)
assert _tar_filter(info) is None
def test_hardlink_rejected(self):
info = self._make_info("link", type_=tarfile.LNKTYPE)
assert _tar_filter(info) is None
def test_path_traversal_rejected(self):
info = self._make_info("../../etc/passwd")
assert _tar_filter(info) is None
def test_path_traversal_in_middle_rejected(self):
info = self._make_info("src/../../../etc/passwd")
assert _tar_filter(info) is None
def test_dotdot_as_name_component_rejected(self):
info = self._make_info("foo/../bar")
assert _tar_filter(info) is None
def test_dotdot_in_filename_allowed(self):
"""A file literally named 'foo..bar' is not traversal."""
info = self._make_info("foo..bar")
assert _tar_filter(info) is info
def test_directory_passes(self):
info = self._make_info("src/", type_=tarfile.DIRTYPE)
assert _tar_filter(info) is info
# ---------------------------------------------------------------------------
# _build_ignore_spec
# ---------------------------------------------------------------------------
class TestBuildIgnoreSpec:
def test_always_excludes_builtins(self, tmp_path):
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("__pycache__/")
assert spec.match_file(".git/")
assert spec.match_file(".venv/")
assert spec.match_file("venv/")
assert spec.match_file("node_modules/")
assert spec.match_file(".tox/")
assert spec.match_file(".mypy_cache/")
def test_regular_file_not_excluded(self, tmp_path):
spec = _build_ignore_spec(tmp_path)
assert not spec.match_file("main.py")
assert not spec.match_file("src/app.py")
def test_merges_dockerignore(self, tmp_path):
(tmp_path / ".dockerignore").write_text("*.log\nbuild/\n")
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("server.log")
assert spec.match_file("build/")
# builtins still present
assert spec.match_file("__pycache__/")
def test_merges_gitignore(self, tmp_path):
(tmp_path / ".gitignore").write_text("*.pyc\ndist/\n")
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("module.pyc")
assert spec.match_file("dist/")
def test_merges_both_ignore_files(self, tmp_path):
(tmp_path / ".dockerignore").write_text("*.log\n")
(tmp_path / ".gitignore").write_text("*.pyc\n")
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("app.log")
assert spec.match_file("mod.pyc")
def test_no_ignore_files_only_builtins(self, tmp_path):
spec = _build_ignore_spec(tmp_path)
assert spec.match_file("__pycache__/")
assert not spec.match_file("README.md")
# ---------------------------------------------------------------------------
# _add_directory
# ---------------------------------------------------------------------------
class TestAddDirectory:
def _create_project(self, tmp_path):
"""Create a small project structure for testing."""
(tmp_path / "main.py").write_text("print('hello')")
(tmp_path / "lib").mkdir()
(tmp_path / "lib" / "util.py").write_text("x = 1")
(tmp_path / "__pycache__").mkdir()
(tmp_path / "__pycache__" / "main.cpython-311.pyc").write_bytes(b"\x00")
return tmp_path
def test_adds_files_without_prefix(self, tmp_path):
project = self._create_project(tmp_path)
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix=None, ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert "main.py" in names
assert "lib/util.py" in names
def test_excludes_pycache(self, tmp_path):
project = self._create_project(tmp_path)
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix=None, ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert not any("__pycache__" in n for n in names)
def test_adds_files_with_prefix(self, tmp_path):
project = self._create_project(tmp_path)
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix="myapp", ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert "myapp/main.py" in names
assert "myapp/lib/util.py" in names
def test_respects_custom_ignore_patterns(self, tmp_path):
project = self._create_project(tmp_path)
(project / ".gitignore").write_text("lib/\n")
spec = _build_ignore_spec(project)
archive_path = tmp_path / "out.tar"
with tarfile.open(archive_path, "w") as tar:
_add_directory(tar, project, arcname_prefix=None, ignore_spec=spec)
with tarfile.open(archive_path, "r") as tar:
names = tar.getnames()
assert "main.py" in names
assert "lib/util.py" not in names
# ---------------------------------------------------------------------------
# create_archive (integration)
# ---------------------------------------------------------------------------
class TestCreateArchive:
def _make_project(self, tmp_path):
"""Set up a minimal project directory with a config file."""
project = tmp_path / "myproject"
project.mkdir()
config_file = project / "langgraph.json"
config_file.write_text('{"dependencies": ["."]}')
(project / "app.py").write_text("print('hello')")
(project / "__pycache__").mkdir()
(project / "__pycache__" / "app.cpython-311.pyc").write_bytes(b"\x00")
return config_file
@patch("langgraph_cli.archive._assemble_local_deps")
def test_yields_archive_with_config(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with create_archive(config_file, {}) as (archive_path, file_size, config_rel):
assert os.path.isfile(archive_path)
assert archive_path.endswith(".tar.gz")
assert file_size > 0
assert config_rel == "langgraph.json"
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert "langgraph.json" in names
assert "app.py" in names
@patch("langgraph_cli.archive._assemble_local_deps")
def test_excludes_pycache(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with create_archive(config_file, {}) as (archive_path, _size, _rel):
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert not any("__pycache__" in n for n in names)
@patch("langgraph_cli.archive._assemble_local_deps")
def test_cleans_up_tmp_dir_on_normal_exit(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with create_archive(config_file, {}) as (archive_path, _size, _rel):
tmp_dir = os.path.dirname(archive_path)
assert os.path.isdir(tmp_dir)
assert not os.path.exists(tmp_dir)
@patch("langgraph_cli.archive._assemble_local_deps")
def test_cleans_up_tmp_dir_on_exception(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with pytest.raises(RuntimeError, match="boom"):
with create_archive(config_file, {}) as (archive_path, _size, _rel):
tmp_dir = os.path.dirname(archive_path)
raise RuntimeError("boom")
assert not os.path.exists(tmp_dir)
@patch("langgraph_cli.archive._assemble_local_deps")
@patch("langgraph_cli.archive._MAX_SIZE", 10)
def test_raises_on_oversized_archive(self, mock_deps, tmp_path):
from langgraph_cli.config import LocalDeps
config_file = self._make_project(tmp_path)
mock_deps.return_value = LocalDeps(
pip_reqs=[], real_pkgs={}, faux_pkgs={}, additional_contexts=None
)
with pytest.raises(click.ClickException, match="exceeds the 200 MB limit"):
with create_archive(config_file, {}):
pass
@patch("langgraph_cli.archive._assemble_local_deps")
def test_handles_extra_contexts(self, mock_deps, tmp_path):
"""Monorepo case: project + sibling dependency directory."""
from langgraph_cli.config import LocalDeps
project = tmp_path / "myproject"
project.mkdir()
config_file = project / "langgraph.json"
config_file.write_text('{"dependencies": [".", "../shared"]}')
(project / "app.py").write_text("print('hello')")
shared = tmp_path / "shared"
shared.mkdir()
(shared / "lib.py").write_text("y = 2")
mock_deps.return_value = LocalDeps(
pip_reqs=[],
real_pkgs={},
faux_pkgs={},
additional_contexts=[shared],
)
with create_archive(config_file, {}) as (archive_path, _size, config_rel):
with tarfile.open(archive_path, "r:gz") as tar:
names = tar.getnames()
assert "myproject/app.py" in names
assert "shared/lib.py" in names
assert config_rel == "myproject/langgraph.json"
@@ -6,12 +6,14 @@ import click
import httpx
import pytest
from langgraph_cli.cli import (
from langgraph_cli.deploy import (
_call_host_backend_with_optional_tenant,
_docker_config_for_token,
_normalize_image_name,
_normalize_image_tag,
_env_without_deployment_name,
_parse_env_from_config,
_resolve_env_path,
normalize_image_name,
normalize_image_tag,
)
from langgraph_cli.host_backend import HostBackendClient, HostBackendError
@@ -40,47 +42,47 @@ class TestDockerConfigForToken:
class TestNormalizeImageName:
def test_simple_name(self):
assert _normalize_image_name("myapp") == "myapp"
assert normalize_image_name("myapp") == "myapp"
def test_uppercase_lowered(self):
assert _normalize_image_name("MyApp") == "myapp"
assert normalize_image_name("MyApp") == "myapp"
def test_special_chars_replaced(self):
assert _normalize_image_name("my app!@#v2") == "my-app-v2"
assert normalize_image_name("my app!@#v2") == "my-app-v2"
def test_dots_and_hyphens_kept(self):
assert _normalize_image_name("my-app.v2") == "my-app.v2"
assert normalize_image_name("my-app.v2") == "my-app.v2"
def test_leading_trailing_stripped(self):
assert _normalize_image_name("--my-app..") == "my-app"
assert normalize_image_name("--my-app..") == "my-app"
def test_empty_string_returns_app(self):
assert _normalize_image_name("") == "app"
assert normalize_image_name("") == "app"
def test_none_returns_app(self):
assert _normalize_image_name(None) == "app"
assert normalize_image_name(None) == "app"
def test_all_invalid_chars_returns_app(self):
assert _normalize_image_name("!!!") == "app"
assert normalize_image_name("!!!") == "app"
class TestNormalizeImageTag:
def test_valid_tag(self):
assert _normalize_image_tag("v1.2.3") == "v1.2.3"
assert normalize_image_tag("v1.2.3") == "v1.2.3"
def test_empty_defaults_to_latest(self):
assert _normalize_image_tag("") == "latest"
assert normalize_image_tag("") == "latest"
def test_alphanumeric_and_special(self):
assert _normalize_image_tag("my_tag-1.0") == "my_tag-1.0"
assert normalize_image_tag("my_tag-1.0") == "my_tag-1.0"
def test_invalid_chars_raises(self):
with pytest.raises(click.UsageError, match="Image tag may only contain"):
_normalize_image_tag("v1.0:bad")
normalize_image_tag("v1.0:bad")
def test_spaces_raises(self):
with pytest.raises(click.UsageError, match="Image tag may only contain"):
_normalize_image_tag("has space")
normalize_image_tag("has space")
class TestParseEnvFromConfig:
@@ -137,6 +139,51 @@ class TestParseEnvFromConfig:
assert result["EMPTY"] == ""
class TestResolveEnvPath:
def test_inline_env_dict_returns_none(self, tmp_path):
config_path = tmp_path / "langgraph.json"
config_path.touch()
assert _resolve_env_path({"env": {"FOO": "bar"}}, config_path) is None
def test_relative_env_path_resolves(self, tmp_path):
env_file = tmp_path / "custom.env"
env_file.write_text("FOO=bar\n")
config_path = tmp_path / "langgraph.json"
config_path.touch()
resolved = _resolve_env_path({"env": "custom.env"}, config_path)
assert resolved == env_file.resolve()
def test_missing_env_file_returns_none(self, tmp_path):
config_path = tmp_path / "langgraph.json"
config_path.touch()
assert _resolve_env_path({"env": "missing.env"}, config_path) is None
def test_default_env_is_cwd_dotenv(self, tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path)
config_path = tmp_path / "langgraph.json"
config_path.touch()
assert _resolve_env_path({}, config_path) == tmp_path / ".env"
class TestEnvWithoutDeploymentName:
def test_removes_deployment_name_only(self):
env = {
"LANGSMITH_DEPLOYMENT_NAME": "my-deploy",
"KEEP_ME": "value",
}
cleaned = _env_without_deployment_name(env)
assert "LANGSMITH_DEPLOYMENT_NAME" not in cleaned
assert cleaned["KEEP_ME"] == "value"
# Original dict should be unchanged.
assert env["LANGSMITH_DEPLOYMENT_NAME"] == "my-deploy"
def test_noop_when_deployment_name_absent(self):
env = {"FOO": "bar"}
assert _env_without_deployment_name(env) == {"FOO": "bar"}
class TestCallHostBackendWithOptionalTenant:
def _make_client(self, handler):
c = HostBackendClient("https://api.example.com", "test-key")
@@ -121,7 +121,9 @@ def test_request_transport_error_raises():
def test_create_deployment(client):
result = client.create_deployment({"name": "my-deploy"})
result = client.create_deployment(
name="my-deploy", deployment_type="dev", source="internal_docker"
)
assert result == {"ok": True}
@@ -1,4 +1,4 @@
from langgraph_cli.helpers import format_log_entry, format_timestamp, level_fg
from langgraph_cli.deploy import format_log_entry, format_timestamp, level_fg
class TestFormatTimestamp:
+2 -3
View File
@@ -1,12 +1,11 @@
from unittest.mock import patch
from langgraph_cli.util import (
from langgraph_cli.deploy import (
_extract_deployment_url,
clean_empty_lines,
format_deployments_table,
format_revisions_table,
warn_non_wolfi_distro,
)
from langgraph_cli.util import clean_empty_lines, warn_non_wolfi_distro
def test_clean_empty_lines():