oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package checks
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/hex"
7 "encoding/json"
8 "hash"
9 "io"
10 "strings"
11
12 "amadan.net/oficina/equipo/internal/runner/provider"
13)
14
15const logLimit = 64 * 1024
16
17type boundedLog struct {
18 bytes.Buffer
19 hash hash.Hash
20}
21
22func (l *boundedLog) Write(p []byte) (int, error) {
23 if l.hash == nil {
24 l.hash = sha256.New()
25 }
26 _, _ = l.hash.Write(p)
27 n := len(p)
28 if left := logLimit - l.Len(); left > 0 {
29 _, _ = l.Buffer.Write(p[:min(left, n)])
30 }
31 return n, nil
32}
33func (l *boundedLog) digest() string {
34 if l.hash == nil {
35 return Digest(nil)
36 }
37 return hex.EncodeToString(l.hash.Sum(nil))
38}
39
40type testState struct {
41 done, failed, matched bool
42 tail string
43}
44type goResults struct {
45 pending []byte
46 log boundedLog
47 bad bool
48 packages map[string]string
49 noTests map[string]bool
50 testEvents map[string]bool
51 tests map[TestIdentity]*testState
52 names []TestIdentity
53 run, failed int
54 symptom string
55 matched bool
56}
57
58func newGoResults() *goResults {
59 return &goResults{packages: map[string]string{}, noTests: map[string]bool{}, testEvents: map[string]bool{}, tests: map[TestIdentity]*testState{}}
60}
61func (g *goResults) Write(p []byte) (int, error) {
62 n := len(p)
63 for len(p) > 0 {
64 i := bytes.IndexByte(p, '\n')
65 if i < 0 {
66 i = len(p) - 1
67 }
68 chunk := p[:i+1]
69 p = p[i+1:]
70 if len(g.pending)+len(chunk) > 1024*1024 {
71 g.bad = true
72 g.pending = nil
73 } else {
74 g.pending = append(g.pending, chunk...)
75 if chunk[len(chunk)-1] == '\n' {
76 g.event(g.pending)
77 g.pending = nil
78 }
79 }
80 }
81 // Parsing always precedes bounded retention.
82 // The caller's full chunk is hashed separately by the command collector.
83 return n, nil
84}
85func (g *goResults) event(line []byte) {
86 defer g.log.Write(line)
87 var e struct{ Action, Package, Test, Output string }
88 if json.Unmarshal(line, &e) != nil || e.Package == "" {
89 g.bad = true
90 return
91 }
92 id := TestIdentity{Package: e.Package, Name: e.Test}
93 if e.Test == "" {
94 switch e.Action {
95 case "start":
96 if _, ok := g.packages[e.Package]; ok {
97 g.bad = true
98 }
99 g.packages[e.Package] = "running"
100 case "pass", "fail":
101 if g.packages[e.Package] != "running" {
102 g.bad = true
103 }
104 g.packages[e.Package] = e.Action
105 case "output":
106 fields := strings.Fields(e.Output)
107 if len(fields) == 5 && fields[0] == "?" && fields[1] == e.Package && strings.Join(fields[2:], " ") == "[no test files]" && g.packages[e.Package] == "running" {
108 g.noTests[e.Package] = true
109 }
110 case "skip":
111 if g.packages[e.Package] != "running" || !g.noTests[e.Package] {
112 g.bad = true
113 }
114 if g.testEvents[e.Package] {
115 g.bad = true
116 }
117 g.packages[e.Package] = "no-tests"
118 default:
119 g.bad = true
120 }
121 return
122 }
123 g.testEvents[e.Package] = true
124 switch e.Action {
125 case "run":
126 if g.packages[e.Package] != "running" || g.tests[id] != nil {
127 g.bad = true
128 return
129 }
130 g.tests[id] = &testState{}
131 g.run++
132 g.names = append(g.names, id)
133 case "pass", "fail":
134 s := g.tests[id]
135 if s == nil || s.done {
136 g.bad = true
137 return
138 }
139 s.done = true
140 s.failed = e.Action == "fail"
141 if s.failed {
142 g.failed++
143 g.matched = g.matched || s.matched
144 }
145 case "output":
146 if s := g.tests[id]; s != nil && g.symptom != "" {
147 text := s.tail + e.Output
148 s.matched = s.matched || strings.Contains(text, g.symptom)
149 if len(text) > len(g.symptom) {
150 text = text[len(text)-len(g.symptom):]
151 }
152 s.tail = text
153 }
154 case "pause", "cont":
155 if g.tests[id] == nil {
156 g.bad = true
157 }
158 default:
159 g.bad = true
160 }
161}
162func (g *goResults) finish(exit int, err error) error {
163 if err != nil {
164 return err
165 }
166 if g.bad || len(g.pending) != 0 || g.run == 0 || len(g.packages) == 0 || exit < 0 {
167 return provider.ErrContract
168 }
169 for _, s := range g.tests {
170 if !s.done {
171 return provider.ErrContract
172 }
173 }
174 for p, status := range g.packages {
175 if status == "no-tests" {
176 continue
177 }
178 if status != "pass" && status != "fail" {
179 return provider.ErrContract
180 }
181 found := false
182 failed := false
183 for id, s := range g.tests {
184 if id.Package == p {
185 found = true
186 failed = failed || s.failed
187 }
188 }
189 if !found || status == "fail" && !failed || status == "pass" && failed {
190 return provider.ErrContract
191 }
192 }
193 if exit == 0 && g.failed > 0 || exit != 0 && g.failed == 0 {
194 return provider.ErrContract
195 }
196 return nil
197}
198
199var _ io.Writer = (*goResults)(nil)
200