refactor: Bottle.exec takes a user= kwarg, default node
test / unit (pull_request) Successful in 26s
test / integration (pull_request) Successful in 41s

Promote the user-switch from a hardcoded `node` to a keyword arg
so callers can opt into root (or any other user) when needed.
Default stays `node` — matches the docker image's USER and the
smolmachines runuser default.

Lifts the change through the base ABC, docker, and smolmachines
backends:
- Base: `def exec(self, script, *, user="node")`.
- Docker: adds `-u <user>` to `docker exec` (no-op when user is
  node, the image's default).
- Smolmachines: `runuser -l <user> -c <script>` — `runuser -l
  root` is the trivial no-op form when the caller asked for root.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-05-27 15:00:13 -04:00
parent e26d459a97
commit af65c10361
3 changed files with 32 additions and 15 deletions
+12 -6
View File
@@ -65,15 +65,21 @@ class SmolmachinesBottle(Bottle):
result = subprocess.run(flags, check=False)
return result.returncode
def exec(self, script: str) -> ExecResult:
"""Run a POSIX shell script as the `node` user and capture
the result. Matches the docker backend's `exec`, which
defaults to the image's USER (also node) — so test
def exec(self, script: str, *, user: str = "node") -> ExecResult:
"""Run a POSIX shell script as `user` (default `node`) and
capture the result. Matches the docker backend's `exec`,
which defaults to the image's USER (also node) — so test
helpers / provision shell-outs run with the same identity
on both backends."""
on both backends. Pass `user="root"` for tests that need
root.
`smolvm machine exec` runs commands as root in the VM, so
we always need to switch user (even when the caller asked
for root, switching to root is a cheap no-op via
`runuser -l root`)."""
r = _smolvm.machine_exec(
self.name,
["runuser", "-l", "node", "-c", script],
["runuser", "-l", user, "-c", script],
)
return ExecResult(
returncode=r.returncode,