mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-06 12:24:44 +00:00
the timeout by exit code 124. artex-exec-wrap returns 124 when it enforces timeout_ms, but the command can also exit 124 itself: coreutils `timeout 1 sleep 5` does. The runner then removed the worker and stopped the task's background jobs after an ordinary inner timeout. Count an exit 124 as the runner's timeout only when the exec ran for at least timeout_ms, which is the only way the wrapper can return it.
133 lines
4.8 KiB
Go
133 lines
4.8 KiB
Go
package sandbox
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestExecFailsClosedAndRemovesWorker(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
fake *fakeDocker
|
|
wantErr bool
|
|
}{
|
|
{"timeout", &fakeDocker{execExitCode: 124, execDelay: 20 * time.Millisecond}, false},
|
|
{"still_running", &fakeDocker{execRunning: true}, true},
|
|
{"truncated_stream", &fakeDocker{execStream: []byte{1, 0, 0}}, true},
|
|
{"truncated_payload", &fakeDocker{execStream: []byte{1, 0, 0, 0, 0, 0, 0, 4, 'x'}}, true},
|
|
{"inspect_failure", &fakeDocker{inspectCode: http.StatusInternalServerError}, true},
|
|
{"missing_exit_code", &fakeDocker{inspectBody: `{"Running":false}`}, true},
|
|
{"missing_running_status", &fakeDocker{inspectBody: `{"ExitCode":0}`}, true},
|
|
{"malformed_inspect", &fakeDocker{inspectBody: `{`}, true},
|
|
{"start_failure", &fakeDocker{execStartCode: http.StatusInternalServerError}, true},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
b := newFakeDockerBackend(t, test.fake)
|
|
tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1")
|
|
tc.scopeHash = scopeFingerprint(nil, nil)
|
|
timeoutMS := 1000
|
|
if test.fake.execDelay > 0 {
|
|
timeoutMS = 10
|
|
}
|
|
res, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: timeoutMS})
|
|
if (err != nil) != test.wantErr {
|
|
t.Fatalf("response=%+v error=%v", res, err)
|
|
}
|
|
if !test.wantErr && !res.TimedOut {
|
|
t.Fatalf("timeout lost: %+v", res)
|
|
}
|
|
if tc.workerID != "" || !test.fake.seen("DELETE /containers/worker-1") {
|
|
t.Fatal("worker survived failed/timed-out execution")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExecCancellationCleansUpWithFreshContext(t *testing.T) {
|
|
f := &fakeDocker{streamReady: make(chan struct{})}
|
|
b := newFakeDockerBackend(t, f)
|
|
tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1")
|
|
tc.scopeHash = scopeFingerprint(nil, nil)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
done := make(chan error, 1)
|
|
go func() { _, err := b.Exec(ctx, "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 1000}); done <- err }()
|
|
select {
|
|
case <-f.streamReady:
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("exec did not start")
|
|
}
|
|
cancel()
|
|
select {
|
|
case err := <-done:
|
|
if err == nil {
|
|
t.Fatal("cancelled exec reported success")
|
|
}
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("cleanup hung")
|
|
}
|
|
if tc.workerID != "" || !f.seen("DELETE /containers/worker-1") {
|
|
t.Fatal("cancelled exec left worker alive")
|
|
}
|
|
}
|
|
|
|
func TestExecTimeoutRemovalFailureKeepsWorker(t *testing.T) {
|
|
f := &fakeDocker{execExitCode: 124, execDelay: 20 * time.Millisecond, deleteCode: map[string]int{"worker-1": 500}}
|
|
b := newFakeDockerBackend(t, f)
|
|
tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1")
|
|
tc.scopeHash = scopeFingerprint(nil, nil)
|
|
if _, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 10}); err == nil {
|
|
t.Fatal("cleanup failure hidden")
|
|
}
|
|
if tc.workerID != "worker-1" {
|
|
t.Fatal("worker lost before cleanup was proven")
|
|
}
|
|
}
|
|
|
|
func TestSuccessfulExecPreservesWorker(t *testing.T) {
|
|
f := &fakeDocker{}
|
|
b := newFakeDockerBackend(t, f)
|
|
tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1")
|
|
tc.scopeHash = scopeFingerprint(nil, nil)
|
|
if _, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 1000}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.workerID != "worker-1" || f.seen("DELETE /containers/worker-1") {
|
|
t.Fatal("successful exec lost its worker")
|
|
}
|
|
}
|
|
|
|
func TestDelayedExecCleanupPreservesReplacementWorker(t *testing.T) {
|
|
f := &fakeDocker{}
|
|
b := newFakeDockerBackend(t, f)
|
|
tc := seedTask(b, "t1", "replacement-worker", "proxy-1", "artex-task-t1")
|
|
if err := b.removeExecWorker(context.Background(), tc, "old-worker"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tc.workerID != "replacement-worker" || f.seen("DELETE /containers/replacement-worker") || !f.seen("DELETE /containers/old-worker") {
|
|
t.Fatal("late cleanup targeted or forgot replacement worker")
|
|
}
|
|
}
|
|
|
|
// A command that exits 124 by itself (coreutils `timeout 1 sleep 5`) did not
|
|
// hit the runner's timeout: report its exit code and keep the worker, so the
|
|
// task's background jobs survive.
|
|
func TestExecCommandExit124BeforeTimeoutKeepsWorker(t *testing.T) {
|
|
f := &fakeDocker{execExitCode: 124}
|
|
b := newFakeDockerBackend(t, f)
|
|
tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1")
|
|
tc.scopeHash = scopeFingerprint(nil, nil)
|
|
res, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"timeout", "1", "sleep", "5"}, TimeoutMS: 30000})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.ExitCode != 124 || res.TimedOut {
|
|
t.Fatalf("command exit 124 reported as runner timeout: %+v", res)
|
|
}
|
|
if tc.workerID != "worker-1" || f.seen("DELETE /containers/worker-1") {
|
|
t.Fatal("worker removed after the command's own exit 124")
|
|
}
|
|
}
|