/
Serverless.test.js
379 lines (317 loc) · 13.3 KB
/
Serverless.test.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
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
'use strict';
const chai = require('chai');
chai.use(require('chai-as-promised'));
const { expect } = chai;
const Serverless = require('../../../lib/Serverless');
const semverRegex = require('semver-regex');
const path = require('path');
const sinon = require('sinon');
const BbPromise = require('bluebird');
const YamlParser = require('../../../lib/classes/YamlParser');
const PluginManager = require('../../../lib/classes/PluginManager');
const Utils = require('../../../lib/classes/Utils');
const Service = require('../../../lib/classes/Service');
const ConfigSchemaHandler = require('../../../lib/classes/ConfigSchemaHandler');
const CLI = require('../../../lib/classes/CLI');
const ServerlessError = require('../../../lib/serverless-error');
const conditionallyLoadDotenv = require('../../../lib/cli/conditionally-load-dotenv');
const runServerless = require('../../utils/run-serverless');
const fixtures = require('../../fixtures/programmatic');
const fsp = require('fs').promises;
describe('Serverless', () => {
let serverless;
beforeEach(() => {
serverless = new Serverless({ commands: ['print'], options: {}, serviceDir: null });
});
describe('#constructor()', () => {
it('should set the correct config if a config object is passed', () => {
const configObj = { some: 'config', commands: [], options: {} };
const serverlessWithConfig = new Serverless(configObj);
expect(serverlessWithConfig.config.some).to.equal('config');
});
it('should set an empty config object if no config object passed', () => {
expect(Object.keys(serverless.config)).to.include('serverless');
expect(Object.keys(serverless.config)).to.include('serverlessPath');
});
it('should set an empty providers object', () => {
expect(serverless.providers).to.deep.equal({});
});
it('should set the Serverless version', () => {
expect(serverless.version.length).to.be.at.least(1);
});
it('should set the YamlParser class instance', () => {
expect(serverless.yamlParser).to.be.instanceof(YamlParser);
});
it('should set the PluginManager class instance', () => {
expect(serverless.pluginManager).to.be.instanceof(PluginManager);
});
it('should set the Utils class instance', () => {
expect(serverless.utils).to.be.instanceof(Utils);
});
it('should set the Service class instance', () => {
expect(serverless.service).to.be.instanceof(Service);
});
it('should set the ConfigSchemaHandler class instance', () => {
expect(serverless.configSchemaHandler).to.be.instanceof(ConfigSchemaHandler);
});
it('should have a config object', () => {
expect(serverless.config).to.not.equal(undefined);
});
it('should have a classes object', () => {
expect(serverless.classes).to.not.equal(undefined);
});
it('should store the CLI class inside the classes object', () => {
expect(serverless.classes.CLI).to.deep.equal(CLI);
});
it('should store the YamlParser class inside the classes object', () => {
expect(serverless.classes.YamlParser).to.deep.equal(YamlParser);
});
it('should store the PluginManager class inside the classes object', () => {
expect(serverless.classes.PluginManager).to.deep.equal(PluginManager);
});
it('should store the Utils class inside the classes object', () => {
expect(serverless.classes.Utils).to.deep.equal(Utils);
});
it('should store the Service class inside the classes object', () => {
expect(serverless.classes.Service).to.deep.equal(Service);
});
it('should store the ConfigSchemaHandler class inside the classes object', () => {
expect(serverless.classes.ConfigSchemaHandler).to.deep.equal(ConfigSchemaHandler);
});
it('should store the Error class inside the classes object', () => {
expect(serverless.classes.Error).to.deep.equal(ServerlessError);
});
});
describe('#init()', () => {
it('should set an instanceId', () =>
serverless.init().then(() => {
expect(serverless.instanceId).to.match(/\d/);
}));
it('should create a new CLI instance', () =>
serverless.init().then(() => {
expect(serverless.cli).to.be.instanceof(CLI);
}));
it('should allow a custom CLI instance', () => {
class CustomCLI extends CLI {}
serverless.classes.CLI = CustomCLI;
return serverless.init().then(() => {
expect(serverless.cli).to.be.instanceof(CLI);
expect(serverless.cli.constructor.name).to.equal('CustomCLI');
});
});
// note: we just test that the processedInput variable is set (not the content of it)
// the test for the correct input is done in the CLI class test file
it('should receive the processed input form the CLI instance', () =>
serverless.init().then(() => {
expect(serverless.processedInput).to.not.deep.equal({});
}));
});
describe('#run()', () => {
let displayHelpStub;
let validateCommandStub;
let populateServiceStub;
let runStub;
beforeEach(() => {
serverless.cli = new CLI(serverless);
serverless.processedInput = { commands: [], options: {} };
// setup default stubs
displayHelpStub = sinon.stub(serverless.cli, 'displayHelp').returns(false);
validateCommandStub = sinon.stub(serverless.pluginManager, 'validateCommand').returns();
populateServiceStub = sinon.stub(serverless.variables, 'populateService').resolves();
runStub = sinon.stub(serverless.pluginManager, 'run').resolves();
});
afterEach(() => {
serverless.cli.displayHelp.restore();
serverless.pluginManager.validateCommand.restore();
serverless.variables.populateService.restore();
serverless.pluginManager.run.restore();
});
it('should resolve if the stats logging call throws an error / is rejected', () => {
return serverless.run().then(() => {
expect(displayHelpStub.calledOnce).to.equal(true);
expect(validateCommandStub.calledOnce).to.equal(true);
expect(populateServiceStub.calledOnce).to.equal(true);
expect(runStub.calledOnce).to.equal(true);
return BbPromise.resolve();
});
});
it('should resolve if help is displayed or no commands are entered', () => {
// overwrite displayHelpStub
displayHelpStub.returns(true);
return serverless.run().then(() => {
expect(displayHelpStub.calledOnce).to.equal(true);
expect(validateCommandStub.calledOnce).to.equal(false);
expect(populateServiceStub.calledOnce).to.equal(false);
expect(runStub.calledOnce).to.equal(false);
});
});
it('should run all the needed functions', () =>
serverless.run().then(() => {
expect(displayHelpStub.calledOnce).to.equal(true);
expect(validateCommandStub.calledOnce).to.equal(true);
expect(populateServiceStub.calledOnce).to.equal(true);
expect(runStub.calledOnce).to.equal(true);
}));
});
describe('#setProvider()', () => {
class ProviderMock {}
it('should set the provider object in the provider object', () => {
const myProvider = new ProviderMock();
serverless.setProvider('myProvider', myProvider);
expect(serverless.providers.myProvider).to.equal(myProvider);
});
});
describe('#getProvider()', () => {
class ProviderMock {}
let myProvider;
beforeEach(() => {
myProvider = new ProviderMock();
serverless.setProvider('myProvider', myProvider);
});
it('should return the provider object', () => {
const retrivedProvider = serverless.getProvider('myProvider');
expect(retrivedProvider).to.deep.equal(myProvider);
});
});
describe('#getVersion()', () => {
it('should get the correct Serverless version', () => {
expect(semverRegex().test(serverless.getVersion())).to.equal(true);
});
});
});
describe('test/unit/lib/Serverless.test.js', () => {
describe('When local version available', () => {
describe('When running global version', () => {
it('Should fallback to local version when it is found and "enableLocalInstallationFallback" is not set', () =>
runServerless({
fixture: 'locallyInstalledServerless',
command: 'print',
modulesCacheStub: {},
}).then(({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([]);
expect(serverless._isInvokedByGlobalInstallation).to.be.true;
expect(serverless.isLocallyInstalled).to.be.true;
expect(serverless.isLocalStub).to.be.true;
}));
it('Should report deprecation notice when "enableLocalInstallationFallback" is set', async () =>
expect(
runServerless({
fixture: 'locallyInstalledServerless',
configExt: { enableLocalInstallationFallback: false },
command: 'print',
modulesCacheStub: {},
})
).to.eventually.be.rejected.and.have.property(
'code',
'REJECTED_DEPRECATION_DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING'
));
it('Should not fallback to local when "enableLocalInstallationFallback" set to false', async () => {
const { serverless } = await runServerless({
fixture: 'locallyInstalledServerless',
configExt: {
disabledDeprecations: 'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING',
enableLocalInstallationFallback: false,
},
command: 'print',
modulesCacheStub: {},
});
expect(serverless.invokedInstance).to.not.exist;
});
it('Should fallback to local version when "enableLocalInstallationFallback" set to true', () =>
runServerless({
fixture: 'locallyInstalledServerless',
configExt: {
disabledDeprecations: 'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING',
enableLocalInstallationFallback: true,
},
command: 'print',
modulesCacheStub: {},
}).then(({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([
'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING',
]);
expect(serverless._isInvokedByGlobalInstallation).to.be.true;
expect(serverless.isLocallyInstalled).to.be.true;
expect(serverless.isLocalStub).to.be.true;
}));
});
describe('When running local version', () => {
it('Should run without notice', () =>
fixtures.setup('locallyInstalledServerless').then(({ servicePath: serviceDir }) =>
runServerless({
serverlessDir: path.resolve(serviceDir, 'node_modules/serverless'),
cwd: serviceDir,
command: 'print',
}).then(({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.not.include(
'DISABLE_LOCAL_INSTALLATION_FALLBACK_SETTING'
);
expect(serverless._isInvokedByGlobalInstallation).to.be.false;
expect(serverless.isLocallyInstalled).to.be.true;
expect(serverless.isLocalStub).to.be.true;
})
));
});
});
describe('When local version not available', () => {
it('Should run without notice', () =>
runServerless({ fixture: 'aws', command: 'print', modulesCacheStub: {} }).then(
({ serverless }) => {
expect(Array.from(serverless.triggeredDeprecations)).to.deep.equal([]);
expect(serverless._isInvokedByGlobalInstallation).to.be.false;
expect(serverless.isLocallyInstalled).to.be.false;
expect(serverless.isLocalStub).to.not.exist;
}
));
});
describe('When .env file is available', () => {
let serviceDir;
before(async () => {
serviceDir = (
await fixtures.setup('function', {
configExt: {
useDotenv: true,
custom: {
fromDefaultEnv: '${env:DEFAULT_ENV_VARIABLE}',
fromStageEnv: "${env:STAGE_ENV_VARIABLE, 'not-found'}",
fromDefaultExpandedEnv: '${env:DEFAULT_ENV_VARIABLE_EXPANDED}',
},
},
})
).servicePath;
const defaultFileContent = `
DEFAULT_ENV_VARIABLE=valuefromdefault
DEFAULT_ENV_VARIABLE_EXPANDED=$DEFAULT_ENV_VARIABLE/expanded
`;
await fsp.writeFile(path.join(serviceDir, '.env'), defaultFileContent);
conditionallyLoadDotenv.clear();
});
it('Should load environment variables from default .env file if no matching stage', async () => {
const result = await runServerless({
cwd: serviceDir,
command: 'package',
shouldUseLegacyVariablesResolver: true,
});
expect(result.serverless.service.custom.fromDefaultEnv).to.equal('valuefromdefault');
expect(result.serverless.service.custom.fromDefaultExpandedEnv).to.equal(
'valuefromdefault/expanded'
);
expect(result.serverless.service.custom.fromStageEnv).to.equal('not-found');
});
});
describe('Legacy API interface', () => {
let serverless;
before(async () => {
({ serverless } = await runServerless({
fixture: 'aws',
command: 'package',
}));
});
it('Ensure that instance is setup', async () => {
expect(serverless.variables).to.have.property('variableSyntax');
});
it('Ensure config.servicePath', async () => {
expect(serverless.config).to.have.property('servicePath');
});
});
});