Skip to content
Open
1 change: 1 addition & 0 deletions .nextchanges/cli/sandbox-forward.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add `databricks sandbox forward`, which serves an app running in a sandbox at a local URL. ([#7395](https://github.com/databricks/cli/pull/7395))
1 change: 1 addition & 0 deletions acceptance/cmd/sandbox/forward/forward/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
28 changes: 28 additions & 0 deletions acceptance/cmd/sandbox/forward/forward/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

=== The command reports the forward once it is listening

>>> retry --until Press Ctrl-C to stop. cat LOG.forward
Forwarding http://127.0.0.1:[LOCAL_PORT] -> happy-panda-1234:10080
Press Ctrl-C to stop.

=== A local request reaches the sandbox app through the forward route

>>> curl -sS -X POST -H X-Databricks-Org-Id: 999 -d ping http://127.0.0.1:[LOCAL_PORT]/api/items?page=2
hello from the sandbox app

=== The workspace receives the CLI's routing header in place of the caller's

>>> print_requests.py //sandbox-proxy
{
"headers": {
"X-Databricks-Org-Id": [
"[WORKSPACE_ID]"
]
},
"method": "POST",
"path": "/sandbox-proxy/sandboxes/happy-panda-1234/ports/10080/api/items",
"q": {
"page": "2"
},
"raw_body": "ping"
}
20 changes: 20 additions & 0 deletions acceptance/cmd/sandbox/forward/forward/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Ports are allocated per run so that tests in parallel worktrees do not collide.
read -r LOCAL_PORT <<< "$(free_port.py 1)"

# The forward runs until interrupted. Start it outside this shell's job table so
# stopping it doesn't print a "Killed" job notice, and stop it by port, which
# also works on Windows.
( $CLI sandbox forward happy-panda-1234 -L "$LOCAL_PORT:10080" &> LOG.forward & )
trap 'kill_port.py "$LOCAL_PORT"' EXIT

export RETRY_MAX_ATTEMPTS=30

title "The command reports the forward once it is listening"
trace retry --until "Press Ctrl-C to stop." cat LOG.forward

title "A local request reaches the sandbox app through the forward route"
trace curl -sS -X POST -H "X-Databricks-Org-Id: 999" -d 'ping' "http://127.0.0.1:$LOCAL_PORT/api/items?page=2"
echo

title "The workspace receives the CLI's routing header in place of the caller's"
trace print_requests.py //sandbox-proxy
15 changes: 15 additions & 0 deletions acceptance/cmd/sandbox/forward/forward/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
RecordRequests = true
IncludeRequestHeaders = ["X-Databricks-Org-Id"]

[[Server]]
Pattern = "GET /api/2.0/lakebox/sandboxes/happy-panda-1234"
Response.Body = '{"sandboxId": "happy-panda-1234", "status": "Running"}'

[[Server]]
Pattern = "POST /sandbox-proxy/sandboxes/happy-panda-1234/ports/10080/{path...}"
Response.Body = 'hello from the sandbox app'

# The local port is allocated per run.
[[Repls]]
Old = '127\.0\.0\.1:\d+'
New = '127.0.0.1:[LOCAL_PORT]'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
3 changes: 3 additions & 0 deletions acceptance/cmd/sandbox/forward/invalid-forward/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Error: invalid -L "8080": expected LOCAL:REMOTE ports, e.g. 8080:10080
Error: invalid -L "8080:8080": remote port must be between 10000 and 65535
Error: required flag(s) "forward" not set
8 changes: 8 additions & 0 deletions acceptance/cmd/sandbox/forward/invalid-forward/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Malformed -L specs are rejected before any API call, so no [[Server]] stub is needed.
musterr $CLI sandbox forward happy-panda-1234 -L 8080

# Remote ports outside 10000-65535 are refused up front.
musterr $CLI sandbox forward happy-panda-1234 -L 8080:8080

# At least one -L is required.
musterr $CLI sandbox forward happy-panda-1234
1 change: 1 addition & 0 deletions acceptance/cmd/sandbox/forward/no-default/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
1 change: 1 addition & 0 deletions acceptance/cmd/sandbox/forward/no-default/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: no default sandbox configured — run `databricks sandbox create` to provision one, or `databricks sandbox default <id>` to set an existing sandbox as the default
2 changes: 2 additions & 0 deletions acceptance/cmd/sandbox/forward/no-default/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Without a sandbox ID the default sandbox is used; the fresh home has none.
musterr $CLI sandbox forward -L 0:10080
1 change: 1 addition & 0 deletions acceptance/cmd/sandbox/forward/not-found/out.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
1 change: 1 addition & 0 deletions acceptance/cmd/sandbox/forward/not-found/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: no sandbox named "no-such-box" — `databricks sandbox list` shows available IDs
1 change: 1 addition & 0 deletions acceptance/cmd/sandbox/forward/not-found/script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
musterr $CLI sandbox forward no-such-box -L 0:10080
4 changes: 4 additions & 0 deletions acceptance/cmd/sandbox/forward/not-found/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[Server]]
Pattern = "GET /api/2.0/lakebox/sandboxes/no-such-box"
Response.StatusCode = 404
Response.Body = '{"error_code": "NOT_FOUND", "message": "Sandbox not found"}'
Loading
Loading