test.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2022 The MMGen Project <mmgen@tuta.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. test/test.py: Test suite for the MMGen wallet system
  20. """
  21. def check_segwit_opts():
  22. for k,m in (('segwit','S'),('segwit_random','S'),('bech32','B')):
  23. if getattr(opt,k) and m not in proto.mmtypes:
  24. die(1,f'--{k.replace("_","-")} option incompatible with {proto.cls_name}')
  25. def create_shm_dir(data_dir,trash_dir):
  26. # Laggy flash media can cause pexpect to fail, so create a temporary directory
  27. # under '/dev/shm' and put datadir and tmpdirs here.
  28. import shutil
  29. from subprocess import run
  30. if g.platform == 'win':
  31. for tdir in (data_dir,trash_dir):
  32. try: os.listdir(tdir)
  33. except: pass
  34. else:
  35. try: shutil.rmtree(tdir)
  36. except: # we couldn't remove data dir - perhaps regtest daemon is running
  37. try:
  38. run(['python3',os.path.join('cmds','mmgen-regtest'),'stop'],check=True)
  39. except:
  40. die(4,f'Unable to remove {tdir!r}!')
  41. else:
  42. time.sleep(2)
  43. shutil.rmtree(tdir)
  44. os.mkdir(tdir,0o755)
  45. shm_dir = 'test'
  46. else:
  47. tdir,pfx = '/dev/shm','mmgen-test-'
  48. try:
  49. run(f'rm -rf {tdir}/{pfx}*',shell=True,check=True)
  50. except Exception as e:
  51. die(2,f'Unable to delete directory tree {tdir}/{pfx}* ({e.args[0]})')
  52. try:
  53. import tempfile
  54. shm_dir = str(tempfile.mkdtemp('',pfx,tdir))
  55. except Exception as e:
  56. die(2,f'Unable to create temporary directory in {tdir} ({e.args[0]})')
  57. dest = os.path.join(shm_dir,os.path.basename(trash_dir))
  58. os.mkdir(dest,0o755)
  59. run(f'rm -rf {trash_dir}',shell=True,check=True)
  60. os.symlink(dest,trash_dir)
  61. dest = os.path.join(shm_dir,os.path.basename(data_dir))
  62. shutil.move(data_dir,dest) # data_dir was created by opts.init()
  63. os.symlink(dest,data_dir)
  64. return shm_dir
  65. import sys,os,time
  66. from include.tests_header import repo_root
  67. from test.overlay import get_overlay_dir,overlay_setup
  68. overlay_dir = get_overlay_dir(repo_root)
  69. sys.path.insert(0,overlay_dir)
  70. if not (len(sys.argv) == 2 and sys.argv[1] == 'clean'):
  71. 'hack: overlay must be set up before mmgen mods are imported'
  72. overlay_setup(repo_root)
  73. from mmgen.common import *
  74. try:
  75. os.unlink(os.path.join(repo_root,'test.py.err'))
  76. except:
  77. pass
  78. g.quiet = False # if 'quiet' was set in config file, disable here
  79. os.environ['MMGEN_QUIET'] = '0' # for this script and spawned scripts
  80. opts_data = {
  81. 'sets': [('list_current_cmd_groups',True,'list_cmd_groups',True)],
  82. 'text': {
  83. 'desc': 'Test suite for the MMGen suite',
  84. 'usage':'[options] [command(s) or metacommand(s)]',
  85. 'options': """
  86. -h, --help Print this help message
  87. --, --longhelp Print help message for long options (common options)
  88. -a, --no-altcoin Skip altcoin tests (WIP)
  89. -A, --no-daemon-autostart Don't start and stop daemons automatically
  90. -B, --bech32 Generate and use Bech32 addresses
  91. -b, --buf-keypress Use buffered keypresses as with real human input
  92. (often required on slow systems, or under emulation)
  93. -c, --print-cmdline Print the command line of each spawned command
  94. -C, --coverage Produce code coverage info using trace module
  95. -x, --debug-pexpect Produce debugging output for pexpect calls
  96. -D, --no-daemon-stop Don't stop auto-started daemons after running tests
  97. -E, --direct-exec Bypass pexpect and execute a command directly (for
  98. debugging only)
  99. -e, --exact-output Show the exact output of the MMGen script(s) being run
  100. -G, --exclude-groups=G Exclude the specified command groups (comma-separated)
  101. -l, --list-cmds List the test script’s available commands
  102. -L, --list-cmd-groups Output a list of command groups with descriptions
  103. -g, --list-current-cmd-groups List command groups for current configuration
  104. -n, --names Display command names instead of descriptions
  105. -N, --no-timings Suppress display of timing information
  106. -o, --log Log commands to file {lf!r}
  107. -O, --pexpect-spawn Use pexpect.spawn instead of popen_spawn (much slower,
  108. kut does real terminal emulation)
  109. -p, --pause Pause between tests, resuming on keypress
  110. -P, --profile Record the execution time of each script
  111. -q, --quiet Produce minimal output. Suppress dependency info
  112. -r, --resume=c Resume at command 'c' after interrupted run
  113. -R, --resume-after=c Same, but resume at command following 'c'
  114. -t, --step After resuming, execute one command and stop
  115. -s, --system Test scripts and modules installed on system rather
  116. than those in the repo root
  117. -S, --skip-deps Skip dependency checking for command
  118. -u, --usr-random Get random data interactively from user
  119. -T, --pexpect-timeout=T Set the timeout for pexpect
  120. -v, --verbose Produce more verbose output
  121. -W, --no-dw-delete Don't remove default wallet from data dir after dw tests
  122. are done
  123. -X, --exit-after=C Exit after command 'C'
  124. -y, --segwit Generate and use Segwit addresses
  125. -Y, --segwit-random Generate and use a random mix of Segwit and Legacy addrs
  126. """,
  127. 'notes': """
  128. If no command is given, the whole test suite is run for the currently
  129. specified coin (default BTC).
  130. For traceback output and error file support, set the EXEC_WRAPPER_TRACEBACK
  131. environment var
  132. """
  133. },
  134. 'code': {
  135. 'options': lambda proto,help_notes,s: s.format(
  136. lf = help_notes('test_py_log_file')
  137. )
  138. }
  139. }
  140. # we need some opt values before running opts.init, so parse without initializing:
  141. po = opts.init(opts_data,parse_only=True)
  142. from test.include.common import *
  143. from test.test_py_d.common import *
  144. data_dir = get_data_dir() # include/common.py
  145. # step 1: delete data_dir symlink in ./test;
  146. resuming = any(k in po.user_opts for k in ('resume','resume_after'))
  147. skipping_deps = resuming or 'skip_deps' in po.user_opts
  148. if not skipping_deps:
  149. try: os.unlink(data_dir)
  150. except: pass
  151. opts.UserOpts._reset_ok += ('no_daemon_autostart','names','no_timings','exit_after')
  152. # step 2: opts.init will create new data_dir in ./test (if not skipping_deps)
  153. usr_args = opts.init(opts_data)
  154. if opt.daemon_id and opt.daemon_id in g.blacklist_daemons.split():
  155. die(0,f'test.py: daemon {opt.daemon_id!r} blacklisted, exiting')
  156. network_id = g.coin.lower() + ('_tn' if opt.testnet else '')
  157. from mmgen.protocol import init_proto_from_opts
  158. proto = init_proto_from_opts()
  159. # step 3: move data_dir to /dev/shm and symlink it back to ./test:
  160. trash_dir = os.path.join('test','trash')
  161. if not skipping_deps:
  162. shm_dir = create_shm_dir(data_dir,trash_dir)
  163. check_segwit_opts()
  164. testing_segwit = opt.segwit or opt.segwit_random or opt.bech32
  165. if g.test_suite_deterministic:
  166. opt.no_timings = True
  167. init_color(num_colors=0)
  168. os.environ['MMGEN_DISABLE_COLOR'] = '1'
  169. if opt.profile:
  170. opt.names = True
  171. if opt.exact_output:
  172. def msg(s): pass
  173. qmsg = qmsg_r = vmsg = vmsg_r = msg_r = msg
  174. if skipping_deps:
  175. opt.no_daemon_autostart = True
  176. from test.test_py_d.cfg import cfgs,fixup_cfgs
  177. for k in cfgs:
  178. cfgs[k]['tmpdir'] = os.path.join('test','tmp',str(k))
  179. fixup_cfgs()
  180. utils = {
  181. # 'check_deps': 'check dependencies for specified command (WIP)', # TODO
  182. 'clean': 'clean specified tmp dir(s) (specify by integer, no arg = all dirs)',
  183. }
  184. def list_cmds():
  185. def gen_output():
  186. gm = CmdGroupMgr()
  187. cw,d = 0,[]
  188. yield green('AVAILABLE COMMANDS:')
  189. for gname in gm.cmd_groups:
  190. ts = gm.gm_init_group(None,gname,None,None)
  191. desc = ts.__doc__.strip() if ts.__doc__ else type(ts).__name__
  192. d.append( (gname,desc,gm.cmd_list,gm.dpy_data) )
  193. cw = max(max(len(k) for k in gm.dpy_data),cw)
  194. for gname,gdesc,clist,dpdata in d:
  195. yield '\n'+green(f'{gname!r} - {gdesc}:')
  196. for cmd in clist:
  197. data = dpdata[cmd]
  198. yield ' {:{w}} - {}'.format(
  199. cmd,
  200. (data if type(data) == str else data[1]),
  201. w = cw )
  202. w = max(map(len,utils))
  203. yield '\n'+green('AVAILABLE UTILITIES:')
  204. for cmd in sorted(utils):
  205. yield ' {:{w}} - {}'.format( cmd, utils[cmd], w=w )
  206. do_pager('\n'.join(gen_output()))
  207. sys.exit(0)
  208. def do_between():
  209. if opt.pause:
  210. confirm_continue()
  211. elif (opt.verbose or opt.exact_output) and not skipping_deps:
  212. sys.stderr.write('\n')
  213. def list_tmpdirs():
  214. return {k:cfgs[k]['tmpdir'] for k in cfgs}
  215. def clean(usr_dirs=None,clean_overlay=True):
  216. if skipping_deps:
  217. return
  218. all_dirs = list_tmpdirs()
  219. dirnums = map(int,(usr_dirs if usr_dirs is not None else all_dirs))
  220. dirlist = list(map(str,sorted(dirnums)))
  221. for d in dirlist:
  222. if d in all_dirs:
  223. cleandir(all_dirs[d])
  224. else:
  225. die(1,f'{d}: invalid directory number')
  226. if dirlist:
  227. iqmsg(green('Cleaned tmp director{} {}'.format(
  228. suf(dirlist,'ies'),
  229. ' '.join(dirlist))
  230. ))
  231. cleandir(data_dir)
  232. cleandir(trash_dir)
  233. iqmsg(green(f'Cleaned directories {data_dir!r} {trash_dir!r}'))
  234. if clean_overlay:
  235. cleandir(overlay_dir)
  236. iqmsg(green(f'Cleaned directory {os.path.relpath(overlay_dir)!r}'))
  237. def create_tmp_dirs(shm_dir):
  238. if g.platform == 'win':
  239. for cfg in sorted(cfgs):
  240. mk_tmpdir(cfgs[cfg]['tmpdir'])
  241. else:
  242. os.makedirs( 'test/tmp', mode=0o755, exist_ok=True )
  243. for cfg in sorted(cfgs):
  244. src = os.path.join(shm_dir,cfgs[cfg]['tmpdir'].split('/')[-1])
  245. mk_tmpdir(src)
  246. try:
  247. os.unlink(cfgs[cfg]['tmpdir'])
  248. except OSError as e:
  249. if e.errno != 2:
  250. raise
  251. finally:
  252. os.symlink(src,cfgs[cfg]['tmpdir'])
  253. def set_environ_for_spawned_scripts():
  254. from mmgen.term import get_terminal_size
  255. os.environ['MMGEN_COLUMNS'] = str(get_terminal_size().width)
  256. if os.getenv('MMGEN_DEBUG_ALL'):
  257. for name in g.env_opts:
  258. if name[:11] == 'MMGEN_DEBUG':
  259. os.environ[name] = '1'
  260. if not opt.pexpect_spawn:
  261. os.environ['MMGEN_TEST_SUITE_POPEN_SPAWN'] = '1'
  262. if not opt.system:
  263. os.environ['PYTHONPATH'] = repo_root
  264. if not opt.buf_keypress:
  265. os.environ['MMGEN_DISABLE_HOLD_PROTECT'] = '1'
  266. os.environ['MMGEN_NO_LICENSE'] = '1'
  267. os.environ['MMGEN_MIN_URANDCHARS'] = '3'
  268. os.environ['MMGEN_BOGUS_SEND'] = '1'
  269. os.environ['MMGEN_TEST_SUITE_PEXPECT'] = '1'
  270. def set_restore_term_at_exit():
  271. import termios,atexit
  272. fd = sys.stdin.fileno()
  273. old = termios.tcgetattr(fd)
  274. def at_exit():
  275. termios.tcsetattr(fd, termios.TCSADRAIN, old)
  276. atexit.register(at_exit)
  277. class CmdGroupMgr(object):
  278. from test.test_py_d.cfg import cmd_groups_dfl,cmd_groups_extra
  279. cmd_groups = cmd_groups_dfl.copy()
  280. cmd_groups.update(cmd_groups_extra)
  281. def load_mod(self,gname,modname=None):
  282. clsname,kwargs = self.cmd_groups[gname]
  283. if modname == None and 'modname' in kwargs:
  284. modname = kwargs['modname']
  285. import importlib
  286. modpath = f'test.test_py_d.ts_{modname or gname}'
  287. return getattr(importlib.import_module(modpath),clsname)
  288. def create_group(self,gname,full_data=False,modname=None,is3seed=False,add_dpy=False):
  289. """
  290. Initializes the list 'cmd_list' and dict 'dpy_data' from module's cmd_group data.
  291. Alternatively, if called with 'add_dpy=True', updates 'dpy_data' from module data
  292. without touching 'cmd_list'
  293. """
  294. cls = self.load_mod(gname,modname)
  295. cdata = []
  296. def get_shared_deps(cmdname,tmpdir_idx):
  297. """
  298. shared_deps are "implied" dependencies for all cmds in cmd_group that don't appear in
  299. the cmd_group data or cmds' argument lists. Supported only for 3seed tests at present.
  300. """
  301. if not hasattr(cls,'shared_deps'):
  302. return []
  303. return [k for k,v in cfgs[str(tmpdir_idx)]['dep_generators'].items()
  304. if k in cls.shared_deps and v != cmdname]
  305. for a,b in cls.cmd_group:
  306. if is3seed:
  307. for n,(i,j) in enumerate(zip(cls.tmpdir_nums,(128,192,256))):
  308. k = f'{a}_{n+1}'
  309. if hasattr(cls,'skip_cmds') and k in cls.skip_cmds:
  310. continue
  311. sdeps = get_shared_deps(k,i)
  312. if type(b) == str:
  313. cdata.append( (k, (i,f'{b} ({j}-bit)',[[[]+sdeps,i]])) )
  314. else:
  315. cdata.append( (k, (i,f'{b[1]} ({j}-bit)',[[b[0]+sdeps,i]])) )
  316. else:
  317. cdata.append( (a, b if full_data else (cls.tmpdir_nums[0],b,[[[],cls.tmpdir_nums[0]]])) )
  318. if add_dpy:
  319. self.dpy_data.update(dict(cdata))
  320. else:
  321. self.cmd_list = tuple(e[0] for e in cdata)
  322. self.dpy_data = dict(cdata)
  323. return cls
  324. def gm_init_group(self,trunner,gname,spawn_prog):
  325. kwargs = self.cmd_groups[gname][1]
  326. cls = self.create_group(gname,**kwargs)
  327. cls.group_name = gname
  328. return cls(trunner,cfgs,spawn_prog)
  329. def list_cmd_groups(self):
  330. ginfo = []
  331. for gname in self.cmd_groups:
  332. clsname,kwargs = self.cmd_groups[gname]
  333. cls = self.load_mod(gname,kwargs['modname'] if 'modname' in kwargs else None)
  334. ginfo.append((gname,cls))
  335. if opt.list_current_cmd_groups:
  336. exclude = (opt.exclude_groups or '').split(',')
  337. ginfo = [g for g in ginfo
  338. if network_id in g[1].networks
  339. and not g[0] in exclude
  340. and g[0] in tuple(self.cmd_groups_dfl) + tuple(usr_args) ]
  341. for name,cls in ginfo:
  342. msg('{:17} - {}'.format(
  343. name,
  344. cls.__doc__.strip() if cls.__doc__ else cls.__name__
  345. ))
  346. Msg( '\n' + ' '.join(e[0] for e in ginfo) )
  347. sys.exit(0)
  348. def find_cmd_in_groups(self,cmd,group=None):
  349. """
  350. Search for a test command in specified group or all configured command groups
  351. and return it as a string. Loads modules but alters no global variables.
  352. """
  353. if group:
  354. if not group in [e[0] for e in self.cmd_groups]:
  355. die(1,f'{group!r}: unrecognized group')
  356. groups = [self.cmd_groups[group]]
  357. else:
  358. groups = self.cmd_groups
  359. for gname in groups:
  360. clsname,kwargs = self.cmd_groups[gname]
  361. cls = self.load_mod(gname,kwargs['modname'] if 'modname' in kwargs else None)
  362. if cmd in cls.cmd_group: # first search the class
  363. return gname
  364. if cmd in dir(cls(None,None,None)): # then a throwaway instance
  365. return gname # cmd might exist in more than one group - we'll go with the first
  366. return None
  367. class TestSuiteRunner(object):
  368. 'test suite runner'
  369. def __del__(self):
  370. if opt.log:
  371. self.log_fd.close()
  372. def __init__(self,data_dir,trash_dir):
  373. self.data_dir = data_dir
  374. self.trash_dir = trash_dir
  375. self.cmd_total = 0
  376. self.rebuild_list = {}
  377. self.gm = CmdGroupMgr()
  378. self.repo_root = repo_root
  379. self.skipped_warnings = []
  380. self.resume_cmd = None
  381. if opt.log:
  382. self.log_fd = open(log_file,'a')
  383. self.log_fd.write(f'\nLog started: {make_timestr()} UTC\n')
  384. omsg(f'INFO → Logging to file {log_file!r}')
  385. else:
  386. self.log_fd = None
  387. if opt.coverage:
  388. coverdir,accfile = init_coverage()
  389. omsg(f'INFO → Writing coverage files to {coverdir!r}')
  390. self.pre_args = ['python3','-m','trace','--count','--coverdir='+coverdir,'--file='+accfile]
  391. else:
  392. self.pre_args = ['python3'] if g.platform == 'win' else []
  393. if opt.pexpect_spawn:
  394. omsg(f'INFO → Using pexpect.spawn() for real terminal emulation')
  395. def spawn_wrapper(self,cmd,
  396. args = [],
  397. extra_desc = '',
  398. no_output = False,
  399. msg_only = False,
  400. no_msg = False,
  401. cmd_dir = 'cmds',
  402. no_exec_wrapper = False ):
  403. desc = self.ts.test_name if opt.names else self.gm.dpy_data[self.ts.test_name][1]
  404. if extra_desc:
  405. desc += ' ' + extra_desc
  406. cmd_path = (
  407. cmd if opt.system # opt.system is broken for main test group with overlay tree
  408. else os.path.relpath(os.path.join(repo_root,cmd_dir,cmd)) )
  409. args = (
  410. self.pre_args +
  411. ([] if no_exec_wrapper else ['scripts/exec_wrapper.py']) +
  412. [cmd_path] +
  413. self.passthru_opts +
  414. self.ts.extra_spawn_args +
  415. args )
  416. for i in args:
  417. if not isinstance(i,str):
  418. die(2,'Error: missing input files in cmd line?:\nName: {}\nCmdline: {!r}'.format(
  419. self.ts.test_name,
  420. args ))
  421. qargs = ['{q}{}{q}'.format( a, q = "'" if ' ' in a else '' ) for a in args]
  422. cmd_disp = ' '.join(qargs).replace('\\','/') # for mingw
  423. if not no_msg:
  424. t_pfx = '' if opt.no_timings else f'[{time.time() - self.start_time:08.2f}] '
  425. if opt.verbose or opt.print_cmdline or opt.exact_output:
  426. omsg(green(f'{t_pfx}Testing: {desc}'))
  427. if not msg_only:
  428. clr1,clr2 = (nocolor,nocolor) if opt.print_cmdline else (green,cyan)
  429. omsg(
  430. clr1('Executing: ') +
  431. clr2(repr(cmd_disp) if g.platform == 'win' else cmd_disp)
  432. )
  433. else:
  434. omsg_r(f'{t_pfx}Testing {desc}: ')
  435. if msg_only:
  436. return
  437. if opt.log:
  438. self.log_fd.write('[{}][{}:{}] {}\n'.format(
  439. proto.coin.lower(),
  440. self.ts.group_name,
  441. self.ts.test_name,
  442. cmd_disp))
  443. os.environ['MMGEN_FORCE_COLOR'] = '1' if self.ts.color else ''
  444. env = { 'EXEC_WRAPPER_SPAWN':'1' }
  445. env.update(os.environ)
  446. if 'exec_wrapper_init' in globals():
  447. # test.py itself is running under exec_wrapper, so disable traceback file writing for spawned script
  448. env.update({ 'EXEC_WRAPPER_TRACEBACK':'' }) # Python 3.9: OR the dicts
  449. from test.include.pexpect import MMGenPexpect
  450. return MMGenPexpect( args, no_output=no_output, env=env )
  451. def end_msg(self):
  452. t = int(time.time() - self.start_time)
  453. sys.stderr.write(green(
  454. f'{self.cmd_total} test{suf(self.cmd_total)} performed' +
  455. ('\n' if opt.no_timings else f'. Elapsed time: {t//60:02d}:{t%60:02d}\n')
  456. ))
  457. def init_group(self,gname,cmd=None,quiet=False,do_clean=True):
  458. ts_cls = CmdGroupMgr().load_mod(gname)
  459. for k in ('segwit','segwit_random','bech32'):
  460. if getattr(opt,k):
  461. segwit_opt = k
  462. break
  463. else:
  464. segwit_opt = None
  465. def gen_msg():
  466. yield ('{g}:{c}' if cmd else 'test group {g!r}').format(g=gname,c=cmd)
  467. if len(ts_cls.networks) != 1:
  468. yield f' for {proto.coin} {proto.network}'
  469. if segwit_opt:
  470. yield ' (--{})'.format( segwit_opt.replace('_','-') )
  471. m = ''.join(gen_msg())
  472. if segwit_opt and not ts_cls.segwit_opts_ok:
  473. iqmsg('INFO → skipping ' + m)
  474. return False
  475. # 'networks = ()' means all networks allowed
  476. nws = [(e.split('_')[0],'testnet') if '_' in e else (e,'mainnet') for e in ts_cls.networks]
  477. if nws:
  478. coin = proto.coin.lower()
  479. nw = ('mainnet','testnet')[proto.testnet]
  480. for a,b in nws:
  481. if a == coin and b == nw:
  482. break
  483. else:
  484. iqmsg('INFO → skipping ' + m)
  485. return False
  486. if do_clean:
  487. clean(ts_cls.tmpdir_nums,clean_overlay=False)
  488. if not quiet:
  489. bmsg('Executing ' + m)
  490. if not self.daemons_started and network_id not in ('eth','etc','xmr'):
  491. start_test_daemons(network_id,remove_datadir=True)
  492. self.daemons_started = True
  493. os.environ['MMGEN_BOGUS_UNSPENT_DATA'] = '' # zero this here, so test groups don't have to
  494. self.ts = self.gm.gm_init_group(self,gname,self.spawn_wrapper)
  495. self.ts_clsname = type(self.ts).__name__
  496. self.passthru_opts = ['--{}{}'.format(
  497. k.replace('_','-'),
  498. '=' + getattr(opt,k) if getattr(opt,k) != True else ''
  499. ) for k in self.ts.base_passthru_opts + self.ts.passthru_opts if getattr(opt,k)]
  500. if resuming:
  501. rc = opt.resume or opt.resume_after
  502. offset = 1 if opt.resume_after else 0
  503. self.resume_cmd = self.gm.cmd_list[self.gm.cmd_list.index(rc)+offset]
  504. omsg(f'INFO → Resuming at command {self.resume_cmd!r}')
  505. if opt.step:
  506. opt.exit_after = self.resume_cmd
  507. if opt.exit_after and opt.exit_after not in self.gm.cmd_list:
  508. die(1,f'{opt.exit_after!r}: command not recognized')
  509. return True
  510. def run_tests(self,usr_args):
  511. self.start_time = time.time()
  512. self.daemons_started = False
  513. gname_save = None
  514. if usr_args:
  515. for arg in usr_args:
  516. if arg in self.gm.cmd_groups:
  517. if not self.init_group(arg):
  518. continue
  519. for cmd in self.gm.cmd_list:
  520. self.check_needs_rerun(cmd,build=True)
  521. do_between()
  522. else:
  523. if ':' in arg:
  524. gname,arg = arg.split(':')
  525. else:
  526. gname = self.gm.find_cmd_in_groups(arg)
  527. if gname:
  528. same_grp = gname == gname_save # same group as previous cmd: don't clean, suppress blue msg
  529. if not self.init_group(gname,arg,quiet=same_grp,do_clean=not same_grp):
  530. continue
  531. try:
  532. self.check_needs_rerun(arg,build=True)
  533. except Exception as e: # allow calling of functions not in cmd_group
  534. if isinstance(e,KeyError) and e.args[0] == arg:
  535. ret = getattr(self.ts,arg)()
  536. if type(ret).__name__ == 'coroutine':
  537. run_session(ret)
  538. else:
  539. raise
  540. do_between()
  541. gname_save = gname
  542. else:
  543. die(1,f'{arg!r}: command not recognized')
  544. else:
  545. if opt.exclude_groups:
  546. exclude = opt.exclude_groups.split(',')
  547. for e in exclude:
  548. if e not in self.gm.cmd_groups_dfl:
  549. die(1,f'{e!r}: group not recognized')
  550. for gname in self.gm.cmd_groups_dfl:
  551. if opt.exclude_groups and gname in exclude:
  552. continue
  553. if not self.init_group(gname):
  554. continue
  555. for cmd in self.gm.cmd_list:
  556. self.check_needs_rerun(cmd,build=True)
  557. do_between()
  558. self.end_msg()
  559. def check_needs_rerun(self,cmd,
  560. build = False,
  561. root = True,
  562. force_delete = False,
  563. dpy = False ):
  564. self.ts.test_name = cmd
  565. if self.ts_clsname == 'TestSuiteMain' and testing_segwit and cmd not in self.ts.segwit_do:
  566. return False
  567. rerun = root # force_delete is not passed to recursive call
  568. fns = []
  569. if force_delete or not root:
  570. # does cmd produce a needed dependency(ies)?
  571. ret = self.get_num_exts_for_cmd(cmd,dpy)
  572. if ret:
  573. for ext in ret[1]:
  574. fn = get_file_with_ext(cfgs[ret[0]]['tmpdir'],ext,delete=build)
  575. if fn:
  576. if force_delete: os.unlink(fn)
  577. else: fns.append(fn)
  578. else: rerun = True
  579. fdeps = self.generate_file_deps(cmd)
  580. cdeps = self.generate_cmd_deps(fdeps)
  581. for fn in fns:
  582. my_age = os.stat(fn).st_mtime
  583. for num,ext in fdeps:
  584. f = get_file_with_ext(cfgs[num]['tmpdir'],ext,delete=build)
  585. if f and os.stat(f).st_mtime > my_age:
  586. rerun = True
  587. for cdep in cdeps:
  588. if self.check_needs_rerun(cdep,build=build,root=False,dpy=cmd):
  589. rerun = True
  590. if build:
  591. if rerun:
  592. for fn in fns:
  593. if not root:
  594. os.unlink(fn)
  595. if not (dpy and skipping_deps):
  596. self.run_test(cmd)
  597. if not root:
  598. do_between()
  599. else:
  600. # If prog produces multiple files:
  601. if cmd not in self.rebuild_list or rerun == True:
  602. self.rebuild_list[cmd] = (rerun,fns[0] if fns else '') # FIX
  603. return rerun
  604. def run_test(self,cmd):
  605. d = [(str(num),ext) for exts,num in self.gm.dpy_data[cmd][2] for ext in exts]
  606. # delete files depended on by this cmd
  607. arg_list = [get_file_with_ext(cfgs[num]['tmpdir'],ext) for num,ext in d]
  608. # remove shared_deps from arg list
  609. if hasattr(self.ts,'shared_deps'):
  610. arg_list = arg_list[:-len(self.ts.shared_deps)]
  611. if self.resume_cmd:
  612. if cmd != self.resume_cmd:
  613. return
  614. bmsg(f'Resuming at {self.resume_cmd!r}')
  615. self.resume_cmd = None
  616. global skipping_deps,resuming
  617. skipping_deps = False
  618. resuming = False
  619. if opt.profile:
  620. start = time.time()
  621. self.ts.test_name = cmd # NB: Do not remove, this needs to be set twice
  622. cdata = self.gm.dpy_data[cmd]
  623. # self.ts.test_dpydata = cdata
  624. self.ts.tmpdir_num = cdata[0]
  625. # self.ts.cfg = cfgs[str(cdata[0])] # will remove this eventually
  626. cfg = cfgs[str(cdata[0])]
  627. for k in ( 'seed_len', 'seed_id',
  628. 'wpasswd', 'kapasswd',
  629. 'segwit', 'hash_preset',
  630. 'bw_filename', 'bw_params', 'ref_bw_seed_id',
  631. 'addr_idx_list', 'pass_idx_list' ):
  632. if k in cfg:
  633. setattr(self.ts,k,cfg[k])
  634. ret = getattr(self.ts,cmd)(*arg_list) # run the test
  635. if type(ret).__name__ == 'coroutine':
  636. ret = run_session(ret)
  637. self.process_retval(cmd,ret)
  638. if opt.profile:
  639. omsg('\r\033[50C{:.4f}'.format( time.time() - start ))
  640. if cmd == opt.exit_after:
  641. sys.exit(0)
  642. def warn_skipped(self):
  643. if self.skipped_warnings:
  644. print(yellow('The following tests were skipped and may require attention:'))
  645. r = '-' * 72 + '\n'
  646. print(r+('\n'+r).join(self.skipped_warnings))
  647. def process_retval(self,cmd,ret):
  648. if type(ret).__name__ == 'MMGenPexpect':
  649. ret.ok()
  650. self.cmd_total += 1
  651. elif ret == 'ok':
  652. ok()
  653. self.cmd_total += 1
  654. elif ret in ('skip','silent'):
  655. pass
  656. elif type(ret) == tuple and ret[0] == 'skip_warn':
  657. self.skipped_warnings.append(
  658. 'Test {!r} was skipped:\n {}'.format(cmd,'\n '.join(ret[1].split('\n'))))
  659. else:
  660. die(2,f'{cmd!r} returned {ret}')
  661. def check_deps(self,cmds): # TODO: broken
  662. if len(cmds) != 1:
  663. die(1,f'Usage: {g.prog_name} check_deps <command>')
  664. cmd = cmds[0]
  665. if cmd not in self.gm.cmd_list:
  666. die(1,f'{cmd!r}: unrecognized command')
  667. if not opt.quiet:
  668. omsg(f'Checking dependencies for {cmd!r}')
  669. self.check_needs_rerun(self.ts,cmd,build=False)
  670. w = max(map(len,self.rebuild_list)) + 1
  671. for cmd in self.rebuild_list:
  672. c = self.rebuild_list[cmd]
  673. m = 'Rebuild' if (c[0] and c[1]) else 'Build' if c[0] else 'OK'
  674. omsg('cmd {:<{w}} {}'.format( cmd+':', m, w=w ))
  675. def generate_file_deps(self,cmd):
  676. return [(str(n),e) for exts,n in self.gm.dpy_data[cmd][2] for e in exts]
  677. def generate_cmd_deps(self,fdeps):
  678. return [cfgs[str(n)]['dep_generators'][ext] for n,ext in fdeps]
  679. def get_num_exts_for_cmd(self,cmd,dpy=False): # dpy ignored here
  680. try:
  681. num = str(self.gm.dpy_data[cmd][0])
  682. except KeyError:
  683. qmsg_r(f'Missing dependency {cmd!r}')
  684. gname = self.gm.find_cmd_in_groups(cmd)
  685. if gname:
  686. kwargs = self.gm.cmd_groups[gname][1]
  687. kwargs.update({'add_dpy':True})
  688. self.gm.create_group(gname,**kwargs)
  689. num = str(self.gm.dpy_data[cmd][0])
  690. qmsg(f' found in group {gname!r}')
  691. else:
  692. qmsg(' not found in any command group!')
  693. raise
  694. dgl = cfgs[num]['dep_generators']
  695. if cmd in dgl.values():
  696. exts = [k for k in dgl if dgl[k] == cmd]
  697. return (num,exts)
  698. else:
  699. return None
  700. # main()
  701. if not skipping_deps: # do this before list cmds exit, so we stay in sync with shm_dir
  702. create_tmp_dirs(shm_dir)
  703. if opt.list_cmd_groups:
  704. CmdGroupMgr().list_cmd_groups()
  705. elif opt.list_cmds:
  706. list_cmds()
  707. elif usr_args and usr_args[0] in utils:
  708. globals()[usr_args[0]](*usr_args[1:])
  709. sys.exit(0)
  710. if opt.pause:
  711. set_restore_term_at_exit()
  712. set_environ_for_spawned_scripts()
  713. from mmgen.exception import TestSuiteException,TestSuiteFatalException
  714. try:
  715. tr = TestSuiteRunner(data_dir,trash_dir)
  716. tr.run_tests(usr_args)
  717. tr.warn_skipped()
  718. if network_id not in ('eth','etc','xmr'):
  719. stop_test_daemons(network_id)
  720. except KeyboardInterrupt:
  721. if network_id not in ('eth','etc','xmr'):
  722. stop_test_daemons(network_id)
  723. tr.warn_skipped()
  724. die(1,'\ntest.py exiting at user request')
  725. except TestSuiteException as e:
  726. die(2,e.args[0])
  727. except TestSuiteFatalException as e:
  728. die(4,e.args[0])
  729. except Exception:
  730. if 'exec_wrapper_init' in globals(): # test.py itself is running under exec_wrapper
  731. import traceback
  732. print(''.join(traceback.format_exception(*sys.exc_info())))
  733. msg(blue('Test script exited with error'))
  734. else:
  735. msg(blue('Spawned script exited with error'))
  736. raise
  737. except:
  738. raise