Skip to content
This repository has been archived by the owner on Feb 1, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into deploy-function-deploys-function
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuens committed Sep 4, 2017
2 parents 1303702 + a43ffcb commit 323ae77
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 101 deletions.
22 changes: 22 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env node

'use strict';

process.on('unhandledRejection', err => {
throw err;
});

if (!process.env.FORCE_COLOR) process.env.FORCE_COLOR = '0';

if (process.argv.length <= 2) {
process.argv.push(
'!(node_modules)/**/*.test.js',
'--require=sinon-bluebird',
'-R',
'spec',
'--recursive',
'--no-exit'
);
}

require('mocha/bin/_mocha');
2 changes: 1 addition & 1 deletion docs/providers/aws/guide/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ functions:
```
In that case, the framework will fetch the values of those `functionPrefix` outputs from the provided stack names and populate your variables. There are many use cases for this functionality and it allows your service to communicate with other services/stacks.

## Referencing S3 Options
## Referencing S3 Objects
You can reference S3 values as the source of your variables to use in your service with the `s3:bucketName/key` syntax. For example:
```yml
service: new-service
Expand Down
79 changes: 33 additions & 46 deletions lib/Serverless.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,58 +110,55 @@ describe('Serverless', () => {
});

describe('#init()', () => {
it('should create a new CLI instance', () => {
serverless.init();
expect(serverless.cli).to.be.instanceof(CLI);
let loadAllPluginsStub;
let updateAutocompleteCacheFileStub;

beforeEach(() => {
loadAllPluginsStub = sinon
.stub(serverless.pluginManager, 'loadAllPlugins').returns();
updateAutocompleteCacheFileStub = sinon
.stub(serverless.pluginManager, 'updateAutocompleteCacheFile').resolves();
});

afterEach(() => {
serverless.pluginManager.loadAllPlugins.restore();
serverless.pluginManager.updateAutocompleteCacheFile.restore();
});

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;
serverless.init();
expect(serverless.cli).to.be.instanceof(CLI);
expect(serverless.cli.constructor.name).to.equal('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();
expect(serverless.processedInput).to.not.deep.equal({});
});
it('should receive the processed input form the CLI instance', () => serverless.init()
.then(() => {
expect(serverless.processedInput).to.not.deep.equal({});
})
);

it('should resolve after loading the service', () => {
const SUtils = new Utils();
const tmpDirPath = testUtils.getTmpDirPath();
const serverlessYml = {
service: 'new-service',
provider: 'aws',
custom: {
selfValues: {
obj: {
one: 1,
two: 'two',
},
dev: true,
},
variableRefs: {
testA: '${self:custom.selfValues.obj}',
testB: '${env:random_env, opt:stage}',
testC: 'number is ${env:random_env, opt:random_opt, self:custom.selfValues.obj.two}',
testD: '${self:custom.selfValues.${opt:stage}}',
},
},
custom: {},
plugins: ['testPlugin'],
functions: {
functionA: {},
},
resources: {
aws: {
resourcesProp: 'value',
},
azure: {},
google: {},
},
resources: {},
package: {
exclude: ['exclude-me'],
include: ['include-me'],
Expand All @@ -172,24 +169,14 @@ describe('Serverless', () => {
SUtils.writeFileSync(path.join(tmpDirPath, 'serverless.yml'),
YAML.dump(serverlessYml));

const serverlessInstance = new Serverless();
serverlessInstance.config.update({ servicePath: tmpDirPath });
serverless.config.update({ servicePath: tmpDirPath });
serverless.pluginManager.cliOptions = {
stage: 'dev',
};

const updateAutocompleteCacheFileStub = sinon
.stub(serverless.pluginManager, 'updateAutocompleteCacheFile');

serverlessInstance.init().then(loadedService => {
expect(loadedService.custom.variableRefs.testA)
.to.deep.equal({ one: 1, two: 'two' });
expect(loadedService.custom.variableRefs.testB).to.equal('dev');
expect(loadedService.custom.variableRefs.testC).to.equal('number is two');
expect(loadedService.custom.variableRefs.testD).to.equal(true);
return serverless.init().then(() => {
expect(loadAllPluginsStub.calledOnce).to.equal(true);
expect(updateAutocompleteCacheFileStub.calledOnce).to.equal(true);

serverless.pluginManager.updateAutocompleteCacheFile.restore();
});
});
});
Expand All @@ -202,7 +189,7 @@ describe('Serverless', () => {
let runStub;

beforeEach(() => {
serverless.init();
serverless.cli = new CLI(serverless);
serverless.processedInput = { commands: [], options: {} };
// setup default stubs
logStatStub = sinon
Expand Down
14 changes: 3 additions & 11 deletions lib/classes/Service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,10 @@ describe('Service', () => {
YAML.dump(serverlessYml));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

return expect(serviceInstance.load()).to.eventually.be.fulfilled
.then(() => {
return expect(serviceInstance.load()).to.eventually.be.fulfilled.then(() => {
expect(serviceInstance.service).to.be.equal('new-service');
expect(serviceInstance.provider.name).to.deep.equal('aws');
expect(serviceInstance.provider.variableSyntax).to.equal(
Expand Down Expand Up @@ -212,12 +210,10 @@ describe('Service', () => {
YAML.dump(serverlessYml));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

return expect(serviceInstance.load()).to.eventually.be.fulfilled
.then(() => {
return expect(serviceInstance.load()).to.eventually.be.fulfilled.then(() => {
expect(serviceInstance.service).to.be.equal('new-service');
expect(serviceInstance.provider.name).to.deep.equal('aws');
expect(serviceInstance.provider.variableSyntax).to.equal(
Expand Down Expand Up @@ -268,7 +264,6 @@ describe('Service', () => {
JSON.stringify(serverlessJSON));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

Expand Down Expand Up @@ -328,7 +323,6 @@ describe('Service', () => {
YAML.dump(serverlessJSON));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

Expand Down Expand Up @@ -736,12 +730,10 @@ describe('Service', () => {
YAML.dump(serverlessYml));

const serverless = new Serverless();
serverless.init();
serverless.config.update({ servicePath: tmpDirPath });
serviceInstance = new Service(serverless);

return expect(serviceInstance.load()).to.eventually.be.fulfilled
.then(() => {
return expect(serviceInstance.load()).to.eventually.be.fulfilled.then(() => {
serviceInstance.setFunctionNames();
expect(serviceInstance.functions.functionA.name).to.be.equal('new-service-dev-functionA');
});
Expand Down
11 changes: 5 additions & 6 deletions lib/classes/Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe('Utils', () => {
beforeEach(() => {
serverless = new Serverless();
utils = new Utils(serverless);
serverless.init();
});

describe('#dirExistsSync()', () => {
Expand Down Expand Up @@ -306,11 +305,11 @@ describe('Utils', () => {
let trackStub;

beforeEach(() => {
serverless.init();

// set the properties for the processed inputs
serverless.processedInput.commands = [];
serverless.processedInput.options = {};
// mock properties for processed inputs
serverless.processedInput = {
commands: [],
options: {},
};

trackStub = sinon.stub(segment, 'track');
getConfigStub = sinon.stub(configUtils, 'getConfig');
Expand Down
54 changes: 26 additions & 28 deletions lib/plugins/aws/invokeLocal/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ describe('AwsInvokeLocal', () => {
});

afterEach(() => {
invokeLocalNodeJsStub.restore();
invokeLocalPythonStub.restore();
awsInvokeLocal.invokeLocalNodeJs.restore();
awsInvokeLocal.invokeLocalPython.restore();
});

it('should call invokeLocalNodeJs when no runtime is set', () => awsInvokeLocal.invokeLocal()
Expand All @@ -309,27 +309,25 @@ describe('AwsInvokeLocal', () => {
{},
undefined
)).to.be.equal(true);
awsInvokeLocal.invokeLocalNodeJs.restore();
})
);

it('should call invokeLocalNodeJs for any node.js runtime version', () => {
awsInvokeLocal.options.functionObj.runtime = 'nodejs6.10';
awsInvokeLocal.invokeLocal()
.then(() => {
expect(invokeLocalNodeJsStub.calledOnce).to.be.equal(true);
expect(invokeLocalNodeJsStub.calledWithExactly(
'handler',
'hello',
{}
)).to.be.equal(true);
awsInvokeLocal.invokeLocalNodeJs.restore();
});
return awsInvokeLocal.invokeLocal().then(() => {
expect(invokeLocalNodeJsStub.calledOnce).to.be.equal(true);
expect(invokeLocalNodeJsStub.calledWithExactly(
'handler',
'hello',
{},
undefined
)).to.be.equal(true);
});
});

it('should call invokeLocalNodeJs with custom context if provided', () => {
awsInvokeLocal.options.context = 'custom context';
awsInvokeLocal.invokeLocal()
return awsInvokeLocal.invokeLocal()
.then(() => {
expect(invokeLocalNodeJsStub.calledOnce).to.be.equal(true);
expect(invokeLocalNodeJsStub.calledWithExactly(
Expand All @@ -338,24 +336,24 @@ describe('AwsInvokeLocal', () => {
{},
'custom context'
)).to.be.equal(true);
awsInvokeLocal.invokeLocalNodeJs.restore();
});
});

it('should call invokeLocalPython when python2.7 runtime is set', () => {
awsInvokeLocal.options.functionObj.runtime = 'python2.7';
awsInvokeLocal.invokeLocal()
.then(() => {
expect(invokeLocalPythonStub.calledOnce).to.be.equal(true);
expect(invokeLocalPythonStub.calledWithExactly(
'python2.7',
'handler',
'hello',
{}
)).to.be.equal(true);
awsInvokeLocal.invokeLocalPython.restore();
});
delete awsInvokeLocal.options.functionObj.runtime;
return awsInvokeLocal.invokeLocal().then(() => {
// NOTE: this is important so that tests on Windows won't fail
const runtime = process.platform === 'win32' ? 'python.exe' : 'python2.7';
expect(invokeLocalPythonStub.calledOnce).to.be.equal(true);
expect(invokeLocalPythonStub.calledWithExactly(
runtime,
'handler',
'hello',
{},
undefined
)).to.be.equal(true);
delete awsInvokeLocal.options.functionObj.runtime;
});
});

it('throw error when using runtime other than Node.js or Python', () => {
Expand Down Expand Up @@ -426,7 +424,7 @@ describe('AwsInvokeLocal', () => {
awsInvokeLocal.invokeLocalNodeJs('fixture/handlerWithSuccess', 'withRemainingTime');

const remainingTimes = JSON.parse(serverless.cli.consoleLog.lastCall.args[0]);
expect(remainingTimes.start).to.eql(5000);
expect(remainingTimes.start).to.match(/\d+/);
});

it('should never become negative', () => {
Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/aws/invokeLocal/invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from importlib import import_module

class FakeLambdaContext(object):
def __init__(self, name='Fake', version='LATEST', timeout=6):
def __init__(self, name='Fake', version='LATEST', timeout=6, **kwargs):
self.name = name
self.version = version
self.created = time()
self.timeout = timeout
for key, value in kwargs.items():
setattr(self, key, value)

def get_remaining_time_in_millis(self):
return int(max((self.timeout * 1000) - (int(round(time() * 1000)) - int(round(self.created * 1000))), 0))
Expand Down Expand Up @@ -56,8 +58,6 @@ def aws_request_id(self):
handler = getattr(module, args.handler_name)

input = json.load(sys.stdin)
context = FakeLambdaContext()
if 'context' in input:
context = input['context']
result = handler(input['event'], context)
context = FakeLambdaContext(**input.get('context', {}))
result = handler(input['event'], context)
sys.stdout.write(json.dumps(result, indent=4))
2 changes: 1 addition & 1 deletion lib/utils/fs/writeFile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const expect = require('chai').expect;
describe('#writeFile()', function () {
let serverless;
this.timeout(0);

beforeEach(() => {
serverless = new Serverless();
serverless.init();
});

it('should write a .json file asynchronously', () => {
Expand Down
1 change: 0 additions & 1 deletion lib/utils/fs/writeFileSync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ describe('#writeFileSync()', () => {

beforeEach(() => {
serverless = new Serverless();
serverless.init();
});

it('should write a .json file synchronously', () => {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"sls": "./bin/serverless"
},
"scripts": {
"test-bare": "env FORCE_COLOR=0 node_modules/mocha/bin/_mocha \"!(node_modules)/**/*.test.js\" --require=sinon-bluebird -R spec --recursive --no-exit",
"test": "env FORCE_COLOR=0 istanbul cover -x \"**/*.test.js\" node_modules/mocha/bin/_mocha \"!(node_modules)/**/*.test.js\" -- --require=sinon-bluebird -R spec --recursive",
"test-bare": "node bin/test",
"test": "istanbul cover -x \"**/*.test.js\" bin/test",
"lint": "eslint . --cache",
"docs": "node scripts/generate-readme.js",
"simple-integration-test": "jest --maxWorkers=5 simple-suite",
Expand Down

0 comments on commit 323ae77

Please sign in to comment.