test.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2023 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(cfg,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 gc.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 Config()
  63. os.symlink(dest,data_dir)
  64. return shm_dir
  65. import sys,os,time
  66. if sys.argv[-1] == 'clean':
  67. os.environ['MMGEN_TEST_SUITE'] = '1'
  68. repo_root = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),os.pardir)))
  69. os.chdir(repo_root)
  70. sys.path[0] = repo_root
  71. from shutil import rmtree
  72. from test.overlay import get_overlay_tree_dir
  73. overlay_tree_dir = get_overlay_tree_dir(repo_root)
  74. rmtree(overlay_tree_dir,ignore_errors=True)
  75. print(f'Removed {os.path.relpath(overlay_tree_dir)!r}')
  76. else:
  77. # overlay must be set up before importing mmgen mods!
  78. import include.test_init
  79. repo_root = include.test_init.repo_root
  80. from mmgen.cfg import Config,gc
  81. from mmgen.color import red,yellow,green,blue,cyan,nocolor,init_color
  82. from mmgen.util import Msg,bmsg,die,suf,make_timestr,async_run
  83. from test.include.common import (
  84. set_globals,
  85. test_py_log_fn,
  86. test_py_error_fn,
  87. mk_tmpdir,
  88. cleandir,
  89. iqmsg,
  90. omsg,
  91. omsg_r,
  92. ok,
  93. start_test_daemons,
  94. stop_test_daemons,
  95. init_coverage,
  96. )
  97. try:
  98. os.unlink(os.path.join(repo_root,test_py_error_fn))
  99. except:
  100. pass
  101. os.environ['MMGEN_QUIET'] = '0' # for this script and spawned scripts
  102. opts_data = {
  103. 'sets': [
  104. ('list_current_cmd_groups',True,'list_cmd_groups',True),
  105. ('demo',True,'exact_output',True),
  106. ('demo',True,'buf_keypress',True),
  107. ('demo',True,'pexpect_spawn',True),
  108. ],
  109. 'text': {
  110. 'desc': 'Test suite for the MMGen suite',
  111. 'usage':'[options] [command [..command]] | [command_group[.command_subgroup][:command]]',
  112. 'options': """
  113. -h, --help Print this help message
  114. --, --longhelp Print help message for long options (common options)
  115. -a, --no-altcoin Skip altcoin tests (WIP)
  116. -A, --no-daemon-autostart Don't start and stop daemons automatically
  117. -B, --bech32 Generate and use Bech32 addresses
  118. -b, --buf-keypress Use buffered keypresses as with real human input
  119. (often required on slow systems, or under emulation)
  120. -c, --print-cmdline Print the command line of each spawned command
  121. -C, --coverage Produce code coverage info using trace module
  122. -x, --debug-pexpect Produce debugging output for pexpect calls
  123. --, --demo Add extra delay after each send to make input visible.
  124. Implies --exact-output --pexpect-spawn --buf-keypress
  125. -d, --deps-only Run a command or command subgroup’s dependencies without
  126. running the command or command group itself.
  127. -D, --no-daemon-stop Don't stop auto-started daemons after running tests
  128. -E, --direct-exec Bypass pexpect and execute a command directly (for
  129. debugging only)
  130. -e, --exact-output Show the exact output of the MMGen script(s) being run
  131. -G, --exclude-groups=G Exclude the specified command groups (comma-separated)
  132. -l, --list-cmds List the test script’s available commands
  133. -L, --list-cmd-groups List the test script’s command groups and subgroups
  134. -g, --list-current-cmd-groups List command groups for current configuration
  135. -n, --names Display command names instead of descriptions
  136. -N, --no-timings Suppress display of timing information
  137. -o, --log Log commands to file {lf!r}
  138. -O, --pexpect-spawn Use pexpect.spawn instead of popen_spawn (much slower,
  139. kut does real terminal emulation)
  140. -p, --pause Pause between tests, resuming on keypress
  141. -P, --profile Record the execution time of each script
  142. -q, --quiet Produce minimal output. Suppress dependency info
  143. -r, --resume=c Resume at command 'c' after interrupted run
  144. -R, --resume-after=c Same, but resume at command following 'c'
  145. -t, --step After resuming, execute one command and stop
  146. -S, --skip-deps Skip dependency checking for command
  147. -u, --usr-random Get random data interactively from user
  148. -T, --pexpect-timeout=T Set the timeout for pexpect
  149. -v, --verbose Produce more verbose output
  150. -W, --no-dw-delete Don't remove default wallet from data dir after dw tests
  151. are done
  152. -X, --exit-after=C Exit after command 'C'
  153. -y, --segwit Generate and use Segwit addresses
  154. -Y, --segwit-random Generate and use a random mix of Segwit and Legacy addrs
  155. """,
  156. 'notes': """
  157. If no command is given, the whole test suite is run for the currently
  158. specified coin (default BTC).
  159. For traceback output and error file support, set the EXEC_WRAPPER_TRACEBACK
  160. environment var
  161. """
  162. },
  163. 'code': {
  164. 'options': lambda proto,help_notes,s: s.format(
  165. lf = test_py_log_fn
  166. )
  167. }
  168. }
  169. # we need some opt values before running opts.init, so parse without initializing:
  170. po = Config(opts_data=opts_data,parse_only=True)._parsed_opts
  171. data_dir = Config.test_datadir
  172. # step 1: delete data_dir symlink in ./test;
  173. if not po.user_opts.get('skipping_deps'):
  174. try: os.unlink(data_dir)
  175. except: pass
  176. # step 2: opts.init will create new data_dir in ./test (if not cfg.skipping_deps)
  177. cfg = Config(opts_data=opts_data)
  178. set_globals(cfg)
  179. from test.test_py_d.common import ( # this must be loaded after set_globals()
  180. get_file_with_ext,
  181. confirm_continue
  182. )
  183. type(cfg)._reset_ok += (
  184. 'no_daemon_autostart',
  185. 'names',
  186. 'no_timings',
  187. 'exit_after',
  188. 'resuming',
  189. 'skipping_deps' )
  190. cfg.resuming = any(k in po.user_opts for k in ('resume','resume_after'))
  191. cfg.skipping_deps = cfg.resuming or 'skip_deps' in po.user_opts
  192. cmd_args = cfg._args
  193. if cfg.pexpect_spawn and gc.platform == 'win':
  194. die(1,'--pexpect-spawn option not supported on Windows platform, exiting')
  195. if cfg.daemon_id and cfg.daemon_id in cfg.blacklisted_daemons.split():
  196. die(1,f'test.py: daemon {cfg.daemon_id!r} blacklisted, exiting')
  197. network_id = cfg.coin.lower() + ('_tn' if cfg.testnet else '')
  198. proto = cfg._proto
  199. # step 3: move data_dir to /dev/shm and symlink it back to ./test:
  200. trash_dir = os.path.join('test','trash')
  201. trash_dir2 = os.path.join('test','trash2')
  202. if not cfg.skipping_deps:
  203. shm_dir = create_shm_dir(data_dir,trash_dir)
  204. check_segwit_opts()
  205. testing_segwit = cfg.segwit or cfg.segwit_random or cfg.bech32
  206. if cfg.test_suite_deterministic:
  207. cfg.no_timings = True
  208. init_color(num_colors=0)
  209. os.environ['MMGEN_DISABLE_COLOR'] = '1' # for this script and spawned scripts
  210. if cfg.profile:
  211. cfg.names = True
  212. if cfg.exact_output:
  213. qmsg = qmsg_r = lambda s: None
  214. else:
  215. qmsg = cfg._util.qmsg
  216. qmsg_r = cfg._util.qmsg_r
  217. if cfg.skipping_deps:
  218. cfg.no_daemon_autostart = True
  219. from test.test_py_d.cfg import cfgs,fixup_cfgs
  220. for k in cfgs:
  221. cfgs[k]['tmpdir'] = os.path.join('test','tmp',str(k))
  222. fixup_cfgs()
  223. utils = {
  224. # 'check_deps': 'check dependencies for specified command (WIP)', # TODO
  225. 'clean': 'clean specified tmp dir(s) (specify by integer, no arg = all dirs)',
  226. }
  227. def list_cmds():
  228. def gen_output():
  229. gm = CmdGroupMgr()
  230. cw,d = 0,[]
  231. yield green('AVAILABLE COMMANDS:')
  232. for gname in gm.cmd_groups:
  233. ts = gm.gm_init_group(None,gname,None,None)
  234. desc = ts.__doc__.strip() if ts.__doc__ else type(ts).__name__
  235. d.append( (gname,desc,gm.cmd_list,gm.dpy_data) )
  236. cw = max(max(len(k) for k in gm.dpy_data),cw)
  237. for gname,gdesc,clist,dpdata in d:
  238. yield '\n'+green(f'{gname!r} - {gdesc}:')
  239. for cmd in clist:
  240. data = dpdata[cmd]
  241. yield ' {:{w}} - {}'.format(
  242. cmd,
  243. (data if type(data) == str else data[1]),
  244. w = cw )
  245. w = max(map(len,utils))
  246. yield '\n'+green('AVAILABLE UTILITIES:')
  247. for cmd in sorted(utils):
  248. yield ' {:{w}} - {}'.format( cmd, utils[cmd], w=w )
  249. from mmgen.ui import do_pager
  250. do_pager('\n'.join(gen_output()))
  251. sys.exit(0)
  252. def do_between():
  253. if cfg.pause:
  254. confirm_continue()
  255. elif (cfg.verbose or cfg.exact_output) and not cfg.skipping_deps:
  256. sys.stderr.write('\n')
  257. def list_tmpdirs():
  258. return {k:cfgs[k]['tmpdir'] for k in cfgs}
  259. def clean(usr_dirs=None,clean_overlay=True):
  260. if cfg.skipping_deps:
  261. return
  262. all_dirs = list_tmpdirs()
  263. dirnums = map(int,(usr_dirs if usr_dirs is not None else all_dirs))
  264. dirlist = list(map(str,sorted(dirnums)))
  265. for d in dirlist:
  266. if d in all_dirs:
  267. cleandir(all_dirs[d])
  268. else:
  269. die(1,f'{d}: invalid directory number')
  270. if dirlist:
  271. iqmsg(green('Cleaned tmp director{} {}'.format(
  272. suf(dirlist,'ies'),
  273. ' '.join(dirlist))
  274. ))
  275. cleandir(data_dir)
  276. cleandir(trash_dir)
  277. cleandir(trash_dir2)
  278. iqmsg(green(f'Cleaned directories {data_dir!r} {trash_dir!r} {trash_dir2!r}'))
  279. if clean_overlay:
  280. cleandir(overlay_tree_dir)
  281. iqmsg(green(f'Cleaned directory {os.path.relpath(overlay_tree_dir)!r}'))
  282. def create_tmp_dirs(shm_dir):
  283. if gc.platform == 'win':
  284. for cfg in sorted(cfgs):
  285. mk_tmpdir(cfgs[cfg]['tmpdir'])
  286. else:
  287. os.makedirs( os.path.join('test','tmp'), mode=0o755, exist_ok=True )
  288. for cfg in sorted(cfgs):
  289. src = os.path.join(shm_dir,cfgs[cfg]['tmpdir'].split('/')[-1])
  290. mk_tmpdir(src)
  291. try:
  292. os.unlink(cfgs[cfg]['tmpdir'])
  293. except OSError as e:
  294. if e.errno != 2:
  295. raise
  296. finally:
  297. os.symlink(src,cfgs[cfg]['tmpdir'])
  298. def set_restore_term_at_exit():
  299. import termios,atexit
  300. fd = sys.stdin.fileno()
  301. old = termios.tcgetattr(fd)
  302. def at_exit():
  303. termios.tcsetattr(fd, termios.TCSADRAIN, old)
  304. atexit.register(at_exit)
  305. class CmdGroupMgr(object):
  306. from test.test_py_d.cfg import cmd_groups_dfl,cmd_groups_extra
  307. cmd_groups = cmd_groups_dfl.copy()
  308. cmd_groups.update(cmd_groups_extra)
  309. @staticmethod
  310. def create_cmd_group(cls,sg_name=None):
  311. cmd_group_in = dict(cls.cmd_group_in)
  312. if sg_name and 'subgroup.' + sg_name not in cmd_group_in:
  313. die(1,f'{sg_name!r}: no such subgroup in test group {cls.__name__}')
  314. def add_entries(key,add_deps=True,added_subgroups=[]):
  315. if add_deps:
  316. for dep in cmd_group_in['subgroup.'+key]:
  317. for e in add_entries(dep):
  318. yield e
  319. assert isinstance(cls.cmd_subgroups[key][0],str), f'header for subgroup {key!r} missing!'
  320. if not key in added_subgroups:
  321. for e in cls.cmd_subgroups[key][1:]:
  322. yield e
  323. added_subgroups.append(key)
  324. def gen():
  325. for name,data in cls.cmd_group_in:
  326. if name.startswith('subgroup.'):
  327. from mmgen.util2 import removeprefix
  328. sg_key = removeprefix(name,'subgroup.')
  329. # sg_key = name.removeprefix('subgroup.') # Python 3.9
  330. if sg_name in (None,sg_key):
  331. for e in add_entries(
  332. sg_key,
  333. add_deps = sg_name and not cfg.skipping_deps,
  334. added_subgroups = [sg_name] if cfg.deps_only else [] ):
  335. yield e
  336. if cfg.deps_only and sg_key == sg_name:
  337. return
  338. elif not cfg.skipping_deps:
  339. yield (name,data)
  340. return tuple(gen())
  341. def load_mod(self,gname,modname=None):
  342. clsname,kwargs = self.cmd_groups[gname]
  343. if modname == None and 'modname' in kwargs:
  344. modname = kwargs['modname']
  345. import importlib
  346. modpath = f'test.test_py_d.ts_{modname or gname}'
  347. return getattr(importlib.import_module(modpath),clsname)
  348. def create_group(self,gname,sg_name,full_data=False,modname=None,is3seed=False,add_dpy=False):
  349. """
  350. Initializes the list 'cmd_list' and dict 'dpy_data' from module's cmd_group data.
  351. Alternatively, if called with 'add_dpy=True', updates 'dpy_data' from module data
  352. without touching 'cmd_list'
  353. """
  354. cls = self.load_mod(gname,modname)
  355. cdata = []
  356. def get_shared_deps(cmdname,tmpdir_idx):
  357. """
  358. shared_deps are "implied" dependencies for all cmds in cmd_group that don't appear in
  359. the cmd_group data or cmds' argument lists. Supported only for 3seed tests at present.
  360. """
  361. if not hasattr(cls,'shared_deps'):
  362. return []
  363. return [k for k,v in cfgs[str(tmpdir_idx)]['dep_generators'].items()
  364. if k in cls.shared_deps and v != cmdname]
  365. if not hasattr(cls,'cmd_group'):
  366. cls.cmd_group = self.create_cmd_group(cls,sg_name)
  367. for a,b in cls.cmd_group:
  368. if is3seed:
  369. for n,(i,j) in enumerate(zip(cls.tmpdir_nums,(128,192,256))):
  370. k = f'{a}_{n+1}'
  371. if hasattr(cls,'skip_cmds') and k in cls.skip_cmds:
  372. continue
  373. sdeps = get_shared_deps(k,i)
  374. if type(b) == str:
  375. cdata.append( (k, (i,f'{b} ({j}-bit)',[[[]+sdeps,i]])) )
  376. else:
  377. cdata.append( (k, (i,f'{b[1]} ({j}-bit)',[[b[0]+sdeps,i]])) )
  378. else:
  379. cdata.append( (a, b if full_data else (cls.tmpdir_nums[0],b,[[[],cls.tmpdir_nums[0]]])) )
  380. if add_dpy:
  381. self.dpy_data.update(dict(cdata))
  382. else:
  383. self.cmd_list = tuple(e[0] for e in cdata)
  384. self.dpy_data = dict(cdata)
  385. return cls
  386. def gm_init_group(self,trunner,gname,sg_name,spawn_prog):
  387. kwargs = self.cmd_groups[gname][1]
  388. cls = self.create_group(gname,sg_name,**kwargs)
  389. cls.group_name = gname
  390. return cls(trunner,cfgs,spawn_prog)
  391. def get_cls_by_gname(self,gname):
  392. return self.load_mod( gname, self.cmd_groups[gname][1].get('modname') )
  393. def list_cmd_groups(self):
  394. ginfo = []
  395. for gname in self.cmd_groups:
  396. ginfo.append(( gname, self.get_cls_by_gname(gname) ))
  397. if cfg.list_current_cmd_groups:
  398. exclude = (cfg.exclude_groups or '').split(',')
  399. ginfo = [g for g in ginfo
  400. if network_id in g[1].networks
  401. and not g[0] in exclude
  402. and g[0] in tuple(self.cmd_groups_dfl) + tuple(cmd_args) ]
  403. desc = 'CONFIGURED'
  404. else:
  405. desc = 'AVAILABLE'
  406. def gen_output():
  407. yield green(f'{desc} COMMAND GROUPS AND SUBGROUPS:')
  408. yield ''
  409. for name,cls in ginfo:
  410. yield ' {} - {}'.format(
  411. yellow(name.ljust(13)),
  412. (cls.__doc__.strip() if cls.__doc__ else cls.__name__) )
  413. if hasattr(cls,'cmd_subgroups'):
  414. subgroups = {k:v for k,v in cls.cmd_subgroups.items() if not k.startswith('_')}
  415. max_w = max(len(k) for k in subgroups)
  416. for k,v in subgroups.items():
  417. yield ' + {} · {}'.format( cyan(k.ljust(max_w+1)), v[0] )
  418. from mmgen.ui import do_pager
  419. do_pager('\n'.join(gen_output()))
  420. Msg( '\n' + ' '.join(e[0] for e in ginfo) )
  421. sys.exit(0)
  422. def find_cmd_in_groups(self,cmd,group=None):
  423. """
  424. Search for a test command in specified group or all configured command groups
  425. and return it as a string. Loads modules but alters no global variables.
  426. """
  427. if group:
  428. if not group in [e[0] for e in self.cmd_groups]:
  429. die(1,f'{group!r}: unrecognized group')
  430. groups = [self.cmd_groups[group]]
  431. else:
  432. groups = self.cmd_groups
  433. for gname in groups:
  434. cls = self.get_cls_by_gname(gname)
  435. if not hasattr(cls,'cmd_group'):
  436. cls.cmd_group = self.create_cmd_group(cls)
  437. if cmd in cls.cmd_group: # first search the class
  438. return gname
  439. if cmd in dir(cls(None,None,None)): # then a throwaway instance
  440. return gname # cmd might exist in more than one group - we'll go with the first
  441. return None
  442. class TestSuiteRunner(object):
  443. 'test suite runner'
  444. def __del__(self):
  445. if cfg.log:
  446. self.log_fd.close()
  447. def __init__(self,data_dir,trash_dir):
  448. self.data_dir = data_dir
  449. self.trash_dir = trash_dir
  450. self.cmd_total = 0
  451. self.rebuild_list = {}
  452. self.gm = CmdGroupMgr()
  453. self.repo_root = repo_root
  454. self.skipped_warnings = []
  455. self.resume_cmd = None
  456. self.deps_only = None
  457. if cfg.log:
  458. self.log_fd = open(test_py_log_fn,'a')
  459. self.log_fd.write(f'\nLog started: {make_timestr()} UTC\n')
  460. omsg(f'INFO → Logging to file {test_py_log_fn!r}')
  461. else:
  462. self.log_fd = None
  463. if cfg.coverage:
  464. coverdir,accfile = init_coverage()
  465. omsg(f'INFO → Writing coverage files to {coverdir!r}')
  466. self.pre_args = ['python3','-m','trace','--count','--coverdir='+coverdir,'--file='+accfile]
  467. else:
  468. self.pre_args = ['python3'] if gc.platform == 'win' else []
  469. if cfg.pexpect_spawn:
  470. omsg(f'INFO → Using pexpect.spawn() for real terminal emulation')
  471. self.set_spawn_env()
  472. def set_spawn_env(self):
  473. self.spawn_env = dict(os.environ)
  474. self.spawn_env.update({
  475. 'MMGEN_NO_LICENSE': '1',
  476. 'MMGEN_BOGUS_SEND': '1',
  477. 'MMGEN_TEST_SUITE_PEXPECT': '1',
  478. 'EXEC_WRAPPER_SPAWN':'1',
  479. # if test.py itself is running under exec_wrapper, disable writing of traceback file for spawned script
  480. 'EXEC_WRAPPER_TRACEBACK': '' if os.getenv('MMGEN_EXEC_WRAPPER') else '1',
  481. })
  482. if cfg.exact_output:
  483. from mmgen.term import get_terminal_size
  484. self.spawn_env['MMGEN_COLUMNS'] = str(get_terminal_size().width)
  485. else:
  486. self.spawn_env['MMGEN_COLUMNS'] = '120'
  487. def spawn_wrapper(self,cmd,
  488. args = [],
  489. extra_desc = '',
  490. no_output = False,
  491. msg_only = False,
  492. no_msg = False,
  493. cmd_dir = 'cmds',
  494. no_exec_wrapper = False,
  495. timeout = None,
  496. pexpect_spawn = None,
  497. direct_exec = False,
  498. env = {} ):
  499. desc = self.ts.test_name if cfg.names else self.gm.dpy_data[self.ts.test_name][1]
  500. if extra_desc:
  501. desc += ' ' + extra_desc
  502. cmd_path = (
  503. cmd if cfg.system # cfg.system is broken for main test group with overlay tree
  504. else os.path.relpath(os.path.join(repo_root,cmd_dir,cmd)) )
  505. args = (
  506. self.pre_args +
  507. ([] if no_exec_wrapper else ['scripts/exec_wrapper.py']) +
  508. [cmd_path] +
  509. self.passthru_opts +
  510. self.ts.extra_spawn_args +
  511. args )
  512. qargs = ['{q}{}{q}'.format( a, q = "'" if ' ' in a else '' ) for a in args]
  513. cmd_disp = ' '.join(qargs).replace('\\','/') # for mingw
  514. if cfg.log:
  515. self.log_fd.write('[{}][{}:{}] {}\n'.format(
  516. proto.coin.lower(),
  517. self.ts.group_name,
  518. self.ts.test_name,
  519. cmd_disp))
  520. for i in args: # die only after writing log entry
  521. if not isinstance(i,str):
  522. die(2,'Error: missing input files in cmd line?:\nName: {}\nCmdline: {!r}'.format(
  523. self.ts.test_name,
  524. args ))
  525. if not no_msg:
  526. t_pfx = '' if cfg.no_timings else f'[{time.time() - self.start_time:08.2f}] '
  527. if cfg.verbose or cfg.print_cmdline or cfg.exact_output:
  528. omsg(green(f'{t_pfx}Testing: {desc}'))
  529. if not msg_only:
  530. clr1,clr2 = (nocolor,nocolor) if cfg.print_cmdline else (green,cyan)
  531. omsg(
  532. clr1('Executing: ') +
  533. clr2(repr(cmd_disp) if gc.platform == 'win' else cmd_disp)
  534. )
  535. else:
  536. omsg_r('{a}Testing {b}: {c}'.format(
  537. a = t_pfx,
  538. b = desc,
  539. c = 'OK\n' if direct_exec or cfg.direct_exec else ''))
  540. if msg_only:
  541. return
  542. # NB: the `pexpect_spawn` arg enables hold_protect and send_delay while the corresponding cmdline
  543. # option does not. For performance reasons, this is the desired behavior. For full emulation of
  544. # the user experience with hold protect enabled, specify --buf-keypress or --demo.
  545. send_delay = 0.4 if pexpect_spawn is True or cfg.buf_keypress else None
  546. pexpect_spawn = pexpect_spawn if pexpect_spawn is not None else bool(cfg.pexpect_spawn)
  547. spawn_env = dict(self.ts.spawn_env)
  548. spawn_env.update({
  549. 'MMGEN_HOLD_PROTECT_DISABLE': '' if send_delay else '1',
  550. 'MMGEN_TEST_SUITE_POPEN_SPAWN': '' if pexpect_spawn else '1',
  551. })
  552. spawn_env.update(env)
  553. from test.include.pexpect import MMGenPexpect
  554. return MMGenPexpect(
  555. args = args,
  556. no_output = no_output,
  557. spawn_env = spawn_env,
  558. pexpect_spawn = pexpect_spawn,
  559. timeout = timeout,
  560. send_delay = send_delay,
  561. direct_exec = direct_exec )
  562. def end_msg(self):
  563. t = int(time.time() - self.start_time)
  564. sys.stderr.write(green(
  565. f'{self.cmd_total} test{suf(self.cmd_total)} performed' +
  566. ('\n' if cfg.no_timings else f'. Elapsed time: {t//60:02d}:{t%60:02d}\n')
  567. ))
  568. def init_group(self,gname,sg_name=None,cmd=None,quiet=False,do_clean=True):
  569. ts_cls = CmdGroupMgr().load_mod(gname)
  570. if gc.platform == 'win' and ts_cls.win_skip:
  571. omsg(f'Skipping test {gname!r} for Windows platform')
  572. return False
  573. for k in ('segwit','segwit_random','bech32'):
  574. if getattr(cfg,k):
  575. segwit_opt = k
  576. break
  577. else:
  578. segwit_opt = None
  579. def gen_msg():
  580. yield ('{g}:{c}' if cmd else 'test group {g!r}').format(g=gname,c=cmd)
  581. if len(ts_cls.networks) != 1:
  582. yield f' for {proto.coin} {proto.network}'
  583. if segwit_opt:
  584. yield ' (--{})'.format( segwit_opt.replace('_','-') )
  585. m = ''.join(gen_msg())
  586. if segwit_opt and not ts_cls.segwit_opts_ok:
  587. iqmsg('INFO → skipping ' + m)
  588. return False
  589. # 'networks = ()' means all networks allowed
  590. nws = [(e.split('_')[0],'testnet') if '_' in e else (e,'mainnet') for e in ts_cls.networks]
  591. if nws:
  592. coin = proto.coin.lower()
  593. nw = ('mainnet','testnet')[proto.testnet]
  594. for a,b in nws:
  595. if a == coin and b == nw:
  596. break
  597. else:
  598. iqmsg('INFO → skipping ' + m)
  599. return False
  600. if do_clean:
  601. clean(ts_cls.tmpdir_nums,clean_overlay=False)
  602. if not quiet:
  603. bmsg('Executing ' + m)
  604. if (not self.daemon_started) and self.gm.get_cls_by_gname(gname).need_daemon:
  605. start_test_daemons(network_id,remove_datadir=True)
  606. self.daemon_started = True
  607. self.ts = self.gm.gm_init_group(self,gname,sg_name,self.spawn_wrapper)
  608. self.ts_clsname = type(self.ts).__name__
  609. # pass through opts from cmdline (po.user_opts)
  610. self.passthru_opts = ['--{}{}'.format(
  611. k.replace('_','-'),
  612. '' if cfg._uopts[k] is True else '=' + cfg._uopts[k]
  613. ) for k in cfg._uopts if k in self.ts.base_passthru_opts + self.ts.passthru_opts]
  614. if cfg.resuming:
  615. rc = cfg.resume or cfg.resume_after
  616. offset = 1 if cfg.resume_after else 0
  617. self.resume_cmd = self.gm.cmd_list[self.gm.cmd_list.index(rc)+offset]
  618. omsg(f'INFO → Resuming at command {self.resume_cmd!r}')
  619. if cfg.step:
  620. cfg.exit_after = self.resume_cmd
  621. if cfg.exit_after and cfg.exit_after not in self.gm.cmd_list:
  622. die(1,f'{cfg.exit_after!r}: command not recognized')
  623. return True
  624. def run_tests(self,cmd_args):
  625. self.start_time = time.time()
  626. self.daemon_started = False
  627. gname_save = None
  628. if cmd_args:
  629. for arg in cmd_args:
  630. if arg in self.gm.cmd_groups:
  631. if not self.init_group(arg):
  632. continue
  633. for cmd in self.gm.cmd_list:
  634. self.check_needs_rerun(cmd,build=True)
  635. do_between()
  636. else:
  637. def parse_arg(arg):
  638. if '.' in arg:
  639. a,b = arg.split('.')
  640. return [a] + b.split(':') if ':' in b else [a,b,None]
  641. elif ':' in arg:
  642. a,b = arg.split(':')
  643. return [a,None,b]
  644. else:
  645. return [self.gm.find_cmd_in_groups(arg),None,arg]
  646. gname,sg_name,cmdname = parse_arg(arg)
  647. if gname:
  648. same_grp = gname == gname_save # same group as previous cmd: don't clean, suppress blue msg
  649. if not self.init_group(gname,sg_name,cmdname,quiet=same_grp,do_clean=not same_grp):
  650. continue
  651. if cmdname:
  652. if cfg.deps_only:
  653. self.deps_only = cmdname
  654. try:
  655. self.check_needs_rerun(cmdname,build=True)
  656. except Exception as e: # allow calling of functions not in cmd_group
  657. if isinstance(e,KeyError) and e.args[0] == cmdname:
  658. ret = getattr(self.ts,cmdname)()
  659. if type(ret).__name__ == 'coroutine':
  660. async_run(ret)
  661. else:
  662. raise
  663. do_between()
  664. else:
  665. for cmd in self.gm.cmd_list:
  666. self.check_needs_rerun(cmd,build=True)
  667. do_between()
  668. gname_save = gname
  669. else:
  670. die(1,f'{arg!r}: command not recognized')
  671. else:
  672. if cfg.exclude_groups:
  673. exclude = cfg.exclude_groups.split(',')
  674. for e in exclude:
  675. if e not in self.gm.cmd_groups_dfl:
  676. die(1,f'{e!r}: group not recognized')
  677. for gname in self.gm.cmd_groups_dfl:
  678. if cfg.exclude_groups and gname in exclude:
  679. continue
  680. if not self.init_group(gname):
  681. continue
  682. for cmd in self.gm.cmd_list:
  683. self.check_needs_rerun(cmd,build=True)
  684. do_between()
  685. self.end_msg()
  686. def check_needs_rerun(self,cmd,
  687. build = False,
  688. root = True,
  689. force_delete = False,
  690. dpy = False ):
  691. self.ts.test_name = cmd
  692. if self.ts_clsname == 'TestSuiteMain' and testing_segwit and cmd not in self.ts.segwit_do:
  693. return False
  694. rerun = root # force_delete is not passed to recursive call
  695. fns = []
  696. if force_delete or not root:
  697. # does cmd produce a needed dependency(ies)?
  698. ret = self.get_num_exts_for_cmd(cmd,dpy)
  699. if ret:
  700. for ext in ret[1]:
  701. fn = get_file_with_ext(cfgs[ret[0]]['tmpdir'],ext,delete=build)
  702. if fn:
  703. if force_delete: os.unlink(fn)
  704. else: fns.append(fn)
  705. else: rerun = True
  706. fdeps = self.generate_file_deps(cmd)
  707. cdeps = self.generate_cmd_deps(fdeps)
  708. for fn in fns:
  709. my_age = os.stat(fn).st_mtime
  710. for num,ext in fdeps:
  711. f = get_file_with_ext(cfgs[num]['tmpdir'],ext,delete=build)
  712. if f and os.stat(f).st_mtime > my_age:
  713. rerun = True
  714. for cdep in cdeps:
  715. if self.check_needs_rerun(cdep,build=build,root=False,dpy=cmd):
  716. rerun = True
  717. if build:
  718. if rerun:
  719. for fn in fns:
  720. if not root:
  721. os.unlink(fn)
  722. if not (dpy and cfg.skipping_deps):
  723. self.run_test(cmd)
  724. if not root:
  725. do_between()
  726. else:
  727. # If prog produces multiple files:
  728. if cmd not in self.rebuild_list or rerun == True:
  729. self.rebuild_list[cmd] = (rerun,fns[0] if fns else '') # FIX
  730. return rerun
  731. def run_test(self,cmd):
  732. if self.deps_only and cmd == self.deps_only:
  733. sys.exit(0)
  734. d = [(str(num),ext) for exts,num in self.gm.dpy_data[cmd][2] for ext in exts]
  735. # delete files depended on by this cmd
  736. arg_list = [get_file_with_ext(cfgs[num]['tmpdir'],ext) for num,ext in d]
  737. # remove shared_deps from arg list
  738. if hasattr(self.ts,'shared_deps'):
  739. arg_list = arg_list[:-len(self.ts.shared_deps)]
  740. if self.resume_cmd:
  741. if cmd != self.resume_cmd:
  742. return
  743. bmsg(f'Resuming at {self.resume_cmd!r}')
  744. self.resume_cmd = None
  745. cfg.skipping_deps = False
  746. cfg.resuming = False
  747. if cfg.profile:
  748. start = time.time()
  749. self.ts.test_name = cmd # NB: Do not remove, this needs to be set twice
  750. cdata = self.gm.dpy_data[cmd]
  751. # self.ts.test_dpydata = cdata
  752. self.ts.tmpdir_num = cdata[0]
  753. # self.ts.cfg = cfgs[str(cdata[0])] # will remove this eventually
  754. test_cfg = cfgs[str(cdata[0])]
  755. for k in ( 'seed_len', 'seed_id',
  756. 'wpasswd', 'kapasswd',
  757. 'segwit', 'hash_preset',
  758. 'bw_filename', 'bw_params', 'ref_bw_seed_id',
  759. 'addr_idx_list', 'pass_idx_list' ):
  760. if k in test_cfg:
  761. setattr(self.ts,k,test_cfg[k])
  762. ret = getattr(self.ts,cmd)(*arg_list) # run the test
  763. if type(ret).__name__ == 'coroutine':
  764. ret = async_run(ret)
  765. self.process_retval(cmd,ret)
  766. if cfg.profile:
  767. omsg('\r\033[50C{:.4f}'.format( time.time() - start ))
  768. if cmd == cfg.exit_after:
  769. sys.exit(0)
  770. def warn_skipped(self):
  771. if self.skipped_warnings:
  772. print(yellow('The following tests were skipped and may require attention:'))
  773. r = '-' * 72 + '\n'
  774. print(r+('\n'+r).join(self.skipped_warnings))
  775. def process_retval(self,cmd,ret):
  776. if type(ret).__name__ == 'MMGenPexpect':
  777. ret.ok()
  778. self.cmd_total += 1
  779. elif ret == 'ok':
  780. ok()
  781. self.cmd_total += 1
  782. elif ret == 'error':
  783. die(2,red(f'\nTest {self.ts.test_name!r} failed'))
  784. elif ret in ('skip','silent'):
  785. if ret == 'silent':
  786. self.cmd_total += 1
  787. elif type(ret) == tuple and ret[0] == 'skip_warn':
  788. self.skipped_warnings.append(
  789. 'Test {!r} was skipped:\n {}'.format(cmd,'\n '.join(ret[1].split('\n'))))
  790. else:
  791. die(2,f'{cmd!r} returned {ret}')
  792. def check_deps(self,cmds): # TODO: broken
  793. if len(cmds) != 1:
  794. die(1,f'Usage: {gc.prog_name} check_deps <command>')
  795. cmd = cmds[0]
  796. if cmd not in self.gm.cmd_list:
  797. die(1,f'{cmd!r}: unrecognized command')
  798. if not cfg.quiet:
  799. omsg(f'Checking dependencies for {cmd!r}')
  800. self.check_needs_rerun(self.ts,cmd,build=False)
  801. w = max(map(len,self.rebuild_list)) + 1
  802. for cmd in self.rebuild_list:
  803. c = self.rebuild_list[cmd]
  804. m = 'Rebuild' if (c[0] and c[1]) else 'Build' if c[0] else 'OK'
  805. omsg('cmd {:<{w}} {}'.format( cmd+':', m, w=w ))
  806. def generate_file_deps(self,cmd):
  807. return [(str(n),e) for exts,n in self.gm.dpy_data[cmd][2] for e in exts]
  808. def generate_cmd_deps(self,fdeps):
  809. return [cfgs[str(n)]['dep_generators'][ext] for n,ext in fdeps]
  810. def get_num_exts_for_cmd(self,cmd,dpy=False): # dpy ignored here
  811. try:
  812. num = str(self.gm.dpy_data[cmd][0])
  813. except KeyError:
  814. qmsg_r(f'Missing dependency {cmd!r}')
  815. gname = self.gm.find_cmd_in_groups(cmd)
  816. if gname:
  817. kwargs = self.gm.cmd_groups[gname][1]
  818. kwargs.update({'add_dpy':True})
  819. self.gm.create_group(gname,None,**kwargs)
  820. num = str(self.gm.dpy_data[cmd][0])
  821. qmsg(f' found in group {gname!r}')
  822. else:
  823. qmsg(' not found in any command group!')
  824. raise
  825. dgl = cfgs[num]['dep_generators']
  826. if cmd in dgl.values():
  827. exts = [k for k in dgl if dgl[k] == cmd]
  828. return (num,exts)
  829. else:
  830. return None
  831. # main()
  832. if not cfg.skipping_deps: # do this before list cmds exit, so we stay in sync with shm_dir
  833. create_tmp_dirs(shm_dir)
  834. if cfg.list_cmd_groups:
  835. CmdGroupMgr().list_cmd_groups()
  836. elif cfg.list_cmds:
  837. list_cmds()
  838. elif cmd_args and cmd_args[0] in utils:
  839. globals()[cmd_args[0]](*cmd_args[1:])
  840. sys.exit(0)
  841. if cfg.pause:
  842. set_restore_term_at_exit()
  843. from mmgen.exception import TestSuiteException,TestSuiteFatalException,TestSuiteSpawnedScriptException
  844. try:
  845. tr = TestSuiteRunner(data_dir,trash_dir)
  846. tr.run_tests(cmd_args)
  847. tr.warn_skipped()
  848. if tr.daemon_started:
  849. stop_test_daemons(network_id)
  850. except KeyboardInterrupt:
  851. if tr.daemon_started:
  852. stop_test_daemons(network_id)
  853. tr.warn_skipped()
  854. die(1,'\ntest.py exiting at user request')
  855. except TestSuiteException as e:
  856. die(2,e.args[0])
  857. except TestSuiteFatalException as e:
  858. die(4,e.args[0])
  859. except TestSuiteSpawnedScriptException as e:
  860. # if spawned script is not running under exec_wrapper, output brief error msg:
  861. if os.getenv('MMGEN_EXEC_WRAPPER'):
  862. Msg(red(str(e)))
  863. Msg(blue('test.py: spawned script exited with error'))
  864. except Exception:
  865. # if test.py itself is running under exec_wrapper, re-raise so exec_wrapper can handle exception:
  866. if os.getenv('MMGEN_EXEC_WRAPPER'):
  867. raise
  868. except:
  869. raise