ut_daemon.py 2.9 KB

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