Skip to content

Commit 7eb6dfe

Browse files
committed
Update invokeFunction to be log independent
1 parent 2de1878 commit 7eb6dfe

File tree

2 files changed

+23
-97
lines changed

2 files changed

+23
-97
lines changed

invoke/lib/invokeFunction.js

+8-29
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ module.exports = {
88
invokeFunction() {
99
return BbPromise.bind(this)
1010
.then(this.invoke)
11-
.then(this.getLogs)
12-
.then(this.printLogs);
11+
.then(this.printResult);
1312
},
1413

1514
invoke() {
@@ -36,37 +35,17 @@ module.exports = {
3635
params);
3736
},
3837

39-
getLogs() {
40-
const project = this.serverless.service.provider.project;
41-
const region = this.options.region;
42-
let func = this.options.function;
43-
44-
func = getGoogleCloudFunctionName(this.serverless.service.functions, func);
45-
46-
return this.provider.request('logging', 'entries', 'list', {
47-
filter: `Function execution ${func} ${region}`,
48-
orderBy: 'timestamp desc',
49-
resourceNames: [
50-
`projects/${project}`,
51-
],
52-
pageSize: 2,
53-
});
54-
},
38+
printResult(result) {
39+
let res = result;
5540

56-
printLogs(logs) {
57-
if (!logs.entries || !logs.entries.length) {
58-
logs = { //eslint-disable-line
59-
entries: [
60-
{},
61-
{ // represents function "result"
62-
timestamp: new Date().toISOString().slice(0, 10),
63-
textPayload: 'There is no log data available right now...',
64-
},
65-
],
41+
if (!result || !result.result) {
42+
res = {
43+
executionId: 'error',
44+
result: 'An error occurred while executing your function...',
6645
};
6746
}
6847

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

7150
this.serverless.cli.log(log);
7251

invoke/lib/invokeFunction.test.js

+15-68
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,24 @@ describe('InvokeFunction', () => {
3434

3535
describe('#invokeFunction()', () => {
3636
let invokeStub;
37-
let getLogsStub;
38-
let printLogsStub;
37+
let printResultStub;
3938

4039
beforeEach(() => {
4140
invokeStub = sinon.stub(googleInvoke, 'invoke')
4241
.returns(BbPromise.resolve());
43-
getLogsStub = sinon.stub(googleInvoke, 'getLogs')
44-
.returns(BbPromise.resolve());
45-
printLogsStub = sinon.stub(googleInvoke, 'printLogs')
42+
printResultStub = sinon.stub(googleInvoke, 'printResult')
4643
.returns(BbPromise.resolve());
4744
});
4845

4946
afterEach(() => {
5047
googleInvoke.invoke.restore();
51-
googleInvoke.getLogs.restore();
52-
googleInvoke.printLogs.restore();
48+
googleInvoke.printResult.restore();
5349
});
5450

5551
it('should run promise chain', () => googleInvoke
5652
.invokeFunction().then(() => {
5753
expect(invokeStub.calledOnce).toEqual(true);
58-
expect(getLogsStub.calledAfter(invokeStub));
59-
expect(printLogsStub.calledAfter(getLogsStub));
54+
expect(printResultStub.calledAfter(invokeStub));
6055
}));
6156
});
6257

@@ -117,44 +112,7 @@ describe('InvokeFunction', () => {
117112
});
118113
});
119114

120-
describe('#getLogs()', () => {
121-
let requestStub;
122-
123-
beforeEach(() => {
124-
requestStub = sinon.stub(googleInvoke.provider, 'request').returns(BbPromise.resolve());
125-
});
126-
127-
afterEach(() => {
128-
googleInvoke.provider.request.restore();
129-
});
130-
131-
it('should return the recent logs of the previously called function', () => {
132-
googleInvoke.options.function = 'func1';
133-
134-
return googleInvoke.getLogs().then(() => {
135-
expect(requestStub.calledWithExactly(
136-
'logging',
137-
'entries',
138-
'list',
139-
{
140-
filter: 'Function execution foo us-central1',
141-
orderBy: 'timestamp desc',
142-
resourceNames: [
143-
'projects/my-project',
144-
],
145-
pageSize: 2,
146-
})).toEqual(true);
147-
});
148-
});
149-
150-
it('should throw an error if the function could not be found in the service', () => {
151-
googleInvoke.options.function = 'missingFunc';
152-
153-
expect(() => googleInvoke.getLogs()).toThrow(Error);
154-
});
155-
});
156-
157-
describe('#printLogs()', () => {
115+
describe('#printResult()', () => {
158116
let consoleLogStub;
159117

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

168-
it('should print the received execution result log on the console', () => {
169-
const logs = {
170-
entries: [
171-
{ timestamp: '1970-01-01 00:00', textPayload: 'Function execution started' },
172-
{ timestamp: '1970-01-01 00:01', textPayload: 'Function result' },
173-
],
126+
it('should print the received execution result on the console', () => {
127+
const result = {
128+
executionId: 'wasdqwerty',
129+
result: 'Foo bar',
174130
};
175131

176132
const expectedOutput =
177-
'1970-01-01 00:01: Function result';
133+
'wasdqwerty: Foo bar';
178134

179-
return googleInvoke.printLogs(logs).then(() => {
135+
return googleInvoke.printResult(result).then(() => {
180136
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
181137
});
182138
});
183139

184-
it('should print a default message to the console when no logs were received', () => {
185-
const date = new Date().toISOString().slice(0, 10);
186-
const logs = {
187-
entries: [
188-
{},
189-
{
190-
timestamp: date,
191-
textPayload: 'There is no log data available right now...',
192-
},
193-
],
194-
};
140+
it('should print an error message to the console when no result was received', () => {
141+
const result = {};
195142

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

198-
return googleInvoke.printLogs({}).then(() => {
145+
return googleInvoke.printResult(result).then(() => {
199146
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
200147
});
201148
});

0 commit comments

Comments
 (0)