UNPKG

13.1 kBJavaScriptView Raw
1// Jasmine tests for servers availability related methods.
2// Here we test various kinds of unavailability with a fake UserAgent.
3
4var CodeGradX = require('../codegradxlib.js');
5
6var _ = require('lodash');
7var when = require('when');
8var rest = require('rest');
9var interceptor = require('rest/interceptor');
10
11describe('CodeGradX', function () {
12 it('should be loaded', function () {
13 expect(CodeGradX).toBeDefined();
14 });
15
16 function initializer (state) {
17 state.servers = {
18 domain: '.localdomain',
19 names: ['e', 'e', 'x', 's'],
20 a: {
21 suffix: '/alive',
22 0: {
23 host: "a0.localdomain"
24 },
25 1: {
26 host: "a1.localdomain"
27 }
28 },
29 e: {
30 suffix: '/alive',
31 0: {
32 host: "e0.localdomain"
33 },
34 1: {
35 host: "e1.localdomain"
36 }
37 },
38 x: {
39 suffix: '/dbalive',
40 0: {
41 host: "x0.localdomain"
42 },
43 1: {
44 host: "x1.localdomain"
45 }
46 },
47 s: {
48 suffix: '/',
49 0: {
50 host: "s0.localdomain"
51 },
52 1: {
53 host: "s1.localdomain"
54 }
55 }
56 };
57 return state;
58 }
59
60/** make_fakeUserAgent creates HttpResponses (with only a status code)
61 as told by `history`. Once used, items in history are removed.
62*/
63
64 function make_fakeUserAgent (history) {
65 var fakeUserAgent = function (options) {
66 var state = CodeGradX.getCurrentState();
67 var i = _.findIndex(history, { path: options.path });
68 if ( i >= 0 ) {
69 state.debug("fakeUserAgent request " + options.path);
70 var item = history[i];
71 history.splice(i, 1);
72 if ( item.status > 0 ) {
73 return when({
74 status: { code: item.status },
75 headers: {}
76 }).delay(100 * Math.random());
77 } else {
78 return when.reject("Non responding server " + options.path);
79 }
80 } else {
81 // History was probably incomplete:
82 return when.reject("Unexpected URL " + options.path);
83 }
84 };
85 return fakeUserAgent;
86 }
87
88 it('should create a State', function (done) {
89 var state = new CodeGradX.State(initializer);
90 expect(state).toBeDefined();
91 expect(state instanceof CodeGradX.State).toBeTruthy();
92 done();
93 });
94
95 it('checks E servers: e0 ok', function (done) {
96 // since no E server is initially available, check a0 and a1.
97 var state = new CodeGradX.State(initializer);
98 state.userAgent = make_fakeUserAgent([
99 // implicit via checkServer('e', 0)
100 { path: 'http://e0.localdomain/alive',
101 status: 200
102 },
103 // implicit via checkServer('e', 1)
104 { path: 'http://e1.localdomain/alive',
105 status: 400
106 }
107 ]);
108 function faildone (reason) {
109 state.debug(reason).show();
110 fail(reason);
111 done();
112 }
113 expect(state).toBeDefined();
114 state.checkServers('e').then(function (descriptions) {
115 expect(descriptions).toBe(state.servers.e);
116 expect(descriptions[0].enabled).toBeTruthy();
117 expect(descriptions[0].lastError).not.toBeDefined();
118 expect(descriptions[1].enabled).toBeFalsy();
119 done();
120 }, faildone);
121 });
122
123 it('checks E servers: only e0 ok', function (done) {
124 // since no E server is initially available, check a0 and a1.
125 var state = new CodeGradX.State(initializer);
126 state.userAgent = make_fakeUserAgent([
127 // implicit via checkServer('e', 0)
128 { path: 'http://e0.localdomain/alive',
129 status: 200
130 },
131 // implicit via checkServer('e', 1)
132 { path: 'http://e1.localdomain/alive',
133 status: 0
134 }
135 ]);
136 function faildone (reason) {
137 state.debug(reason).show();
138 fail(reason);
139 done();
140 }
141 expect(state).toBeDefined();
142 state.checkServers('e').then(function (descriptions) {
143 expect(descriptions).toBe(state.servers.e);
144 expect(descriptions[0].enabled).toBeTruthy();
145 expect(descriptions[0].lastError).not.toBeDefined();
146 expect(descriptions[1].enabled).toBeFalsy();
147 done();
148 }, faildone);
149 });
150
151 it('checks E servers: e0 and e1 ok', function (done) {
152 // since no E server is initially available, check a0 and a1.
153 var state = new CodeGradX.State(initializer);
154 state.userAgent = make_fakeUserAgent([
155 // implicit via checkServer('e', 0)
156 { path: 'http://e0.localdomain/alive',
157 status: 200
158 },
159 // implicit via checkServer('e', 1)
160 { path: 'http://e1.localdomain/alive',
161 status: 200
162 }
163 ]);
164 function faildone (reason) {
165 state.debug(reason).show();
166 fail(reason);
167 done();
168 }
169 expect(state).toBeDefined();
170 state.checkServers('e').then(function (descriptions) {
171 expect(descriptions).toBe(state.servers.e);
172 expect(descriptions[0].enabled).toBeTruthy();
173 expect(descriptions[0].lastError).not.toBeDefined();
174 expect(descriptions[1].enabled).toBeTruthy();
175 expect(descriptions[1].lastError).not.toBeDefined();
176 done();
177 }, faildone);
178 });
179
180 it('request an E server once via e0', function (done) {
181 // since no E server is initially available, this will force a
182 // checkServers('e') which in turn will trigger checkServer('e', 0)
183 // and checkServer('e', 1).
184 var state = new CodeGradX.State(initializer);
185 state.userAgent = make_fakeUserAgent([
186 // implicit via checkServer('a', 0)
187 { path: 'http://e0.localdomain/alive',
188 status: 200
189 },
190 // implicit via checkServer('a', 1)
191 { path: 'http://e1.localdomain/alive',
192 status: 0
193 },
194 { path: 'http://e0.localdomain/foobar',
195 status: 201
196 }
197 ]);
198 function faildone (reason) {
199 state.debug(reason).show();
200 fail(reason);
201 done();
202 }
203 expect(state).toBeDefined();
204 state.sendAXServer('e', {
205 path: '/foobar'
206 }).then(function (response) {
207 expect(response.status.code).toBe(201);
208 expect(state.servers.e[0].enabled).toBeTruthy();
209 expect(state.servers.e[1].enabled).toBeFalsy();
210 done();
211 }, faildone);
212 });
213
214 it('request an E server twice via e0 while e1 ko', function (done) {
215 // since no E server is initially available, this will force a
216 // checkServers('e') which in turn will trigger checkServer('e', 0)
217 // and checkServer('e', 1).
218 var state = new CodeGradX.State(initializer);
219 var history = [
220 // implicit via checkServer('e', 0)
221 { path: 'http://e0.localdomain/alive',
222 status: 200
223 },
224 // implicit via checkServer('e', 1)
225 { path: 'http://e1.localdomain/alive',
226 status: 0
227 },
228 { path: 'http://e0.localdomain/foo',
229 status: 201
230 },
231 { path: 'http://e0.localdomain/bar',
232 status: 202
233 }
234 ];
235 state.userAgent = make_fakeUserAgent(history);
236 function faildone (reason) {
237 state.debug(reason).show();
238 fail(reason);
239 done();
240 }
241 expect(state).toBeDefined();
242 state.sendAXServer('e', {
243 path: '/foo'
244 }).then(function (response) {
245 expect(response.status.code).toBe(201);
246 expect(state.servers.e[0].enabled).toBeTruthy();
247 expect(state.servers.e[1].enabled).toBeFalsy();
248 state.sendAXServer('e', {
249 path: '/bar'
250 }).then(function (response2) {
251 expect(response2.status.code).toBe(202);
252 expect(state.servers.e[0].enabled).toBeTruthy();
253 expect(state.servers.e[1].enabled).toBeFalsy();
254 expect(history.length).toBe(0);
255 done();
256 }, faildone);
257 }, faildone);
258 });
259
260 it('request an E server twice via e0 while e1 becomes ok', function (done) {
261 // since no E server is initially available, this will force a
262 // checkServers('e') which in turn will trigger checkServer('e', 0)
263 // and checkServer('e', 1).
264 var state = new CodeGradX.State(initializer);
265 var history = [
266 // implicit via checkServer('e', 0)
267 { path: 'http://e0.localdomain/alive',
268 status: 200
269 },
270 // implicit via checkServer('e', 1)
271 { path: 'http://e1.localdomain/alive',
272 status: 200
273 },
274 { path: 'http://e0.localdomain/foo',
275 status: 201
276 },
277 { path: 'http://e0.localdomain/bar',
278 status: 202
279 },
280 // Cannot be used:
281 { path: 'http://e1.localdomain/bar',
282 status: 203
283 }
284 ];
285 state.userAgent = make_fakeUserAgent(history);
286 function faildone (reason) {
287 state.debug(reason).show();
288 fail(reason);
289 done();
290 }
291 expect(state).toBeDefined();
292 state.sendAXServer('e', {
293 path: '/foo'
294 }).then(function (response) {
295 expect(response.status.code).toBe(201);
296 expect(state.servers.e[0].enabled).toBeTruthy();
297 expect(state.servers.e[1].enabled).toBeTruthy();
298 state.sendAXServer('e', {
299 path: '/bar'
300 }).then(function (response2) {
301 expect(response2.status.code).toBe(202);
302 expect(state.servers.e[0].enabled).toBeTruthy();
303 expect(state.servers.e[1].enabled).toBeTruthy();
304 expect(history.length).toBe(1);
305 done();
306 }, faildone);
307 }, faildone);
308 });
309
310 it('request an E server twice via e0 and e1 (always ok)', function (done) {
311 // since no E server is initially available, this will force a
312 // checkServers('e') which in turn will trigger checkServer('e', 0)
313 // and checkServer('e', 1).
314 var state = new CodeGradX.State(initializer);
315 state.log.size = 50;
316 var history = [
317 // implicit via checkServer('e', 0)
318 { path: 'http://e0.localdomain/alive',
319 status: 200
320 },
321 // implicit via checkServer('e', 1)
322 { path: 'http://e1.localdomain/alive',
323 status: 200
324 },
325 // 1st request: ok
326 { path: 'http://e0.localdomain/foo',
327 status: 201
328 },
329 // 1st request: ok
330 { path: 'http://e1.localdomain/foo',
331 status: 201
332 },
333 // 2nd request: ko since e0 subitly failed!
334 { path: 'http://e0.localdomain/bar',
335 status: 0
336 },
337 // 2nd request: ok
338 { path: 'http://e1.localdomain/bar',
339 status: 212
340 }
341 ];
342 state.userAgent = make_fakeUserAgent(history);
343 function faildone (reason) {
344 state.debug(reason).show();
345 fail(reason);
346 done();
347 }
348 expect(state).toBeDefined();
349 state.sendAXServer('e', {
350 path: '/foo'
351 }).then(function (response) {
352 expect(response.status.code).toBe(201);
353 expect(state.servers.e[0].enabled).toBeTruthy();
354 expect(state.servers.e[1].enabled).toBeTruthy();
355 state.sendAXServer('e', {
356 path: '/bar'
357 }).then(function (response2) {
358 expect(response2.status.code).toBe(212);
359 expect(state.servers.e[1].enabled).toBeTruthy();
360 expect(state.servers.e[0].enabled).toBeFalsy();
361 //console.log(history);
362 // Promise requesting http://e1.localdomain/foo may not be completed:
363 expect(history.length).toBeLessThan(2);
364 done();
365 }, faildone);
366 }, faildone);
367 });
368
369 it('request an E server twice via e0 ', function (done) {
370 // since no E server is initially available, this will force a
371 // checkServers('e') which in turn will trigger checkServer('e', 0)
372 // and checkServer('e', 1).
373 var state = new CodeGradX.State(initializer);
374 state.log.size = 50;
375 state.userAgent = make_fakeUserAgent([
376 // implicit via checkServer('e', 0)
377 { path: 'http://e0.localdomain/alive',
378 status: 200
379 },
380 // implicit via checkServer('e', 1)
381 { path: 'http://e1.localdomain/alive',
382 status: 200
383 },
384 // 1st request: ok
385 { path: 'http://e0.localdomain/foo',
386 status: 201
387 },
388 // 2nd request: ko since e0 subitly failed!
389 { path: 'http://e0.localdomain/bar',
390 status: 0
391 },
392 // 2nd request: ko since e1 also subitly failed!
393 { path: 'http://e1.localdomain/bar',
394 status: 0
395 },
396 // checkServers('e') again: e0 still ko
397 { path: 'http://e0.localdomain/alive',
398 status: 402
399 },
400 // checkServers('e') again, e1 now ok
401 { path: 'http://e1.localdomain/alive',
402 status: 0
403 }
404 ]);
405 function faildone (reason) {
406 state.debug(reason).show();
407 fail(reason);
408 done();
409 }
410 expect(state).toBeDefined();
411 state.sendAXServer('e', {
412 path: '/foo'
413 }).then(function (response) {
414 expect(response.status.code).toBe(201);
415 expect(state.servers.e[0].enabled).toBeTruthy();
416 expect(state.servers.e[1].enabled).toBeTruthy();
417 state.sendAXServer('e', {
418 path: '/bar'
419 }).then(function (response2) {
420 faildone();
421 }, function (reason) {
422 done();
423 });
424 }, faildone);
425 });
426
427
428});