| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/wire" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | // TestProgressSkipsPreviews: a preview step, done or failed, is never the |
| 11 | // step that drives the task. |
| 12 | func TestProgressSkipsPreviews(t *testing.T) { |
| 13 | cand := "2222222222222222222222222222222222222222" |
| 14 | steps := []stepRecord{ |
| 15 | {ID: "e", Specialist: wire.SpecialistEngineering, State: wire.StepDone, Pushed: true, CandidateSHA: cand}, |
| 16 | {ID: "c", Specialist: wire.SpecialistChecks, State: wire.StepDone, Checks: wire.ChecksPassed, CandidateSHA: cand}, |
| 17 | {ID: "r", Specialist: wire.SpecialistReview, State: wire.StepDone, Verdict: wire.VerdictApprove, CandidateSHA: cand}, |
| 18 | {ID: "p", Specialist: wire.SpecialistPreview, State: wire.StepFailed, Reason: "preview_cap", Terminated: true, CandidateSHA: cand}, |
| 19 | } |
| 20 | p := progressOf(steps) |
| 21 | if p.last == nil || p.last.ID != "r" || p.review == nil || p.candidate != cand { |
| 22 | t.Fatalf("progress %+v", p.last) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // previewRig is a task with a preview step that started on RUNNER1, its |
| 27 | // export recorded as built when built is set. |
| 28 | func previewRig(t *testing.T, built bool) (*hkRig, taskRecord, stepRecord) { |
| 29 | r := newHKRig(t) |
| 30 | r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil } |
| 31 | k := r.task("tkpv1", "Barber mockups", "owner@example.com") |
| 32 | start := r.now |
| 33 | s := stepRecord{ID: "s_pv", TaskID: k.ID, Idx: 5, Specialist: wire.SpecialistPreview, Person: "owner@example.com", State: wire.StepRunning, Attempt: 1, CandidateSHA: "2222222222222222222222222222222222222222", RunnerID: "RUNNER1", StartedAt: &start, Revision: 1, CreatedAt: r.now, UpdatedAt: r.now} |
| 34 | if built { |
| 35 | s.Preview = `{"phase":"built","digest":"` + digestA + `","files":3,"bytes":100}` |
| 36 | } |
| 37 | r.must(r.tx(func(t *txn) error { return t.db.Create(&s).Error })) |
| 38 | return r, k, s |
| 39 | } |
| 40 | |
| 41 | var digestA = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 42 | |
| 43 | // TestPreviewNeverReplacedAfterStart: a preview attempt that started is |
| 44 | // never followed by another: once built it may have published, so it is |
| 45 | // done and could not be confirmed; before that it is cancelled. |
| 46 | func TestPreviewNeverReplacedAfterStart(t *testing.T) { |
| 47 | for _, built := range []bool{true, false} { |
| 48 | r, k, s := previewRig(t, built) |
| 49 | r.must(r.tx(func(t *txn) error { |
| 50 | if err := t.replace(&k, &s); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | return t.saveStep(&s, &k) |
| 54 | })) |
| 55 | var got stepRecord |
| 56 | r.must(r.tx(func(t *txn) error { return t.db.Where("id = ?", "s_pv").First(&got).Error })) |
| 57 | p := decodePreview(got.Preview) |
| 58 | switch { |
| 59 | case got.Attempt != 1: |
| 60 | t.Fatalf("built %v: a replacement attempt %d", built, got.Attempt) |
| 61 | case built && (got.State != wire.StepDone || p == nil || p.Outcome != wire.PreviewUnconfirmed || p.Digest != digestA): |
| 62 | t.Fatalf("built: %s %+v", got.State, p) |
| 63 | case !built && got.State != wire.StepCancelled: |
| 64 | t.Fatalf("not built: %s", got.State) |
| 65 | } |
| 66 | if n := r.count("SELECT count(*) FROM runner_commands WHERE kind = ?", wire.FrameDispatch); n != 0 { |
| 67 | t.Fatalf("dispatched %d", n) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // TestPreviewWithdrawnKeepsOutcome: a withdrawn preview's done report is |
| 73 | // kept as it says (here deployed, with its link), and nothing replaces it. |
| 74 | func TestPreviewWithdrawnKeepsOutcome(t *testing.T) { |
| 75 | r, _, s := previewRig(t, true) |
| 76 | r.must(r.tx(func(t *txn) error { |
| 77 | return t.db.Model(&stepRecord{}).Where("id = ?", s.ID).Update("withdrawn", true).Error |
| 78 | })) |
| 79 | l := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: "owner@example.com"}, Protocol: wire.ProtocolCurrent} |
| 80 | rep := &wire.StepReport{TaskID: s.TaskID, StepID: s.ID, Attempt: 1, State: wire.StepDone, CandidateSHA: s.CandidateSHA, Termination: true, Findings: []wire.Finding{}, |
| 81 | Preview: &wire.PreviewResult{Phase: wire.PreviewDone, App: "tmp-tkpv1", URL: "https://tmp-tkpv1.bds.oncarlos.com/", Digest: digestA, Outcome: wire.PreviewDeployed}} |
| 82 | r.must(r.tx(func(t *txn) error { return t.stepReport(l, rep) })) |
| 83 | var got stepRecord |
| 84 | r.must(r.tx(func(t *txn) error { return t.db.Where("id = ?", s.ID).First(&got).Error })) |
| 85 | if p := decodePreview(got.Preview); got.Attempt != 1 || got.State != wire.StepDone || got.Withdrawn || p == nil || p.Outcome != wire.PreviewDeployed || p.URL == "" { |
| 86 | t.Fatalf("withdrawn report %s attempt %d %+v", got.State, got.Attempt, p) |
| 87 | } |
| 88 | } |
| 89 | |