Skip to content

Commit 89e1011

Browse files
Add KillCaller feature to test server for CLI crash recovery testing
1 parent f915956 commit 89e1011

File tree

13 files changed

+177
-2
lines changed

13 files changed

+177
-2
lines changed

acceptance/internal/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ type ServerStub struct {
156156
// Configure as "1ms", "2s", "3m", etc.
157157
// See [time.ParseDuration] for details.
158158
Delay time.Duration
159+
160+
// If set, send this signal to the caller process instead of returning a response.
161+
// Use signal numbers: 9 for SIGKILL, 15 for SIGTERM, etc.
162+
// Requires DATABRICKS_CLI_TEST_PID=1 to be set.
163+
KillCaller int
159164
}
160165

161166
// FindConfigs finds all the config relevant for this test,

acceptance/internal/prepare_server.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"slices"
1111
"strings"
1212
"sync"
13+
"syscall"
1314
"testing"
1415
"time"
1516
"unicode/utf8"
@@ -209,6 +210,43 @@ func startLocalServer(t *testing.T,
209210
}
210211
}
211212

213+
if stub.KillCaller > 0 {
214+
pid := testserver.ExtractPidFromHeaders(req.Headers)
215+
if pid == 0 {
216+
t.Errorf("KillCaller configured but test-pid not found in User-Agent")
217+
return testserver.Response{
218+
StatusCode: http.StatusBadRequest,
219+
Body: "test-pid not found in User-Agent (set DATABRICKS_CLI_TEST_PID=1)",
220+
}
221+
}
222+
223+
signal := syscall.Signal(stub.KillCaller)
224+
t.Logf("KillCaller: sending signal %d to PID %d (pattern: %s)", signal, pid, stub.Pattern)
225+
226+
process, err := os.FindProcess(pid)
227+
if err != nil {
228+
t.Errorf("Failed to find process %d: %s", pid, err)
229+
return testserver.Response{
230+
StatusCode: http.StatusInternalServerError,
231+
Body: fmt.Sprintf("failed to find process: %s", err),
232+
}
233+
}
234+
235+
if err := process.Signal(signal); err != nil {
236+
t.Errorf("Failed to send signal %d to process %d: %s", signal, pid, err)
237+
return testserver.Response{
238+
StatusCode: http.StatusInternalServerError,
239+
Body: fmt.Sprintf("failed to send signal: %s", err),
240+
}
241+
}
242+
243+
// Return a response (the CLI will likely be killed before it receives this)
244+
return testserver.Response{
245+
StatusCode: http.StatusOK,
246+
Body: fmt.Sprintf("sent signal %d to PID %d", signal, pid),
247+
}
248+
}
249+
212250
return stub.Response
213251
})
214252
}

acceptance/selftest/kill_caller/mid_request/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
>>> errcode [CLI] workspace list /
3+
script: line [N]: [PID] Killed "$@"
4+
5+
Exit code: 137
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trace errcode $CLI workspace list /
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Local = true
2+
Env.DATABRICKS_CLI_TEST_PID = "1"
3+
4+
[[Server]]
5+
Pattern = "GET /api/2.0/workspace/list"
6+
KillCaller = 9
7+
8+
[[Repls]]
9+
Old = 'script: line \d+: \d+ Killed'
10+
New = 'script: line [N]: [PID] Killed'

acceptance/selftest/kill_caller/out.test.toml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
>>> errcode [CLI] current-user me
3+
script: line [N]: [PID] Killed "$@"
4+
5+
Exit code: 137
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
trace errcode $CLI current-user me
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Local = true
2+
Env.DATABRICKS_CLI_TEST_PID = "1"
3+
4+
# Kill the CLI when it calls /Me endpoint
5+
[[Server]]
6+
Pattern = "GET /api/2.0/preview/scim/v2/Me"
7+
KillCaller = 9
8+
9+
[[Repls]]
10+
Old = 'script: line \d+: \d+ Killed'
11+
New = 'script: line [N]: [PID] Killed'

0 commit comments

Comments
 (0)