2014-07-03 21:48:46 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
'''
|
2018-05-28 21:13:12 +00:00
|
|
|
:codeauthor: Nicole Thomas <nicole@saltstack.com>
|
2014-07-03 21:48:46 +00:00
|
|
|
'''
|
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import python libs
|
2018-01-13 04:39:03 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
2016-03-16 12:15:37 +00:00
|
|
|
import copy
|
2018-02-18 18:33:48 +00:00
|
|
|
import datetime
|
2017-11-29 08:47:19 +00:00
|
|
|
import os
|
2014-07-03 21:48:46 +00:00
|
|
|
|
|
|
|
# Import Salt Testing Libs
|
2017-02-27 13:58:07 +00:00
|
|
|
from tests.support.unit import skipIf, TestCase
|
|
|
|
from tests.support.mock import MagicMock, patch, NO_MOCK, NO_MOCK_REASON
|
2017-02-27 15:59:04 +00:00
|
|
|
import tests.integration as integration
|
2015-04-23 04:30:58 +00:00
|
|
|
|
2016-03-16 12:15:37 +00:00
|
|
|
# Import Salt Libs
|
|
|
|
import salt.config
|
|
|
|
from salt.utils.schedule import Schedule
|
|
|
|
|
2017-11-29 08:47:19 +00:00
|
|
|
# pylint: disable=import-error,unused-import
|
|
|
|
try:
|
|
|
|
import croniter
|
|
|
|
_CRON_SUPPORTED = True
|
|
|
|
except ImportError:
|
|
|
|
_CRON_SUPPORTED = False
|
|
|
|
# pylint: enable=import-error
|
|
|
|
|
|
|
|
|
2016-03-16 12:15:37 +00:00
|
|
|
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')
|
2014-07-03 21:48:46 +00:00
|
|
|
|
|
|
|
|
2017-11-29 08:47:19 +00:00
|
|
|
# pylint: disable=too-many-public-methods,invalid-name
|
2014-08-07 22:59:17 +00:00
|
|
|
@skipIf(NO_MOCK, NO_MOCK_REASON)
|
2014-07-03 21:48:46 +00:00
|
|
|
class ScheduleTestCase(TestCase):
|
|
|
|
'''
|
|
|
|
Unit tests for salt.utils.schedule module
|
|
|
|
'''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
with patch('salt.utils.schedule.clean_proc_dir', MagicMock(return_value=None)):
|
2016-03-16 12:15:37 +00:00
|
|
|
self.schedule = Schedule(copy.deepcopy(DEFAULT_CONFIG), {}, returners={})
|
2014-07-03 21:48:46 +00:00
|
|
|
|
|
|
|
# delete_job tests
|
|
|
|
|
|
|
|
def test_delete_job_exists(self):
|
|
|
|
'''
|
|
|
|
Tests ensuring the job exists and deleting it
|
|
|
|
'''
|
2017-03-15 18:02:08 +00:00
|
|
|
self.schedule.opts.update({'schedule': {'foo': 'bar'}, 'pillar': {}})
|
2016-06-02 11:44:37 +00:00
|
|
|
self.assertIn('foo', self.schedule.opts['schedule'])
|
2014-07-03 21:48:46 +00:00
|
|
|
self.schedule.delete_job('foo')
|
2016-06-02 11:44:37 +00:00
|
|
|
self.assertNotIn('foo', self.schedule.opts['schedule'])
|
2014-07-03 21:48:46 +00:00
|
|
|
|
|
|
|
def test_delete_job_in_pillar(self):
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
Tests ignoring deletion job from pillar
|
2014-07-03 21:48:46 +00:00
|
|
|
'''
|
2017-03-15 18:02:08 +00:00
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {'foo': 'bar'}}, 'schedule': {}})
|
2016-06-02 11:44:37 +00:00
|
|
|
self.assertIn('foo', self.schedule.opts['pillar']['schedule'])
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.delete_job('foo')
|
2016-06-02 11:44:37 +00:00
|
|
|
self.assertIn('foo', self.schedule.opts['pillar']['schedule'])
|
2014-07-03 21:48:46 +00:00
|
|
|
|
|
|
|
def test_delete_job_intervals(self):
|
|
|
|
'''
|
|
|
|
Tests removing job from intervals
|
|
|
|
'''
|
2017-03-15 18:02:08 +00:00
|
|
|
self.schedule.opts.update({'pillar': {}, 'schedule': {}})
|
2014-07-03 21:48:46 +00:00
|
|
|
self.schedule.intervals = {'foo': 'bar'}
|
|
|
|
self.schedule.delete_job('foo')
|
|
|
|
self.assertNotIn('foo', self.schedule.intervals)
|
|
|
|
|
2016-06-02 11:44:37 +00:00
|
|
|
def test_delete_job_prefix(self):
|
|
|
|
'''
|
|
|
|
Tests ensuring jobs exists and deleting them by prefix
|
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'schedule': {'foobar': 'bar', 'foobaz': 'baz', 'fooboo': 'boo'},
|
2017-03-15 18:02:08 +00:00
|
|
|
'pillar': {}})
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
|
|
|
del ret['schedule']['foobar']
|
|
|
|
del ret['schedule']['foobaz']
|
|
|
|
self.schedule.delete_job_prefix('fooba')
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
|
|
|
def test_delete_job_prefix_in_pillar(self):
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
Tests ignoring deletion jobs by prefix from pillar
|
2016-06-02 11:44:37 +00:00
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {'foobar': 'bar', 'foobaz': 'baz', 'fooboo': 'boo'}},
|
2017-03-15 18:02:08 +00:00
|
|
|
'schedule': {}})
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.delete_job_prefix('fooba')
|
2016-06-02 11:44:37 +00:00
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
2014-07-03 21:48:46 +00:00
|
|
|
# add_job tests
|
|
|
|
|
|
|
|
def test_add_job_data_not_dict(self):
|
|
|
|
'''
|
|
|
|
Tests if data is a dictionary
|
|
|
|
'''
|
|
|
|
data = 'foo'
|
|
|
|
self.assertRaises(ValueError, Schedule.add_job, self.schedule, data)
|
|
|
|
|
|
|
|
def test_add_job_multiple_jobs(self):
|
|
|
|
'''
|
|
|
|
Tests if more than one job is scheduled at a time
|
|
|
|
'''
|
|
|
|
data = {'key1': 'value1', 'key2': 'value2'}
|
|
|
|
self.assertRaises(ValueError, Schedule.add_job, self.schedule, data)
|
|
|
|
|
2014-07-08 22:58:19 +00:00
|
|
|
def test_add_job(self):
|
|
|
|
'''
|
|
|
|
Tests adding a job to the schedule
|
|
|
|
'''
|
2015-08-13 17:53:37 +00:00
|
|
|
data = {'foo': {'bar': 'baz'}}
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2016-03-16 12:15:37 +00:00
|
|
|
ret.update({'schedule': {'foo': {'bar': 'baz', 'enabled': True},
|
2017-03-15 15:36:15 +00:00
|
|
|
'hello': {'world': 'peace', 'enabled': True}},
|
2017-03-15 18:02:08 +00:00
|
|
|
'pillar': {}})
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'schedule': {'hello': {'world': 'peace', 'enabled': True}},
|
2017-03-15 18:02:08 +00:00
|
|
|
'pillar': {}})
|
2014-07-08 22:58:19 +00:00
|
|
|
Schedule.add_job(self.schedule, data)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
2014-07-08 18:00:08 +00:00
|
|
|
# enable_job tests
|
|
|
|
|
|
|
|
def test_enable_job(self):
|
|
|
|
'''
|
|
|
|
Tests enabling a job
|
|
|
|
'''
|
2016-03-16 12:15:37 +00:00
|
|
|
self.schedule.opts.update({'schedule': {'name': {'enabled': 'foo'}}})
|
2014-07-08 18:00:08 +00:00
|
|
|
Schedule.enable_job(self.schedule, 'name')
|
|
|
|
self.assertTrue(self.schedule.opts['schedule']['name']['enabled'])
|
|
|
|
|
|
|
|
def test_enable_job_pillar(self):
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
Tests ignoring enable a job from pillar
|
2014-07-08 18:00:08 +00:00
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {'name': {'enabled': False}}}})
|
|
|
|
Schedule.enable_job(self.schedule, 'name', persist=False)
|
|
|
|
self.assertFalse(self.schedule.opts['pillar']['schedule']['name']['enabled'])
|
2014-07-08 18:00:08 +00:00
|
|
|
|
|
|
|
# disable_job tests
|
|
|
|
|
|
|
|
def test_disable_job(self):
|
|
|
|
'''
|
|
|
|
Tests disabling a job
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'schedule': {'name': {'enabled': 'foo'}}, 'pillar': {}})
|
2014-07-08 18:00:08 +00:00
|
|
|
Schedule.disable_job(self.schedule, 'name')
|
|
|
|
self.assertFalse(self.schedule.opts['schedule']['name']['enabled'])
|
|
|
|
|
|
|
|
def test_disable_job_pillar(self):
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
Tests ignoring disable a job in pillar
|
2014-07-08 18:00:08 +00:00
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {'name': {'enabled': True}}}, 'schedule': {}})
|
|
|
|
Schedule.disable_job(self.schedule, 'name', persist=False)
|
|
|
|
self.assertTrue(self.schedule.opts['pillar']['schedule']['name']['enabled'])
|
2014-07-08 18:00:08 +00:00
|
|
|
|
2014-07-08 21:54:30 +00:00
|
|
|
# modify_job tests
|
|
|
|
|
|
|
|
def test_modify_job(self):
|
|
|
|
'''
|
|
|
|
Tests modifying a job in the scheduler
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
schedule = {'foo': 'bar'}
|
|
|
|
self.schedule.opts.update({'schedule': {'name': 'baz'}, 'pillar': {}})
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2017-03-15 15:36:15 +00:00
|
|
|
ret.update({'schedule': {'name': {'foo': 'bar'}}})
|
|
|
|
Schedule.modify_job(self.schedule, 'name', schedule)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
|
|
|
def test_modify_job_not_exists(self):
|
|
|
|
'''
|
|
|
|
Tests modifying a job in the scheduler if jobs not exists
|
|
|
|
'''
|
|
|
|
schedule = {'foo': 'bar'}
|
|
|
|
self.schedule.opts.update({'schedule': {}, 'pillar': {}})
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2017-03-15 15:36:15 +00:00
|
|
|
ret.update({'schedule': {'name': {'foo': 'bar'}}})
|
2014-07-08 21:54:30 +00:00
|
|
|
Schedule.modify_job(self.schedule, 'name', schedule)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
|
|
|
def test_modify_job_pillar(self):
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
Tests ignoring modification of job from pillar
|
2014-07-08 21:54:30 +00:00
|
|
|
'''
|
|
|
|
schedule = {'foo': 'bar'}
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'schedule': {}, 'pillar': {'schedule': {'name': 'baz'}}})
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2017-03-15 15:36:15 +00:00
|
|
|
Schedule.modify_job(self.schedule, 'name', schedule, persist=False)
|
2014-07-08 21:54:30 +00:00
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
2016-03-16 12:15:37 +00:00
|
|
|
maxDiff = None
|
|
|
|
|
2014-07-08 18:00:08 +00:00
|
|
|
# enable_schedule tests
|
2014-07-08 17:27:32 +00:00
|
|
|
|
|
|
|
def test_enable_schedule(self):
|
|
|
|
'''
|
|
|
|
Tests enabling the scheduler
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'schedule': {'enabled': 'foo'}, 'pillar': {}})
|
2014-07-08 17:27:32 +00:00
|
|
|
Schedule.enable_schedule(self.schedule)
|
|
|
|
self.assertTrue(self.schedule.opts['schedule']['enabled'])
|
|
|
|
|
|
|
|
# disable_schedule tests
|
|
|
|
|
|
|
|
def test_disable_schedule(self):
|
|
|
|
'''
|
|
|
|
Tests disabling the scheduler
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'schedule': {'enabled': 'foo'}, 'pillar': {}})
|
2014-07-08 17:27:32 +00:00
|
|
|
Schedule.disable_schedule(self.schedule)
|
|
|
|
self.assertFalse(self.schedule.opts['schedule']['enabled'])
|
|
|
|
|
|
|
|
# reload tests
|
|
|
|
|
|
|
|
def test_reload_update_schedule_key(self):
|
|
|
|
'''
|
2014-07-08 17:38:16 +00:00
|
|
|
Tests reloading the schedule from saved schedule where both the
|
|
|
|
saved schedule and self.schedule.opts contain a schedule key
|
2014-07-08 17:27:32 +00:00
|
|
|
'''
|
|
|
|
saved = {'schedule': {'foo': 'bar'}}
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2016-03-16 12:15:37 +00:00
|
|
|
ret.update({'schedule': {'foo': 'bar', 'hello': 'world'}})
|
|
|
|
self.schedule.opts.update({'schedule': {'hello': 'world'}})
|
2014-07-08 17:27:32 +00:00
|
|
|
Schedule.reload(self.schedule, saved)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
|
|
|
def test_reload_update_schedule_no_key(self):
|
|
|
|
'''
|
2014-07-08 17:38:16 +00:00
|
|
|
Tests reloading the schedule from saved schedule that does not
|
2016-06-02 11:44:37 +00:00
|
|
|
contain a schedule key but self.schedule.opts does
|
2014-07-08 17:27:32 +00:00
|
|
|
'''
|
|
|
|
saved = {'foo': 'bar'}
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
2016-03-16 12:15:37 +00:00
|
|
|
ret.update({'schedule': {'foo': 'bar', 'hello': 'world'}})
|
|
|
|
self.schedule.opts.update({'schedule': {'hello': 'world'}})
|
2014-07-08 17:27:32 +00:00
|
|
|
Schedule.reload(self.schedule, saved)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
|
|
|
def test_reload_no_schedule_in_opts(self):
|
|
|
|
'''
|
2014-07-08 17:38:16 +00:00
|
|
|
Tests reloading the schedule from saved schedule that does not
|
|
|
|
contain a schedule key and neither does self.schedule.opts
|
2014-07-08 17:27:32 +00:00
|
|
|
'''
|
2016-06-02 11:44:37 +00:00
|
|
|
saved = {'foo': 'bar'}
|
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
|
|
|
ret['schedule'] = {'foo': 'bar'}
|
|
|
|
self.schedule.opts.pop('schedule', None)
|
2014-07-08 17:27:32 +00:00
|
|
|
Schedule.reload(self.schedule, saved)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
|
|
|
def test_reload_schedule_in_saved_but_not_opts(self):
|
|
|
|
'''
|
2014-07-08 17:38:16 +00:00
|
|
|
Tests reloading the schedule from saved schedule that contains
|
|
|
|
a schedule key, but self.schedule.opts does not
|
2014-07-08 17:27:32 +00:00
|
|
|
'''
|
|
|
|
saved = {'schedule': {'foo': 'bar'}}
|
2016-06-02 11:44:37 +00:00
|
|
|
ret = copy.deepcopy(self.schedule.opts)
|
|
|
|
ret['schedule'] = {'foo': 'bar'}
|
|
|
|
self.schedule.opts.pop('schedule', None)
|
2014-07-08 17:27:32 +00:00
|
|
|
Schedule.reload(self.schedule, saved)
|
|
|
|
self.assertEqual(self.schedule.opts, ret)
|
|
|
|
|
2014-07-09 17:59:31 +00:00
|
|
|
# eval tests
|
|
|
|
|
|
|
|
def test_eval_schedule_is_not_dict(self):
|
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
Tests eval if the schedule is not a dictionary
|
2014-07-09 17:59:31 +00:00
|
|
|
'''
|
2017-03-15 15:36:15 +00:00
|
|
|
self.schedule.opts.update({'schedule': '', 'pillar': {'schedule': {}}})
|
2014-07-09 17:59:31 +00:00
|
|
|
self.assertRaises(ValueError, Schedule.eval, self.schedule)
|
|
|
|
|
2017-03-15 15:36:15 +00:00
|
|
|
def test_eval_schedule_is_not_dict_in_pillar(self):
|
|
|
|
'''
|
|
|
|
Tests eval if the schedule from pillar is not a dictionary
|
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'schedule': {}, 'pillar': {'schedule': ''}})
|
|
|
|
self.assertRaises(ValueError, Schedule.eval, self.schedule)
|
2017-11-29 08:47:19 +00:00
|
|
|
|
|
|
|
def test_eval_schedule_time(self):
|
|
|
|
'''
|
|
|
|
Tests eval if the schedule setting time is in the future
|
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {}}})
|
|
|
|
self.schedule.opts.update({'schedule': {'testjob': {'function': 'test.true', 'seconds': 60}}})
|
2018-02-18 18:33:48 +00:00
|
|
|
now = datetime.datetime.now()
|
2017-11-29 08:47:19 +00:00
|
|
|
self.schedule.eval()
|
|
|
|
self.assertTrue(self.schedule.opts['schedule']['testjob']['_next_fire_time'] > now)
|
|
|
|
|
|
|
|
def test_eval_schedule_time_eval(self):
|
|
|
|
'''
|
|
|
|
Tests eval if the schedule setting time is in the future plus splay
|
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {}}})
|
|
|
|
self.schedule.opts.update(
|
|
|
|
{'schedule': {'testjob': {'function': 'test.true', 'seconds': 60, 'splay': 5}}})
|
2018-02-18 18:33:48 +00:00
|
|
|
now = datetime.datetime.now()
|
2017-11-29 08:47:19 +00:00
|
|
|
self.schedule.eval()
|
2018-02-18 18:33:48 +00:00
|
|
|
self.assertTrue((self.schedule.opts['schedule']['testjob']['_splay'] - now).total_seconds() > 60)
|
2017-11-29 08:47:19 +00:00
|
|
|
|
|
|
|
@skipIf(not _CRON_SUPPORTED, 'croniter module not installed')
|
|
|
|
def test_eval_schedule_cron(self):
|
|
|
|
'''
|
|
|
|
Tests eval if the schedule is defined with cron expression
|
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {}}})
|
|
|
|
self.schedule.opts.update({'schedule': {'testjob': {'function': 'test.true', 'cron': '* * * * *'}}})
|
2018-02-18 18:33:48 +00:00
|
|
|
now = datetime.datetime.now()
|
2017-11-29 08:47:19 +00:00
|
|
|
self.schedule.eval()
|
|
|
|
self.assertTrue(self.schedule.opts['schedule']['testjob']['_next_fire_time'] > now)
|
|
|
|
|
|
|
|
@skipIf(not _CRON_SUPPORTED, 'croniter module not installed')
|
|
|
|
def test_eval_schedule_cron_splay(self):
|
|
|
|
'''
|
|
|
|
Tests eval if the schedule is defined with cron expression plus splay
|
|
|
|
'''
|
|
|
|
self.schedule.opts.update({'pillar': {'schedule': {}}})
|
|
|
|
self.schedule.opts.update(
|
|
|
|
{'schedule': {'testjob': {'function': 'test.true', 'cron': '* * * * *', 'splay': 5}}})
|
|
|
|
self.schedule.eval()
|
|
|
|
self.assertTrue(self.schedule.opts['schedule']['testjob']['_splay'] >
|
|
|
|
self.schedule.opts['schedule']['testjob']['_next_fire_time'])
|