-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluemix.alerts.cognitive.test.js
200 lines (166 loc) · 7.25 KB
/
bluemix.alerts.cognitive.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
/*
* 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');
const validSpace = 'testSpace';
// 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 via Natural Language', 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();
});
afterEach(function() {
room.destroy();
});
context('user sets up alerts', function() {
it('should respond with no alerts', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('app.alert.list.off'));
done();
});
let res = { message: {text: 'Show my alerts'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.list', res, {});
});
it('should enable alerts', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('app.alert.enabled', 'all', validSpace));
done();
});
let res = { message: {text: 'Enable all alerts'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.enable', res, {type: 'all'});
});
it('should fail to enable alerts', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('cognitive.parse.problem.type'));
done();
});
let res = { message: {text: 'Enable alerts'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.enable', res, {});
});
it('should disable alerts', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('app.alert.disabled.no.alerts', validSpace));
done();
});
let res = { message: {text: 'Disable all alerts'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.disable', res, {type: 'all'});
});
it('should fail to disable alerts', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('cognitive.parse.problem.type'));
done();
});
let res = { message: {text: 'Disable alerts'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.disable', res, {});
});
it('should enable alerts', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('app.alert.enable.and.set.enabled', 'cpu', '10', '%',
validSpace));
done();
});
let res = { message: {text: 'Enable cpu threshold alerts at 10%'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.enableAndSet', res, {thresholdType: 'cpu', threshold: 10});
});
it('should fail to enable alerts due to missing thresholdType', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('cognitive.parse.problem.thresholdType'));
done();
});
let res = { message: {text: 'Enable cpu threshold alerts at 10%'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.enableAndSet', res, {threshold: 10});
});
it('should fail to enable alerts due to missing threshold', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('cognitive.parse.problem.threshold'));
done();
});
let res = { message: {text: 'Enable cpu threshold alerts at 10%'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.enableAndSet', res, {thresholdType: 'cpu'});
});
it('should set alerts threshold', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('app.alert.config.please.enable', 'cpu'));
done();
});
let res = { message: {text: 'Set cpu alert threshold to 10%'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.threshold', res, {thresholdType: 'cpu', threshold: 10});
});
it('should fail to set alerts threshold missing thresholdType', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('cognitive.parse.problem.thresholdType'));
done();
});
let res = { message: {text: 'Set cpu alert threshold to 10%'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.threshold', res, {threshold: 10});
});
it('should fail to set alerts threshold missing threshold', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('cognitive.parse.problem.threshold'));
done();
});
let res = { message: {text: 'Set cpu alert threshold to 10%'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.threshold', res, {thresholdType: 'cpu'});
});
it('should turn on app events', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain(i18n.__('app.alert.enable.app.events', validSpace));
done();
});
let res = { message: {text: 'start monitoring my apps'}, user: {id: 'anId'}, response: room };
room.robot.emit('bluemix.alerts.app.enable', res, {});
});
});
context('user calls `alert help`', function() {
it('should respond with help', function(done) {
room.robot.on('ibmcloud.formatter', (event) => {
expect(event.message).to.be.a('string');
expect(event.message).to.contain('hubot alert me when app events happen');
expect(event.message).to.contain('hubot alert turn on cpu|memory|disk|event|all');
done();
});
let res = { message: {text: 'Can you help me with app alerts?'}, user: {id: 'anId'}, response: room };
room.robot.emit('alerts.help', res, {});
});
});
});