2017-12-04 21:03:43 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Import Python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
import copy
|
|
|
|
import logging
|
|
|
|
import os
|
2017-12-07 05:44:47 +00:00
|
|
|
|
|
|
|
import dateutil.parser as dateutil_parser
|
2017-12-04 21:03:43 +00:00
|
|
|
|
|
|
|
# Import Salt Testing libs
|
|
|
|
from tests.support.case import ModuleCase
|
|
|
|
from tests.support.mixins import SaltReturnAssertsMixin
|
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-12-05 16:56:06 +00:00
|
|
|
from tests.support.mock import MagicMock, patch
|
2017-12-04 21:03:43 +00:00
|
|
|
import tests.integration as integration
|
|
|
|
|
|
|
|
# Import Salt libs
|
|
|
|
import salt.utils.schedule
|
|
|
|
|
|
|
|
from salt.modules.test import ping as ping
|
|
|
|
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
2017-12-06 21:09:35 +00:00
|
|
|
class SchedulerSkipTest(ModuleCase, SaltReturnAssertsMixin):
|
2017-12-04 21:03:43 +00:00
|
|
|
'''
|
|
|
|
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-04 21:03:43 +00:00
|
|
|
|
2018-02-07 19:35:30 +00:00
|
|
|
def tearDown(self):
|
|
|
|
del self.schedule
|
|
|
|
|
2017-12-04 21:03:43 +00:00
|
|
|
def test_skip(self):
|
|
|
|
'''
|
2017-12-04 22:35:26 +00:00
|
|
|
verify that scheduled job is skipped at the specified time
|
2017-12-04 21:03:43 +00:00
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'when': ['11/29/2017 4pm', '11/29/2017 5pm'],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add job to schedule
|
2018-02-20 04:40:24 +00:00
|
|
|
self.schedule.opts['schedule'] = {}
|
2017-12-04 21:03:43 +00:00
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 4:00pm')
|
2018-02-20 18:00:41 +00:00
|
|
|
self.schedule.skip_job('job1', {'time': run_time.strftime('%Y-%m-%dT%H:%M:%S'),
|
|
|
|
'time_fmt': '%Y-%m-%dT%H:%M:%S'})
|
2017-12-04 21:03:43 +00:00
|
|
|
|
|
|
|
# Run 11/29/2017 at 4pm
|
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertNotIn('_last_run', ret)
|
2018-02-07 19:35:30 +00:00
|
|
|
self.assertEqual(ret['_skip_reason'], 'skip_explicit')
|
|
|
|
self.assertEqual(ret['_skipped_time'], run_time)
|
2017-12-04 21:03:43 +00:00
|
|
|
|
|
|
|
# Run 11/29/2017 at 5pm
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 5:00pm')
|
2017-12-04 21:03:43 +00:00
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|
|
|
|
|
|
|
|
def test_skip_during_range(self):
|
|
|
|
'''
|
2017-12-04 22:35:26 +00:00
|
|
|
verify that scheduled job is skipped during the specified range
|
2017-12-04 21:03:43 +00:00
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'hours': '1',
|
|
|
|
'skip_during_range': {
|
2018-02-07 19:35:30 +00:00
|
|
|
'start': '11/29/2017 2pm',
|
|
|
|
'end': '11/29/2017 3pm'
|
2017-12-04 21:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add job to schedule
|
2018-02-20 04:40:24 +00:00
|
|
|
self.schedule.opts['schedule'] = {}
|
2017-12-04 21:03:43 +00:00
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
2018-02-07 19:35:30 +00:00
|
|
|
# eval at 1:30pm to prime.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 1:30pm')
|
2018-02-07 19:35:30 +00:00
|
|
|
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)
|
2018-02-07 19:35:30 +00:00
|
|
|
|
2017-12-04 21:03:43 +00:00
|
|
|
# eval at 2:30pm, will not run during range.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 2:30pm')
|
2017-12-04 21:03:43 +00:00
|
|
|
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-04 21:03:43 +00:00
|
|
|
self.assertNotIn('_last_run', ret)
|
2018-02-07 17:47:08 +00:00
|
|
|
self.assertEqual(ret['_skip_reason'], 'in_skip_range')
|
2018-02-07 19:35:30 +00:00
|
|
|
self.assertEqual(ret['_skipped_time'], run_time)
|
2017-12-04 21:03:43 +00:00
|
|
|
|
|
|
|
# eval at 3:30pm, will run.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 3:30pm')
|
2017-12-04 21:03:43 +00:00
|
|
|
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-04 21:03:43 +00:00
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|
2017-12-11 22:44:17 +00:00
|
|
|
|
|
|
|
def test_skip_during_range_global(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job is skipped during the specified range
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'skip_during_range': {
|
2018-02-07 19:35:30 +00:00
|
|
|
'start': '11/29/2017 2pm',
|
|
|
|
'end': '11/29/2017 3pm'
|
2017-12-11 22:44:17 +00:00
|
|
|
},
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'hours': '1',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add job to schedule
|
2018-02-20 04:40:24 +00:00
|
|
|
self.schedule.opts['schedule'] = {}
|
2017-12-11 22:44:17 +00:00
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
2018-02-07 19:35:30 +00:00
|
|
|
# eval at 1:30pm to prime.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 1:30pm')
|
2018-02-07 19:35:30 +00:00
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
|
2017-12-11 22:44:17 +00:00
|
|
|
# eval at 2:30pm, will not run during range.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 2:30pm')
|
2017-12-11 22:44:17 +00:00
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertNotIn('_last_run', ret)
|
2018-02-07 17:47:08 +00:00
|
|
|
self.assertEqual(ret['_skip_reason'], 'in_skip_range')
|
2018-02-07 19:35:30 +00:00
|
|
|
self.assertEqual(ret['_skipped_time'], run_time)
|
2017-12-11 22:44:17 +00:00
|
|
|
|
|
|
|
# eval at 3:30pm, will run.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 3:30pm')
|
2017-12-11 22:44:17 +00:00
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|
2017-12-12 21:26:47 +00:00
|
|
|
|
|
|
|
def test_run_after_skip_range(self):
|
|
|
|
'''
|
|
|
|
verify that scheduled job is skipped during the specified range
|
|
|
|
'''
|
|
|
|
job = {
|
|
|
|
'schedule': {
|
|
|
|
'job1': {
|
|
|
|
'function': 'test.ping',
|
|
|
|
'when': '11/29/2017 2:30pm',
|
|
|
|
'run_after_skip_range': True,
|
|
|
|
'skip_during_range': {
|
|
|
|
'start': '11/29/2017 2pm',
|
|
|
|
'end': '11/29/2017 3pm'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Add job to schedule
|
2018-02-20 04:40:24 +00:00
|
|
|
self.schedule.opts['schedule'] = {}
|
2017-12-12 21:26:47 +00:00
|
|
|
self.schedule.opts.update(job)
|
|
|
|
|
|
|
|
# eval at 2:30pm, will not run during range.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 2:30pm')
|
2017-12-12 21:26:47 +00:00
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertNotIn('_last_run', ret)
|
2018-02-07 17:47:08 +00:00
|
|
|
self.assertEqual(ret['_skip_reason'], 'in_skip_range')
|
2018-02-07 19:35:30 +00:00
|
|
|
self.assertEqual(ret['_skipped_time'], run_time)
|
2017-12-12 21:26:47 +00:00
|
|
|
|
|
|
|
# eval at 3:00:01pm, will run.
|
2018-02-16 21:20:48 +00:00
|
|
|
run_time = dateutil_parser.parse('11/29/2017 3:00:01pm')
|
2017-12-12 21:26:47 +00:00
|
|
|
self.schedule.eval(now=run_time)
|
|
|
|
ret = self.schedule.job_status('job1')
|
|
|
|
self.assertEqual(ret['_last_run'], run_time)
|