Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to start MongoDB using --replSet #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ dist
build
*.egg-info
*.egg
*.eggs
__pycache__
6 changes: 5 additions & 1 deletion mongobox/mongobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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")

Expand Down
9 changes: 8 additions & 1 deletion mongobox/nose_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
24 changes: 21 additions & 3 deletions tests/test_mongobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))


Expand Down Expand Up @@ -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')