1 |
|
2 |
|
3 |
|
4 | var CodeGradX = require('../codegradxlib.js');
|
5 |
|
6 | var _ = require('lodash');
|
7 | var when = require('when');
|
8 | var rest = require('rest');
|
9 | var interceptor = require('rest/interceptor');
|
10 |
|
11 | describe('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 |
|
61 |
|
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 |
|
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 |
|
97 | var state = new CodeGradX.State(initializer);
|
98 | state.userAgent = make_fakeUserAgent([
|
99 |
|
100 | { path: 'http://e0.localdomain/alive',
|
101 | status: 200
|
102 | },
|
103 |
|
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 |
|
125 | var state = new CodeGradX.State(initializer);
|
126 | state.userAgent = make_fakeUserAgent([
|
127 |
|
128 | { path: 'http://e0.localdomain/alive',
|
129 | status: 200
|
130 | },
|
131 |
|
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 |
|
153 | var state = new CodeGradX.State(initializer);
|
154 | state.userAgent = make_fakeUserAgent([
|
155 |
|
156 | { path: 'http://e0.localdomain/alive',
|
157 | status: 200
|
158 | },
|
159 |
|
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 |
|
182 |
|
183 |
|
184 | var state = new CodeGradX.State(initializer);
|
185 | state.userAgent = make_fakeUserAgent([
|
186 |
|
187 | { path: 'http://e0.localdomain/alive',
|
188 | status: 200
|
189 | },
|
190 |
|
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 |
|
216 |
|
217 |
|
218 | var state = new CodeGradX.State(initializer);
|
219 | var history = [
|
220 |
|
221 | { path: 'http://e0.localdomain/alive',
|
222 | status: 200
|
223 | },
|
224 |
|
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 |
|
262 |
|
263 |
|
264 | var state = new CodeGradX.State(initializer);
|
265 | var history = [
|
266 |
|
267 | { path: 'http://e0.localdomain/alive',
|
268 | status: 200
|
269 | },
|
270 |
|
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 |
|
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 |
|
312 |
|
313 |
|
314 | var state = new CodeGradX.State(initializer);
|
315 | state.log.size = 50;
|
316 | var history = [
|
317 |
|
318 | { path: 'http://e0.localdomain/alive',
|
319 | status: 200
|
320 | },
|
321 |
|
322 | { path: 'http://e1.localdomain/alive',
|
323 | status: 200
|
324 | },
|
325 |
|
326 | { path: 'http://e0.localdomain/foo',
|
327 | status: 201
|
328 | },
|
329 |
|
330 | { path: 'http://e1.localdomain/foo',
|
331 | status: 201
|
332 | },
|
333 |
|
334 | { path: 'http://e0.localdomain/bar',
|
335 | status: 0
|
336 | },
|
337 |
|
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 |
|
362 |
|
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 |
|
371 |
|
372 |
|
373 | var state = new CodeGradX.State(initializer);
|
374 | state.log.size = 50;
|
375 | state.userAgent = make_fakeUserAgent([
|
376 |
|
377 | { path: 'http://e0.localdomain/alive',
|
378 | status: 200
|
379 | },
|
380 |
|
381 | { path: 'http://e1.localdomain/alive',
|
382 | status: 200
|
383 | },
|
384 |
|
385 | { path: 'http://e0.localdomain/foo',
|
386 | status: 201
|
387 | },
|
388 |
|
389 | { path: 'http://e0.localdomain/bar',
|
390 | status: 0
|
391 | },
|
392 |
|
393 | { path: 'http://e1.localdomain/bar',
|
394 | status: 0
|
395 | },
|
396 |
|
397 | { path: 'http://e0.localdomain/alive',
|
398 | status: 402
|
399 | },
|
400 |
|
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 | });
|