ut_daemon.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. """
  3. test.unit_tests_d.ut_daemon: unit test for the MMGen suite's Daemon class
  4. """
  5. from subprocess import run,PIPE
  6. from collections import namedtuple
  7. from mmgen.color import orange
  8. from mmgen.util import fmt_list
  9. from mmgen.daemon import CoinDaemon
  10. from ..include.common import cfg,qmsg,qmsg_r,vmsg
  11. def test_flags():
  12. d = CoinDaemon(cfg,'eth')
  13. vmsg(f'Available opts: {fmt_list(d.avail_opts,fmt="bare")}')
  14. vmsg(f'Available flags: {fmt_list(d.avail_flags,fmt="bare")}')
  15. vals = namedtuple('vals',['online','no_daemonize','keep_cfg_file'])
  16. def gen():
  17. for opts,flags,val in (
  18. (None,None, vals(False,False,False)),
  19. (None,['keep_cfg_file'], vals(False,False,True)),
  20. (['online'],['keep_cfg_file'], vals(True,False,True)),
  21. (['online','no_daemonize'],['keep_cfg_file'], vals(True,True,True)),
  22. ):
  23. d = CoinDaemon(cfg,'eth',opts=opts,flags=flags)
  24. assert d.flag.keep_cfg_file == val.keep_cfg_file
  25. assert d.opt.online == val.online
  26. assert d.opt.no_daemonize == val.no_daemonize
  27. d.flag.keep_cfg_file = not val.keep_cfg_file
  28. d.flag.keep_cfg_file = val.keep_cfg_file
  29. yield d
  30. return tuple(gen())
  31. def test_flags_err(ut,d):
  32. def bad1(): d[0].flag.foo = False
  33. def bad2(): d[0].opt.foo = False
  34. def bad3(): d[0].opt.no_daemonize = True
  35. def bad4(): d[0].flag.keep_cfg_file = 'x'
  36. def bad5(): d[0].opt.no_daemonize = 'x'
  37. def bad6(): d[0].flag.keep_cfg_file = False
  38. def bad7(): d[1].flag.keep_cfg_file = True
  39. ut.process_bad_data((
  40. ('flag (1)', 'ClassFlagsError', 'unrecognized flag', bad1 ),
  41. ('opt (1)', 'ClassFlagsError', 'unrecognized opt', bad2 ),
  42. ('opt (2)', 'AttributeError', 'is read-only', bad3 ),
  43. ('flag (2)', 'AssertionError', 'not boolean', bad4 ),
  44. ('opt (3)', 'AttributeError', 'is read-only', bad5 ),
  45. ('flag (3)', 'ClassFlagsError', 'not set', bad6 ),
  46. ('flag (4)', 'ClassFlagsError', 'already set', bad7 ),
  47. ))
  48. class unit_tests:
  49. win_skip = ('start','status','stop')
  50. def _pre(self):
  51. self.daemon_ctrl_args = ['btc','btc_tn','btc_rt'] if cfg.no_altcoin_deps else ['all']
  52. def _test_cmd(self,args_in,message):
  53. qmsg_r(message)
  54. args = ['python3', f'test/{args_in[0]}-coin-daemons.py'] + list(args_in[1:]) + self.daemon_ctrl_args
  55. vmsg('\n' + orange(f"Running '{' '.join(args)}':"))
  56. pipe = None if cfg.verbose else PIPE
  57. run( args, stdout=pipe, stderr=pipe, check=True )
  58. qmsg('OK')
  59. return True
  60. def flags(self,name,ut):
  61. qmsg_r('Testing flags and opts...')
  62. vmsg('')
  63. daemons = test_flags()
  64. qmsg('OK')
  65. qmsg_r('Testing error handling for flags and opts...')
  66. vmsg('')
  67. test_flags_err(ut,daemons)
  68. qmsg('OK')
  69. return True
  70. def exec(self,name,ut):
  71. return self._test_cmd(['start','-Vm'], 'Testing availability of coin daemons...')
  72. def cmds(self,name,ut):
  73. return self._test_cmd(['start','-t'], 'Testing start commands for coin daemons...')
  74. def start(self,name,ut):
  75. return self._test_cmd(['start'], 'Starting coin daemons...')
  76. def status(self,name,ut):
  77. return self._test_cmd(['start'], 'Checking status of coin daemons...')
  78. def stop(self,name,ut):
  79. return self._test_cmd(['stop'], 'Stopping coin daemons...')