core.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # -*- coding: utf-8 -*-
  2. """
  3. plan.testsuite.core
  4. ~~~~~~~~~~~~~~~~~~~
  5. Tests the core classes for Plan.
  6. :copyright: (c) 2014 by Shipeng Feng.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import sys
  10. import unittest
  11. from plan.testsuite import BaseTestCase
  12. from plan.core import Plan
  13. from plan.exceptions import PlanError
  14. class PlanTestCase(BaseTestCase):
  15. def test_empty_cron_content(self):
  16. plan = Plan()
  17. desired_cron_content = """\
  18. # Begin Plan generated jobs for: main
  19. # End Plan generated jobs for: main
  20. """
  21. self.assert_equal(plan.cron_content, desired_cron_content)
  22. def test_cron_content(self):
  23. plan = Plan()
  24. plan.command('command', every='1.day')
  25. plan.script('script.py', every='1.day', path='/web/scripts',
  26. environment={'key': 'value'}, output='null')
  27. plan.module('calendar', every='1.day')
  28. desired_cron_content = """\
  29. # Begin Plan generated jobs for: main
  30. 0 0 * * * command
  31. 0 0 * * * cd /web/scripts && key=value %s script.py > /dev/null 2>&1
  32. 0 0 * * * %s -m calendar
  33. # End Plan generated jobs for: main
  34. """ % (sys.executable, sys.executable)
  35. self.assert_equal(plan.cron_content, desired_cron_content)
  36. def test_environment_variables(self):
  37. plan = Plan()
  38. plan.env('MAILTO', 'user@example.com')
  39. plan.command('command', every='1.day')
  40. desired_cron_content = """\
  41. # Begin Plan generated jobs for: main
  42. MAILTO="user@example.com"
  43. 0 0 * * * command
  44. # End Plan generated jobs for: main
  45. """
  46. self.assert_equal(plan.cron_content, desired_cron_content)
  47. def test_global_parameters(self):
  48. plan = Plan('test', path='/web/scripts',
  49. environment={'testkey': 'testvalue'},
  50. output=dict(stdout='/tmp/out.log'))
  51. plan.script('script.py', every='1.day')
  52. desired_cron_content = """\
  53. # Begin Plan generated jobs for: test
  54. 0 0 * * * cd /web/scripts && testkey=testvalue %s script.py >> /tmp/out.log 2>> /dev/null
  55. # End Plan generated jobs for: test
  56. """ % sys.executable
  57. self.assert_equal(plan.cron_content, desired_cron_content)
  58. plan = Plan('test', path='/web/global/scripts',
  59. environment={'globalkey': 'globalvalue'},
  60. output=dict(stdout='/tmp/global.log'))
  61. plan.script('script.py', every='1.day', path='/web/scripts',
  62. environment={'testkey': 'testvalue'},
  63. output=dict(stdout='/tmp/out.log'))
  64. desired_cron_content = """\
  65. # Begin Plan generated jobs for: test
  66. 0 0 * * * cd /web/scripts && testkey=testvalue %s script.py >> /tmp/out.log 2>> /dev/null
  67. # End Plan generated jobs for: test
  68. """ % sys.executable
  69. self.assert_equal(plan.cron_content, desired_cron_content)
  70. class CrontabTestCase(BaseTestCase):
  71. """TestCase for communicating with crontab process."""
  72. def setup(self):
  73. self.plan = Plan()
  74. self.original_crontab_content = self.plan.read_crontab()
  75. self.write_crontab('', '')
  76. def write_crontab(self, action, content):
  77. self.plan._write_to_crontab(action, content)
  78. def test_read_and_write_crontab(self):
  79. test_crontab_content = """\
  80. # TEST BEGIN
  81. * * * * * test
  82. # TEST END
  83. """
  84. self.assert_equal(self.plan.read_crontab(), '')
  85. self.write_crontab('', test_crontab_content)
  86. self.assert_equal(self.plan.read_crontab(), test_crontab_content)
  87. def test_update_crontab_error(self):
  88. test_crontab_content = """\
  89. # Begin Plan generated jobs for: main
  90. 0 12 * * * ls /tmp
  91. # End Plan generated jobs for:
  92. """
  93. self.write_crontab('', test_crontab_content)
  94. self.assert_raises(PlanError, self.plan.update_crontab, 'update')
  95. test_crontab_content = """\
  96. # Begin Plan generated jobs for:
  97. 0 12 * * * ls /tmp
  98. # End Plan generated jobs for: main
  99. """
  100. self.write_crontab('', test_crontab_content)
  101. self.assert_raises(PlanError, self.plan.update_crontab, 'update')
  102. def test_write_crontab_error(self):
  103. test_crontab_content = """\
  104. test
  105. """
  106. self.assert_raises(PlanError, self.write_crontab, '',
  107. test_crontab_content)
  108. def teardown(self):
  109. self.write_crontab('', self.original_crontab_content)
  110. self.plan = None
  111. def suite():
  112. suite = unittest.TestSuite()
  113. suite.addTest(unittest.makeSuite(PlanTestCase))
  114. suite.addTest(unittest.makeSuite(CrontabTestCase))
  115. return suite