-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluemix.alerts.test.js
315 lines (293 loc) · 12.9 KB
/
bluemix.alerts.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Licensed Materials - Property of IBM
* (C) Copyright IBM Corp. 2016. All Rights Reserved.
* US Government Users Restricted Rights - Use, duplication or
* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
*/
'use strict';
const Helper = require('hubot-test-helper');
const helper = new Helper('../src/scripts');
const expect = require('chai').expect;
const palette = require('hubot-ibmcloud-utils').palette;
const mockUtils = require('./mock.utils.cf.js');
const mockESUtils = require('./mock.utils.es.js');
// Leverage rewire to gain access to internal functions.
const rewire = require('rewire');
const i18n = new (require('i18n-2'))({
locales: ['en'],
extension: '.json',
// Add more languages to the list of locales when the files are created.
directory: __dirname + '/../src/messages',
defaultLocale: 'en',
// Prevent messages file from being overwritten in error conditions (like poor JSON).
updateFiles: false
});
// At some point we need to toggle this setting based on some user input.
i18n.setLocale('en');
const validSpace = 'testSpace';
// Return a promise that will be resolved in the specified # of ms.
function delay(ms) {
return new Promise((resolve) => {
setTimeout(function() {
resolve();
}, ms);
});
}
// Passing arrow functions to mocha is discouraged: https://mochajs.org/#arrow-functions
// return promises from mocha tests rather than calling done() - http://tobyho.com/2015/12/16/mocha-with-promises/
describe('Interacting with Alerts commands via Reg Ex', function() {
let room;
let cf;
let alertsRewire;
before(function() {
mockUtils.setupMockery();
mockESUtils.setupMockery();
// initialize cf, hubot-test-helper doesn't test Middleware
cf = require('hubot-cf-convenience');
// Rewire target module for access to internal functions.
// This must come after cf is mocked up.
alertsRewire = rewire('../src/scripts/app.alerts.js');
return cf.promise.then();
});
beforeEach(function() {
room = helper.createRoom();
// Force all emits into a reply.
room.robot.on('ibmcloud.formatter', function(event) {
if (event.message) {
event.response.reply(event.message);
}
else {
event.response.send({attachments: event.attachments});
}
});
});
afterEach(function() {
room.destroy();
});
context('user sets up alerts', function() {
it('should respond with no alerts', function() {
return room.user.say('mimiron', '@hubot alert list').then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.list.off')]);
});
});
it('should enable and turn off all alerts', function() {
return room.user.say('mimiron', '@hubot alert enable all').then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.enabled', 'all', validSpace)]);
return room.user.say('mimiron', '@hubot alert turn off all');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.disabled', 'all', validSpace)]);
return room.user.say('mimiron', '@hubot alert list');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.list.off')]);
});
});
it('should enable and adjust alerts', function() {
return room.user.say('mimiron', '@hubot alert me when memory exceeds 50%').then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.enable.and.set.enabled', 'memory', '50',
'%',
validSpace)]);
return room.user.say('mimiron', '@hubot alert me if cpu exceeds 10 percent');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot',
'@mimiron ' + i18n.__('app.alert.enable.and.set.enabled', 'cpu', '10', '%',
validSpace)
]);
return room.user.say('mimiron', '@hubot alert set cpu threshold to 50%');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' +
i18n.__('app.alert.config.enabled', 'cpu', '50', '%',
validSpace)
]);
return room.user.say('mimiron', '@hubot alert change memory threshold to 90%');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' +
i18n.__('app.alert.config.enabled', 'memory', '90', '%',
validSpace)
]);
return room.user.say('mimiron', '@hubot alert list');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' +
i18n.__('app.alert.list.enabled', validSpace) + ' cpu:50%, memory:90%\n'
]);
});
});
it('should turn on app events', function() {
return room.user.say('mimiron', '@hubot alert me when app events happen').then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.enable.app.events', validSpace)]);
return room.user.say('mimiron', '@hubot alert me if application events happen');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' +
i18n.__('app.alert.enable.app.events', validSpace)
]);
return room.user.say('mimiron', '@hubot alert list');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.list.enabled', validSpace) + ' event\n']);
});
});
});
// ------------------------------------------------------
// Test: For Slack, emit an alert, via monitorAppEvents.
// ------------------------------------------------------
context('Slack: An app event detected', function() {
it('should trigger a bluemix alert.', function(done) {
room.robot.adapterName = 'slack';
return room.user.say('mimiron', '@hubot alert enable all').then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.enabled', 'all', validSpace)]);
room.robot.on('ibmcloud.formatter', function(event) {
expect(event.attachments.length).to.eql(1);
expect(event.attachments[0].fields.length).to.eql(5);
expect(event.attachments[0].fields[1].title).to.eql(i18n.__('app.alert.monitor.events.type'));
expect(event.attachments[0].fields[1].value).to.eql('app.crash');
expect(event.attachments[0].fields[2].title).to.eql(i18n.__('app.alert.monitor.events.space'));
expect(event.attachments[0].fields[2].value).to.eql('testSpace');
expect(event.attachments[0].fields[3].title).to.eql(i18n.__('app.alert.monitor.events.user'));
expect(event.attachments[0].fields[3].value).to.eql('event1Name');
expect(event.attachments[0].title).to.eql('event1ActeeName');
done();
});
alertsRewire.__get__('monitorAppEvents')(room.robot);
});
});
});
// ------------------------------------------------------
// Test: Call internal monitorAppEvents, but not have anything enabled.
// ------------------------------------------------------
context('If alerting is not enabled', function() {
it('no alerts should trigger.', function() {
alertsRewire.__get__('monitorAppEvents')(room.robot);
return delay(100).then(() => {
// wait a bit to allow any mocked http calls to happen, then ensure no alert messages were written to the room.
expect(room.messages.length).to.eql(0);
});
});
});
// ------------------------------------------------------
// Test: For Slack, emit an alert, via monitorAppResources
// ------------------------------------------------------
context('Slack: An app event detected', function() {
it('should trigger a bluemix alert.', function(done) {
room.robot.adapterName = 'slack';
return room.user.say('mimiron', '@hubot alert enable all').then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.alert.enabled', 'all', validSpace)]);
return room.user.say('mimiron', '@hubot alert me when cpu exceeds 50%');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot',
'@mimiron ' + i18n.__('app.alert.enable.and.set.enabled', 'cpu', '50', '%', validSpace)
]);
return room.user.say('mimiron', '@hubot alert me when memory exceeds 90%');
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot',
'@mimiron ' + i18n.__('app.alert.enable.and.set.enabled', 'memory', '90', '%', validSpace)
]);
room.robot.on('ibmcloud.formatter', function(event) {
expect(event.attachments.length).to.eql(1);
expect(event.attachments[0].title).to.eql(i18n.__('app.alert.monitor.resource.title', 'testApp1Name'));
expect(event.attachments[0].fallback).to.eql(i18n.__('app.alert.monitor.resource.fallback', 'testApp1Name'));
expect(event.attachments[0].text).to.eql(i18n.__('app.alert.monitor.resource.text.cpu.memory.disk', 'testApp1Name', 'testSpace'));
expect(event.attachments[0].fields.length).to.eql(2);
expect(event.attachments[0].fields[0].short).to.eql(false);
expect(event.attachments[0].fields[0].title).to.eql(i18n.__('app.alert.monitor.resource.instances'));
let alertText = i18n.__('app.alert.threshold.description', '0') + i18n.__('app.alert.threshold.cpu',
'99.51', '%') +
i18n.__('app.alert.threshold.memory', '100', '%') + i18n.__('app.alert.threshold.disk', '100', '%');
alertText = alertText.substr(0, alertText.length - 1) + '.'; // remove last comma.
expect(event.attachments[0].fields[0].value).to.eql(alertText);
expect(event.attachments[0].fields[1].short).to.eql(false);
expect(event.attachments[0].fields[1].title).to.eql(i18n.__('app.alert.monitor.resource.recommendations'));
let recText = i18n.__('app.command.or', 'app scale testApp1Name to 2 instances 1280 memory 1536 disk') +
i18n.__('app.command.or', 'app scale testApp1Name') +
i18n.__('app.command.last', 'app restart testApp1Name');
expect(event.attachments[0].fields[1].value).to.eql(recText);
done();
});
alertsRewire.__get__('monitorAppResources')(room.robot);
}).catch((error) => {
done(error);
});
});
});
// ------------------------------------------------------
// Test: Verify green returned for event STARTED
// ------------------------------------------------------
context('Calling getEventColor for STARTED state', function() {
it('should return the color green', function(done) {
let color = alertsRewire.__get__('getEventColor')({
metadata: {
request: {
state: 'STARTED'
}
}
});
expect(color).to.eql(palette.positive);
done();
});
});
// ------------------------------------------------------
// Test: Verify red returned for event STOPPED
// ------------------------------------------------------
context('Calling getEventColor for STARTED state', function() {
it('should return the color green', function(done) {
let color = alertsRewire.__get__('getEventColor')({
metadata: {
request: {
state: 'STOPPED'
}
}
});
expect(color).to.eql(palette.negative);
done();
});
});
// ------------------------------------------------------
// Test: Verify red returned for event with app crashed
// ------------------------------------------------------
context('Calling getEventColor for STARTED state', function() {
it('should return the color green', function(done) {
let color = alertsRewire.__get__('getEventColor')({
type: 'app.crash',
metadata: {
request: {
state: 'STARTED'
}
}
});
expect(color).to.eql(palette.negative);
done();
});
});
context('user calls `alert help`', function() {
beforeEach(function() {
return room.user.say('mimiron', '@hubot alert help');
});
it('should respond with help', function() {
expect(room.messages.length).to.eql(2);
let help =
'hubot alert change cpu|memory|disk threshold to x% - ' + i18n.__('help.app.alert.change') + '\n';
help += 'hubot alert list|show - ' + i18n.__('help.app.alert.list') + '\n';
help += 'hubot alert me when cpu|memory|disk exceeds x% - ' + i18n.__(
'help.app.alert.set.app.resource') + '\n';
help +=
'hubot alert me when app events happen - ' + i18n.__('help.app.alert.set.app.event') + '\n';
help += 'hubot alert turn on cpu|memory|disk|event|all - ' + i18n.__('help.app.alert.on') + '\n';
help += 'hubot alert turn off cpu|memory|disk|event|all - ' + i18n.__('help.app.alert.off') + '\n';
help += 'hubot app show me problems [today|this week] - ' + i18n.__('help.app.show.me.problems') + '\n';
expect(room.messages[1]).to.eql(['hubot', '@mimiron \n' + help]);
});
});
});