ut_daemon.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. class unit_test(object):
  11. def run_test(self,name,ut):
  12. def test_flags():
  13. d = CoinDaemon('eth')
  14. vmsg(f'Available opts: {fmt_list(d.avail_opts,fmt="bare")}')
  15. vmsg(f'Available flags: {fmt_list(d.avail_flags,fmt="bare")}')
  16. vals = namedtuple('vals',['online','no_daemonize','keep_cfg_file'])
  17. def gen():
  18. for opts,flags,val in (
  19. (None,None, vals(False,False,False)),
  20. (None,['keep_cfg_file'], vals(False,False,True)),
  21. (['online'],['keep_cfg_file'], vals(True,False,True)),
  22. (['online','no_daemonize'],['keep_cfg_file'], vals(True,True,True)),
  23. ):
  24. d = CoinDaemon('eth',opts=opts,flags=flags)
  25. assert d.flag.keep_cfg_file == val.keep_cfg_file
  26. assert d.opt.online == val.online
  27. assert d.opt.no_daemonize == val.no_daemonize
  28. d.flag.keep_cfg_file = not val.keep_cfg_file
  29. d.flag.keep_cfg_file = val.keep_cfg_file
  30. yield d
  31. return tuple(gen())
  32. def test_flags_err(d):
  33. def bad1(): d[0].flag.foo = False
  34. def bad2(): d[0].opt.foo = False
  35. def bad3(): d[0].opt.no_daemonize = True
  36. def bad4(): d[0].flag.keep_cfg_file = 'x'
  37. def bad5(): d[0].opt.no_daemonize = 'x'
  38. def bad6(): d[0].flag.keep_cfg_file = False
  39. def bad7(): d[1].flag.keep_cfg_file = True
  40. ut.process_bad_data((
  41. ('flag (1)', 'ClassFlagsError', 'unrecognized flag', bad1 ),
  42. ('opt (1)', 'ClassFlagsError', 'unrecognized opt', bad2 ),
  43. ('opt (2)', 'AttributeError', 'is read-only', bad3 ),
  44. ('flag (2)', 'AssertionError', 'not boolean', bad4 ),
  45. ('opt (3)', 'AttributeError', 'is read-only', bad5 ),
  46. ('flag (3)', 'ClassFlagsError', 'not set', bad6 ),
  47. ('flag (4)', 'ClassFlagsError', 'already set', bad7 ),
  48. ))
  49. def test_cmds(op):
  50. network_ids = CoinDaemon.get_network_ids()
  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. for network in globals()[daemon_id+'_daemon'].networks:
  56. d = CoinDaemon(
  57. proto=init_proto(coin=coin,network=network),
  58. daemon_id = daemon_id,
  59. test_suite = test_suite )
  60. if op == 'print':
  61. for cmd in d.start_cmds:
  62. vmsg(' '.join(cmd))
  63. else:
  64. if run(['which',d.exec_fn],stdout=DEVNULL,stderr=DEVNULL).returncode:
  65. if op == 'start':
  66. qmsg(yellow(f'Warning: {d.exec_fn} not found in executable path'))
  67. else:
  68. if opt.quiet:
  69. msg_r('.')
  70. getattr(d,op)(silent=opt.quiet)
  71. qmsg_r('Testing flags and opts...')
  72. vmsg('')
  73. daemons = test_flags()
  74. qmsg('OK')
  75. qmsg_r('Testing error handling for flags and opts...')
  76. vmsg('')
  77. test_flags_err(daemons)
  78. qmsg('OK')
  79. qmsg_r('Testing start commands for configured daemons...')
  80. vmsg('')
  81. test_cmds('print')
  82. qmsg('OK')
  83. msg_r('Starting all configured daemons available on system...')
  84. qmsg('')
  85. test_cmds('start')
  86. msg('OK')
  87. msg_r('Stopping all configured daemons available on system...')
  88. qmsg('')
  89. test_cmds('stop')
  90. msg('OK')
  91. return True