Skip to content

Instantly share code, notes, and snippets.

@renctan
Created August 4, 2012 19:38
Show Gist options
  • Save renctan/3259512 to your computer and use it in GitHub Desktop.
Save renctan/3259512 to your computer and use it in GitHub Desktop.
Answer for Problem Set#4
#! /usr/bin/python
# A simple test that modifies random bytes of an existing dump file created
# from mongodump and runs mongorestore against a running mongod process on
# the default port
import random
import string
import subprocess
import time
from subprocess import call
# Note: Edit the paths accordingly
MONGOD = ['/Users/ren/mongo//mongod', '--nojournal', '-vvvvv', '--logpath', 'log']
BSON_DUMP = '/Users/ren/mongo/dump/test/user.bson'
TEMP_DUMP = '/Users/ren/mongo/dump2/test/user.bson'
MONGO_RESTORE = ['/Users/ren/mongo/mongorestore', '--drop', TEMP_DUMP]
FUZZ_FACTOR = 20000
FUZZ_THRESH = FUZZ_FACTOR / 1000
num_tests = 10
mongod = subprocess.Popen(MONGOD)
for i in range(num_tests):
with open(BSON_DUMP, 'r') as dump_file:
with open(TEMP_DUMP, 'w') as temp_file:
byte = dump_file.read(1)
while byte != "":
if random.randrange(FUZZ_FACTOR) >= FUZZ_THRESH:
temp_file.write(byte)
else:
temp_file.write(chr(random.randrange(256)))
byte = dump_file.read(1)
ret_code = call(MONGO_RESTORE)
#assert(ret_code == 0)
time.sleep(5) # give time for mongod to die (if it actually crashes)
assert(mongod.poll() is None)
mongod.terminate()
print('Test passed!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment