ut_daemon.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. import mmgen.daemon as daemon_mod
  51. for test_suite in [True,False] if op == 'print' else [True]:
  52. vmsg(orange(f'Start commands (op={op}, test_suite={test_suite}):'))
  53. for coin,data in CoinDaemon.coins.items():
  54. for daemon_id in data.daemon_ids:
  55. if daemon_id in arm_skip_daemons:
  56. continue
  57. for network in getattr( daemon_mod, daemon_id+'_daemon' ).networks:
  58. if opt.no_altcoin_deps and coin != 'BTC':
  59. continue
  60. d = CoinDaemon(
  61. proto=init_proto(coin=coin,network=network),
  62. daemon_id = daemon_id,
  63. test_suite = test_suite )
  64. if op == 'print':
  65. for cmd in d.start_cmds:
  66. vmsg(' '.join(cmd))
  67. elif op == 'check':
  68. try:
  69. cp = run([d.exec_fn,'--help'],stdout=PIPE,stderr=PIPE)
  70. except:
  71. die(2,f'Unable to execute {d.exec_fn}')
  72. if cp.returncode:
  73. die(2,f'Unable to execute {d.exec_fn}')
  74. else:
  75. vmsg('{:16} {}'.format(
  76. d.exec_fn+':',
  77. cp.stdout.decode().splitlines()[0] ))
  78. else:
  79. if opt.quiet:
  80. msg_r('.')
  81. if op == 'stop' and hasattr(d,'rpc'):
  82. run_session(d.rpc.stop_daemon(quiet=opt.quiet))
  83. else:
  84. getattr(d,op)(silent=opt.quiet)
  85. class unit_tests:
  86. win_skip = ('start','status','stop')
  87. def flags(self,name,ut):
  88. qmsg_r('Testing flags and opts...')
  89. vmsg('')
  90. daemons = test_flags()
  91. qmsg('OK')
  92. qmsg_r('Testing error handling for flags and opts...')
  93. vmsg('')
  94. test_flags_err(ut,daemons)
  95. qmsg('OK')
  96. return True
  97. def cmds(self,name,ut):
  98. qmsg_r('Testing start commands for coin daemons...')
  99. vmsg('')
  100. test_cmds('print')
  101. qmsg('OK')
  102. return True
  103. def exec(self,name,ut):
  104. qmsg_r('Testing availability of coin daemons...')
  105. vmsg('')
  106. test_cmds('check')
  107. qmsg('OK')
  108. return True
  109. def start(self,name,ut):
  110. msg_r('Starting coin daemons...')
  111. qmsg('')
  112. test_cmds('start')
  113. msg('OK')
  114. return True
  115. def status(self,name,ut):
  116. msg_r('Checking status of coin daemons...')
  117. qmsg('')
  118. test_cmds('start')
  119. msg('OK')
  120. return True
  121. def stop(self,name,ut):
  122. msg_r('Stopping coin daemons...')
  123. qmsg('')
  124. test_cmds('stop')
  125. msg('OK')
  126. return True