This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
296 lines (249 loc) · 9.5 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
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
'use strict';
/**
* Serverless Test Plugin
*/
module.exports = function(S) {
const BbPromise = require('bluebird'),
SCli = require(S.getServerlessPath('utils/cli')),
SError = require(S.getServerlessPath('Error')),
chalk = require('chalk'),
JUnitWriter = require('junitwriter'),
intercept = require('intercept-stdout');
/**
* ServerlessTestPlugin
*/
class ServerlessTestPlugin extends S.classes.Plugin {
/**
* Constructor
* - Keep this and don't touch it unless you know what you're doing.
*/
constructor() {
super();
}
/**
* Define your plugins name
* - We recommend adding prefixing your personal domain to the name so people know the plugin author
*/
static getName() {
return 'com.serverless.' + ServerlessTestPlugin.name;
}
/**
* Register Actions
* - If you would like to register a Custom Action or overwrite a Core Serverless Action, add this function.
* - If you would like your Action to be used programatically, include a "handler" which can be called in code.
* - If you would like your Action to be used via the CLI, include a "description", "context", "action" and any options you would like to offer.
* - Your custom Action can be called programatically and via CLI, as in the example provided below
*/
registerActions() {
S.addAction(this._runFunctionTest.bind(this), {
handler: 'runFunctionTest',
description: 'Run tests on a given function',
context: 'function',
contextAction: 'test',
options: [{ // These must be specified in the CLI like this "-option true" or "-o true"
option: 'all',
shortcut: 'a',
description: 'Optional - Test all functions'
},{
option: 'out',
shortcut: 'o',
description: 'Optional - JUnit output file'
},{
option: 'stage',
shortcut: 's',
description: 'The stage used to populate your templates. Default: the first stage found in your project'
},{
option: 'region',
shortcut: 'r',
description: 'The region used to populate your templates. Default: the first region for the first stage found.'
},{
option: 'dont-fail-on-error',
description: 'Exit with error code 0 even if one or more test cases fail.'
}],
parameters: [{
parameter: 'names',
description: 'One or multiple function names',
position: '0->'
}]
});
return BbPromise.resolve();
}
/**
* Register Hooks
* - If you would like to register hooks (i.e., functions) that fire before or after a core Serverless Action or your Custom Action, include this function.
* - Make sure to identify the Action you want to add a hook for and put either "pre" or "post" to describe when it should happen.
*/
registerHooks() {
return BbPromise.resolve();
}
/**
* Custom Action Example
* - Here is an example of a Custom Action. Include this and modify it if you would like to write your own Custom Action for the Serverless Framework.
* - Be sure to ALWAYS accept and return the "evt" object, or you will break the entire flow.
* - The "evt" object contains Action-specific data. You can add custom data to it, but if you change any data it will affect subsequent Actions and Hooks.
* - You can also access other Project-specific data @ this.S Again, if you mess with data on this object, it could break everything, so make sure you know what you're doing ;)
*/
_runFunctionTest(evt) {
// Set an environment variable the invoked functions can check for
process.env.SERVERLESS_TEST = true;
// Prepare result object
evt.data.result = { status: false };
// Instantiate Classes
let functions;
if (evt.options.all) {
// Load all functions
functions = S.getProject().getAllFunctions();
}
else if (S.cli && evt.options.names && evt.options.names.length === 0) {
// no names or options so use cwd behavior
// will return all functions if none in cwd
functions = S.utils.getFunctionsByCwd(S.getProject().getAllFunctions());
}
else if (evt.options.names && evt.options.names.length > 0) {
// return by passed name(s)
functions = evt.options.names.map(name => {
const func = S.getProject().getFunction(name);
if (!func) {
throw new SError(`Function ${name} does not exist in your project`);
}
return func;
});
}
if (!functions || functions.length === 0) {
throw new SError(`You need to specify either a function path or --all to test all functions`);
}
// Set stage and region
const stages = S.getProject().stages;
const stagesKeys = Object.keys(stages);
if (!stagesKeys.length) {
throw new SError(`We could not find a default stage for your project: it looks like your _meta folder is empty. If you cloned your project using git, try "sls project init" to recreate your _meta folder`);
}
const stage = evt.options.stage || stagesKeys[0];
const stageVariables = stages[stage];
const region = evt.options.region || Object.keys(stageVariables.regions)[0];
// Iterate all functions, execute their handler and
// write the results into a JUnit file...
const junitWriter = new JUnitWriter();
let count = 0, succeeded = 0, failed = 0;
return BbPromise.each(functions, (functionData) => {
let functionTestSuite = junitWriter.addTestsuite(functionData.name);
count++;
if (functionData.runtime.substring(0, 6) === 'nodejs') {
// TODO Should we skip a function that's explicitly specified via command line option?
if (functionData.custom.test && functionData.custom.test.skip) {
SCli.log(`Skipping ${functionData.name}`);
functionTestSuite.addTestcase('skipped', functionData.name);
functionTestSuite.setSkipped(true);
return BbPromise.resolve(); // skip this function
}
// Load test event data
const eventFile = functionData.getRootPath((functionData.custom.test ?
functionData.custom.test.event : false) || 'event.json');
const eventData = S.utils.readFileSync(eventFile);
return BbPromise.try(() => {
// We intercept all stdout from the function and dump
// it into our test results instead.
SCli.log(`Testing ${functionData.name}...`);
let testCase = functionTestSuite.addTestcase('should succeed', functionData.name);
let capturedText = '';
let unhookIntercept = intercept(function(txt) {
// Remove all ANSI color codes from output
const regex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g;
capturedText += txt.replace(regex, '');
return ''; // don't print anything
});
// Finally run the Lambda function...
let startTime = Date.now();
return functionData.run(stage, region, eventData)
.timeout(functionData.timeout * 1000)
.then((result) => {
let duration = (Date.now() - startTime) / 1000;
unhookIntercept(); // stop intercepting stdout
testCase.setSystemOut(capturedText);
testCase.setTime(duration);
if (!result || result.status !== "success") {
let msg;
if (result && result.error) {
msg = result.error.toString();
}
else {
msg = "xxxx";
}
testCase.addFailure(msg, "Failed");
SCli.log(chalk.bgRed.white(" ERROR ") + " " +
chalk.red(msg));
failed++;
}
else {
// Done.
SCli.log(chalk.green("Success!"));
succeeded++;
}
})
.catch(BbPromise.TimeoutError, () => {
unhookIntercept(); // stop intercepting stdout
let msg = `Timeout of ${functionData.timeout} seconds exceeded`;
testCase.addFailure(msg, "Timeout");
SCli.log(chalk.bgMagenta.white(" TIMEOUT ") + " " +
chalk.magenta(msg));
failed++;
})
.catch((err) => {
unhookIntercept(); // stop intercepting stdout
let msg = err.toString();
testCase.addFailure(msg, "Failed");
// Done with errors.
SCli.log(chalk.bgRed.white(" ERROR ") + " " +
chalk.red(msg));
failed++;
});
})
.catch((err) => {
SCli.log("-----------------");
SCli.log(chalk.bold("Failed to Run Handler - This Error Was Thrown:"));
SCli.log(err);
evt.data.result.status = 'error';
evt.data.result.response = err.message;
});
}
else {
SCli.log("Skipping " + functionData.name);
functionTestSuite.setSkipped(true);
}
})
.then(() => {
SCli.log("-----------------");
// All done. Print a summary and write the test results
SCli.log("Tests completed: " +
chalk.green(String(succeeded) + " succeeded") + " / " +
chalk.red(String(failed) + " failed") + " / " +
chalk.white(String(count - succeeded - failed) + " skipped"));
if (evt.options.out) {
// Write test results to file
return new BbPromise(function(resolve) {
junitWriter.save(evt.options.out, function() {
SCli.log("Test results written to " + evt.options.out);
resolve();
});
});
}
})
.then(() => {
var exitCode = (failed && !evt.options["dont-fail-on-error"]) ? 1 : 0;
process.exit(exitCode);
})
.catch((err) => {
SCli.log("-----------------");
SCli.log(chalk.bold("Failed to Run Tests - This Error Was Thrown:"));
SCli.log(err);
evt.data.result.status = 'error';
evt.data.result.response = err.message;
})
.finally(() => {
process.env.SERVERLESS_TEST = undefined;
});
}
}
// Export Plugin Class
return ServerlessTestPlugin;
};