test.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. #!/usr/bin/env python3
  2. #
  3. # mmgen = Multi-Mode GENerator, command-line Bitcoin cold storage solution
  4. # Copyright (C)2013-2020 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 g.proto.mmtypes:
  24. die(1,'--{} option incompatible with {}'.format(k.replace('_','-'),g.proto.__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. rdie(1,"Unable to remove {!r}!".format(tdir))
  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('rm -rf {}/{}*'.format(tdir,pfx),shell=True,check=True)
  50. except Exception as e:
  51. die(2,'Unable to delete directory tree {}/{}* ({})'.format(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,'Unable to create temporary directory in {} ({})'.format(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. try: os.unlink(os.path.join(repo_root,'my.err'))
  68. except: pass
  69. from mmgen.common import *
  70. from mmgen.daemon import CoinDaemon
  71. from test.include.common import *
  72. from test.test_py_d.common import *
  73. g.quiet = False # if 'quiet' was set in config file, disable here
  74. os.environ['MMGEN_QUIET'] = '0' # for this script and spawned scripts
  75. opts_data = {
  76. 'sets': [('list_current_cmd_groups',True,'list_cmd_groups',True)],
  77. 'text': {
  78. 'desc': 'Test suite for the MMGen suite',
  79. 'usage':'[options] [command(s) or metacommand(s)]',
  80. 'options': """
  81. -h, --help Print this help message
  82. --, --longhelp Print help message for long options (common options)
  83. -A, --no-daemon-autostart Don't start and stop daemons automatically
  84. -B, --bech32 Generate and use Bech32 addresses
  85. -b, --buf-keypress Use buffered keypresses as with real human input
  86. (often required on slow systems, or under emulation)
  87. -c, --print-cmdline Print the command line of each spawned command
  88. -C, --coverage Produce code coverage info using trace module
  89. -x, --debug-pexpect Produce debugging output for pexpect calls
  90. -D, --no-daemon-stop Don't stop auto-started daemons after running tests
  91. -E, --direct-exec Bypass pexpect and execute a command directly (for
  92. debugging only)
  93. -e, --exact-output Show the exact output of the MMGen script(s) being run
  94. -G, --exclude-groups=G Exclude the specified command groups (comma-separated)
  95. -l, --list-cmds List and describe the commands in the test suite
  96. -L, --list-cmd-groups Output a list of command groups with descriptions
  97. -g, --list-current-cmd-groups List command groups for current configuration
  98. -n, --names Display command names instead of descriptions
  99. -o, --log Log commands to file {lf}
  100. -O, --pexpect-spawn Use pexpect.spawn instead of popen_spawn (much slower,
  101. kut does real terminal emulation)
  102. -p, --pause Pause between tests, resuming on keypress
  103. -P, --profile Record the execution time of each script
  104. -q, --quiet Produce minimal output. Suppress dependency info
  105. -r, --resume=c Resume at command 'c' after interrupted run
  106. -R, --resume-after=c Same, but resume at command following 'c'
  107. -s, --system Test scripts and modules installed on system rather
  108. than those in the repo root
  109. -S, --skip-deps Skip dependency checking for command
  110. -u, --usr-random Get random data interactively from user
  111. -t, --traceback Run the command inside the '{tbc}' script
  112. -T, --pexpect-timeout=T Set the timeout for pexpect
  113. -v, --verbose Produce more verbose output
  114. -W, --no-dw-delete Don't remove default wallet from data dir after dw tests are done
  115. -X, --exit-after=C Exit after command 'C'
  116. -y, --segwit Generate and use Segwit addresses
  117. -Y, --segwit-random Generate and use a random mix of Segwit and Legacy addrs
  118. """,
  119. 'notes': """
  120. If no command is given, the whole test suite is run.
  121. """
  122. },
  123. 'code': {
  124. 'options': lambda s: s.format(
  125. tbc='scripts/traceback_run.py',
  126. lf=log_file),
  127. }
  128. }
  129. data_dir = os.path.join('test','data_dir' + ('','-α')[bool(os.getenv('MMGEN_DEBUG_UTF8'))])
  130. # we need the values of two opts before running opts.init, so parse without initializing:
  131. _uopts = opts.init(opts_data,parse_only=True).user_opts
  132. # step 1: delete data_dir symlink in ./test;
  133. if not ('resume' in _uopts or 'skip_deps' in _uopts):
  134. try: os.unlink(data_dir)
  135. except: pass
  136. def get_coin():
  137. return (_uopts.get('coin') or 'btc').lower()
  138. network_id = get_network_id(get_coin(),bool(_uopts.get('testnet')))
  139. sys.argv.insert(1,'--data-dir=' + data_dir)
  140. sys.argv.insert(1,'--daemon-data-dir=test/daemons/' + get_coin())
  141. sys.argv.insert(1,'--rpc-port={}'.format(CoinDaemon(network_id,test_suite=True).rpc_port))
  142. # step 2: opts.init will create new data_dir in ./test (if not 'resume' or 'skip_deps'):
  143. usr_args = opts.init(opts_data)
  144. # step 3: move data_dir to /dev/shm and symlink it back to ./test:
  145. trash_dir = os.path.join('test','trash')
  146. if not ('resume' in _uopts or 'skip_deps' in _uopts):
  147. shm_dir = create_shm_dir(data_dir,trash_dir)
  148. check_segwit_opts()
  149. if opt.profile:
  150. opt.names = True
  151. if opt.exact_output:
  152. def msg(s): pass
  153. qmsg = qmsg_r = vmsg = vmsg_r = msg_r = msg
  154. if opt.resume or opt.resume_after:
  155. opt.skip_deps = True
  156. resume = opt.resume or opt.resume_after
  157. else:
  158. resume = False
  159. cfgs = { # addr_idx_lists (except 31,32,33,34) must contain exactly 8 addresses
  160. '1': { 'wpasswd': 'Dorian-α',
  161. 'kapasswd': 'Grok the blockchain',
  162. 'addr_idx_list': '12,99,5-10,5,12',
  163. 'dep_generators': {
  164. pwfile: 'walletgen',
  165. 'mmdat': 'walletgen',
  166. 'addrs': 'addrgen',
  167. 'rawtx': 'txcreate',
  168. 'txbump': 'txbump',
  169. 'sigtx': 'txsign',
  170. 'mmwords': 'export_mnemonic',
  171. 'mmseed': 'export_seed',
  172. 'mmhex': 'export_hex',
  173. 'mmincog': 'export_incog',
  174. 'mmincox': 'export_incog_hex',
  175. hincog_fn: 'export_incog_hidden',
  176. incog_id_fn: 'export_incog_hidden',
  177. 'akeys.mmenc': 'keyaddrgen'
  178. },
  179. },
  180. '2': { 'wpasswd': 'Hodling away',
  181. 'addr_idx_list': '37,45,3-6,22-23',
  182. 'seed_len': 128,
  183. 'dep_generators': {
  184. 'mmdat': 'walletgen2',
  185. 'addrs': 'addrgen2',
  186. 'rawtx': 'txcreate2',
  187. 'sigtx': 'txsign2',
  188. 'mmwords': 'export_mnemonic2',
  189. },
  190. },
  191. '3': { 'wpasswd': 'Major miner',
  192. 'addr_idx_list': '73,54,1022-1023,2-5',
  193. 'dep_generators': {
  194. 'mmdat': 'walletgen3',
  195. 'addrs': 'addrgen3',
  196. 'rawtx': 'txcreate3',
  197. 'sigtx': 'txsign3'
  198. },
  199. },
  200. '4': { 'wpasswd': 'Hashrate good',
  201. 'addr_idx_list': '63,1004,542-544,7-9',
  202. 'seed_len': 192,
  203. 'dep_generators': {
  204. 'mmdat': 'walletgen4',
  205. 'mmbrain': 'walletgen4',
  206. 'addrs': 'addrgen4',
  207. 'rawtx': 'txcreate4',
  208. 'sigtx': 'txsign4',
  209. 'txdo': 'txdo4',
  210. },
  211. 'bw_filename': 'brainwallet.mmbrain',
  212. 'bw_params': '192,1',
  213. },
  214. '5': { 'wpasswd': 'My changed password-α',
  215. 'hash_preset': '2',
  216. 'dep_generators': {
  217. 'mmdat': 'passchg',
  218. pwfile: 'passchg',
  219. },
  220. },
  221. '6': { 'seed_len': 128,
  222. 'seed_id': 'FE3C6545',
  223. 'ref_bw_seed_id': '33F10310',
  224. 'wpasswd': 'reference password',
  225. 'kapasswd': '',
  226. 'dep_generators': {
  227. 'mmdat': 'ref_walletgen_brain_1',
  228. pwfile: 'ref_walletgen_brain_1',
  229. 'addrs': 'refaddrgen_1',
  230. 'akeys.mmenc': 'refkeyaddrgen_1'
  231. },
  232. },
  233. '7': { 'seed_len': 192,
  234. 'seed_id': '1378FC64',
  235. 'ref_bw_seed_id': 'CE918388',
  236. 'wpasswd': 'reference password',
  237. 'kapasswd': '',
  238. 'dep_generators': {
  239. 'mmdat': 'ref_walletgen_brain_2',
  240. pwfile: 'ref_walletgen_brain_2',
  241. 'addrs': 'refaddrgen_2',
  242. 'akeys.mmenc': 'refkeyaddrgen_2'
  243. },
  244. },
  245. '8': { 'seed_len': 256,
  246. 'seed_id': '98831F3A',
  247. 'ref_bw_seed_id': 'B48CD7FC',
  248. 'wpasswd': 'reference password',
  249. 'kapasswd': '',
  250. 'dep_generators': {
  251. 'mmdat': 'ref_walletgen_brain_3',
  252. pwfile: 'ref_walletgen_brain_3',
  253. 'addrs': 'refaddrgen_3',
  254. 'akeys.mmenc': 'refkeyaddrgen_3'
  255. },
  256. },
  257. '9': { 'tool_enc_infn': 'tool_encrypt.in',
  258. 'dep_generators': {
  259. 'tool_encrypt.in': 'tool_encrypt',
  260. 'tool_encrypt.in.mmenc': 'tool_encrypt',
  261. },
  262. },
  263. '14': { 'kapasswd': 'Maxwell',
  264. 'wpasswd': 'The Halving',
  265. 'addr_idx_list': '61,998,502-504,7-9',
  266. 'seed_len': 256,
  267. 'dep_generators': {
  268. 'mmdat': 'walletgen14',
  269. 'addrs': 'addrgen14',
  270. 'akeys.mmenc': 'keyaddrgen14',
  271. },
  272. },
  273. '15': { 'wpasswd': 'Dorian-α',
  274. 'kapasswd': 'Grok the blockchain',
  275. 'addr_idx_list': '12,99,5-10,5,12',
  276. 'dep_generators': {
  277. pwfile: 'walletgen_dfl_wallet',
  278. 'addrs': 'addrgen_dfl_wallet',
  279. 'rawtx': 'txcreate_dfl_wallet',
  280. 'sigtx': 'txsign_dfl_wallet',
  281. 'mmseed': 'export_seed_dfl_wallet',
  282. 'del_dw_run': 'delete_dfl_wallet',
  283. },
  284. },
  285. '16': { 'wpasswd': 'My changed password',
  286. 'hash_preset': '2',
  287. 'dep_generators': {
  288. pwfile: 'passchg_dfl_wallet',
  289. },
  290. },
  291. '17': {},
  292. '18': {},
  293. '19': { 'wpasswd':'abc' }, # B2X
  294. '20': { 'wpasswd': 'Vsize it',
  295. 'addr_idx_list': '1-8',
  296. 'seed_len': 256,
  297. 'dep_generators': {
  298. 'mmdat': 'walletgen5',
  299. 'addrs': 'addrgen5',
  300. 'rawtx': 'txcreate5',
  301. 'sigtx': 'txsign5',
  302. },
  303. },
  304. '21': { 'wpasswd': 'Vsize it',
  305. 'addr_idx_list': '1-8',
  306. 'seed_len': 256,
  307. 'dep_generators': {
  308. 'mmdat': 'walletgen6',
  309. 'addrs': 'addrgen6',
  310. 'rawtx': 'txcreate6',
  311. 'sigtx': 'txsign6',
  312. },
  313. },
  314. '22': {},
  315. '23': {},
  316. # 26,27,28 are taken
  317. '31': {},
  318. '32': {},
  319. '33': {},
  320. '34': {},
  321. '40': {},
  322. '41': {},
  323. }
  324. def fixup_cfgs():
  325. for k in ('6','7','8'):
  326. cfgs['2'+k] = {}
  327. cfgs['2'+k].update(cfgs[k])
  328. for k in cfgs:
  329. cfgs[k]['tmpdir'] = os.path.join('test','tmp{}'.format(k))
  330. cfgs[k]['segwit'] = randbool() if opt.segwit_random else bool(opt.segwit or opt.bech32)
  331. from copy import deepcopy
  332. for a,b in (('6','11'),('7','12'),('8','13')):
  333. cfgs[b] = deepcopy(cfgs[a])
  334. cfgs[b]['tmpdir'] = os.path.join('test','tmp'+b)
  335. if g.debug_utf8:
  336. for k in cfgs: cfgs[k]['tmpdir'] += '-α'
  337. fixup_cfgs()
  338. utils = {
  339. # 'check_deps': 'check dependencies for specified command (WIP)', # TODO
  340. 'clean': 'clean specified tmp dir(s) (specify by integer, no arg = all dirs)',
  341. }
  342. def list_cmds():
  343. gm = CmdGroupMgr()
  344. cw,d = 0,[]
  345. Msg(green('AVAILABLE COMMANDS:'))
  346. for gname in gm.cmd_groups:
  347. ts = gm.gm_init_group(None,gname,None)
  348. d.append((gname,ts.__doc__.strip(),gm.cmd_list,gm.dpy_data))
  349. cw = max(max(len(k) for k in gm.dpy_data),cw)
  350. for gname,gdesc,clist,dpdata in d:
  351. Msg('\n'+green('{!r} - {}:'.format(gname,gdesc)))
  352. for cmd in clist:
  353. data = dpdata[cmd]
  354. Msg(' {:{w}} - {}'.format(cmd,data if type(data) == str else data[1],w=cw))
  355. w = max(map(len,utils))
  356. Msg('\n'+green('AVAILABLE UTILITIES:'))
  357. for cmd in sorted(utils):
  358. Msg(' {:{w}} - {}'.format(cmd,utils[cmd],w=w))
  359. sys.exit(0)
  360. def do_between():
  361. if opt.pause:
  362. confirm_continue()
  363. elif (opt.verbose or opt.exact_output) and not opt.skip_deps:
  364. sys.stderr.write('\n')
  365. def list_tmpdirs():
  366. return {k:cfgs[k]['tmpdir'] for k in cfgs}
  367. def clean(usr_dirs=None):
  368. if opt.skip_deps:
  369. return
  370. all_dirs = list_tmpdirs()
  371. dirnums = map(int,(usr_dirs if usr_dirs is not None else all_dirs))
  372. dirlist = list(map(str,sorted(dirnums)))
  373. for d in dirlist:
  374. if d in all_dirs:
  375. cleandir(all_dirs[d])
  376. else:
  377. die(1,'{}: invalid directory number'.format(d))
  378. if dirlist:
  379. iqmsg(green('Cleaned tmp director{} {}'.format(suf(dirlist,'ies'),' '.join(dirlist))))
  380. cleandir(data_dir)
  381. cleandir(trash_dir)
  382. iqmsg(green("Cleaned directories '{}'".format("' '".join([data_dir,trash_dir]))))
  383. def create_tmp_dirs(shm_dir):
  384. if g.platform == 'win':
  385. for cfg in sorted(cfgs):
  386. mk_tmpdir(cfgs[cfg]['tmpdir'])
  387. else:
  388. for cfg in sorted(cfgs):
  389. src = os.path.join(shm_dir,cfgs[cfg]['tmpdir'].split('/')[-1])
  390. mk_tmpdir(src)
  391. try:
  392. os.unlink(cfgs[cfg]['tmpdir'])
  393. except OSError as e:
  394. if e.errno != 2: raise
  395. finally:
  396. os.symlink(src,cfgs[cfg]['tmpdir'])
  397. def set_environ_for_spawned_scripts():
  398. from mmgen.term import get_terminal_size
  399. os.environ['MMGEN_TERMINAL_WIDTH'] = str(get_terminal_size().width)
  400. if os.getenv('MMGEN_DEBUG_ALL'):
  401. for name in g.env_opts:
  402. if name[:11] == 'MMGEN_DEBUG':
  403. os.environ[name] = '1'
  404. if not opt.pexpect_spawn: os.environ['MMGEN_TEST_SUITE_POPEN_SPAWN'] = '1'
  405. if not opt.system: os.environ['PYTHONPATH'] = repo_root
  406. if not opt.buf_keypress:
  407. os.environ['MMGEN_DISABLE_HOLD_PROTECT'] = '1'
  408. # If test.py itself is running under traceback, the spawned script shouldn't be, so disable this:
  409. if os.getenv('MMGEN_TRACEBACK') and not opt.traceback:
  410. os.environ['MMGEN_TRACEBACK'] = ''
  411. # Disable color in spawned scripts so pexpect can parse their output
  412. os.environ['MMGEN_DISABLE_COLOR'] = '1'
  413. os.environ['MMGEN_NO_LICENSE'] = '1'
  414. os.environ['MMGEN_MIN_URANDCHARS'] = '3'
  415. os.environ['MMGEN_BOGUS_SEND'] = '1'
  416. def set_restore_term_at_exit():
  417. import termios,atexit
  418. fd = sys.stdin.fileno()
  419. old = termios.tcgetattr(fd)
  420. def at_exit():
  421. termios.tcsetattr(fd, termios.TCSADRAIN, old)
  422. atexit.register(at_exit)
  423. class CmdGroupMgr(object):
  424. cmd_groups_dfl = {
  425. 'opts': ('TestSuiteOpts',{'full_data':True}),
  426. 'cfg': ('TestSuiteCfg',{'full_data':True}),
  427. 'helpscreens': ('TestSuiteHelp',{'modname':'misc','full_data':True}),
  428. 'main': ('TestSuiteMain',{'full_data':True}),
  429. 'conv': ('TestSuiteWalletConv',{'is3seed':True,'modname':'wallet'}),
  430. 'ref': ('TestSuiteRef',{}),
  431. 'ref3': ('TestSuiteRef3Seed',{'is3seed':True,'modname':'ref_3seed'}),
  432. 'ref3_addr': ('TestSuiteRef3Addr',{'is3seed':True,'modname':'ref_3seed'}),
  433. 'ref_altcoin': ('TestSuiteRefAltcoin',{}),
  434. 'seedsplit': ('TestSuiteSeedSplit',{}),
  435. 'tool': ('TestSuiteTool',{'full_data':True}),
  436. 'input': ('TestSuiteInput',{'full_data':True}),
  437. 'output': ('TestSuiteOutput',{'modname':'misc','full_data':True}),
  438. 'autosign': ('TestSuiteAutosign',{}),
  439. 'regtest': ('TestSuiteRegtest',{}),
  440. # 'chainsplit': ('TestSuiteChainsplit',{}),
  441. 'ethdev': ('TestSuiteEthdev',{}),
  442. }
  443. cmd_groups_extra = {
  444. 'autosign_btc': ('TestSuiteAutosignBTC',{'modname':'autosign'}),
  445. 'autosign_live': ('TestSuiteAutosignLive',{'modname':'autosign'}),
  446. 'autosign_live_simulate': ('TestSuiteAutosignLiveSimulate',{'modname':'autosign'}),
  447. 'create_ref_tx': ('TestSuiteRefTX',{'modname':'misc','full_data':True}),
  448. }
  449. cmd_groups = cmd_groups_dfl.copy()
  450. cmd_groups.update(cmd_groups_extra)
  451. def load_mod(self,gname,modname=None):
  452. clsname,kwargs = self.cmd_groups[gname]
  453. if modname == None and 'modname' in kwargs:
  454. modname = kwargs['modname']
  455. import importlib
  456. modpath = 'test.test_py_d.ts_{}'.format(modname or gname)
  457. return getattr(importlib.import_module(modpath),clsname)
  458. def create_group(self,gname,full_data=False,modname=None,is3seed=False,add_dpy=False):
  459. """
  460. Initializes the list 'cmd_list' and dict 'dpy_data' from module's cmd_group data.
  461. Alternatively, if called with 'add_dpy=True', updates 'dpy_data' from module data
  462. without touching 'cmd_list'
  463. """
  464. cls = self.load_mod(gname,modname)
  465. cdata = []
  466. def get_shared_deps(cmdname,tmpdir_idx):
  467. """
  468. shared_deps are "implied" dependencies for all cmds in cmd_group that don't appear in
  469. the cmd_group data or cmds' argument lists. Supported only for 3seed tests at present.
  470. """
  471. if not hasattr(cls,'shared_deps'):
  472. return []
  473. return [k for k,v in cfgs[str(tmpdir_idx)]['dep_generators'].items()
  474. if k in cls.shared_deps and v != cmdname]
  475. for a,b in cls.cmd_group:
  476. if is3seed:
  477. for n,(i,j) in enumerate(zip(cls.tmpdir_nums,(128,192,256))):
  478. k = '{}_{}'.format(a,n+1)
  479. if hasattr(cls,'skip_cmds') and k in cls.skip_cmds:
  480. continue
  481. sdeps = get_shared_deps(k,i)
  482. if type(b) == str:
  483. cdata.append( (k, (i,'{} ({}-bit)'.format(b,j),[[[]+sdeps,i]])) )
  484. else:
  485. cdata.append( (k, (i,'{} ({}-bit)'.format(b[1],j),[[b[0]+sdeps,i]])) )
  486. else:
  487. cdata.append( (a, b if full_data else (cls.tmpdir_nums[0],b,[[[],cls.tmpdir_nums[0]]])) )
  488. if add_dpy:
  489. self.dpy_data.update(dict(cdata))
  490. else:
  491. self.cmd_list = tuple(e[0] for e in cdata)
  492. self.dpy_data = dict(cdata)
  493. return cls
  494. def gm_init_group(self,trunner,gname,spawn_prog):
  495. kwargs = self.cmd_groups[gname][1]
  496. cls = self.create_group(gname,**kwargs)
  497. cls.group_name = gname
  498. return cls(trunner,cfgs,spawn_prog)
  499. def list_cmd_groups(self):
  500. ginfo = []
  501. for gname in self.cmd_groups:
  502. clsname,kwargs = self.cmd_groups[gname]
  503. cls = self.load_mod(gname,kwargs['modname'] if 'modname' in kwargs else None)
  504. ginfo.append((gname,cls))
  505. if opt.list_current_cmd_groups:
  506. exclude = (opt.exclude_groups or '').split(',')
  507. ginfo = [g for g in ginfo
  508. if network_id in g[1].networks
  509. and not g[0] in exclude
  510. and g[0] in self.cmd_groups_dfl + tuple(usr_args) ]
  511. for name,cls in ginfo:
  512. msg('{:17} - {}'.format(name,cls.__doc__))
  513. Die(0,'\n'+' '.join(e[0] for e in ginfo))
  514. def find_cmd_in_groups(self,cmd,group=None):
  515. """
  516. Search for a test command in specified group or all configured command groups
  517. and return it as a string. Loads modules but alters no global variables.
  518. """
  519. if group:
  520. if not group in [e[0] for e in self.cmd_groups]:
  521. die(1,'{!r}: unrecognized group'.format(group))
  522. groups = [self.cmd_groups[group]]
  523. else:
  524. groups = self.cmd_groups
  525. for gname in groups:
  526. clsname,kwargs = self.cmd_groups[gname]
  527. cls = self.load_mod(gname,kwargs['modname'] if 'modname' in kwargs else None)
  528. if cmd in cls.cmd_group: # first search the class
  529. return gname
  530. if cmd in dir(cls(None,None,None)): # then a throwaway instance
  531. return gname # cmd might exist in more than one group - we'll go with the first
  532. return None
  533. class TestSuiteRunner(object):
  534. 'test suite runner'
  535. def __init__(self,data_dir,trash_dir):
  536. self.data_dir = data_dir
  537. self.trash_dir = trash_dir
  538. self.cmd_total = 0
  539. self.rebuild_list = {}
  540. self.gm = CmdGroupMgr()
  541. self.repo_root = repo_root
  542. self.skipped_warnings = []
  543. if opt.log:
  544. self.log_fd = open(log_file,'a')
  545. self.log_fd.write('\nLog started: {} UTC\n'.format(make_timestr()))
  546. omsg('INFO → Logging to file {!r}'.format(log_file))
  547. else:
  548. self.log_fd = None
  549. if opt.coverage:
  550. self.coverdir,self.accfile = init_coverage()
  551. omsg('INFO → Writing coverage files to {!r}'.format(self.coverdir))
  552. def spawn_wrapper( self, cmd,
  553. args = [],
  554. extra_desc = '',
  555. no_output = False,
  556. msg_only = False,
  557. no_msg = False,
  558. cmd_dir = 'cmds' ):
  559. desc = self.ts.test_name if opt.names else self.gm.dpy_data[self.ts.test_name][1]
  560. if extra_desc: desc += ' ' + extra_desc
  561. if not opt.system:
  562. cmd = os.path.relpath(os.path.join(repo_root,cmd_dir,cmd))
  563. elif g.platform == 'win':
  564. cmd = os.path.join('/mingw64','opt','bin',cmd)
  565. passthru_opts = ['--{}{}'.format(k.replace('_','-'),
  566. '=' + getattr(opt,k) if getattr(opt,k) != True else '')
  567. for k in self.ts.base_passthru_opts + self.ts.passthru_opts if getattr(opt,k)]
  568. args = [cmd] + passthru_opts + self.ts.extra_spawn_args + args
  569. if opt.traceback:
  570. args = ['scripts/traceback_run.py'] + args
  571. if g.platform == 'win':
  572. args = ['python3'] + args
  573. for i in args:
  574. if type(i) != str:
  575. m = 'Error: missing input files in cmd line?:\nName: {}\nCmdline: {!r}'
  576. die(2,m.format(self.ts.test_name,args))
  577. if opt.coverage:
  578. args = ['python3','-m','trace','--count','--coverdir='+self.coverdir,'--file='+self.accfile] + args
  579. qargs = ['{q}{}{q}'.format(a,q=('',"'")[' ' in a]) for a in args]
  580. cmd_disp = ' '.join(qargs).replace('\\','/') # for mingw
  581. if not no_msg:
  582. if opt.verbose or opt.print_cmdline or opt.exact_output:
  583. clr1,clr2 = ((green,cyan),(nocolor,nocolor))[bool(opt.print_cmdline)]
  584. omsg(green('Testing: {}'.format(desc)))
  585. if not msg_only:
  586. s = repr(cmd_disp) if g.platform == 'win' else cmd_disp
  587. omsg(clr1('Executing: ') + clr2(s))
  588. else:
  589. omsg_r('Testing {}: '.format(desc))
  590. if msg_only:
  591. return
  592. if opt.log:
  593. self.log_fd.write('[{}][{}:{}] {}\n'.format(
  594. g.coin.lower(),
  595. self.ts.group_name,
  596. self.ts.test_name,
  597. cmd_disp))
  598. from test.include.pexpect import MMGenPexpect
  599. return MMGenPexpect(args,no_output=no_output)
  600. def end_msg(self):
  601. t = int(time.time()) - self.start_time
  602. m = '{} test{} performed. Elapsed time: {:02d}:{:02d}\n'
  603. sys.stderr.write(green(m.format(self.cmd_total,suf(self.cmd_total),t//60,t%60)))
  604. def init_group(self,gname,cmd=None,quiet=False):
  605. ts_cls = CmdGroupMgr().load_mod(gname)
  606. for k in ('segwit','segwit_random','bech32'):
  607. if getattr(opt,k):
  608. segwit_opt = k
  609. break
  610. else:
  611. segwit_opt = None
  612. m1 = ('test group {g!r}','{g}:{c}')[bool(cmd)].format(g=gname,c=cmd)
  613. m2 = ' for {} {}net'.format(g.coin.lower(),'test' if g.testnet else 'main') \
  614. if len(ts_cls.networks) != 1 else ''
  615. m3 = ' (--{})'.format(segwit_opt.replace('_','-')) if segwit_opt else ''
  616. m = m1 + m2 + m3
  617. if segwit_opt and not ts_cls.segwit_opts_ok:
  618. iqmsg('INFO → skipping ' + m)
  619. return False
  620. # 'networks = ()' means all networks allowed
  621. nws = [(e.split('_')[0],'testnet') if '_' in e else (e,'mainnet') for e in ts_cls.networks]
  622. if nws:
  623. coin = g.coin.lower()
  624. nw = ('mainnet','testnet')[g.testnet]
  625. for a,b in nws:
  626. if a == coin and b == nw:
  627. break
  628. else:
  629. iqmsg('INFO → skipping ' + m)
  630. return False
  631. if not quiet:
  632. bmsg('Executing ' + m)
  633. self.ts = self.gm.gm_init_group(self,gname,self.spawn_wrapper)
  634. if opt.resume_after:
  635. global resume
  636. resume = self.gm.cmd_list[self.gm.cmd_list.index(resume)+1]
  637. omsg(f'INFO → Resuming at command {resume!r}')
  638. if opt.exit_after and opt.exit_after not in self.gm.cmd_list:
  639. die(1,'{!r}: command not recognized'.format(opt.exit_after))
  640. return True
  641. def run_tests(self,usr_args):
  642. self.start_time = int(time.time())
  643. gname_save = None
  644. if usr_args:
  645. for arg in usr_args:
  646. if arg in self.gm.cmd_groups:
  647. if not self.init_group(arg):
  648. continue
  649. clean(self.ts.tmpdir_nums)
  650. for cmd in self.gm.cmd_list:
  651. self.check_needs_rerun(cmd,build=True)
  652. do_between()
  653. elif arg in utils:
  654. params = usr_args[usr_args.index(arg)+1:]
  655. globals()[arg](*params)
  656. sys.exit(0)
  657. else:
  658. if ':' in arg:
  659. gname,arg = arg.split(':')
  660. else:
  661. gname = self.gm.find_cmd_in_groups(arg)
  662. if gname:
  663. same_grp = gname == gname_save # same group as previous cmd: don't clean, suppress blue msg
  664. if not self.init_group(gname,arg,quiet=same_grp):
  665. continue
  666. if not same_grp:
  667. clean(self.ts.tmpdir_nums)
  668. self.check_needs_rerun(arg,build=True)
  669. do_between()
  670. gname_save = gname
  671. else:
  672. die(1,'{!r}: command not recognized'.format(arg))
  673. else:
  674. if opt.exclude_groups:
  675. exclude = opt.exclude_groups.split(',')
  676. for e in exclude:
  677. if e not in self.gm.cmd_groups_dfl:
  678. die(1,'{!r}: group not recognized'.format(e))
  679. for gname in self.gm.cmd_groups_dfl:
  680. if opt.exclude_groups and gname in exclude:
  681. continue
  682. if not self.init_group(gname):
  683. continue
  684. clean(self.ts.tmpdir_nums)
  685. for cmd in self.gm.cmd_list:
  686. self.check_needs_rerun(cmd,build=True)
  687. do_between()
  688. self.end_msg()
  689. def check_needs_rerun(self,
  690. cmd,
  691. build=False,
  692. root=True,
  693. force_delete=False,
  694. dpy=False
  695. ):
  696. rerun = root # force_delete is not passed to recursive call
  697. fns = []
  698. if force_delete or not root:
  699. # does cmd produce a needed dependency(ies)?
  700. ret = self.get_num_exts_for_cmd(cmd,dpy)
  701. if ret:
  702. for ext in ret[1]:
  703. fn = get_file_with_ext(cfgs[ret[0]]['tmpdir'],ext,delete=build)
  704. if fn:
  705. if force_delete: os.unlink(fn)
  706. else: fns.append(fn)
  707. else: rerun = True
  708. fdeps = self.generate_file_deps(cmd)
  709. cdeps = self.generate_cmd_deps(fdeps)
  710. for fn in fns:
  711. my_age = os.stat(fn).st_mtime
  712. for num,ext in fdeps:
  713. f = get_file_with_ext(cfgs[num]['tmpdir'],ext,delete=build)
  714. if f and os.stat(f).st_mtime > my_age:
  715. rerun = True
  716. for cdep in cdeps:
  717. if self.check_needs_rerun(cdep,build=build,root=False,dpy=cmd):
  718. rerun = True
  719. if build:
  720. if rerun:
  721. for fn in fns:
  722. if not root: os.unlink(fn)
  723. if not (dpy and opt.skip_deps):
  724. self.run_test(cmd)
  725. if not root: 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. d = [(str(num),ext) for exts,num in self.gm.dpy_data[cmd][2] for ext in exts]
  733. # delete files depended on by this cmd
  734. arg_list = [get_file_with_ext(cfgs[num]['tmpdir'],ext) for num,ext in d]
  735. # remove shared_deps from arg list
  736. if hasattr(self.ts,'shared_deps'):
  737. arg_list = arg_list[:-len(self.ts.shared_deps)]
  738. global resume
  739. if resume:
  740. if cmd != resume:
  741. return
  742. bmsg('Resuming at {!r}'.format(cmd))
  743. resume = False
  744. opt.skip_deps = False
  745. if opt.profile: start = time.time()
  746. cdata = self.gm.dpy_data[cmd]
  747. self.ts.test_name = cmd
  748. # self.ts.test_dpydata = cdata
  749. self.ts.tmpdir_num = cdata[0]
  750. # self.ts.cfg = cfgs[str(cdata[0])] # will remove this eventually
  751. cfg = cfgs[str(cdata[0])]
  752. for k in ( 'seed_len', 'seed_id',
  753. 'wpasswd', 'kapasswd',
  754. 'segwit', 'hash_preset',
  755. 'bw_filename', 'bw_params', 'ref_bw_seed_id',
  756. 'addr_idx_list', 'pass_idx_list' ):
  757. if k in cfg:
  758. setattr(self.ts,k,cfg[k])
  759. ret = getattr(self.ts,cmd)(*arg_list) # run the test
  760. if type(ret).__name__ == 'coroutine':
  761. ret = run_session(ret)
  762. self.process_retval(cmd,ret)
  763. if opt.profile:
  764. omsg('\r\033[50C{:.4f}'.format(time.time() - start))
  765. if cmd == opt.exit_after:
  766. sys.exit(0)
  767. def warn_skipped(self):
  768. if self.skipped_warnings:
  769. print(yellow('The following tests were skipped and may require attention:'))
  770. r = '-' * 72 + '\n'
  771. print(r+('\n'+r).join(self.skipped_warnings))
  772. def process_retval(self,cmd,ret):
  773. if type(ret).__name__ == 'MMGenPexpect':
  774. ret.ok()
  775. self.cmd_total += 1
  776. elif ret == 'ok':
  777. ok()
  778. self.cmd_total += 1
  779. elif ret == 'skip':
  780. pass
  781. elif type(ret) == tuple and ret[0] == 'skip_warn':
  782. self.skipped_warnings.append(
  783. 'Test {!r} was skipped:\n {}'.format(cmd,'\n '.join(ret[1].split('\n'))))
  784. else:
  785. rdie(1,'{!r} returned {}'.format(cmd,ret))
  786. def check_deps(self,cmds): # TODO: broken
  787. if len(cmds) != 1:
  788. die(1,'Usage: {} check_deps <command>'.format(g.prog_name))
  789. cmd = cmds[0]
  790. if cmd not in self.gm.cmd_list:
  791. die(1,'{!r}: unrecognized command'.format(cmd))
  792. if not opt.quiet:
  793. omsg('Checking dependencies for {!r}'.format(cmd))
  794. self.check_needs_rerun(self.ts,cmd,build=False)
  795. w = max(map(len,self.rebuild_list)) + 1
  796. for cmd in self.rebuild_list:
  797. c = self.rebuild_list[cmd]
  798. m = 'Rebuild' if (c[0] and c[1]) else 'Build' if c[0] else 'OK'
  799. omsg('cmd {:<{w}} {}'.format(cmd+':', m, w=w))
  800. def generate_file_deps(self,cmd):
  801. return [(str(n),e) for exts,n in self.gm.dpy_data[cmd][2] for e in exts]
  802. def generate_cmd_deps(self,fdeps):
  803. return [cfgs[str(n)]['dep_generators'][ext] for n,ext in fdeps]
  804. def get_num_exts_for_cmd(self,cmd,dpy=False): # dpy ignored here
  805. try:
  806. num = str(self.gm.dpy_data[cmd][0])
  807. except KeyError:
  808. qmsg_r('Missing dependency {!r}'.format(cmd))
  809. gname = self.gm.find_cmd_in_groups(cmd)
  810. if gname:
  811. kwargs = self.gm.cmd_groups[gname][1]
  812. kwargs.update({'add_dpy':True})
  813. self.gm.create_group(gname,**kwargs)
  814. num = str(self.gm.dpy_data[cmd][0])
  815. qmsg(' found in group {!r}'.format(gname))
  816. else:
  817. qmsg(' not found in any command group!')
  818. raise
  819. dgl = cfgs[num]['dep_generators']
  820. if cmd in dgl.values():
  821. exts = [k for k in dgl if dgl[k] == cmd]
  822. return (num,exts)
  823. else:
  824. return None
  825. # main()
  826. if not opt.skip_deps: # do this before list cmds exit, so we stay in sync with shm_dir
  827. create_tmp_dirs(shm_dir)
  828. if opt.list_cmd_groups:
  829. CmdGroupMgr().list_cmd_groups()
  830. elif opt.list_cmds:
  831. list_cmds()
  832. if opt.pause:
  833. set_restore_term_at_exit()
  834. set_environ_for_spawned_scripts()
  835. if network_id not in ('eth','etc'):
  836. start_test_daemons(network_id)
  837. try:
  838. tr = TestSuiteRunner(data_dir,trash_dir)
  839. tr.run_tests(usr_args)
  840. tr.warn_skipped()
  841. if network_id not in ('eth','etc'):
  842. stop_test_daemons(network_id)
  843. except KeyboardInterrupt:
  844. if network_id not in ('eth','etc'):
  845. stop_test_daemons(network_id)
  846. tr.warn_skipped()
  847. die(1,'\ntest.py exiting at user request')
  848. except TestSuiteException as e:
  849. ydie(1,e.args[0])
  850. except TestSuiteFatalException as e:
  851. rdie(1,e.args[0])
  852. except Exception:
  853. if opt.traceback:
  854. msg(blue('Spawned script exited with error'))
  855. else:
  856. import traceback
  857. print(''.join(traceback.format_exception(*sys.exc_info())))
  858. msg(blue('Test script exited with error'))
  859. raise
  860. except:
  861. raise