-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluemix.app.problems.test.js
167 lines (144 loc) · 6.4 KB
/
bluemix.app.problems.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
/*
* 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 mockUtils = require('./mock.utils.cf.js');
const mockESUtils = require('./mock.utils.es.js');
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');
// 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('Testing problematic apps via Reg Ex', function() {
let room;
let cf;
before(function() {
mockUtils.setupMockery();
mockESUtils.setupMockery();
// initialize cf, hubot-test-helper doesn't test Middleware
cf = require('hubot-cf-convenience');
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('Testing an invalid elasticsearch client', function() {
let saveESUrl;
before(function() {
saveESUrl = process.env.HUBOT_AUDIT_ENDPOINT;
delete process.env.HUBOT_AUDIT_ENDPOINT;
let es = require('../src/lib/es');
es.clearClient();
});
after(function() {
room.destroy();
process.env.HUBOT_AUDIT_ENDPOINT = saveESUrl;
});
it('should respond with not available', function() {
process.env.uuid = 'prob_nothing';
return room.user.say('mimiron', '@hubot app show me problems today').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.problems.error')]);
});
});
});
context('user query bot problematic apps', function() {
// Controlling what data is returned from ES for the usage query is done via the container uuid environment variable.
it('should respond with no problematic apps', function() {
process.env.uuid = 'prob_nothing';
return room.user.say('mimiron', '@hubot app show me problems today').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.problems.no.problematic', ' today')]);
});
});
it('should respond with error due to invalid response', function() {
process.env.uuid = 'prob_invalid';
return room.user.say('mimiron', '@hubot app show me problems today').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.problems.error')]);
});
});
it('should respond with problematic apps for today', function() {
process.env.uuid = 'prob_today';
return room.user.say('mimiron', '@hubot app show me problems today').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot',
'@mimiron \n' + i18n.__('app.problems.most.problematic', '3', ' today', '\n1. The \`testapp6\` application crashed \`3\` times and had \`1\` cpu and \`1\` disk threshold violations.\n2. The \`testapp5\` application crashed \`5\` times.\n3. The \`testapp4\` application had \`4\` cpu threshold violations.')
]);
});
});
it('should respond with problematic apps for the week', function() {
process.env.uuid = 'prob_week';
return room.user.say('mimiron', '@hubot app show me problems this week').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot',
'@mimiron \n' + i18n.__('app.problems.most.problematic', '5', ' this week', '\n1. The \`testapp6\` application crashed \`4\` times and had \`3\` cpu, \`1\` memory, and \`4\` disk threshold violations.\n2. The \`testapp5\` application crashed \`6\` times and had \`1\` cpu and \`3\` memory threshold violations.\n3. The \`testapp4\` application had \`4\` memory and \`4\` disk threshold violations.\n4. The \`testapp3\` application crashed \`5\` times and had \`1\` disk threshold violation.\n5. The \`testapp2\` application crashed \`1\` time and had \`3\` memory threshold violations.')
]);
});
});
it('should respond with problematic apps', function() {
process.env.uuid = 'prob_week';
return room.user.say('mimiron', '@hubot app show me problems').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot',
'@mimiron \n' + i18n.__('app.problems.most.problematic', '5', '', '\n1. The \`testapp6\` application crashed \`4\` times and had \`3\` cpu, \`1\` memory, and \`4\` disk threshold violations.\n2. The \`testapp5\` application crashed \`6\` times and had \`1\` cpu and \`3\` memory threshold violations.\n3. The \`testapp4\` application had \`4\` memory and \`4\` disk threshold violations.\n4. The \`testapp3\` application crashed \`5\` times and had \`1\` disk threshold violation.\n5. The \`testapp2\` application crashed \`1\` time and had \`3\` memory threshold violations.')
]);
});
});
it('should respond with error', function() {
process.env.uuid = 'week';
return room.user.say('mimiron', '@hubot app show me problems').then(() => {
return delay(100);
}).then(() => {
let response = room.messages[room.messages.length - 1];
expect(response).to.eql(['hubot', '@mimiron ' + i18n.__('app.problems.error')]);
});
});
});
});