diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 41a56871..0d81231d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -175,8 +175,10 @@ jobs: # Both figures go into the PR description between marker comments, so # a merge that takes the description as its message carries them too. + # A pull request from a fork gets a read-only token that cannot edit its + # description; the job summary and the uploaded reports still have them. - name: Publish coverage numbers to the pull request - if: github.event_name == 'pull_request' + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} diff --git a/CURRENT-STATUS.md b/CURRENT-STATUS.md index 6ed04b08..34c2de5e 100644 --- a/CURRENT-STATUS.md +++ b/CURRENT-STATUS.md @@ -20,8 +20,8 @@ coordination branch. Detailed state belongs in each linked status file. | `website` | 2026-09-16 | Deliver the website autonomy experiment: repository-owned landing/docs/blog content, independently developable website submodule, local preview and publishing mechanism | paused 2026-09-30; producer migration merged in PR #152 and the versioned documentation live; W04-C, W09-C, W11-C retained | `ws-website/contract-v1` | [workstream status](engineering-docs/wip/2026-09-16-website/CURRENT-STATUS.md) | | `capsule-webconsole` | 2026-10-09 | Serve a token-gated web console from every capsule: configuration, versions, info, live resources, later records and workflow state | active; work order accepted 2026-10-09; the first iteration, deliverables 1 and 2, is three stacked pull requests, #184, #186 and the runtime-and-base slice, each reviewed by the Codex pair; the owner merges | `ws-capsule-webconsole/first-slice` | [workstream status](engineering-docs/wip/2026-10-09-capsule-webconsole/CURRENT-STATUS.md) | | `wrapped-projects` | 2026-11-02 | Wrap a foreign repository as a submodule of a small wrapper project and work on it in a capsule with three commands; FRC learning project first | active; PR1 (#333) accepted by the owner 2026-11-03, awaiting merge; the project-capabilities design handed to workflow-improvements | `ws-wrapped-projects/wrap` | [workstream status](engineering-docs/wip/2026-11-02-wrapped-projects/CURRENT-STATUS.md) | -| `macos-launcher` | 2026-11-03 | Run the DevCapsule launcher natively on Apple Silicon Macs against Docker Desktop, with capsules as `linux-amd64` under emulation | active 2026-11-03; slices 1 to 4 done, the PyCharm smoke passing on an Apple Silicon Mac; waits on publication and the owner's rulings | `ws-macos-launcher/host-port` | [workstream status](engineering-docs/wip/2026-11-03-macos-launcher/CURRENT-STATUS.md) | -| `linux-arm64` | 2026-11-04 | Run capsules as `linux-arm64` as well as `linux-amd64`: natively on Apple Silicon Macs and on ARM Linux hosts | active 2026-11-04; slices 1 to 4 done, six IDEs running natively on Apple Silicon; waits on publication, the owner's arm64 base and rulings | `ws-linux-arm64/capsules` | [workstream status](engineering-docs/wip/2026-11-04-linux-arm64/CURRENT-STATUS.md) | +| `macos-launcher` | 2026-11-03 | Run the DevCapsule launcher natively on Apple Silicon Macs against Docker Desktop, with capsules as `linux-amd64` under emulation | active 2026-11-03; slices 1 to 4 done, on `main` since #338; waits on the owner's rulings | `ws-macos-launcher/host-port` | [workstream status](engineering-docs/wip/2026-11-03-macos-launcher/CURRENT-STATUS.md) | +| `linux-arm64` | 2026-11-04 | Run capsules as `linux-arm64` as well as `linux-amd64`: natively on Apple Silicon Macs and on ARM Linux hosts | active 2026-11-04; on `main` since #338, arm64 base published; the follow-up turns `main`'s CI green; waits on the owner's rulings | `ws-linux-arm64/mirror-and-published-base` | [workstream status](engineering-docs/wip/2026-11-04-linux-arm64/CURRENT-STATUS.md) | | `website` | 2026-09-16 | Deliver the website autonomy experiment: repository-owned landing/docs/blog content, independently developable website submodule, local preview and publishing mechanism | paused 2026-09-20 at owner request to switch to maintenance; experiment accepted, A−; blog integrated and published; content/parent-integration follow-ups remain | `ws-website/initial-cut`; renamed from `website/initial-cut`; preparation and backlog-handoff exceptions in status | [workstream status](engineering-docs/wip/2026-09-16-website/CURRENT-STATUS.md) | Paused and blocked workstreams remain open until they conclude successfully or diff --git a/devcapsule-src/devcapsule/configuration/operations.py b/devcapsule-src/devcapsule/configuration/operations.py index 23d2c149..ecc61c85 100644 --- a/devcapsule-src/devcapsule/configuration/operations.py +++ b/devcapsule-src/devcapsule/configuration/operations.py @@ -42,7 +42,7 @@ ) from .capabilities import CapabilityPolicy, LocalCapabilities, requirement from .capability_commands import check_documents, pin_selection -from .capability_selection import compose_lock, effective_lock, generate_lock, local_pins, matrix_for +from .capability_selection import compose_lock, effective_lock, generate_lock, local_pins from .file_formats import ( AuthorizationScalar, ConfigurationFileKind, @@ -315,8 +315,6 @@ class InitializeReport: checkout_record: Path # None when the owner's checkout has no IDE yet: nothing to resolve. resolve: ResolveReport | None - # The lock pins a base its registry does not hold yet (see ResolutionMatrix.base_unpublished). - base_unpublished: bool def render(self) -> str: required = " ".join(self.required) or "none" @@ -330,9 +328,6 @@ def render(self) -> str: if self.offered: lines.append(f"Offers: {' '.join(self.offered)}") lines.append(f"{self.lock_action} {self.lock_path} (base {self.base_display})") - if self.base_unpublished: - lines.append("Its base is not published yet: a checkout runs this lock only on a locally built base of " - "the same recipe, selected with 'devcapsule project config authorize base-image IMAGE'.") for name, value, justification in self.recommendations: line = f"Recommended {name} = {value} for every checkout" lines.append(f"{line} ({justification})." if justification else f"{line}.") @@ -551,7 +546,6 @@ def persist() -> None: base_display=_base_display(shared_lock), recommendations=recommendations, selection=selection, answered=(), carried=(), recommended=(), checkout_record=record.input_path, resolve=None, - base_unpublished=_pins_unpublished_base(shared_lock), ) lock = effective_lock(manifest, compose_lock(manifest, shared_lock, record.document), record.document, warn=False) declarations = authorization_declarations(manifest, lock) @@ -579,7 +573,6 @@ def persist() -> None: answered=tuple(settled.answered), carried=tuple(settled.carried), recommended=accepted_recommendations, checkout_record=record.input_path, resolve=resolve_report, - base_unpublished=_pins_unpublished_base(shared_lock), ) @@ -596,9 +589,7 @@ def _elicit_platform(elicitor: Elicitor, root: Path) -> Platform: native = runnable[0] locked = next((platform for platform in runnable if platform_lock_path(root, platform).is_file()), None) choices = " or ".join( - f"{platform} ({'native' if platform is native else 'emulated'}" - + ("; its base is built locally" if _base_unpublished(MATRICES[platform]) else "") + ")" - for platform in runnable + f"{platform} ({'native' if platform is native else 'emulated'})" for platform in runnable ) answer = elicitor.seek( "platform", @@ -638,16 +629,11 @@ def validate(value: str) -> str: return validate -def _base_unpublished(matrix: ResolutionMatrix) -> bool: - """Whether every base ``matrix`` pins is still unpublished.""" - return all(matrix.base_unpublished(base.reference) for base in matrix.base_images()) - - def _initialize_report( manifest_path: Path, manifest_action: str, identity: _ProjectIdentity, lock_path: Path, lock_action: str, *, base_display: str, recommendations: list[tuple[str, str]], selection: _OwnerSelection, answered: tuple[str, ...], carried: tuple[str, ...], recommended: tuple[str, ...], - checkout_record: Path, resolve: ResolveReport | None, base_unpublished: bool, + checkout_record: Path, resolve: ResolveReport | None, ) -> InitializeReport: return InitializeReport( manifest_path=manifest_path, @@ -672,15 +658,9 @@ def _initialize_report( recommended=recommended, checkout_record=checkout_record, resolve=resolve, - base_unpublished=base_unpublished, ) -def _pins_unpublished_base(lock: Mapping[str, Any]) -> bool: - """Whether ``lock`` pins a base its registry does not hold yet.""" - return matrix_for(lock).base_unpublished(str(lock.get("base", {}).get("reference"))) - - def _base_display(lock: Mapping[str, Any]) -> str: base = lock.get("base") if isinstance(base, dict) and isinstance(base.get("reference"), str): @@ -1656,7 +1636,6 @@ def _apply_answers_to_standing_checkout( base_display=base_display, recommendations=[], selection=_existing_selection(root, manifest), answered=tuple(settled.answered), carried=tuple(settled.carried), recommended=(), checkout_record=record.input_path, resolve=resolve_report, - base_unpublished=_pins_unpublished_base(lock), ) diff --git a/devcapsule-src/devcapsule/environment_realization.py b/devcapsule-src/devcapsule/environment_realization.py index bdb7eab4..e92ec9be 100644 --- a/devcapsule-src/devcapsule/environment_realization.py +++ b/devcapsule-src/devcapsule/environment_realization.py @@ -112,16 +112,6 @@ def realize_environment( selection.local_image_identity if local_authorization else locked.base_identity ) - if not local_authorization and base_override is None: - from devcapsule.resolution_matrix import MATRICES - - if MATRICES[capsule_platform].base_unpublished(base_reference): - raise CliError( - f"The lock's base {base_reference} is not published yet, so it cannot be pulled. Build the " - f"{capsule_platform} base locally ('devcapsule images build --type base --recipe ubuntu-24.04 " - f"--platform {capsule_platform} --tag IMAGE --source-revision REVISION') and select it " - "for this checkout ('devcapsule project config authorize base-image IMAGE')." - ) if obtain_image is not None: obtain = obtain_image elif local_authorization: diff --git a/devcapsule-src/devcapsule/resolution_matrix.py b/devcapsule-src/devcapsule/resolution_matrix.py index 05c1eea9..02648be6 100644 --- a/devcapsule-src/devcapsule/resolution_matrix.py +++ b/devcapsule-src/devcapsule/resolution_matrix.py @@ -101,7 +101,7 @@ class Capability: @dataclass(frozen=True) class _BasePin: - """One base image and the capabilities it ships.""" + """One published base image and the capabilities it ships.""" mnemonic: str # The family of base releases this pin belongs to — see _VerifiedEdge: @@ -123,9 +123,6 @@ class _BasePin: runtime: str = LAUNCHER_SUPPLIED_RUNTIME source_revision: str | None = None # A clean public-source builder need not have a release tag. - # False while the reference names an image its registry does not hold - # yet: only a locally built base of the same recipe stands in for it. - published: bool = True @property def contract(self) -> BaseContract: @@ -250,10 +247,6 @@ def base_family(self, lock: Mapping[str, Any]) -> str | None: return next((base.base_family for base in self._bases if base.lock_table.get("reference") == reference), None) - def base_unpublished(self, reference: str) -> bool: - """Whether the pinned base at ``reference`` is not in its registry yet.""" - return any(base.lock_table.get("reference") == reference and not base.published for base in self._bases) - def base_image(self, reference: str) -> BaseImage | None: """The pinned base at ``reference``, described by its contract.""" return next((base.image for base in self._bases @@ -1724,11 +1717,11 @@ def _formation( # the public main commit b3f10b7d, whose devcapsule-webconsole the image # installed. Inspected: linux/arm64, recipe 11 ready, display=contained, # runtime=launcher-supplied, all six services, the web console root. -# Not published yet. The reference is the digest of the OCI image index in -# the builder's containerd store, which a push of this exact image to -# mycodespaceai/devcapsule-base keeps; a rebuild gets another digest and -# must be pinned again. Until the owner pushes it, a checkout selects the -# local image: `project config authorize base-image IMAGE`. +# This exact image was pushed as +# mycodespaceai/devcapsule-base:linux-arm64-recipe-11-20261104 at 15:29 UTC +# the same day; the digest below, its OCI image index, was read from the +# registry after the push. A rebuild gets another digest and must be +# pinned again. _LINUX_ARM64_RECIPE_11_BASE = _BasePin( mnemonic="v0.3.1.dev0-local-macos-aarch64", source_revision="b3f10b7d95aec659f268afecc03d0912a2e89d2f", @@ -1740,7 +1733,6 @@ def _formation( recipe=11, display=CONTAINED_DISPLAY, runtime=LAUNCHER_SUPPLIED_RUNTIME, - published=False, lock_table={ "reference": ( "docker.io/mycodespaceai/devcapsule-base" @@ -1782,8 +1774,9 @@ def _on_linux_arm64(pin: _ComponentPin) -> _ComponentPin: }, edges=( # Native IDE smokes on Apple Silicon (Docker Desktop 29.3.1) against - # the local, unpublished arm64 recipe-11 base above. They establish - # the IDE only: Playwright and the .NET SDK rode along unexercised. + # the arm64 recipe-11 base above, run on the local image before its + # push. They establish the IDE only: Playwright and the .NET SDK rode + # along unexercised. _VerifiedEdge( "codium", "1.143.17232", _BASE_FAMILY_UBUNTU_24_04, "noVNC HTTP/window startup passed natively on the local arm64 recipe-11 base, " diff --git a/devcapsule-src/tests/configuration/test_contract.py b/devcapsule-src/tests/configuration/test_contract.py index e1eeb182..74bc78c8 100644 --- a/devcapsule-src/tests/configuration/test_contract.py +++ b/devcapsule-src/tests/configuration/test_contract.py @@ -43,9 +43,6 @@ invoke, replace_answers, ) -from devcapsule.launch.pycharm import DockerMode, PycharmRunOptions, build_run_config -from devcapsule.platforms import Platform -from tests.test_pycharm import base_env pytestmark = pytest.mark.legacy @@ -293,18 +290,6 @@ def test_independent_invalid_values_are_reported_together(checkout, capsys): assert 'editor.theme' in message and 'service.port' in message -def test_launcher_environment_cannot_override_explicit_sudo_denial(checkout, tmp_path): - project, _, _ = checkout - env = base_env(tmp_path) - env['PYCHARM_ENABLE_SUDO'] = '1' - # ProjectRunCommand passes enable_sudo=False for the persisted denial. - # Check the NEXT boundary, rather than replacing it with a launch mock. - config = build_run_config( - PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, docker_mode=DockerMode.none, enable_sudo=False), env, - ) - assert config.enable_sudo is False - - def test_shipped_cli_recommendation_override_and_omission(checkout, capsys): project, record, resolution = checkout declaration = project / '.devcapsule/devcapsule.toml' diff --git a/devcapsule-src/tests/configuration/test_invariants.py b/devcapsule-src/tests/configuration/test_invariants.py index 923859f2..bd910f5d 100644 --- a/devcapsule-src/tests/configuration/test_invariants.py +++ b/devcapsule-src/tests/configuration/test_invariants.py @@ -42,9 +42,6 @@ ProvidedAnswer, initialize_project, ) -from devcapsule.launch.pycharm import DockerMode, PycharmRunOptions, build_run_config -from devcapsule.platforms import Platform -from tests.test_pycharm import base_env from tests.test_upgrade_recovery import checkout, invoke, install_external_fakes pytestmark = pytest.mark.legacy @@ -222,37 +219,6 @@ def test_a_removed_binding_never_reaches_the_launch(checkout, monkeypatch): assert 'bindings' not in derived.get('state', {}) and 'secret' not in derived -def test_project_launch_cannot_inherit_unbound_paths_or_credentials(checkout, tmp_path): - project, _, _ = checkout - env = base_env(tmp_path) - unbound = tmp_path / 'unbound' - env.update({ - 'DEVCAPSULE_HOME_DIR': str(unbound), 'PYCHARM_IDE_CONFIG_DIR': str(unbound), - 'PYCHARM_PLUGIN_DIR': str(unbound), 'GITHUB_TOKEN_FILE': str(unbound / 'secret'), - 'PYCHARM_GIT_TOKEN_ENV': 'UNBOUND_TOKEN', 'PYCHARM_ENABLE_SUDO': 'invalid', - 'DOCKER_MODE': 'invalid', - }) - config = build_run_config(PycharmRunOptions( - project=project, capsule_platform=Platform.LINUX_AMD64, inherit_legacy_configuration=False, - docker_mode=DockerMode.none, enable_sudo=False, network_mode='bridge', - ), env) - assert config.persistent_home != unbound and config.ide_config != unbound and config.plugins != unbound - assert config.git_token_file is None and config.git_token_env == '' - assert config.enable_sudo is False and config.docker_mode == 'none' and config.network_mode == 'bridge' - assert not unbound.exists() - - -@pytest.mark.parametrize('value', [False, True, None]) -def test_explicit_sudo_decision_overrides_legacy_environment(value, tmp_path): - project = tmp_path / 'project' - project.mkdir() - env = base_env(tmp_path) - env['PYCHARM_ENABLE_SUDO'] = '1' - config = build_run_config(PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, docker_mode=DockerMode.none, - enable_sudo=value), env) - assert config.enable_sudo is (True if value is None else value) - - @pytest.mark.parametrize('field,value', [('memory-limit-bytes', '8GiB'), ('memory-limit-bytes', True), ('cpus', 0), ('component', []), ('project-mount', 3), ('image', [])]) def test_an_invalid_derived_plan_is_refused_as_a_value(checkout, field, value): diff --git a/devcapsule-src/tests/e2e/test_component_cache.py b/devcapsule-src/tests/e2e/test_component_cache.py index e75aa1dc..7816a482 100644 --- a/devcapsule-src/tests/e2e/test_component_cache.py +++ b/devcapsule-src/tests/e2e/test_component_cache.py @@ -19,9 +19,10 @@ FileComponent, ImageBuildSpec, ) from devcapsule.materialization import ArtifactSpec, ensure_materialized_surface +from devcapsule.platforms import Platform from devcapsule.runtime_command import RuntimeCommand -pytestmark = pytest.mark.legacy +pytestmark = pytest.mark.e2e def docker(*args: str) -> str: @@ -35,8 +36,9 @@ def docker(*args: str) -> str: return completed.stdout.strip() -@pytest.mark.e2e def test_installation_is_reused_across_images_and_invalidated_by_recipe(tmp_path: Path) -> None: + """A component's installation layer is reused by every image built on the same base and recipe, + whatever else the images contain, and runs again when the recipe changes.""" base_reference = os.environ.get("DEVCAPSULE_E2E_BASE_IMAGE", "ubuntu:24.04") if expected_base := os.environ.get("DEVCAPSULE_E2E_BUILT_BASE"): assert json.loads(docker("image", "inspect", base_reference))[0]["Id"] == expected_base @@ -64,7 +66,7 @@ def contribution(version: str) -> ContributionComponent: stages = (contribution(version), sibling) if index == 0 else (sibling, contribution(version)) builder.build(ImageBuildSpec(image, base_reference, ( *stages, FileComponent(runtime, "/runtime"), - )), network="none") + )), platform=Platform.current(), network="none") observed.append(docker("run", "--rm", "--network=none", image, "cat", f"/opt/jdk/current/installed-{token}")) assert docker("run", "--rm", image, "cat", "/runtime") == f"launcher-{index}" @@ -74,12 +76,13 @@ def contribution(version: str) -> ContributionComponent: subprocess.run(["docker", "image", "rm", *images], capture_output=True) -@pytest.mark.e2e @pytest.mark.parametrize("surface", ["pycharm", "codium"]) @pytest.mark.parametrize("command", list(RuntimeCommand)) def test_formation_receives_exact_launcher_on_runtime_free_base( tmp_path: Path, built_pex: Path, surface: str, command: RuntimeCommand, ) -> None: + """A materialized surface on a base without a runtime carries the exact executable under test, + labelled with its digest, and reaches it through each public command as an ordinary user.""" archive_path = tmp_path / "surface.tar.gz" names = ("bin/pycharm.sh",) if surface == "pycharm" else ("codium", "bin/codium", "chrome-sandbox") with tarfile.open(archive_path, "w:gz") as archive: @@ -101,7 +104,7 @@ def test_formation_receives_exact_launcher_on_runtime_free_base( hashlib.sha256(archive_path.read_bytes()).hexdigest(), "professional" if surface == "pycharm" else None), cache_root=tmp_path / "cache", inspect_image=optional_local_image, - build=lambda spec: BuildxImageBuilder().build(spec, network="none"), + build=lambda spec: BuildxImageBuilder().build(spec, platform=Platform.current(), network="none"), recipe_id="jetbrains-local-materialization" if surface == "pycharm" else "vscode-local-materialization", recipe_version="1" if surface == "pycharm" else "2", component_id=surface, runtime_pex=built_pex, runtime_command=command, diff --git a/devcapsule-src/tests/e2e/test_ide_comes_alive.py b/devcapsule-src/tests/e2e/test_ide_comes_alive.py index 989c26fe..465a45a7 100644 --- a/devcapsule-src/tests/e2e/test_ide_comes_alive.py +++ b/devcapsule-src/tests/e2e/test_ide_comes_alive.py @@ -22,7 +22,7 @@ from devcapsule.runtime_artifact import RUNTIME_PEX_ENV, executable_platform from tests.e2e.ide_session import SURFACES, IdeSurface, capture_desktop, command, desktop_page_answers, ide_session, launched_ide, stop_session, wait_for_console_url, wait_for_ide_window -pytestmark = pytest.mark.legacy +pytestmark = pytest.mark.e2e EVIDENCE_ROOT = Path(__file__).resolve().parents[2] / "dist" / "e2e-evidence" / "ide-smoke" @@ -39,10 +39,12 @@ def evidence_run() -> Path: return run -@pytest.mark.e2e @pytest.mark.ide_smoke @pytest.mark.parametrize("surface", SURFACES, ids=[surface.name for surface in SURFACES]) def test_ide_comes_alive(surface: IdeSurface, built_pex: Path, tmp_path: Path, evidence_run: Path) -> None: + """Each IDE, launched in a fresh project by the executable under test, opens its window on the + capsule's desktop, the desktop and the token-gated web console answer, and the capsule runs the + runtime that executable delivers.""" evidence = evidence_run / surface.name (evidence_run / "run.json").write_text(json.dumps({ "executable": str(built_pex), diff --git a/devcapsule-src/tests/test_base_image.py b/devcapsule-src/tests/test_base_image.py index 238311c9..012fea28 100644 --- a/devcapsule-src/tests/test_base_image.py +++ b/devcapsule-src/tests/test_base_image.py @@ -12,7 +12,6 @@ from devcapsule.images.base import ( NVIDIA_CUDA_ROOT_IMAGE, BaseImageBuildOptions, - build_base_image, build_base_image_spec, verify_public_github_revision, ) @@ -20,7 +19,6 @@ from devcapsule.compat import CliError from devcapsule.images.build import render_build_context from devcapsule.images.tooling import MAVEN_VERSION, TEMURIN_VERSION -from devcapsule.platforms import Platform pytestmark = pytest.mark.legacy @@ -172,33 +170,6 @@ def test_recipe_root_image_can_be_overridden(tmp_path: Path) -> None: assert ("devcapsule.base.recipe", "nvidia-cuda-devel") in plan.labels -def test_base_image_build_forwards_host_network_to_buildx(tmp_path: Path) -> None: - pex = pex_fixture(tmp_path / "devcapsule.pex") - options = BaseImageBuildOptions(pex, "test-base:latest", source_revision="a" * 40) - builder = Mock() - source_verifier = Mock() - - build_base_image(options, builder=builder, platform=Platform.LINUX_AMD64, network="host", - source_verifier=source_verifier) - - source_verifier.assert_called_once() - builder.build.assert_called_once() - assert builder.build.call_args.args[0].image == "test-base:latest" - assert builder.build.call_args.kwargs == {"network": "host", "platform": Platform.LINUX_AMD64} - - -def test_base_image_rejects_unreachable_revision_before_buildx(tmp_path: Path) -> None: - pex = pex_fixture(tmp_path / "devcapsule.pex") - options = BaseImageBuildOptions(pex, "test-base:latest", source_revision="a" * 40) - builder = Mock() - source_verifier = Mock(side_effect=CliError("source revision is not publicly reachable")) - - with pytest.raises(CliError, match="not publicly reachable"): - build_base_image(options, builder=builder, platform=Platform.LINUX_AMD64, source_verifier=source_verifier) - - builder.build.assert_not_called() - - def test_base_image_rejects_revision_that_disagrees_with_pex(tmp_path: Path) -> None: pex = pex_fixture(tmp_path / "devcapsule.pex") options = BaseImageBuildOptions(pex, source_revision="b" * 40) @@ -237,31 +208,6 @@ def test_base_without_baseline_allows_explicit_local_source_escape_hatch(tmp_pat assert not any(key == "devcapsule.base.webconsole" for key, _ in plan.labels) -def test_console_base_rejects_missing_source_before_starting_the_builder(tmp_path: Path) -> None: - pex = pex_fixture(tmp_path / "devcapsule.pex", revision="unknown", public=False) - builder = Mock() - with pytest.raises(CliError, match="--allow-local-source cannot supply the console source"): - build_base_image(BaseImageBuildOptions(pex, allow_local_source=True), builder=builder, - platform=Platform.LINUX_AMD64) - builder.build.assert_not_called() - - -def test_base_image_local_source_escape_hatch_skips_public_verification(tmp_path: Path) -> None: - pex = pex_fixture(tmp_path / "devcapsule.pex") - builder = Mock() - source_verifier = Mock() - - build_base_image( - BaseImageBuildOptions(pex, allow_local_source=True), - builder=builder, - platform=Platform.LINUX_AMD64, - source_verifier=source_verifier, - ) - - source_verifier.assert_not_called() - builder.build.assert_called_once() - - def test_public_revision_verification_checks_exact_commit_url(monkeypatch: pytest.MonkeyPatch) -> None: info = BuildInfo( schema_version=1, diff --git a/devcapsule-src/tests/test_cli.py b/devcapsule-src/tests/test_cli.py index e342f180..c907d96f 100644 --- a/devcapsule-src/tests/test_cli.py +++ b/devcapsule-src/tests/test_cli.py @@ -16,7 +16,6 @@ from devcapsule.launch.pycharm._image_build import PycharmImageBuildOptions from devcapsule.environment_realization import RealizedEnvironment from devcapsule.images.metadata import LocalImageRecord -from devcapsule.platforms import Platform from devcapsule.materialization import ImageDetails, parse_locked_environment from devcapsule.configuration.file_formats import ( canonical_digest, @@ -325,59 +324,6 @@ def test_images_list_prints_managed_local_inventory(capsys) -> None: assert "1234567890ab" in output -def test_images_build_base_maps_cli_options(tmp_path: Path, capsys) -> None: - pex = tmp_path / "devcapsule.pex" - pex.write_bytes(b"pex") - inspected = SimpleNamespace( - id="sha256:abc123", - config=SimpleNamespace( - labels={ - "devcapsule.base.recipe-version": "1", - "devcapsule.pex.build-mnemonic": "v026", - "devcapsule.pex.sha256": "a" * 64, - "devcapsule.source.revision": "revision-1", - } - ), - ) - - with ( - patch("devcapsule.commands.images.build_base_image") as build, - patch("devcapsule.commands.images.inspect_local_image", return_value=inspected), - ): - result = cli.main( - [ - "images", - "build", - "--type", - "base", - "--tag", - "devcapsule-base:debug-v019", - "--from", - "local-root:test", - "--pex", - str(pex), - "--source-revision", - "revision-1", - "--network", - "host", - ] - ) - - assert result == 0 - options = build.call_args.args[0] - assert options.pex == pex.resolve() - assert options.image == "devcapsule-base:debug-v019" - assert options.root_image == "local-root:test" - assert options.source_revision == "revision-1" - assert options.allow_local_source is False - assert options.recipe == "ubuntu-24.04" - assert build.call_args.kwargs == {"network": "host", "platform": Platform.LINUX_AMD64} - output = capsys.readouterr().out - assert "Image ID: sha256:abc123" in output - assert "Runtime: supplied by the launcher" in output - assert "Source verification: public GitHub commit reachable" in output - - def test_images_build_base_selects_wip_nvidia_cuda_recipe(tmp_path: Path, capsys) -> None: pex = tmp_path / "devcapsule.pex" pex.write_bytes(b"pex") diff --git a/devcapsule-src/tests/test_environment_realization.py b/devcapsule-src/tests/test_environment_realization.py index 19494ae6..7c0f45e9 100644 --- a/devcapsule-src/tests/test_environment_realization.py +++ b/devcapsule-src/tests/test_environment_realization.py @@ -1,7 +1,7 @@ from __future__ import annotations from pathlib import Path -from unittest.mock import Mock, patch +from unittest.mock import Mock import pytest @@ -10,10 +10,8 @@ CLAUDE_CODE_AUTHORIZATION, CLAUDE_CODE_TERMS_URL, ) -from devcapsule.environment_realization import ensure_local_image, realize_environment -from devcapsule.platforms import Platform +from devcapsule.environment_realization import realize_environment from devcapsule.materialization import ImageDetails -from devcapsule.runtime_command import RuntimeCommand from devcapsule.configuration.storage import ( ResolvedProject, ) @@ -263,43 +261,6 @@ def test_realize_environment_allows_terms_bound_claude_download_authorization( assert claude.destination == "/opt/claude/bin/claude" -@pytest.mark.parametrize("already_local", [True, False]) -def test_ensure_local_image_pulls_only_when_missing(already_local: bool) -> None: - details = base_image() - with ( - patch( - "devcapsule.environment_realization.docker.image.exists", - return_value=already_local, - ), - patch("devcapsule.environment_realization.docker.image.pull") as pull, - patch( - "devcapsule.environment_realization.required_local_image", - return_value=details, - ) as require, - ): - assert ensure_local_image(BASE_REFERENCE, Platform.LINUX_AMD64) == details - - if already_local: - pull.assert_not_called() - else: - pull.assert_called_once_with(BASE_REFERENCE, platform=None) - require.assert_called_once_with(BASE_REFERENCE) - - -@pytest.mark.parametrize("daemon,expected", [("arm64", "linux/amd64"), ("amd64", None)]) -def test_a_daemon_of_another_architecture_pulls_the_lock_platform(daemon: str, expected: str | None) -> None: - # Docker Desktop on Apple Silicon is an arm64 daemon; a linux-amd64 lock's - # base must not resolve to its arm64 variant. - with ( - patch("devcapsule.environment_realization.daemon_architecture", return_value=daemon), - patch("devcapsule.environment_realization.docker.image.exists", return_value=False), - patch("devcapsule.environment_realization.docker.image.pull") as pull, - patch("devcapsule.environment_realization.required_local_image", return_value=base_image()), - ): - ensure_local_image(BASE_REFERENCE, Platform.LINUX_AMD64) - pull.assert_called_once_with(BASE_REFERENCE, platform=expected) - - def test_realize_environment_allows_explicit_managed_base_override(tmp_path: Path) -> None: selected = resolved_project(tmp_path, authorized=False) local_reference = "local/devcapsule-base:test" @@ -404,16 +365,3 @@ def test_realize_environment_propagates_canonical_conflict_without_launch( ) require.assert_not_called() - - -@pytest.mark.parametrize("command", [None, "devcapsule", "devcapsule0"]) -def test_realizer_uses_resolved_shipped_command_choice(tmp_path, monkeypatch, command): - selected = resolved_project(tmp_path) - if command is not None: - selected.resolution["runtime"]["devcapsule-command"] = command - materialize = Mock(return_value=(CANONICAL_IMAGE, True)) - monkeypatch.setattr("devcapsule.environment_realization.ensure_materialized_surface", materialize) - monkeypatch.setattr("devcapsule.environment_realization.runtime_artifact", lambda _platform: tmp_path / "runtime.pex") - realize_environment(selected, root=tmp_path / "cache", obtain_image=lambda _ref: base_image(), - require_image=lambda _ref: completed_image(), build=Mock(), inspect_image=Mock()) - assert materialize.call_args.kwargs["runtime_command"] is RuntimeCommand(command or "devcapsule") diff --git a/devcapsule-src/tests/test_host_open.py b/devcapsule-src/tests/test_host_open.py index 31707e76..d1d27fe1 100644 --- a/devcapsule-src/tests/test_host_open.py +++ b/devcapsule-src/tests/test_host_open.py @@ -194,19 +194,6 @@ def test_physical_host_bridge_owns_and_cleans_broker(tmp_path: Path) -> None: assert not root.exists() -def test_macos_host_bridge_is_unavailable_with_a_warning(tmp_path: Path, capsys) -> None: - # Docker Desktop refuses a host Unix socket as a bind source, so the - # optional integration degrades instead of failing the launch. - with ( - patch("devcapsule.host_open.in_container", return_value=False), - patch("devcapsule.host_open.HostOs.current", return_value=HostOs.DARWIN), - host_open_bridge({"XDG_RUNTIME_DIR": str(tmp_path)}, enabled=True) as path, - ): - assert path is None - assert "host-browser is unavailable on macOS" in capsys.readouterr().err - assert list(tmp_path.iterdir()) == [] - - @pytest.mark.skipif(sys.platform != "linux", reason="the broker reads SO_PEERCRED, which only Linux has") def test_hidden_host_open_command_uses_authorized_environment(tmp_path: Path, capsys) -> None: assert "host-open" not in cli.command_names() diff --git a/devcapsule-src/tests/test_image_build.py b/devcapsule-src/tests/test_image_build.py index 51d1466b..3694852c 100644 --- a/devcapsule-src/tests/test_image_build.py +++ b/devcapsule-src/tests/test_image_build.py @@ -1,7 +1,6 @@ from __future__ import annotations from pathlib import Path -from unittest.mock import patch import pytest @@ -11,8 +10,7 @@ build_pycharm_image_spec, parse_pycharm_build_options, ) -from devcapsule.images.build import BuildxImageBuilder, ImageBuildSpec, normalize_pycharm_source, render_build_context -from devcapsule.platforms import Platform +from devcapsule.images.build import ImageBuildSpec, normalize_pycharm_source, render_build_context pytestmark = pytest.mark.legacy @@ -126,40 +124,6 @@ def test_render_build_context_includes_network_host_compatible_dockerfile_conten assert 'ENTRYPOINT ["/usr/bin/tini", "--", "/usr/local/bin/entrypoint.sh"]' in dockerfile -def test_buildx_builder_reuses_one_context_root_and_attaches_named_contexts(tmp_path: Path) -> None: - """BuildKit keys incremental transfer on the context root's path: the same - root every build, directories attached as named contexts, nothing copied.""" - from devcapsule.images.build import DirectoryComponent - - context_root = tmp_path / "build-contexts" / "context" - tree = tmp_path / "tree" - tree.mkdir() - (tree / "big.bin").write_bytes(b"x" * 1024) - spec = ImageBuildSpec(image="result:test", base_image="sha256:base", - components=(DirectoryComponent(tree, "/opt/tree"),)) - with patch("devcapsule.images.build.docker.build") as build: - BuildxImageBuilder(context_root=context_root).build(spec, platform=Platform.LINUX_AMD64, network="none") - (context_root / "stale-from-last-build").write_text("x") - BuildxImageBuilder(context_root=context_root).build(spec, platform=Platform.LINUX_AMD64, network="host") - first, second = build.call_args_list - assert first.args[0] == context_root and second.args[0] == context_root - assert first.kwargs["build_contexts"] == {"copy-dir-0": str(tree)} - assert first.kwargs["network"] == "none" and first.kwargs["allow"] == [] - assert second.kwargs["allow"] == ["network.host"] - assert sorted(child.name for child in context_root.iterdir()) == ["Dockerfile"] # cleared each build - assert (tmp_path / "build-contexts" / "context.lock").is_file() - assert not (context_root / "copy-dir-0").exists() - - -def test_buildx_builder_without_a_root_uses_a_temporary_context(tmp_path: Path) -> None: - with patch("devcapsule.images.build.docker.build") as build: - BuildxImageBuilder().build(ImageBuildSpec(image="result:test", base_image="sha256:base"), - platform=Platform.LINUX_AMD64, network="none") - used_context = build.call_args.args[0] - assert used_context.name.startswith("devcapsule-buildx-context-") and not used_context.exists() - assert build.call_args.kwargs["build_contexts"] == {} - - def test_named_contexts_preserve_distinct_contribution_inputs(tmp_path: Path) -> None: from devcapsule.images.build import ContributionComponent, DirectoryComponent @@ -183,23 +147,3 @@ def test_named_contexts_preserve_distinct_contribution_inputs(tmp_path: Path) -> assert "COPY --link --from=tool /opt/tool /opt/tool" in dockerfile assert (first / "payload").read_bytes() == b"first" assert (second / "payload").read_bytes() == b"second" - - -def test_reused_context_preserves_sources_and_recovers_after_build_failure(tmp_path: Path) -> None: - from devcapsule.images.build import DirectoryComponent - - source = tmp_path / "source" - source.mkdir() - (source / "payload").write_text("keep") - context = tmp_path / "context" - context.mkdir() - (context / "stale-link").symlink_to(source, target_is_directory=True) - spec = ImageBuildSpec("result:test", "base:test", (DirectoryComponent(source, "/opt/tool"),)) - with patch("devcapsule.images.build.docker.build", side_effect=OSError("build failed")): - with pytest.raises(CliError, match="build failed"): - BuildxImageBuilder(context).build(spec, platform=Platform.LINUX_AMD64) - with patch("devcapsule.images.build.docker.build") as build: - BuildxImageBuilder(context).build(spec, platform=Platform.LINUX_AMD64) - assert build.call_count == 1 - assert (source / "payload").read_text() == "keep" - assert sorted(path.name for path in context.iterdir()) == ["Dockerfile"] diff --git a/devcapsule-src/tests/test_linux_arm64_matrix.py b/devcapsule-src/tests/test_linux_arm64_matrix.py deleted file mode 100644 index 8f342328..00000000 --- a/devcapsule-src/tests/test_linux_arm64_matrix.py +++ /dev/null @@ -1,127 +0,0 @@ -"""The linux-arm64 matrix pins the linux-amd64 versions with arm64 artifacts only.""" - -from __future__ import annotations - -import json -import re -from typing import Any - -import pytest - -from devcapsule.components.linux_arm64_pins import COMPONENT_LOCK_TABLES -from devcapsule.configuration.capability_selection import pins_for_platform -from devcapsule.configuration.file_formats import ProjectConfigurationError -from devcapsule.platforms import Platform -from devcapsule.resolution_matrix import MATRICES - -AMD64 = MATRICES[Platform.LINUX_AMD64] -ARM64 = MATRICES[Platform.LINUX_ARM64] -AMD64_MARKERS = re.compile(r"x86[_-]64|[_-]x64|amd64|linux64/|cft/[^/]+/linux64|ffmpeg-linux\.zip") - - -def _artifacts(value: Any) -> list[dict[str, Any]]: - """Every table at any depth that names a download by ``url``.""" - if isinstance(value, dict): - own = [value] if "url" in value else [] - return own + [found for item in value.values() for found in _artifacts(item)] - if isinstance(value, (list, tuple)): - return [found for item in value for found in _artifacts(item)] - return [] - - -def test_every_arm64_pin_is_a_version_amd64_pins_and_the_matrix_serves() -> None: - # The converse, an amd64 version without an arm64 pin, fails at import. - for (component, version), table in COMPONENT_LOCK_TABLES.items(): - assert AMD64.component_pin(component, version) is not None, (component, version) - assert ARM64.component_pin(component, version) == table, (component, version) - - -def test_no_arm64_pin_names_an_amd64_artifact() -> None: - for key, table in COMPONENT_LOCK_TABLES.items(): - for artifact in _artifacts(table): - assert not AMD64_MARKERS.search(str(artifact["url"])), (key, artifact["url"]) - - -def test_every_arm64_artifact_is_hash_pinned() -> None: - for key, table in COMPONENT_LOCK_TABLES.items(): - for artifact in _artifacts(table): - assert re.fullmatch(r"[0-9a-f]{64}", str(artifact.get("sha256"))), (key, artifact) - - -def test_arm64_artifacts_are_not_amd64_bytes() -> None: - # Architecture-independent files are shared: Codex's JavaScript package, - # pure-Python wheels, and Ubuntu _all packages. Everything else differs. - shared = {artifact["sha256"] for (component, version) in COMPONENT_LOCK_TABLES - for artifact in _artifacts(AMD64.component_pin(component, version))} - for (component, version), table in COMPONENT_LOCK_TABLES.items(): - for artifact in _artifacts(table): - url = str(artifact["url"]) - independent = url.endswith(("_all.deb", "-none-any.whl")) or url.endswith(f"codex-{version}.tgz") - if not independent: - assert artifact["sha256"] not in shared, (component, version, url) - - -def test_platform_fields_and_artifact_keys_name_arm64() -> None: - for (component, _version), table in COMPONENT_LOCK_TABLES.items(): - if "platform" in table: - assert table["platform"] == "linux-arm64", component - if component in {"codex", "claude-code", "antigravity-cli"}: - assert list(table["artifacts"]) == ["linux-arm64"], component - assert "linux-amd64" not in json.dumps(list(COMPONENT_LOCK_TABLES.values())) - - -def test_eclipse_native_closure_is_arm64_or_architecture_independent() -> None: - packages = ARM64.component_pin("eclipse", "2026-09-R")["native-packages"] - # The same packages, plus what arm64's own dependencies add. - assert set(packages) - set(AMD64.component_pin("eclipse", "2026-09-R")["native-packages"]) == {"libunwind8"} - for name, entry in packages.items(): - # Architecture-independent packages keep the amd64 closure's bytes. - if entry["url"].endswith("_all.deb"): - continue - assert entry["url"].startswith("https://ports.ubuntu.com/ubuntu-ports/pool/"), name - assert entry["url"].endswith("_arm64.deb"), name - - -def test_arm64_base_is_its_own_unpublished_recipe_11_pin() -> None: - (base,) = ARM64.base_images() - assert base.contract.recipe == 11 - assert base.reference.startswith("docker.io/mycodespaceai/devcapsule-base@sha256:") - assert base.reference not in {image.reference for image in AMD64.base_images()} - assert ARM64.base_unpublished(base.reference) - assert not any(AMD64.base_unpublished(image.reference) for image in AMD64.base_images()) - - -@pytest.mark.parametrize("ide", ["frontend-ide", "vscode-ide", "eclipse-ide", "python-ide", "java-ide"]) -def test_each_ide_smoked_natively_on_arm64_resolves_verified(ide: str) -> None: - assert ARM64.resolve([ide]).unverified == () - - -def _personal_pins(platform: str, component: str, matrix: Any) -> dict[str, Any]: - pin = matrix.component_pins()[component] - return {"platform": platform, "components": {component: dict(pin), "interactive-surface": component}} - - -def test_personal_pins_take_the_artifacts_of_the_platform_they_run_on() -> None: - # A checkout chose codium while the project ran as linux-amd64; the - # project then gained an arm64 lock. Its pins must deliver arm64 bytes. - amd64 = _personal_pins("linux-amd64", "codium", AMD64) - on_arm64 = pins_for_platform(amd64, ARM64) - assert on_arm64["platform"] == "linux-arm64" - assert on_arm64["components"]["codium"] == dict(ARM64.component_pins()["codium"]) - assert on_arm64["components"]["interactive-surface"] == "codium" - assert amd64["components"]["codium"] == dict(AMD64.component_pins()["codium"]) # input unchanged - # A table that is another platform's embedded pin is that platform's, - # whatever the label says, so a mislabelled record takes arm64's bytes too. - mislabelled = dict(amd64, platform="linux-arm64") - assert pins_for_platform(mislabelled, ARM64)["components"]["codium"] == dict(ARM64.component_pins()["codium"]) - # Pins written for the platform they run on are records, returned as they stand. - recorded = {**amd64, "components": {**amd64["components"], "codium": {**amd64["components"]["codium"], "note": "kept"}}} - assert pins_for_platform(recorded, AMD64) == recorded - assert pins_for_platform(on_arm64, AMD64)["components"]["codium"] == dict(AMD64.component_pins()["codium"]) - - -def test_a_personal_version_the_other_platform_lacks_asks_for_a_new_choice() -> None: - channel = {"platform": "linux-amd64", "components": {"codex": {"version": "9.9.9", "url": "https://example.test/x64.tgz"}}} - with pytest.raises(ProjectConfigurationError, match="codex 9.9.9 is pinned for linux-amd64.*linux-arm64 has no pin"): - pins_for_platform(channel, ARM64) - assert pins_for_platform(channel, AMD64)["components"]["codex"]["version"] == "9.9.9" diff --git a/devcapsule-src/tests/test_owner_commands.py b/devcapsule-src/tests/test_owner_commands.py index 5bdea0f4..a52d30e3 100644 --- a/devcapsule-src/tests/test_owner_commands.py +++ b/devcapsule-src/tests/test_owner_commands.py @@ -11,14 +11,13 @@ from devcapsule import cli from devcapsule.configuration import owner_commands -from devcapsule.configuration.capability_commands import paths, recover +from devcapsule.configuration.capability_commands import paths from devcapsule.configuration.file_formats import ProjectConfigurationError, render_toml from devcapsule.configuration.manifest_document import render_manifest from devcapsule.configuration.storage import load_toml from devcapsule.configuration.storage import checkout_record_paths from devcapsule.platforms import Platform from devcapsule.resolution_matrix import MATRICES -from tests.test_platforms import on_host pytestmark = pytest.mark.legacy @@ -222,8 +221,6 @@ def test_an_unrelated_command_never_rewrites_a_legacy_manifest(legacy_project: P assert manifest_text(legacy_project) == before - - def test_drop_offered_tool_removes_only_its_declaration_and_reason(project: Path) -> None: owner_commands.require_tools(project, ["python"], because="backend") owner_commands.offer_tools(project, ["playwright"], because="browser tests") @@ -424,63 +421,3 @@ def test_sources_survive_other_owner_edits_and_do_not_change_pending_decisions(p {"kind": "source", "name": "notes", "effective": "no toolchain declared", "source": "project: upstream, read-only", "pending": "", "command": ""}, {"kind": "source", "name": "vendor/robot.code", "effective": "java 17", "source": "project: fork of https://example.test/up", "pending": "", "command": ""}, ] - - -@pytest.fixture -def two_platform_project(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: - """A project on Apple Silicon with a linux-amd64 lock and a linux-arm64 lock added beside it.""" - monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) - monkeypatch.setenv("HOME", str(tmp_path / "home")) - on_host(monkeypatch, "Darwin", "arm64") - root = tmp_path / "project" - root.mkdir() - assert cli.main(["project", "--path", str(root), "init", "--platform", "linux-amd64", "--require", "python", - "--creator", "mailto:owner@example.test"]) == 0 - assert cli.main(["project", "--path", str(root), "init", "--platform", "linux-arm64"]) == 0 - return root - - -def _locked_components(root: Path) -> dict[str, set[str]]: - return {path.name: set(load_toml(path).get("components", {})) - for path in sorted((root / ".devcapsule").glob("devcapsule.*.lock"))} - - -@pytest.mark.own_host_patches -def test_an_owner_edit_regenerates_every_platform_lock(two_platform_project: Path, capsys) -> None: - # Edited on the Mac, the committed linux-amd64 lock follows the manifest too. - assert cli.main(["project", "--path", str(two_platform_project), "config", "require", "dotnet", "--unverified"]) == 0 - assert "the manifest and its locks are updated" in capsys.readouterr().out - assert _locked_components(two_platform_project) == { - "devcapsule.linux-amd64.lock": {"dotnet-sdk"}, "devcapsule.linux-arm64.lock": {"dotnet-sdk"}, - } - - -@pytest.mark.own_host_patches -def test_an_edit_one_lock_cannot_follow_changes_nothing(two_platform_project: Path) -> None: - # The .NET SDK has no linux-arm64 validation yet; linux-amd64 alone would accept it. - before = {path: path.read_bytes() for path in (two_platform_project / ".devcapsule").iterdir() if path.is_file()} - with pytest.raises(ProjectConfigurationError, match="linux-arm64 lock cannot follow this edit"): - owner_commands.require_tools(two_platform_project, ["dotnet"]) - assert {path: path.read_bytes() for path in before} == before - - -@pytest.mark.own_host_patches -@pytest.mark.parametrize("committed", [False, True]) -def test_an_interrupted_edit_recovers_every_lock_to_one_side(two_platform_project: Path, committed: bool) -> None: - devcapsule = two_platform_project / ".devcapsule" - manifest, locks = paths(two_platform_project)[0], sorted(devcapsule.glob("devcapsule.*.lock")) - before = {path: path.read_text() for path in (manifest, *locks)} - owner_commands.require_tools(two_platform_project, ["dotnet"], allow_unverified=True) - after = {path: path.read_text() for path in (manifest, *locks)} - assert all(before[path] != after[path] for path in locks) - journal = {"before-manifest": before[manifest], "after-manifest": after[manifest], - "before-locks": {path.name.split(".")[1]: before[path] for path in locks}, - "after-locks": {path.name.split(".")[1]: after[path] for path in locks}} - paths(two_platform_project)[2].write_text(render_toml(journal)) - if not committed: - manifest.write_text(before[manifest]) - locks[0].write_text(before[locks[0]]) # Interrupted between the two lock writes. - recover(two_platform_project) - expected = after if committed else before - assert {path: path.read_text() for path in (manifest, *locks)} == expected - assert not paths(two_platform_project)[2].exists() diff --git a/devcapsule-src/tests/test_platforms.py b/devcapsule-src/tests/test_platforms.py index 6011c41c..b50cce93 100644 --- a/devcapsule-src/tests/test_platforms.py +++ b/devcapsule-src/tests/test_platforms.py @@ -7,19 +7,11 @@ import pytest from devcapsule import platforms -from devcapsule.platforms import HostOs, Platform, UnsupportedPlatformError, XdgHomes +from devcapsule.platforms import Platform, UnsupportedPlatformError, XdgHomes pytestmark = pytest.mark.legacy -def test_platform_values_are_the_wire_format() -> None: - assert Platform.LINUX_AMD64.value == "linux-amd64" - # StrEnum: the member formats as its value wherever locks and filenames - # interpolate it. - assert f"devcapsule.{Platform.LINUX_AMD64}.lock" == "devcapsule.linux-amd64.lock" - assert Platform.LINUX_ARM64.value == "linux-arm64" - - def test_parse_accepts_every_member_and_only_members() -> None: for member in Platform: assert Platform.parse(member.value) is member @@ -38,79 +30,6 @@ def test_current_returns_a_member_on_a_supported_host() -> None: assert Platform.parse(current.value) is current -def on_host(monkeypatch: pytest.MonkeyPatch, system: str, machine: str) -> None: - """Make this host the one ``platform.system()`` and ``platform.machine()`` would name.""" - monkeypatch.setattr(platforms.host_platform, "system", lambda: system) - monkeypatch.setattr(platforms.host_platform, "machine", lambda: machine) - - -@pytest.mark.own_host_patches -@pytest.mark.parametrize("system,machine,runnable,created", [ - ("Linux", "x86_64", ("linux-amd64",), "linux-amd64"), - ("Linux", "aarch64", ("linux-arm64",), "linux-arm64"), - ("Darwin", "x86_64", ("linux-amd64",), "linux-amd64"), - ("Darwin", "arm64", ("linux-arm64", "linux-amd64"), "linux-arm64"), -]) -def test_each_host_runs_its_platforms_and_creates_projects_for_one(monkeypatch, system, machine, runnable, created) -> None: - # A Mac runs Linux capsules. Apple Silicon runs linux-arm64 natively and - # linux-amd64 under emulation; a new project defaults to the native one. - on_host(monkeypatch, system, machine) - assert tuple(str(platform) for platform in Platform.runnable()) == runnable - assert Platform.current() is Platform.parse(created) - - -def test_emulation_is_the_daemons() -> None: - # Docker Desktop on Apple Silicon is an arm64 daemon, whatever runs the launcher. - assert Platform.LINUX_AMD64.emulated_docker_platform("arm64") == "linux/amd64" - assert Platform.LINUX_ARM64.emulated_docker_platform("arm64") is None - assert Platform.LINUX_AMD64.docker_platform_args("arm64") == ["--platform=linux/amd64"] - assert Platform.LINUX_AMD64.docker_platform_args("amd64") == [] - assert Platform.LINUX_ARM64.docker_platform_args("arm64") == [] - - -@pytest.mark.own_host_patches -@pytest.mark.parametrize("system,machine,message", [ - ("Linux", "riscv64", "supported platforms: linux-amd64, linux-arm64"), - ("Darwin", "ppc", "supported platforms: linux-amd64, linux-arm64"), - ("Windows", "AMD64", "supported: linux, darwin"), -]) -def test_current_refuses_a_host_without_a_capsule_platform(monkeypatch, system, machine, message) -> None: - on_host(monkeypatch, system, machine) - with pytest.raises(UnsupportedPlatformError, match=message): - Platform.current() - - -def test_an_elf_machine_names_its_platform() -> None: - assert Platform.of_elf_machine(0x3E) is Platform.LINUX_AMD64 - assert Platform.of_elf_machine(0xB7) is Platform.LINUX_ARM64 - assert Platform.of_elf_machine(0xF3) is None - - -def test_docker_desktop_facts_are_the_macos_hosts() -> None: - assert HostOs.DARWIN.docker_socket_gid == 0 and HostOs.LINUX.docker_socket_gid is None - assert not HostOs.DARWIN.host_network_reachable and HostOs.LINUX.host_network_reachable - assert not HostOs.DARWIN.binds_unix_sockets and HostOs.LINUX.binds_unix_sockets - assert not HostOs.DARWIN.chown_reaches_bind_mounts and HostOs.LINUX.chown_reaches_bind_mounts - - -def test_docker_spelling_and_architecture() -> None: - assert Platform.LINUX_AMD64.docker_platform == "linux/amd64" - assert Platform.LINUX_AMD64.architecture == "amd64" - assert Platform.LINUX_ARM64.docker_platform == "linux/arm64" - assert Platform.LINUX_ARM64.architecture == "arm64" - - -@pytest.mark.own_host_patches -def test_host_os_names_linux_and_macos_only(monkeypatch) -> None: - on_host(monkeypatch, "Darwin", "arm64") - assert HostOs.current() is HostOs.DARWIN - on_host(monkeypatch, "Linux", "x86_64") - assert HostOs.current() is HostOs.LINUX - on_host(monkeypatch, "Windows", "AMD64") - with pytest.raises(UnsupportedPlatformError, match="'windows'"): - HostOs.current() - - def test_xdg_homes_honor_explicit_overrides() -> None: homes = XdgHomes.from_environment( { diff --git a/devcapsule-src/tests/test_project_platform_selection.py b/devcapsule-src/tests/test_project_platform_selection.py deleted file mode 100644 index a1c2d61c..00000000 --- a/devcapsule-src/tests/test_project_platform_selection.py +++ /dev/null @@ -1,130 +0,0 @@ -"""The capsule platform a project runs as: the first one this host runs that it has a lock for.""" - -from __future__ import annotations - -import io -from pathlib import Path - -import pytest - -from devcapsule import cli -from devcapsule.configuration.file_formats import ProjectConfigurationError -from devcapsule.configuration.operations import InitializeRequest, initialize_project -from devcapsule.configuration.storage import load_toml, project_platform -from devcapsule.platforms import Platform -from devcapsule.resolution_matrix import MATRICES -from tests.test_platforms import on_host - - -def _project(root: Path, *locked: str) -> Path: - (root / ".devcapsule").mkdir(parents=True) - (root / ".devcapsule" / "devcapsule.toml").write_text("") - for platform in locked: - (root / ".devcapsule" / f"devcapsule.{platform}.lock").write_text("") - return root - - -@pytest.mark.own_host_patches -@pytest.mark.parametrize("locked,expected", [ - ((), "linux-arm64"), - (("linux-amd64",), "linux-amd64"), - (("linux-arm64",), "linux-arm64"), - (("linux-amd64", "linux-arm64"), "linux-arm64"), -]) -def test_apple_silicon_runs_a_project_as_the_platform_it_is_locked_for( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, locked: tuple[str, ...], expected: str, -) -> None: - # Natively when the project has an arm64 lock, else its amd64 lock under - # emulation; a project without a lock is created for the native platform. - on_host(monkeypatch, "Darwin", "arm64") - assert project_platform(_project(tmp_path / "project", *locked)) is Platform.parse(expected) - - -@pytest.mark.own_host_patches -def test_a_host_runs_a_project_only_as_a_platform_it_runs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - # An amd64 Linux host cannot run an arm64-only project; it keeps its own - # platform, and the missing lock is reported where the lock is read. - on_host(monkeypatch, "Linux", "x86_64") - assert project_platform(_project(tmp_path / "project", "linux-arm64")) is Platform.LINUX_AMD64 - - -@pytest.mark.own_host_patches -def test_init_platform_adds_that_lock_and_the_project_runs_as_it(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - on_host(monkeypatch, "Darwin", "arm64") - project = tmp_path / "project" - project.mkdir() - initialize_project(InitializeRequest(directory=project, creator="owner@example.test", - platform=Platform.LINUX_AMD64, interactive=False)) - initialize_project(InitializeRequest(directory=project, platform=Platform.LINUX_ARM64, interactive=False)) - locks = sorted(path.name for path in (project / ".devcapsule").glob("*.lock")) - assert locks == ["devcapsule.linux-amd64.lock", "devcapsule.linux-arm64.lock"] - arm64_lock = load_toml(project / ".devcapsule" / "devcapsule.linux-arm64.lock") - assert MATRICES[Platform.LINUX_ARM64].base_unpublished(arm64_lock["base"]["reference"]) - assert project_platform(project) is Platform.LINUX_ARM64 - - -@pytest.mark.own_host_patches -def test_init_refuses_a_lock_the_project_would_not_run_as(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: - # Init completes the checkout for the lock it writes; with an arm64 lock - # the project runs as arm64 here, so an amd64 lock cannot be added here. - on_host(monkeypatch, "Darwin", "arm64") - project = _project(tmp_path / "project", "linux-arm64") - with pytest.raises(ProjectConfigurationError, match="runs as linux-arm64"): - initialize_project(InitializeRequest(directory=project, platform=Platform.LINUX_AMD64, interactive=False)) - assert not (project / ".devcapsule" / "devcapsule.linux-amd64.lock").exists() - - -def test_init_platform_refuses_a_platform_this_host_cannot_run( - tmp_path: Path, capsys: pytest.CaptureFixture[str], -) -> None: - # The suite's host runs linux-amd64 only. - project = _project(tmp_path / "project", "linux-amd64") - assert cli.main(["project", "--path", str(project), "init", "--platform", "linux-arm64"]) != 0 - assert "this host runs linux-amd64 capsules, not linux-arm64" in capsys.readouterr().err - assert cli.main(["project", "--path", str(project), "init", "--platform", "windows-amd64"]) != 0 - assert "Unsupported platform 'windows-amd64'" in capsys.readouterr().err - - -def _init_answering(project: Path, answers: str) -> str: - """Run an interactive init of ``project``, answering each question in turn; return what it asked.""" - asked = io.StringIO() - initialize_project( - InitializeRequest(directory=project, creator="owner@example.test", interactive=True), - input_stream=io.StringIO(answers), output_stream=asked, - ) - return asked.getvalue() - - -@pytest.mark.own_host_patches -@pytest.mark.parametrize("answer,expected", [("", "linux-arm64"), ("linux-amd64", "linux-amd64")]) -def test_init_asks_a_host_of_two_platforms_the_native_one_by_default( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, answer: str, expected: str, -) -> None: - on_host(monkeypatch, "Darwin", "arm64") - project = tmp_path / "project" - project.mkdir() - asked = _init_answering(project, f"{answer}\n" + "\n" * 8) - assert ("Capsule platform for this project: linux-arm64 (native; its base is built locally) " - "or linux-amd64 (emulated) [linux-arm64]:") in asked - assert [path.name for path in (project / ".devcapsule").glob("*.lock")] == [f"devcapsule.{expected}.lock"] - - -@pytest.mark.own_host_patches -def test_init_keeps_the_platform_of_a_lock_the_host_runs_without_asking( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, -) -> None: - on_host(monkeypatch, "Darwin", "arm64") - project = tmp_path / "project" - project.mkdir() - initialize_project(InitializeRequest(directory=project, creator="owner@example.test", - platform=Platform.LINUX_AMD64, interactive=False)) - assert "Capsule platform" not in _init_answering(project, "\n" * 8) - assert [path.name for path in (project / ".devcapsule").glob("*.lock")] == ["devcapsule.linux-amd64.lock"] - - -def test_a_host_of_one_platform_is_not_asked(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - assert "Capsule platform" not in _init_answering(project, "\n" * 8) - assert (project / ".devcapsule" / "devcapsule.linux-amd64.lock").is_file() - diff --git a/devcapsule-src/tests/test_pycharm.py b/devcapsule-src/tests/test_pycharm.py index 036c22e0..2b6dc666 100644 --- a/devcapsule-src/tests/test_pycharm.py +++ b/devcapsule-src/tests/test_pycharm.py @@ -1,8 +1,6 @@ from __future__ import annotations -from datetime import datetime, timedelta, timezone from pathlib import Path -import socket import time from types import SimpleNamespace from typing import cast @@ -11,50 +9,28 @@ import pytest from devcapsule.components.pycharm import runtime_template as pycharm_runtime_template -from devcapsule.components.codex import runtime_template as codex_runtime_template -from devcapsule.components.codium import runtime_template as codium_runtime_template -from devcapsule.launch.pycharm import ( - DockerMode, - IdeConfigMode, - PycharmRunOptions, - build_run_config, -) +from devcapsule.launch.pycharm import DockerMode, PycharmRunOptions, build_run_config from devcapsule.container_runtime.contract import INFRASTRUCTURE_FAILURE_EXIT_CODE -from devcapsule.platforms import HostOs, Platform +from devcapsule.platforms import Platform from devcapsule.launch.pycharm._launcher import ( AttachedResult, - display_disclosure, describe_run_command, console_development_source, console_disclosure, HostUser, PycharmRunError, PycharmRunConfig, - SUDOERS_POLICY, - SUDOERS_POLICY_PATH, TempRuntimeFiles, build_docker_args, cleanup_temp_runtime_files, - ensure_sudoers_policy_ownership, prepare_temp_runtime_files, print_storage_summary, - remove_abandoned_sudoers_volumes, - run_pycharm, write_user_files, ) from devcapsule.container_runtime.contract import DisplayPlan, Identity, LogsPlan, RuntimePlan from devcapsule.launch.session_logs import SESSION_LOG_DESTINATION -from devcapsule.materialization import RUNTIME_PLAN_PATH -from devcapsule.host_open import ( - HOST_OPEN_BROWSER, - HOST_OPEN_INTEGRATION, - HOST_OPEN_SOCKET_DESTINATION, - HOST_OPEN_SOCKET_ENV, -) -from devcapsule.project import plan_project from devcapsule.container_runtime.contract import ConsolePlan -from devcapsule.display_client import display_url pytestmark = pytest.mark.legacy @@ -76,1028 +52,6 @@ def external_runtime_plan() -> RuntimePlan: ) -def test_external_runtime_plan_is_readable_and_mounted_read_only(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - with ( - patch("devcapsule.launch.pycharm._launcher.write_xauthority"), - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - ): - files = prepare_temp_runtime_files(config, env) - try: - assert files.runtime_plan_file is not None - assert files.runtime_plan_file.stat().st_mode & 0o777 == 0o644 - # The plan now records the transport explicitly, even for passthrough, - # and the web console, which runs whenever the runtime runs. - written = RuntimePlan.from_file(files.runtime_plan_file) - assert written.display == DisplayPlan.host_x11() - assert written.console == ConsolePlan("0.0.0.0", 6081, "/run/devcapsule-console-token") - assert written.logs == LogsPlan(SESSION_LOG_DESTINATION, segment_bytes=8 * 1024 * 1024, rotations=4) - assert written == external_runtime_plan().with_display(DisplayPlan.host_x11()).with_console(written.console).with_logs(written.logs) - args = build_docker_args(config, files, env) - assert "JAVA_TOOL_OPTIONS=-Dide.browser.jcef.sandbox.enable=false" in args - assert "SYS_ADMIN" not in args - assert "seccomp=unconfined" not in args - assert "apparmor=unconfined" not in args - assert ( - f"type=bind,src={files.runtime_plan_file},dst={RUNTIME_PLAN_PATH},ro" - in args - ) - finally: - cleanup_temp_runtime_files(files) - assert files.runtime_plan_file is not None - assert not files.runtime_plan_file.exists() - - -def codium_external_runtime_plan() -> RuntimePlan: - return RuntimePlan.for_component( - codium_runtime_template(), - project_path="/workspace/project", - home="/home/devcapsule", - identity=Identity(1000, 1000, "developer"), - ) - - -def codium_interactive_state_mounts(tmp_path: Path) -> dict[str, tuple[Path, str]]: - return { - "codium/user-data": (tmp_path / "slots" / "user-data", "/ide-user-data"), - "codium/extensions": (tmp_path / "slots" / "extensions", "/ide-extensions"), - "codium/cache": (tmp_path / "slots" / "cache", "/home/devcapsule/.cache"), - } - - -def test_plan_driven_surface_mounts_declared_slots_instead_of_pycharm_paths( - tmp_path: Path, -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=codium_external_runtime_plan(), - use_image_process=True, - interactive_state_mounts=codium_interactive_state_mounts(tmp_path), - ), - env, - ) - - assert config.ide_config is None - assert config.plugins is None - assert config.ide_system is None - assert config.ide_log is None - assert config.tool_cache is None - - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - runtime_plan_file=tmp_path / "runtime-plan", - ) - args = build_docker_args(config, files, env) - - assert f"type=bind,src={tmp_path / 'slots' / 'user-data'},dst=/ide-user-data" in args - assert f"type=bind,src={tmp_path / 'slots' / 'extensions'},dst=/ide-extensions" in args - assert ( - f"type=bind,src={tmp_path / 'slots' / 'cache'},dst=/home/devcapsule/.cache" in args - ) - assert not any("dst=/ide-config" in value for value in args) - assert not any("dst=/ide-plugins" in value for value in args) - assert not any("dst=/ide-project-state" in value for value in args) - # Renderer sandboxing is retired (product-owner ruling 2026-09-02): - # codium runs --no-sandbox and keeps the uniform full hardening. - assert "no-new-privileges" in args - assert ["--cap-drop", "ALL"] == args[args.index("--cap-drop") : args.index("--cap-drop") + 2] - assert "--cap-add" not in args - # No PyCharm state directories are invented on the host for this surface. - data_namespace = Path(env["XDG_DATA_HOME"]) / "devcapsule" / "projects" / "by-path" - assert not list(data_namespace.glob("*/components/pycharm")) - - -def test_interactive_state_mounts_require_a_runtime_plan(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - with pytest.raises(PycharmRunError, match="external runtime plan"): - build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - interactive_state_mounts=codium_interactive_state_mounts(tmp_path), - ), - base_env(tmp_path), - ) - - -def test_interactive_state_mounts_must_belong_to_the_plan_component(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - with pytest.raises(PycharmRunError, match="Interactive state mount 'codex/home'"): - build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=codium_external_runtime_plan(), - use_image_process=True, - interactive_state_mounts={ - "codex/home": (tmp_path / "codex", "/home/devcapsule/.codex"), - }, - ), - base_env(tmp_path), - ) - - -def test_no_sudo_launches_keep_full_hardening_on_every_surface( - tmp_path: Path, -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - for plan in (external_runtime_plan(), codium_external_runtime_plan()): - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=plan, - use_image_process=True, - interactive_state_mounts=( - codium_interactive_state_mounts(tmp_path) - if plan.component.id == "codium" - else {} - ), - ), - env, - ) - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - runtime_plan_file=tmp_path / "runtime-plan", - ) - - args = build_docker_args(config, files, env) - - assert "no-new-privileges" in args - assert "SYS_ADMIN" not in args - - -def test_sudo_launches_grant_no_capabilities_beyond_dockers_default( - tmp_path: Path, -) -> None: - """Regression for the 2026-09-02 sudo-sandbox bug: the sudo posture is - Docker's default capability set plus the sudo group — no per-surface - capability grants, and no setuid-helper posture left to crash the zygote. - """ - - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=codium_external_runtime_plan(), - use_image_process=True, - interactive_state_mounts=codium_interactive_state_mounts(tmp_path), - enable_sudo=True, - ), - env, - ) - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - runtime_plan_file=tmp_path / "runtime-plan", - shadow_file=tmp_path / "shadow", - sudoers_file=tmp_path / "sudoers", - ) - - args = build_docker_args(config, files, env) - - assert "--group-add" in args - assert "--cap-add" not in args - assert "--cap-drop" not in args - assert "no-new-privileges" not in args - - -def test_plan_driven_storage_summary_names_the_surface_and_its_slots( - tmp_path: Path, capsys: pytest.CaptureFixture[str] -) -> None: - project = tmp_path / "project" - project.mkdir() - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=codium_external_runtime_plan(), - use_image_process=True, - interactive_state_mounts=codium_interactive_state_mounts(tmp_path), - ), - base_env(tmp_path), - ) - - print_storage_summary(config) - - output = capsys.readouterr().err - assert "codium storage:" in output - assert "codium/user-data" in output - assert "PyCharm config" not in output - # The unsandboxed-renderer disclosure (product-owner ruling 2026-09-02) - # keys on the --no-sandbox flag the template carries. - assert "Chromium renderers run unsandboxed" in output - assert "Docker's outer isolation is unchanged" in output - - -def test_detached_lifecycle_changes_only_the_docker_process_flags(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - runtime_plan_file=tmp_path / "runtime-plan", - ) - - args = build_docker_args(config, files, env) - - # The launcher owns exactly one lifecycle: attached, removed on exit. - assert args[:2] == ["--rm", "-i"] - assert "--detach" not in args and "-d" not in args - - -def test_jcef_disclosure_is_printed_for_component_policy( - tmp_path: Path, capsys: pytest.CaptureFixture[str] -) -> None: - project = tmp_path / "project" - project.mkdir() - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - base_env(tmp_path), - ) - - print_storage_summary(config) - - disclosure = capsys.readouterr().err - assert "JCEF's inner Chromium sandbox is disabled" in disclosure - assert "Docker's outer isolation is unchanged" in disclosure - - -def test_host_browser_bridge_is_explicit_in_plan_mount_environment_and_summary( - tmp_path: Path, - capsys: pytest.CaptureFixture[str], -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - socket_path = tmp_path / "host-open.sock" - - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as broker: - broker.bind(str(socket_path)) - env[HOST_OPEN_SOCKET_ENV] = str(socket_path) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - enable_host_browser=True, - ), - env, - ) - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - runtime_plan_file=tmp_path / "runtime-plan", - ) - args = build_docker_args(config, files, env) - print_storage_summary(config) - - assert config.runtime_plan is not None - assert config.runtime_plan.host_integrations == (HOST_OPEN_INTEGRATION,) - assert f"BROWSER={HOST_OPEN_BROWSER}" in args - assert f"{HOST_OPEN_SOCKET_ENV}={HOST_OPEN_SOCKET_DESTINATION}" in args - assert ( - f"type=bind,src={socket_path},dst={HOST_OPEN_SOCKET_DESTINATION},ro" in args - ) - disclosure = capsys.readouterr().err - assert "Any process running as the" in disclosure - assert "capsule user can ask the physical host" in disclosure - - -def test_host_browser_bridge_can_be_disabled_despite_inherited_environment( - tmp_path: Path, -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - socket_path = tmp_path / "host-open.sock" - - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as broker: - broker.bind(str(socket_path)) - env[HOST_OPEN_SOCKET_ENV] = str(socket_path) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - docker_mode=DockerMode.none, - enable_host_browser=False, - ), - env, - ) - - assert config.host_browser_socket is None - - -def test_physical_host_launcher_owns_browser_broker_for_docker_lifetime( - tmp_path: Path, -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - - with ( - patch("devcapsule.host_open.in_container", return_value=False), - patch("devcapsule.host_open.HostOs.current", return_value=HostOs.LINUX), - patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), - patch("devcapsule.launch.pycharm._launcher.run_attached") as run, - ): - run.return_value = AttachedResult(0, interrupted=False, stopped=True) - assert run_pycharm( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - docker_mode=DockerMode.none, - enable_host_browser=True, - ), - env, - ) == 0 - - command = run.call_args.args[0] - socket_mount = next( - argument - for argument in command - if argument.startswith("type=bind,src=") - and f"dst={HOST_OPEN_SOCKET_DESTINATION}" in argument - ) - source = Path( - next( - field.removeprefix("src=") - for field in socket_mount.split(",") - if field.startswith("src=") - ) - ) - assert not source.exists() - - -def test_selected_codex_state_and_explicit_secret_are_delivered(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - env["OPENAI_API_KEY"] = "sk-test-never-in-docker-args" - runtime = RuntimePlan.for_component( - pycharm_runtime_template(), - project_path="/workspace/project", - home="/home/devcapsule", - identity=Identity(1000, 1000, "developer"), - ancillary_templates=(codex_runtime_template(),), - ) - state = tmp_path / "codex-home" - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=runtime, - use_image_process=True, - additional_state_mounts={"codex/home": (state, "/home/devcapsule/.codex")}, - secret_environment=("OPENAI_API_KEY",), - ), - env, - ) - with ( - patch("devcapsule.launch.pycharm._launcher.write_xauthority"), - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - ): - files = prepare_temp_runtime_files(config, env) - try: - args = build_docker_args(config, files, env) - finally: - cleanup_temp_runtime_files(files) - - assert "CODEX_HOME=/home/devcapsule/.codex" in args - assert "OPENAI_API_KEY" in args - assert "sk-test-never-in-docker-args" not in args - assert f"type=bind,src={state.resolve()},dst=/home/devcapsule/.codex" in args - - -def test_authorized_runtime_effects_build_the_complete_docker_plan(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - docker_socket = tmp_path / "docker.sock" - docker_socket.touch() - home = tmp_path / "persistent-home" - config_dir = tmp_path / "pycharm-config" - plugins = tmp_path / "pycharm-plugins" - system = tmp_path / "pycharm-system" - log = tmp_path / "pycharm-log" - cache = tmp_path / "pycharm-cache" - host_user = HostUser(uid=1000, gid=1000, name="developer", group_name="developer") - - with ( - patch("devcapsule.launch.pycharm._launcher.current_host_user", return_value=host_user), - patch("devcapsule.launch.pycharm._launcher.is_socket", return_value=True), - ): - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - persistent_home=home, - ide_config=config_dir, - plugins=plugins, - ide_system=system, - ide_log=log, - tool_cache=cache, - docker_mode=DockerMode.host, - host_docker_socket=docker_socket, - enable_sudo=True, - network_mode="host", - memory_limit_bytes=8 * 1024**3, - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - with patch("devcapsule.launch.pycharm._launcher.write_xauthority"): - files = prepare_temp_runtime_files(config, env) - try: - assert files.sudoers_file is not None - assert files.sudoers_directory is not None - sudoers_directory = files.sudoers_directory - assert sudoers_directory.stat().st_mode & 0o777 == 0o700 - sudoers_file = files.sudoers_file - assert sudoers_file.read_text(encoding="utf-8") == SUDOERS_POLICY - assert sudoers_file.stat().st_mode & 0o777 == 0o440 - args = build_docker_args(config, files, env) - files.sudoers_volume = "capsule-sudoers" - volume_args = build_docker_args(config, files, env) - files.sudoers_volume = None - files.sudoers_file = None - with pytest.raises(PycharmRunError, match="shadow and sudoers files"): - build_docker_args(config, files, env) - files.sudoers_file = sudoers_file - finally: - cleanup_temp_runtime_files(files) - - assert ["--network", "host"] == args[args.index("--network") : args.index("--network") + 2] - assert ["--memory", str(8 * 1024**3)] == args[args.index("--memory") : args.index("--memory") + 2] - assert ["--user", "1000:1000"] == args[args.index("--user") : args.index("--user") + 2] - assert "ENABLE_SUDO=1" in args - assert f"type=bind,src={sudoers_file},dst={SUDOERS_POLICY_PATH},ro" in args - # On macOS the policy comes from its volume, not the Mac's file. - assert f"type=volume,src=capsule-sudoers,volume-subpath=policy,dst={SUDOERS_POLICY_PATH},ro" in volume_args - assert f"type=bind,src={sudoers_file},dst={SUDOERS_POLICY_PATH},ro" not in volume_args - assert not sudoers_file.exists() - assert not sudoers_directory.exists() - assert "DOCKER_HOST=unix:///run/host-docker.sock" in args - assert f"DEVCAPSULE_CONTAINER_NAME={config.name}" in args - assert f"type=bind,src={docker_socket.resolve()},dst=/run/host-docker.sock" in args - for source, destination in ( - (project.resolve(), "/workspace/project"), - (home.resolve(), "/home/devcapsule"), - (config_dir.resolve(), "/ide-config"), - (plugins.resolve(), "/ide-plugins"), - (system.resolve(), "/ide-project-state/system"), - (log.resolve(), "/ide-project-state/log"), - (cache.resolve(), "/home/devcapsule/.cache"), - ): - assert f"type=bind,src={source},dst={destination}" in args - assert "--read-only" not in args - assert "--privileged" not in args - assert "SYS_ADMIN" not in args - - -@pytest.mark.parametrize("host_os,expected_gid", [(HostOs.LINUX, "file"), (HostOs.DARWIN, 0)]) -def test_host_docker_group_follows_where_the_socket_is_owned(tmp_path: Path, host_os, expected_gid) -> None: - # On Linux the capsule joins the socket file's group. Docker Desktop shows - # its socket inside the VM as root:root 0660, so a Mac host joins group 0 - # and writes no second group for it. - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - docker_socket = tmp_path / "docker.sock" - docker_socket.touch() - gid = docker_socket.stat().st_gid if expected_gid == "file" else expected_gid - host_user = HostUser(uid=501, gid=20, name="developer", group_name="staff") - with ( - patch("devcapsule.launch.pycharm._launcher.current_host_user", return_value=host_user), - patch("devcapsule.launch.pycharm._launcher.is_socket", return_value=True), - patch("devcapsule.launch.pycharm._launcher.HostOs.current", return_value=host_os), - ): - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.host, - host_docker_socket=docker_socket, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - with patch("devcapsule.launch.pycharm._launcher.write_xauthority"): - files = prepare_temp_runtime_files(config, env) - try: - args = build_docker_args(config, files, env) - groups = files.group_file.read_text(encoding="utf-8") - finally: - cleanup_temp_runtime_files(files) - - assert config.host_docker_gid == gid - assert ["--group-add", str(gid)] == args[args.index("--group-add") : args.index("--group-add") + 2] - assert ("host-docker:x:" in groups) is (gid not in (0, host_user.gid)) - - -@pytest.mark.parametrize("host_os,effective", [(HostOs.LINUX, "host"), (HostOs.DARWIN, "bridge")]) -def test_host_networking_falls_back_to_bridge_on_macos(tmp_path: Path, capsys, host_os, effective) -> None: - # Docker Desktop's host network is its VM's; the contained desktop would - # listen where the Mac cannot reach it. - project = tmp_path / "project" - project.mkdir() - with patch("devcapsule.launch.pycharm._launcher.HostOs.current", return_value=host_os): - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="host", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - base_env(tmp_path), - ) - assert config.network_mode == effective - assert ("host networking is unavailable on macOS" in capsys.readouterr().err) is (effective == "bridge") - - -def test_unauthorized_runtime_effects_keep_safe_docker_defaults(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - host_user = HostUser(uid=1000, gid=1000, name="developer", group_name="developer") - - with patch( - "devcapsule.launch.pycharm._launcher.current_host_user", - return_value=host_user, - ): - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - with patch("devcapsule.launch.pycharm._launcher.write_xauthority"): - files = prepare_temp_runtime_files(config, env) - try: - assert files.sudoers_file is None - args = build_docker_args(config, files, env) - finally: - cleanup_temp_runtime_files(files) - - assert ["--network", "bridge"] == args[args.index("--network") : args.index("--network") + 2] - assert "ENABLE_SUDO=0" in args - assert not any(SUDOERS_POLICY_PATH in argument for argument in args) - assert "DOCKER_HOST=unix:///run/host-docker.sock" not in args - assert not any("dst=/run/host-docker.sock" in argument for argument in args) - assert "--memory" not in args - assert ["--cap-drop", "ALL"] == args[args.index("--cap-drop") : args.index("--cap-drop") + 2] - assert "no-new-privileges" in args - assert "--read-only" in args - assert "--privileged" not in args - - -def test_sudoers_ownership_helper_is_constrained(tmp_path: Path) -> None: - policy = tmp_path / "sudoers" - policy.write_text(SUDOERS_POLICY, encoding="utf-8") - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - sudoers_file=policy, - ) - config = cast( - PycharmRunConfig, - SimpleNamespace(enable_sudo=True, image="devcapsule-local-pycharm:canonical", - capsule_platform=Platform.LINUX_AMD64, daemon_architecture="amd64"), - ) - - with ( - patch("devcapsule.launch.pycharm._launcher.os.chown", side_effect=PermissionError), - patch("devcapsule.launch.pycharm._launcher.file_owner_uid", return_value=0), - patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run, - ): - run.return_value = SimpleNamespace(returncode=0, stderr="") - ensure_sudoers_policy_ownership(config, files, {"PATH": "/usr/bin"}) - - command = run.call_args.args[0] - assert command[:3] == ["docker", "run", "--rm"] - assert ["--network", "none"] == command[command.index("--network") : command.index("--network") + 2] - assert ["--cap-drop", "ALL"] == command[command.index("--cap-drop") : command.index("--cap-drop") + 2] - assert ["--cap-add", "CHOWN"] == command[command.index("--cap-add") : command.index("--cap-add") + 2] - assert "no-new-privileges" in command - assert "--read-only" in command - assert "--pull=never" in command - assert "0:0" == command[-2] - - -def test_sudoers_ownership_helper_failure_is_actionable(tmp_path: Path) -> None: - policy = tmp_path / "sudoers" - policy.write_text(SUDOERS_POLICY, encoding="utf-8") - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - sudoers_file=policy, - ) - config = cast( - PycharmRunConfig, - SimpleNamespace(enable_sudo=True, image="devcapsule-local-pycharm:canonical", - capsule_platform=Platform.LINUX_AMD64, daemon_architecture="amd64"), - ) - - with ( - patch("devcapsule.launch.pycharm._launcher.os.chown", side_effect=PermissionError), - patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run, - ): - run.return_value = SimpleNamespace(returncode=1, stderr="daemon denied helper") - with pytest.raises(PycharmRunError, match="daemon denied helper"): - ensure_sudoers_policy_ownership(config, files, {}) - - -def _macos_sudo_files(tmp_path: Path) -> tuple[PycharmRunConfig, TempRuntimeFiles]: - policy = tmp_path / "sudoers" - policy.write_text(SUDOERS_POLICY, encoding="utf-8") - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - sudoers_file=policy, - ) - config = cast( - PycharmRunConfig, - SimpleNamespace(enable_sudo=True, image="devcapsule-local-pycharm:canonical", name="capsule", - capsule_platform=Platform.LINUX_AMD64, daemon_architecture="arm64"), - ) - return config, files - - -def test_macos_sudo_policy_is_written_into_a_volume_by_root( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, -) -> None: - # Docker Desktop shares Mac files as the Mac user: no chown makes a - # bind-mounted policy root's, so the policy lives in a volume. - monkeypatch.setattr(HostOs, "current", classmethod(lambda cls: HostOs.DARWIN)) - config, files = _macos_sudo_files(tmp_path) - - with ( - patch("devcapsule.launch.pycharm._launcher.os.chown") as chown, - patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run, - ): - run.return_value = SimpleNamespace(returncode=0, stderr="", stdout="") - ensure_sudoers_policy_ownership(config, files, {"PATH": "/usr/bin"}) - - chown.assert_not_called() - assert files.sudoers_volume == "capsule-sudoers" - listed, create, write = (call.args[0] for call in run.call_args_list) - assert listed == ["docker", "volume", "ls", "--quiet", "--filter", - "label=devcapsule.purpose=development-sudo-policy"] - assert create == ["docker", "volume", "create", "--label", "devcapsule.purpose=development-sudo-policy", - "capsule-sudoers"] - assert write[:4] == ["docker", "run", "--rm", "-i"] - assert "--platform=linux/amd64" in write - assert ["--network", "none"] == write[write.index("--network") : write.index("--network") + 2] - assert ["--cap-drop", "ALL"] == write[write.index("--cap-drop") : write.index("--cap-drop") + 2] - assert "--cap-add" not in write - assert ["--user", "0:0"] == write[write.index("--user") : write.index("--user") + 2] - assert {"--read-only", "--pull=never", "no-new-privileges"} <= set(write) - assert "type=volume,src=capsule-sudoers,dst=/policy" in write - assert write[-1].startswith("umask 0337;") - assert write[-1].endswith("/policy/policy") - assert run.call_args_list[2].kwargs["input"] == SUDOERS_POLICY - - -def test_macos_sudo_policy_failure_removes_its_volume( - tmp_path: Path, monkeypatch: pytest.MonkeyPatch, -) -> None: - monkeypatch.setattr(HostOs, "current", classmethod(lambda cls: HostOs.DARWIN)) - config, files = _macos_sudo_files(tmp_path) - - with patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run: - run.side_effect = [ - SimpleNamespace(returncode=0, stderr="", stdout=""), - SimpleNamespace(returncode=0, stderr=""), - SimpleNamespace(returncode=125, stderr="no such image"), - SimpleNamespace(returncode=0, stderr=""), - ] - with pytest.raises(PycharmRunError, match="no such image"): - ensure_sudoers_policy_ownership(config, files, {}) - - assert run.call_args_list[-1].args[0] == ["docker", "volume", "rm", "--force", "capsule-sudoers"] - assert files.sudoers_volume is None - - -def test_only_an_old_policy_volume_counts_as_abandoned() -> None: - # A concurrent launch's fresh volume is unused until its capsule starts. - old = (datetime.now(timezone.utc) - timedelta(days=2)).isoformat() - fresh = datetime.now(timezone.utc).isoformat() - with patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run: - run.side_effect = [ - SimpleNamespace(returncode=0, stdout="killed-sudoers\nconcurrent-sudoers\n"), - SimpleNamespace(returncode=0, stdout=f"killed-sudoers {old}\nconcurrent-sudoers {fresh}\n"), - SimpleNamespace(returncode=0), - ] - remove_abandoned_sudoers_volumes({}) - - assert run.call_args_list[-1].args[0] == ["docker", "volume", "rm", "killed-sudoers"] - - -def test_sudo_banner_is_printed_after_complete_policy_preparation( - tmp_path: Path, - capsys: pytest.CaptureFixture[str], -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - - with ( - patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), - patch( - "devcapsule.launch.pycharm._launcher.ensure_sudoers_policy_ownership", - side_effect=lambda _config, files, _env: setattr(files, "sudoers_volume", "capsule-sudoers"), - ) as ensure, - patch("devcapsule.launch.pycharm._launcher.remove_sudoers_policy_volume") as remove, - patch("devcapsule.launch.pycharm._launcher.run_attached") as run, - ): - run.return_value = AttachedResult(0, interrupted=False, stopped=True) - assert ( - run_pycharm( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - image="devcapsule-local-pycharm:canonical", - docker_mode=DockerMode.none, - network_mode="bridge", - enable_sudo=True, - ), - env, - ) - == 0 - ) - - ensure.assert_called_once() - assert "DEVELOPMENT SUDO IS ENABLED" in capsys.readouterr().err - command = run.call_args.args[0] - assert any(SUDOERS_POLICY_PATH in argument and "src=capsule-sudoers" in argument for argument in command) - # The policy volume outlives nothing but the session. - assert remove.call_args.args[0] == "capsule-sudoers" - - -def test_sudo_policy_preparation_failure_cleans_files_without_enabled_banner( - tmp_path: Path, - capsys: pytest.CaptureFixture[str], -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - - with ( - patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), - patch( - "devcapsule.launch.pycharm._launcher.ensure_sudoers_policy_ownership", - side_effect=PycharmRunError("sudo policy failed"), - ), - patch( - "devcapsule.launch.pycharm._launcher.cleanup_temp_runtime_files", - wraps=cleanup_temp_runtime_files, - ) as cleanup, - ): - with pytest.raises(PycharmRunError, match="sudo policy failed"): - run_pycharm( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - image="devcapsule-local-pycharm:canonical", - docker_mode=DockerMode.none, - network_mode="bridge", - enable_sudo=True, - ), - env, - ) - - files = cleanup.call_args.args[0] - assert files.sudoers_file is not None - assert files.sudoers_directory is not None - assert all( - path is None or not path.exists() - for path in ( - files.xauth_file, - files.passwd_file, - files.group_file, - files.shadow_file, - files.sudoers_file, - ) - ) - assert not files.sudoers_directory.exists() - assert "DEVELOPMENT SUDO IS ENABLED" not in capsys.readouterr().err - - -def test_bound_secret_must_exist_on_host(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - with pytest.raises(PycharmRunError, match="is not set on the host"): - build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - docker_mode=DockerMode.none, - secret_environment=("OPENAI_API_KEY",), - ), - base_env(tmp_path), - ) - - -def test_image_process_uses_oci_command_and_cleans_all_temporary_files(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - image = "devcapsule-local-pycharm:canonical" - with ( - patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), - patch("devcapsule.launch.pycharm._launcher.run_attached") as run, - ): - run.return_value = AttachedResult(0, interrupted=False, stopped=True) - assert ( - run_pycharm( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - image=image, - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - == 0 - ) - - command = run.call_args.args[0] - assert command[-1] == image - assert "/opt/pycharm/bin/pycharm.sh" not in command - temporary_sources = [ - Path(next(part.removeprefix("src=") for part in argument.split(",") if part.startswith("src="))) - for argument in command - if argument.startswith("type=bind,src=") - and any( - destination in argument - for destination in ( - "dst=/tmp/.docker.xauth", - "dst=/etc/passwd", - "dst=/etc/group", - f"dst={RUNTIME_PLAN_PATH}", - ) - ) - ] - assert len(temporary_sources) == 4 - assert all(not path.exists() for path in temporary_sources) - - -def test_launch_preparation_failure_cleans_runtime_and_identity_files(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - with ( - patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), - patch( - "devcapsule.launch.pycharm._launcher.cleanup_temp_runtime_files", - wraps=cleanup_temp_runtime_files, - ) as cleanup, - ): - with pytest.raises(PycharmRunError, match="variable is empty or unset"): - run_pycharm( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - image="devcapsule-local-pycharm:canonical", - docker_mode=DockerMode.none, - network_mode="bridge", - git_token_env="MISSING_TOKEN", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - - files = cleanup.call_args.args[0] - assert files.runtime_plan_file is not None - assert all( - path is None or not path.exists() - for path in ( - files.xauth_file, - files.passwd_file, - files.group_file, - files.runtime_plan_file, - ) - ) - - def test_runtime_plan_serialization_failure_leaves_no_temporary_files(tmp_path: Path) -> None: runtime_directory = tmp_path / "runtime" config = cast( @@ -1139,163 +93,6 @@ def test_generated_passwd_home_matches_persistent_container_home(tmp_path: Path) assert "/ide-global-settings/home" not in files.passwd_file.read_text() -def test_ide_config_option_implies_custom_config_mode(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - custom_config = tmp_path / "custom-config" - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - ide_config=custom_config, - docker_mode=DockerMode.none, - ), - base_env(tmp_path), - ) - - assert config.ide_config_mode == "custom" - assert config.ide_config == custom_config.resolve() - - -def test_explicit_shared_config_mode_ignores_env_ide_config_dir(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["PYCHARM_IDE_CONFIG_DIR"] = str(tmp_path / "env-config") - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - config_mode=IdeConfigMode.shared, - docker_mode=DockerMode.none, - ), - env, - ) - - assert config.ide_config_mode == "shared" - project_id = plan_project(project).project_id - assert config.ide_config == ( - tmp_path / "data" / "devcapsule" / "projects" / "by-path" / project_id / "components" / "pycharm" / "config" - ).resolve() - - -def test_env_ide_config_dir_implies_custom_when_mode_is_unset(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["PYCHARM_IDE_CONFIG_DIR"] = str(tmp_path / "env-config") - - config = build_run_config(PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, docker_mode=DockerMode.none), env) - - assert config.ide_config_mode == "custom" - assert config.ide_config == (tmp_path / "env-config").resolve() - - -def test_profile_sets_global_settings_and_plugins_defaults(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_CONFIG_HOME"] = str(tmp_path / "config") - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - profile="codex", - docker_mode=DockerMode.none, - ), - env, - ) - - profile_root = tmp_path / "config" / "docker-pycharm-codex" - assert config.global_settings == (profile_root / "state").resolve() - assert config.plugins == (profile_root / "plugins").resolve() - - -def test_project_state_root_mirrors_project_path_under_root_parent(tmp_path: Path) -> None: - workspace = tmp_path / "workspace" - project = workspace / "myProjects" / "example" - project.mkdir(parents=True) - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_state_root=workspace / ".state", - docker_mode=DockerMode.none, - ), - base_env(tmp_path), - ) - - assert config.project_state == (workspace / ".state" / "myProjects" / "example").resolve() - - -def test_explicit_project_state_overrides_project_state_root(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_state=tmp_path / "explicit-state", - project_state_root=tmp_path / ".state", - docker_mode=DockerMode.none, - ), - base_env(tmp_path), - ) - - assert config.project_state == (tmp_path / "explicit-state").resolve() - - -def test_dogfood_legacy_roots_map_to_persistent_home_and_component_slots(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - global_settings = tmp_path / "docker-pycharm-codex" / "state" - plugins = tmp_path / "docker-pycharm-codex" / "plugins" - project_state = tmp_path / ".state" / "project" - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - global_settings=global_settings, - plugins=plugins, - project_state=project_state, - docker_mode=DockerMode.none, - ), - base_env(tmp_path), - ) - - assert config.persistent_home == (global_settings / "home").resolve() - assert config.ide_config == (global_settings / "config").resolve() - assert config.plugins == plugins.resolve() - assert config.ide_system == (project_state / "system").resolve() - assert config.ide_log == (project_state / "log").resolve() - assert config.tool_cache == (project_state / "home" / ".cache").resolve() - - -def test_persistent_home_defaults_to_checkout_scoped_xdg_data(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - docker_mode=DockerMode.none, - ), - base_env(tmp_path), - ) - - project_id = plan_project(project).project_id - assert config.persistent_home == ( - tmp_path / "data" / "devcapsule" / "projects" / "by-path" / project_id / "home" - ).resolve() - - def test_launcher_owned_docker_options_are_refused_in_passthrough() -> None: """Docker keeps the last occurrence of a single-instance option, so a raw passthrough repetition would silently override the resolved plan; ruled @@ -1414,178 +211,6 @@ def test_unknown_display_transport_is_refused(tmp_path: Path) -> None: contained_config(tmp_path, display_transport="xephyr") -def test_host_x11_passthrough_still_needs_a_host_display_and_binds_it(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env.pop("DISPLAY") - with pytest.raises(PycharmRunError, match="DISPLAY is not set"): - build_run_config(PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project"), env) - env = base_env(tmp_path) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - ), - env, - ) - assert config.display_transport == "host-x11" - assert config.runtime_plan is not None and config.runtime_plan.display == DisplayPlan.host_x11() - with ( - patch("devcapsule.launch.pycharm._launcher.write_xauthority") as xauthority, - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - ): - files = prepare_temp_runtime_files(config, env) - try: - xauthority.assert_called_once() - assert files.display_token_file is None - args = build_docker_args(config, files, env) - assert "type=bind,src=/tmp/.X11-unix,dst=/tmp/.X11-unix,ro" in args - assert "XAUTHORITY=/tmp/.docker.xauth" in args - assert "DISPLAY" in args - published = [args[index + 1] for index, item in enumerate(args) if item == "--publish"] - assert published == [f"127.0.0.1:{config.console_host_port}:6081"] # the console only; no display port - finally: - cleanup_temp_runtime_files(files) - - -def test_display_disclosure_states_the_transport(tmp_path: Path) -> None: - contained = contained_config(tmp_path) - text = display_disclosure(contained) - assert "Contained desktop" in text - assert f"published to host loopback port {contained.display_host_port}" in text - assert "No host X socket, credential, or DISPLAY is shared" in text - assert "boundary test is waived" in display_disclosure( - build_run_config( - PycharmRunOptions( - project=tmp_path / "project", - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - ), - base_env(tmp_path), - ) - ) - - -def test_run_pycharm_announces_the_display_url_and_opens_it_when_ready( - tmp_path: Path, capsys: pytest.CaptureFixture[str] -) -> None: - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env.pop("DISPLAY") - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - opened: list[str] = [] - listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - listener.bind(("127.0.0.1", 0)) - listener.listen(1) - port = listener.getsockname()[1] - - def fake_docker_run(command: list[str], env: dict[str, str], **_kwargs: object) -> AttachedResult: - # "The capsule is up": the published port answers, so the watcher opens - # the URL; then docker run returns as the session ends. - deadline = time.monotonic() + 10 - while not opened and time.monotonic() < deadline: - time.sleep(0.05) - return AttachedResult(0, interrupted=False, stopped=True) - - with ( - patch("devcapsule.launch.pycharm._launcher.allocate_loopback_port", side_effect=[port, 1]), - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - patch("devcapsule.launch.pycharm._launcher.run_attached", side_effect=fake_docker_run), - patch("devcapsule.launch.pycharm._launcher.current_host_user", return_value=HostUser(1000, 1000, "dev", "dev")), - ): - try: - exit_code = run_pycharm( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="bridge", - runtime_plan=external_runtime_plan(), - use_image_process=True, - display_transport="contained", - open_display_url=opened.append, - ), - env, - ) - finally: - listener.close() - assert exit_code == 0 - (url,) = opened - assert url.startswith(f"http://127.0.0.1:{port}/vnc.html?autoconnect=1&resize=remote&path=websockify%3Ftoken%3D") - captured = capsys.readouterr() - assert f"Contained display: {url}" in captured.err - - -def test_launcher_mounts_live_configuration_directory_and_private_snapshot_read_only(tmp_path): - import json - from devcapsule.launch.pycharm._launcher import channel_watcher, describe_run_command - from devcapsule.runtime_configuration import CHANNEL_PATH, CONFIGURATION_PATH, CONTEXT_PATH, LaunchConfiguration - project = tmp_path / "project" - project.mkdir() - records = tmp_path / "records" - records.mkdir() - document = {"format": 1, "launcher-root": str(project), "runtime-root": "/workspace/project", - "checkout-file": "devcapsule.checkout.toml", "running": {"identity": "captured-at-launch"}} - home = tmp_path / "chosen-home" - home.mkdir() - (home / "settings.toml").write_text('minimum-devcapsule = "0.3.0"\ndevcapsule-settings-schema-version = 1\n') - document["configuration-home"] = str(home) - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - config = build_run_config(PycharmRunOptions( - project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none, - runtime_plan=external_runtime_plan(), use_image_process=True, - launch_configuration=LaunchConfiguration(records, document), - ), env) - with patch("devcapsule.launch.pycharm._launcher.write_xauthority"), patch("devcapsule.launch.pycharm._launcher.write_user_files"): - files = prepare_temp_runtime_files(config, env) - try: - assert files.launch_context_file is not None - assert files.launch_context_file.stat().st_mode & 0o777 == 0o600 - assert json.loads(files.launch_context_file.read_text()) == document - args = build_docker_args(config, files, env) - assert f"type=bind,src={records},dst={CONFIGURATION_PATH},ro" in args - assert f"type=bind,src={home}/settings.toml,dst=/etc/devcapsule/home/settings.toml,ro" in args - assert not any("DEVCAPSULE_CONFIG_HOME" in arg for arg in args) - assert not any(f"src={home}," in arg for arg in args) - assert f"type=bind,src={files.launch_context_file},dst={CONTEXT_PATH},ro" in args - assert not any("src=" + str(records / "devcapsule.checkout.toml") in arg for arg in args) - # The console's channel: a private directory, mounted writable, with the launch listing when run computed one. - assert files.configuration_directory is not None and files.configuration_directory.stat().st_mode & 0o777 == 0o700 - assert f"type=bind,src={files.configuration_directory},dst={CHANNEL_PATH}" in args - assert not (files.configuration_directory / "listing.json").exists() - watcher = channel_watcher(config, files) - assert watcher is not None and watcher.checkout_record == records / "devcapsule.checkout.toml" and watcher.root == project - finally: - cleanup_temp_runtime_files(files) - assert not files.launch_context_file.exists() and not files.configuration_directory.exists() - listing = {"schema-version": 2, "rows": [], "pending": [], "notes": []} - config = build_run_config(PycharmRunOptions( - project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none, - runtime_plan=external_runtime_plan(), use_image_process=True, - launch_configuration=LaunchConfiguration(records, document, listing), - ), env) - with patch("devcapsule.launch.pycharm._launcher.write_xauthority"), patch("devcapsule.launch.pycharm._launcher.write_user_files"): - files = prepare_temp_runtime_files(config, env) - try: - assert files.configuration_directory is not None - written = json.loads((files.configuration_directory / "listing.json").read_text()) - assert written["format"] == 1 and written["listing"] == listing and written["at"] - assert "Temporary Configuration channel:" in describe_run_command(["docker", "run"], config, files) - finally: - cleanup_temp_runtime_files(files) - - def test_web_console_runs_whenever_the_runtime_runs_behind_its_own_token(tmp_path: Path) -> None: """The console mirrors the display: one loopback port, one per-run token, the token mounted read-only and never on the command line (R-CONSOLE-001).""" @@ -1770,38 +395,6 @@ def fail_chmod(path: Path, mode: int) -> None: assert list(runtime.iterdir()) == [] -@pytest.mark.parametrize("report_only", [False, True]) -def test_console_launcher_cleans_up_after_command_reporting_or_docker_failure( - tmp_path: Path, report_only: bool, -) -> None: - from unittest.mock import Mock - - config = contained_config(tmp_path) - runtime = tmp_path / "runtime" - env = {**base_env(tmp_path), "XDG_RUNTIME_DIR": str(runtime)} - report = Mock() - options = PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64, command_report=report if report_only else None) - with ( - patch("devcapsule.launch.pycharm._launcher.build_run_config", return_value=config), - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - patch("devcapsule.launch.pycharm._launcher.watch_display_ready") as watch, - patch("devcapsule.launch.pycharm._launcher.run_attached", side_effect=OSError("docker failed")) as docker, - ): - if report_only: - assert run_pycharm(options, env) == 0 - docker.assert_not_called() - watch.assert_not_called() - report.assert_called_once() - assert "Temporary Console token:" in report.call_args.args[0] - assert config.console_token not in report.call_args.args[0] - else: - with pytest.raises(OSError, match="docker failed"): - run_pycharm(options, env) - assert watch.call_count == 2 - assert watch.call_args.args[3].is_set() - assert list(runtime.iterdir()) == [] - - def test_web_console_runs_the_checkouts_source_under_the_self_hosting_exception(tmp_path: Path) -> None: project = tmp_path / "project" (project / "devcapsule-webconsole" / "devcapsule_webconsole").mkdir(parents=True) @@ -1812,27 +405,6 @@ def test_web_console_runs_the_checkouts_source_under_the_self_hosting_exception( assert console_development_source(tmp_path / "other", "/workspace/other") == "" -def test_web_console_is_absent_without_a_runtime_plan(tmp_path: Path) -> None: - project = tmp_path / "project" - project.mkdir() - config = build_run_config( - PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none), - base_env(tmp_path), - ) - assert config.console_host_port is None and config.console_token == "" - assert console_disclosure(config) == "" - with ( - patch("devcapsule.launch.pycharm._launcher.write_xauthority"), - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - ): - files = prepare_temp_runtime_files(config, base_env(tmp_path)) - try: - assert files.console_token_file is None - assert "devcapsule-console-token" not in " ".join(build_docker_args(config, files, base_env(tmp_path))) - finally: - cleanup_temp_runtime_files(files) - - def test_console_disclosure_states_where_it_listens(tmp_path: Path) -> None: contained = contained_config(tmp_path) text = console_disclosure(contained) @@ -1842,71 +414,6 @@ def test_console_disclosure_states_where_it_listens(tmp_path: Path) -> None: assert f"host loopback port {direct.console_host_port} (host networking)" in console_disclosure(direct) -@pytest.mark.parametrize("display_transport", ["contained", "host-x11"]) -@pytest.mark.parametrize("custom_console_opener", [False, True]) -def test_run_pycharm_announces_the_console_url_and_opens_it_only_without_a_desktop( - tmp_path: Path, capsys: pytest.CaptureFixture[str], display_transport: str, custom_console_opener: bool, -) -> None: - from threading import Event - from unittest.mock import Mock - - project = tmp_path / "project" - project.mkdir() - env = base_env(tmp_path) - env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") - display_opener = Mock() - console_opener = Mock() - default_console_opener = Mock() - tokens = ["a" * 48, "b" * 48] - stops: list[Event] = [] - - def ready(port: int, url: str, opener: object, stop: Event, **kwargs: object) -> None: - from typing import Callable - - stops.append(stop) - assert not stop.is_set() - cast(Callable[[str], None], opener)(url) - - with ( - patch("devcapsule.launch.pycharm._launcher.allocate_loopback_port", side_effect=[41000, 41001]), - patch("devcapsule.launch.pycharm._launcher.new_run_token", side_effect=tokens), - patch("devcapsule.launch.pycharm._launcher.watch_display_ready", side_effect=ready) as watcher, - patch("devcapsule.launch.pycharm._launcher.write_user_files"), - patch("devcapsule.launch.pycharm._launcher.write_xauthority"), - patch("devcapsule.launch.pycharm._launcher.run_attached", return_value=AttachedResult(7, interrupted=False, stopped=True)), - patch("devcapsule.launch.pycharm._launcher.default_opener", return_value=default_console_opener) as default, - ): - assert run_pycharm(PycharmRunOptions( - project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none, - network_mode="bridge", runtime_plan=external_runtime_plan(), use_image_process=True, - display_transport=display_transport, open_display_url=display_opener, - open_console_url=console_opener if custom_console_opener else None, - ), env) == 7 - contained = display_transport == "contained" - console_port, token = (41001, tokens[1]) if contained else (41000, tokens[0]) - url = f"http://127.0.0.1:{console_port}/?token={token}" - output = capsys.readouterr().err - assert f"Web console: {url}\n" in output - assert watcher.call_count == (2 if contained else 1) - assert watcher.call_args.args[:2] == (console_port, url) - assert watcher.call_args.kwargs == {"label": "Web console"} - assert all(stop is stops[0] and stop.is_set() for stop in stops) - assert list((tmp_path / "runtime").iterdir()) == [] - if contained: - display_opener.assert_called_once_with(display_url(41000, tokens[0])) - else: - display_opener.assert_not_called() - if custom_console_opener: - console_opener.assert_called_once_with(url) - default.assert_not_called() - elif contained: - assert f"Web console is ready; open it in a browser: {url}" in output - default.assert_not_called() - else: - assert default.call_args.kwargs == {"label": "Web console"} - default_console_opener.assert_called_once_with(url) - - @pytest.mark.parametrize("enabled", [False, True]) def test_console_environment_contains_only_the_origin(tmp_path: Path, enabled: bool) -> None: from dataclasses import replace @@ -2022,34 +529,6 @@ def test_begin_session_logs_writes_the_manifest_and_the_launcher_log_and_prunes_ logs.reset() -def test_unusable_session_directory_does_not_add_a_mount_or_stop_launch(tmp_path, capsys): - from devcapsule import logs - from devcapsule.launch.pycharm import _launcher as launcher - - blocked = tmp_path / "blocked-state" - blocked.write_text("not a directory") - # Existing IDE storage still has to be writable; this regression isolates - # the new session directory from that pre-existing launch requirement. - with patch.object(launcher, "session_logs_root", return_value=blocked): - config, env = session_config(tmp_path) - assert config.session_log_directory is None - assert config.runtime_plan is not None and config.runtime_plan.logs is None - try: - with ( - patch.object(launcher, "build_run_config", return_value=config), - patch.object(launcher, "write_xauthority"), - patch.object(launcher, "write_user_files"), - patch.object(launcher, "watch_display_ready"), - patch.object(launcher, "run_attached", return_value=AttachedResult(7, interrupted=False, stopped=True)) as docker, - ): - assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == 7 - command = docker.call_args.args[0] - assert not any(SESSION_LOG_DESTINATION in arg for arg in command) - assert "cannot use session logs" in capsys.readouterr().err - finally: - logs.reset() - - @pytest.mark.parametrize("override,expected", [("debug", "DEBUG"), (" Info ", "INFO"), ("loud", "WARNING")]) def test_session_log_level_reaches_the_capsule(tmp_path, override, expected): config, env = session_config(tmp_path, DEVCAPSULE_LOG_LEVEL=override) @@ -2097,94 +576,6 @@ def test_session_logs_with_an_external_daemon(tmp_path, backed, capsys): assert "not backed by a mount" in capsys.readouterr().err -def test_session_log_masks_inline_environment_values_of_extra_arguments(tmp_path, capsys): - """The launcher log keeps the docker run line but never a saved or one-off inline env value.""" - from dataclasses import replace - from devcapsule import logs - from devcapsule.launch.pycharm import _launcher as launcher - - config, env = session_config(tmp_path) - config = replace(config, extra_docker_args=[ - "--gpus", "all", "--env", "APP_SECRET=saved-sentinel-value", "-eOTHER=one-off-sentinel", "--env", "PLAIN", - ]) - commands: list[list[str]] = [] - - class Client: - def __init__(self, command, env): - commands.append(list(command)) - - def wait(self, timeout=None): - return 0 - - try: - with ( - patch.object(launcher, "build_run_config", return_value=config), - patch.object(launcher, "write_xauthority"), - patch.object(launcher, "write_user_files"), - patch.object(launcher, "watch_display_ready"), - patch.object(launcher.subprocess, "Popen", Client), - ): - assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == 0 - # Docker receives the literal arguments, in order, before the image. - [command] = commands - image = command.index(config.image) - assert command[image - len(config.extra_docker_args):image] == config.extra_docker_args - assert config.session_log_directory is not None - record = (config.session_log_directory / "devcapsule-launcher.log").read_text() - assert "docker run: " in record - assert "--gpus all" in record and "APP_SECRET=" in record and "-eOTHER=" in record - assert "--env PLAIN" in record - assert "saved-sentinel-value" not in record and "one-off-sentinel" not in record - assert "saved-sentinel-value" not in capsys.readouterr().err - finally: - logs.reset() - - -def test_interrupted_launch_ends_with_a_result_and_the_log_paths(tmp_path, capsys): - """Ctrl+C ends with a result, not a traceback (the first-session report of 2026-09-15, C10).""" - from devcapsule import logs - from devcapsule.launch.pycharm import _launcher as launcher - - config, env = session_config(tmp_path) - stops = [] - stopped_before_return = False - - class Client: - waits = 0 - - def wait(self, timeout=None): - nonlocal stopped_before_return - self.waits += 1 - if self.waits == 1: - assert stops and not any(stop.is_set() for stop in stops) - raise KeyboardInterrupt - stopped_before_return = bool(stops) and all(stop.is_set() for stop in stops) - return 143 - - try: - with ( - patch.object(launcher, "build_run_config", return_value=config), - patch.object(launcher, "write_xauthority"), - patch.object(launcher, "write_user_files"), - patch.object(launcher, "watch_display_ready", side_effect=lambda port, url, opener, stop, **kw: stops.append(stop)), - patch.object(launcher.subprocess, "Popen", return_value=Client()), - ): - assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == launcher.INTERRUPTED_EXIT_CODE - assert stopped_before_return - assert stops and all(stop.is_set() for stop in stops) - assert list((tmp_path / "runtime").iterdir()) == [] - assert config.session_log_directory is not None - record = (config.session_log_directory / "devcapsule-launcher.log").read_text() - assert "session end: interrupted from the terminal (Ctrl+C); docker run exit code 143" in record - err = capsys.readouterr().err - assert "Session interrupted from the terminal (Ctrl+C); the capsule stopped." in err - assert "Session logs on this computer:" in err - assert f" {config.session_log_directory / 'devcapsule-launcher.log'}" in err - assert f" {config.session_log_directory / 'session.json'}" in err - finally: - logs.reset() - - def test_run_attached_waits_through_ctrl_c_for_the_capsules_end(capsys) -> None: from devcapsule.launch.pycharm import _launcher as launcher @@ -2433,69 +824,3 @@ def test_the_log_budget_knob_sets_the_segment_and_reports_nonsense(tmp_path: Pat warning = capsys.readouterr().err assert message in warning assert warning.count("using the default log budget of 256 MiB") == 1 - - -def test_the_launcher_records_the_sessions_end_in_its_manifest(tmp_path: Path) -> None: - import json - - from devcapsule import logs - from devcapsule.launch.pycharm import _launcher as launcher - from devcapsule.launch.session_logs import mark_session_ended - - assert "130" in (mark_session_ended.__doc__ or "") and "None" not in (mark_session_ended.__doc__ or "") - config, env = session_config(tmp_path) - assert config.session_log_directory is not None - try: - with ( - patch.object(launcher, "build_run_config", return_value=config), - patch.object(launcher, "write_xauthority"), - patch.object(launcher, "write_user_files"), - patch.object(launcher, "watch_display_ready"), - patch.object(launcher, "run_attached", return_value=AttachedResult(7, interrupted=False, stopped=True)), - ): - assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == 7 - manifest = json.loads((config.session_log_directory / "session.json").read_text(encoding="utf-8")) - assert manifest["session"] == config.session_id and manifest["exit-code"] == 7 - assert manifest["ended-at"].endswith("Z") and logs.session_ended(config.session_log_directory) - with ( - patch.object(launcher, "build_run_config", return_value=config), - patch.object(launcher, "write_xauthority"), - patch.object(launcher, "write_user_files"), - patch.object(launcher, "watch_display_ready"), - patch.object(launcher, "run_attached", return_value=AttachedResult(143, interrupted=True, stopped=True)), - ): - assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == launcher.INTERRUPTED_EXIT_CODE - manifest = json.loads((config.session_log_directory / "session.json").read_text(encoding="utf-8")) - assert manifest["exit-code"] == launcher.INTERRUPTED_EXIT_CODE and manifest["ended-at"] - finally: - logs.reset() - - -def test_interrupted_launch_with_unstopped_capsule_leaves_session_manifest_unended(tmp_path: Path) -> None: - import json - - from devcapsule import logs - from devcapsule.launch.pycharm import _launcher as launcher - - config, env = session_config(tmp_path) - assert config.session_log_directory is not None - try: - with ( - patch.object(launcher, "build_run_config", return_value=config), - patch.object(launcher, "write_xauthority"), - patch.object(launcher, "write_user_files"), - patch.object(launcher, "watch_display_ready"), - patch.object( - launcher, - "run_attached", - return_value=AttachedResult(launcher.INTERRUPTED_EXIT_CODE, interrupted=True, stopped=False), - ), - ): - assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == launcher.INTERRUPTED_EXIT_CODE - manifest = json.loads((config.session_log_directory / "session.json").read_text(encoding="utf-8")) - assert "ended-at" not in manifest and "exit-code" not in manifest - assert not logs.session_ended(config.session_log_directory) - record = (config.session_log_directory / "devcapsule-launcher.log").read_text(encoding="utf-8") - assert f"session end unknown: interrupted from the terminal (Ctrl+C); container {config.name} may still be running" in record - finally: - logs.reset() diff --git a/devcapsule-src/tests/test_recursive_successor_plan.py b/devcapsule-src/tests/test_recursive_successor_plan.py index 2c1532f3..0bcedb63 100644 --- a/devcapsule-src/tests/test_recursive_successor_plan.py +++ b/devcapsule-src/tests/test_recursive_successor_plan.py @@ -2,27 +2,10 @@ import copy import json -from pathlib import Path -import socket from typing import Any import pytest -from devcapsule.components.pycharm import runtime_template as pycharm_runtime_template -from devcapsule.launch.pycharm import DockerMode, PycharmRunOptions -from devcapsule.platforms import Platform -from devcapsule.launch.pycharm._launcher import ( - TempRuntimeFiles, - build_docker_args, - build_run_config, -) -from devcapsule.container_runtime.contract import Identity, RuntimePlan -from devcapsule.materialization import RUNTIME_PLAN_PATH -from devcapsule.host_open import ( - HOST_OPEN_BROWSER, - HOST_OPEN_SOCKET_DESTINATION, - HOST_OPEN_SOCKET_ENV, -) from devcapsule.recursive_successor_plan import ( REDACTED_HOST_PATH, ExpectedSuccessorPlan, @@ -166,96 +149,6 @@ def rejected(inspection: dict[str, Any]) -> str: return str(error.value) -@pytest.mark.parametrize("enable_sudo", [False, True]) -def test_plan_models_every_flag_the_real_launcher_emits(tmp_path: Path, enable_sudo: bool) -> None: - """The launch now fails closed on an unmodelled flag, so keep the model current.""" - - project = tmp_path / "project" - project.mkdir() - env = { - "DISPLAY": ":1", - "HOME": str(tmp_path / "home"), - "XDG_DATA_HOME": str(tmp_path / "data"), - "PYCHARM_GIT_IDENTITY_FROM_HOST": "0", - } - browser_socket = tmp_path / "host-open.sock" - with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as broker: - broker.bind(str(browser_socket)) - env[HOST_OPEN_SOCKET_ENV] = str(browser_socket) - config = build_run_config( - PycharmRunOptions( - project=project, - capsule_platform=Platform.LINUX_AMD64, - project_mount="/workspace/project", - docker_mode=DockerMode.none, - network_mode="host", - memory_limit_bytes=8589934592, - enable_sudo=enable_sudo, - debug_native=True, - runtime_plan=RuntimePlan.for_component( - pycharm_runtime_template(), - project_path="/workspace/project", - home="/home/devcapsule", - identity=Identity(1000, 1000, "developer"), - ), - use_image_process=True, - additional_environment={ - "DEVCAPSULE_RECURSIVE_E2E": "1", - "DEVCAPSULE_RUN_ID": "b2093d85912fa34ac1324e1da26a9dcd", - }, - enable_host_browser=True, - extra_docker_args=[ - "--pull=never", - "--label", - "devcapsule.e2e.managed=true", - "--label", - "devcapsule.e2e.role=successor", - ], - ), - env, - ) - files = TempRuntimeFiles( - xauth_file=tmp_path / "xauth", - passwd_file=tmp_path / "passwd", - group_file=tmp_path / "group", - console_token_file=tmp_path / "console-token", - shadow_file=tmp_path / "shadow", - sudoers_file=tmp_path / "sudoers", - runtime_plan_file=tmp_path / "runtime-plan", - ) - args = build_docker_args(config, files, env) - - plan = ExpectedSuccessorPlan.from_docker_args( - args, - image_reference=IMAGE_REFERENCE, - image_identity=IMAGE_IDENTITY, - image_labels=IMAGE_LABELS, - runtime_plan_destination=RUNTIME_PLAN_PATH, - runtime_plan_digest=RUNTIME_PLAN_DIGEST, - ) - - assert plan.network_mode == "host" - assert plan.memory_limit_bytes == 8589934592 - assert plan.environment["DEVCAPSULE_RECURSIVE_E2E"] == "1" - assert plan.environment["DEVCAPSULE_RUN_ID"] == "b2093d85912fa34ac1324e1da26a9dcd" - assert plan.environment["BROWSER"] == HOST_OPEN_BROWSER - assert plan.environment[HOST_OPEN_SOCKET_ENV] == str(HOST_OPEN_SOCKET_DESTINATION) - browser_mount = next( - mount for mount in plan.mounts if mount.destination == str(HOST_OPEN_SOCKET_DESTINATION) - ) - assert browser_mount.source == str(browser_socket) - assert browser_mount.read_only is True - assert plan.labels["devcapsule.e2e.role"] == "successor" - assert "SYS_PTRACE" in plan.cap_add - assert RUNTIME_PLAN_PATH in {mount.destination for mount in plan.mounts} - assert plan.read_only_root is not enable_sudo - if enable_sudo: - assert plan.group_add and plan.cap_drop == () - else: - assert plan.cap_drop == ("ALL",) - assert "no-new-privileges" in plan.security_opt - - def test_plan_models_the_complete_translated_launch() -> None: plan = build_plan() @@ -568,21 +461,3 @@ def test_duplicate_mount_destination_is_rejected() -> None: inspection["Mounts"].append(dict(inspection["Mounts"][0])) assert "declared more than once" in rejected(inspection) - - -@pytest.mark.parametrize("spelling", [["--platform=linux/amd64"], ["--platform", "linux/amd64"]]) -def test_an_emulated_launch_plans_its_platform_and_the_successor_must_run_it(spelling: list[str]) -> None: - # The launcher names the platform when the daemon emulates it; both of - # Docker's spellings parse, and a successor that ran another is refused. - plan = build_plan([*docker_args(), *spelling]) - assert plan.platform == "linux/amd64" - inspection = matching_inspection(plan) - inspection["ImageManifestDescriptor"] = {"platform": {"os": "linux", "architecture": "amd64"}} - compare_inspection(plan, inspection) - inspection["ImageManifestDescriptor"] = {"platform": {"os": "linux", "architecture": "arm64"}} - with pytest.raises(SuccessorPlanError, match="successor platform does not match the planned --platform"): - compare_inspection(plan, inspection) - # The independent inspection reloads the retained plan, platform included; - # a native plan's retained form is unchanged. - assert ExpectedSuccessorPlan.from_mapping(plan.to_mapping(show_host_paths=True)) == plan - assert "platform" not in build_plan(docker_args()).to_mapping() diff --git a/devcapsule-src/tests/test_runtime_artifact.py b/devcapsule-src/tests/test_runtime_artifact.py index e8857ac7..72727f16 100644 --- a/devcapsule-src/tests/test_runtime_artifact.py +++ b/devcapsule-src/tests/test_runtime_artifact.py @@ -1,143 +1,16 @@ from __future__ import annotations import hashlib -from pathlib import Path import pytest -from devcapsule.compat import CliError from devcapsule.materialization import ArtifactSpec, formation_descriptor, formation_identity -from devcapsule.build_info import BuildInfo -from devcapsule.platforms import HostOs, Platform -from devcapsule.runtime_artifact import executable_platform, runtime_artifact from devcapsule.runtime_command import RuntimeCommand from tests.test_base_image import pex_fixture -from tests.test_platforms import on_host pytestmark = pytest.mark.legacy -AMD64 = Platform.LINUX_AMD64 -ARM64 = Platform.LINUX_ARM64 -MACH_O_ARM64 = b"\xcf\xfa\xed\xfe" + bytes(16) -ELF_AMD64 = b"\x7fELF\x02\x01\x01" + bytes(11) + (0x3E).to_bytes(2, "little") -ELF_ARM64 = b"\x7fELF\x02\x01\x01" + bytes(11) + (0xB7).to_bytes(2, "little") -ELF_RISCV = b"\x7fELF\x02\x01\x01" + bytes(11) + (0xF3).to_bytes(2, "little") - - -def pex_with_native_header(path: Path, header: bytes, *, revision: str = "a" * 40) -> Path: - """A PEX fixture whose first bytes are a native executable's header. - - A scie is a native launcher with the PEX appended; the build information - is read from the archive, so the header and the archive coexist. - """ - - pex = pex_fixture(path, revision=revision) - pex.write_bytes(header + pex.read_bytes()) - return pex - - -@pytest.fixture(autouse=True) -def launcher_of_the_fixtures_build(monkeypatch: pytest.MonkeyPatch) -> None: - """The running launcher is the build the fixtures record: 0.1.0 at revision a…a.""" - - monkeypatch.setattr("devcapsule.runtime_artifact.current_build_info", lambda: BuildInfo( - schema_version=2, version="0.1.0", source_repository="https://github.com/example/devcapsule", - source_revision="a" * 40, source_url="unknown")) - - -def test_packaged_launcher_supplies_itself_despite_source_override(tmp_path: Path, monkeypatch) -> None: - launcher = pex_fixture(tmp_path / "launcher.pex") - monkeypatch.setenv("SCIE", str(launcher)) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(tmp_path / "different.pex")) - assert runtime_artifact(AMD64) == launcher - - -def test_source_launcher_requires_explicit_built_runtime(tmp_path: Path, monkeypatch) -> None: - monkeypatch.delenv("SCIE", raising=False) - monkeypatch.delenv("PEX", raising=False) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(tmp_path / "missing.pex")) - with pytest.raises(CliError, match="DEVCAPSULE_RUNTIME_PEX"): - runtime_artifact(AMD64) - launcher = pex_fixture(tmp_path / "runtime.pex") - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(launcher)) - assert runtime_artifact(AMD64) == launcher - - -def test_executable_platform_reads_native_headers(tmp_path: Path) -> None: - assert executable_platform(pex_with_native_header(tmp_path / "mac", MACH_O_ARM64)) is HostOs.DARWIN - assert executable_platform(pex_with_native_header(tmp_path / "amd64", ELF_AMD64)) is AMD64 - assert executable_platform(pex_with_native_header(tmp_path / "arm64", ELF_ARM64)) is ARM64 - assert executable_platform(pex_with_native_header(tmp_path / "riscv", ELF_RISCV)) is HostOs.LINUX - assert executable_platform(pex_fixture(tmp_path / "plain.pex")) is None - assert executable_platform(tmp_path / "absent") is None - - -@pytest.mark.own_host_patches -def test_macos_launcher_delivers_the_named_linux_runtime(tmp_path: Path, monkeypatch) -> None: - on_host(monkeypatch, "Darwin", "arm64") - monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) - linux = pex_with_native_header(tmp_path / "devcapsule-linux", ELF_AMD64) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(linux)) - assert runtime_artifact(AMD64) == linux - - -@pytest.mark.own_host_patches -def test_macos_launcher_without_a_linux_runtime_says_what_to_set(tmp_path: Path, monkeypatch) -> None: - on_host(monkeypatch, "Darwin", "arm64") - monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) - monkeypatch.delenv("DEVCAPSULE_RUNTIME_PEX", raising=False) - with pytest.raises(CliError, match="a macOS executable and cannot run inside linux-amd64 capsules.*DEVCAPSULE_RUNTIME_PEX"): - runtime_artifact(AMD64) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(pex_with_native_header(tmp_path / "wrong-arch", ELF_ARM64))) - with pytest.raises(CliError, match="a linux-arm64 executable"): - runtime_artifact(AMD64) - - -@pytest.mark.own_host_patches -def test_a_plain_pex_launcher_on_a_mac_is_not_delivered(tmp_path: Path, monkeypatch) -> None: - # A PEX bundles native wheels for the machine it was built on. - on_host(monkeypatch, "Darwin", "arm64") - monkeypatch.delenv("SCIE", raising=False) - monkeypatch.setenv("PEX", str(pex_fixture(tmp_path / "launcher.pex"))) - linux = pex_with_native_header(tmp_path / "devcapsule-linux", ELF_AMD64) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(linux)) - assert runtime_artifact(AMD64) == linux - monkeypatch.delenv("DEVCAPSULE_RUNTIME_PEX") - with pytest.raises(CliError, match="a plain PEX built for another machine"): - runtime_artifact(AMD64) - - -@pytest.mark.own_host_patches -def test_a_runtime_of_another_build_is_refused(tmp_path: Path, monkeypatch) -> None: - # D-0009: the runtime runs the plan this launcher writes. A runtime left - # from an earlier build, after the launcher was updated, is refused. - on_host(monkeypatch, "Darwin", "arm64") - monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) - stale = pex_with_native_header(tmp_path / "devcapsule-linux", ELF_AMD64, revision="b" * 40) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(stale)) - with pytest.raises(CliError, match="is DevCapsule 0.1.0 from b+, but this launcher is 0.1.0 from a+"): - runtime_artifact(AMD64) - - -@pytest.mark.own_host_patches -def test_a_runtime_directory_serves_each_capsule_platform(tmp_path: Path, monkeypatch) -> None: - # Apple Silicon runs both platforms; one setting names both runtimes, and - # each launch delivers the one for its lock's platform. - on_host(monkeypatch, "Darwin", "arm64") - monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) - runtimes = tmp_path / "runtimes" - runtimes.mkdir() - amd64 = pex_with_native_header(runtimes / "devcapsule-linux-amd64", ELF_AMD64) - arm64 = pex_with_native_header(runtimes / "devcapsule-linux-arm64", ELF_ARM64) - monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(runtimes)) - assert runtime_artifact(ARM64) == arm64 - assert runtime_artifact(AMD64) == amd64 - amd64.unlink() - with pytest.raises(CliError, match="holds no devcapsule-linux-amd64"): - runtime_artifact(AMD64) - - def test_runtime_update_changes_formation_identity_without_changing_components() -> None: artifact = ArtifactSpec("1", "https://example.test/ide", "a" * 64) before = formation_descriptor(platform="linux-amd64", base_identity="sha256:base", artifact=artifact, diff --git a/devcapsule-src/tests/test_supervisor.py b/devcapsule-src/tests/test_supervisor.py index 3ae83194..50e869bc 100644 --- a/devcapsule-src/tests/test_supervisor.py +++ b/devcapsule-src/tests/test_supervisor.py @@ -28,11 +28,12 @@ SupervisorError, ) -pytestmark = pytest.mark.legacy - # The supervisor is the capsule's entry process: it runs only inside Linux # capsules and relies on Linux signal primitives (sigwaitinfo). -pytestmark = pytest.mark.skipif(sys.platform != "linux", reason="container-side code; capsules are Linux") +pytestmark = [ + pytest.mark.legacy, + pytest.mark.skipif(sys.platform != "linux", reason="container-side code; capsules are Linux"), +] DRIVER = Path(__file__).parent / "resources" / "supervisor_driver.py" REPO_ROOT = Path(__file__).resolve().parents[1] diff --git a/devcapsule-src/tests/unit/devcapsule/commands/__init__.py b/devcapsule-src/tests/unit/devcapsule/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/devcapsule-src/tests/unit/devcapsule/commands/test_framework.py b/devcapsule-src/tests/unit/devcapsule/commands/test_framework.py new file mode 100644 index 00000000..e66f482e --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/commands/test_framework.py @@ -0,0 +1,27 @@ +"""Unit tests of ``devcapsule/commands/framework.py``: the ``--platform`` value ``project init`` and ``images build`` take. + +The suite's host is amd64 Linux, which runs linux-amd64 only. +""" +from __future__ import annotations + +import argparse + +import pytest + +from devcapsule.commands.framework import capsule_platform_argument +from devcapsule.platforms import Platform + + +def test_a_platform_this_host_runs_is_accepted() -> None: + """A platform this host runs parses to its member.""" + assert capsule_platform_argument("linux-amd64") is Platform.LINUX_AMD64 + + +@pytest.mark.parametrize("value,message", [ + ("linux-arm64", "this host runs linux-amd64 capsules, not linux-arm64"), + ("windows-amd64", "Unsupported platform 'windows-amd64'"), +]) +def test_a_platform_this_host_cannot_run_is_a_usage_error(value: str, message: str) -> None: + """A platform this host does not run, or no platform at all, is a usage error naming why.""" + with pytest.raises(argparse.ArgumentTypeError, match=message): + capsule_platform_argument(value) diff --git a/devcapsule-src/tests/unit/devcapsule/commands/test_images.py b/devcapsule-src/tests/unit/devcapsule/commands/test_images.py new file mode 100644 index 00000000..8777139c --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/commands/test_images.py @@ -0,0 +1,66 @@ +"""Unit tests of ``devcapsule/commands/images.py``: how ``images build`` maps its options onto the build. + +The builder and the image inspection are fakes; nothing reaches Docker. +""" +from __future__ import annotations + +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import patch + +from devcapsule import cli +from devcapsule.platforms import Platform + + +def test_images_build_base_maps_cli_options(tmp_path: Path, capsys) -> None: + """A base build passes its options to the builder, with the platform this host creates projects for when none is named, and reports the image it built.""" + pex = tmp_path / "devcapsule.pex" + pex.write_bytes(b"pex") + inspected = SimpleNamespace( + id="sha256:abc123", + config=SimpleNamespace( + labels={ + "devcapsule.base.recipe-version": "1", + "devcapsule.pex.build-mnemonic": "v026", + "devcapsule.pex.sha256": "a" * 64, + "devcapsule.source.revision": "revision-1", + } + ), + ) + + with ( + patch("devcapsule.commands.images.build_base_image") as build, + patch("devcapsule.commands.images.inspect_local_image", return_value=inspected), + ): + result = cli.main( + [ + "images", + "build", + "--type", + "base", + "--tag", + "devcapsule-base:debug-v019", + "--from", + "local-root:test", + "--pex", + str(pex), + "--source-revision", + "revision-1", + "--network", + "host", + ] + ) + + assert result == 0 + options = build.call_args.args[0] + assert options.pex == pex.resolve() + assert options.image == "devcapsule-base:debug-v019" + assert options.root_image == "local-root:test" + assert options.source_revision == "revision-1" + assert options.allow_local_source is False + assert options.recipe == "ubuntu-24.04" + assert build.call_args.kwargs == {"network": "host", "platform": Platform.LINUX_AMD64} + output = capsys.readouterr().out + assert "Image ID: sha256:abc123" in output + assert "Runtime: supplied by the launcher" in output + assert "Source verification: public GitHub commit reachable" in output diff --git a/devcapsule-src/tests/unit/devcapsule/components/__init__.py b/devcapsule-src/tests/unit/devcapsule/components/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/devcapsule-src/tests/unit/devcapsule/components/test_linux_arm64_pins.py b/devcapsule-src/tests/unit/devcapsule/components/test_linux_arm64_pins.py new file mode 100644 index 00000000..e1a50b54 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/components/test_linux_arm64_pins.py @@ -0,0 +1,73 @@ +"""Unit tests of ``devcapsule/components/linux_arm64_pins.py``: the arm64 builds of the amd64 matrix's pins. + +The tables are data; each test reads them, and the amd64 matrix where it compares. +""" +from __future__ import annotations + +import json +import re +from typing import Any + +from devcapsule.components.linux_arm64_pins import COMPONENT_LOCK_TABLES, LINUX_ARM64_ECLIPSE_NATIVE_PACKAGES +from devcapsule.platforms import Platform +from devcapsule.resolution_matrix import MATRICES + +AMD64 = MATRICES[Platform.LINUX_AMD64] +AMD64_MARKERS = re.compile(r"x86[_-]64|[_-]x64|amd64|linux64/|cft/[^/]+/linux64|ffmpeg-linux\.zip") +"""How amd64 archives name themselves in their URLs.""" + + +def artifacts(value: Any) -> list[dict[str, Any]]: + """Every table at any depth that names a download by ``url``.""" + if isinstance(value, dict): + own = [value] if "url" in value else [] + return own + [found for item in value.values() for found in artifacts(item)] + if isinstance(value, (list, tuple)): + return [found for item in value for found in artifacts(item)] + return [] + + +def test_every_arm64_pin_is_a_version_the_amd64_matrix_pins() -> None: + """Each table is the arm64 build of a version the amd64 matrix pins; the converse fails at import.""" + assert all(AMD64.component_pin(component, version) is not None for component, version in COMPONENT_LOCK_TABLES) + + +def test_no_arm64_pin_names_an_amd64_artifact() -> None: + """No artifact URL names an amd64 archive.""" + urls = [str(artifact["url"]) for table in COMPONENT_LOCK_TABLES.values() for artifact in artifacts(table)] + assert [url for url in urls if AMD64_MARKERS.search(url)] == [] + + +def test_every_arm64_artifact_is_hash_pinned() -> None: + """Every artifact carries a SHA-256.""" + hashes = [artifact.get("sha256") for table in COMPONENT_LOCK_TABLES.values() for artifact in artifacts(table)] + assert all(re.fullmatch(r"[0-9a-f]{64}", str(value)) for value in hashes) + + +def test_arm64_artifacts_are_not_amd64_bytes() -> None: + """Every artifact differs from amd64's, except architecture-independent ones: Codex's JavaScript + package, pure-Python wheels and Ubuntu ``_all`` packages.""" + shared = {artifact["sha256"] for (component, version) in COMPONENT_LOCK_TABLES + for artifact in artifacts(AMD64.component_pin(component, version))} + same = [(version, str(artifact["url"])) for (_, version), table in COMPONENT_LOCK_TABLES.items() + for artifact in artifacts(table) if artifact["sha256"] in shared] + assert all(url.endswith(("_all.deb", "-none-any.whl", f"codex-{version}.tgz")) for version, url in same) + + +def test_platform_fields_and_artifact_keys_name_arm64() -> None: + """Every table that names a platform names linux-arm64, and none names linux-amd64.""" + assert "linux-amd64" not in json.dumps(list(COMPONENT_LOCK_TABLES.values())) + assert {table["platform"] for table in COMPONENT_LOCK_TABLES.values() if "platform" in table} == {"linux-arm64"} + assert all(list(table["artifacts"]) == ["linux-arm64"] for (component, _), table in COMPONENT_LOCK_TABLES.items() + if component in {"codex", "claude-code", "antigravity-cli"}) + + +def test_eclipse_native_closure_is_arm64_or_architecture_independent() -> None: + """Eclipse's native packages are amd64's plus libunwind8, each an arm64 package of ports.ubuntu.com + or an architecture-independent one.""" + amd64 = AMD64.component_pin("eclipse", "2026-09-R") + assert amd64 is not None + assert set(LINUX_ARM64_ECLIPSE_NATIVE_PACKAGES) - set(amd64["native-packages"]) == {"libunwind8"} + assert all(entry["url"].endswith("_all.deb") or ( + entry["url"].startswith("https://ports.ubuntu.com/ubuntu-ports/pool/") and entry["url"].endswith("_arm64.deb")) + for entry in LINUX_ARM64_ECLIPSE_NATIVE_PACKAGES.values()) diff --git a/devcapsule-src/tests/unit/devcapsule/configuration/test_capability_selection.py b/devcapsule-src/tests/unit/devcapsule/configuration/test_capability_selection.py new file mode 100644 index 00000000..db22e10a --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/configuration/test_capability_selection.py @@ -0,0 +1,53 @@ +"""Unit tests of ``devcapsule/configuration/capability_selection.py``: personal pins across capsule platforms. + +The pins are built from the matrices' own tables; no file is read. +""" +from __future__ import annotations + +from typing import Any + +import pytest + +from devcapsule.configuration.capability_selection import pins_for_platform +from devcapsule.configuration.file_formats import ProjectConfigurationError +from devcapsule.platforms import Platform +from devcapsule.resolution_matrix import MATRICES, ResolutionMatrix + +AMD64 = MATRICES[Platform.LINUX_AMD64] +ARM64 = MATRICES[Platform.LINUX_ARM64] + + +def personal_pins(label: str, matrix: ResolutionMatrix) -> dict[str, Any]: + """A checkout's pins of VSCodium as ``matrix`` embeds it, labelled ``label``.""" + return {"platform": label, "components": {"codium": dict(matrix.component_pins()["codium"]), + "interactive-surface": "codium"}} + + +def test_pins_written_for_another_platform_take_this_platforms_artifacts() -> None: + """A checkout's amd64 pins, on a project that runs as arm64, deliver arm64's table for the same + version, relabelled; the input is unchanged.""" + amd64 = personal_pins("linux-amd64", AMD64) + on_arm64 = pins_for_platform(amd64, ARM64) + assert on_arm64 == personal_pins("linux-arm64", ARM64) + assert amd64 == personal_pins("linux-amd64", AMD64) + + +def test_another_platforms_table_takes_this_platforms_artifacts_whatever_its_label() -> None: + """A table that is another platform's embedded pin is that platform's, even under this platform's label.""" + mislabelled = personal_pins("linux-arm64", AMD64) + assert pins_for_platform(mislabelled, ARM64)["components"]["codium"] == dict(ARM64.component_pins()["codium"]) + + +def test_a_table_no_matrix_embeds_is_kept_as_written() -> None: + """Pins written for the platform they run on, matching no embedded table, are a record, returned as they stand.""" + recorded = personal_pins("linux-amd64", AMD64) + recorded["components"]["codium"]["note"] = "kept" + assert pins_for_platform(recorded, AMD64) == recorded + + +def test_a_personal_version_the_other_platform_lacks_asks_for_a_new_choice() -> None: + """A component version pinned for another platform, which this platform does not pin, is refused + with the command that chooses again.""" + channel = {"platform": "linux-amd64", "components": {"codex": {"version": "9.9.9", "url": "https://example.test/x64.tgz"}}} + with pytest.raises(ProjectConfigurationError, match="codex 9.9.9 is pinned for linux-amd64.*linux-arm64 has no pin.*config use"): + pins_for_platform(channel, ARM64) diff --git a/devcapsule-src/tests/unit/devcapsule/configuration/test_operations.py b/devcapsule-src/tests/unit/devcapsule/configuration/test_operations.py new file mode 100644 index 00000000..32d48d39 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/configuration/test_operations.py @@ -0,0 +1,97 @@ +"""Unit tests of ``devcapsule/configuration/operations.py``: which platform's lock ``project init`` writes. + +Each test initializes a project under ``tmp_path`` without an IDE, so nothing resolves or reaches +Docker, and names its host with ``on_host`` where it is not the suite's amd64 Linux. +""" +from __future__ import annotations + +import io +from pathlib import Path + +import pytest + +from devcapsule.configuration.file_formats import ProjectConfigurationError +from devcapsule.configuration.operations import InitializeRequest, initialize_project +from devcapsule.configuration.storage import load_toml +from devcapsule.platforms import Platform +from devcapsule.resolution_matrix import MATRICES +from tests.unit.devcapsule.test_platforms import on_host + +CREATOR = "owner@example.test" + + +def locks_of(project: Path) -> list[str]: + return sorted(path.name for path in (project / ".devcapsule").glob("*.lock")) + + +def init_answering(project: Path, answers: str) -> str: + """Run an interactive init of ``project``, answering its questions in turn; return what it asked.""" + asked = io.StringIO() + initialize_project(InitializeRequest(directory=project, creator=CREATOR, interactive=True), + input_stream=io.StringIO(answers), output_stream=asked) + return asked.getvalue() + + +@pytest.fixture +def amd64_project_on_apple_silicon(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + """A project initialized for linux-amd64 on an Apple Silicon host.""" + on_host(monkeypatch, "Darwin", "arm64") + project = tmp_path / "project" + project.mkdir() + initialize_project(InitializeRequest(directory=project, creator=CREATOR, platform=Platform.LINUX_AMD64, + interactive=False)) + return project + + +@pytest.mark.own_host_patches +def test_init_platform_adds_that_lock_beside_the_projects_other(amd64_project_on_apple_silicon: Path) -> None: + """Init with a platform writes that platform's lock beside the project's existing one, with that platform's base.""" + initialize_project(InitializeRequest(directory=amd64_project_on_apple_silicon, platform=Platform.LINUX_ARM64, + interactive=False)) + assert locks_of(amd64_project_on_apple_silicon) == ["devcapsule.linux-amd64.lock", "devcapsule.linux-arm64.lock"] + arm64_lock = load_toml(amd64_project_on_apple_silicon / ".devcapsule" / "devcapsule.linux-arm64.lock") + (arm64_base,) = MATRICES[Platform.LINUX_ARM64].base_images() + assert arm64_lock["base"]["reference"] == arm64_base.reference + + +@pytest.mark.own_host_patches +def test_init_refuses_a_lock_the_project_would_not_run_as(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """Init completes the checkout for the lock it writes, so it refuses a lock of a platform the project + would not run as on this host, and writes nothing.""" + on_host(monkeypatch, "Darwin", "arm64") + project = tmp_path / "project" + project.mkdir() + initialize_project(InitializeRequest(directory=project, creator=CREATOR, platform=Platform.LINUX_ARM64, + interactive=False)) + with pytest.raises(ProjectConfigurationError, match="runs as linux-arm64"): + initialize_project(InitializeRequest(directory=project, platform=Platform.LINUX_AMD64, interactive=False)) + assert locks_of(project) == ["devcapsule.linux-arm64.lock"] + + +@pytest.mark.own_host_patches +@pytest.mark.parametrize("answer,expected", [("", "linux-arm64"), ("linux-amd64", "linux-amd64")]) +def test_init_asks_a_host_of_two_platforms_the_native_one_by_default( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, answer: str, expected: str, +) -> None: + """On a host of two platforms, init asks which a new project is for, the native one by default.""" + on_host(monkeypatch, "Darwin", "arm64") + project = tmp_path / "project" + project.mkdir() + asked = init_answering(project, f"{answer}\n" + "\n" * 8) + assert "Capsule platform for this project: linux-arm64 (native) or linux-amd64 (emulated) [linux-arm64]:" in asked + assert locks_of(project) == [f"devcapsule.{expected}.lock"] + + +@pytest.mark.own_host_patches +def test_init_keeps_the_platform_of_a_lock_the_host_runs_without_asking(amd64_project_on_apple_silicon: Path) -> None: + """Init of a project with a lock this host runs asks no platform and writes no other lock.""" + assert "Capsule platform" not in init_answering(amd64_project_on_apple_silicon, "\n" * 8) + assert locks_of(amd64_project_on_apple_silicon) == ["devcapsule.linux-amd64.lock"] + + +def test_a_host_of_one_platform_is_not_asked(tmp_path: Path) -> None: + """On a host of one platform, init asks no platform and locks the project for it.""" + project = tmp_path / "project" + project.mkdir() + assert "Capsule platform" not in init_answering(project, "\n" * 8) + assert locks_of(project) == ["devcapsule.linux-amd64.lock"] diff --git a/devcapsule-src/tests/unit/devcapsule/configuration/test_owner_commands.py b/devcapsule-src/tests/unit/devcapsule/configuration/test_owner_commands.py new file mode 100644 index 00000000..1c8fb7f2 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/configuration/test_owner_commands.py @@ -0,0 +1,79 @@ +"""Unit tests of ``devcapsule/configuration/owner_commands.py`` on a project locked for two platforms. + +Each test initializes its project under ``tmp_path`` on an Apple Silicon host the test sets up. +""" +from __future__ import annotations + +from pathlib import Path + +import pytest + +from devcapsule import cli +from devcapsule.configuration import owner_commands +from devcapsule.configuration.capability_commands import paths, recover +from devcapsule.configuration.file_formats import ProjectConfigurationError, render_toml +from devcapsule.configuration.storage import load_toml +from tests.unit.devcapsule.test_platforms import on_host + + +@pytest.fixture +def two_platform_project(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path: + """A project on Apple Silicon with a linux-amd64 lock and a linux-arm64 lock added beside it.""" + monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path / "config")) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + on_host(monkeypatch, "Darwin", "arm64") + root = tmp_path / "project" + root.mkdir() + assert cli.main(["project", "--path", str(root), "init", "--platform", "linux-amd64", "--require", "python", + "--creator", "mailto:owner@example.test"]) == 0 + assert cli.main(["project", "--path", str(root), "init", "--platform", "linux-arm64"]) == 0 + return root + + +def _locked_components(root: Path) -> dict[str, set[str]]: + return {path.name: set(load_toml(path).get("components", {})) + for path in sorted((root / ".devcapsule").glob("devcapsule.*.lock"))} + + +@pytest.mark.own_host_patches +def test_an_owner_edit_regenerates_every_platform_lock(two_platform_project: Path, capsys) -> None: + """An owner's edit of the shared policy regenerates every lock the project carries.""" + # Edited on the Mac, the committed linux-amd64 lock follows the manifest too. + assert cli.main(["project", "--path", str(two_platform_project), "config", "require", "dotnet", "--unverified"]) == 0 + assert "the manifest and its locks are updated" in capsys.readouterr().out + assert _locked_components(two_platform_project) == { + "devcapsule.linux-amd64.lock": {"dotnet-sdk"}, "devcapsule.linux-arm64.lock": {"dotnet-sdk"}, + } + + +@pytest.mark.own_host_patches +def test_an_edit_one_lock_cannot_follow_changes_nothing(two_platform_project: Path) -> None: + """An edit one platform's lock cannot follow is refused, naming that platform, and no file changes.""" + # The .NET SDK has no linux-arm64 validation yet; linux-amd64 alone would accept it. + before = {path: path.read_bytes() for path in (two_platform_project / ".devcapsule").iterdir() if path.is_file()} + with pytest.raises(ProjectConfigurationError, match="linux-arm64 lock cannot follow this edit"): + owner_commands.require_tools(two_platform_project, ["dotnet"]) + assert {path: path.read_bytes() for path in before} == before + + +@pytest.mark.own_host_patches +@pytest.mark.parametrize("committed", [False, True]) +def test_an_interrupted_edit_recovers_every_lock_to_one_side(two_platform_project: Path, committed: bool) -> None: + """Recovery of an interrupted edit puts the manifest and every lock on the same side of the edit.""" + devcapsule = two_platform_project / ".devcapsule" + manifest, locks = paths(two_platform_project)[0], sorted(devcapsule.glob("devcapsule.*.lock")) + before = {path: path.read_text() for path in (manifest, *locks)} + owner_commands.require_tools(two_platform_project, ["dotnet"], allow_unverified=True) + after = {path: path.read_text() for path in (manifest, *locks)} + assert all(before[path] != after[path] for path in locks) + journal = {"before-manifest": before[manifest], "after-manifest": after[manifest], + "before-locks": {path.name.split(".")[1]: before[path] for path in locks}, + "after-locks": {path.name.split(".")[1]: after[path] for path in locks}} + paths(two_platform_project)[2].write_text(render_toml(journal)) + if not committed: + manifest.write_text(before[manifest]) + locks[0].write_text(before[locks[0]]) # Interrupted between the two lock writes. + recover(two_platform_project) + expected = after if committed else before + assert {path: path.read_text() for path in (manifest, *locks)} == expected + assert not paths(two_platform_project)[2].exists() diff --git a/devcapsule-src/tests/unit/devcapsule/configuration/test_storage.py b/devcapsule-src/tests/unit/devcapsule/configuration/test_storage.py new file mode 100644 index 00000000..99f45ee8 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/configuration/test_storage.py @@ -0,0 +1,50 @@ +"""Unit tests of ``devcapsule/configuration/storage.py``: which capsule platform a project runs as on a host. + +Each test writes its project's lock files under ``tmp_path`` and names its host with ``on_host``. +""" +from __future__ import annotations + +from pathlib import Path + +import pytest + +from devcapsule.configuration.storage import locked_platforms, project_platform +from devcapsule.platforms import Platform +from tests.unit.devcapsule.test_platforms import on_host + + +def project_locked_for(root: Path, *platforms: str) -> Path: + """A project directory at ``root`` with an empty lock file for each of ``platforms``.""" + (root / ".devcapsule").mkdir(parents=True) + for platform in platforms: + (root / ".devcapsule" / f"devcapsule.{platform}.lock").write_text("") + return root + + +@pytest.mark.own_host_patches +@pytest.mark.parametrize("locked,expected", [ + ((), "linux-arm64"), + (("linux-amd64",), "linux-amd64"), + (("linux-arm64",), "linux-arm64"), + (("linux-amd64", "linux-arm64"), "linux-arm64"), +]) +def test_apple_silicon_runs_a_project_as_the_platform_it_is_locked_for( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, locked: tuple[str, ...], expected: str, +) -> None: + """Apple Silicon runs a project natively when it has an arm64 lock, else as its amd64 lock under + emulation; a project without a lock runs as the native platform.""" + on_host(monkeypatch, "Darwin", "arm64") + assert project_platform(project_locked_for(tmp_path / "project", *locked)) is Platform.parse(expected) + + +@pytest.mark.own_host_patches +def test_a_host_runs_a_project_only_as_a_platform_it_runs(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + """An amd64 Linux host runs an arm64-only project as its own platform; reading that lock reports it missing.""" + on_host(monkeypatch, "Linux", "x86_64") + assert project_platform(project_locked_for(tmp_path / "project", "linux-arm64")) is Platform.LINUX_AMD64 + + +def test_locked_platforms_names_every_lock_on_any_host(tmp_path: Path) -> None: + """A project's locked platforms are all those it has a lock for, including ones this host cannot run.""" + project = project_locked_for(tmp_path / "project", "linux-arm64", "linux-amd64") + assert locked_platforms(project) == (Platform.LINUX_AMD64, Platform.LINUX_ARM64) diff --git a/devcapsule-src/tests/unit/devcapsule/images/__init__.py b/devcapsule-src/tests/unit/devcapsule/images/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/devcapsule-src/tests/unit/devcapsule/images/test_base.py b/devcapsule-src/tests/unit/devcapsule/images/test_base.py new file mode 100644 index 00000000..2f44ca85 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/images/test_base.py @@ -0,0 +1,71 @@ +"""Unit tests of ``devcapsule/images/base.py``: the base build's checks before it hands the plan to the builder. + +The builder and the public-source verifier are fakes; the PEX is a fixture under ``tmp_path``. +""" +from __future__ import annotations + +from pathlib import Path +from unittest.mock import Mock + +import pytest + +from devcapsule.compat import CliError +from devcapsule.images.base import BaseImageBuildOptions, build_base_image +from devcapsule.platforms import Platform +from tests.test_base_image import pex_fixture + + +def test_base_image_build_forwards_host_network_to_buildx(tmp_path: Path) -> None: + """A base build verifies its source and hands the builder the named network and the capsule platform.""" + pex = pex_fixture(tmp_path / "devcapsule.pex") + options = BaseImageBuildOptions(pex, "test-base:latest", source_revision="a" * 40) + builder = Mock() + source_verifier = Mock() + + build_base_image(options, builder=builder, platform=Platform.LINUX_AMD64, network="host", + source_verifier=source_verifier) + + source_verifier.assert_called_once() + builder.build.assert_called_once() + assert builder.build.call_args.args[0].image == "test-base:latest" + assert builder.build.call_args.kwargs == {"network": "host", "platform": Platform.LINUX_AMD64} + + +def test_base_image_rejects_unreachable_revision_before_buildx(tmp_path: Path) -> None: + """A source revision the verifier cannot reach stops the build before the builder starts.""" + pex = pex_fixture(tmp_path / "devcapsule.pex") + options = BaseImageBuildOptions(pex, "test-base:latest", source_revision="a" * 40) + builder = Mock() + source_verifier = Mock(side_effect=CliError("source revision is not publicly reachable")) + + with pytest.raises(CliError, match="not publicly reachable"): + build_base_image(options, builder=builder, platform=Platform.LINUX_AMD64, source_verifier=source_verifier) + + builder.build.assert_not_called() + + +def test_console_base_rejects_missing_source_before_starting_the_builder(tmp_path: Path) -> None: + """A web-console base without public source stops before the builder starts, even with local source allowed.""" + pex = pex_fixture(tmp_path / "devcapsule.pex", revision="unknown", public=False) + builder = Mock() + with pytest.raises(CliError, match="--allow-local-source cannot supply the console source"): + build_base_image(BaseImageBuildOptions(pex, allow_local_source=True), builder=builder, + platform=Platform.LINUX_AMD64) + builder.build.assert_not_called() + + +def test_base_image_local_source_escape_hatch_skips_public_verification(tmp_path: Path) -> None: + """Allowing local source skips the public-source verification and still builds.""" + pex = pex_fixture(tmp_path / "devcapsule.pex") + builder = Mock() + source_verifier = Mock() + + build_base_image( + BaseImageBuildOptions(pex, allow_local_source=True), + builder=builder, + platform=Platform.LINUX_AMD64, + source_verifier=source_verifier, + ) + + source_verifier.assert_not_called() + builder.build.assert_called_once() diff --git a/devcapsule-src/tests/unit/devcapsule/images/test_build.py b/devcapsule-src/tests/unit/devcapsule/images/test_build.py new file mode 100644 index 00000000..43c84dbc --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/images/test_build.py @@ -0,0 +1,68 @@ +"""Unit tests of ``devcapsule/images/build.py``: how the buildx builder lays out its build context. + +``docker.build`` is a fake; the contexts are directories under ``tmp_path``. +""" +from __future__ import annotations + +from pathlib import Path +from unittest.mock import patch + +import pytest + +from devcapsule.compat import CliError +from devcapsule.images.build import BuildxImageBuilder, DirectoryComponent, ImageBuildSpec +from devcapsule.platforms import Platform + + +def test_buildx_builder_reuses_one_context_root_and_attaches_named_contexts(tmp_path: Path) -> None: + """BuildKit keys incremental transfer on the context root's path: the same + root every build, directories attached as named contexts, nothing copied.""" + + context_root = tmp_path / "build-contexts" / "context" + tree = tmp_path / "tree" + tree.mkdir() + (tree / "big.bin").write_bytes(b"x" * 1024) + spec = ImageBuildSpec(image="result:test", base_image="sha256:base", + components=(DirectoryComponent(tree, "/opt/tree"),)) + with patch("devcapsule.images.build.docker.build") as build: + BuildxImageBuilder(context_root=context_root).build(spec, platform=Platform.LINUX_AMD64, network="none") + (context_root / "stale-from-last-build").write_text("x") + BuildxImageBuilder(context_root=context_root).build(spec, platform=Platform.LINUX_AMD64, network="host") + first, second = build.call_args_list + assert first.args[0] == context_root and second.args[0] == context_root + assert first.kwargs["build_contexts"] == {"copy-dir-0": str(tree)} + assert first.kwargs["network"] == "none" and first.kwargs["allow"] == [] + assert second.kwargs["allow"] == ["network.host"] + assert sorted(child.name for child in context_root.iterdir()) == ["Dockerfile"] # cleared each build + assert (tmp_path / "build-contexts" / "context.lock").is_file() + assert not (context_root / "copy-dir-0").exists() + + +def test_buildx_builder_without_a_root_uses_a_temporary_context(tmp_path: Path) -> None: + """A builder without a context root builds from a temporary directory it removes afterwards.""" + with patch("devcapsule.images.build.docker.build") as build: + BuildxImageBuilder().build(ImageBuildSpec(image="result:test", base_image="sha256:base"), + platform=Platform.LINUX_AMD64, network="none") + used_context = build.call_args.args[0] + assert used_context.name.startswith("devcapsule-buildx-context-") and not used_context.exists() + assert build.call_args.kwargs["build_contexts"] == {} + + +def test_reused_context_preserves_sources_and_recovers_after_build_failure(tmp_path: Path) -> None: + """A failed build leaves its sources intact, and the next build in the same context starts from a cleared directory.""" + + source = tmp_path / "source" + source.mkdir() + (source / "payload").write_text("keep") + context = tmp_path / "context" + context.mkdir() + (context / "stale-link").symlink_to(source, target_is_directory=True) + spec = ImageBuildSpec("result:test", "base:test", (DirectoryComponent(source, "/opt/tool"),)) + with patch("devcapsule.images.build.docker.build", side_effect=OSError("build failed")): + with pytest.raises(CliError, match="build failed"): + BuildxImageBuilder(context).build(spec, platform=Platform.LINUX_AMD64) + with patch("devcapsule.images.build.docker.build") as build: + BuildxImageBuilder(context).build(spec, platform=Platform.LINUX_AMD64) + assert build.call_count == 1 + assert (source / "payload").read_text() == "keep" + assert sorted(path.name for path in context.iterdir()) == ["Dockerfile"] diff --git a/devcapsule-src/tests/unit/devcapsule/launch/__init__.py b/devcapsule-src/tests/unit/devcapsule/launch/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/devcapsule-src/tests/unit/devcapsule/launch/pycharm/__init__.py b/devcapsule-src/tests/unit/devcapsule/launch/pycharm/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/devcapsule-src/tests/unit/devcapsule/launch/pycharm/test_launcher.py b/devcapsule-src/tests/unit/devcapsule/launch/pycharm/test_launcher.py new file mode 100644 index 00000000..d287e6fe --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/launch/pycharm/test_launcher.py @@ -0,0 +1,1806 @@ +"""Unit tests of ``devcapsule/launch/pycharm/_launcher.py``: the run configuration a launch derives, +the Docker plan it builds and the session it runs. + +Each test builds its project and environment under ``tmp_path`` and replaces Docker and the +host's processes with fakes. The suite's host is amd64 Linux unless a test sets another. +""" +from __future__ import annotations + +from dataclasses import replace +from datetime import datetime, timedelta, timezone +import json +from pathlib import Path +import socket +from threading import Event +import time +from types import SimpleNamespace +from typing import cast, Callable +from unittest.mock import patch, Mock + +import pytest + +from devcapsule import logs +from devcapsule.components.codex import runtime_template as codex_runtime_template +from devcapsule.components.codium import runtime_template as codium_runtime_template +from devcapsule.components.pycharm import runtime_template as pycharm_runtime_template +from devcapsule.container_runtime.contract import ConsolePlan, DisplayPlan, Identity, LogsPlan, RuntimePlan +from devcapsule.display_client import display_url +from devcapsule.host_open import ( + HOST_OPEN_BROWSER, + HOST_OPEN_INTEGRATION, + HOST_OPEN_SOCKET_DESTINATION, + HOST_OPEN_SOCKET_ENV, +) +from devcapsule.launch.pycharm import ( + DockerMode, + IdeConfigMode, + PycharmRunOptions, + build_run_config, + _launcher as launcher, +) +from devcapsule.launch.pycharm._launcher import ( + AttachedResult, + display_disclosure, + describe_run_command, + console_disclosure, + HostUser, + PycharmRunError, + PycharmRunConfig, + SUDOERS_POLICY, + SUDOERS_POLICY_PATH, + TempRuntimeFiles, + build_docker_args, + cleanup_temp_runtime_files, + ensure_sudoers_policy_ownership, + prepare_temp_runtime_files, + print_storage_summary, + remove_abandoned_sudoers_volumes, + run_pycharm, + write_user_files, + channel_watcher, +) +from devcapsule.launch.session_logs import SESSION_LOG_DESTINATION, mark_session_ended +from devcapsule.materialization import RUNTIME_PLAN_PATH +from devcapsule.platforms import HostOs, Platform +from devcapsule.project import plan_project +from devcapsule.runtime_configuration import ( + CHANNEL_PATH, + CONFIGURATION_PATH, + CONTEXT_PATH, + LaunchConfiguration, +) +from tests.test_pycharm import base_env, contained_config, external_runtime_plan, session_config + + +def codium_external_runtime_plan() -> RuntimePlan: + return RuntimePlan.for_component( + codium_runtime_template(), + project_path="/workspace/project", + home="/home/devcapsule", + identity=Identity(1000, 1000, "developer"), + ) + + +def codium_interactive_state_mounts(tmp_path: Path) -> dict[str, tuple[Path, str]]: + return { + "codium/user-data": (tmp_path / "slots" / "user-data", "/ide-user-data"), + "codium/extensions": (tmp_path / "slots" / "extensions", "/ide-extensions"), + "codium/cache": (tmp_path / "slots" / "cache", "/home/devcapsule/.cache"), + } + + +def _macos_sudo_files(tmp_path: Path) -> tuple[PycharmRunConfig, TempRuntimeFiles]: + policy = tmp_path / "sudoers" + policy.write_text(SUDOERS_POLICY, encoding="utf-8") + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + sudoers_file=policy, + ) + config = cast( + PycharmRunConfig, + SimpleNamespace(enable_sudo=True, image="devcapsule-local-pycharm:canonical", name="capsule", + capsule_platform=Platform.LINUX_AMD64, daemon_architecture="arm64"), + ) + return config, files + + +def test_authorized_runtime_effects_build_the_complete_docker_plan(tmp_path: Path) -> None: + """Authorized network, Docker access and development sudo each appear in the Docker plan, with a private sudo policy.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + docker_socket = tmp_path / "docker.sock" + docker_socket.touch() + home = tmp_path / "persistent-home" + config_dir = tmp_path / "pycharm-config" + plugins = tmp_path / "pycharm-plugins" + system = tmp_path / "pycharm-system" + log = tmp_path / "pycharm-log" + cache = tmp_path / "pycharm-cache" + host_user = HostUser(uid=1000, gid=1000, name="developer", group_name="developer") + + with ( + patch("devcapsule.launch.pycharm._launcher.current_host_user", return_value=host_user), + patch("devcapsule.launch.pycharm._launcher.is_socket", return_value=True), + ): + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + persistent_home=home, + ide_config=config_dir, + plugins=plugins, + ide_system=system, + ide_log=log, + tool_cache=cache, + docker_mode=DockerMode.host, + host_docker_socket=docker_socket, + enable_sudo=True, + network_mode="host", + memory_limit_bytes=8 * 1024**3, + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + with patch("devcapsule.launch.pycharm._launcher.write_xauthority"): + files = prepare_temp_runtime_files(config, env) + try: + assert files.sudoers_file is not None + assert files.sudoers_directory is not None + sudoers_directory = files.sudoers_directory + assert sudoers_directory.stat().st_mode & 0o777 == 0o700 + sudoers_file = files.sudoers_file + assert sudoers_file.read_text(encoding="utf-8") == SUDOERS_POLICY + assert sudoers_file.stat().st_mode & 0o777 == 0o440 + args = build_docker_args(config, files, env) + files.sudoers_volume = "capsule-sudoers" + volume_args = build_docker_args(config, files, env) + files.sudoers_volume = None + files.sudoers_file = None + with pytest.raises(PycharmRunError, match="shadow and sudoers files"): + build_docker_args(config, files, env) + files.sudoers_file = sudoers_file + finally: + cleanup_temp_runtime_files(files) + + assert ["--network", "host"] == args[args.index("--network") : args.index("--network") + 2] + assert ["--memory", str(8 * 1024**3)] == args[args.index("--memory") : args.index("--memory") + 2] + assert ["--user", "1000:1000"] == args[args.index("--user") : args.index("--user") + 2] + assert "ENABLE_SUDO=1" in args + assert f"type=bind,src={sudoers_file},dst={SUDOERS_POLICY_PATH},ro" in args + # On macOS the policy comes from its volume, not the Mac's file. + assert f"type=volume,src=capsule-sudoers,volume-subpath=policy,dst={SUDOERS_POLICY_PATH},ro" in volume_args + assert f"type=bind,src={sudoers_file},dst={SUDOERS_POLICY_PATH},ro" not in volume_args + assert not sudoers_file.exists() + assert not sudoers_directory.exists() + assert "DOCKER_HOST=unix:///run/host-docker.sock" in args + assert f"DEVCAPSULE_CONTAINER_NAME={config.name}" in args + assert f"type=bind,src={docker_socket.resolve()},dst=/run/host-docker.sock" in args + for source, destination in ( + (project.resolve(), "/workspace/project"), + (home.resolve(), "/home/devcapsule"), + (config_dir.resolve(), "/ide-config"), + (plugins.resolve(), "/ide-plugins"), + (system.resolve(), "/ide-project-state/system"), + (log.resolve(), "/ide-project-state/log"), + (cache.resolve(), "/home/devcapsule/.cache"), + ): + assert f"type=bind,src={source},dst={destination}" in args + assert "--read-only" not in args + assert "--privileged" not in args + assert "SYS_ADMIN" not in args + + +def test_bound_secret_must_exist_on_host(tmp_path: Path) -> None: + """A secret environment variable bound to the launch must be set on the host, or the run configuration is refused.""" + project = tmp_path / "project" + project.mkdir() + with pytest.raises(PycharmRunError, match="is not set on the host"): + build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + docker_mode=DockerMode.none, + secret_environment=("OPENAI_API_KEY",), + ), + base_env(tmp_path), + ) + + +@pytest.mark.parametrize("report_only", [False, True]) + + +def test_console_launcher_cleans_up_after_command_reporting_or_docker_failure( + tmp_path: Path, report_only: bool, +) -> None: + """A launch with the web console reports its command without the token and removes its temporary files whether Docker succeeds or fails.""" + + config = contained_config(tmp_path) + runtime = tmp_path / "runtime" + env = {**base_env(tmp_path), "XDG_RUNTIME_DIR": str(runtime)} + report = Mock() + options = PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64, command_report=report if report_only else None) + with ( + patch("devcapsule.launch.pycharm._launcher.build_run_config", return_value=config), + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + patch("devcapsule.launch.pycharm._launcher.watch_display_ready") as watch, + patch("devcapsule.launch.pycharm._launcher.run_attached", side_effect=OSError("docker failed")) as docker, + ): + if report_only: + assert run_pycharm(options, env) == 0 + docker.assert_not_called() + watch.assert_not_called() + report.assert_called_once() + assert "Temporary Console token:" in report.call_args.args[0] + assert config.console_token not in report.call_args.args[0] + else: + with pytest.raises(OSError, match="docker failed"): + run_pycharm(options, env) + assert watch.call_count == 2 + assert watch.call_args.args[3].is_set() + assert list(runtime.iterdir()) == [] + + +def test_detached_lifecycle_changes_only_the_docker_process_flags(tmp_path: Path) -> None: + """Every launch stays attached: Docker removes the container on exit and no detach flag is planned.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + runtime_plan_file=tmp_path / "runtime-plan", + ) + + args = build_docker_args(config, files, env) + + # The launcher owns exactly one lifecycle: attached, removed on exit. + assert args[:2] == ["--rm", "-i"] + assert "--detach" not in args and "-d" not in args + + +def test_display_disclosure_states_the_transport(tmp_path: Path) -> None: + """The display disclosure names the transport: the contained desktop's loopback port, or the host X session it shares.""" + contained = contained_config(tmp_path) + text = display_disclosure(contained) + assert "Contained desktop" in text + assert f"published to host loopback port {contained.display_host_port}" in text + assert "No host X socket, credential, or DISPLAY is shared" in text + assert "boundary test is waived" in display_disclosure( + build_run_config( + PycharmRunOptions( + project=tmp_path / "project", + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + ), + base_env(tmp_path), + ) + ) + + +def test_dogfood_legacy_roots_map_to_persistent_home_and_component_slots(tmp_path: Path) -> None: + """Legacy dogfood state roots map onto the persistent home and the component's state slots.""" + project = tmp_path / "project" + project.mkdir() + global_settings = tmp_path / "docker-pycharm-codex" / "state" + plugins = tmp_path / "docker-pycharm-codex" / "plugins" + project_state = tmp_path / ".state" / "project" + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + global_settings=global_settings, + plugins=plugins, + project_state=project_state, + docker_mode=DockerMode.none, + ), + base_env(tmp_path), + ) + + assert config.persistent_home == (global_settings / "home").resolve() + assert config.ide_config == (global_settings / "config").resolve() + assert config.plugins == plugins.resolve() + assert config.ide_system == (project_state / "system").resolve() + assert config.ide_log == (project_state / "log").resolve() + assert config.tool_cache == (project_state / "home" / ".cache").resolve() + + +def test_env_ide_config_dir_implies_custom_when_mode_is_unset(tmp_path: Path) -> None: + """An IDE configuration directory from the environment selects the custom configuration mode when none is set.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["PYCHARM_IDE_CONFIG_DIR"] = str(tmp_path / "env-config") + + config = build_run_config(PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, docker_mode=DockerMode.none), env) + + assert config.ide_config_mode == "custom" + assert config.ide_config == (tmp_path / "env-config").resolve() + + +def test_explicit_project_state_overrides_project_state_root(tmp_path: Path) -> None: + """An explicit project state directory wins over the project state root.""" + project = tmp_path / "project" + project.mkdir() + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_state=tmp_path / "explicit-state", + project_state_root=tmp_path / ".state", + docker_mode=DockerMode.none, + ), + base_env(tmp_path), + ) + + assert config.project_state == (tmp_path / "explicit-state").resolve() + + +def test_explicit_shared_config_mode_ignores_env_ide_config_dir(tmp_path: Path) -> None: + """An explicit shared configuration mode ignores an IDE configuration directory from the environment.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["PYCHARM_IDE_CONFIG_DIR"] = str(tmp_path / "env-config") + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + config_mode=IdeConfigMode.shared, + docker_mode=DockerMode.none, + ), + env, + ) + + assert config.ide_config_mode == "shared" + project_id = plan_project(project).project_id + assert config.ide_config == ( + tmp_path / "data" / "devcapsule" / "projects" / "by-path" / project_id / "components" / "pycharm" / "config" + ).resolve() + + +def test_external_runtime_plan_is_readable_and_mounted_read_only(tmp_path: Path) -> None: + """An external runtime plan is written world-readable and mounted read-only at the plan path.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + with ( + patch("devcapsule.launch.pycharm._launcher.write_xauthority"), + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + ): + files = prepare_temp_runtime_files(config, env) + try: + assert files.runtime_plan_file is not None + assert files.runtime_plan_file.stat().st_mode & 0o777 == 0o644 + # The plan now records the transport explicitly, even for passthrough, + # and the web console, which runs whenever the runtime runs. + written = RuntimePlan.from_file(files.runtime_plan_file) + assert written.display == DisplayPlan.host_x11() + assert written.console == ConsolePlan("0.0.0.0", 6081, "/run/devcapsule-console-token") + assert written.logs == LogsPlan(SESSION_LOG_DESTINATION, segment_bytes=8 * 1024 * 1024, rotations=4) + assert written == external_runtime_plan().with_display(DisplayPlan.host_x11()).with_console(written.console).with_logs(written.logs) + args = build_docker_args(config, files, env) + assert "JAVA_TOOL_OPTIONS=-Dide.browser.jcef.sandbox.enable=false" in args + assert "SYS_ADMIN" not in args + assert "seccomp=unconfined" not in args + assert "apparmor=unconfined" not in args + assert ( + f"type=bind,src={files.runtime_plan_file},dst={RUNTIME_PLAN_PATH},ro" + in args + ) + finally: + cleanup_temp_runtime_files(files) + assert files.runtime_plan_file is not None + assert not files.runtime_plan_file.exists() + + +def test_host_browser_bridge_can_be_disabled_despite_inherited_environment( + tmp_path: Path, +) -> None: + """A launch without the host-browser bridge plans no broker socket, whatever the inherited environment names.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + socket_path = tmp_path / "host-open.sock" + + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as broker: + broker.bind(str(socket_path)) + env[HOST_OPEN_SOCKET_ENV] = str(socket_path) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + docker_mode=DockerMode.none, + enable_host_browser=False, + ), + env, + ) + + assert config.host_browser_socket is None + + +def test_host_browser_bridge_is_explicit_in_plan_mount_environment_and_summary( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """An enabled host-browser bridge appears in the runtime plan, the socket mount, the BROWSER variable and the summary.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + socket_path = tmp_path / "host-open.sock" + + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as broker: + broker.bind(str(socket_path)) + env[HOST_OPEN_SOCKET_ENV] = str(socket_path) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + enable_host_browser=True, + ), + env, + ) + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + runtime_plan_file=tmp_path / "runtime-plan", + ) + args = build_docker_args(config, files, env) + print_storage_summary(config) + + assert config.runtime_plan is not None + assert config.runtime_plan.host_integrations == (HOST_OPEN_INTEGRATION,) + assert f"BROWSER={HOST_OPEN_BROWSER}" in args + assert f"{HOST_OPEN_SOCKET_ENV}={HOST_OPEN_SOCKET_DESTINATION}" in args + assert ( + f"type=bind,src={socket_path},dst={HOST_OPEN_SOCKET_DESTINATION},ro" in args + ) + disclosure = capsys.readouterr().err + assert "Any process running as the" in disclosure + assert "capsule user can ask the physical host" in disclosure + + +@pytest.mark.parametrize("host_os,expected_gid", [(HostOs.LINUX, "file"), (HostOs.DARWIN, 0)]) + + +def test_host_docker_group_follows_where_the_socket_is_owned(tmp_path: Path, host_os, expected_gid) -> None: + """The capsule joins the group that owns the Docker socket: the socket file's on Linux, group 0 on Docker Desktop.""" + # On Linux the capsule joins the socket file's group. Docker Desktop shows + # its socket inside the VM as root:root 0660, so a Mac host joins group 0 + # and writes no second group for it. + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + docker_socket = tmp_path / "docker.sock" + docker_socket.touch() + gid = docker_socket.stat().st_gid if expected_gid == "file" else expected_gid + host_user = HostUser(uid=501, gid=20, name="developer", group_name="staff") + with ( + patch("devcapsule.launch.pycharm._launcher.current_host_user", return_value=host_user), + patch("devcapsule.launch.pycharm._launcher.is_socket", return_value=True), + patch("devcapsule.launch.pycharm._launcher.HostOs.current", return_value=host_os), + ): + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.host, + host_docker_socket=docker_socket, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + with patch("devcapsule.launch.pycharm._launcher.write_xauthority"): + files = prepare_temp_runtime_files(config, env) + try: + args = build_docker_args(config, files, env) + groups = files.group_file.read_text(encoding="utf-8") + finally: + cleanup_temp_runtime_files(files) + + assert config.host_docker_gid == gid + assert ["--group-add", str(gid)] == args[args.index("--group-add") : args.index("--group-add") + 2] + assert ("host-docker:x:" in groups) is (gid not in (0, host_user.gid)) + + +@pytest.mark.parametrize("host_os,effective", [(HostOs.LINUX, "host"), (HostOs.DARWIN, "bridge")]) + + +def test_host_networking_falls_back_to_bridge_on_macos(tmp_path: Path, capsys, host_os, effective) -> None: + """Host networking falls back to bridge networking, with a warning, on a host whose Docker host network it cannot reach.""" + # Docker Desktop's host network is its VM's; the contained desktop would + # listen where the Mac cannot reach it. + project = tmp_path / "project" + project.mkdir() + with patch("devcapsule.launch.pycharm._launcher.HostOs.current", return_value=host_os): + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="host", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + base_env(tmp_path), + ) + assert config.network_mode == effective + assert ("host networking is unavailable on macOS" in capsys.readouterr().err) is (effective == "bridge") + + +def test_host_x11_passthrough_still_needs_a_host_display_and_binds_it(tmp_path: Path) -> None: + """Host X11 passthrough requires a host display and binds its socket and credential into the capsule.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env.pop("DISPLAY") + with pytest.raises(PycharmRunError, match="DISPLAY is not set"): + build_run_config(PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project"), env) + env = base_env(tmp_path) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + assert config.display_transport == "host-x11" + assert config.runtime_plan is not None and config.runtime_plan.display == DisplayPlan.host_x11() + with ( + patch("devcapsule.launch.pycharm._launcher.write_xauthority") as xauthority, + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + ): + files = prepare_temp_runtime_files(config, env) + try: + xauthority.assert_called_once() + assert files.display_token_file is None + args = build_docker_args(config, files, env) + assert "type=bind,src=/tmp/.X11-unix,dst=/tmp/.X11-unix,ro" in args + assert "XAUTHORITY=/tmp/.docker.xauth" in args + assert "DISPLAY" in args + published = [args[index + 1] for index, item in enumerate(args) if item == "--publish"] + assert published == [f"127.0.0.1:{config.console_host_port}:6081"] # the console only; no display port + finally: + cleanup_temp_runtime_files(files) + + +def test_ide_config_option_implies_custom_config_mode(tmp_path: Path) -> None: + """An explicit IDE configuration directory selects the custom configuration mode.""" + project = tmp_path / "project" + project.mkdir() + custom_config = tmp_path / "custom-config" + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + ide_config=custom_config, + docker_mode=DockerMode.none, + ), + base_env(tmp_path), + ) + + assert config.ide_config_mode == "custom" + assert config.ide_config == custom_config.resolve() + + +def test_image_process_uses_oci_command_and_cleans_all_temporary_files(tmp_path: Path) -> None: + """An image-process launch runs the image's own OCI command and removes every temporary file afterwards.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + image = "devcapsule-local-pycharm:canonical" + with ( + patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), + patch("devcapsule.launch.pycharm._launcher.run_attached") as run, + ): + run.return_value = AttachedResult(0, interrupted=False, stopped=True) + assert ( + run_pycharm( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + image=image, + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + == 0 + ) + + command = run.call_args.args[0] + assert command[-1] == image + assert "/opt/pycharm/bin/pycharm.sh" not in command + temporary_sources = [ + Path(next(part.removeprefix("src=") for part in argument.split(",") if part.startswith("src="))) + for argument in command + if argument.startswith("type=bind,src=") + and any( + destination in argument + for destination in ( + "dst=/tmp/.docker.xauth", + "dst=/etc/passwd", + "dst=/etc/group", + f"dst={RUNTIME_PLAN_PATH}", + ) + ) + ] + assert len(temporary_sources) == 4 + assert all(not path.exists() for path in temporary_sources) + + +def test_interactive_state_mounts_must_belong_to_the_plan_component(tmp_path: Path) -> None: + """Interactive state mounts must name slots of the runtime plan's own component.""" + project = tmp_path / "project" + project.mkdir() + with pytest.raises(PycharmRunError, match="Interactive state mount 'codex/home'"): + build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=codium_external_runtime_plan(), + use_image_process=True, + interactive_state_mounts={ + "codex/home": (tmp_path / "codex", "/home/devcapsule/.codex"), + }, + ), + base_env(tmp_path), + ) + + +def test_interactive_state_mounts_require_a_runtime_plan(tmp_path: Path) -> None: + """Interactive state mounts are refused without a runtime plan.""" + project = tmp_path / "project" + project.mkdir() + with pytest.raises(PycharmRunError, match="external runtime plan"): + build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + interactive_state_mounts=codium_interactive_state_mounts(tmp_path), + ), + base_env(tmp_path), + ) + + +def test_interrupted_launch_ends_with_a_result_and_the_log_paths(tmp_path, capsys): + """Ctrl+C ends with a result, not a traceback (the first-session report of 2026-09-15, C10).""" + + config, env = session_config(tmp_path) + stops = [] + stopped_before_return = False + + class Client: + waits = 0 + + def wait(self, timeout=None): + nonlocal stopped_before_return + self.waits += 1 + if self.waits == 1: + assert stops and not any(stop.is_set() for stop in stops) + raise KeyboardInterrupt + stopped_before_return = bool(stops) and all(stop.is_set() for stop in stops) + return 143 + + try: + with ( + patch.object(launcher, "build_run_config", return_value=config), + patch.object(launcher, "write_xauthority"), + patch.object(launcher, "write_user_files"), + patch.object(launcher, "watch_display_ready", side_effect=lambda port, url, opener, stop, **kw: stops.append(stop)), + patch.object(launcher.subprocess, "Popen", return_value=Client()), + ): + assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == launcher.INTERRUPTED_EXIT_CODE + assert stopped_before_return + assert stops and all(stop.is_set() for stop in stops) + assert list((tmp_path / "runtime").iterdir()) == [] + assert config.session_log_directory is not None + record = (config.session_log_directory / "devcapsule-launcher.log").read_text() + assert "session end: interrupted from the terminal (Ctrl+C); docker run exit code 143" in record + err = capsys.readouterr().err + assert "Session interrupted from the terminal (Ctrl+C); the capsule stopped." in err + assert "Session logs on this computer:" in err + assert f" {config.session_log_directory / 'devcapsule-launcher.log'}" in err + assert f" {config.session_log_directory / 'session.json'}" in err + finally: + logs.reset() + + +def test_interrupted_launch_with_unstopped_capsule_leaves_session_manifest_unended(tmp_path: Path) -> None: + """An interrupted launch whose capsule may still run leaves the session manifest without an end.""" + + + config, env = session_config(tmp_path) + assert config.session_log_directory is not None + try: + with ( + patch.object(launcher, "build_run_config", return_value=config), + patch.object(launcher, "write_xauthority"), + patch.object(launcher, "write_user_files"), + patch.object(launcher, "watch_display_ready"), + patch.object( + launcher, + "run_attached", + return_value=AttachedResult(launcher.INTERRUPTED_EXIT_CODE, interrupted=True, stopped=False), + ), + ): + assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == launcher.INTERRUPTED_EXIT_CODE + manifest = json.loads((config.session_log_directory / "session.json").read_text(encoding="utf-8")) + assert "ended-at" not in manifest and "exit-code" not in manifest + assert not logs.session_ended(config.session_log_directory) + record = (config.session_log_directory / "devcapsule-launcher.log").read_text(encoding="utf-8") + assert f"session end unknown: interrupted from the terminal (Ctrl+C); container {config.name} may still be running" in record + finally: + logs.reset() + + +def test_jcef_disclosure_is_printed_for_component_policy( + tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """A component whose policy disables JCEF's inner sandbox discloses it, and that Docker's isolation is unchanged.""" + project = tmp_path / "project" + project.mkdir() + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + base_env(tmp_path), + ) + + print_storage_summary(config) + + disclosure = capsys.readouterr().err + assert "JCEF's inner Chromium sandbox is disabled" in disclosure + assert "Docker's outer isolation is unchanged" in disclosure + + +def test_launch_preparation_failure_cleans_runtime_and_identity_files(tmp_path: Path) -> None: + """A failure while preparing a launch removes the runtime plan and identity files it wrote.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + with ( + patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), + patch( + "devcapsule.launch.pycharm._launcher.cleanup_temp_runtime_files", + wraps=cleanup_temp_runtime_files, + ) as cleanup, + ): + with pytest.raises(PycharmRunError, match="variable is empty or unset"): + run_pycharm( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + image="devcapsule-local-pycharm:canonical", + docker_mode=DockerMode.none, + network_mode="bridge", + git_token_env="MISSING_TOKEN", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + + files = cleanup.call_args.args[0] + assert files.runtime_plan_file is not None + assert all( + path is None or not path.exists() + for path in ( + files.xauth_file, + files.passwd_file, + files.group_file, + files.runtime_plan_file, + ) + ) + + +def test_launcher_mounts_live_configuration_directory_and_private_snapshot_read_only(tmp_path): + """The launch context is a private snapshot, and the configuration channel directory is mounted live; both read-only where the plan says so.""" + project = tmp_path / "project" + project.mkdir() + records = tmp_path / "records" + records.mkdir() + document = {"format": 1, "launcher-root": str(project), "runtime-root": "/workspace/project", + "checkout-file": "devcapsule.checkout.toml", "running": {"identity": "captured-at-launch"}} + home = tmp_path / "chosen-home" + home.mkdir() + (home / "settings.toml").write_text('minimum-devcapsule = "0.3.0"\ndevcapsule-settings-schema-version = 1\n') + document["configuration-home"] = str(home) + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + config = build_run_config(PycharmRunOptions( + project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none, + runtime_plan=external_runtime_plan(), use_image_process=True, + launch_configuration=LaunchConfiguration(records, document), + ), env) + with patch("devcapsule.launch.pycharm._launcher.write_xauthority"), patch("devcapsule.launch.pycharm._launcher.write_user_files"): + files = prepare_temp_runtime_files(config, env) + try: + assert files.launch_context_file is not None + assert files.launch_context_file.stat().st_mode & 0o777 == 0o600 + assert json.loads(files.launch_context_file.read_text()) == document + args = build_docker_args(config, files, env) + assert f"type=bind,src={records},dst={CONFIGURATION_PATH},ro" in args + assert f"type=bind,src={home}/settings.toml,dst=/etc/devcapsule/home/settings.toml,ro" in args + assert not any("DEVCAPSULE_CONFIG_HOME" in arg for arg in args) + assert not any(f"src={home}," in arg for arg in args) + assert f"type=bind,src={files.launch_context_file},dst={CONTEXT_PATH},ro" in args + assert not any("src=" + str(records / "devcapsule.checkout.toml") in arg for arg in args) + # The console's channel: a private directory, mounted writable, with the launch listing when run computed one. + assert files.configuration_directory is not None and files.configuration_directory.stat().st_mode & 0o777 == 0o700 + assert f"type=bind,src={files.configuration_directory},dst={CHANNEL_PATH}" in args + assert not (files.configuration_directory / "listing.json").exists() + watcher = channel_watcher(config, files) + assert watcher is not None and watcher.checkout_record == records / "devcapsule.checkout.toml" and watcher.root == project + finally: + cleanup_temp_runtime_files(files) + assert not files.launch_context_file.exists() and not files.configuration_directory.exists() + listing = {"schema-version": 2, "rows": [], "pending": [], "notes": []} + config = build_run_config(PycharmRunOptions( + project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none, + runtime_plan=external_runtime_plan(), use_image_process=True, + launch_configuration=LaunchConfiguration(records, document, listing), + ), env) + with patch("devcapsule.launch.pycharm._launcher.write_xauthority"), patch("devcapsule.launch.pycharm._launcher.write_user_files"): + files = prepare_temp_runtime_files(config, env) + try: + assert files.configuration_directory is not None + written = json.loads((files.configuration_directory / "listing.json").read_text()) + assert written["format"] == 1 and written["listing"] == listing and written["at"] + assert "Temporary Configuration channel:" in describe_run_command(["docker", "run"], config, files) + finally: + cleanup_temp_runtime_files(files) + + +def test_macos_sudo_policy_failure_removes_its_volume( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """On macOS, a failure to write the sudo policy removes the volume created for it.""" + monkeypatch.setattr(HostOs, "current", classmethod(lambda cls: HostOs.DARWIN)) + config, files = _macos_sudo_files(tmp_path) + + with patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run: + run.side_effect = [ + SimpleNamespace(returncode=0, stderr="", stdout=""), + SimpleNamespace(returncode=0, stderr=""), + SimpleNamespace(returncode=125, stderr="no such image"), + SimpleNamespace(returncode=0, stderr=""), + ] + with pytest.raises(PycharmRunError, match="no such image"): + ensure_sudoers_policy_ownership(config, files, {}) + + assert run.call_args_list[-1].args[0] == ["docker", "volume", "rm", "--force", "capsule-sudoers"] + assert files.sudoers_volume is None + + +def test_macos_sudo_policy_is_written_into_a_volume_by_root( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch, +) -> None: + """On macOS, root writes the sudo policy into a fresh labelled volume through a confined helper container, without a chown.""" + # Docker Desktop shares Mac files as the Mac user: no chown makes a + # bind-mounted policy root's, so the policy lives in a volume. + monkeypatch.setattr(HostOs, "current", classmethod(lambda cls: HostOs.DARWIN)) + config, files = _macos_sudo_files(tmp_path) + + with ( + patch("devcapsule.launch.pycharm._launcher.os.chown") as chown, + patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run, + ): + run.return_value = SimpleNamespace(returncode=0, stderr="", stdout="") + ensure_sudoers_policy_ownership(config, files, {"PATH": "/usr/bin"}) + + chown.assert_not_called() + assert files.sudoers_volume == "capsule-sudoers" + listed, create, write = (call.args[0] for call in run.call_args_list) + assert listed == ["docker", "volume", "ls", "--quiet", "--filter", + "label=devcapsule.purpose=development-sudo-policy"] + assert create == ["docker", "volume", "create", "--label", "devcapsule.purpose=development-sudo-policy", + "capsule-sudoers"] + assert write[:4] == ["docker", "run", "--rm", "-i"] + assert "--platform=linux/amd64" in write + assert ["--network", "none"] == write[write.index("--network") : write.index("--network") + 2] + assert ["--cap-drop", "ALL"] == write[write.index("--cap-drop") : write.index("--cap-drop") + 2] + assert "--cap-add" not in write + assert ["--user", "0:0"] == write[write.index("--user") : write.index("--user") + 2] + assert {"--read-only", "--pull=never", "no-new-privileges"} <= set(write) + assert "type=volume,src=capsule-sudoers,dst=/policy" in write + assert write[-1].startswith("umask 0337;") + assert write[-1].endswith("/policy/policy") + assert run.call_args_list[2].kwargs["input"] == SUDOERS_POLICY + + +def test_no_sudo_launches_keep_full_hardening_on_every_surface( + tmp_path: Path, +) -> None: + """A launch without development sudo keeps no-new-privileges and adds no capability on every surface.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + for plan in (external_runtime_plan(), codium_external_runtime_plan()): + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=plan, + use_image_process=True, + interactive_state_mounts=( + codium_interactive_state_mounts(tmp_path) + if plan.component.id == "codium" + else {} + ), + ), + env, + ) + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + runtime_plan_file=tmp_path / "runtime-plan", + ) + + args = build_docker_args(config, files, env) + + assert "no-new-privileges" in args + assert "SYS_ADMIN" not in args + + +def test_only_an_old_policy_volume_counts_as_abandoned() -> None: + """Only an unused sudo-policy volume older than a day is removed, so a concurrent launch's fresh volume survives.""" + # A concurrent launch's fresh volume is unused until its capsule starts. + old = (datetime.now(timezone.utc) - timedelta(days=2)).isoformat() + fresh = datetime.now(timezone.utc).isoformat() + with patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run: + run.side_effect = [ + SimpleNamespace(returncode=0, stdout="killed-sudoers\nconcurrent-sudoers\n"), + SimpleNamespace(returncode=0, stdout=f"killed-sudoers {old}\nconcurrent-sudoers {fresh}\n"), + SimpleNamespace(returncode=0), + ] + remove_abandoned_sudoers_volumes({}) + + assert run.call_args_list[-1].args[0] == ["docker", "volume", "rm", "killed-sudoers"] + + +def test_persistent_home_defaults_to_checkout_scoped_xdg_data(tmp_path: Path) -> None: + """The persistent home defaults to a checkout-scoped directory under the XDG data home.""" + project = tmp_path / "project" + project.mkdir() + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + docker_mode=DockerMode.none, + ), + base_env(tmp_path), + ) + + project_id = plan_project(project).project_id + assert config.persistent_home == ( + tmp_path / "data" / "devcapsule" / "projects" / "by-path" / project_id / "home" + ).resolve() + + +def test_physical_host_launcher_owns_browser_broker_for_docker_lifetime( + tmp_path: Path, +) -> None: + """A launch on the physical host owns the browser broker for the container's lifetime and removes its socket after.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + + with ( + patch("devcapsule.host_open.in_container", return_value=False), + patch("devcapsule.host_open.HostOs.current", return_value=HostOs.LINUX), + patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), + patch("devcapsule.launch.pycharm._launcher.run_attached") as run, + ): + run.return_value = AttachedResult(0, interrupted=False, stopped=True) + assert run_pycharm( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + docker_mode=DockerMode.none, + enable_host_browser=True, + ), + env, + ) == 0 + + command = run.call_args.args[0] + socket_mount = next( + argument + for argument in command + if argument.startswith("type=bind,src=") + and f"dst={HOST_OPEN_SOCKET_DESTINATION}" in argument + ) + source = Path( + next( + field.removeprefix("src=") + for field in socket_mount.split(",") + if field.startswith("src=") + ) + ) + assert not source.exists() + + +def test_plan_driven_storage_summary_names_the_surface_and_its_slots( + tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """The storage summary of a plan-driven surface names the surface and its slots, not PyCharm's directories.""" + project = tmp_path / "project" + project.mkdir() + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=codium_external_runtime_plan(), + use_image_process=True, + interactive_state_mounts=codium_interactive_state_mounts(tmp_path), + ), + base_env(tmp_path), + ) + + print_storage_summary(config) + + output = capsys.readouterr().err + assert "codium storage:" in output + assert "codium/user-data" in output + assert "PyCharm config" not in output + # The unsandboxed-renderer disclosure (product-owner ruling 2026-09-02) + # keys on the --no-sandbox flag the template carries. + assert "Chromium renderers run unsandboxed" in output + assert "Docker's outer isolation is unchanged" in output + + +def test_plan_driven_surface_mounts_declared_slots_instead_of_pycharm_paths( + tmp_path: Path, +) -> None: + """A plan-driven surface mounts its declared slots and none of PyCharm's state paths.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=codium_external_runtime_plan(), + use_image_process=True, + interactive_state_mounts=codium_interactive_state_mounts(tmp_path), + ), + env, + ) + + assert config.ide_config is None + assert config.plugins is None + assert config.ide_system is None + assert config.ide_log is None + assert config.tool_cache is None + + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + runtime_plan_file=tmp_path / "runtime-plan", + ) + args = build_docker_args(config, files, env) + + assert f"type=bind,src={tmp_path / 'slots' / 'user-data'},dst=/ide-user-data" in args + assert f"type=bind,src={tmp_path / 'slots' / 'extensions'},dst=/ide-extensions" in args + assert ( + f"type=bind,src={tmp_path / 'slots' / 'cache'},dst=/home/devcapsule/.cache" in args + ) + assert not any("dst=/ide-config" in value for value in args) + assert not any("dst=/ide-plugins" in value for value in args) + assert not any("dst=/ide-project-state" in value for value in args) + # Renderer sandboxing is retired (product-owner ruling 2026-09-02): + # codium runs --no-sandbox and keeps the uniform full hardening. + assert "no-new-privileges" in args + assert ["--cap-drop", "ALL"] == args[args.index("--cap-drop") : args.index("--cap-drop") + 2] + assert "--cap-add" not in args + # No PyCharm state directories are invented on the host for this surface. + data_namespace = Path(env["XDG_DATA_HOME"]) / "devcapsule" / "projects" / "by-path" + assert not list(data_namespace.glob("*/components/pycharm")) + + +def test_profile_sets_global_settings_and_plugins_defaults(tmp_path: Path) -> None: + """A profile sets the defaults of the global settings and plugins directories.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_CONFIG_HOME"] = str(tmp_path / "config") + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + profile="codex", + docker_mode=DockerMode.none, + ), + env, + ) + + profile_root = tmp_path / "config" / "docker-pycharm-codex" + assert config.global_settings == (profile_root / "state").resolve() + assert config.plugins == (profile_root / "plugins").resolve() + + +def test_project_state_root_mirrors_project_path_under_root_parent(tmp_path: Path) -> None: + """The project state root mirrors the project's path beneath the root's parent.""" + workspace = tmp_path / "workspace" + project = workspace / "myProjects" / "example" + project.mkdir(parents=True) + + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_state_root=workspace / ".state", + docker_mode=DockerMode.none, + ), + base_env(tmp_path), + ) + + assert config.project_state == (workspace / ".state" / "myProjects" / "example").resolve() + + +@pytest.mark.parametrize("display_transport", ["contained", "host-x11"]) + + +@pytest.mark.parametrize("custom_console_opener", [False, True]) + + +def test_run_pycharm_announces_the_console_url_and_opens_it_only_without_a_desktop( + tmp_path: Path, capsys: pytest.CaptureFixture[str], display_transport: str, custom_console_opener: bool, +) -> None: + """A launch announces the web console's URL and opens it only when there is no desktop to open.""" + + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + display_opener = Mock() + console_opener = Mock() + default_console_opener = Mock() + tokens = ["a" * 48, "b" * 48] + stops: list[Event] = [] + + def ready(port: int, url: str, opener: object, stop: Event, **kwargs: object) -> None: + + stops.append(stop) + assert not stop.is_set() + cast(Callable[[str], None], opener)(url) + + with ( + patch("devcapsule.launch.pycharm._launcher.allocate_loopback_port", side_effect=[41000, 41001]), + patch("devcapsule.launch.pycharm._launcher.new_run_token", side_effect=tokens), + patch("devcapsule.launch.pycharm._launcher.watch_display_ready", side_effect=ready) as watcher, + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + patch("devcapsule.launch.pycharm._launcher.write_xauthority"), + patch("devcapsule.launch.pycharm._launcher.run_attached", return_value=AttachedResult(7, interrupted=False, stopped=True)), + patch("devcapsule.launch.pycharm._launcher.default_opener", return_value=default_console_opener) as default, + ): + assert run_pycharm(PycharmRunOptions( + project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none, + network_mode="bridge", runtime_plan=external_runtime_plan(), use_image_process=True, + display_transport=display_transport, open_display_url=display_opener, + open_console_url=console_opener if custom_console_opener else None, + ), env) == 7 + contained = display_transport == "contained" + console_port, token = (41001, tokens[1]) if contained else (41000, tokens[0]) + url = f"http://127.0.0.1:{console_port}/?token={token}" + output = capsys.readouterr().err + assert f"Web console: {url}\n" in output + assert watcher.call_count == (2 if contained else 1) + assert watcher.call_args.args[:2] == (console_port, url) + assert watcher.call_args.kwargs == {"label": "Web console"} + assert all(stop is stops[0] and stop.is_set() for stop in stops) + assert list((tmp_path / "runtime").iterdir()) == [] + if contained: + display_opener.assert_called_once_with(display_url(41000, tokens[0])) + else: + display_opener.assert_not_called() + if custom_console_opener: + console_opener.assert_called_once_with(url) + default.assert_not_called() + elif contained: + assert f"Web console is ready; open it in a browser: {url}" in output + default.assert_not_called() + else: + assert default.call_args.kwargs == {"label": "Web console"} + default_console_opener.assert_called_once_with(url) + + +def test_run_pycharm_announces_the_display_url_and_opens_it_when_ready( + tmp_path: Path, capsys: pytest.CaptureFixture[str] +) -> None: + """A contained-display launch announces the desktop's token URL and opens it once the display is ready.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env.pop("DISPLAY") + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + opened: list[str] = [] + listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + listener.bind(("127.0.0.1", 0)) + listener.listen(1) + port = listener.getsockname()[1] + + def fake_docker_run(command: list[str], env: dict[str, str], **_kwargs: object) -> AttachedResult: + # "The capsule is up": the published port answers, so the watcher opens + # the URL; then docker run returns as the session ends. + deadline = time.monotonic() + 10 + while not opened and time.monotonic() < deadline: + time.sleep(0.05) + return AttachedResult(0, interrupted=False, stopped=True) + + with ( + patch("devcapsule.launch.pycharm._launcher.allocate_loopback_port", side_effect=[port, 1]), + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + patch("devcapsule.launch.pycharm._launcher.run_attached", side_effect=fake_docker_run), + patch("devcapsule.launch.pycharm._launcher.current_host_user", return_value=HostUser(1000, 1000, "dev", "dev")), + ): + try: + exit_code = run_pycharm( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + display_transport="contained", + open_display_url=opened.append, + ), + env, + ) + finally: + listener.close() + assert exit_code == 0 + (url,) = opened + assert url.startswith(f"http://127.0.0.1:{port}/vnc.html?autoconnect=1&resize=remote&path=websockify%3Ftoken%3D") + captured = capsys.readouterr() + assert f"Contained display: {url}" in captured.err + + +def test_selected_codex_state_and_explicit_secret_are_delivered(tmp_path: Path) -> None: + """A selected Codex state slot and an explicitly bound secret reach the capsule, the secret's value never in Docker's arguments.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + env["OPENAI_API_KEY"] = "sk-test-never-in-docker-args" + runtime = RuntimePlan.for_component( + pycharm_runtime_template(), + project_path="/workspace/project", + home="/home/devcapsule", + identity=Identity(1000, 1000, "developer"), + ancillary_templates=(codex_runtime_template(),), + ) + state = tmp_path / "codex-home" + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=runtime, + use_image_process=True, + additional_state_mounts={"codex/home": (state, "/home/devcapsule/.codex")}, + secret_environment=("OPENAI_API_KEY",), + ), + env, + ) + with ( + patch("devcapsule.launch.pycharm._launcher.write_xauthority"), + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + ): + files = prepare_temp_runtime_files(config, env) + try: + args = build_docker_args(config, files, env) + finally: + cleanup_temp_runtime_files(files) + + assert "CODEX_HOME=/home/devcapsule/.codex" in args + assert "OPENAI_API_KEY" in args + assert "sk-test-never-in-docker-args" not in args + assert f"type=bind,src={state.resolve()},dst=/home/devcapsule/.codex" in args + + +def test_session_log_masks_inline_environment_values_of_extra_arguments(tmp_path, capsys): + """The launcher log keeps the docker run line but never a saved or one-off inline env value.""" + + config, env = session_config(tmp_path) + config = replace(config, extra_docker_args=[ + "--gpus", "all", "--env", "APP_SECRET=saved-sentinel-value", "-eOTHER=one-off-sentinel", "--env", "PLAIN", + ]) + commands: list[list[str]] = [] + + class Client: + def __init__(self, command, env): + commands.append(list(command)) + + def wait(self, timeout=None): + return 0 + + try: + with ( + patch.object(launcher, "build_run_config", return_value=config), + patch.object(launcher, "write_xauthority"), + patch.object(launcher, "write_user_files"), + patch.object(launcher, "watch_display_ready"), + patch.object(launcher.subprocess, "Popen", Client), + ): + assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == 0 + # Docker receives the literal arguments, in order, before the image. + [command] = commands + image = command.index(config.image) + assert command[image - len(config.extra_docker_args):image] == config.extra_docker_args + assert config.session_log_directory is not None + record = (config.session_log_directory / "devcapsule-launcher.log").read_text() + assert "docker run: " in record + assert "--gpus all" in record and "APP_SECRET=" in record and "-eOTHER=" in record + assert "--env PLAIN" in record + assert "saved-sentinel-value" not in record and "one-off-sentinel" not in record + assert "saved-sentinel-value" not in capsys.readouterr().err + finally: + logs.reset() + + +def test_sudo_banner_is_printed_after_complete_policy_preparation( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """The development-sudo banner is printed only after the policy is prepared and mounted.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + + with ( + patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), + patch( + "devcapsule.launch.pycharm._launcher.ensure_sudoers_policy_ownership", + side_effect=lambda _config, files, _env: setattr(files, "sudoers_volume", "capsule-sudoers"), + ) as ensure, + patch("devcapsule.launch.pycharm._launcher.remove_sudoers_policy_volume") as remove, + patch("devcapsule.launch.pycharm._launcher.run_attached") as run, + ): + run.return_value = AttachedResult(0, interrupted=False, stopped=True) + assert ( + run_pycharm( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + image="devcapsule-local-pycharm:canonical", + docker_mode=DockerMode.none, + network_mode="bridge", + enable_sudo=True, + ), + env, + ) + == 0 + ) + + ensure.assert_called_once() + assert "DEVELOPMENT SUDO IS ENABLED" in capsys.readouterr().err + command = run.call_args.args[0] + assert any(SUDOERS_POLICY_PATH in argument and "src=capsule-sudoers" in argument for argument in command) + # The policy volume outlives nothing but the session. + assert remove.call_args.args[0] == "capsule-sudoers" + + +def test_sudo_launches_grant_no_capabilities_beyond_dockers_default( + tmp_path: Path, +) -> None: + """Regression for the 2026-09-02 sudo-sandbox bug: the sudo posture is + Docker's default capability set plus the sudo group — no per-surface + capability grants, and no setuid-helper posture left to crash the zygote. + """ + + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=codium_external_runtime_plan(), + use_image_process=True, + interactive_state_mounts=codium_interactive_state_mounts(tmp_path), + enable_sudo=True, + ), + env, + ) + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + runtime_plan_file=tmp_path / "runtime-plan", + shadow_file=tmp_path / "shadow", + sudoers_file=tmp_path / "sudoers", + ) + + args = build_docker_args(config, files, env) + + assert "--group-add" in args + assert "--cap-add" not in args + assert "--cap-drop" not in args + assert "no-new-privileges" not in args + + +def test_sudo_policy_preparation_failure_cleans_files_without_enabled_banner( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """A failure to prepare the sudo policy removes its files and prints no enabled banner.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + + with ( + patch("devcapsule.launch.pycharm._launcher.shutil.which", return_value=None), + patch( + "devcapsule.launch.pycharm._launcher.ensure_sudoers_policy_ownership", + side_effect=PycharmRunError("sudo policy failed"), + ), + patch( + "devcapsule.launch.pycharm._launcher.cleanup_temp_runtime_files", + wraps=cleanup_temp_runtime_files, + ) as cleanup, + ): + with pytest.raises(PycharmRunError, match="sudo policy failed"): + run_pycharm( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + image="devcapsule-local-pycharm:canonical", + docker_mode=DockerMode.none, + network_mode="bridge", + enable_sudo=True, + ), + env, + ) + + files = cleanup.call_args.args[0] + assert files.sudoers_file is not None + assert files.sudoers_directory is not None + assert all( + path is None or not path.exists() + for path in ( + files.xauth_file, + files.passwd_file, + files.group_file, + files.shadow_file, + files.sudoers_file, + ) + ) + assert not files.sudoers_directory.exists() + assert "DEVELOPMENT SUDO IS ENABLED" not in capsys.readouterr().err + + +def test_sudoers_ownership_helper_failure_is_actionable(tmp_path: Path) -> None: + """A failed ownership helper reports Docker's own error.""" + policy = tmp_path / "sudoers" + policy.write_text(SUDOERS_POLICY, encoding="utf-8") + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + sudoers_file=policy, + ) + config = cast( + PycharmRunConfig, + SimpleNamespace(enable_sudo=True, image="devcapsule-local-pycharm:canonical", + capsule_platform=Platform.LINUX_AMD64, daemon_architecture="amd64"), + ) + + with ( + patch("devcapsule.launch.pycharm._launcher.os.chown", side_effect=PermissionError), + patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run, + ): + run.return_value = SimpleNamespace(returncode=1, stderr="daemon denied helper") + with pytest.raises(PycharmRunError, match="daemon denied helper"): + ensure_sudoers_policy_ownership(config, files, {}) + + +def test_sudoers_ownership_helper_is_constrained(tmp_path: Path) -> None: + """The helper that makes the sudo policy root's runs without network or capabilities beyond CHOWN, read-only and never pulling.""" + policy = tmp_path / "sudoers" + policy.write_text(SUDOERS_POLICY, encoding="utf-8") + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + sudoers_file=policy, + ) + config = cast( + PycharmRunConfig, + SimpleNamespace(enable_sudo=True, image="devcapsule-local-pycharm:canonical", + capsule_platform=Platform.LINUX_AMD64, daemon_architecture="amd64"), + ) + + with ( + patch("devcapsule.launch.pycharm._launcher.os.chown", side_effect=PermissionError), + patch("devcapsule.launch.pycharm._launcher.file_owner_uid", return_value=0), + patch("devcapsule.launch.pycharm._launcher.subprocess.run") as run, + ): + run.return_value = SimpleNamespace(returncode=0, stderr="") + ensure_sudoers_policy_ownership(config, files, {"PATH": "/usr/bin"}) + + command = run.call_args.args[0] + assert command[:3] == ["docker", "run", "--rm"] + assert ["--network", "none"] == command[command.index("--network") : command.index("--network") + 2] + assert ["--cap-drop", "ALL"] == command[command.index("--cap-drop") : command.index("--cap-drop") + 2] + assert ["--cap-add", "CHOWN"] == command[command.index("--cap-add") : command.index("--cap-add") + 2] + assert "no-new-privileges" in command + assert "--read-only" in command + assert "--pull=never" in command + assert "0:0" == command[-2] + + +def test_the_launcher_records_the_sessions_end_in_its_manifest(tmp_path: Path) -> None: + """A completed launch records the session's end and exit code in its manifest.""" + + + assert "130" in (mark_session_ended.__doc__ or "") and "None" not in (mark_session_ended.__doc__ or "") + config, env = session_config(tmp_path) + assert config.session_log_directory is not None + try: + with ( + patch.object(launcher, "build_run_config", return_value=config), + patch.object(launcher, "write_xauthority"), + patch.object(launcher, "write_user_files"), + patch.object(launcher, "watch_display_ready"), + patch.object(launcher, "run_attached", return_value=AttachedResult(7, interrupted=False, stopped=True)), + ): + assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == 7 + manifest = json.loads((config.session_log_directory / "session.json").read_text(encoding="utf-8")) + assert manifest["session"] == config.session_id and manifest["exit-code"] == 7 + assert manifest["ended-at"].endswith("Z") and logs.session_ended(config.session_log_directory) + with ( + patch.object(launcher, "build_run_config", return_value=config), + patch.object(launcher, "write_xauthority"), + patch.object(launcher, "write_user_files"), + patch.object(launcher, "watch_display_ready"), + patch.object(launcher, "run_attached", return_value=AttachedResult(143, interrupted=True, stopped=True)), + ): + assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == launcher.INTERRUPTED_EXIT_CODE + manifest = json.loads((config.session_log_directory / "session.json").read_text(encoding="utf-8")) + assert manifest["exit-code"] == launcher.INTERRUPTED_EXIT_CODE and manifest["ended-at"] + finally: + logs.reset() + + +def test_unauthorized_runtime_effects_keep_safe_docker_defaults(tmp_path: Path) -> None: + """Without authorizations a launch keeps bridge networking, no sudo policy and no Docker access.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["XDG_RUNTIME_DIR"] = str(tmp_path / "runtime") + host_user = HostUser(uid=1000, gid=1000, name="developer", group_name="developer") + + with patch( + "devcapsule.launch.pycharm._launcher.current_host_user", + return_value=host_user, + ): + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="bridge", + runtime_plan=external_runtime_plan(), + use_image_process=True, + ), + env, + ) + with patch("devcapsule.launch.pycharm._launcher.write_xauthority"): + files = prepare_temp_runtime_files(config, env) + try: + assert files.sudoers_file is None + args = build_docker_args(config, files, env) + finally: + cleanup_temp_runtime_files(files) + + assert ["--network", "bridge"] == args[args.index("--network") : args.index("--network") + 2] + assert "ENABLE_SUDO=0" in args + assert not any(SUDOERS_POLICY_PATH in argument for argument in args) + assert "DOCKER_HOST=unix:///run/host-docker.sock" not in args + assert not any("dst=/run/host-docker.sock" in argument for argument in args) + assert "--memory" not in args + assert ["--cap-drop", "ALL"] == args[args.index("--cap-drop") : args.index("--cap-drop") + 2] + assert "no-new-privileges" in args + assert "--read-only" in args + assert "--privileged" not in args + + +def test_unusable_session_directory_does_not_add_a_mount_or_stop_launch(tmp_path, capsys): + """A session log directory that cannot be used adds no mount and does not stop the launch.""" + + blocked = tmp_path / "blocked-state" + blocked.write_text("not a directory") + # Existing IDE storage still has to be writable; this regression isolates + # the new session directory from that pre-existing launch requirement. + with patch.object(launcher, "session_logs_root", return_value=blocked): + config, env = session_config(tmp_path) + assert config.session_log_directory is None + assert config.runtime_plan is not None and config.runtime_plan.logs is None + try: + with ( + patch.object(launcher, "build_run_config", return_value=config), + patch.object(launcher, "write_xauthority"), + patch.object(launcher, "write_user_files"), + patch.object(launcher, "watch_display_ready"), + patch.object(launcher, "run_attached", return_value=AttachedResult(7, interrupted=False, stopped=True)) as docker, + ): + assert run_pycharm(PycharmRunOptions(project=config.project, capsule_platform=Platform.LINUX_AMD64), env) == 7 + command = docker.call_args.args[0] + assert not any(SESSION_LOG_DESTINATION in arg for arg in command) + assert "cannot use session logs" in capsys.readouterr().err + finally: + logs.reset() + + +def test_web_console_is_absent_without_a_runtime_plan(tmp_path: Path) -> None: + """Without a runtime plan there is no web console: no port, token, file or disclosure.""" + project = tmp_path / "project" + project.mkdir() + config = build_run_config( + PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, project_mount="/workspace/project", docker_mode=DockerMode.none), + base_env(tmp_path), + ) + assert config.console_host_port is None and config.console_token == "" + assert console_disclosure(config) == "" + with ( + patch("devcapsule.launch.pycharm._launcher.write_xauthority"), + patch("devcapsule.launch.pycharm._launcher.write_user_files"), + ): + files = prepare_temp_runtime_files(config, base_env(tmp_path)) + try: + assert files.console_token_file is None + assert "devcapsule-console-token" not in " ".join(build_docker_args(config, files, base_env(tmp_path))) + finally: + cleanup_temp_runtime_files(files) + + +def test_launcher_environment_cannot_override_explicit_sudo_denial(tmp_path: Path) -> None: + """A project launch"s explicit sudo denial wins over a legacy PYCHARM_ENABLE_SUDO in the environment.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["PYCHARM_ENABLE_SUDO"] = "1" + # ProjectRunCommand passes enable_sudo=False for the persisted denial. + # Check the NEXT boundary, rather than replacing it with a launch mock. + config = build_run_config( + PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, docker_mode=DockerMode.none, enable_sudo=False), env, + ) + assert config.enable_sudo is False + + +@pytest.mark.parametrize("value", [False, True, None]) + + +def test_explicit_sudo_decision_overrides_legacy_environment(value: bool | None, tmp_path: Path) -> None: + """An explicit sudo decision wins over the legacy environment; without one, the environment"s applies.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + env["PYCHARM_ENABLE_SUDO"] = "1" + config = build_run_config(PycharmRunOptions(project=project, capsule_platform=Platform.LINUX_AMD64, docker_mode=DockerMode.none, + enable_sudo=value), env) + assert config.enable_sudo is (True if value is None else value) + + +def test_project_launch_cannot_inherit_unbound_paths_or_credentials(tmp_path: Path) -> None: + """A project launch inherits no state path, credential, sudo, Docker or network setting from the legacy environment.""" + project = tmp_path / "project" + project.mkdir() + env = base_env(tmp_path) + unbound = tmp_path / "unbound" + env.update({ + "DEVCAPSULE_HOME_DIR": str(unbound), "PYCHARM_IDE_CONFIG_DIR": str(unbound), + "PYCHARM_PLUGIN_DIR": str(unbound), "GITHUB_TOKEN_FILE": str(unbound / "secret"), + "PYCHARM_GIT_TOKEN_ENV": "UNBOUND_TOKEN", "PYCHARM_ENABLE_SUDO": "invalid", + "DOCKER_MODE": "invalid", + }) + config = build_run_config(PycharmRunOptions( + project=project, capsule_platform=Platform.LINUX_AMD64, inherit_legacy_configuration=False, + docker_mode=DockerMode.none, enable_sudo=False, network_mode="bridge", + ), env) + assert config.persistent_home != unbound and config.ide_config != unbound and config.plugins != unbound + assert config.git_token_file is None and config.git_token_env == "" + assert config.enable_sudo is False and config.docker_mode == "none" and config.network_mode == "bridge" + assert not unbound.exists() diff --git a/devcapsule-src/tests/unit/devcapsule/test_environment_realization.py b/devcapsule-src/tests/unit/devcapsule/test_environment_realization.py new file mode 100644 index 00000000..5c51c45a --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/test_environment_realization.py @@ -0,0 +1,73 @@ +"""Unit tests of ``devcapsule/environment_realization.py``: obtaining the base and materializing a checkout's environment. + +Docker, the materializer and the runtime choice are fakes. +""" +from __future__ import annotations + +from unittest.mock import Mock, patch + +import pytest + +from devcapsule.environment_realization import ensure_local_image, realize_environment +from devcapsule.platforms import Platform +from devcapsule.runtime_command import RuntimeCommand +from tests.test_environment_realization import ( + BASE_REFERENCE, + CANONICAL_IMAGE, + base_image, + completed_image, + resolved_project, +) + + +@pytest.mark.parametrize("already_local", [True, False]) +def test_ensure_local_image_pulls_only_when_missing(already_local: bool) -> None: + """The base is pulled only when the local store lacks it, with no platform for a native daemon.""" + details = base_image() + with ( + patch( + "devcapsule.environment_realization.docker.image.exists", + return_value=already_local, + ), + patch("devcapsule.environment_realization.docker.image.pull") as pull, + patch( + "devcapsule.environment_realization.required_local_image", + return_value=details, + ) as require, + ): + assert ensure_local_image(BASE_REFERENCE, Platform.LINUX_AMD64) == details + + if already_local: + pull.assert_not_called() + else: + pull.assert_called_once_with(BASE_REFERENCE, platform=None) + require.assert_called_once_with(BASE_REFERENCE) + + +@pytest.mark.parametrize("daemon,expected", [("arm64", "linux/amd64"), ("amd64", None)]) +def test_a_daemon_of_another_architecture_pulls_the_lock_platform(daemon: str, expected: str | None) -> None: + """A daemon of another architecture than the lock's platform is told the lock's platform when pulling.""" + # Docker Desktop on Apple Silicon is an arm64 daemon; a linux-amd64 lock's + # base must not resolve to its arm64 variant. + with ( + patch("devcapsule.environment_realization.daemon_architecture", return_value=daemon), + patch("devcapsule.environment_realization.docker.image.exists", return_value=False), + patch("devcapsule.environment_realization.docker.image.pull") as pull, + patch("devcapsule.environment_realization.required_local_image", return_value=base_image()), + ): + ensure_local_image(BASE_REFERENCE, Platform.LINUX_AMD64) + pull.assert_called_once_with(BASE_REFERENCE, platform=expected) + + +@pytest.mark.parametrize("command", [None, "devcapsule", "devcapsule0"]) +def test_realizer_uses_resolved_shipped_command_choice(tmp_path, monkeypatch, command): + """Realization delivers the runtime command the resolution chose, the standard one by default.""" + selected = resolved_project(tmp_path) + if command is not None: + selected.resolution["runtime"]["devcapsule-command"] = command + materialize = Mock(return_value=(CANONICAL_IMAGE, True)) + monkeypatch.setattr("devcapsule.environment_realization.ensure_materialized_surface", materialize) + monkeypatch.setattr("devcapsule.environment_realization.runtime_artifact", lambda _platform: tmp_path / "runtime.pex") + realize_environment(selected, root=tmp_path / "cache", obtain_image=lambda _ref: base_image(), + require_image=lambda _ref: completed_image(), build=Mock(), inspect_image=Mock()) + assert materialize.call_args.kwargs["runtime_command"] is RuntimeCommand(command or "devcapsule") diff --git a/devcapsule-src/tests/unit/devcapsule/test_host_open.py b/devcapsule-src/tests/unit/devcapsule/test_host_open.py new file mode 100644 index 00000000..0b4df501 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/test_host_open.py @@ -0,0 +1,26 @@ +"""Unit tests of ``devcapsule/host_open.py``: the host-browser bridge on a host that cannot carry it. + +The host is named by patching ``HostOs.current``; nothing binds a socket. +""" +from __future__ import annotations + +from pathlib import Path +from unittest.mock import patch + +import pytest + +from devcapsule.host_open import host_open_bridge +from devcapsule.platforms import HostOs + + +def test_macos_host_bridge_is_unavailable_with_a_warning(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: + """On a host whose Unix sockets Docker cannot bind into a capsule, the bridge yields no socket, + warns, and creates nothing: the optional integration degrades instead of failing the launch.""" + with ( + patch("devcapsule.host_open.in_container", return_value=False), + patch("devcapsule.host_open.HostOs.current", return_value=HostOs.DARWIN), + host_open_bridge({"XDG_RUNTIME_DIR": str(tmp_path)}, enabled=True) as path, + ): + assert path is None + assert "host-browser is unavailable on macOS" in capsys.readouterr().err + assert list(tmp_path.iterdir()) == [] diff --git a/devcapsule-src/tests/unit/devcapsule/test_platforms.py b/devcapsule-src/tests/unit/devcapsule/test_platforms.py new file mode 100644 index 00000000..ecc872bc --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/test_platforms.py @@ -0,0 +1,99 @@ +"""Unit tests of ``devcapsule/platforms.py``: the capsule platforms, the host operating systems, and what each host runs. + +A test of a particular host names it with ``on_host`` and carries ``own_host_patches``. +""" +from __future__ import annotations + +import pytest + +from devcapsule import platforms +from devcapsule.platforms import HostOs, Platform, UnsupportedPlatformError + + +def on_host(monkeypatch: pytest.MonkeyPatch, system: str, machine: str) -> None: + """Make this host the one ``platform.system()`` and ``platform.machine()`` would name.""" + monkeypatch.setattr(platforms.host_platform, "system", lambda: system) + monkeypatch.setattr(platforms.host_platform, "machine", lambda: machine) + + +def test_platform_values_are_the_wire_format() -> None: + """Each platform's value is its wire format, and the member formats as that value in a lock's name.""" + assert Platform.LINUX_AMD64.value == "linux-amd64" + # StrEnum: the member formats as its value wherever locks and filenames + # interpolate it. + assert f"devcapsule.{Platform.LINUX_AMD64}.lock" == "devcapsule.linux-amd64.lock" + assert Platform.LINUX_ARM64.value == "linux-arm64" + + +@pytest.mark.own_host_patches +@pytest.mark.parametrize("system,machine,runnable,created", [ + ("Linux", "x86_64", ("linux-amd64",), "linux-amd64"), + ("Linux", "aarch64", ("linux-arm64",), "linux-arm64"), + ("Darwin", "x86_64", ("linux-amd64",), "linux-amd64"), + ("Darwin", "arm64", ("linux-arm64", "linux-amd64"), "linux-arm64"), +]) +def test_each_host_runs_its_platforms_and_creates_projects_for_one(monkeypatch, system, machine, runnable, created) -> None: + """Each supported host runs its capsule platforms, native first, and creates new projects for its native one.""" + # A Mac runs Linux capsules. Apple Silicon runs linux-arm64 natively and + # linux-amd64 under emulation; a new project defaults to the native one. + on_host(monkeypatch, system, machine) + assert tuple(str(platform) for platform in Platform.runnable()) == runnable + assert Platform.current() is Platform.parse(created) + + +def test_emulation_is_the_daemons() -> None: + """A daemon of another architecture is told the platform, as an option or a value; a native daemon is told nothing.""" + # Docker Desktop on Apple Silicon is an arm64 daemon, whatever runs the launcher. + assert Platform.LINUX_AMD64.emulated_docker_platform("arm64") == "linux/amd64" + assert Platform.LINUX_ARM64.emulated_docker_platform("arm64") is None + assert Platform.LINUX_AMD64.docker_platform_args("arm64") == ["--platform=linux/amd64"] + assert Platform.LINUX_AMD64.docker_platform_args("amd64") == [] + assert Platform.LINUX_ARM64.docker_platform_args("arm64") == [] + + +@pytest.mark.own_host_patches +@pytest.mark.parametrize("system,machine,message", [ + ("Linux", "riscv64", "supported platforms: linux-amd64, linux-arm64"), + ("Darwin", "ppc", "supported platforms: linux-amd64, linux-arm64"), + ("Windows", "AMD64", "supported: linux, darwin"), +]) +def test_current_refuses_a_host_without_a_capsule_platform(monkeypatch, system, machine, message) -> None: + """A host with no capsule platform is refused, naming the supported ones.""" + on_host(monkeypatch, system, machine) + with pytest.raises(UnsupportedPlatformError, match=message): + Platform.current() + + +def test_an_elf_machine_names_its_platform() -> None: + """An ELF machine value names the platform its executables run on, and another architecture none.""" + assert Platform.of_elf_machine(0x3E) is Platform.LINUX_AMD64 + assert Platform.of_elf_machine(0xB7) is Platform.LINUX_ARM64 + assert Platform.of_elf_machine(0xF3) is None + + +def test_docker_desktop_facts_are_the_macos_hosts() -> None: + """Docker Desktop's conventions are the macOS host's properties, and none of them the Linux host's.""" + assert HostOs.DARWIN.docker_socket_gid == 0 and HostOs.LINUX.docker_socket_gid is None + assert not HostOs.DARWIN.host_network_reachable and HostOs.LINUX.host_network_reachable + assert not HostOs.DARWIN.binds_unix_sockets and HostOs.LINUX.binds_unix_sockets + assert not HostOs.DARWIN.chown_reaches_bind_mounts and HostOs.LINUX.chown_reaches_bind_mounts + + +def test_docker_spelling_and_architecture() -> None: + """Each platform has Docker's spelling of it and its CPU architecture.""" + assert Platform.LINUX_AMD64.docker_platform == "linux/amd64" + assert Platform.LINUX_AMD64.architecture == "amd64" + assert Platform.LINUX_ARM64.docker_platform == "linux/arm64" + assert Platform.LINUX_ARM64.architecture == "arm64" + + +@pytest.mark.own_host_patches +def test_host_os_names_linux_and_macos_only(monkeypatch) -> None: + """The host operating system is Linux or macOS; any other host is refused.""" + on_host(monkeypatch, "Darwin", "arm64") + assert HostOs.current() is HostOs.DARWIN + on_host(monkeypatch, "Linux", "x86_64") + assert HostOs.current() is HostOs.LINUX + on_host(monkeypatch, "Windows", "AMD64") + with pytest.raises(UnsupportedPlatformError, match="'windows'"): + HostOs.current() diff --git a/devcapsule-src/tests/unit/devcapsule/test_recursive_successor_plan.py b/devcapsule-src/tests/unit/devcapsule/test_recursive_successor_plan.py new file mode 100644 index 00000000..61da4500 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/test_recursive_successor_plan.py @@ -0,0 +1,137 @@ +"""Unit tests of ``devcapsule/recursive_successor_plan.py``: the expected plan of a recursive successor launch. + +The Docker arguments come from the real launcher's planning; inspections are constructed. +""" +from __future__ import annotations + +from pathlib import Path +import socket + +import pytest + +from devcapsule.components.pycharm import runtime_template as pycharm_runtime_template +from devcapsule.container_runtime.contract import Identity, RuntimePlan +from devcapsule.host_open import HOST_OPEN_BROWSER, HOST_OPEN_SOCKET_DESTINATION, HOST_OPEN_SOCKET_ENV +from devcapsule.launch.pycharm import DockerMode, PycharmRunOptions +from devcapsule.launch.pycharm._launcher import TempRuntimeFiles, build_docker_args, build_run_config +from devcapsule.materialization import RUNTIME_PLAN_PATH +from devcapsule.platforms import Platform +from devcapsule.recursive_successor_plan import ExpectedSuccessorPlan, SuccessorPlanError, compare_inspection +from tests.test_recursive_successor_plan import ( + IMAGE_IDENTITY, + IMAGE_LABELS, + IMAGE_REFERENCE, + RUNTIME_PLAN_DIGEST, + build_plan, + docker_args, + matching_inspection, +) + + +@pytest.mark.parametrize("enable_sudo", [False, True]) +def test_plan_models_every_flag_the_real_launcher_emits(tmp_path: Path, enable_sudo: bool) -> None: + """The plan models every flag the launcher emits, since a launch fails closed on an unmodelled one.""" + + project = tmp_path / "project" + project.mkdir() + env = { + "DISPLAY": ":1", + "HOME": str(tmp_path / "home"), + "XDG_DATA_HOME": str(tmp_path / "data"), + "PYCHARM_GIT_IDENTITY_FROM_HOST": "0", + } + browser_socket = tmp_path / "host-open.sock" + with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as broker: + broker.bind(str(browser_socket)) + env[HOST_OPEN_SOCKET_ENV] = str(browser_socket) + config = build_run_config( + PycharmRunOptions( + project=project, + capsule_platform=Platform.LINUX_AMD64, + project_mount="/workspace/project", + docker_mode=DockerMode.none, + network_mode="host", + memory_limit_bytes=8589934592, + enable_sudo=enable_sudo, + debug_native=True, + runtime_plan=RuntimePlan.for_component( + pycharm_runtime_template(), + project_path="/workspace/project", + home="/home/devcapsule", + identity=Identity(1000, 1000, "developer"), + ), + use_image_process=True, + additional_environment={ + "DEVCAPSULE_RECURSIVE_E2E": "1", + "DEVCAPSULE_RUN_ID": "b2093d85912fa34ac1324e1da26a9dcd", + }, + enable_host_browser=True, + extra_docker_args=[ + "--pull=never", + "--label", + "devcapsule.e2e.managed=true", + "--label", + "devcapsule.e2e.role=successor", + ], + ), + env, + ) + files = TempRuntimeFiles( + xauth_file=tmp_path / "xauth", + passwd_file=tmp_path / "passwd", + group_file=tmp_path / "group", + console_token_file=tmp_path / "console-token", + shadow_file=tmp_path / "shadow", + sudoers_file=tmp_path / "sudoers", + runtime_plan_file=tmp_path / "runtime-plan", + ) + args = build_docker_args(config, files, env) + + plan = ExpectedSuccessorPlan.from_docker_args( + args, + image_reference=IMAGE_REFERENCE, + image_identity=IMAGE_IDENTITY, + image_labels=IMAGE_LABELS, + runtime_plan_destination=RUNTIME_PLAN_PATH, + runtime_plan_digest=RUNTIME_PLAN_DIGEST, + ) + + assert plan.network_mode == "host" + assert plan.memory_limit_bytes == 8589934592 + assert plan.environment["DEVCAPSULE_RECURSIVE_E2E"] == "1" + assert plan.environment["DEVCAPSULE_RUN_ID"] == "b2093d85912fa34ac1324e1da26a9dcd" + assert plan.environment["BROWSER"] == HOST_OPEN_BROWSER + assert plan.environment[HOST_OPEN_SOCKET_ENV] == str(HOST_OPEN_SOCKET_DESTINATION) + browser_mount = next( + mount for mount in plan.mounts if mount.destination == str(HOST_OPEN_SOCKET_DESTINATION) + ) + assert browser_mount.source == str(browser_socket) + assert browser_mount.read_only is True + assert plan.labels["devcapsule.e2e.role"] == "successor" + assert "SYS_PTRACE" in plan.cap_add + assert RUNTIME_PLAN_PATH in {mount.destination for mount in plan.mounts} + assert plan.read_only_root is not enable_sudo + if enable_sudo: + assert plan.group_add and plan.cap_drop == () + else: + assert plan.cap_drop == ("ALL",) + assert "no-new-privileges" in plan.security_opt + + +@pytest.mark.parametrize("spelling", [["--platform=linux/amd64"], ["--platform", "linux/amd64"]]) +def test_an_emulated_launch_plans_its_platform_and_the_successor_must_run_it(spelling: list[str]) -> None: + """An emulated launch's plan names its platform in either Docker spelling, keeps it when retained, and refuses a successor that ran another.""" + # The launcher names the platform when the daemon emulates it; both of + # Docker's spellings parse, and a successor that ran another is refused. + plan = build_plan([*docker_args(), *spelling]) + assert plan.platform == "linux/amd64" + inspection = matching_inspection(plan) + inspection["ImageManifestDescriptor"] = {"platform": {"os": "linux", "architecture": "amd64"}} + compare_inspection(plan, inspection) + inspection["ImageManifestDescriptor"] = {"platform": {"os": "linux", "architecture": "arm64"}} + with pytest.raises(SuccessorPlanError, match="successor platform does not match the planned --platform"): + compare_inspection(plan, inspection) + # The independent inspection reloads the retained plan, platform included; + # a native plan's retained form is unchanged. + assert ExpectedSuccessorPlan.from_mapping(plan.to_mapping(show_host_paths=True)) == plan + assert "platform" not in build_plan(docker_args()).to_mapping() diff --git a/devcapsule-src/tests/unit/devcapsule/test_resolution_matrix.py b/devcapsule-src/tests/unit/devcapsule/test_resolution_matrix.py new file mode 100644 index 00000000..f7426f68 --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/test_resolution_matrix.py @@ -0,0 +1,34 @@ +"""Unit tests of ``devcapsule/resolution_matrix.py``: the linux-arm64 matrix beside the linux-amd64 one. + +The matrices are the launcher's embedded data; each test asks one of them one question. +""" +from __future__ import annotations + +import pytest + +from devcapsule.components.linux_arm64_pins import COMPONENT_LOCK_TABLES +from devcapsule.platforms import Platform +from devcapsule.resolution_matrix import MATRICES + +AMD64 = MATRICES[Platform.LINUX_AMD64] +ARM64 = MATRICES[Platform.LINUX_ARM64] + + +def test_the_arm64_matrix_serves_each_arm64_pin_at_its_version() -> None: + """The arm64 matrix's pin for each component version is that version's arm64 table.""" + assert all(ARM64.component_pin(component, version) == table + for (component, version), table in COMPONENT_LOCK_TABLES.items()) + + +def test_the_arm64_base_is_its_own_recipe_11_pin() -> None: + """The arm64 matrix pins one recipe-11 base of DevCapsule's repository, not an amd64 one.""" + (base,) = ARM64.base_images() + assert base.contract.recipe == 11 + assert base.reference.startswith("docker.io/mycodespaceai/devcapsule-base@sha256:") + assert base.reference not in {image.reference for image in AMD64.base_images()} + + +@pytest.mark.parametrize("ide", ["frontend-ide", "vscode-ide", "eclipse-ide", "python-ide", "java-ide"]) +def test_each_ide_smoked_natively_on_arm64_resolves_verified(ide: str) -> None: + """An IDE with a native arm64 smoke resolves on the arm64 matrix without an unverified combination.""" + assert ARM64.resolve([ide]).unverified == () diff --git a/devcapsule-src/tests/unit/devcapsule/test_runtime_artifact.py b/devcapsule-src/tests/unit/devcapsule/test_runtime_artifact.py new file mode 100644 index 00000000..b512e8fe --- /dev/null +++ b/devcapsule-src/tests/unit/devcapsule/test_runtime_artifact.py @@ -0,0 +1,151 @@ +"""Unit tests of ``devcapsule/runtime_artifact.py``: which DevCapsule executable a launch delivers into a capsule. + +The executables are PEX fixtures under ``tmp_path``, some behind a native header; the running +launcher is the fixtures' build, and a test of another host names it. +""" +from __future__ import annotations + +from pathlib import Path + +import pytest + +from devcapsule.build_info import BuildInfo +from devcapsule.compat import CliError +from devcapsule.platforms import HostOs, Platform +from devcapsule.runtime_artifact import executable_platform, runtime_artifact +from tests.test_base_image import pex_fixture +from tests.unit.devcapsule.test_platforms import on_host + + +AMD64 = Platform.LINUX_AMD64 + +ARM64 = Platform.LINUX_ARM64 + +MACH_O_ARM64 = b"\xcf\xfa\xed\xfe" + bytes(16) + +ELF_AMD64 = b"\x7fELF\x02\x01\x01" + bytes(11) + (0x3E).to_bytes(2, "little") + +ELF_ARM64 = b"\x7fELF\x02\x01\x01" + bytes(11) + (0xB7).to_bytes(2, "little") + +ELF_RISCV = b"\x7fELF\x02\x01\x01" + bytes(11) + (0xF3).to_bytes(2, "little") + + +def pex_with_native_header(path: Path, header: bytes, *, revision: str = "a" * 40) -> Path: + """A PEX fixture whose first bytes are a native executable's header. + + A scie is a native launcher with the PEX appended; the build information + is read from the archive, so the header and the archive coexist. + """ + + pex = pex_fixture(path, revision=revision) + pex.write_bytes(header + pex.read_bytes()) + return pex + + +@pytest.fixture(autouse=True) +def launcher_of_the_fixtures_build(monkeypatch: pytest.MonkeyPatch) -> None: + """The running launcher is the build the fixtures record: 0.1.0 at revision a…a.""" + + monkeypatch.setattr("devcapsule.runtime_artifact.current_build_info", lambda: BuildInfo( + schema_version=2, version="0.1.0", source_repository="https://github.com/example/devcapsule", + source_revision="a" * 40, source_url="unknown")) + + +def test_packaged_launcher_supplies_itself_despite_source_override(tmp_path: Path, monkeypatch) -> None: + """A packaged launcher that runs in the capsule delivers itself, whatever DEVCAPSULE_RUNTIME_PEX names.""" + launcher = pex_fixture(tmp_path / "launcher.pex") + monkeypatch.setenv("SCIE", str(launcher)) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(tmp_path / "different.pex")) + assert runtime_artifact(AMD64) == launcher + + +def test_source_launcher_requires_explicit_built_runtime(tmp_path: Path, monkeypatch) -> None: + """A launcher run from source delivers the runtime DEVCAPSULE_RUNTIME_PEX names, and refuses one that does not exist.""" + monkeypatch.delenv("SCIE", raising=False) + monkeypatch.delenv("PEX", raising=False) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(tmp_path / "missing.pex")) + with pytest.raises(CliError, match="DEVCAPSULE_RUNTIME_PEX"): + runtime_artifact(AMD64) + launcher = pex_fixture(tmp_path / "runtime.pex") + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(launcher)) + assert runtime_artifact(AMD64) == launcher + + +def test_executable_platform_reads_native_headers(tmp_path: Path) -> None: + """An executable's platform is read from its native header: macOS, a capsule platform, another Linux, or none for a plain PEX.""" + assert executable_platform(pex_with_native_header(tmp_path / "mac", MACH_O_ARM64)) is HostOs.DARWIN + assert executable_platform(pex_with_native_header(tmp_path / "amd64", ELF_AMD64)) is AMD64 + assert executable_platform(pex_with_native_header(tmp_path / "arm64", ELF_ARM64)) is ARM64 + assert executable_platform(pex_with_native_header(tmp_path / "riscv", ELF_RISCV)) is HostOs.LINUX + assert executable_platform(pex_fixture(tmp_path / "plain.pex")) is None + assert executable_platform(tmp_path / "absent") is None + + +@pytest.mark.own_host_patches +def test_macos_launcher_delivers_the_named_linux_runtime(tmp_path: Path, monkeypatch) -> None: + """A macOS launcher delivers the Linux runtime DEVCAPSULE_RUNTIME_PEX names.""" + on_host(monkeypatch, "Darwin", "arm64") + monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) + linux = pex_with_native_header(tmp_path / "devcapsule-linux", ELF_AMD64) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(linux)) + assert runtime_artifact(AMD64) == linux + + +@pytest.mark.own_host_patches +def test_macos_launcher_without_a_linux_runtime_says_what_to_set(tmp_path: Path, monkeypatch) -> None: + """A macOS launcher without a runtime of the capsule's platform says what it is and what to set.""" + on_host(monkeypatch, "Darwin", "arm64") + monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) + monkeypatch.delenv("DEVCAPSULE_RUNTIME_PEX", raising=False) + with pytest.raises(CliError, match="a macOS executable and cannot run inside linux-amd64 capsules.*DEVCAPSULE_RUNTIME_PEX"): + runtime_artifact(AMD64) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(pex_with_native_header(tmp_path / "wrong-arch", ELF_ARM64))) + with pytest.raises(CliError, match="a linux-arm64 executable"): + runtime_artifact(AMD64) + + +@pytest.mark.own_host_patches +def test_a_plain_pex_launcher_on_a_mac_is_not_delivered(tmp_path: Path, monkeypatch) -> None: + """A plain PEX launcher on a Mac is not delivered, since its native wheels are the Mac's; a named runtime is.""" + # A PEX bundles native wheels for the machine it was built on. + on_host(monkeypatch, "Darwin", "arm64") + monkeypatch.delenv("SCIE", raising=False) + monkeypatch.setenv("PEX", str(pex_fixture(tmp_path / "launcher.pex"))) + linux = pex_with_native_header(tmp_path / "devcapsule-linux", ELF_AMD64) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(linux)) + assert runtime_artifact(AMD64) == linux + monkeypatch.delenv("DEVCAPSULE_RUNTIME_PEX") + with pytest.raises(CliError, match="a plain PEX built for another machine"): + runtime_artifact(AMD64) + + +@pytest.mark.own_host_patches +def test_a_runtime_of_another_build_is_refused(tmp_path: Path, monkeypatch) -> None: + """A delivered runtime of another revision than the launcher's is refused.""" + # D-0009: the runtime runs the plan this launcher writes. A runtime left + # from an earlier build, after the launcher was updated, is refused. + on_host(monkeypatch, "Darwin", "arm64") + monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) + stale = pex_with_native_header(tmp_path / "devcapsule-linux", ELF_AMD64, revision="b" * 40) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(stale)) + with pytest.raises(CliError, match="is DevCapsule 0.1.0 from b+, but this launcher is 0.1.0 from a+"): + runtime_artifact(AMD64) + + +@pytest.mark.own_host_patches +def test_a_runtime_directory_serves_each_capsule_platform(tmp_path: Path, monkeypatch) -> None: + """A runtime directory delivers the executable of each launch's platform, and names the one it lacks.""" + # Apple Silicon runs both platforms; one setting names both runtimes, and + # each launch delivers the one for its lock's platform. + on_host(monkeypatch, "Darwin", "arm64") + monkeypatch.setenv("SCIE", str(pex_with_native_header(tmp_path / "devcapsule-macos", MACH_O_ARM64))) + runtimes = tmp_path / "runtimes" + runtimes.mkdir() + amd64 = pex_with_native_header(runtimes / "devcapsule-linux-amd64", ELF_AMD64) + arm64 = pex_with_native_header(runtimes / "devcapsule-linux-arm64", ELF_ARM64) + monkeypatch.setenv("DEVCAPSULE_RUNTIME_PEX", str(runtimes)) + assert runtime_artifact(ARM64) == arm64 + assert runtime_artifact(AMD64) == amd64 + amd64.unlink() + with pytest.raises(CliError, match="holds no devcapsule-linux-amd64"): + runtime_artifact(AMD64) diff --git a/devcapsule-src/tests/unit/tests/test_conftest.py b/devcapsule-src/tests/unit/tests/test_conftest.py index 7dfcc6cb..37740ce7 100644 --- a/devcapsule-src/tests/unit/tests/test_conftest.py +++ b/devcapsule-src/tests/unit/tests/test_conftest.py @@ -8,14 +8,15 @@ _PYPROJECT = Path(__file__).resolve().parents[3] / "pyproject.toml" -NON_DOCKER_MARKERS = frozenset({"integration", "flaky", "legacy"}) -"""The registered markers that never need Docker: process-boundary tests, visibility runs, legacy files.""" +NON_DOCKER_MARKERS = frozenset({"integration", "flaky", "legacy", "own_host_patches"}) +"""The registered markers that never need Docker: process-boundary tests, visibility runs, legacy files, +and unit tests that set up their host themselves.""" def test_every_registered_marker_is_a_docker_marker_unless_it_is_a_known_non_docker_one() -> None: """Precondition: ``pyproject.toml`` registers the suite's markers. Action: read the registered names. Postcondition: ``DOCKER_MARKERS`` is exactly the registered - set minus the three non-Docker markers, so a Docker suite cannot be registered + set minus the four non-Docker markers, so a Docker suite cannot be registered without the isolation fixture learning that it keeps the developer's homes.""" registered = {entry.split(":", 1)[0] for entry in tomllib.loads(_PYPROJECT.read_text())["tool"]["pytest"]["ini_options"]["markers"]} assert set(DOCKER_MARKERS) == registered - NON_DOCKER_MARKERS diff --git a/docs/platforms/macos.md b/docs/platforms/macos.md index c89837fd..454e5187 100644 --- a/docs/platforms/macos.md +++ b/docs/platforms/macos.md @@ -14,10 +14,10 @@ experimental and not a supported platform in {{tag}}. Intel Macs are untested. A project runs as one of two capsule platforms: -- **`linux-arm64`, natively, the default.** Faster, but its base image is not - published yet: you build it on your Mac once and select it for each checkout. +- **`linux-arm64`, natively, the default.** Faster, with a base image of its + own for ARM. - **`linux-amd64`, under emulation.** The same environments Linux users get, - with the published base, run by Docker Desktop under x86-64 emulation. + with the same base, run by Docker Desktop under x86-64 emulation. `devcapsule project init` asks which one a new project is for; Enter takes `linux-arm64`, and `--platform PLATFORM` answers without the question. A @@ -93,40 +93,32 @@ export DEVCAPSULE_RUNTIME_PEX="$HOME/.local/lib/devcapsule" ``` Add the `export` line, and `$HOME/.local/bin` on your `PATH`, to your shell -profile. Then build the arm64 base below, or continue with -[your first session](../getting-started/first-session.md) and answer -`linux-amd64` when init asks for the platform. +profile. Then continue with +[your first session](../getting-started/first-session.md); init asks for the +platform, and Enter takes `linux-arm64`. ## Run a project natively as `linux-arm64` -Build the arm64 base once, natively, with the launcher you installed and the -revision it was built from (`devcapsule version` shows it): +A new project is `linux-arm64` unless you answer otherwise. To run a project +locked for `linux-amd64` natively, add the arm64 lock beside the other: ```bash -devcapsule images build --type base --recipe ubuntu-24.04 --platform linux-arm64 \ - --tag devcapsule-base-local:arm64 --source-revision "$revision" -``` - -Then, in each project, create the arm64 lock, or add it beside an existing -`linux-amd64` one, and select your local base for the checkout: - -```bash -devcapsule project init --platform linux-arm64 --authorize base-image default -devcapsule project config authorize base-image devcapsule-base-local:arm64 +devcapsule project init --platform linux-arm64 devcapsule project run ``` -VSCodium, VS Code, Eclipse, PyCharm, IntelliJ IDEA and Rider have each started -natively on this base. A component no arm64 run has validated yet, such as the -.NET SDK or Playwright, needs `--unverified` on `init` or on the `config` -command that adds it; the command says so. The arm64 lock names a base that is -not published yet, so do not commit it to a shared repository: anyone without -a local arm64 base cannot run it. A change to the project's requirements made -on your Mac updates both locks. +The first launch pulls the arm64 base. VSCodium, VS Code, Eclipse, PyCharm, +IntelliJ IDEA and Rider have each started natively on it. A component no arm64 +run has validated yet, such as the .NET SDK or Playwright, needs `--unverified` +on `init` or on the `config` command that adds it; the command says so. A +change to the project's requirements made on your Mac updates both locks. + +A checkout that selected a locally built arm64 base before this one was +published keeps it until you select the project's base again with +`devcapsule project config authorize base-image default`. To run the project under emulation again, delete -`.devcapsule/devcapsule.linux-arm64.lock` and select the project's base for -your checkout again with `devcapsule project config authorize base-image default`. +`.devcapsule/devcapsule.linux-arm64.lock`. If something fails on your Mac, please [open an issue](https://github.com/ccozianu/devcapsule/issues/new) with your macOS and Docker Desktop versions and the error output, omitting diff --git a/engineering-docs/development/unit-tests.md b/engineering-docs/development/unit-tests.md index c552869b..a064636c 100644 --- a/engineering-docs/development/unit-tests.md +++ b/engineering-docs/development/unit-tests.md @@ -34,7 +34,9 @@ changed lines covered and the whole package, into the pull request's description between two marker comments, through `scripts/publish_coverage_numbers_to_pr.py`; a merge that takes the description as its message carries them into `main`'s history. The block is -replaced on every run and the author's text around it is never touched. +replaced on every run and the author's text around it is never touched. A pull +request from a fork gets a read-only token, so its description is left alone; +the job summary and the uploaded coverage reports still carry the figures. The hosted `tests.yml` workflow selects the existing serial unit suite when runtime, test, build or unclassified inputs change. It runs no Docker. diff --git a/engineering-docs/requirements/devcapsule/r-runtime-001-launcher-runtime-identity.md b/engineering-docs/requirements/devcapsule/r-runtime-001-launcher-runtime-identity.md index 8e6fd616..198843f0 100644 --- a/engineering-docs/requirements/devcapsule/r-runtime-001-launcher-runtime-identity.md +++ b/engineering-docs/requirements/devcapsule/r-runtime-001-launcher-runtime-identity.md @@ -70,8 +70,9 @@ dependency cycle and keeps runtime-plan production and execution in one build. Existing coverage for the default, delivered with v0.2.11: -- `tests/test_runtime_artifact.py` checks executable selection, explicit - source-runtime selection, and formation identity changing with runtime bytes. +- `tests/unit/devcapsule/test_runtime_artifact.py` checks executable selection + and explicit source-runtime selection; `tests/test_runtime_artifact.py`, + formation identity changing with runtime bytes. - `tests/e2e/test_component_cache.py` checks the installed executable's SHA-256, image label, runtime invocation, and inside/outside version metadata for both fixture surface families. @@ -86,7 +87,7 @@ Future changes must preserve these properties. Any additional override interface needs its own explicit-selection and default-regression coverage; this record does not claim that interface has been validated. -The macOS delivery path is covered by `tests/test_runtime_artifact.py`: the +The macOS delivery path is covered by `tests/unit/devcapsule/test_runtime_artifact.py`: the named runtime and the directory form, refusal of a foreign or missing runtime, of a plain PEX on a non-Linux host, and of a runtime from another build. The IDE smoke compares the capsule's runtime digest with the runtime the workspace's diff --git a/engineering-docs/wip/2026-11-03-macos-launcher/CURRENT-STATUS.md b/engineering-docs/wip/2026-11-03-macos-launcher/CURRENT-STATUS.md index 7d3eb985..79f838ed 100644 --- a/engineering-docs/wip/2026-11-03-macos-launcher/CURRENT-STATUS.md +++ b/engineering-docs/wip/2026-11-03-macos-launcher/CURRENT-STATUS.md @@ -4,7 +4,7 @@ Mnemonic: `macos-launcher` Start date: 2026-11-03 -State: active; slices 1 to 4 done 2026-11-03; waits on the SSH key to publish, and on the owner's rulings +State: active; on `main` since #338, 2026-11-04, with `linux-arm64`; waits on the owner's rulings Branch association: `ws-macos-launcher/host-port`, forked from local `main` at b3f10b7d @@ -107,16 +107,15 @@ with development sudo: the policy `0 0 0440` in the capsule, `sudo -n id -u` as prints 0, the volume gone afterwards. Unit suite on this Mac: 2,729 passed, 19 skipped. The fixed pair is installed in the human's `~/.local`. +**2026-11-04, merged.** This branch reached `main` inside `linux-arm64`'s pull request, +which the owner merged as #338. + ## Next Resumable Task -None in the plan; the slices are done. Publish the workstream and push the branch once the -SSH key is loaded, give the human the compare link, and wait for the owner's rulings below. +None in the plan; the slices are done and on `main`. The owner's rulings below remain. ## Open Threads -- The SSH agent on this Mac holds no identity, so `git fetch`, `workflow claim` and - `workflow publish` fail with `Permission denied (publickey)`. The human loads the key; - until then the registration is local only and `main` could not be re-verified. - No `python3.12` exists on this Mac. `.venv` was created with `uv venv --python 3.12` (CPython 3.12.12) instead of `python3.12 -m venv`; the executable still embeds 3.12.14. - Adding a supported host platform is a product commitment. The repository owner has not diff --git a/engineering-docs/wip/2026-11-04-linux-arm64/2026-11-04-design-linux-arm64.md b/engineering-docs/wip/2026-11-04-linux-arm64/2026-11-04-design-linux-arm64.md index d8b8c027..68346871 100644 --- a/engineering-docs/wip/2026-11-04-linux-arm64/2026-11-04-design-linux-arm64.md +++ b/engineering-docs/wip/2026-11-04-linux-arm64/2026-11-04-design-linux-arm64.md @@ -25,6 +25,9 @@ the base recipe, on top of `macos-launcher`'s platform split. File references ar 1. Every component at once, not a first slice. 2. The arm64 base is built locally and approved per checkout; the owner publishes later. + Settled the same day: the human pushed this Mac's image, at the digest the matrix pins, + as `mycodespaceai/devcapsule-base:linux-arm64-recipe-11-20261104`, so an arm64 checkout + pulls its base like an amd64 one, and the unpublished-base report and refusal are gone. 3. On Apple Silicon, a project runs as `linux-arm64` when it has a `linux-arm64` lock, and otherwise as `linux-amd64` under emulation. Amended the same day, after the human's first init of a new project: on a host that runs several platforms, init asks which a @@ -56,7 +59,8 @@ the base recipe, on top of `macos-launcher`'s platform split. File references ar both record one. 3. **The base.** Built natively on this Mac by `devcapsule images build --type base`, and approved per checkout by the documented local-base path. Whether the `linux-arm64` matrix - can resolve without a published base pin is slice 2's first question. + can resolve without a published base pin is slice 2's first question. Since its push + (section 2), the pin is a published base like every other. 4. **Pins.** The same versions as the amd64 matrix, each with its arm64 archive's URL and a sha256 computed from the downloaded bytes, never copied from a page alone. Eclipse's native closure comes from `ports.ubuntu.com`. Playwright's Chromium has no diff --git a/engineering-docs/wip/2026-11-04-linux-arm64/CURRENT-STATUS.md b/engineering-docs/wip/2026-11-04-linux-arm64/CURRENT-STATUS.md index 5c1e0a31..07efc23e 100644 --- a/engineering-docs/wip/2026-11-04-linux-arm64/CURRENT-STATUS.md +++ b/engineering-docs/wip/2026-11-04-linux-arm64/CURRENT-STATUS.md @@ -4,12 +4,14 @@ Mnemonic: `linux-arm64` Start date: 2026-11-04 -State: active; slices 1 to 4 done 2026-11-04, six IDEs running natively; waits on publication and the owner +State: active; on `main` since #338, 2026-11-04, its arm64 base published; the follow-up pull request turns +`main`'s CI green again Branch association: `ws-linux-arm64/capsules`, forked from local `main` at b3f10b7d, with -`ws-macos-launcher/host-port` merged in as its foundation +`ws-macos-launcher/host-port` merged in as its foundation; merged to `main` as #338 from dcc2a7db. +Follow-up: `ws-linux-arm64/mirror-and-published-base`, from `main` at bf7e5d66 -Integration target: `main`, after `macos-launcher`'s pull request +Integration target: `main` Delivery method: pull request, merge commit; the human opens and merges it @@ -112,27 +114,42 @@ arm64 on arm"). `Platform.current()` is now the native platform, and init asks t elicitor before the identity questions: Enter, or a run without a terminal, takes it. A project with a lock the host runs is not asked, and a Linux host never is. +**2026-11-04, on today's `main`.** Rebased onto `main` at 7ab3adcd, which brought the +unit-test conventions of WORKFLOW-LOCAL.md: a legacy test file only loses tests. Every test +this branch added or changed now lives in the `tests/unit` mirror with a contract docstring; +the two end-to-end files it changed are marked `e2e` at module level; the Linux-only skips +it had added to three legacy files are gone, so on a Mac those tests fail as they do on +`main`. The gate: 108 legacy files holding 1,461 tests, 22 conforming files, no breach. +In a `linux/amd64` container, 2,821 passed and 5 failed, the five that fail on `main` there. + +**2026-11-04, merged, and the base published.** The owner merged the pull request as #338 +from dcc2a7db, before the three commits above it were pushed, so `main`'s CI fails at the +conventions gate: 115 breaches in legacy files. `ws-linux-arm64/mirror-and-published-base` +carries those three commits, applied unchanged onto `main` at bf7e5d66. `main` now has the +Linux-only skips the move had taken out of three legacy files, so they stay: taking them out +again would change legacy tests. The gate against bf7e5d66: 108 legacy files holding 1,461 +tests, 22 conforming files, no breach. The human pushed +this Mac's arm64 base as `mycodespaceai/devcapsule-base:linux-arm64-recipe-11-20261104` +at 15:29 UTC; the registry returns the pinned index digest, `sha256:7d32268065b6…`, for +`linux/arm64`, so the pin and its six verified edges stand. The pin's `published` flag, +init's note and report line, and the launch's refusal served only the unpublished base and +are removed; the macOS page no longer builds or selects a local base. + ## Next Resumable Task -Publish this workstream and `macos-launcher` once the SSH agent holds a key, and hand both -pull requests to the human, the macOS one first. The owner's rulings and the arm64 base's -publication follow. +The human pushes `ws-linux-arm64/mirror-and-published-base` and opens its pull request; +once it is merged and `main`'s CI is green, the owner's rulings remain. ## Open Threads -- The SSH agent on this Mac holds no identity, so the workstream cannot be published or - pushed; registration is local, as is `macos-launcher`'s. -- This branch builds on `ws-macos-launcher/host-port`, which is not yet on `main`. The - registration commit is forked from `main` as topic 7.5 requires, and the macOS branch is - merged after it; this pull request follows that one. -- An arm64 base on `docker.io/mycodespaceai`, and its digest in the matrix, need the owner. - The pin holds the digest a push of this Mac's exact image keeps; a rebuild must re-pin. +- `main` at bf7e5d66 fails CI at the conventions gate until the follow-up branch is merged. +- A rebuilt arm64 base gets another digest: it must be pushed, pinned and smoked again. - The libsoup 404 on amd64 goes to `maintenance` by intake mail once the coordination branch can be pushed. - Docker access from an arm64 capsule reaches an arm64 daemon either way; on amd64 capsules under emulation it also defaults to arm64 images (`docs/platforms/macos.md`). -- A checkout keeps one base-image answer. Deleting the arm64 lock to return to emulation - needs the base answered again, as the macOS page says. +- A checkout that selected a locally built arm64 base before the push keeps it until its + base is answered `default` again, as the macOS page says. ## Document Index