1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
#!/usr/bin/env python
#
# Copyright (C) 2013 Martin Owens
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
#
"""
Test crontab ranges.
"""
import unittest
from crontab import CronTab, CronSlice, PY3
try:
from test import test_support
except ImportError:
from test import support as test_support
from .utils import LoggingMixin
if PY3:
unicode = str #pylint: disable=redefined-builtin
class RangeTestCase(LoggingMixin, unittest.TestCase):
"""Test basic functionality of crontab."""
log_name = 'crontab'
def setUp(self):
super(RangeTestCase, self).setUp()
self.crontab = CronTab(tab="")
def test_01_atevery(self):
"""At Every"""
tab = CronTab(tab="""
* * * * * command
61 * * * * command
* 25 * * * command
* * 32 * * command
* * * 13 * command
* * * * 8 command
""")
self.assertEqual(len(tab), 1)
self.assertLog("error", "'61', not in 0-59 for Minutes")
self.assertLog("error", "'25', not in 0-23 for Hours")
self.assertLog("error", "'32', not in 1-31 for Day of Month")
self.assertLog("error", "'13', not in 1-12 for Month")
self.assertLog("error", "'8', not in 0-6 for Day of Week")
def test_02_withinevery(self):
"""Within Every"""
tab = CronTab(tab="""
* * * * * command
1-61 * * * * command
* 1-25 * * * command
* * 1-32 * * command
* * * 1-13 * command
* * * * 1-8 command
""")
self.assertEqual(len(tab), 1)
self.assertLog("error", "'61', not in 0-59 for Minutes")
self.assertLog("error", "'25', not in 0-23 for Hours")
self.assertLog("error", "'32', not in 1-31 for Day of Month")
self.assertLog("error", "'13', not in 1-12 for Month")
self.assertLog("error", "'8', not in 0-6 for Day of Week")
def test_03_outevery(self):
"""Out of Every"""
tab = CronTab(tab="""
* * * * * command
*/61 * * * * command
* */25 * * * command
* * */32 * * command
* * * */13 * command
* * * * */8 command
""")
self.assertEqual(len(tab), 1)
self.assertLog("error", "'61', not in 0-59 for Minutes")
self.assertLog("error", "'25', not in 0-23 for Hours")
self.assertLog("error", "'32', not in 1-31 for Day of Month")
self.assertLog("error", "'13', not in 1-12 for Month")
self.assertLog("error", "'8', not in 0-6 for Day of Week")
def test_03_inevery(self):
"""Inside of Every"""
tab = CronTab(tab="""
* * * * * command
*/59 * * * * command
* */23 * * * command
* * */30 * * command
* * * */11 * command
* * * * */7 command
""")
self.assertEqual(len(tab), 6, str(tab))
def test_04_zero_seq(self):
"""Zero divisor in range"""
tab = CronTab(tab="""
*/0 * * * * command
""")
self.assertEqual(len(tab), 0)
self.assertLog("error", "Sequence can not be divided by zero or max")
def test_14_invalid_range(self):
"""No numerator in range"""
tab = CronTab(tab="/10 * * * * command")
self.assertEqual(len(tab), 0)
with self.assertRaises(ValueError):
tab.render(errors=True)
self.assertEqual(unicode(tab), "# DISABLED LINE\n# /10 * * * * command\n")
self.assertLog('error', u"No enumeration for Minutes: '/10'")
def test_05_sunday(self):
"""Test all possible day of week combinations"""
for (a, b) in (\
("7", "0"), ("5-7", "0,5-6"), ("1-7", "*"), ("*/7", "0"),\
("0-6", "*"), ("2-7", "0,2-6"), ("1-5", "1-5"), ("0-5", "0-5")):
v = str(CronSlice(4, a))
self.assertEqual(v, b, "%s != %s, from %s" % (v, b, a))
def test_06_backwards(self):
"""Test backwards ranges for error"""
tab = CronTab(tab="* * * * 3-1 command")
self.assertEqual(str(tab), "# DISABLED LINE\n# * * * * 3-1 command\n")
self.assertLog("error", "Bad range '3-1'")
if __name__ == '__main__':
test_support.run_unittest(RangeTestCase)
|