-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundlevalidate.go
More file actions
356 lines (329 loc) · 10.9 KB
/
Copy pathbundlevalidate.go
File metadata and controls
356 lines (329 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package testrunner
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
)
// BundleValidationOptions controls offline validation of a completed
// run/suite bundle. It intentionally works on loose JSON maps so product-owned
// suite wrappers can add fields without requiring a platform schema release.
type BundleValidationOptions struct {
RequirePass bool
RequireTiming bool
RequireChildBundles bool
ExpectScenario string
ExpectCommitPrefix string
ExpectedChildren []string
}
// BundleValidationReport is a machine-readable summary of validate-bundle.
type BundleValidationReport struct {
Dir string `json:"dir"`
OK bool `json:"ok"`
RunID string `json:"run_id,omitempty"`
Scenario string `json:"scenario,omitempty"`
Status string `json:"status,omitempty"`
ProductCommit string `json:"product_commit,omitempty"`
Errors []string `json:"errors,omitempty"`
}
// ValidateBundle verifies the result/status metadata for a run bundle without
// touching the test lab. It is the platform-level counterpart to product-side
// post-run artifact inspection.
func ValidateBundle(dir string, opts BundleValidationOptions) (*BundleValidationReport, error) {
result, err := readJSONMap(filepath.Join(dir, "result.json"))
if err != nil {
return nil, err
}
status, err := readJSONMap(filepath.Join(dir, "status.json"))
if err != nil {
return nil, err
}
manifest, _ := readJSONMap(filepath.Join(dir, "manifest.json"))
provenance, _ := readJSONMap(filepath.Join(dir, "provenance.json"))
report := &BundleValidationReport{
Dir: dir,
RunID: firstStringIn([]map[string]any{result, status}, "run_id"),
Scenario: firstStringIn([]map[string]any{result, status, manifest}, "scenario", "name", "scenario_name"),
Status: firstStringIn([]map[string]any{result, status, manifest}, "status", "state"),
ProductCommit: firstCommit(result, status, manifest, provenance),
}
var errors []string
resultRunID := stringField(result, "run_id")
statusRunID := stringField(status, "run_id")
if resultRunID == "" || statusRunID == "" || resultRunID != statusRunID {
errors = append(errors, fmt.Sprintf("run_id differs between result.json and status.json: %q vs %q", resultRunID, statusRunID))
}
if r, s := firstStringIn([]map[string]any{result}, "scenario", "name"), firstStringIn([]map[string]any{status}, "scenario"); r != "" && s != "" && r != s {
errors = append(errors, fmt.Sprintf("scenario differs between result.json and status.json: %q vs %q", r, s))
}
if opts.ExpectScenario != "" && report.Scenario != opts.ExpectScenario {
errors = append(errors, fmt.Sprintf("scenario %q does not match expected %q", report.Scenario, opts.ExpectScenario))
}
resultStatus := normalizeBundleStatus(firstStringIn([]map[string]any{result}, "status"))
statusState := normalizeBundleStatus(firstStringIn([]map[string]any{status}, "state", "status"))
if resultStatus != "" && statusState != "" && resultStatus != statusState {
errors = append(errors, fmt.Sprintf("result/status terminal state differs: %q vs %q", resultStatus, statusState))
}
if opts.RequirePass {
if resultStatus != "pass" {
errors = append(errors, fmt.Sprintf("result status is %q, expected pass", firstStringIn([]map[string]any{result}, "status")))
}
if statusState != "pass" {
errors = append(errors, fmt.Sprintf("status state is %q, expected pass", firstStringIn([]map[string]any{status}, "state", "status")))
}
}
if opts.RequireTiming {
requireTiming("result.json", result, opts.RequirePass, &errors)
requireTiming("status.json", status, opts.RequirePass, &errors)
}
if opts.ExpectCommitPrefix != "" {
var candidates []string
collectCommitCandidates(&candidates, result)
collectCommitCandidates(&candidates, status)
collectCommitCandidates(&candidates, manifest)
collectCommitCandidates(&candidates, provenance)
matched := false
for _, c := range candidates {
if strings.HasPrefix(c, opts.ExpectCommitPrefix) {
matched = true
break
}
}
if !matched {
errors = append(errors, fmt.Sprintf("no commit field matches prefix %q (candidates=%v)", opts.ExpectCommitPrefix, candidates))
}
}
if len(opts.ExpectedChildren) > 0 {
validateChildBundles(dir, result, status, opts, &errors)
} else if opts.RequirePass {
done, doneOK := intField(status, "phases_done")
total, totalOK := intField(status, "phases_total")
if doneOK && totalOK && done != total {
errors = append(errors, fmt.Sprintf("phases_done=%d phases_total=%d", done, total))
}
}
report.Status = coalesceStatus(resultStatus, statusState, report.Status)
report.Errors = errors
report.OK = len(errors) == 0
return report, nil
}
func validateChildBundles(root string, result, status map[string]any, opts BundleValidationOptions, errors *[]string) {
resultPhases, err := namedObjects(result, "phase_results")
if err != nil {
*errors = append(*errors, err.Error())
return
}
statusPhases, err := namedObjects(status, "phases")
if err != nil {
*errors = append(*errors, err.Error())
return
}
if got := resultPhases.order; !sameStringSlice(got, opts.ExpectedChildren) {
*errors = append(*errors, fmt.Sprintf("result child order mismatch: got %v want %v", got, opts.ExpectedChildren))
}
if got := statusPhases.order; !sameStringSlice(got, opts.ExpectedChildren) {
*errors = append(*errors, fmt.Sprintf("status child order mismatch: got %v want %v", got, opts.ExpectedChildren))
}
for _, child := range opts.ExpectedChildren {
rp, rok := resultPhases.Get(child)
sp, sok := statusPhases.Get(child)
if !rok || !sok {
*errors = append(*errors, fmt.Sprintf("%s: missing in result/status phases", child))
continue
}
rs := normalizeBundleStatus(firstStringIn([]map[string]any{rp}, "status", "state"))
ss := normalizeBundleStatus(firstStringIn([]map[string]any{sp}, "status", "state"))
if rs != "" && ss != "" && rs != ss {
*errors = append(*errors, fmt.Sprintf("%s: status differs between result/status: %q vs %q", child, rs, ss))
}
if opts.RequirePass && rs != "pass" {
*errors = append(*errors, fmt.Sprintf("%s: status is %q, expected pass", child, firstStringIn([]map[string]any{rp}, "status", "state")))
}
runID := stringField(rp, "run_id")
statusRunID := stringField(sp, "run_id")
if runID == "" || statusRunID == "" || runID != statusRunID {
*errors = append(*errors, fmt.Sprintf("%s: run_id differs between result/status: %q vs %q", child, runID, statusRunID))
}
if done, ok := intField(rp, "phases_done"); ok {
if total, ok := intField(rp, "phases_total"); ok && opts.RequirePass && done != total {
*errors = append(*errors, fmt.Sprintf("%s: phases_done=%d phases_total=%d", child, done, total))
}
}
if opts.RequireChildBundles && runID != "" {
// Validate the bundle being inspected, not only an absolute run_dir
// pointer captured when the suite originally ran. Operators often
// copy bundles for negative tests or archival; stale absolute paths
// must not make a broken copy look valid.
runDir := filepath.Join(root, child, "runs", runID)
for _, name := range []string{"status.json", "result.json"} {
if _, err := os.Stat(filepath.Join(runDir, name)); err != nil {
*errors = append(*errors, fmt.Sprintf("%s: missing child %s at %s", child, name, runDir))
}
}
}
}
}
type orderedObjects struct {
order []string
data map[string]map[string]any
}
func namedObjects(doc map[string]any, field string) (orderedObjects, error) {
raw, ok := doc[field]
if !ok {
return orderedObjects{}, fmt.Errorf("%s missing", field)
}
items, ok := raw.([]any)
if !ok {
return orderedObjects{}, fmt.Errorf("%s must be a list", field)
}
out := orderedObjects{data: map[string]map[string]any{}}
for _, item := range items {
obj, ok := item.(map[string]any)
if !ok {
return orderedObjects{}, fmt.Errorf("%s entries must be objects", field)
}
name := stringField(obj, "name")
if name == "" {
return orderedObjects{}, fmt.Errorf("%s entry missing name", field)
}
out.order = append(out.order, name)
out.data[name] = obj
}
return out, nil
}
func (o orderedObjects) Get(name string) (map[string]any, bool) {
v, ok := o.data[name]
return v, ok
}
func readJSONMap(path string) (map[string]any, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read %s: %w", path, err)
}
var out map[string]any
if err := json.Unmarshal(data, &out); err != nil {
return nil, fmt.Errorf("parse %s: %w", path, err)
}
return out, nil
}
func firstStringIn(docs []map[string]any, keys ...string) string {
for _, doc := range docs {
for _, key := range keys {
if v := stringField(doc, key); v != "" {
return v
}
}
}
return ""
}
func stringField(doc map[string]any, key string) string {
if doc == nil {
return ""
}
if v, ok := doc[key].(string); ok {
return v
}
return ""
}
func intField(doc map[string]any, key string) (int, bool) {
switch v := doc[key].(type) {
case float64:
return int(v), true
case int:
return v, true
default:
return 0, false
}
}
func normalizeBundleStatus(s string) string {
switch strings.ToLower(s) {
case "pass", "passed":
return "pass"
case "fail", "failed":
return "fail"
case "cancelled", "canceled":
return "cancelled"
case "error":
return "error"
case "running", "queued", "pending":
return strings.ToLower(s)
default:
return strings.ToLower(s)
}
}
func requireTiming(label string, doc map[string]any, requireEnded bool, errors *[]string) {
if stringField(doc, "started_at") == "" {
*errors = append(*errors, label+" missing started_at")
}
if requireEnded && stringField(doc, "ended_at") == "" {
*errors = append(*errors, label+" missing ended_at")
}
value, ok := doc["wall_clock_s"]
if !ok {
*errors = append(*errors, label+" missing wall_clock_s")
return
}
if !positiveNumber(value) {
*errors = append(*errors, label+" wall_clock_s must be > 0")
}
}
func positiveNumber(v any) bool {
switch n := v.(type) {
case float64:
return n > 0
case int:
return n > 0
case int64:
return n > 0
case json.Number:
f, err := n.Float64()
return err == nil && f > 0
default:
return false
}
}
func collectCommitCandidates(out *[]string, doc map[string]any) {
if doc == nil {
return
}
for _, key := range []string{"product_commit", "source_commit", "git_sha"} {
if v := stringField(doc, key); v != "" {
*out = append(*out, v)
}
}
if gitObj, ok := doc["git"].(map[string]any); ok {
if v := stringField(gitObj, "sha"); v != "" {
*out = append(*out, v)
}
}
}
func firstCommit(docs ...map[string]any) string {
var candidates []string
for _, doc := range docs {
collectCommitCandidates(&candidates, doc)
}
if len(candidates) == 0 {
return ""
}
return candidates[0]
}
func coalesceStatus(vals ...string) string {
for _, v := range vals {
if v != "" {
return v
}
}
return ""
}
func sameStringSlice(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}