ut_daemon.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. """
  3. test/unit_tests_d/ut_daemon.py: unit test for the MMGen suite's Daemon class
  4. """
  5. from subprocess import run,DEVNULL
  6. from mmgen.common import *
  7. from mmgen.exception import *
  8. from mmgen.daemon import *
  9. from mmgen.protocol import init_proto
  10. def test_flags():
  11. d = CoinDaemon('eth')
  12. vmsg(f'Available opts: {fmt_list(d.avail_opts,fmt="bare")}')
  13. vmsg(f'Available flags: {fmt_list(d.avail_flags,fmt="bare")}')
  14. vals = namedtuple('vals',['online','no_daemonize','keep_cfg_file'])
  15. def gen():
  16. for opts,flags,val in (
  17. (None,None, vals(False,False,False)),
  18. (None,['keep_cfg_file'], vals(False,False,True)),
  19. (['online'],['keep_cfg_file'], vals(True,False,True)),
  20. (['online','no_daemonize'],['keep_cfg_file'], vals(True,True,True)),
  21. ):
  22. d = CoinDaemon('eth',opts=opts,flags=flags)
  23. assert d.flag.keep_cfg_file == val.keep_cfg_file
  24. assert d.opt.online == val.online
  25. assert d.opt.no_daemonize == val.no_daemonize
  26. d.flag.keep_cfg_file = not val.keep_cfg_file
  27. d.flag.keep_cfg_file = val.keep_cfg_file
  28. yield d
  29. return tuple(gen())
  30. def test_flags_err(ut,d):
  31. def bad1(): d[0].flag.foo = False
  32. def bad2(): d[0].opt.foo = False
  33. def bad3(): d[0].opt.no_daemonize = True
  34. def bad4(): d[0].flag.keep_cfg_file = 'x'
  35. def bad5(): d[0].opt.no_daemonize = 'x'
  36. def bad6(): d[0].flag.keep_cfg_file = False
  37. def bad7(): d[1].flag.keep_cfg_file = True
  38. ut.process_bad_data((
  39. ('flag (1)', 'ClassFlagsError', 'unrecognized flag', bad1 ),
  40. ('opt (1)', 'ClassFlagsError', 'unrecognized opt', bad2 ),
  41. ('opt (2)', 'AttributeError', 'is read-only', bad3 ),
  42. ('flag (2)', 'AssertionError', 'not boolean', bad4 ),
  43. ('opt (3)', 'AttributeError', 'is read-only', bad5 ),
  44. ('flag (3)', 'ClassFlagsError', 'not set', bad6 ),
  45. ('flag (4)', 'ClassFlagsError', 'already set', bad7 ),
  46. ))
  47. arm_skip_daemons = ('openethereum','parity')
  48. def test_cmds(op):
  49. network_ids = CoinDaemon.get_network_ids()
  50. for test_suite in [True,False] if op == 'print' else [True]:
  51. vmsg(orange(f'Start commands (op={op}, test_suite={test_suite}):'))
  52. for coin,data in CoinDaemon.coins.items():
  53. for daemon_id in data.daemon_ids:
  54. if daemon_id in arm_skip_daemons:
  55. continue
  56. for network in globals()[daemon_id+'_daemon'].networks:
  57. if opt.no_altcoin_deps and coin != 'BTC':
  58. continue
  59. d = CoinDaemon(
  60. proto=init_proto(coin=coin,network=network),
  61. daemon_id = daemon_id,
  62. test_suite = test_suite )
  63. if op == 'print':
  64. for cmd in d.start_cmds:
  65. vmsg(' '.join(cmd))
  66. elif op == 'check':
  67. try:
  68. cp = run([d.exec_fn,'--help'],stdout=PIPE,stderr=PIPE)
  69. except:
  70. ydie(1,f'Unable to execute {d.exec_fn}')
  71. if cp.returncode:
  72. ydie(1,f'Unable to execute {d.exec_fn}')
  73. else:
  74. vmsg('{:16} {}'.format(
  75. d.exec_fn+':',
  76. cp.stdout.decode().splitlines()[0] ))
  77. else:
  78. if opt.quiet:
  79. msg_r('.')
  80. if op == 'stop' and hasattr(d,'rpc'):
  81. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  82. else:
  83. getattr(d,op)(silent=opt.quiet)
  84. class unit_tests:
  85. win_skip = ('start','status','stop')
  86. def flags(self,name,ut):
  87. qmsg_r('Testing flags and opts...')
  88. vmsg('')
  89. daemons = test_flags()
  90. qmsg('OK')
  91. qmsg_r('Testing error handling for flags and opts...')
  92. vmsg('')
  93. test_flags_err(ut,daemons)
  94. qmsg('OK')
  95. return True
  96. def cmds(self,name,ut):
  97. qmsg_r('Testing start commands for coin daemons...')
  98. vmsg('')
  99. test_cmds('print')
  100. qmsg('OK')
  101. return True
  102. def exec(self,name,ut):
  103. qmsg_r('Testing availability of coin daemons...')
  104. vmsg('')
  105. test_cmds('check')
  106. qmsg('OK')
  107. return True
  108. def start(self,name,ut):
  109. msg_r('Starting coin daemons...')
  110. qmsg('')
  111. test_cmds('start')
  112. msg('OK')
  113. return True
  114. def status(self,name,ut):
  115. msg_r('Checking status of coin daemons...')
  116. qmsg('')
  117. test_cmds('start')
  118. msg('OK')
  119. return True
  120. def stop(self,name,ut):
  121. msg_r('Stopping coin daemons...')
  122. qmsg('')
  123. test_cmds('stop')
  124. msg('OK')
  125. return True