UNPKG

12.7 kBJavaScriptView Raw
1'use strict';
2
3var childProcess = require('child_process');
4var fs = require('fs');
5var http = require('http');
6var path = require('path');
7var temp = require('temp');
8
9var mocha = require('mocha');
10var describe = mocha.describe;
11var it = mocha.it;
12var afterEach = mocha.afterEach;
13var expect = require('expect');
14
15var CLI_PATH = path.resolve(__dirname, '../bin/wait-on');
16
17temp.track(); // cleanup files on exit
18
19function execCLI(args, options) {
20 var fullArgs = [CLI_PATH].concat(args);
21 return childProcess.spawn(process.execPath, fullArgs, options);
22}
23
24var FAST_OPTS = '-t 1000 -i 100 -w 100'.split(' ');
25
26describe('cli', function () {
27 this.timeout(3000);
28 var httpServer = null;
29
30 afterEach(function (done) {
31 if (httpServer) {
32 httpServer.close();
33 httpServer = null;
34 }
35 done();
36 });
37
38 it('should succeed when file resources are available', function (done) {
39 temp.mkdir({}, function (err, dirPath) {
40 var opts = {
41 resources: [
42 path.resolve(dirPath, 'foo'),
43 path.resolve(dirPath, 'bar')
44 ]
45 };
46 fs.writeFileSync(opts.resources[0], 'data1');
47 fs.writeFileSync(opts.resources[1], 'data2');
48
49 execCLI(opts.resources.concat(FAST_OPTS), {})
50 .on('exit', function (code) {
51 expect(code).toBe(0);
52 done();
53 });
54 });
55 });
56
57 it('should succeed when file resources are become available later', function (done) {
58 temp.mkdir({}, function (err, dirPath) {
59 var opts = {
60 resources: [
61 path.resolve(dirPath, 'foo'),
62 path.resolve(dirPath, 'bar')
63 ]
64 };
65
66 setTimeout(function () {
67 fs.writeFile(opts.resources[0], 'data1', function () {});
68 fs.writeFile(opts.resources[1], 'data2', function () {});
69 }, 300);
70
71 execCLI(opts.resources.concat(FAST_OPTS), {})
72 .on('exit', function (code) {
73 expect(code).toBe(0);
74 done();
75 });
76 });
77 });
78
79 it('should succeed when http resources become available later', function (done) {
80 var opts = {
81 resources: [
82 'http://localhost:8123',
83 'http://localhost:8123/foo'
84 ]
85 };
86
87 setTimeout(function () {
88 httpServer = http.createServer()
89 .on('request', function (req, res) {
90 res.end('data');
91 });
92 httpServer.listen(8123, 'localhost');
93 }, 300);
94
95 execCLI(opts.resources.concat(FAST_OPTS), {})
96 .on('exit', function (code) {
97 expect(code).toBe(0);
98 done();
99 });
100 });
101
102 it('should succeed when http GET resources become available later', function (done) {
103 var opts = {
104 resources: [
105 'http-get://localhost:8124',
106 'http-get://localhost:8124/foo'
107 ]
108 };
109
110 setTimeout(function () {
111 httpServer = http.createServer()
112 .on('request', function (req, res) {
113 res.end('data');
114 });
115 httpServer.listen(8124, 'localhost');
116 }, 300);
117
118 execCLI(opts.resources.concat(FAST_OPTS), {})
119 .on('exit', function (code) {
120 expect(code).toBe(0);
121 done();
122 });
123 });
124
125 /*
126 it('should succeed when an https resource is available', function (done) {
127 var opts = {
128 resources: [
129 'https://www.google.com'
130 ]
131 };
132
133 execCLI(opts.resources.concat(FAST_OPTS), {})
134 .on('exit', function (code) {
135 expect(code).toBe(0);
136 done();
137 });
138 });
139 */
140
141 it('should succeed when a service is listening to tcp port', function (done) {
142 var opts = {
143 resources: [
144 'tcp:localhost:3030',
145 'tcp:3030'
146 ]
147 };
148
149 setTimeout(function () {
150 httpServer = http.createServer()
151 .on('request', function (req, res) {
152 res.end('data');
153 });
154 httpServer.listen(3030, 'localhost');
155 }, 300);
156
157 execCLI(opts.resources.concat(FAST_OPTS), {})
158 .on('exit', function (code) {
159 expect(code).toBe(0);
160 done();
161 });
162 });
163
164 it('should succeed when a service is listening to a socket', function (done) {
165 var socketPath;
166 temp.mkdir({}, function (err, dirPath) {
167 socketPath = path.resolve(dirPath, 'sock');
168 var opts = {
169 resources: [
170 'socket:'+socketPath
171 ]
172 };
173
174 setTimeout(function () {
175 httpServer = http.createServer();
176 httpServer.listen(socketPath);
177 }, 300);
178
179 execCLI(opts.resources.concat(FAST_OPTS), {})
180 .on('exit', function (code) {
181 expect(code).toBe(0);
182 done();
183 });
184 });
185 });
186
187 it('should succeed when a http service is listening to a socket', function (done) {
188 var socketPath;
189 temp.mkdir({}, function (err, dirPath) {
190 socketPath = path.resolve(dirPath, 'sock');
191 var opts = {
192 resources: [
193 'http://unix:' + socketPath + ':/',
194 'http://unix:' + socketPath + ':/foo'
195 ]
196 };
197
198 setTimeout(function () {
199 httpServer = http.createServer()
200 .on('request', function (req, res) {
201 res.end('data');
202 });
203 httpServer.listen(socketPath);
204 }, 300);
205
206 execCLI(opts.resources.concat(FAST_OPTS), {})
207 .on('exit', function (code) {
208 expect(code).toBe(0);
209 done();
210 });
211 });
212 });
213
214 it('should succeed when a http GET service is listening to a socket', function (done) {
215 var socketPath;
216 temp.mkdir({}, function (err, dirPath) {
217 socketPath = path.resolve(dirPath, 'sock');
218 var opts = {
219 resources: [
220 'http-get://unix:' + socketPath + ':/',
221 'http-get://unix:' + socketPath + ':/foo'
222 ]
223 };
224
225 setTimeout(function () {
226 httpServer = http.createServer()
227 .on('request', function (req, res) {
228 res.end('data');
229 });
230 httpServer.listen(socketPath);
231 }, 300);
232
233 execCLI(opts.resources.concat(FAST_OPTS), {})
234 .on('exit', function (code) {
235 expect(code).toBe(0);
236 done();
237 });
238 });
239 });
240
241 // Error situations
242
243 it('should timeout when all resources are not available and timout option is specified', function (done) {
244 temp.mkdir({}, function (err, dirPath) {
245 var opts = {
246 resources: [ path.resolve(dirPath, 'foo') ],
247 timeout: 1000
248 };
249 execCLI(opts.resources.concat(FAST_OPTS), {})
250 .on('exit', function (code) {
251 expect(code).toNotBe(0);
252 done();
253 });
254 });
255 });
256
257 it('should timeout when some resources are not available and timout option is specified', function (done) {
258 temp.mkdir({}, function (err, dirPath) {
259 var opts = {
260 resources: [
261 path.resolve(dirPath, 'foo'),
262 path.resolve(dirPath, 'bar')
263 ],
264 timeout: 1000
265 };
266 fs.writeFile(opts.resources[0], 'data', function () {});
267 execCLI(opts.resources.concat(FAST_OPTS), {})
268 .on('exit', function (code) {
269 expect(code).toNotBe(0);
270 done();
271 });
272 });
273 });
274
275 it('should timeout when an http resource returns 404', function (done) {
276 var opts = {
277 resources: [
278 'http://localhost:3998'
279 ],
280 timeout: 1000,
281 interval: 100,
282 window: 100
283 };
284
285 setTimeout(function () {
286 httpServer = http.createServer()
287 .on('request', function (req, res) {
288 res.statusCode = 404;
289 res.end('data');
290 });
291 httpServer.listen(3998, 'localhost');
292 }, 300);
293 execCLI(opts.resources.concat(FAST_OPTS), {})
294 .on('exit', function (code) {
295 expect(code).toNotBe(0);
296 done();
297 });
298 });
299
300 it('should timeout when an http resource is not available', function (done) {
301 var opts = {
302 resources: [
303 'http://localhost:3999'
304 ],
305 timeout: 1000,
306 interval: 100,
307 window: 100
308 };
309
310 execCLI(opts.resources.concat(FAST_OPTS), {})
311 .on('exit', function (code) {
312 expect(code).toNotBe(0);
313 done();
314 });
315 });
316
317 it('should timeout when an http GET resource is not available', function (done) {
318 var opts = {
319 resources: [
320 'http-get://localhost:3999'
321 ],
322 timeout: 1000,
323 interval: 100,
324 window: 100
325 };
326
327 execCLI(opts.resources.concat(FAST_OPTS), {})
328 .on('exit', function (code) {
329 expect(code).toNotBe(0);
330 done();
331 });
332 });
333
334 it('should timeout when an https resource is not available', function (done) {
335 var opts = {
336 resources: [
337 'https://localhost:3010/foo/bar'
338 ],
339 timeout: 1000,
340 interval: 100,
341 window: 100
342 };
343
344 execCLI(opts.resources.concat(FAST_OPTS), {})
345 .on('exit', function (code) {
346 expect(code).toNotBe(0);
347 done();
348 });
349 });
350
351 it('should timeout when an https GET resource is not available', function (done) {
352 var opts = {
353 resources: [
354 'https-get://localhost:3010/foo/bar'
355 ],
356 timeout: 1000,
357 interval: 100,
358 window: 100
359 };
360
361 execCLI(opts.resources.concat(FAST_OPTS), {})
362 .on('exit', function (code) {
363 expect(code).toNotBe(0);
364 done();
365 });
366 });
367
368 it('should timeout when a service is not listening to tcp port', function (done) {
369 var opts = {
370 resources: [
371 'tcp:localhost:3010'
372 ],
373 timeout: 1000
374 };
375
376 execCLI(opts.resources.concat(FAST_OPTS), {})
377 .on('exit', function (code) {
378 expect(code).toNotBe(0);
379 done();
380 });
381 });
382
383 it('should timeout when a service is not listening to a socket', function (done) {
384 var socketPath;
385 temp.mkdir({}, function (err, dirPath) {
386 socketPath = path.resolve(dirPath, 'sock');
387 var opts = {
388 resources: [
389 'socket:'+socketPath
390 ],
391 timeout: 1000,
392 interval: 100,
393 window: 100
394 };
395
396 execCLI(opts.resources.concat(FAST_OPTS), {})
397 .on('exit', function (code) {
398 expect(code).toNotBe(0);
399 done();
400 });
401 });
402 });
403
404 it('should timeout when an http service listening to a socket returns 404', function (done) {
405 var socketPath;
406 temp.mkdir({}, function (err, dirPath) {
407 socketPath = path.resolve(dirPath, 'sock');
408 var opts = {
409 resources: [
410 'http://unix:' + socketPath + ':/',
411 'http://unix:' + socketPath + ':/foo'
412 ],
413 timeout: 1000,
414 interval: 100,
415 window: 100
416 };
417
418 setTimeout(function () {
419 httpServer = http.createServer()
420 .on('request', function (req, res) {
421 res.statusCode = 404;
422 res.end('data');
423 });
424 httpServer.listen(socketPath);
425 }, 300);
426
427 execCLI(opts.resources.concat(FAST_OPTS), {})
428 .on('exit', function (code) {
429 expect(code).toNotBe(0);
430 done();
431 });
432 });
433 });
434
435
436 it('should succeed when file resources are not available in reverse mode', function (done) {
437 temp.mkdir({}, function (err, dirPath) {
438 var opts = {
439 resources: [
440 path.resolve(dirPath, 'foo'),
441 path.resolve(dirPath, 'bar')
442 ],
443 };
444 var OPTS = FAST_OPTS.concat(['-r']);
445 execCLI(opts.resources.concat(OPTS), {})
446 .on('exit', function (code) {
447 expect(code).toBe(0);
448 done();
449 });
450 });
451 });
452
453 it('should succeed when file resources are not available later in reverse mode', function (done) {
454 temp.mkdir({}, function (err, dirPath) {
455 var opts = {
456 resources: [
457 path.resolve(dirPath, 'foo'),
458 path.resolve(dirPath, 'bar')
459 ],
460 };
461 fs.writeFileSync(opts.resources[0], 'data1');
462 fs.writeFileSync(opts.resources[1], 'data2');
463 setTimeout(function () {
464 fs.unlinkSync(opts.resources[0]);
465 fs.unlinkSync(opts.resources[1]);
466 }, 300);
467 var OPTS = FAST_OPTS.concat(['-r']);
468 execCLI(opts.resources.concat(OPTS), {})
469 .on('exit', function (code) {
470 expect(code).toBe(0);
471 done();
472 });
473 });
474 });
475
476 it('should timeout when file resources are available in reverse mode', function (done) {
477 temp.mkdir({}, function (err, dirPath) {
478 var opts = {
479 resources: [
480 path.resolve(dirPath, 'foo'),
481 path.resolve(dirPath, 'bar')
482 ],
483 };
484 fs.writeFileSync(opts.resources[0], 'data1');
485 fs.writeFileSync(opts.resources[1], 'data2');
486 var OPTS = FAST_OPTS.concat(['-r']);
487 execCLI(opts.resources.concat(OPTS), {})
488 .on('exit', function (code) {
489 expect(code).toNotBe(0);
490 done();
491 });
492 });
493 });
494
495});