123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946 |
- # -*- coding: utf-8 -*-
- # Import Python libs
- from __future__ import absolute_import
- import copy
- import datetime
- import logging
- import os
- import random
- import dateutil.parser as dateutil_parser
- import datetime
- # 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
- from tests.support.unit import skipIf
- import tests.integration as integration
- # Import Salt libs
- import salt.utils.schedule
- import salt.utils.platform
- from salt.modules.test import ping as ping
- try:
- import croniter # pylint: disable=W0611
- HAS_CRONITER = True
- except ImportError:
- HAS_CRONITER = False
- 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')
- JOB_FUNCTION = 'test.ping'
- class SchedulerEvalTest(ModuleCase, SaltReturnAssertsMixin):
- '''
- Validate the scheduler
- '''
- 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={})
- self.schedule.opts['loop_interval'] = 1
- self.schedule.opts['run_schedule_jobs_in_background'] = False
- self.schedule.opts['grains']['whens'] = {'tea time': '11/29/2017 12:00pm'}
- def tearDown(self):
- self.schedule.reset()
- salt.utils.schedule.clean_proc_dir(self.schedule.opts)
- def test_eval(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- }
- }
- }
- run_time2 = dateutil_parser.parse('11/29/2017 4:00pm')
- run_time1 = run_time2 - datetime.timedelta(seconds=1)
- # 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(job_name)
- self.assertNotIn('_last_run', ret)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time2)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time2)
- def test_eval_multiple_whens(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_multiple_whens'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': [
- '11/29/2017 4:00pm',
- '11/29/2017 5:00pm',
- ],
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
- run_time2 = dateutil_parser.parse('11/29/2017 5:00pm')
- # 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(job_name)
- self.assertEqual(ret['_last_run'], run_time1)
- # Evaluate run time2
- self.schedule.eval(now=run_time2)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time2)
- def test_eval_whens(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_whens'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': 'tea time',
- }
- }
- }
- run_time = dateutil_parser.parse('11/29/2017 12:00pm')
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate run time1
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_loop_interval(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_loop_interval'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- }
- }
- }
- # 30 second loop interval
- LOOP_INTERVAL = random.randint(30, 59)
- self.schedule.opts['loop_interval'] = LOOP_INTERVAL
- run_time2 = dateutil_parser.parse('11/29/2017 4:00pm')
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time2 + datetime.timedelta(seconds=LOOP_INTERVAL))
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time2 + datetime.timedelta(seconds=LOOP_INTERVAL))
- def test_eval_multiple_whens_loop_interval(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_multiple_whens_loop_interval'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': [
- '11/29/2017 4:00pm',
- '11/29/2017 5:00pm',
- ],
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- # 30 second loop interval
- LOOP_INTERVAL = random.randint(30, 59)
- self.schedule.opts['loop_interval'] = LOOP_INTERVAL
- 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)
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time1)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time1)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time2)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time2)
- def test_eval_once(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_once'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'once': '2017-12-13T13:00:00',
- }
- }
- }
- run_time = dateutil_parser.parse('12/13/2017 1:00pm')
- # Add the job to the scheduler
- self.schedule.opts['schedule'] = {}
- self.schedule.opts.update(job)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_once_loop_interval(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_once_loop_interval'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'once': '2017-12-13T13:00:00',
- }
- }
- }
- # Randomn second loop interval
- LOOP_INTERVAL = random.randint(0, 59)
- self.schedule.opts['loop_interval'] = LOOP_INTERVAL
- # Run the job at the right plus LOOP_INTERVAL
- run_time = dateutil_parser.parse('12/13/2017 1:00pm') + datetime.timedelta(seconds=LOOP_INTERVAL)
- # 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(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- @skipIf(not HAS_CRONITER, 'Cannot find croniter python module')
- def test_eval_cron(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_cron'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'cron': '0 16 29 11 *',
- }
- }
- }
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- run_time = dateutil_parser.parse('11/29/2017 4:00pm')
- with patch('croniter.croniter.get_next', MagicMock(return_value=run_time)):
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- @skipIf(not HAS_CRONITER, 'Cannot find croniter python module')
- def test_eval_cron_loop_interval(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_cron_loop_interval'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- '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)
- run_time = dateutil_parser.parse('11/29/2017 4:00pm')
- with patch('croniter.croniter.get_next', MagicMock(return_value=run_time)):
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_until(self):
- '''
- verify that scheduled job is skipped once the current
- time reaches the specified until time
- '''
- job_name = 'test_eval_until'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'hours': '1',
- 'until': '11/29/2017 5:00pm',
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- # Add job to schedule
- self.schedule.delete_job('test_eval_until')
- self.schedule.opts.update(job)
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # eval at 3:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 3:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 4:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 4:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 5:00pm, will not run
- run_time = dateutil_parser.parse('11/29/2017 5:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_skip_reason'], 'until_passed')
- self.assertEqual(ret['_skipped_time'], run_time)
- def test_eval_after(self):
- '''
- verify that scheduled job is skipped until after the specified
- time has been reached.
- '''
- job_name = 'test_eval_after'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'hours': '1',
- 'after': '11/29/2017 5:00pm',
- }
- }
- }
- # Add job to schedule
- self.schedule.opts.update(job)
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # eval at 3:00pm, will not run.
- run_time = dateutil_parser.parse('11/29/2017 3:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_skip_reason'], 'after_not_passed')
- self.assertEqual(ret['_skipped_time'], run_time)
- # eval at 4:00pm, will not run.
- run_time = dateutil_parser.parse('11/29/2017 4:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_skip_reason'], 'after_not_passed')
- self.assertEqual(ret['_skipped_time'], run_time)
- # eval at 5:00pm, will not run
- run_time = dateutil_parser.parse('11/29/2017 5:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_skip_reason'], 'after_not_passed')
- self.assertEqual(ret['_skipped_time'], run_time)
- # eval at 6:00pm, will run
- run_time = dateutil_parser.parse('11/29/2017 6:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_enabled(self):
- '''
- verify that scheduled job does not run
- '''
- job_name = 'test_eval_enabled'
- job = {
- 'schedule': {
- 'enabled': True,
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- }
- }
- }
- run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time1)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time1)
- def test_eval_enabled_key(self):
- '''
- verify that scheduled job runs
- when the enabled key is in place
- https://github.com/saltstack/salt/issues/47695
- '''
- job_name = 'test_eval_enabled_key'
- job = {
- 'schedule': {
- 'enabled': True,
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- }
- }
- }
- run_time2 = dateutil_parser.parse('11/29/2017 4:00pm')
- run_time1 = run_time2 - datetime.timedelta(seconds=1)
- # 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('test_eval_enabled_key')
- self.assertNotIn('_last_run', ret)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time2)
- ret = self.schedule.job_status('test_eval_enabled_key')
- self.assertEqual(ret['_last_run'], run_time2)
- def test_eval_disabled(self):
- '''
- verify that scheduled job does not run
- '''
- job_name = 'test_eval_disabled'
- job = {
- 'schedule': {
- 'enabled': False,
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- }
- }
- }
- run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time1)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- self.assertEqual(ret['_skip_reason'], 'disabled')
- def test_eval_global_disabled_job_enabled(self):
- '''
- verify that scheduled job does not run
- '''
- job_name = 'test_eval_global_disabled'
- job = {
- 'schedule': {
- 'enabled': False,
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- 'enabled': True,
- }
- }
- }
- run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate 1 second at the run time
- self.schedule.eval(now=run_time1)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- self.assertEqual(ret['_skip_reason'], 'disabled')
- def test_eval_run_on_start(self):
- '''
- verify that scheduled job is run when minion starts
- '''
- job_name = 'test_eval_run_on_start'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'hours': '1',
- 'run_on_start': True,
- }
- }
- }
- # Add job to schedule
- self.schedule.opts.update(job)
- # eval at 2:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 3:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 3:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- def test_eval_splay(self):
- '''
- verify that scheduled job runs with splayed time
- '''
- job_name = 'job_eval_splay'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'seconds': '30',
- 'splay': '10',
- }
- }
- }
- # Add job to schedule
- self.schedule.opts.update(job)
- with patch('random.randint', MagicMock(return_value=10)):
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # eval at 2:00:40pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:40pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_splay_range(self):
- '''
- verify that scheduled job runs with splayed time
- '''
- job_name = 'job_eval_splay_range'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'seconds': '30',
- 'splay': {'start': 5, 'end': 10},
- }
- }
- }
- # Add job to schedule
- self.schedule.opts.update(job)
- with patch('random.randint', MagicMock(return_value=10)):
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # eval at 2:00:40pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:40pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_splay_global(self):
- '''
- verify that scheduled job runs with splayed time
- '''
- job_name = 'job_eval_splay_global'
- job = {
- 'schedule': {
- 'splay': {'start': 5, 'end': 10},
- job_name: {
- 'function': JOB_FUNCTION,
- 'seconds': '30',
- }
- }
- }
- # Add job to schedule
- self.schedule.opts.update(job)
- with patch('random.randint', MagicMock(return_value=10)):
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # eval at 2:00:40pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:40pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_seconds(self):
- '''
- verify that scheduled job run mutiple times with seconds
- '''
- job_name = 'job_eval_seconds'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'seconds': '30',
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- # Add job to schedule
- self.schedule.opts.update(job)
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- next_run_time = run_time + datetime.timedelta(seconds=30)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:00:01pm, will not run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:01pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:00:30pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:30pm')
- next_run_time = run_time + datetime.timedelta(seconds=30)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:01:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:01:00pm')
- next_run_time = run_time + datetime.timedelta(seconds=30)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:01:30pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:01:30pm')
- next_run_time = run_time + datetime.timedelta(seconds=30)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- def test_eval_minutes(self):
- '''
- verify that scheduled job run mutiple times with minutes
- '''
- job_name = 'job_eval_minutes'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'minutes': '30',
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- # Add job to schedule
- self.schedule.opts.update(job)
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- next_run_time = run_time + datetime.timedelta(minutes=30)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:00:01pm, will not run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:01pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:30:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:30:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 3:00:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 3:00:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 3:30:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 3:30:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_hours(self):
- '''
- verify that scheduled job run mutiple times with hours
- '''
- job_name = 'job_eval_hours'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'hours': '2',
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- # Add job to schedule
- self.schedule.opts.update(job)
- # eval at 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/29/2017 2:00pm')
- next_run_time = run_time + datetime.timedelta(hours=2)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 2:00:01pm, will not run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:01pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 4:00:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 4:00:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 6:00:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 6:00:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- # eval at 8:00:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 8:00:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- def test_eval_days(self):
- '''
- verify that scheduled job run mutiple times with days
- '''
- job_name = 'job_eval_days'
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'days': '2',
- 'dry_run': True
- }
- }
- }
- if salt.utils.platform.is_darwin():
- job['schedule'][job_name]['dry_run'] = True
- # Add job to schedule
- self.schedule.opts.update(job)
- # eval at 11/23/2017 2:00pm to prime, simulate minion start up.
- run_time = dateutil_parser.parse('11/23/2017 2:00pm')
- next_run_time = run_time + datetime.timedelta(days=2)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 11/25/2017 2:00:00pm, will run.
- run_time = dateutil_parser.parse('11/25/2017 2:00:00pm')
- next_run_time = run_time + datetime.timedelta(days=2)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 11/26/2017 2:00:00pm, will not run.
- run_time = dateutil_parser.parse('11/26/2017 2:00:00pm')
- last_run_time = run_time - datetime.timedelta(days=1)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], last_run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 11/27/2017 2:00:00pm, will run.
- run_time = dateutil_parser.parse('11/27/2017 2:00:00pm')
- next_run_time = run_time + datetime.timedelta(days=2)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 11/28/2017 2:00:00pm, will not run.
- run_time = dateutil_parser.parse('11/28/2017 2:00:00pm')
- last_run_time = run_time - datetime.timedelta(days=1)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], last_run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- # eval at 11/29/2017 2:00:00pm, will run.
- run_time = dateutil_parser.parse('11/29/2017 2:00:00pm')
- next_run_time = run_time + datetime.timedelta(days=2)
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time)
- self.assertEqual(ret['_next_fire_time'], next_run_time)
- def test_eval_when_splay(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_when_splay'
- splay = 300
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': '11/29/2017 4:00pm',
- 'splay': splay
- }
- }
- }
- run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
- run_time2 = run_time1 + datetime.timedelta(seconds=splay)
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- with patch('random.randint', MagicMock(return_value=splay)):
- # Evaluate to prime
- run_time = dateutil_parser.parse('11/29/2017 3:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # Evaluate at expected runtime1, should not run
- self.schedule.eval(now=run_time1)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- # Evaluate at expected runtime2, should run
- self.schedule.eval(now=run_time2)
- ret = self.schedule.job_status(job_name)
- self.assertEqual(ret['_last_run'], run_time2)
- def test_eval_when_splay_in_past(self):
- '''
- verify that scheduled job runs
- '''
- job_name = 'test_eval_when_splay_in_past'
- splay = 300
- job = {
- 'schedule': {
- job_name: {
- 'function': JOB_FUNCTION,
- 'when': ['11/29/2017 6:00am'],
- 'splay': splay
- }
- }
- }
- run_time1 = dateutil_parser.parse('11/29/2017 4:00pm')
- # Add the job to the scheduler
- self.schedule.opts.update(job)
- # Evaluate to prime
- run_time = dateutil_parser.parse('11/29/2017 3:00pm')
- self.schedule.eval(now=run_time)
- ret = self.schedule.job_status(job_name)
- # Evaluate at expected runtime1, should not run
- # and _next_fire_time should be None
- self.schedule.eval(now=run_time1)
- ret = self.schedule.job_status(job_name)
- self.assertNotIn('_last_run', ret)
- self.assertEqual(ret['_next_fire_time'], None)
|