test_procmontap.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Tests for L{twisted.runner.procmontap}.
  5. """
  6. from twisted.python.usage import UsageError
  7. from twisted.trial import unittest
  8. from twisted.runner.procmon import ProcessMonitor
  9. from twisted.runner import procmontap as tap
  10. class ProcessMonitorTapTests(unittest.TestCase):
  11. """
  12. Tests for L{twisted.runner.procmontap}'s option parsing and makeService
  13. method.
  14. """
  15. def test_commandLineRequired(self):
  16. """
  17. The command line arguments must be provided.
  18. """
  19. opt = tap.Options()
  20. self.assertRaises(UsageError, opt.parseOptions, [])
  21. def test_threshold(self):
  22. """
  23. The threshold option is recognised as a parameter and coerced to
  24. float.
  25. """
  26. opt = tap.Options()
  27. opt.parseOptions(['--threshold', '7.5', 'foo'])
  28. self.assertEqual(opt['threshold'], 7.5)
  29. def test_killTime(self):
  30. """
  31. The killtime option is recognised as a parameter and coerced to float.
  32. """
  33. opt = tap.Options()
  34. opt.parseOptions(['--killtime', '7.5', 'foo'])
  35. self.assertEqual(opt['killtime'], 7.5)
  36. def test_minRestartDelay(self):
  37. """
  38. The minrestartdelay option is recognised as a parameter and coerced to
  39. float.
  40. """
  41. opt = tap.Options()
  42. opt.parseOptions(['--minrestartdelay', '7.5', 'foo'])
  43. self.assertEqual(opt['minrestartdelay'], 7.5)
  44. def test_maxRestartDelay(self):
  45. """
  46. The maxrestartdelay option is recognised as a parameter and coerced to
  47. float.
  48. """
  49. opt = tap.Options()
  50. opt.parseOptions(['--maxrestartdelay', '7.5', 'foo'])
  51. self.assertEqual(opt['maxrestartdelay'], 7.5)
  52. def test_parameterDefaults(self):
  53. """
  54. The parameters all have default values
  55. """
  56. opt = tap.Options()
  57. opt.parseOptions(['foo'])
  58. self.assertEqual(opt['threshold'], 1)
  59. self.assertEqual(opt['killtime'], 5)
  60. self.assertEqual(opt['minrestartdelay'], 1)
  61. self.assertEqual(opt['maxrestartdelay'], 3600)
  62. def test_makeService(self):
  63. """
  64. The command line gets added as a process to the ProcessMontor.
  65. """
  66. opt = tap.Options()
  67. opt.parseOptions(['ping', '-c', '3', '8.8.8.8'])
  68. s = tap.makeService(opt)
  69. self.assertIsInstance(s, ProcessMonitor)
  70. self.assertIn('ping -c 3 8.8.8.8', s.processes)