PageRenderTime 2184ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/buildscripts/smoke.py

https://github.com/homocury/mongo
Python | 521 lines | 445 code | 41 blank | 35 comment | 29 complexity | c2725a336cacc470a78bcd132898c182 MD5 | raw file
  1. #!/usr/bin/env python
  2. # smoke.py: run some mongo tests.
  3. # Bugs, TODOs:
  4. # 0 Some tests hard-code pathnames relative to the mongo repository,
  5. # so the smoke.py process and all its children must be run with the
  6. # mongo repo as current working directory. That's kinda icky.
  7. # 1 The tests that are implemented as standalone executables ("test",
  8. # "perftest"), don't take arguments for the dbpath, but
  9. # unconditionally use "/tmp/unittest".
  10. # 2 mongod output gets intermingled with mongo output, and it's often
  11. # hard to find error messages in the slop. Maybe have smoke.py do
  12. # some fancier wrangling of child process output?
  13. # 3 Some test suites run their own mongods, and so don't need us to
  14. # run any mongods around their execution. (It's harmless to do so,
  15. # but adds noise in the output.)
  16. # 4 Running a separate mongo shell for each js file is slower than
  17. # loading js files into one mongo shell process. Maybe have runTest
  18. # queue up all filenames ending in ".js" and run them in one mongo
  19. # shell at the "end" of testing?
  20. # 5 Right now small-oplog implies master/slave replication. Maybe
  21. # running with replication should be an orthogonal concern. (And
  22. # maybe test replica set replication, too.)
  23. # 6 We use cleanbb.py to clear out the dbpath, but cleanbb.py kills
  24. # off all mongods on a box, which means you can't run two smoke.py
  25. # jobs on the same host at once. So something's gotta change.
  26. from __future__ import with_statement
  27. import glob
  28. from optparse import OptionParser
  29. import os
  30. import parser
  31. import re
  32. import shutil
  33. import socket
  34. from subprocess import (Popen,
  35. PIPE,
  36. call)
  37. import sys
  38. import time
  39. from pymongo import Connection
  40. import utils
  41. # TODO clean this up so we don't need globals...
  42. mongo_repo = os.getcwd() #'./'
  43. test_path = None
  44. mongod_executable = None
  45. mongod_port = None
  46. shell_executable = None
  47. continue_on_failure = None
  48. tests = []
  49. winners = []
  50. losers = {}
  51. # For replication hash checking
  52. replicated_collections = []
  53. lost_in_slave = []
  54. lost_in_master = []
  55. screwy_in_slave = {}
  56. smoke_db_prefix = ''
  57. small_oplog = False
  58. # This class just implements the with statement API, for a sneaky
  59. # purpose below.
  60. class Nothing(object):
  61. def __enter__(self):
  62. return self
  63. def __exit__(self, type, value, traceback):
  64. return not isinstance(value, Exception)
  65. class mongod(object):
  66. def __init__(self, **kwargs):
  67. self.kwargs = kwargs
  68. self.proc = None
  69. def __enter__(self):
  70. self.start()
  71. return self
  72. def __exit__(self, type, value, traceback):
  73. try:
  74. self.stop()
  75. except Exception, e:
  76. print >> sys.stderr, "error shutting down mongod"
  77. print >> sys.stderr, e
  78. return not isinstance(value, Exception)
  79. def ensure_test_dirs(self):
  80. utils.ensureDir(smoke_db_prefix + "/tmp/unittest/")
  81. utils.ensureDir(smoke_db_prefix + "/data/")
  82. utils.ensureDir(smoke_db_prefix + "/data/db/")
  83. def check_mongo_port(self, port=27017):
  84. sock = socket.socket()
  85. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  86. sock.settimeout(1)
  87. sock.connect(("localhost", int(port)))
  88. sock.close()
  89. def did_mongod_start(self, port=mongod_port, timeout=300):
  90. while timeout > 0:
  91. time.sleep(1)
  92. try:
  93. self.check_mongo_port(int(port))
  94. return True
  95. except Exception,e:
  96. print >> sys.stderr, e
  97. timeout = timeout - 1
  98. print >> sys.stderr, "timeout starting mongod"
  99. return False
  100. def start(self):
  101. global mongod_port
  102. global mongod
  103. if self.proc:
  104. print >> sys.stderr, "probable bug: self.proc already set in start()"
  105. return
  106. self.ensure_test_dirs()
  107. dir_name = smoke_db_prefix + "/data/db/sconsTests/"
  108. self.port = int(mongod_port)
  109. self.slave = False
  110. if 'slave' in self.kwargs:
  111. dir_name = smoke_db_prefix + '/data/db/sconsTestsSlave/'
  112. srcport = mongod_port
  113. self.port += 1
  114. self.slave = True
  115. if os.path.exists(dir_name):
  116. if 'slave' in self.kwargs:
  117. argv = ["python", "buildscripts/cleanbb.py", '--nokill', dir_name]
  118. else:
  119. argv = ["python", "buildscripts/cleanbb.py", dir_name]
  120. call(argv)
  121. utils.ensureDir(dir_name)
  122. argv = [mongod_executable, "--port", str(self.port), "--dbpath", dir_name]
  123. if self.kwargs.get('small_oplog'):
  124. argv += ["--master", "--oplogSize", "256"]
  125. if self.slave:
  126. argv += ['--slave', '--source', 'localhost:' + str(srcport)]
  127. if self.kwargs.get('no_journal'):
  128. argv += ['--nojournal']
  129. if self.kwargs.get('no_preallocj'):
  130. argv += ['--nopreallocj']
  131. print "running " + " ".join(argv)
  132. self.proc = Popen(argv)
  133. if not self.did_mongod_start(self.port):
  134. raise Exception("Failed to start mongod")
  135. if self.slave:
  136. local = Connection(port=self.port, slave_okay=True).local
  137. synced = False
  138. while not synced:
  139. synced = True
  140. for source in local.sources.find(fields=["syncedTo"]):
  141. synced = synced and "syncedTo" in source and source["syncedTo"]
  142. def stop(self):
  143. if not self.proc:
  144. print >> sys.stderr, "probable bug: self.proc unset in stop()"
  145. return
  146. try:
  147. # This function not available in Python 2.5
  148. self.proc.terminate()
  149. except AttributeError:
  150. if os.sys.platform == "win32":
  151. import win32process
  152. win32process.TerminateProcess(self.proc._handle, -1)
  153. else:
  154. from os import kill
  155. kill(self.proc.pid, 15)
  156. self.proc.wait()
  157. sys.stderr.flush()
  158. sys.stdout.flush()
  159. def wait_for_repl(self):
  160. Connection(port=self.port).test.smokeWait.insert({}, w=2, wtimeout=5*60*1000)
  161. class Bug(Exception):
  162. def __str__(self):
  163. return 'bug in smoke.py: ' + super(Bug, self).__str__()
  164. class TestFailure(Exception):
  165. pass
  166. class TestExitFailure(TestFailure):
  167. def __init__(self, *args):
  168. self.path = args[0]
  169. self.status=args[1]
  170. def __str__(self):
  171. return "test %s exited with status %d" % (self.path, self.status)
  172. class TestServerFailure(TestFailure):
  173. def __init__(self, *args):
  174. self.path = args[0]
  175. self.status = -1 # this is meaningless as an exit code, but
  176. # that's the point.
  177. def __str__(self):
  178. return 'mongod not running after executing test %s' % self.path
  179. def check_db_hashes(master, slave):
  180. # Need to pause a bit so a slave might catch up...
  181. if not slave.slave:
  182. raise(Bug("slave instance doesn't have slave attribute set"))
  183. print "waiting for slave to catch up"
  184. master.wait_for_repl()
  185. print "caught up!"
  186. # FIXME: maybe make this run dbhash on all databases?
  187. for mongod in [master, slave]:
  188. mongod.dbhash = Connection(port=mongod.port, slave_okay=True).test.command("dbhash")
  189. mongod.dict = mongod.dbhash["collections"]
  190. global lost_in_slave, lost_in_master, screwy_in_slave, replicated_collections
  191. replicated_collections += master.dict.keys()
  192. for db in replicated_collections:
  193. if db not in slave.dict:
  194. lost_in_slave.append(db)
  195. mhash = master.dict[db]
  196. shash = slave.dict[db]
  197. if mhash != shash:
  198. screwy_in_slave[db] = mhash + "/" + shash
  199. for db in slave.dict.keys():
  200. if db not in master.dict:
  201. lost_in_master.append(db)
  202. # Blech.
  203. def skipTest(path):
  204. if small_oplog:
  205. if os.path.basename(path) in ["cursor8.js", "indexh.js", "dropdb.js"]:
  206. return True
  207. return False
  208. def runTest(test):
  209. # test is a tuple of ( filename , usedb<bool> )
  210. # filename should be a js file to run
  211. # usedb is true if the test expects a mongod to be running
  212. (path, usedb) = test
  213. (ignore, ext) = os.path.splitext(path)
  214. if skipTest(path):
  215. print "skipping " + path
  216. return
  217. if ext == ".js":
  218. argv = [shell_executable, "--port", mongod_port]
  219. if not usedb:
  220. argv += ["--nodb"]
  221. if small_oplog:
  222. argv += ["--eval", 'testingReplication = true;']
  223. argv += [path]
  224. elif ext in ["", ".exe"]:
  225. # Blech.
  226. if os.path.basename(path) in ["test", "test.exe", "perftest", "perftest.exe"]:
  227. argv = [path]
  228. # more blech
  229. elif os.path.basename(path) == 'mongos':
  230. argv = [path, "--test"]
  231. else:
  232. argv = [test_path and os.path.abspath(os.path.join(test_path, path)) or path,
  233. "--port", mongod_port]
  234. else:
  235. raise Bug("fell off in extenstion case: %s" % path)
  236. sys.stderr.write( "starting test : %s \n" % os.path.basename(path) )
  237. sys.stderr.flush()
  238. print " *******************************************"
  239. print " Test : " + os.path.basename(path) + " ..."
  240. t1 = time.time()
  241. # FIXME: we don't handle the case where the subprocess
  242. # hangs... that's bad.
  243. if argv[0].endswith( 'mongo' ) and not '--eval' in argv :
  244. argv = argv + [ '--eval', 'TestData = new Object();' +
  245. 'TestData.testPath = "' + path + '";' +
  246. 'TestData.testFile = "' + os.path.basename( path ) + '";' +
  247. 'TestData.testName = "' + re.sub( ".js$", "", os.path.basename( path ) ) + '";' +
  248. 'TestData.noJournal = ' + ( 'true' if no_journal else 'false' ) + ";" +
  249. 'TestData.noJournalPrealloc = ' + ( 'true' if no_preallocj else 'false' ) + ";" ]
  250. if argv[0].endswith( 'test' ) and no_preallocj :
  251. argv = argv + [ '--nopreallocj' ]
  252. print argv
  253. r = call(argv, cwd=test_path)
  254. t2 = time.time()
  255. print " " + str((t2 - t1) * 1000) + "ms"
  256. if r != 0:
  257. raise TestExitFailure(path, r)
  258. try:
  259. c = Connection( "127.0.0.1" , int(mongod_port) )
  260. except Exception,e:
  261. raise TestServerFailure(path)
  262. print ""
  263. def run_tests(tests):
  264. # FIXME: some suites of tests start their own mongod, so don't
  265. # need this. (So long as there are no conflicts with port,
  266. # dbpath, etc., and so long as we shut ours down properly,
  267. # starting this mongod shouldn't break anything, though.)
  268. # The reason we use with is so that we get __exit__ semantics
  269. with mongod(small_oplog=small_oplog,no_journal=no_journal,no_preallocj=no_preallocj) as master:
  270. with mongod(slave=True) if small_oplog else Nothing() as slave:
  271. if small_oplog:
  272. master.wait_for_repl()
  273. for test in tests:
  274. try:
  275. runTest(test)
  276. winners.append(test)
  277. except TestFailure, f:
  278. try:
  279. print f
  280. # Record the failing test and re-raise.
  281. losers[f.path] = f.status
  282. raise f
  283. except TestServerFailure, f:
  284. return 2
  285. except TestFailure, f:
  286. if not continue_on_failure:
  287. return 1
  288. if isinstance(slave, mongod):
  289. check_db_hashes(master, slave)
  290. return 0
  291. def report():
  292. print "%d test%s succeeded" % (len(winners), '' if len(winners) == 1 else 's')
  293. num_missed = len(tests) - (len(winners) + len(losers.keys()))
  294. if num_missed:
  295. print "%d tests didn't get run" % num_missed
  296. if losers:
  297. print "The following tests failed (with exit code):"
  298. for loser in losers:
  299. print "%s\t%d" % (loser, losers[loser])
  300. def missing(lst, src, dst):
  301. if lst:
  302. print """The following collections were present in the %s but not the %s
  303. at the end of testing:""" % (src, dst)
  304. for db in lst:
  305. print db
  306. missing(lost_in_slave, "master", "slave")
  307. missing(lost_in_master, "slave", "master")
  308. if screwy_in_slave:
  309. print """The following collections has different hashes in master and slave
  310. at the end of testing:"""
  311. for db in screwy_in_slave.keys():
  312. print "%s\t %s" % (db, screwy_in_slave[db])
  313. if small_oplog and not (lost_in_master or lost_in_slave or screwy_in_slave):
  314. print "replication ok for %d collections" % (len(replicated_collections))
  315. if losers or lost_in_slave or lost_in_master or screwy_in_slave:
  316. raise Exception("Test failures")
  317. def expand_suites(suites):
  318. globstr = None
  319. tests = []
  320. for suite in suites:
  321. if suite == 'all':
  322. return expand_suites(['test', 'perf', 'client', 'js', 'jsPerf', 'jsSlowNightly', 'jsSlowWeekly', 'parallel', 'clone', 'parallel', 'repl', 'auth', 'sharding', 'tool'])
  323. if suite == 'test':
  324. if os.sys.platform == "win32":
  325. program = 'test.exe'
  326. else:
  327. program = 'test'
  328. (globstr, usedb) = (program, False)
  329. elif suite == 'perf':
  330. if os.sys.platform == "win32":
  331. program = 'perftest.exe'
  332. else:
  333. program = 'perftest'
  334. (globstr, usedb) = (program, False)
  335. elif suite == 'client':
  336. paths = ["firstExample", "secondExample", "whereExample", "authTest", "clientTest", "httpClientTest"]
  337. if os.sys.platform == "win32":
  338. paths = [path + '.exe' for path in paths]
  339. # hack
  340. tests += [(test_path and path or os.path.join(mongo_repo, path), False) for path in paths]
  341. elif suite == 'mongosTest':
  342. if os.sys.platform == "win32":
  343. program = 'mongos.exe'
  344. else:
  345. program = 'mongos'
  346. tests += [(os.path.join(mongo_repo, program), False)]
  347. elif os.path.exists( suite ):
  348. tests += [ ( os.path.join( mongo_repo , suite ) , True ) ]
  349. else:
  350. try:
  351. globstr, usedb = {"js": ("[!_]*.js", True),
  352. "quota": ("quota/*.js", True),
  353. "jsPerf": ("perf/*.js", True),
  354. "disk": ("disk/*.js", True),
  355. "jsSlowNightly": ("slowNightly/*.js", True),
  356. "jsSlowWeekly": ("slowWeekly/*.js", True),
  357. "parallel": ("parallel/*.js", True),
  358. "clone": ("clone/*.js", False),
  359. "repl": ("repl/*.js", False),
  360. "replSets": ("replsets/*.js", False),
  361. "dur": ("dur/*.js", False),
  362. "auth": ("auth/*.js", False),
  363. "sharding": ("sharding/*.js", False),
  364. "tool": ("tool/*.js", False)}[suite]
  365. except KeyError:
  366. raise Exception('unknown test suite %s' % suite)
  367. if globstr:
  368. globstr = os.path.join(mongo_repo, (os.path.join(('jstests/' if globstr.endswith('.js') else ''), globstr)))
  369. paths = glob.glob(globstr)
  370. paths.sort()
  371. tests += [(path, usedb) for path in paths]
  372. return tests
  373. def add_exe(e):
  374. if os.sys.platform.startswith( "win" ) and not e.endswith( ".exe" ):
  375. e += ".exe"
  376. return e
  377. def main():
  378. global mongod_executable, mongod_port, shell_executable, continue_on_failure, small_oplog, no_journal, no_preallocj, smoke_db_prefix, test_path
  379. parser = OptionParser(usage="usage: smoke.py [OPTIONS] ARGS*")
  380. parser.add_option('--mode', dest='mode', default='suite',
  381. help='If "files", ARGS are filenames; if "suite", ARGS are sets of tests (%default)')
  382. # Some of our tests hard-code pathnames e.g., to execute, so until
  383. # that changes we don't have the freedom to run from anyplace.
  384. # parser.add_option('--mongo-repo', dest='mongo_repo', default=None,
  385. parser.add_option('--test-path', dest='test_path', default=None,
  386. help="Path to the test executables to run, "
  387. "currently only used for 'client' (%default)")
  388. parser.add_option('--mongod', dest='mongod_executable', default=os.path.join(mongo_repo, 'mongod'),
  389. help='Path to mongod to run (%default)')
  390. parser.add_option('--port', dest='mongod_port', default="32000",
  391. help='Port the mongod will bind to (%default)')
  392. parser.add_option('--mongo', dest='shell_executable', default=os.path.join(mongo_repo, 'mongo'),
  393. help='Path to mongo, for .js test files (%default)')
  394. parser.add_option('--continue-on-failure', dest='continue_on_failure',
  395. action="store_true", default=False,
  396. help='If supplied, continue testing even after a test fails')
  397. parser.add_option('--from-file', dest='File',
  398. help="Run tests/suites named in FILE, one test per line, '-' means stdin")
  399. parser.add_option('--smoke-db-prefix', dest='smoke_db_prefix', default=smoke_db_prefix,
  400. help="Prefix to use for the mongods' dbpaths ('%default')")
  401. parser.add_option('--small-oplog', dest='small_oplog', default=False,
  402. action="store_true",
  403. help='Run tests with master/slave replication & use a small oplog')
  404. parser.add_option('--nojournal', dest='no_journal', default=False,
  405. action="store_true",
  406. help='Do not turn on journaling in tests')
  407. parser.add_option('--nopreallocj', dest='no_preallocj', default=False,
  408. action="store_true",
  409. help='Do not preallocate journal files in tests')
  410. global tests
  411. (options, tests) = parser.parse_args()
  412. print tests
  413. test_path = options.test_path
  414. mongod_executable = add_exe(options.mongod_executable)
  415. if not os.path.exists(mongod_executable):
  416. raise Exception("no mongod found in this directory.")
  417. mongod_port = options.mongod_port
  418. shell_executable = add_exe( options.shell_executable )
  419. if not os.path.exists(shell_executable):
  420. raise Exception("no mongo shell found in this directory.")
  421. continue_on_failure = options.continue_on_failure
  422. smoke_db_prefix = options.smoke_db_prefix
  423. small_oplog = options.small_oplog
  424. no_journal = options.no_journal
  425. no_preallocj = options.no_preallocj
  426. if options.File:
  427. if options.File == '-':
  428. tests = sys.stdin.readlines()
  429. else:
  430. with open(options.File) as f:
  431. tests = f.readlines()
  432. tests = [t.rstrip('\n') for t in tests]
  433. # If we're in suite mode, tests is a list of names of sets of tests.
  434. if options.mode == 'suite':
  435. tests = expand_suites(tests)
  436. elif options.mode == 'files':
  437. tests = [(os.path.abspath(test), True) for test in tests]
  438. if not tests:
  439. raise Exception( "no tests specified" )
  440. try:
  441. run_tests(tests)
  442. finally:
  443. report()
  444. if __name__ == "__main__":
  445. main()