-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.js
214 lines (189 loc) · 5.75 KB
/
spec.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
'use strict';
var should = require('chai').should()
, nock = require('nock')
, monit = require('./monit')
, Client = monit.Client
, UnsupportedActionError = monit.errors.UnsupportedActionError;
var responseBody = (function() {/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<monit>
<server>
<id>91767252e99b1a45f1d2a21fa42f92f8</id>
<incarnation>1453203838</incarnation>
<version>5.6</version>
<uptime>21360</uptime>
<poll>120</poll>
<startdelay>0</startdelay>
<localhostname>dummy-host</localhostname>
<controlfile>/etc/monit/monitrc</controlfile>
<httpd>
<address>0.0.0.0</address>
<port>2812</port>
<ssl>0</ssl>
</httpd>
</server>
</monit>
*/}).toString().match(/[^]*\/\*([^]*)\*\/\}$/)[1];
var checkResultMatchResponseBody = function(result) {
var serverNode = result.monit.server;
serverNode.id.should.equal('91767252e99b1a45f1d2a21fa42f92f8');
serverNode.incarnation.should.equal(1453203838);
serverNode.version.should.equal(5.6);
serverNode.poll.should.equal(120);
serverNode.startdelay.should.equal(0);
serverNode.localhostname.should.equal('dummy-host');
serverNode.controlfile.should.equal('/etc/monit/monitrc');
};
describe('Client', function() {
var subject;
describe('constructor', function() {
beforeEach(function() {
subject = new Client();
});
it('defaults hostname option to "localhost"', function() {
subject.hostname.should.equal('localhost');
});
it('defaults port option to 2812', function() {
subject.port.should.equal(2812);
});
it('defaults ssl option to false', function() {
subject.ssl.should.equal(false);
});
});
describe('getUrlObj', function() {
describe('when ssl is true', function() {
before(function() {
var client = new Client({ssl: true});
subject = client.getUrlObj();
});
it('has protocol attribute set to https', function() {
subject.protocol.should.equal('https');
});
});
describe('when ssl is false', function() {
before(function() {
var client = new Client();
subject = client.getUrlObj();
});
it('has protocol attribute set to http', function() {
subject.protocol.should.equal('http');
});
});
describe('when username and password are set', function() {
before(function() {
var client = new Client({
username: 'admin',
password: 'monit'
});
subject = client.getUrlObj();
});
it('has auth attribute set', function() {
subject.auth.should.equal('admin:monit');
});
});
});
describe('getUrl', function() {
beforeEach(function() {
var client = new Client({
username: 'admin',
password: 'monit'
});
subject = client.getUrl();
});
it('builds monit host URL from options', function() {
subject.should.equal('http://admin:monit@localhost:2812');
});
});
describe('getStatusUrl', function() {
beforeEach(function() {
var client = new Client({
username: 'admin',
password: 'monit'
});
subject = client.getStatusUrl();
});
it('builds monit host URL from options', function() {
subject.should.equal('http://admin:monit@localhost:2812/_status?format=xml');
});
});
describe('parse', function() {
it('builds a server instance', function(done) {
var client = new Client();
client.parse(responseBody).then(function(result) {
checkResultMatchResponseBody(result);
done();
}).catch(function(err) {
done(err);
});
});
});
describe('status', function() {
it('makes a request to monit and parses the response', function(done) {
var client = new Client();
var url = client.getUrl();
nock(url)
.get('/_status')
.query({format: 'xml'})
.reply(200, responseBody);
client.status().then(function(result) {
checkResultMatchResponseBody(result);
done();
}).catch(function(err) {
done(err);
});
});
});
describe('action', function() {
it('performs a post to service resource', function(done) {
var client = new Client();
var url = client.getUrl();
nock(url + '/dummy-service')
.post('', {action: 'monitor'})
.reply(200, 'OK');
client.action({
service: 'dummy-service',
action: 'monitor'
}).then(function(result) {
result.should.equal('OK');
done();
}).catch(function(err) {
done(err);
});
});
it('raises an error when no options is undefined', function(done) {
var client = new Client();
client.action().catch(function(err) {
err.message.should.equal('Service and action must be provided.');
done();
});
});
it('raises an error when no service is not provided', function(done) {
var client = new Client({
action: 'monitor'
});
client.action().catch(function(err) {
err.message.should.equal('Service and action must be provided.');
done();
});
});
it('raises an error when no action is not provided', function(done) {
var client = new Client({
service: 'dummy-service'
});
client.action().catch(function(err) {
err.message.should.equal('Service and action must be provided.');
done();
});
});
it('raises an error when an invalid action is provided', function(done) {
var client = new Client();
client.action({
service: 'dummy-service',
action: 'invalid-action'
}).catch(function(err) {
err.should.be.an.instanceof(UnsupportedActionError);
done();
});
});
});
});