-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunbundle_test.go
More file actions
320 lines (282 loc) · 10.3 KB
/
Copy pathrunbundle_test.go
File metadata and controls
320 lines (282 loc) · 10.3 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
package testrunner
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestCreateRunBundle_CreatesDirectoryAndFiles(t *testing.T) {
tmpDir := t.TempDir()
// Write a minimal scenario file.
scenarioContent := "name: test-bundle\ntimeout: 1m\nphases:\n- name: test\n actions:\n - action: print\n msg: hello\n"
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte(scenarioContent), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, []string{"run", "test.yaml"})
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
// Run directory exists.
if _, err := os.Stat(bundle.Dir); err != nil {
t.Fatalf("run dir missing: %v", err)
}
// Artifacts subdirectory exists.
if _, err := os.Stat(bundle.ArtifactsDir()); err != nil {
t.Fatalf("artifacts dir missing: %v", err)
}
// manifest.json exists and is valid.
manifestData, err := os.ReadFile(filepath.Join(bundle.Dir, "manifest.json"))
if err != nil {
t.Fatalf("read manifest: %v", err)
}
var manifest RunManifest
if err := json.Unmarshal(manifestData, &manifest); err != nil {
t.Fatalf("parse manifest: %v", err)
}
if manifest.RunID == "" {
t.Error("RunID is empty")
}
if manifest.ScenarioName != "test-bundle" {
t.Errorf("ScenarioName = %q, want test-bundle", manifest.ScenarioName)
}
if manifest.ScenarioSHA256 == "" {
t.Error("ScenarioSHA256 is empty")
}
if manifest.StartedAt == "" {
t.Error("StartedAt is empty")
}
// scenario.yaml is a frozen copy.
copied, err := os.ReadFile(filepath.Join(bundle.Dir, "scenario.yaml"))
if err != nil {
t.Fatalf("read scenario copy: %v", err)
}
if string(copied) != scenarioContent {
t.Errorf("scenario copy mismatch: got %q", string(copied))
}
// Run ID matches directory name.
dirName := filepath.Base(bundle.Dir)
if dirName != manifest.RunID {
t.Errorf("dir name %q != RunID %q", dirName, manifest.RunID)
}
}
func TestRunBundle_Finalize_WritesAllOutputs(t *testing.T) {
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: finalize-test\ntimeout: 1m\nphases:\n- name: test\n actions:\n - action: print\n msg: hello\n"), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, []string{"run"})
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
result := &ScenarioResult{
Name: "finalize-test",
Status: StatusPass,
Duration: 5 * time.Second,
Phases: []PhaseResult{
{Name: "setup", Status: StatusPass, Duration: 1 * time.Second},
},
}
if err := bundle.Finalize(result); err != nil {
t.Fatalf("Finalize: %v", err)
}
// result.json exists.
if _, err := os.Stat(filepath.Join(bundle.Dir, "result.json")); err != nil {
t.Error("result.json missing")
}
// result.xml exists.
if _, err := os.Stat(filepath.Join(bundle.Dir, "result.xml")); err != nil {
t.Error("result.xml missing")
}
// result.html exists.
if _, err := os.Stat(filepath.Join(bundle.Dir, "result.html")); err != nil {
t.Error("result.html missing")
}
// manifest.json updated with FinishedAt and Status.
manifestData, _ := os.ReadFile(filepath.Join(bundle.Dir, "manifest.json"))
var manifest RunManifest
json.Unmarshal(manifestData, &manifest)
if manifest.FinishedAt == "" {
t.Error("FinishedAt not set after Finalize")
}
if manifest.Status != "PASS" {
t.Errorf("Status = %q, want PASS", manifest.Status)
}
}
func TestRunBundle_UniqueRunIDs(t *testing.T) {
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: unique-test\ntimeout: 1m\nphases:\n- name: test\n actions:\n - action: print\n msg: hello\n"), 0644)
ids := make(map[string]bool)
for i := 0; i < 10; i++ {
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, nil)
if err != nil {
t.Fatalf("iteration %d: %v", i, err)
}
id := bundle.Manifest.RunID
if ids[id] {
t.Fatalf("duplicate RunID: %s", id)
}
ids[id] = true
}
}
func TestRunBundle_Finalize_WritesProvenanceJSON(t *testing.T) {
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: prov-test\ntimeout: 1m\nphases:\n- name: p\n actions:\n - action: print\n msg: hi\n"), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, []string{"run"})
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
bundle.RecordBinary(ProvBinary{Path: "/tmp/blockmaster", SHA256: "abcd1234", Package: "./cmd/blockmaster"})
bundle.RecordBinary(ProvBinary{Path: "/tmp/blockvolume", SHA256: "ef567890", Package: "./cmd/blockvolume", Node: "m02"})
bundle.RecordImage(ProvImage{Tag: "sw-block:local", Digest: "sha256:8865ebbb", BuiltBy: "build_block"})
if err := bundle.Finalize(&ScenarioResult{Name: "prov-test", Status: StatusPass}); err != nil {
t.Fatalf("Finalize: %v", err)
}
provData, err := os.ReadFile(filepath.Join(bundle.Dir, "provenance.json"))
if err != nil {
t.Fatalf("read provenance.json: %v", err)
}
var prov Provenance
if err := json.Unmarshal(provData, &prov); err != nil {
t.Fatalf("parse provenance.json: %v", err)
}
if prov.RunID != bundle.Manifest.RunID {
t.Errorf("RunID = %q, want %q", prov.RunID, bundle.Manifest.RunID)
}
if prov.Scenario.Name != "prov-test" {
t.Errorf("Scenario.Name = %q, want prov-test", prov.Scenario.Name)
}
if prov.Scenario.SHA256 == "" {
t.Error("Scenario.SHA256 empty")
}
if len(prov.Binaries) != 2 {
t.Fatalf("Binaries count = %d, want 2", len(prov.Binaries))
}
if prov.Binaries[0].Path != "/tmp/blockmaster" || prov.Binaries[0].SHA256 != "abcd1234" {
t.Errorf("Binaries[0] = %+v", prov.Binaries[0])
}
if prov.Binaries[1].Node != "m02" {
t.Errorf("Binaries[1].Node = %q, want m02", prov.Binaries[1].Node)
}
if len(prov.Images) != 1 || prov.Images[0].Tag != "sw-block:local" {
t.Errorf("Images = %+v", prov.Images)
}
if prov.Host.Arch == "" {
t.Error("Host.Arch empty (runtime.GOARCH should always be set)")
}
}
func TestRunBundle_DirAndArtifactsDirAccessible(t *testing.T) {
// CLI injects bundle.Dir and bundle.ArtifactsDir() into scenario.Env
// as bundle_dir / artifacts_dir. Verify those paths exist and the
// artifacts dir is a subdir of the bundle dir.
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: dir-test\nphases:\n- name: p\n actions:\n - action: print\n msg: hi\n"), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, nil)
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
if _, err := os.Stat(bundle.Dir); err != nil {
t.Errorf("bundle.Dir not a real directory: %v", err)
}
if _, err := os.Stat(bundle.ArtifactsDir()); err != nil {
t.Errorf("bundle.ArtifactsDir() not a real directory: %v", err)
}
if !strings.HasPrefix(bundle.ArtifactsDir(), bundle.Dir) {
t.Errorf("ArtifactsDir %q not a subdir of Dir %q", bundle.ArtifactsDir(), bundle.Dir)
}
}
func TestRunBundle_Finalize_RedactsResultJSONVars(t *testing.T) {
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: redact-test\nphases:\n- name: p\n actions:\n - action: print\n msg: hi\n"), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, []string{"run"})
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
result := &ScenarioResult{
Name: "redact-test",
Status: StatusPass,
Vars: map[string]string{
"chap_secret": "VERY-SECRET",
"target_iqn": "iqn.example",
"api_token": "Bearer xyz",
"host": "m02",
},
}
if err := bundle.Finalize(result); err != nil {
t.Fatalf("Finalize: %v", err)
}
data, err := os.ReadFile(filepath.Join(bundle.Dir, "result.json"))
if err != nil {
t.Fatalf("read result.json: %v", err)
}
body := string(data)
for _, leaked := range []string{"VERY-SECRET", "Bearer xyz"} {
if strings.Contains(body, leaked) {
t.Errorf("result.json leaks secret %q:\n%s", leaked, body)
}
}
for _, kept := range []string{"target_iqn", "iqn.example", "host", "m02"} {
if !strings.Contains(body, kept) {
t.Errorf("result.json missing non-secret %q", kept)
}
}
if !strings.Contains(body, RedactedValue) {
t.Errorf("result.json missing redaction marker; body:\n%s", body)
}
// Caller's struct must remain untouched (we copied before redacting).
if result.Vars["chap_secret"] != "VERY-SECRET" {
t.Errorf("Finalize mutated caller's Vars: %q", result.Vars["chap_secret"])
}
}
func TestRunBundle_RecordOnNilBundleIsSafe(t *testing.T) {
// Actions hold a *RunBundle that may be nil (e.g. --no-bundle, unit
// tests). Record* must be no-ops on a nil receiver.
var b *RunBundle
b.RecordBinary(ProvBinary{Path: "/x", SHA256: "y"})
b.RecordImage(ProvImage{Tag: "x:y"})
}
func TestRunBundle_ProvenanceConcurrentRecord(t *testing.T) {
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: race-test\nphases:\n- name: p\n actions:\n - action: print\n msg: hi\n"), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile, nil)
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
const N = 50
done := make(chan struct{})
for i := 0; i < N; i++ {
go func(i int) {
bundle.RecordBinary(ProvBinary{Path: "/p", SHA256: "s"})
bundle.RecordImage(ProvImage{Tag: "t"})
done <- struct{}{}
_ = i
}(i)
}
for i := 0; i < N; i++ {
<-done
}
prov := bundle.Provenance()
if len(prov.Binaries) != N {
t.Errorf("Binaries len = %d, want %d", len(prov.Binaries), N)
}
if len(prov.Images) != N {
t.Errorf("Images len = %d, want %d", len(prov.Images), N)
}
}
func TestRunBundle_CommandLineRecorded(t *testing.T) {
tmpDir := t.TempDir()
scenarioFile := filepath.Join(tmpDir, "test.yaml")
os.WriteFile(scenarioFile, []byte("name: cmd-test\ntimeout: 1m\nphases:\n- name: test\n actions:\n - action: print\n msg: hello\n"), 0644)
bundle, err := CreateRunBundle(filepath.Join(tmpDir, "results"), scenarioFile,
[]string{"sw-test-runner", "run", "--tiers", "block", "test.yaml"})
if err != nil {
t.Fatalf("CreateRunBundle: %v", err)
}
if !strings.Contains(bundle.Manifest.CommandLine, "--tiers") {
t.Errorf("CommandLine = %q, want to contain --tiers", bundle.Manifest.CommandLine)
}
}