-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathinvokeFunction.test.js
222 lines (190 loc) · 5.92 KB
/
invokeFunction.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
'use strict';
const sinon = require('sinon');
const BbPromise = require('bluebird');
const chalk = require('chalk');
const GoogleProvider = require('../../provider/googleProvider');
const GoogleInvoke = require('../googleInvoke');
const Serverless = require('../../test/serverless');
describe('InvokeFunction', () => {
let serverless;
let googleInvoke;
beforeEach(() => {
serverless = new Serverless();
serverless.service = {
service: 'my-service',
};
serverless.service.provider = {
project: 'my-project',
};
serverless.service.functions = {
func1: {
handler: 'foo',
},
func2: {
handler: 'foo2',
prependService: true,
},
func3: {
handler: 'foo3',
prependStage: true,
},
func4: {
handler: 'foo4',
prependStage: true,
prependService: true,
},
};
serverless.setProvider('google', new GoogleProvider(serverless));
const options = {
stage: 'dev',
region: 'us-central1',
};
googleInvoke = new GoogleInvoke(serverless, options);
});
describe('#invokeFunction()', () => {
let invokeStub;
let printResultStub;
beforeEach(() => {
invokeStub = sinon.stub(googleInvoke, 'invoke')
.returns(BbPromise.resolve());
printResultStub = sinon.stub(googleInvoke, 'printResult')
.returns(BbPromise.resolve());
});
afterEach(() => {
googleInvoke.invoke.restore();
googleInvoke.printResult.restore();
});
it('should run promise chain', () => googleInvoke
.invokeFunction().then(() => {
expect(invokeStub.calledOnce).toEqual(true);
expect(printResultStub.calledAfter(invokeStub));
}));
});
describe('#invoke()', () => {
let requestStub;
beforeEach(() => {
requestStub = sinon.stub(googleInvoke.provider, 'request').returns(BbPromise.resolve());
});
afterEach(() => {
googleInvoke.provider.request.restore();
});
it('should invoke the provided function without data option', () => {
googleInvoke.options.function = 'func1';
return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/foo',
resource: {
data: '',
},
})).toEqual(true);
});
});
it('should invoke the provided function with prependService', () => {
googleInvoke.options.function = 'func2';
return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/my-service-foo2',
resource: {
data: '',
},
})).toEqual(true);
});
});
it('should invoke the provided function with prependStage', () => {
googleInvoke.options.function = 'func3';
return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/dev-foo3',
resource: {
data: '',
},
})).toEqual(true);
});
});
it('should invoke the provided function with all prepend', () => {
googleInvoke.options.function = 'func4';
return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/my-service-dev-foo4',
resource: {
data: '',
},
})).toEqual(true);
});
});
it('should invoke the provided function with the data option', () => {
googleInvoke.options.function = 'func1';
googleInvoke.options.data = '{ "some": "json" }';
return googleInvoke.invoke().then(() => {
expect(requestStub.calledWithExactly(
'cloudfunctions',
'projects',
'locations',
'functions',
'call',
{
name: 'projects/my-project/locations/us-central1/functions/foo',
resource: {
data: googleInvoke.options.data,
},
})).toEqual(true);
});
});
it('should throw an error if the function could not be found in the service', () => {
googleInvoke.options.function = 'missingFunc';
expect(() => googleInvoke.invoke()).toThrow(Error);
});
});
describe('#printResult()', () => {
let consoleLogStub;
beforeEach(() => {
consoleLogStub = sinon.stub(googleInvoke.serverless.cli, 'log').returns();
});
afterEach(() => {
googleInvoke.serverless.cli.log.restore();
});
it('should print the received execution result on the console', () => {
const result = {
executionId: 'wasdqwerty',
result: 'Foo bar',
};
const expectedOutput =
`${chalk.grey('wasdqwerty')} Foo bar`;
return googleInvoke.printResult(result).then(() => {
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
});
});
it('should print an error message to the console when no result was received', () => {
const result = {};
const expectedOutput =
`${chalk.grey('error')} An error occurred while executing your function...`;
return googleInvoke.printResult(result).then(() => {
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
});
});
});
});