forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmounts_test.go
530 lines (477 loc) · 19 KB
/
mounts_test.go
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
package main
import (
"encoding/json"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"github.com/taskcluster/slugid-go/slugid"
"github.com/taskcluster/taskcluster/v40/workers/generic-worker/gwconfig"
)
func TestMissingScopes(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "SampleArtifacts/_/X.txt", "SampleArtifacts/_/X.txt")
// Create a new task to mount the artifact without the scope to do so
mounts := []MountEntry{
// requires scope "queue:get-artifact:SampleArtifacts/_/X.txt"
&FileMount{
File: filepath.Join("preloaded", "Mr X.txt"),
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "SampleArtifacts/_/X.txt"
}`),
},
// requires scope "generic-worker:cache:banana-cache"
&WritableDirectoryCache{
CacheName: "banana-cache",
Directory: filepath.Join("my-task-caches", "bananas"),
},
}
payload := GenericWorkerPayload{
Mounts: toMountArray(t, &mounts),
Command: helloGoodbye(),
MaxRunTime: 180,
}
td := testTask(t)
td.Dependencies = []string{
taskID,
}
// don't set any scopes
_ = submitAndAssert(t, td, payload, "exception", "malformed-payload")
logtext := LogText(t)
if !strings.Contains(logtext, "queue:get-artifact:SampleArtifacts/_/X.txt") || !strings.Contains(logtext, "generic-worker:cache:banana-cache") {
t.Fatalf("Was expecting log file to contain missing scopes, but it doesn't")
}
}
// TestMissingDependency tests that if artifact content is mounted, it must be included as a task dependency
func TestMissingMountsDependency(t *testing.T) {
defer setup(t)()
pretendTaskID := slugid.Nice()
mounts := []MountEntry{
// requires scope "queue:get-artifact:SampleArtifacts/_/X.txt"
&FileMount{
File: filepath.Join("preloaded", "Mr X.txt"),
// Pretend task
Content: json.RawMessage(`{
"taskId": "` + pretendTaskID + `",
"artifact": "SampleArtifacts/_/X.txt"
}`),
},
// requires scope "generic-worker:cache:banana-cache"
&WritableDirectoryCache{
CacheName: "banana-cache",
Directory: filepath.Join("my-task-caches", "bananas"),
},
}
payload := GenericWorkerPayload{
Mounts: toMountArray(t, &mounts),
Command: helloGoodbye(),
MaxRunTime: 180,
}
td := testTask(t)
td.Scopes = []string{
"generic-worker:cache:banana-cache",
"queue:get-artifact:SampleArtifacts/_/X.txt",
}
_ = submitAndAssert(t, td, payload, "exception", "malformed-payload")
logtext := LogText(t)
if !strings.Contains(logtext, "[mounts] task.dependencies needs to include "+pretendTaskID+" since one or more of its artifacts are mounted") {
t.Fatalf("Was expecting log file to explain that task dependency was missing, but it doesn't: \n%v", logtext)
}
}
func Test32BitOverflow(t *testing.T) {
config = &gwconfig.Config{
PublicConfig: gwconfig.PublicConfig{
RequiredDiskSpaceMegabytes: 1024 * 10,
},
}
if requiredFreeSpace := requiredSpaceBytes(); requiredFreeSpace != 10737418240 {
t.Fatalf("Some kind of int overflow problem: requiredFreeSpace is %v but expected it to be 10737418240", requiredFreeSpace)
}
}
func TestCorruptZipDoesntCrashWorker(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "SampleArtifacts/_/X.txt", "SampleArtifacts/_/X.txt")
mounts := []MountEntry{
// requires scope "queue:get-artifact:SampleArtifacts/_/X.txt"
&ReadOnlyDirectory{
Directory: ".",
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "SampleArtifacts/_/X.txt"
}`),
Format: "zip",
},
}
payload := GenericWorkerPayload{
Mounts: toMountArray(t, &mounts),
Command: helloGoodbye(),
MaxRunTime: 180,
}
td := testTask(t)
td.Dependencies = []string{
taskID,
}
td.Scopes = []string{"queue:get-artifact:SampleArtifacts/_/X.txt"}
_ = submitAndAssert(t, td, payload, "failed", "failed")
logtext := LogText(t)
if !strings.Contains(logtext, "zip: not a valid zip file") {
t.Fatalf("Was expecting log file to contain a zip error message, but it instead contains:\n%v", logtext)
}
}
// TODO: maybe want to create a test where an error artifact is uploaded but
// task is resolved as successful, and then have artifact content that mounts
// the error artifact. This would be a bizarre test case though as it would be
// unusual for a task to resolve successfully if it contains error artifacts -
// although there is nothing stopping a task from publishing error artifacts
// and then resolving successfully - so it could be an attack vector for a
// malicious task.
// TestNonExistentArtifact depends on an artifact that does not exist from a
// task that *does* exist.
func TestNonExistentArtifact(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "SampleArtifacts/_/X.txt", "SampleArtifacts/_/X.txt")
mounts := []MountEntry{
// requires scope "queue:get-artifact:SampleArtifacts/_/X.txt"
&ReadOnlyDirectory{
Directory: ".",
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "SampleArtifacts/_/non-existent-artifact.txt"
}`),
Format: "zip",
},
}
payload := GenericWorkerPayload{
Mounts: toMountArray(t, &mounts),
Command: helloGoodbye(),
MaxRunTime: 180,
}
td := testTask(t)
td.Dependencies = []string{
taskID,
}
td.Scopes = []string{"queue:get-artifact:SampleArtifacts/_/non-existent-artifact.txt"}
_ = submitAndAssert(t, td, payload, "failed", "failed")
logtext := LogText(t)
expectedText := "[mounts] Could not fetch from task " + taskID + " artifact SampleArtifacts/_/non-existent-artifact.txt into file"
if !strings.Contains(logtext, expectedText) {
t.Fatalf("Log did not contain expected text %q:\n%v", expectedText, logtext)
}
}
// We currently don't check for any of these strings:
// [mounts] Could not download %v to %v due to %v
// [mounts] Could not make MkdirAll %v: %v
// [mounts] Could not open file %v: %v
// [mounts] Could not reach purgecache service to see if caches need purging:
// [mounts] Could not write http response from %v to file %v: %v
type MountsLoggingTestCase struct {
Test *testing.T
Mounts []MountEntry
Scopes []string
Dependencies []string
TaskRunResolutionState string
TaskRunReasonResolved string
PerTaskRunLogExcerpts [][]string
Payload *GenericWorkerPayload
}
// This is an extremely strict test helper, that requires you to specify
// extracts from every log line that the mounts feature writes to the log
func LogTest(m *MountsLoggingTestCase) {
payload := m.Payload
if payload == nil {
payload = &GenericWorkerPayload{
Command: helloGoodbye(),
MaxRunTime: 180,
}
}
payload.Mounts = toMountArray(m.Test, &m.Mounts)
for _, run := range m.PerTaskRunLogExcerpts {
td := testTask(m.Test)
td.Scopes = m.Scopes
td.Dependencies = m.Dependencies
_ = submitAndAssert(m.Test, td, *payload, m.TaskRunResolutionState, m.TaskRunReasonResolved)
logtext := LogText(m.Test)
allLogLines := strings.Split(logtext, "\n")
mountsLogLines := make([]string, 0, len(run))
for _, logLine := range allLogLines {
if strings.Contains(logLine, "[mounts] ") {
mountsLogLines = append(mountsLogLines, logLine)
}
}
if len(mountsLogLines) != len(run) {
m.Test.Log("Wrong number of lines logged by mounts feature")
m.Test.Log("Required lines:")
for _, l := range run {
m.Test.Log(l)
}
m.Test.Log("Actual logged lines:")
for _, l := range mountsLogLines {
m.Test.Log(l)
}
m.Test.FailNow()
}
for i := range mountsLogLines {
if matched, err := regexp.MatchString(`\[mounts\] `+run[i], mountsLogLines[i]); err != nil || !matched {
m.Test.Fatalf("Was expecting log line to match pattern '%v', but it does not:\n%v", run[i], mountsLogLines[i])
}
}
err := os.RemoveAll(taskContext.TaskDir)
if err != nil {
m.Test.Fatalf("Could not delete task directory: %v", err)
}
}
}
func TestInvalidSHA256(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "unknown_issuer_app_1.zip", "public/build/unknown_issuer_app_1.zip")
LogTest(
&MountsLoggingTestCase{
Test: t,
Mounts: []MountEntry{
&ReadOnlyDirectory{
Directory: "unknown_issuer_app_1",
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "public/build/unknown_issuer_app_1.zip",
"sha256": "9263625672993742f0916f7a22b4d9924ed0327f2e02edd18456c0c4e5876850"
}`),
Format: "zip",
},
},
Dependencies: []string{
taskID,
},
TaskRunResolutionState: "failed",
TaskRunReasonResolved: "failed",
PerTaskRunLogExcerpts: [][]string{
// Required text from first task with no cached value
[]string{
`Downloading task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Downloaded 4220 bytes with SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Removing cache artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip from cache table`,
`Deleting cache artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip file\(s\) at .*`,
`Download .* of task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip has SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e but task definition explicitly requires 9263625672993742f0916f7a22b4d9924ed0327f2e02edd18456c0c4e5876850; not retrying download as there were no connection failures and HTTP response status code was 200`,
},
// Required text from second task when download is already cached
[]string{
`Downloading task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Downloaded 4220 bytes with SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Removing cache artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip from cache table`,
`Deleting cache artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip file\(s\) at .*`,
`Download .* of task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip has SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e but task definition explicitly requires 9263625672993742f0916f7a22b4d9924ed0327f2e02edd18456c0c4e5876850; not retrying download as there were no connection failures and HTTP response status code was 200`,
},
},
},
)
}
func TestValidSHA256(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "unknown_issuer_app_1.zip", "public/build/unknown_issuer_app_1.zip")
// whether permission is granted to task user depends if running under windows or not
// and is independent of whether running as current user or not
granting, _ := grantingDenying(t, "directory", "unknown_issuer_app_1")
// Required text from first task with no cached value
pass1 := append([]string{
`Downloading task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Downloaded 4220 bytes with SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Content from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip \(.*\) matches required SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e`,
`Creating directory .*unknown_issuer_app_1 with permissions 0700`,
`Extracting zip file .* to '.*unknown_issuer_app_1'`,
},
granting...,
)
// Required text from second task when download is already cached
pass2 := append([]string{
`Found existing download for artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip \(.*\) with correct SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e`,
`Creating directory .*unknown_issuer_app_1 with permissions 0700`,
`Extracting zip file .* to '.*unknown_issuer_app_1'`,
},
granting...,
)
LogTest(
&MountsLoggingTestCase{
Test: t,
Mounts: []MountEntry{
&ReadOnlyDirectory{
Directory: "unknown_issuer_app_1",
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "public/build/unknown_issuer_app_1.zip",
"sha256": "625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e"
}`),
Format: "zip",
},
},
Dependencies: []string{
taskID,
},
TaskRunResolutionState: "completed",
TaskRunReasonResolved: "completed",
PerTaskRunLogExcerpts: [][]string{
pass1,
pass2,
},
},
)
}
func TestFileMountNoSHA256(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "unknown_issuer_app_1.zip", "public/build/unknown_issuer_app_1.zip")
// whether permission is granted to task user depends if running under windows or not
// and is independent of whether running as current user or not
granting, _ := grantingDenying(t, "file", t.Name())
// No cache on first pass
pass1 := append([]string{
`Downloading task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Downloaded 4220 bytes with SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Download .* of task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip has SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e but task payload does not declare a required value, so content authenticity cannot be verified`,
`Creating directory .* with permissions 0700`,
`Copying .* to .*` + t.Name(),
},
granting...,
)
// On second pass, cache already exists
pass2 := append([]string{
`No SHA256 specified in task mounts for artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip - SHA256 from downloaded file .* is 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e.`,
`Creating directory .* with permissions 0700`,
`Copying .* to .*` + t.Name(),
},
granting...,
)
LogTest(
&MountsLoggingTestCase{
Test: t,
Mounts: []MountEntry{
&FileMount{
File: t.Name(),
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "public/build/unknown_issuer_app_1.zip"
}`),
},
},
Dependencies: []string{
taskID,
},
TaskRunResolutionState: "completed",
TaskRunReasonResolved: "completed",
PerTaskRunLogExcerpts: [][]string{
// Required text from first task with no cached value
pass1,
// Required text from second task when download is already cached
pass2,
},
},
)
}
func TestMountFileAtCWD(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "unknown_issuer_app_1.zip", "public/build/unknown_issuer_app_1.zip")
LogTest(
&MountsLoggingTestCase{
Test: t,
Mounts: []MountEntry{
&FileMount{
// note path needs to be relative, not absolute, so don't use cwd here!
// intentionally setting the path of a directory (current directory) since this should fail test
// since a content can't be mounted at the location of an existing directory (content has no explicit filename)
File: ".",
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "public/build/unknown_issuer_app_1.zip"
}`),
},
},
Dependencies: []string{
taskID,
},
TaskRunResolutionState: "failed",
TaskRunReasonResolved: "failed",
PerTaskRunLogExcerpts: [][]string{
// Required text from first task with no cached value
[]string{
`Downloading task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Downloaded 4220 bytes with SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Download .* of task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip has SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e but task payload does not declare a required value, so content authenticity cannot be verified`,
`Creating directory .* with permissions 0700`,
`Copying .* to .*`,
`Not able to mount content from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip at path .*`,
`open .*: is a directory`,
},
// Required text from second task when download is already cached
[]string{
`No SHA256 specified in task mounts for artifact:` + taskID + `:public/build/unknown_issuer_app_1.zip - SHA256 from downloaded file .* is 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e.`,
`Creating directory .* with permissions 0700`,
`Copying .* to .*`,
`Not able to mount content from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip at path .*`,
`open .*: is a directory`,
},
},
},
)
}
func TestWritableDirectoryCacheNoSHA256(t *testing.T) {
defer setup(t)()
taskID := CreateArtifactFromFile(t, "unknown_issuer_app_1.zip", "public/build/unknown_issuer_app_1.zip")
// whether permission is granted to task user depends if running under windows or not
// and is independent of whether running as current user or not
granting, denying := grantingDenying(t, "directory", t.Name())
// No cache on first pass
pass1 := append([]string{
`No existing writable directory cache 'banana-cache' - creating .*`,
`Downloading task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Downloaded 4220 bytes with SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e from task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip to .*`,
`Download .* of task ` + taskID + ` artifact public/build/unknown_issuer_app_1.zip has SHA256 625554ec8ce731e486a5fb904f3331d18cf84a944dd9e40c19550686d4e8492e but task payload does not declare a required value, so content authenticity cannot be verified`,
`Creating directory .*` + t.Name() + ` with permissions 0700`,
`Extracting zip file .* to '.*` + t.Name() + `'`,
},
granting...,
)
pass1 = append(pass1,
`Successfully mounted writable directory cache '.*`+t.Name()+`'`,
`Preserving cache: Moving ".*`+t.Name()+`" to ".*"`,
)
pass1 = append(pass1, denying...)
// On second pass, cache already exists
pass2 := append([]string{
`Moving existing writable directory cache banana-cache from .* to .*` + t.Name(),
`Creating directory .* with permissions 0700`,
},
granting...,
)
pass2 = append(pass2,
`Successfully mounted writable directory cache '.*`+t.Name()+`'`,
`Preserving cache: Moving ".*`+t.Name()+`" to ".*"`,
)
pass2 = append(pass2, denying...)
LogTest(
&MountsLoggingTestCase{
Test: t,
Mounts: []MountEntry{
&WritableDirectoryCache{
CacheName: "banana-cache",
Directory: t.Name(),
Content: json.RawMessage(`{
"taskId": "` + taskID + `",
"artifact": "public/build/unknown_issuer_app_1.zip"
}`),
Format: "zip",
},
},
Dependencies: []string{
taskID,
},
TaskRunResolutionState: "completed",
TaskRunReasonResolved: "completed",
PerTaskRunLogExcerpts: [][]string{
// Required text from first task with no cached value
pass1,
// Required text from second task when download is already cached
pass2,
},
Scopes: []string{"generic-worker:cache:banana-cache"},
},
)
}