UNPKG

15.9 kBJavaScriptView Raw
1"use strict"
2
3var co = require('co'),
4 f = require('util').format,
5 Logger = require('../lib/logger'),
6 assert = require('assert');
7
8// Polyfill Promise if none exists
9if(!global.Promise) {
10 require('es6-promise').polyfill();
11}
12
13// Get babel polyfill
14require("babel-polyfill");
15
16describe('ReplSet', function() {
17 describe('manager', function() {
18 it('establish server version', function(done) {
19 this.timeout(1000000);
20
21 co(function*() {
22 var ReplSet = require('../').ReplSet;
23
24 // Create new instance
25 var topology = new ReplSet('mongod', [{
26 // mongod process options
27 options: {
28 bind_ip: 'localhost',
29 port: 31000,
30 dbpath: f('%s/../db/31000', __dirname)
31 }
32 }, {
33 // mongod process options
34 options: {
35 bind_ip: 'localhost',
36 port: 31001,
37 dbpath: f('%s/../db/31001', __dirname)
38 }
39 }, {
40 // Type of node
41 arbiter: true,
42 // mongod process options
43 options: {
44 bind_ip: 'localhost',
45 port: 31002,
46 dbpath: f('%s/../db/31002', __dirname)
47 }
48 }], {
49 replSet: 'rs'
50 });
51
52 // Perform discovery
53 var version = yield topology.discover();
54 // Expect 3 integers
55 assert.ok(typeof version.version[0] == 'number');
56 assert.ok(typeof version.version[1] == 'number');
57 assert.ok(typeof version.version[2] == 'number');
58 done();
59 }).catch(function(err) {
60 console.log(err.stack);
61 });
62 });
63
64 it('start simple replicaset with 1 primary, 1 secondary and one arbiter', function(done) {
65 this.timeout(1000000);
66
67 co(function*() {
68 var ReplSet = require('../').ReplSet;
69
70 // Create new instance
71 var topology = new ReplSet('mongod', [{
72 // mongod process options
73 options: {
74 bind_ip: 'localhost',
75 port: 31000,
76 dbpath: f('%s/../db/31000', __dirname)
77 }
78 }, {
79 // mongod process options
80 options: {
81 bind_ip: 'localhost',
82 port: 31001,
83 dbpath: f('%s/../db/31001', __dirname)
84 }
85 }, {
86 // Type of node
87 arbiter: true,
88 // mongod process options
89 options: {
90 bind_ip: 'localhost',
91 port: 31002,
92 dbpath: f('%s/../db/31002', __dirname)
93 }
94 }], {
95 replSet: 'rs'
96 });
97
98 // Purge any directories
99 yield topology.purge();
100
101 // Start set
102 yield topology.start();
103
104 // Stop the set
105 yield topology.stop();
106
107 // Finish up
108 done();
109 }).catch(function(err) {
110 console.log(err.stack);
111 });
112 });
113
114 it('start simple ssl replicaset with 1 primary, 1 secondary and one arbiter', function(done) {
115 this.timeout(200000);
116
117 co(function*() {
118 var ReplSet = require('../').ReplSet;
119
120 // Create new instance
121 var topology = new ReplSet('mongod', [{
122 // mongod process options
123 options: {
124 bind_ip: 'localhost',
125 port: 31000,
126 dbpath: f('%s/../db/31000', __dirname),
127
128 // SSL server instance options
129 sslOnNormalPorts:null,
130 sslPEMKeyFile: f('%s/ssl/server.pem', __dirname),
131 sslAllowInvalidCertificates:null
132 }
133 }, {
134 // mongod process options
135 options: {
136 bind_ip: 'localhost',
137 port: 31001,
138 dbpath: f('%s/../db/31001', __dirname),
139
140 // SSL server instance options
141 sslOnNormalPorts:null,
142 sslPEMKeyFile: f('%s/ssl/server.pem', __dirname),
143 sslAllowInvalidCertificates:null
144 }
145 }, {
146 // Type of node
147 arbiter: true,
148 // mongod process options
149 options: {
150 bind_ip: 'localhost',
151 port: 31002,
152 dbpath: f('%s/../db/31002', __dirname),
153
154 // SSL server instance options
155 sslOnNormalPorts:null,
156 sslPEMKeyFile: f('%s/ssl/server.pem', __dirname),
157 sslAllowInvalidCertificates:null
158 }
159 }], {
160 // SSL client instance options
161 replSet: 'rs',
162 ssl:true,
163 rejectUnauthorized:false
164 });
165
166 // Perform discovery
167 var result = yield topology.discover();
168 // Skip ssl test
169 if(!result.ssl) return done();
170
171 // Purge any directories
172 yield topology.purge();
173
174 // Start set
175 yield topology.start();
176
177 // Stop the set
178 yield topology.stop();
179
180 // Finish up
181 done();
182 }).catch(function(err) {
183 console.log(err.stack);
184 });
185 });
186
187 it('stepdown primary', function(done) {
188 this.timeout(200000);
189
190 co(function*() {
191 var ReplSet = require('../').ReplSet;
192
193 // Create new instance
194 var topology = new ReplSet('mongod', [{
195 // mongod process options
196 options: {
197 bind_ip: 'localhost',
198 port: 31000,
199 dbpath: f('%s/../db/31000', __dirname)
200 }
201 }, {
202 // mongod process options
203 options: {
204 bind_ip: 'localhost',
205 port: 31001,
206 dbpath: f('%s/../db/31001', __dirname)
207 }
208 }, {
209 // Type of node
210 arbiter: true,
211 // mongod process options
212 options: {
213 bind_ip: 'localhost',
214 port: 31002,
215 dbpath: f('%s/../db/31002', __dirname)
216 }
217 }], {
218 replSet: 'rs',
219 electionCycleWaitMS: 5000,
220 retryWaitMS: 1000
221 });
222
223 // Purge any directories
224 yield topology.purge();
225
226 // Start set
227 yield topology.start();
228
229 // Step down primary and block until we have a new primary
230 yield topology.stepDownPrimary(false, {stepDownSecs: 0, force:true});
231
232 // Step down primary and immediately return
233 yield topology.stepDownPrimary(true, {stepDownSecs: 0, force:true});
234
235 // Block waiting for a new primary to be elected
236 yield topology.waitForPrimary();
237
238 // Stop the set
239 yield topology.stop();
240
241 // Finish up
242 done();
243 }).catch(function(err) {
244 console.log(err.stack);
245 });
246 });
247
248 it('add new member to set', function(done) {
249 this.timeout(200000);
250
251 co(function*() {
252 var ReplSet = require('../').ReplSet;
253
254 // Create new instance
255 var topology = new ReplSet('mongod', [{
256 // mongod process options
257 options: {
258 bind_ip: 'localhost',
259 port: 31000,
260 dbpath: f('%s/../db/31000', __dirname)
261 }
262 }, {
263 // mongod process options
264 options: {
265 bind_ip: 'localhost',
266 port: 31001,
267 dbpath: f('%s/../db/31001', __dirname)
268 }
269 }, {
270 // Type of node
271 arbiter: true,
272 // mongod process options
273 options: {
274 bind_ip: 'localhost',
275 port: 31002,
276 dbpath: f('%s/../db/31002', __dirname)
277 }
278 }], {
279 replSet: 'rs',
280 electionCycleWaitMS: 5000,
281 retryWaitMS: 1000
282 });
283
284 // Purge any directories
285 yield topology.purge();
286
287 // Start set
288 yield topology.start();
289
290 // Add a new member to the set
291 var manager = yield topology.addMember({
292 options: {
293 bind_ip: 'localhost',
294 port: 31003,
295 dbpath: f('%s/../db/31003', __dirname)
296 }
297 }, {
298 returnImmediately: false, force:false
299 });
300
301 // Assert we have the expected number of instances
302 var primary = yield topology.primary();
303 var ismaster = yield primary.ismaster();
304 assert.equal(1, ismaster.arbiters.length);
305 assert.equal(3, ismaster.hosts.length);
306
307 // Stop the set
308 yield topology.stop();
309
310 // Finish up
311 done();
312 }).catch(function(err) {
313 console.log(err.stack);
314 });
315 });
316
317 it('add new member to set with high priority', function(done) {
318 this.timeout(200000);
319
320 // // Set the info level
321 // Logger.setLevel('info');
322
323 co(function*() {
324 var ReplSet = require('../').ReplSet;
325
326 // Create new instance
327 var topology = new ReplSet('mongod', [{
328 // mongod process options
329 options: {
330 bind_ip: 'localhost',
331 port: 31000,
332 dbpath: f('%s/../db/31000', __dirname)
333 }
334 }, {
335 // mongod process options
336 options: {
337 bind_ip: 'localhost',
338 port: 31001,
339 dbpath: f('%s/../db/31001', __dirname)
340 }
341 }, {
342 // Type of node
343 arbiter: true,
344 // mongod process options
345 options: {
346 bind_ip: 'localhost',
347 port: 31002,
348 dbpath: f('%s/../db/31002', __dirname)
349 }
350 }], {
351 replSet: 'rs',
352 electionCycleWaitMS: 5000,
353 retryWaitMS: 1000
354 });
355
356 // Purge any directories
357 yield topology.purge();
358
359 // Start set
360 yield topology.start();
361
362 // Add a new member to the set
363 var manager = yield topology.addMember({
364 priority: 20,
365 options: {
366 bind_ip: 'localhost',
367 port: 31003,
368 dbpath: f('%s/../db/31003', __dirname),
369 }
370 }, {
371 returnImmediately: false, force:false
372 });
373
374 // Assert we have the expected number of instances
375 var primary = yield topology.primary();
376 var ismaster = yield primary.ismaster();
377 assert.equal(1, ismaster.arbiters.length);
378 assert.equal(3, ismaster.hosts.length);
379
380 // Stop the set
381 yield topology.stop();
382
383 // Finish up
384 done();
385 }).catch(function(err) {
386 console.log(err.stack);
387 });
388 });
389
390 it('remove member from set', function(done) {
391 this.timeout(200000);
392
393 co(function*() {
394 var ReplSet = require('../').ReplSet;
395
396 // Create new instance
397 var topology = new ReplSet('mongod', [{
398 // mongod process options
399 options: {
400 bind_ip: 'localhost',
401 port: 31000,
402 dbpath: f('%s/../db/31000', __dirname)
403 }
404 }, {
405 // mongod process options
406 options: {
407 bind_ip: 'localhost',
408 port: 31001,
409 dbpath: f('%s/../db/31001', __dirname)
410 }
411 }, {
412 // Type of node
413 arbiter: true,
414 // mongod process options
415 options: {
416 bind_ip: 'localhost',
417 port: 31002,
418 dbpath: f('%s/../db/31002', __dirname)
419 }
420 }, {
421 // mongod process options
422 options: {
423 bind_ip: 'localhost',
424 port: 31003,
425 dbpath: f('%s/../db/31003', __dirname)
426 }
427 }], {
428 replSet: 'rs',
429 electionCycleWaitMS: 5000,
430 retryWaitMS: 1000
431 });
432
433 // Purge any directories
434 yield topology.purge();
435
436 // Start set
437 yield topology.start();
438
439 // Get all the secondaries
440 var secondaries = yield topology.secondaries();
441
442 // Remove a member from the set
443 yield topology.removeMember(secondaries[0], {
444 returnImmediately: false, force: false
445 });
446
447 // Assert we have the expected number of instances
448 var primary = yield topology.primary();
449 var ismaster = yield primary.ismaster();
450 assert.equal(1, ismaster.arbiters.length);
451 assert.equal(2, ismaster.hosts.length);
452
453 // Stop the set
454 yield topology.stop();
455
456 // Finish up
457 done();
458 }).catch(function(err) {
459 console.log(err.stack);
460 });
461 });
462
463 it('put secondary in maintenance mode', function(done) {
464 this.timeout(200000);
465
466 co(function*() {
467 var ReplSet = require('../').ReplSet;
468
469 // Create new instance
470 var topology = new ReplSet('mongod', [{
471 // mongod process options
472 options: {
473 bind_ip: 'localhost',
474 port: 31000,
475 dbpath: f('%s/../db/31000', __dirname)
476 }
477 }, {
478 // mongod process options
479 options: {
480 bind_ip: 'localhost',
481 port: 31001,
482 dbpath: f('%s/../db/31001', __dirname)
483 }
484 }, {
485 // Type of node
486 arbiter: true,
487 // mongod process options
488 options: {
489 bind_ip: 'localhost',
490 port: 31002,
491 dbpath: f('%s/../db/31002', __dirname)
492 }
493 }], {
494 replSet: 'rs',
495 electionCycleWaitMS: 5000,
496 retryWaitMS: 1000
497 });
498
499 // Purge any directories
500 yield topology.purge();
501
502 // Start set
503 yield topology.start();
504
505 // Get all the secondaries
506 var secondaries = yield topology.secondaries();
507
508 // Put secondary in maintenance mode
509 yield topology.maintenance(true, secondaries[0], {
510 returnImmediately: false
511 });
512
513 // Assert we have the expected number of instances
514 var ismaster = yield secondaries[0].ismaster();
515 assert.equal(false, ismaster.secondary);
516 assert.equal(false, ismaster.ismaster);
517
518 // Wait for server to come back
519 yield topology.maintenance(false, secondaries[0], {
520 returnImmediately: false
521 });
522
523 var ismaster = yield secondaries[0].ismaster();
524 assert.equal(true, ismaster.secondary);
525
526 // Stop the set
527 yield topology.stop();
528
529 // Finish up
530 done();
531 }).catch(function(err) {
532 console.log(err.stack);
533 });
534 });
535
536 it('reconfigure using existing configuration', function(done) {
537 this.timeout(200000);
538
539 co(function*() {
540 var ReplSet = require('../').ReplSet;
541
542 // Create new instance
543 var topology = new ReplSet('mongod', [{
544 // mongod process options
545 options: {
546 bind_ip: 'localhost',
547 port: 31000,
548 dbpath: f('%s/../db/31000', __dirname)
549 }
550 }, {
551 // mongod process options
552 options: {
553 bind_ip: 'localhost',
554 port: 31001,
555 dbpath: f('%s/../db/31001', __dirname)
556 }
557 }, {
558 // mongod process options
559 options: {
560 bind_ip: 'localhost',
561 port: 31002,
562 dbpath: f('%s/../db/31002', __dirname)
563 }
564 }], {
565 replSet: 'rs',
566 electionCycleWaitMS: 5000,
567 retryWaitMS: 1000
568 });
569
570 // Purge any directories
571 yield topology.purge();
572
573 // Start set
574 yield topology.start();
575
576 // Get the configuration
577 var config = JSON.parse(JSON.stringify(topology.configurations[0]));
578 config.members[2].priority = 10;
579
580 // Force the reconfiguration
581 yield topology.reconfigure(config, {
582 returnImmediately:false, force:false
583 })
584
585 // Get the current configuration
586 var primary = yield topology.primary();
587 var currentConfig = yield topology.configuration(primary);
588 assert.equal(10, currentConfig.members[2].priority);
589
590 // Stop the set
591 yield topology.stop();
592
593 // Finish up
594 done();
595 }).catch(function(err) {
596 console.log(err.stack);
597 });
598 });
599 });
600});