| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http/httptest" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/sylvinus/agent-vm/internal/cage" |
| 12 | ) |
| 13 | |
| 14 | func TestStopPreemptsJobWithoutLosingStopResult(t *testing.T) { |
| 15 | s := testServer(t) |
| 16 | started, cancelled, finished := make(chan struct{}), make(chan struct{}), make(chan struct{}) |
| 17 | s.launch(httptest.NewRecorder(), "safety", func() error { |
| 18 | ctx := s.operationContext() |
| 19 | close(started) |
| 20 | <-ctx.Done() |
| 21 | close(cancelled) |
| 22 | s.setMessage("stale result") |
| 23 | _, _ = s.jobs.execution().Write([]byte("stale log")) |
| 24 | return ctx.Err() |
| 25 | }) |
| 26 | select { |
| 27 | case <-started: |
| 28 | case <-time.After(time.Second): |
| 29 | t.Fatal("job did not start") |
| 30 | } |
| 31 | w := httptest.NewRecorder() |
| 32 | s.launch(w, "stop", func() error { s.setMessage("stopped"); close(finished); return nil }) |
| 33 | if w.Code != 200 { |
| 34 | t.Fatalf("stop rejected: %d", w.Code) |
| 35 | } |
| 36 | select { |
| 37 | case <-cancelled: |
| 38 | case <-time.After(time.Second): |
| 39 | t.Fatal("old job was not cancelled") |
| 40 | } |
| 41 | select { |
| 42 | case <-finished: |
| 43 | case <-time.After(time.Second): |
| 44 | t.Fatal("stop did not run") |
| 45 | } |
| 46 | s.op.Lock() |
| 47 | defer s.op.Unlock() |
| 48 | s.mu.Lock() |
| 49 | defer s.mu.Unlock() |
| 50 | if s.jobs.snapshot().Kind != "stop" || s.jobs.snapshot().Running || s.jobs.snapshot().Error != "" || s.jobs.snapshot().Log != "" { |
| 51 | t.Fatalf("stale job overwrote stop: %+v", s.jobs.snapshot()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | func TestPauseBypassesBusyQueueAndRevokesCheck(t *testing.T) { |
| 56 | s := testServer(t) |
| 57 | cfg, err := loadConfig(s.configPath) |
| 58 | if err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | dir := filepath.Join(s.profile, "state", "lima", cage.InstanceName(s.profile, s.environmentDir())) |
| 62 | if err = os.MkdirAll(dir, 0700); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | g, err := cage.NewEgressGate(cfg) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | stop, err := cage.StartGateControl(s.configPath, dir, g) |
| 70 | if err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | defer stop() |
| 74 | if err = g.BeginCheck(); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | ctx, cancel := context.WithCancel(context.Background()) |
| 78 | defer cancel() |
| 79 | s.setJobForTest(Job{Kind: "safety", Running: true}) |
| 80 | s.checkCancel = cancel |
| 81 | w := request(s, "POST", "/api/safety/close", s.origin, "{}", true) |
| 82 | if w.Code != 200 { |
| 83 | t.Fatalf("pause rejected while checking: %d %s", w.Code, w.Body.String()) |
| 84 | } |
| 85 | if ctx.Err() == nil || g.Status().Mode != cage.GateClosed || !s.networkPaused { |
| 86 | t.Fatal("pause did not cancel and close the gate") |
| 87 | } |
| 88 | } |