-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathretrieveLogs.test.js
169 lines (142 loc) · 5.16 KB
/
retrieveLogs.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
'use strict';
const sinon = require('sinon');
const BbPromise = require('bluebird');
const chalk = require('chalk');
const GoogleProvider = require('../../provider/googleProvider');
const GoogleLogs = require('../googleLogs');
const Serverless = require('../../test/serverless');
describe('RetrieveLogs', () => {
let serverless;
let googleLogs;
beforeEach(() => {
serverless = new Serverless();
serverless.service = {
service: 'my-service',
};
serverless.service.provider = {
project: 'my-project',
};
serverless.service.functions = {
func1: {
handler: 'foo',
name: 'full-function-name',
},
};
serverless.setProvider('google', new GoogleProvider(serverless));
const options = {
stage: 'dev',
region: 'us-central1',
};
googleLogs = new GoogleLogs(serverless, options);
});
describe('#retrieveLogs()', () => {
let getLogsStub;
let printLogsStub;
beforeEach(() => {
getLogsStub = sinon.stub(googleLogs, 'getLogs').returns(BbPromise.resolve());
printLogsStub = sinon.stub(googleLogs, 'printLogs').returns(BbPromise.resolve());
});
afterEach(() => {
googleLogs.getLogs.restore();
googleLogs.printLogs.restore();
});
it('should run promise chain', () =>
googleLogs.retrieveLogs().then(() => {
expect(getLogsStub.calledOnce).toEqual(true);
expect(printLogsStub.calledAfter(getLogsStub));
}));
});
describe('#getLogs()', () => {
let requestStub;
beforeEach(() => {
requestStub = sinon.stub(googleLogs.provider, 'request').returns(BbPromise.resolve());
});
afterEach(() => {
googleLogs.provider.request.restore();
});
it('should return a default amount of logs for the function if the "count" option is not given', () => {
googleLogs.options.function = 'func1';
return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: 10,
})
).toEqual(true);
});
});
it('should return logs of the function if the "count" option is given', () => {
googleLogs.options.function = 'func1';
googleLogs.options.count = 100;
return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: googleLogs.options.count,
})
).toEqual(true);
});
});
it('should parse the "count" option as an integer', () => {
googleLogs.options.function = 'func1';
googleLogs.options.count = '100';
return googleLogs.getLogs().then(() => {
expect(
requestStub.calledWithExactly('logging', 'entries', 'list', {
filter: 'resource.labels.function_name="full-function-name" AND NOT textPayload=""',
orderBy: 'timestamp desc',
resourceNames: ['projects/my-project'],
pageSize: parseInt(googleLogs.options.count, 10),
})
).toEqual(true);
});
});
it('should throw an error if the function could not be found in the service', () => {
googleLogs.options.function = 'missingFunc';
expect(() => googleLogs.getLogs()).toThrow(Error);
});
});
describe('#printLogs()', () => {
let consoleLogStub;
beforeEach(() => {
consoleLogStub = sinon.stub(googleLogs.serverless.cli, 'log').returns();
});
afterEach(() => {
googleLogs.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: 'Entry 1',
labels: { execution_id: 'foo' },
},
{
timestamp: '1970-01-01 00:01',
textPayload: 'Entry 2',
labels: { execution_id: 'bar' },
},
],
};
const logEntry1 = `${chalk.grey('1970-01-01 00:00:')}\t[foo]\tEntry 1`;
const logEntry2 = `${chalk.grey('1970-01-01 00:01:')}\t[bar]\tEntry 2`;
const expectedOutput = `Displaying the 2 most recent log(s):\n\n${logEntry1}\n${logEntry2}`;
return googleLogs.printLogs(logs).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 logEntry = `${chalk.grey(date)}\tThere is no log data to show...`;
const expectedOutput = `Displaying the 1 most recent log(s):\n\n${logEntry}`;
return googleLogs.printLogs({}).then(() => {
expect(consoleLogStub.calledWithExactly(expectedOutput)).toEqual(true);
});
});
});
});