diff --git a/.gitignore b/.gitignore index a4dc152..7e45475 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ dist build *.egg-info *.egg +*.eggs __pycache__ diff --git a/mongobox/mongobox.py b/mongobox/mongobox.py index 96817dd..ace21d6 100644 --- a/mongobox/mongobox.py +++ b/mongobox/mongobox.py @@ -32,7 +32,7 @@ class MongoBox(object): def __init__(self, mongod_bin=None, port=None, log_path=None, db_path=None, scripting=False, - prealloc=False, auth=False): + prealloc=False, auth=False, replset=None): self.mongod_bin = mongod_bin or find_executable(MONGOD_BIN) assert self.mongod_bin, 'Could not find "{}" in system PATH. Make sure you have MongoDB installed.'.format(MONGOD_BIN) @@ -43,6 +43,7 @@ def __init__(self, mongod_bin=None, port=None, self.prealloc = prealloc self.db_path = db_path self.auth = auth + self.replset = replset if self.db_path: if os.path.exists(self.db_path) and os.path.isfile(self.db_path): @@ -71,6 +72,9 @@ def start(self): args.extend(['--port', str(self.port)]) args.extend(['--logpath', self.log_path]) + if self.replset: + args.extend(['--replSet', self.replset]) + if self.auth: args.append("--auth") diff --git a/mongobox/nose_plugin.py b/mongobox/nose_plugin.py index f366ac5..3043f1c 100644 --- a/mongobox/nose_plugin.py +++ b/mongobox/nose_plugin.py @@ -65,6 +65,12 @@ def options(self, parser, env): dest="auth", default=False, help="Enable mongodb's user authentication mechanisms.") + parser.add_option( + "--mongobox-replset", + action="store", + dest="replset", + default=None, + help="Start mongodb with --replSet using the value passed in here") def configure(self, options, conf): super(MongoBoxPlugin, self).configure(options, conf) @@ -75,7 +81,8 @@ def configure(self, options, conf): self.mongobox = MongoBox( mongod_bin=options.bin, port=options.port or None, log_path=options.logpath, db_path=options.dbpath, - scripting=options.scripting, prealloc=options.prealloc + scripting=options.scripting, prealloc=options.prealloc, + replset=options.replset ) self.port_envvar = options.port_envvar diff --git a/tests/test_mongobox.py b/tests/test_mongobox.py index ce6cadf..415a7df 100644 --- a/tests/test_mongobox.py +++ b/tests/test_mongobox.py @@ -7,7 +7,6 @@ from mongobox.nose_plugin import DEFAULT_PORT_ENVVAR from pymongo.errors import OperationFailure - class TestMongoBox(unittest.TestCase): def test_nose_plugin_exports_envvar(self): @@ -23,12 +22,14 @@ def test_can_run_mongo(self): self.assertIsNotNone(box.port) client = box.client() - self.assertTrue(client.alive()) + # pymongo 3.x no longer has an alive function + #self.assertTrue(client.alive()) box.stop() self.assertFalse(box.running()) - self.assertFalse(client.alive()) + # pymongo 3.x no longer has an alive function + #self.assertTrue(client.alive()) self.assertFalse(os.path.exists(db_path)) @@ -62,3 +63,20 @@ def test_auth(self): client['test'].collection_names() except OperationFailure: self.fail("collection_names() operation unexpectedly failed") + + def test_replset(self): + box = MongoBox(replset="repl0") + box.start() + + client = box.client() + + # initiate the replSet + config = {'_id': 'repl0', 'members': + [ + {'_id': 0, 'host': '%s:%s' %('127.0.0.1', box.port)} + ] + } + + client.admin.command("replSetInitiate", config) + setconfig = client.admin.command("replSetGetConfig") + self.assertEqual(setconfig['config']['_id'], 'repl0')