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
|
#!/usr/bin/env python
#
# Copyright (C) 2016 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 any internal scheduling
"""
import os
import sys
import unittest
import crontab
import logging
import string
import random
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
logger = logging.getLogger()
logger.level = logging.WARNING
try:
from test import test_support
except ImportError:
from test import support as test_support
TEST_DIR = os.path.dirname(__file__)
COMMAND = sys.executable + ' ' + os.path.join(TEST_DIR, 'data', 'crontest ')
class SchedulerTestCase(unittest.TestCase):
"""Test scheduling functions of CronTab."""
def setUp(self):
self.tab = crontab.CronTab()
try:
import croniter
except ImportError:
self.skipTest("Croniter not installed")
self.handlers = []
self.log = crontab.LOG
self.log.setLevel(logging.DEBUG)
self.log.propagate = False
for handler in self.log.handlers:
self.handlers.append(handler)
self.log.removeHandler(handler)
self.err = StringIO()
self.handler = logging.StreamHandler(self.err)
self.log.addHandler(self.handler)
def tearDown(self):
logger.removeHandler(self.handler)
for handler in self.handlers:
logger.addHandler(handler)
#self.err.close()
def assertLog(self, text):
self.handler.flush()
self.assertEqual(self.err.getvalue().strip(), text)
def assertSchedule(self, slices, count, result):
uid = random.choice(string.ascii_letters)
self.tab.new(command=COMMAND + '-h ' + uid).setall(slices)
ret = list(self.tab.run_scheduler(count, cadence=0.01, warp=True))
self.assertEqual(len(ret), result)
if count > 0:
self.assertEqual(ret[0], '-h|' + uid)
def test_01_run(self):
"""Run the command"""
self.tab.env['SHELL'] = crontab.SHELL
ret = self.tab.new(command=COMMAND+'-h A').run()
self.assertEqual(ret, '-h|A')
def test_02_run_error(self):
"""Run with errors"""
ret = self.tab.new(command=COMMAND+'-e B').run()
self.assertEqual(ret, '')
self.assertLog('-e|B')
def test_03_schedule(self):
"""Simple Schedule"""
self.assertSchedule("* * * * *", 5, 4)
def test_04_schedule_ten(self):
"""Every Ten Minutes"""
self.assertSchedule("*/10 * * * *", 12, 1)
def test_05_env_passed(self):
"""Environment is passed in"""
self.tab.env['CR_VAR'] = 'BABARIAN'
ret = self.tab.new(command=COMMAND+' -ev').run()
self.assertEqual(ret, 'BABARIAN')
if __name__ == '__main__':
test_support.run_unittest(SchedulerTestCase)
|