| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sync/atomic" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/runner/containment" |
| 10 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Astra 4b round 2, finding 2a: after a question's answer is held for an |
| 15 | // unverified exit, the worker runs nothing else (another question or a |
| 16 | // step) until the exit is verified. |
| 17 | func TestNothingRunsBesideAnUnverifiedExit(t *testing.T) { |
| 18 | old := heldRetry |
| 19 | heldRetry = 20 * time.Millisecond |
| 20 | t.Cleanup(func() { heldRetry = old }) |
| 21 | ctx := context.Background() |
| 22 | s, err := OpenState(ctx, t.TempDir()) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | defer s.Close() |
| 27 | var verified atomic.Bool |
| 28 | var asked, stepped atomic.Int32 |
| 29 | e := &StepEngine{State: s, Notify: func() {}, Verify: func(context.Context, string) bool { return verified.Load() }, |
| 30 | Ask: func(_ context.Context, a askRow, _ func(wire.AnswerReport)) askResult { |
| 31 | asked.Add(1) |
| 32 | // The first answer's namespace exit is not verified. |
| 33 | return askResult{Report: wire.AnswerReport{TaskID: a.TaskID, QuestionID: a.QuestionID, Attempt: a.Attempt, State: wire.QuestionAnswered, Text: "x"}, Terminated: a.QuestionID != "q1"} |
| 34 | }, |
| 35 | Exec: func(_ context.Context, r stepRow, _ func(wire.StepReport)) wire.StepReport { |
| 36 | stepped.Add(1) |
| 37 | return wire.StepReport{TaskID: r.TaskID, StepID: r.StepID, Attempt: r.Attempt, State: wire.StepFailed, Reason: "no_changes", Termination: true} |
| 38 | }} |
| 39 | runCtx, cancel := context.WithCancel(ctx) |
| 40 | defer func() { cancel(); e.Wait() }() |
| 41 | if err := e.Start(runCtx); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if err := s.AcceptAsk(ctx, "c1", wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q1", Attempt: 1}); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | e.Wake() |
| 48 | waitFor(t, func() bool { return asked.Load() == 1 }) |
| 49 | if err := s.AcceptAsk(ctx, "c2", wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q2", Attempt: 1}); err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | if err := s.AcceptDispatch(ctx, "c3", dispatchFor("tk2", "s2", 1)); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | e.Wake() |
| 56 | time.Sleep(200 * time.Millisecond) |
| 57 | if asked.Load() != 1 || stepped.Load() != 0 { |
| 58 | t.Fatalf("ran beside an unverified exit: %d questions, %d steps", asked.Load(), stepped.Load()) |
| 59 | } |
| 60 | verified.Store(true) |
| 61 | waitFor(t, func() bool { return asked.Load() == 2 && stepped.Load() == 1 }) |
| 62 | } |
| 63 | |
| 64 | // Astra 4b round 2, finding 2b: a held answer with no namespace recorded |
| 65 | // is never released unless no namespace remains at all. |
| 66 | func TestHeldAnswerWithoutNamespaceNeedsVerification(t *testing.T) { |
| 67 | ctx := context.Background() |
| 68 | s, err := OpenState(ctx, t.TempDir()) |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | defer s.Close() |
| 73 | a := askRow{AskCommand: wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q1", Attempt: 1}} |
| 74 | if err := s.AcceptAsk(ctx, "c1", a.AskCommand); err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | if err := s.startAsk(ctx, a); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | if err := s.FinishAsk(ctx, a, wire.AnswerReport{TaskID: "tk1", QuestionID: "q1", Attempt: 1, State: wire.QuestionAnswered, Text: "x"}, false); err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | var asked []string |
| 84 | e := &StepEngine{State: s, Notify: func() {}, Verify: func(_ context.Context, id string) bool { asked = append(asked, id); return false }} |
| 85 | e.Probe(ctx) |
| 86 | if got := outboxAnswers(t, s); len(got) != 0 || len(asked) != 1 || asked[0] != "" { |
| 87 | t.Fatalf("released without verification: %+v (verify asked %q)", got, asked) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // fakeStarter follows containment's contract: record first, launch only |
| 92 | // if it succeeded. |
| 93 | type fakeStarter struct{ launched bool } |
| 94 | |
| 95 | func (f *fakeStarter) StartRecorded(ctx context.Context, _ provider.Launch, _ []string, record func(context.Context, containment.Record) error) (provider.Process, error) { |
| 96 | if err := record(ctx, containment.Record{ID: "ns-1"}); err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | f.launched = true |
| 100 | return nil, nil |
| 101 | } |
| 102 | |
| 103 | // Astra 4b round 2, finding 2c: the namespace is written before the |
| 104 | // process launches; when it cannot be written nothing launches. |
| 105 | func TestNamespaceRecordedBeforeLaunch(t *testing.T) { |
| 106 | ctx := context.Background() |
| 107 | s, err := OpenState(ctx, t.TempDir()) |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | a := askRow{AskCommand: wire.AskCommand{Person: "owner@example.com", TaskID: "tk1", QuestionID: "q1", Attempt: 1}} |
| 112 | if err := s.AcceptAsk(ctx, "c1", a.AskCommand); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | f := &fakeStarter{} |
| 116 | if _, err := recordedStart(ctx, f, provider.Launch{}, []string{"/bin/true"}, func(ctx context.Context, id string) error { return s.setAskNamespace(ctx, a, id) }); err != nil || !f.launched { |
| 117 | t.Fatalf("launch %v %v", f.launched, err) |
| 118 | } |
| 119 | var ns string |
| 120 | s.db.QueryRow("SELECT ns_id FROM asks WHERE question_id='q1'").Scan(&ns) |
| 121 | if ns != "ns-1" { |
| 122 | t.Fatalf("namespace %q", ns) |
| 123 | } |
| 124 | s.Close() |
| 125 | f = &fakeStarter{} |
| 126 | if _, err := recordedStart(ctx, f, provider.Launch{}, []string{"/bin/true"}, func(ctx context.Context, id string) error { return s.setAskNamespace(ctx, a, id) }); err == nil || f.launched { |
| 127 | t.Fatalf("launched without its namespace recorded: %v", err) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | func waitFor(t *testing.T, cond func() bool) { |
| 132 | t.Helper() |
| 133 | deadline := time.Now().Add(5 * time.Second) |
| 134 | for !cond() { |
| 135 | if time.Now().After(deadline) { |
| 136 | t.Fatal("timed out") |
| 137 | } |
| 138 | time.Sleep(10 * time.Millisecond) |
| 139 | } |
| 140 | } |
| 141 | |