2017-12-06 21:09:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import copy
|
2018-02-16 21:20:48 +00:00
|
|
|
import datetime
|
2017-12-06 21:09:35 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2017-12-14 01:50:03 +00:00
|
|
|
import random
|
2017-12-07 05:44:47 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
import dateutil.parser as dateutil_parser
|
2017-12-06 21:09:35 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from tests.support.case import ModuleCase
|
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
|
|
|
from tests.support.mock import MagicMock, patch
|
2017-12-28 19:19:50 +00:00
|
|
|
from tests.support.unit import skipIf
|
2017-12-06 21:09:35 +00:00
|
|
|
import tests.integration as integration
|
|
|
|
|
|
|
|
# Import Salt libs
|
|
|
|
import salt.utils.schedule
|
|
|
|
|
|
|
|
from salt.modules.test import ping as ping
|
|
|
|
|
2018-02-16 22:12:04 +00:00
|
|
|
try:
|
|
|
|
import croniter # pylint: disable=W0611
|
|
|
|
HAS_CRONITER = True
|
|
|
|
except ImportError:
|
2018-02-17 00:32:19 +00:00
|
|
|
HAS_CRONITER = False
|
2017-12-28 19:19:50 +00:00
|
|
|
|
2017-12-06 21:09:35 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
ROOT_DIR = os.path.join(integration.TMP, 'schedule-unit-tests')
|
|
|
|
SOCK_DIR = os.path.join(ROOT_DIR, 'test-socks')
|
|
|
|
|
|
|
|
DEFAULT_CONFIG = salt.config.minion_config(None)
|
|
|
|
DEFAULT_CONFIG['conf_dir'] = ROOT_DIR
|
|
|
|
DEFAULT_CONFIG['root_dir'] = ROOT_DIR
|
|
|
|
DEFAULT_CONFIG['sock_dir'] = SOCK_DIR
|
|
|
|
DEFAULT_CONFIG['pki_dir'] = os.path.join(ROOT_DIR, 'pki')
|
|
|
|
DEFAULT_CONFIG['cachedir'] = os.path.join(ROOT_DIR, 'cache')
|
|
|
|
|
|
|
|
|
|
|
|
class SchedulerEvalTest(ModuleCase, SaltReturnAssertsMixin):
|
|
|
|
'''
|
|
|
|
Validate the pkg module
|
|
|
|
'''
|
|
|
|
def setUp(self):
|
|
|
|
with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
|
|
|
|
functions = {'test.ping': ping}
|
|
|
|
self.schedule = salt.utils.schedule.Schedule(copy.deepcopy(DEFAULT_CONFIG), functions, returners={})
|
2017-12-14 23:22:50 +00:00
|
|
|
self.schedule.opts['loop_interval'] = 1
|
2017-12-06 21:09:35 +00:00
|
|
|
|
2018-02-07 19:35:30 +00:00
|
|
|
def tearDown(self):
|
|
|
|
del self.schedule
|
|
|
|
|
2017-12-06 21:09:35 +00:00
|
|
|
def test_eval(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
2017-12-07 05:44:47 +00:00
|
|
|
'when': '11/29/2017 4:00pm',
|
2017-12-06 21:09:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time2 = dateutil_parser.parse('11/29/2017 4:00pm')
|
|
|
|
run_time1 = run_time2 - datetime.timedelta(seconds=1)
|
2017-12-06 21:09:35 +00:00
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# Evaluate 1 second before the run time
|
|
|
|
self.schedule.eval(now=run_time1)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertNotIn('_last_run', ret)
|
|
|
|
|
|
|
|
# Evaluate 1 second at the run time
|
|
|
|
self.schedule.eval(now=run_time2)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time2)
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
def test_eval_multiple_whens(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'when': [
|
|
|
|
'11/29/2017 4:00pm',
|
|
|
|
'11/29/2017 5:00pm',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
|
|
|
|
run_time2 = dateutil_parser.parse('11/29/2017 5:00pm')
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# Evaluate run time1
|
|
|
|
self.schedule.eval(now=run_time1)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time1)
|
|
|
|
|
|
|
|
# Evaluate run time2
|
|
|
|
self.schedule.eval(now=run_time2)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time2)
|
|
|
|
|
|
|
|
def test_eval_loop_interval(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'when': '11/29/2017 4:00pm',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# 30 second loop interval
|
2017-12-15 16:52:36 +00:00
|
|
|
LOOP_INTERVAL = random.randint(30, 59)
|
2017-12-14 01:50:03 +00:00
|
|
|
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
|
|
|
|
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time2 = dateutil_parser.parse('11/29/2017 4:00pm')
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# Evaluate 1 second at the run time
|
2018-02-16 21:20:48 +00:00
|
|
|
self.schedule.eval(now=run_time2 + datetime.timedelta(seconds=LOOP_INTERVAL))
|
2017-12-14 23:22:50 +00:00
|
|
|
|
2017-12-14 01:50:03 +00:00
|
|
|
ret = self.schedule.job_status('job1')
|
2018-02-16 21:20:48 +00:00
|
|
|
self.assertEqual(ret['_last_run'], run_time2 + datetime.timedelta(seconds=LOOP_INTERVAL))
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
def test_eval_multiple_whens_loop_interval(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'when': [
|
|
|
|
'11/29/2017 4:00pm',
|
|
|
|
'11/29/2017 5:00pm',
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# 30 second loop interval
|
2017-12-15 16:52:36 +00:00
|
|
|
LOOP_INTERVAL = random.randint(30, 59)
|
2017-12-14 01:50:03 +00:00
|
|
|
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
|
|
|
|
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time1 = dateutil_parser.parse('11/29/2017 4:00pm') + datetime.timedelta(seconds=LOOP_INTERVAL)
|
|
|
|
run_time2 = dateutil_parser.parse('11/29/2017 5:00pm') + datetime.timedelta(seconds=LOOP_INTERVAL)
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# Evaluate 1 second at the run time
|
2018-02-16 21:20:48 +00:00
|
|
|
self.schedule.eval(now=run_time1)
|
2017-12-14 01:50:03 +00:00
|
|
|
ret = self.schedule.job_status('job1')
|
2018-02-16 21:20:48 +00:00
|
|
|
self.assertEqual(ret['_last_run'], run_time1)
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
# Evaluate 1 second at the run time
|
2018-02-16 21:20:48 +00:00
|
|
|
self.schedule.eval(now=run_time2)
|
2017-12-14 01:50:03 +00:00
|
|
|
ret = self.schedule.job_status('job1')
|
2018-02-16 21:20:48 +00:00
|
|
|
self.assertEqual(ret['_last_run'], run_time2)
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
def test_eval_once(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'once': '2017-12-13T13:00:00',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('12/13/2017 1:00pm')
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# Evaluate 1 second at the run time
|
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
2018-02-16 21:20:48 +00:00
|
|
|
log.debug('=== ret %s ===', ret)
|
2017-12-14 01:50:03 +00:00
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|
|
|
|
|
|
|
|
def test_eval_once_loop_interval(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'once': '2017-12-13T13:00:00',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Randomn second loop interval
|
2017-12-15 16:52:36 +00:00
|
|
|
LOOP_INTERVAL = random.randint(0, 59)
|
2017-12-14 01:50:03 +00:00
|
|
|
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
|
|
|
|
|
|
|
|
# Run the job at the right plus LOOP_INTERVAL
|
2018-02-17 00:32:19 +00:00
|
|
|
run_time = dateutil_parser.parse('12/13/2017 1:00pm') + datetime.timedelta(seconds=LOOP_INTERVAL)
|
2017-12-14 01:50:03 +00:00
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# Evaluate at the run time
|
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|
2017-12-15 19:15:56 +00:00
|
|
|
|
2017-12-28 19:19:50 +00:00
|
|
|
@skipIf(not HAS_CRONITER, 'Cannot find croniter python module')
|
2017-12-15 19:15:56 +00:00
|
|
|
def test_eval_cron(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'cron': '0 16 29 11 *'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 4:00pm')
|
2017-12-15 19:15:56 +00:00
|
|
|
with patch('croniter.croniter.get_next', MagicMock(return_value=run_time)):
|
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|
|
|
|
|
2017-12-28 19:19:50 +00:00
|
|
|
@skipIf(not HAS_CRONITER, 'Cannot find croniter python module')
|
2017-12-15 19:15:56 +00:00
|
|
|
def test_eval_cron_loop_interval(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job runs
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'cron': '0 16 29 11 *'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# Randomn second loop interval
|
|
|
|
LOOP_INTERVAL = random.randint(0, 59)
|
|
|
|
self.schedule.opts['loop_interval'] = LOOP_INTERVAL
|
|
|
|
|
|
|
|
# Add the job to the scheduler
|
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 4:00pm')
|
2017-12-15 19:15:56 +00:00
|
|
|
with patch('croniter.croniter.get_next', MagicMock(return_value=run_time)):
|
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|