add job cache integration tests

This commit is contained in:
Ch3LL 2016-08-08 17:01:02 -06:00
parent f8158124d5
commit 6837acf742
3 changed files with 154 additions and 0 deletions

View file

@ -0,0 +1 @@
# -*- coding: utf-8 -*-

View file

@ -0,0 +1,143 @@
# -*- coding: utf-8 -*-
'''
Tests for the salt-run command
'''
# Import Python libs
from __future__ import absolute_import
import logging
import os
import platform
import tempfile
# Import Salt Testing libs
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import salt libs
import integration
import salt.utils.job
from salt.returners import local_cache
log = logging.getLogger(__name__)
if platform.uname()[0] == 'Darwin':
SYS_TMP_DIR = '/tmp'
elif salt.utils.is_windows():
SYS_TMP_DIR = os.path.join("c:/", "tmp")
else:
SYS_TMP_DIR = os.environ.get('TMPDIR', tempfile.gettempdir())
# CACHE_DIR
TMP = os.path.join(SYS_TMP_DIR, 'salt-tests-tmpdir')
TMP_CACHE_DIR = os.path.join(TMP, 'master-minion-root', 'cache')
# JOBS DIR and FILES
JOBS_DIR = os.path.join(TMP, TMP_CACHE_DIR, 'jobs')
JID_DIR = os.path.join(JOBS_DIR, 'b1', '4dac7bc743c928d7fac908823af1ab')
JID_FILE = os.path.join(JID_DIR, 'jid')
JID_MINION_DIR = os.path.join(JID_DIR, 'minion', 'return.p')
JOB_CACHE_DIR_FILES = [JID_FILE, JID_MINION_DIR]
KEEP_JOBS = 0.0000000010
EMPTY_JID_DIR = []
local_cache.__opts__ = {'cachedir': TMP_CACHE_DIR,
'keep_jobs': KEEP_JOBS}
class Local_CacheTest(integration.ShellCase):
'''
Test the local cache returner
'''
def _check_dir_files(self, msg, contents, status='None'):
'''
helper method to ensure files or dirs
are either present or removed
'''
for content in contents:
log.debug('CONTENT {0}'.format(content))
if status == 'present':
check_job_dir = os.path.exists(content)
elif status == 'removed':
if os.path.exists(content):
check_job_dir = False
else:
check_job_dir = True
self.assertTrue(check_job_dir,
msg=msg + content)
def _add_job(self):
'''
helper method to add job.
'''
# add the job.
opts = {}
opts.update(self.get_config('master'))
load = {'fun_args': [], 'jid': '20160603132323715452',
'return': True, 'retcode': 0, 'success': True,
'cmd': '_return', 'fun': 'test.ping', 'id': 'minion'}
add_job = salt.utils.job.store_job(opts, load)
self.assertEqual(add_job, None)
self._check_dir_files('Dir/file does not exist: ',
JOB_CACHE_DIR_FILES,
status='present')
def test_clean_old_jobs(self):
'''
test to ensure jobs are removed from job cache
'''
self._add_job()
# remove job
remove_cache = local_cache.clean_old_jobs()
self.assertEqual(remove_cache, None)
self._check_dir_files('job cache was not removed: ',
JOB_CACHE_DIR_FILES,
status='removed')
def test_not_clean_new_jobs(self):
'''
test to ensure jobs are not removed when
jobs dir is new
'''
self._add_job()
local_cache.__opts__['keep_jobs'] = 24
remove_cache = local_cache.clean_old_jobs()
self.assertEqual(remove_cache, None)
self._check_dir_files('job cache was removed: ',
JOB_CACHE_DIR_FILES,
status='present')
# need to set back to initial KEEP_JOBS
local_cache.__opts__['keep_jobs'] = KEEP_JOBS
def test_empty_jid_dir(self):
'''
test to ensure removal of empty jid dir
'''
# add empty jid dir
new_jid_dir = os.path.join(JOBS_DIR, 'z0')
EMPTY_JID_DIR.append(new_jid_dir)
os.makedirs(new_jid_dir)
# check dir exists
self._check_dir_files('new_jid_dir was not created',
EMPTY_JID_DIR,
status='present')
# remove job
remove_cache = local_cache.clean_old_jobs()
self.assertEqual(remove_cache, None)
# check jid dir is removed
self._check_dir_files('new_jid_dir was not removed',
EMPTY_JID_DIR,
status='removed')
if __name__ == '__main__':
from integration import run_tests
run_tests(Local_CacheTest)

View file

@ -138,6 +138,13 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
action='store_true',
help='Run salt/renderers/*.py tests'
)
self.test_selection_group.add_option(
'--returners',
dest='returners',
default=False,
action='store_true',
help='Run salt/returners/*.py tests'
)
self.test_selection_group.add_option(
'-l',
'--loader',
@ -222,6 +229,7 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
self.options.state,
self.options.runners,
self.options.renderers,
self.options.returners,
self.options.loader,
self.options.name,
self.options.outputter,
@ -253,6 +261,7 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
self.options.unit = True
self.options.runners = True
self.options.renderers = True
self.options.returners = True
self.options.state = True
self.options.loader = True
self.options.outputter = True
@ -372,6 +381,7 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
if (self.options.unit or named_unit_test) and not \
(self.options.runners or
self.options.renderers or
self.options.returners or
self.options.state or
self.options.module or
self.options.cli or