Skip to content

Commit

Permalink
Merge 7eb6dfe into 2de1878
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuens committed Mar 2, 2017
2 parents 2de1878 + 7eb6dfe commit e047642
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 97 deletions.
37 changes: 8 additions & 29 deletions invoke/lib/invokeFunction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module.exports = {
invokeFunction() {
return BbPromise.bind(this)
.then(this.invoke)
.then(this.getLogs)
.then(this.printLogs);
.then(this.printResult);
},

invoke() {
Expand All @@ -36,37 +35,17 @@ module.exports = {
params);
},

getLogs() {
const project = this.serverless.service.provider.project;
const region = this.options.region;
let func = this.options.function;

func = getGoogleCloudFunctionName(this.serverless.service.functions, func);

return this.provider.request('logging', 'entries', 'list', {
filter: `Function execution ${func} ${region}`,
orderBy: 'timestamp desc',
resourceNames: [
`projects/${project}`,
],
pageSize: 2,
});
},
printResult(result) {
let res = result;

printLogs(logs) {
if (!logs.entries || !logs.entries.length) {
logs = { //eslint-disable-line
entries: [
{},
{ // represents function "result"
timestamp: new Date().toISOString().slice(0, 10),
textPayload: 'There is no log data available right now...',
},
],
if (!result || !result.result) {
res = {
executionId: 'error',
result: 'An error occurred while executing your function...',
};
}

const log = `${logs.entries[1].timestamp}: ${logs.entries[1].textPayload}`;
const log = `${res.executionId}: ${res.result}`;

this.serverless.cli.log(log);

Expand Down
83 changes: 15 additions & 68 deletions invoke/lib/invokeFunction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,24 @@ describe('InvokeFunction', () => {

describe('#invokeFunction()', () => {
let invokeStub;
let getLogsStub;
let printLogsStub;
let printResultStub;

beforeEach(() => {
invokeStub = sinon.stub(googleInvoke, 'invoke')
.returns(BbPromise.resolve());
getLogsStub = sinon.stub(googleInvoke, 'getLogs')
.returns(BbPromise.resolve());
printLogsStub = sinon.stub(googleInvoke, 'printLogs')
printResultStub = sinon.stub(googleInvoke, 'printResult')
.returns(BbPromise.resolve());
});

afterEach(() => {
googleInvoke.invoke.restore();
googleInvoke.getLogs.restore();
googleInvoke.printLogs.restore();
googleInvoke.printResult.restore();
});

it('should run promise chain', () => googleInvoke
.invokeFunction().then(() => {
expect(invokeStub.calledOnce).toEqual(true);
expect(getLogsStub.calledAfter(invokeStub));
expect(printLogsStub.calledAfter(getLogsStub));
expect(printResultStub.calledAfter(invokeStub));
}));
});

Expand Down Expand Up @@ -117,44 +112,7 @@ describe('InvokeFunction', () => {
});
});

describe('#getLogs()', () => {
let requestStub;

beforeEach(() => {
requestStub = sinon.stub(googleInvoke.provider, 'request').returns(BbPromise.resolve());
});

afterEach(() => {
googleInvoke.provider.request.restore();
});

it('should return the recent logs of the previously called function', () => {
googleInvoke.options.function = 'func1';

return googleInvoke.getLogs().then(() => {
expect(requestStub.calledWithExactly(
'logging',
'entries',
'list',
{
filter: 'Function execution foo us-central1',
orderBy: 'timestamp desc',
resourceNames: [
'projects/my-project',
],
pageSize: 2,
})).toEqual(true);
});
});

it('should throw an error if the function could not be found in the service', () => {
googleInvoke.options.function = 'missingFunc';

expect(() => googleInvoke.getLogs()).toThrow(Error);
});
});

describe('#printLogs()', () => {
describe('#printResult()', () => {
let consoleLogStub;

beforeEach(() => {
Expand All @@ -165,37 +123,26 @@ describe('InvokeFunction', () => {
googleInvoke.serverless.cli.log.restore();
});

it('should print the received execution result log on the console', () => {
const logs = {
entries: [
{ timestamp: '1970-01-01 00:00', textPayload: 'Function execution started' },
{ timestamp: '1970-01-01 00:01', textPayload: 'Function result' },
],
it('should print the received execution result on the console', () => {
const result = {
executionId: 'wasdqwerty',
result: 'Foo bar',
};

const expectedOutput =
'1970-01-01 00:01: Function result';
'wasdqwerty: Foo bar';

return googleInvoke.printLogs(logs).then(() => {
return googleInvoke.printResult(result).then(() => {
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
});
});

it('should print a default message to the console when no logs were received', () => {
const date = new Date().toISOString().slice(0, 10);
const logs = {
entries: [
{},
{
timestamp: date,
textPayload: 'There is no log data available right now...',
},
],
};
it('should print an error message to the console when no result was received', () => {
const result = {};

const expectedOutput = `${date}: ${logs.entries[1].textPayload}`;
const expectedOutput = 'error: An error occurred while executing your function...';

return googleInvoke.printLogs({}).then(() => {
return googleInvoke.printResult(result).then(() => {
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
});
});
Expand Down

0 comments on commit e047642

Please sign in to comment.