forked from taskcluster/taskcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
482 lines (438 loc) · 16 KB
/
helper_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
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
"github.com/gorilla/mux"
"github.com/pborman/uuid"
"github.com/taskcluster/httpbackoff/v3"
"github.com/taskcluster/slugid-go/slugid"
tcclient "github.com/taskcluster/taskcluster/v47/clients/client-go"
"github.com/taskcluster/taskcluster/v47/clients/client-go/tcqueue"
"github.com/taskcluster/taskcluster/v47/internal/mocktc"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/fileutil"
"github.com/taskcluster/taskcluster/v47/workers/generic-worker/gwconfig"
)
var (
inAnHour tcclient.Time
globalTestName string
testdataDir = filepath.Join(cwd, "testdata")
)
func setup(t *testing.T) {
test := GWTest(t)
err := test.Setup()
if err != nil {
test.Teardown()
t.Fatalf("%v", err)
}
t.Cleanup(test.Teardown)
}
// testWorkerType returns a fake workerType identifier that conforms to
// workerType naming restrictions.
//
// See https://bugzil.la/1553953
func testWorkerType() string {
return "test-" + strings.ToLower(strings.Replace(slugid.Nice(), "_", "", -1)) + "-a"
}
func scheduleTask(t *testing.T, td *tcqueue.TaskDefinitionRequest, payload GenericWorkerPayload) (taskID string) {
taskID = slugid.Nice()
scheduleNamedTask(t, td, payload, taskID)
return
}
func scheduleNamedTask(t *testing.T, td *tcqueue.TaskDefinitionRequest, payload GenericWorkerPayload, taskID string) {
if td.Payload == nil {
b, err := json.Marshal(&payload)
if err != nil {
t.Fatalf("Could not convert task payload to json")
}
//////////////////////////////////////////////////////////////////////////////////
//
// horrible hack here, until we have jsonschema2go generating pointer types...
//
//////////////////////////////////////////////////////////////////////////////////
b = bytes.Replace(b, []byte(`"expires":"0001-01-01T00:00:00.000Z",`), []byte{}, -1)
b = bytes.Replace(b, []byte(`,"expires":"0001-01-01T00:00:00.000Z"`), []byte{}, -1)
payloadJSON := json.RawMessage{}
err = json.Unmarshal(b, &payloadJSON)
if err != nil {
t.Fatalf("Could not convert json bytes of payload to json.RawMessage")
}
td.Payload = payloadJSON
}
// submit task
queue := serviceFactory.Queue(config.Credentials(), config.RootURL)
_, err := queue.CreateTask(taskID, td)
if err != nil {
t.Fatalf("Could not submit task: %v", err)
}
t.Logf("Scheduled task %v", taskID)
}
func execute(t *testing.T, expectedExitCode ExitCode) {
err := UpdateTasksResolvedFile(0)
if err != nil {
t.Fatalf("Test setup failure - could not write to tasks-resolved-count.txt file: %v", err)
}
exitCode := RunWorker()
if exitCode != expectedExitCode {
t.Fatalf("Something went wrong executing worker - got exit code %v but was expecting exit code %v", exitCode, expectedExitCode)
} else {
t.Logf("Worker exited with exit code %v as required.", exitCode)
}
}
func testTask(t *testing.T) *tcqueue.TaskDefinitionRequest {
created := time.Now().UTC()
// reset nanoseconds
created = created.Add(time.Nanosecond * time.Duration(created.Nanosecond()*-1))
// deadline in one hour' time
deadline := created.Add(15 * time.Minute)
// expiry in two weeks, in case we need test results
expires := created.AddDate(0, 0, 14)
return &tcqueue.TaskDefinitionRequest{
Created: tcclient.Time(created),
Deadline: tcclient.Time(deadline),
Expires: tcclient.Time(expires),
Extra: json.RawMessage(`{}`),
Dependencies: []string{},
Requires: "all-completed",
Metadata: tcqueue.TaskMetadata{
Description: "Test task",
Name: "[TC] Generic Worker CI - " + globalTestName,
Owner: "[email protected]",
Source: "https://github.com/taskcluster/taskcluster",
},
Payload: nil,
ProvisionerID: config.ProvisionerID,
Retries: 1,
Routes: []string{},
SchedulerID: "test-scheduler",
Scopes: []string{},
Tags: map[string]string{"CI": "generic-worker"},
Priority: "lowest",
TaskGroupID: taskGroupID,
TaskQueueID: config.ProvisionerID + "/" + config.WorkerType,
WorkerType: config.WorkerType,
}
}
func ensureResolution(t *testing.T, taskID, state, reason string) {
if state == "exception" && reason == "worker-shutdown" {
execute(t, WORKER_SHUTDOWN)
} else {
execute(t, TASKS_COMPLETE)
}
queue := serviceFactory.Queue(config.Credentials(), config.RootURL)
status, err := queue.Status(taskID)
if err != nil {
t.Fatal("Error retrieving status from queue")
}
if status.Status.Runs[0].State != state || status.Status.Runs[0].ReasonResolved != reason {
t.Logf("Expected task %v to resolve as '%v/%v' but resolved as '%v/%v'", taskID, state, reason, status.Status.Runs[0].State, status.Status.Runs[0].ReasonResolved)
t.Log("Task logs:")
// This extra space is *super-useful* for breaking up the output since
// this shows a task log embedded inside a different task log
t.Log("")
t.Log("")
t.Log("")
t.Fatal(LogText(t))
t.Log("")
t.Log("")
t.Log("")
} else {
t.Logf("Task %v resolved as %v/%v as required.", taskID, status.Status.Runs[0].State, status.Status.Runs[0].ReasonResolved)
}
}
func LogText(t *testing.T) string {
bytes, err := os.ReadFile(filepath.Join(taskContext.TaskDir, logPath))
if err != nil {
t.Fatalf("Error when trying to read log file: %v", err)
}
return string(bytes)
}
func submitAndAssert(t *testing.T, td *tcqueue.TaskDefinitionRequest, payload GenericWorkerPayload, state, reason string) (taskID string) {
taskID = scheduleTask(t, td, payload)
ensureResolution(t, taskID, state, reason)
return taskID
}
func toMountArray(t *testing.T, x interface{}) []json.RawMessage {
b, err := json.Marshal(x)
if err != nil {
t.Fatalf("Could not convert %#v to json: %v", x, err)
}
rawMessageArray := []json.RawMessage{}
err = json.Unmarshal(b, &rawMessageArray)
if err != nil {
t.Fatalf("Could not convert json bytes to []json.RawMessage")
}
return rawMessageArray
}
// CreateArtifactFromFile returns a taskID for a task with an artifact with the
// given name whose content matches the content of the local file (relative to
// the testdata folder) with the given path. It does this by creating a hash of
// the file content together with the name of the artifact, and then converts
// the hash into a "nice" slug. It then checks if the task already exists. If
// it does exist, it simply returns the taskID. If it doesn't, it creates the
// task and returns.
func CreateArtifactFromFile(t *testing.T, path string, name string) (taskID string) {
// Calculate hash of file content
rawContent, err := os.Open(filepath.Join(testdataDir, path))
if err != nil {
t.Fatal(err)
}
defer rawContent.Close()
hasher := sha256.New()
_, err = io.Copy(hasher, rawContent)
if err != nil {
t.Fatal(err)
}
// Append a 0 byte and the artifact name to the hash source, to ensure that
// if the artifact name changes, we get a different taskID. Since file
// names can't include a 0 byte, adding the zero byte as a separator
// between the two parts ensures that a one-to-one mapping exists between
// {file content, artifact name} and hash.
_, err = hasher.Write(append([]byte{0}, []byte(name)...))
if err != nil {
t.Fatal(err)
}
// Now add a fixed string which we can change if we ever need to roll
// new artifacts, for example if we get a failure like we did for
// https://community-tc.services.mozilla.com/tasks/RgyFPm08TxaF1c9KcpRUaQ/runs/0
_, err = hasher.Write(append([]byte{0}, []byte("tum te tum te tum")...))
if err != nil {
t.Fatal(err)
}
// Use first 128 bits of 256 bit hash for UUID
sha256 := hasher.Sum(nil)
v4uuid := sha256[:16]
// Comply to uuid v4 rules (mask six bits)
v4uuid[6] = (v4uuid[6] & 0x0f) | 0x40 // Version 4
v4uuid[8] = (v4uuid[8] & 0x3f) | 0x80 // Variant is 10
// Make slugid a "nice" one (mask one further bit => 121 bits entropy)
v4uuid[0] &= 0x7f
// Convert to a string taskID
taskID = slugid.Encode(uuid.UUID(v4uuid))
// See if task already exists
queue := serviceFactory.Queue(config.Credentials(), config.RootURL)
tdr, err := queue.Task(taskID)
if err != nil {
switch e := err.(type) {
case *tcclient.APICallException:
switch r := e.RootCause.(type) {
case httpbackoff.BadHttpResponseCode:
if r.HttpResponseCode == 404 {
if engine == "docker" {
switch serviceFactory.(type) {
case *mocktc.ServiceFactory:
t.Skip()
}
t.Fatalf("You've been extremely unlucky. This test depends on task %v with artifact %v that the docker engine can't create, but any of the other engines can create. It is created once every six months by whichever CI task first runs after the previous version expired. It looks like docker engine got there first, which is unfortunate, as it is the only one that can't create it. Probably if you rerun this test, all will be fine, because another CI task will have created it by now.", taskID, name)
}
t.Logf("Creating task %q for artifact %v under path %v...", taskID, name, path)
payload := GenericWorkerPayload{
Command: copyTestdataFile(path),
MaxRunTime: 30,
Artifacts: []Artifact{
{
Path: path,
Name: name,
Type: "file",
},
},
}
td := testTask(t)
// Set 6 month expiry
td.Expires = tcclient.Time(time.Now().AddDate(0, 6, 0))
td.Metadata.Name = "Task dependency for generic-worker integration tests"
td.Metadata.Description = fmt.Sprintf("Single artifact %v from path %v with hash %v", name, path, hex.EncodeToString(sha256))
scheduleNamedTask(t, td, payload, taskID)
ensureResolution(t, taskID, "completed", "completed")
return
}
}
}
t.Fatalf("%#v", err)
}
// If task expires in the next two minutes, just fail intentionally. It
// isn't worth trying to handle this situation, since the task only expires
// after 6 months, so the chance of hitting the two minute period before it
// expires is extremely small, and the error will explicitly report it
// anyway.
remainingTime := time.Until(time.Time(tdr.Expires))
if remainingTime.Seconds() < 120 {
t.Fatalf("You've been extremely unlucky. This test depends on task %q that was created six months ago but is due to expire in less than two minutes (%v). Wait a few minutes and try again!", taskID, remainingTime)
}
t.Logf("Depend on task %q which expires in %v.", taskID, remainingTime)
return
}
type Test struct {
t *testing.T
Config *gwconfig.Config
OldConfigureForGCP bool
srv *http.Server
router *mux.Router
}
func GWTest(t *testing.T) *Test {
testConfig := &gwconfig.Config{
PrivateConfig: gwconfig.PrivateConfig{
AccessToken: os.Getenv("TASKCLUSTER_ACCESS_TOKEN"),
Certificate: os.Getenv("TASKCLUSTER_CERTIFICATE"),
},
PublicConfig: gwconfig.PublicConfig{
AvailabilityZone: "outer-space",
// Need common caches directory across tests, since files
// directory-caches.json and file-caches.json are not per-test.
CachesDir: filepath.Join(cwd, "caches"),
CheckForNewDeploymentEverySecs: 0,
CleanUpTaskDirs: false,
ClientID: os.Getenv("TASKCLUSTER_CLIENT_ID"),
DeploymentID: "",
DisableReboots: true,
// Need common downloads directory across tests, since files
// directory-caches.json and file-caches.json are not per-test.
DownloadsDir: filepath.Join(cwd, "downloads"),
Ed25519SigningKeyLocation: filepath.Join(testdataDir, "ed25519_private_key"),
IdleTimeoutSecs: 60,
InstanceID: "test-instance-id",
InstanceType: "p3.enormous",
LiveLogExecutable: "livelog",
// The base port on which the livelog process listens locally. (Livelog uses this and the next port.)
// These ports are not exposed outside of the host. However, in CI they must differ from those of the
// generic-worker instance running the test suite.
LiveLogPortBase: 30583,
NumberOfTasksToRun: 1,
PrivateIP: net.ParseIP("87.65.43.21"),
ProvisionerID: "test-provisioner",
PublicIP: net.ParseIP("12.34.56.78"),
Region: "test-worker-group",
// should be enough for tests, and travis-ci.org CI environments don't
// have a lot of free disk
RequiredDiskSpaceMegabytes: 16,
RootURL: "http://localhost:13243",
RunAfterUserCreation: "",
SentryProject: "generic-worker-tests",
ShutdownMachineOnIdle: false,
ShutdownMachineOnInternalError: false,
TaskclusterProxyExecutable: "taskcluster-proxy",
TaskclusterProxyPort: 34569,
TasksDir: filepath.Join(testdataDir, t.Name(), "tasks"),
WorkerGroup: "test-worker-group",
WorkerID: "test-worker-id",
WorkerType: testWorkerType(),
WorkerTypeMetadata: map[string]interface{}{
"generic-worker": map[string]string{
"go-arch": runtime.GOARCH,
"go-os": runtime.GOOS,
"go-version": runtime.Version(),
"version": version,
"revision": revision,
"engine": engine,
},
"parent-task": map[string]string{
"taskId": os.Getenv("TASK_ID"),
"runId": os.Getenv("RUN_ID"),
},
},
},
}
setConfigRunTasksAsCurrentUser(testConfig)
for _, dir := range []string{
filepath.Join(cwd, "downloads"),
filepath.Join(cwd, "caches"),
filepath.Join(testdataDir, t.Name()),
} {
err := os.RemoveAll(dir)
if err != nil {
t.Fatalf("Could not remove directory %v: %v", dir, err)
}
err = os.Mkdir(dir, 0755)
if err != nil {
t.Fatalf("Could not create directory %v: %v", dir, err)
}
}
for _, file := range []string{
filepath.Join(cwd, "file-caches.json"),
filepath.Join(cwd, "directory-caches.json"),
} {
err := os.RemoveAll(file)
if err != nil {
t.Fatalf("Could not remove file %v: %v", file, err)
}
}
// Needed for tests that don't call RunWorker()
// but test methods/functions directly
taskContext = &TaskContext{
TaskDir: testdataDir,
}
// useful for expiry dates of tasks
inAnHour = tcclient.Time(time.Now().Add(time.Hour * 1))
globalTestName = t.Name()
r := mux.NewRouter().UseEncodedPath()
r.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(404)
_, _ = w.Write([]byte(fmt.Sprintf("URL %v with method %v NOT FOUND\n", req.URL, req.Method)))
})
srv := &http.Server{
Addr: ":13243",
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r, // Pass our instance of gorilla/mux in.
}
go func() {
_ = srv.ListenAndServe()
}()
if os.Getenv("GW_TESTS_USE_EXTERNAL_TASKCLUSTER") == "" {
for _, s := range mocktc.ServiceProviders(t, "http://localhost:13243") {
s.RegisterService(r)
}
testConfig.AccessToken = "test-access-token"
testConfig.ClientID = "test-client-id"
testConfig.Certificate = ""
}
serviceFactory = mocktc.NewServiceFactory(t)
return &Test{
t: t,
Config: testConfig,
srv: srv,
router: r,
}
}
func (gwtest *Test) Setup() error {
configFile = &gwconfig.File{
Path: filepath.Join(testdataDir, gwtest.t.Name(), "generic-worker.config"),
}
err := fileutil.WriteToFileAsJSON(gwtest.Config, configFile.Path)
if err != nil {
gwtest.t.Fatalf("Could not write config file: %v", err)
}
return loadConfig(configFile)
}
func (gwtest *Test) Teardown() {
gwtest.t.Logf("Removing test directory %v...", filepath.Join(testdataDir, gwtest.t.Name()))
err := os.RemoveAll(filepath.Join(testdataDir, gwtest.t.Name()))
if err != nil {
gwtest.t.Logf("WARNING: Not able to clean up after test: %v", err)
}
taskContext = nil
globalTestName = ""
config = nil
// gwtest.srv nil if no services
if gwtest.srv != nil {
err = gwtest.srv.Shutdown(context.Background())
if err != nil {
gwtest.t.Fatalf("Error shutting down http server: %v", err)
}
gwtest.t.Log("Mock HTTP services stopped")
}
}