-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
267 lines (233 loc) · 8.73 KB
/
index.js
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
// -----------------------------------------------------------------------------
// Copyright (c) 2018 Pau Sanchez
//
// MIT Licensed
// -----------------------------------------------------------------------------
const redis = require("redis");
const crypto = require("crypto");
const GRANULARITY = {
TEST: "test",
SUITE: "suite",
};
// Initialize variables from environment
const g_redisAddress = process.env.MOCHA_DISTRIBUTED || "";
const g_testExecutionId = process.env.MOCHA_DISTRIBUTED_EXECUTION_ID || "";
const g_expirationTime =
process.env.MOCHA_DISTRIBUTED_EXPIRATION_TIME || `${7 * 24 * 3600}`;
// Generate a unique random id for this runner (with almost 100% certainty
// to be different on any machine/environment).
const _randomRunnerBuf = Buffer.alloc(16);
const _randomRunnerId = crypto.randomFillSync(_randomRunnerBuf).toString("hex");
const g_runnerId = process.env.MOCHA_DISTRIBUTED_RUNNER_ID || _randomRunnerId;
let g_granularity =
process.env.MOCHA_DISTRIBUTED_GRANULARITY || GRANULARITY.TEST;
const g_mochaVerbose = process.env.MOCHA_DISTRIBUTED_VERBOSE === "true";
if (g_granularity !== GRANULARITY.TEST) {
g_granularity = GRANULARITY.SUITE;
}
let g_redis = null;
let g_capture = { stdout: null, stderr: null };
// -----------------------------------------------------------------------------
// getTestPath
//
// Returns an array with the test suites and test name from a test context
// as found in the hooks
//
// Example:
//
// >>> getTestPath(ctxt)
//
// -----------------------------------------------------------------------------
function getTestPath(testContext) {
const path = [testContext.title];
while (!testContext.root && testContext.parent) {
testContext = testContext.parent;
if (testContext && !testContext.root) {
path.push(testContext.title);
}
}
return path.reverse();
}
// -----------------------------------------------------------------------------
// getSerialGranularity
//
// Returns the full string or the "serial string" which is whatever finds that
// follows this regex "[serial.*]" on the string. Only first instance is
// returned.
//
// This will allow serializing tests with given serial name.
// -----------------------------------------------------------------------------
function getSerialGranularity(testKey) {
// NOTE: a regular expression might be trickier to get right, since you can
// have multiple instances of [serialxxxx] on the same string
let index = testKey.indexOf('[serial')
if (index === -1)
return testKey
let index2 = testKey.indexOf(']', index)
if (index2 === -1)
return testKey
return testKey.substring(index, index2+1)
}
// -----------------------------------------------------------------------------
// captureStream
// -----------------------------------------------------------------------------
function captureStream(stream) {
var oldWrite = stream.write;
var buf = [];
stream.write = function (chunk, encoding, callback) {
buf.push(chunk.toString()); // chunk is a String or Buffer
oldWrite.apply(stream, arguments);
};
return {
unhook() {
stream.write = oldWrite;
},
captured() {
return buf;
},
};
}
// -----------------------------------------------------------------------------
// Initialize redis once before the tests
// -----------------------------------------------------------------------------
exports.mochaGlobalSetup = async function () {
if (g_mochaVerbose) {
const redisNoCredentials = g_redisAddress.replace(
/\/\/[^@]*@/,
"//***:***@"
);
console.log("---------------------------------------------------");
console.log(" Mocha Distributed");
console.log(" - Runner Id :", g_runnerId);
console.log(" - Redis Address :", redisNoCredentials);
console.log(" - Execution Id :", g_testExecutionId);
console.log(" - Data Expiration Time :", g_expirationTime);
console.log(" - Test Parallel Granularity:", g_granularity);
console.log("---------------------------------------------------");
}
if (!g_redisAddress || !g_testExecutionId) {
console.log(g_redisAddress, g_testExecutionId);
console.error(
"You need to set at least the following environment variables:\n" +
" - MOCHA_DISTRIBUTED\n" +
" - MOCHA_DISTRIBUTED_EXECUTION_ID\n"
);
process.exit(-1);
}
g_redis = redis.createClient({ url: g_redisAddress });
g_redis.on("error", (err) => {
console.log("Redis Client Error", err);
console.log("Closing application!");
process.exit(-1);
});
await g_redis.connect();
};
// -----------------------------------------------------------------------------
// Quit from redis
// -----------------------------------------------------------------------------
exports.mochaGlobalTeardown = async function () {
if (g_redis) {
await g_redis.quit();
}
};
// -----------------------------------------------------------------------------
// Hook tests
//
// Please note that we run skip before each test if the ownership of it has
// already been defined by another runner.
// -----------------------------------------------------------------------------
exports.mochaHooks = {
beforeEach: async function () {
const testPath = getTestPath(this.currentTest);
const testKeyFullPath = `${g_testExecutionId}:${getSerialGranularity(testPath.join(":"))}`;
const testKeySuite = `${g_testExecutionId}:${getSerialGranularity(testPath[0])}`;
const testKey =
g_granularity === GRANULARITY.TEST ? testKeyFullPath : testKeySuite;
// Atomically set/get the runner id associated to this test. Only the first
// runner to get there will set the value to its own runner id.
const [_, assignedRunnerId] = await g_redis
.multi()
.set(testKey, g_runnerId, { EX: g_expirationTime, NX: true })
.get(testKey)
.exec();
if (assignedRunnerId !== g_runnerId) {
this.currentTest.title += " (skipped by mocha_distributted)";
this.skip();
} else {
g_capture.stdout = captureStream(process.stdout);
g_capture.stderr = captureStream(process.stderr);
}
},
afterEach(done) {
const SKIPPED = "pending";
const FAILED = "failed";
const PASSED = "passed";
let capturedStdout = "";
let capturedStderr = "";
if (g_capture.stdout) {
const stdoutArray = g_capture.stdout.captured();
capturedStdout = stdoutArray.join("");
capturedStdout = capturedStdout.replace(
/\s*\u001b\[3[12]m[^\n]*\n$/g,
""
);
g_capture.stdout.unhook();
g_capture.stdout = null;
}
if (g_capture.stderr) {
capturedStderr = g_capture.stderr.captured().join("");
g_capture.stderr.unhook();
g_capture.stderr = null;
}
// Save all data in redis in a way it can be retrieved and aggregated
// easily for all test by an external reporter
if (this.currentTest.state !== SKIPPED) {
const retryAttempt = this.currentTest._currentRetry || 0;
const retryTotal = this.currentTest._retries || 1;
// adjust state value accounting for exceptions, timeouts & retries
let stateFixed = PASSED;
if (
this.currentTest.state === FAILED ||
this.currentTest.timedOut ||
(typeof this.currentTest.state === "undefined" &&
retryAttempt < retryTotal)
) {
stateFixed = FAILED;
}
// Error objects cannot be properly serialized with stringify, thus
// we need to use this hack to make it look like a normal object.
// Hopefully this should work as well with other sort of objects
const err = this.currentTest.err || null;
const errObj = JSON.parse(
JSON.stringify(err, Object.getOwnPropertyNames(err || {}))
);
const testResult = {
id: getTestPath(this.currentTest),
type: this.currentTest.type,
title: this.currentTest.title,
timedOut: this.currentTest.timedOut,
duration: this.currentTest.duration,
startTime: Date.now() - (this.currentTest.duration || 0),
endTime: Date.now(),
retryAttempt: retryAttempt,
retryTotal: retryTotal,
file: this.currentTest.file,
state: stateFixed,
failed: stateFixed === FAILED,
speed: this.currentTest.speed,
err: errObj,
stdout: capturedStdout,
stderr: capturedStderr,
};
// save results as single line on purpose
const key = `${g_testExecutionId}:test_result`;
g_redis.rPush(key, JSON.stringify(testResult));
g_redis.expire(key, g_expirationTime);
// increment passed_count/failed_count & set expiry time
const countKey = `${g_testExecutionId}:${stateFixed}_count`;
g_redis.incr(countKey);
g_redis.expire(countKey, g_expirationTime);
}
done();
},
};