| 1 | package team |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestRunCheckOutcomes(t *testing.T) { |
| 9 | cases := []struct { |
| 10 | name string |
| 11 | repro []byte |
| 12 | subject string |
| 13 | want CheckOutcome |
| 14 | run int |
| 15 | }{ |
| 16 | {"fail on base", FixtureReproduction, FixtureBaseSource, CheckFail, 3}, |
| 17 | {"pass on fix", FixtureReproduction, FixtureFixedSource, CheckPass, 3}, |
| 18 | {"compile error", FixtureReproduction, "func Sum(a, b int) int { return a +* b }", CheckCompileError, 0}, |
| 19 | {"wrong shape", FixtureReproduction, "func Add(a, b int) int { return a + b }", CheckCompileError, 0}, |
| 20 | {"no tests", []byte(`[]`), FixtureFixedSource, CheckNoTests, 0}, |
| 21 | {"not a list", []byte(`{"name":"x"}`), FixtureFixedSource, CheckNoTests, 0}, |
| 22 | {"unknown field", []byte(`[{"name":"x","a":1,"b":1,"want":2,"timeout":5}]`), FixtureFixedSource, CheckNoTests, 0}, |
| 23 | {"all skipped", []byte(`[{"name":"x","a":1,"b":1,"want":2,"skip":true}]`), FixtureFixedSource, CheckSkipped, 0}, |
| 24 | {"precedence", []byte(`[{"name":"x","a":2,"b":3,"want":11}]`), "func Sum(a, b int) int { return a + b * 3 }", CheckPass, 1}, |
| 25 | } |
| 26 | for _, c := range cases { |
| 27 | cr := RunCheck(c.repro, []byte(c.subject)) |
| 28 | if cr.Outcome != c.want || cr.TestsRun != c.run { |
| 29 | t.Errorf("%s: %+v", c.name, cr) |
| 30 | } |
| 31 | if cr.ReproductionHash != hashBytes(c.repro) || cr.SubjectHash != hashBytes([]byte(c.subject)) || cr.CheckDefinitionHash != CheckDefinitionHash { |
| 32 | t.Errorf("%s: hashes %+v", c.name, cr) |
| 33 | } |
| 34 | } |
| 35 | if cr := RunCheck(FixtureReproduction, []byte(FixtureBaseSource)); !strings.Contains(cr.FailureMessage, FixtureSymptom) { |
| 36 | t.Fatalf("symptom missing from %q", cr.FailureMessage) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestArtifactRegistrationRules(t *testing.T) { |
| 41 | e := newEnv(t) |
| 42 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 43 | h := e.claim(scope, "reproduce", "runner-a", "ws-1") |
| 44 | prov := Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-1"} |
| 45 | spec := func(role Role, payload []byte) ArtifactSpec { |
| 46 | return ArtifactSpec{Role: role, MediaType: "application/json", Payload: payload, Provenance: prov} |
| 47 | } |
| 48 | _, err := e.store.Register(e.ctx, scope, h, spec(RoleCandidate, []byte("x"))) |
| 49 | wantCode(t, err, CodeScope) |
| 50 | _, err = e.store.Register(e.ctx, scope, h, spec("transcript", []byte("x"))) |
| 51 | wantCode(t, err, CodeUnknownKind) |
| 52 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleReproduction, nil)) |
| 53 | wantCode(t, err, CodeSize) |
| 54 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleReproduction, make([]byte, MaxArtifactBytes+1))) |
| 55 | wantCode(t, err, CodeSize) |
| 56 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleReproduction, MediaType: "application/json", Payload: FixtureReproduction, Provenance: Provenance{Producer: "r"}}) |
| 57 | wantCode(t, err, CodeInvalid) |
| 58 | // A base result before any reproduction, or under another definition, is refused. |
| 59 | cr := RunCheck(FixtureReproduction, []byte(FixtureBaseSource)) |
| 60 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleBaseResult, mustJSON(cr))) |
| 61 | wantCode(t, err, CodeEvidence) |
| 62 | repro, err := e.store.Register(e.ctx, scope, h, spec(RoleReproduction, FixtureReproduction)) |
| 63 | must(t, err) |
| 64 | if repro.Hash != hashBytes(FixtureReproduction) || repro.Fence != h.Fence { |
| 65 | t.Fatalf("artifact %+v", repro) |
| 66 | } |
| 67 | other := cr |
| 68 | other.CheckDefinitionHash = hashBytes([]byte("relaxed")) |
| 69 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleBaseResult, mustJSON(other))) |
| 70 | wantCode(t, err, CodeEvidence) |
| 71 | other = cr |
| 72 | other.SubjectHash = hashBytes([]byte("not base")) |
| 73 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleBaseResult, mustJSON(other))) |
| 74 | wantCode(t, err, CodeEvidence) |
| 75 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleBaseResult, []byte(`{"reproduction_hash":"x","outcome":"fail","extra":1}`))) |
| 76 | wantCode(t, err, CodeInvalid) |
| 77 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleBaseResult, []byte(`{"reproduction_hash":"`+repro.Hash+`","subject_hash":"x","check_definition_hash":"`+CheckDefinitionHash+`","outcome":"flaky","tests_run":1,"tests_failed":1}`))) |
| 78 | wantCode(t, err, CodeUnknownKind) |
| 79 | // Nothing was written by the refusals. |
| 80 | arts, _ := e.store.Artifacts(e.ctx, scope) |
| 81 | if len(arts) != 1 { |
| 82 | t.Fatalf("%d artifacts", len(arts)) |
| 83 | } |
| 84 | _, err = e.store.Register(e.ctx, scope, h, spec(RoleBaseResult, mustJSON(cr))) |
| 85 | must(t, err) |
| 86 | must(t, e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 87 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| 88 | // A candidate freezes the reproduction, even for a reproduce attempt on another job graph. |
| 89 | hi := e.claim(scope, "implement", "runner-a", "ws-2") |
| 90 | cand, err := e.store.Register(e.ctx, scope, hi, ArtifactSpec{Role: RoleCandidate, MediaType: "text/x-fixture", Payload: []byte(FixtureFixedSource), Provenance: prov}) |
| 91 | must(t, err) |
| 92 | must(t, e.store.Complete(e.ctx, scope, hi, AttemptResult{Outcome: OutcomeSuccess})) |
| 93 | must(t, e.store.Contain(e.ctx, scope, hi.Attempt, "s")) |
| 94 | hr := e.claim(scope, "review", "runner-b", "ws-3") |
| 95 | _, err = e.store.Register(e.ctx, scope, hr, ArtifactSpec{Role: RoleReview, MediaType: "application/json", Payload: mustJSON(Review{SubjectHash: hashBytes([]byte("old")), Verdict: VerdictApprove}), |
| 96 | Provenance: Provenance{Producer: "runner-b", Provider: "fake-b", Context: "ctx-3"}}) |
| 97 | wantCode(t, err, CodeEvidence) |
| 98 | _, err = e.store.Register(e.ctx, scope, hr, ArtifactSpec{Role: RoleReview, MediaType: "application/json", Payload: mustJSON(Review{SubjectHash: cand.Hash, Verdict: "maybe"}), |
| 99 | Provenance: Provenance{Producer: "runner-b", Provider: "fake-b", Context: "ctx-3"}}) |
| 100 | wantCode(t, err, CodeUnknownKind) |
| 101 | // Same provider as the candidate: not independent under the pilot policy. |
| 102 | _, err = e.store.Register(e.ctx, scope, hr, ArtifactSpec{Role: RoleReview, MediaType: "application/json", Payload: mustJSON(Review{SubjectHash: cand.Hash, Verdict: VerdictApprove}), |
| 103 | Provenance: Provenance{Producer: "runner-b", Provider: "fake-a", Context: "ctx-3"}}) |
| 104 | wantCode(t, err, CodeEvidence) |
| 105 | } |
| 106 | |
| 107 | func TestPredicateRejectsInvalidEvidence(t *testing.T) { |
| 108 | e := newEnv(t) |
| 109 | prov := Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-1"} |
| 110 | run := func(name string, base CheckResult, symptom string) EvidenceReport { |
| 111 | spec := softwareSpec(SoftwareSteps(false)) |
| 112 | if symptom != "" { |
| 113 | spec.Evidence.ExpectedSymptom = symptom |
| 114 | } |
| 115 | _, scope := e.createJob(spec) |
| 116 | h := e.claim(scope, "reproduce", "runner-a", "ws-"+name) |
| 117 | _, err := e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleReproduction, MediaType: "application/json", Payload: FixtureReproduction, Provenance: prov}) |
| 118 | must(t, err) |
| 119 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleBaseResult, MediaType: "application/json", Payload: mustJSON(base), Provenance: prov}) |
| 120 | must(t, err) |
| 121 | must(t, e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeSuccess})) |
| 122 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| 123 | rep, err := e.store.Conclude(e.ctx, scope, "test") |
| 124 | wantCode(t, err, CodeEvidence) |
| 125 | if j := e.job(scope); j.State != JobFailed { |
| 126 | t.Fatalf("%s: job %s", name, j.State) |
| 127 | } |
| 128 | return rep |
| 129 | } |
| 130 | good := RunCheck(FixtureReproduction, []byte(FixtureBaseSource)) |
| 131 | compile := good |
| 132 | compile.Outcome, compile.TestsRun, compile.TestsFailed = CheckCompileError, 0, 0 |
| 133 | if rep := run("compile", compile, ""); !strings.Contains(rep.Reason, "compile_error") { |
| 134 | t.Fatalf("compile: %s", rep.Reason) |
| 135 | } |
| 136 | skipped := good |
| 137 | skipped.Outcome, skipped.TestsRun, skipped.TestsFailed = CheckSkipped, 0, 0 |
| 138 | if rep := run("skipped", skipped, ""); !strings.Contains(rep.Reason, "skipped") { |
| 139 | t.Fatalf("skipped: %s", rep.Reason) |
| 140 | } |
| 141 | zero := good |
| 142 | zero.TestsRun, zero.TestsFailed = 0, 0 |
| 143 | if rep := run("zero", zero, ""); !strings.Contains(rep.Reason, "0 run") { |
| 144 | t.Fatalf("zero: %s", rep.Reason) |
| 145 | } |
| 146 | passing := good |
| 147 | passing.Outcome, passing.TestsFailed, passing.FailureMessage = CheckPass, 0, "" |
| 148 | if rep := run("pass", passing, ""); !strings.Contains(rep.Reason, "expected a failing test") { |
| 149 | t.Fatalf("pass: %s", rep.Reason) |
| 150 | } |
| 151 | if rep := run("symptom", good, "panic: index out of range"); !strings.Contains(rep.Reason, "symptom") { |
| 152 | t.Fatalf("symptom: %s", rep.Reason) |
| 153 | } |
| 154 | if rep := run("no-candidate", good, ""); rep.Reason != "no candidate" { |
| 155 | t.Fatalf("no candidate: %s", rep.Reason) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestArtifactsOfUnfinishedAttemptsAreNotEvidence(t *testing.T) { |
| 160 | e := newEnv(t) |
| 161 | _, scope := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 162 | w := e.workflow(scope) |
| 163 | _, _, err := w.Reproduce(e.ctx, []byte(FixtureBaseSource)) |
| 164 | must(t, err) |
| 165 | h := e.claim(scope, "implement", "runner-a", "ws-1") |
| 166 | _, err = e.store.Register(e.ctx, scope, h, ArtifactSpec{Role: RoleCandidate, MediaType: "text/x-fixture", Payload: []byte(FixtureFixedSource), |
| 167 | Provenance: Provenance{Producer: "runner-a", Provider: "fake-a", Context: "ctx-1"}}) |
| 168 | must(t, err) |
| 169 | must(t, e.store.Complete(e.ctx, scope, h, AttemptResult{Outcome: OutcomeFailure, Detail: "gave up"})) |
| 170 | must(t, e.store.Contain(e.ctx, scope, h.Attempt, "s")) |
| 171 | rep, err := e.store.Conclude(e.ctx, scope, "test") |
| 172 | wantCode(t, err, CodeEvidence) |
| 173 | if rep.Reason != "no candidate" { |
| 174 | t.Fatalf("reason %q", rep.Reason) |
| 175 | } |
| 176 | // Conclude refuses while an attempt is live. |
| 177 | _, scope2 := e.createJob(softwareSpec(SoftwareSteps(false))) |
| 178 | e.claim(scope2, "reproduce", "runner-a", "ws-1") |
| 179 | _, err = e.store.Conclude(e.ctx, scope2, "test") |
| 180 | wantCode(t, err, CodeState) |
| 181 | _, err = e.store.Conclude(e.ctx, scope, "test") |
| 182 | wantCode(t, err, CodeState) |
| 183 | } |
| 184 | |