Merge pull request #35302 from Ch3LL/add_job_cache_test

Add job cache test
This commit is contained in:
Mike Place 2016-08-17 19:45:28 +09:00 committed by GitHub
commit 9f87081cef
3 changed files with 144 additions and 2 deletions

View file

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

View file

@ -0,0 +1,129 @@
# -*- coding: utf-8 -*-
'''
Tests for the local_cache returner
'''
# Import Python libs
from __future__ import absolute_import
import logging
import os
# Import Salt Testing libs
from salttesting.helpers import ensure_in_syspath
ensure_in_syspath('../../')
# Import salt libs
import integration
from integration import TMP
import salt.utils.job
from salt.returners import local_cache
log = logging.getLogger(__name__)
# JOBS DIR and FILES
TMP_CACHE_DIR = os.path.join(TMP, 'master-minion-root', 'cache')
JOBS_DIR = os.path.join(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
self.assertEqual(local_cache.clean_old_jobs(), 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
self.assertEqual(local_cache.clean_old_jobs(), 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
self.assertEqual(local_cache.clean_old_jobs(), 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,
@ -244,7 +252,7 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
self.options.loader, self.options.name,
self.options.outputter, self.options.cloud_provider_tests,
self.options.fileserver, self.options.wheel,
self.options.api, self.options.renderers)):
self.options.api, self.options.returners, self.options.renderers)):
self.options.module = True
self.options.cli = True
self.options.client = True
@ -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
@ -406,7 +416,7 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
self.options.shell, self.options.state,
self.options.loader, self.options.outputter,
self.options.name, self.options.cloud_provider_tests,
self.options.api, self.options.renderers,
self.options.api, self.options.returners, self.options.renderers,
self.options.fileserver, self.options.wheel]):
return status
@ -444,6 +454,8 @@ class SaltTestsuiteParser(SaltCoverageTestingParser):
status.append(self.run_integration_suite('cloud/providers', 'Cloud Provider'))
if self.options.api:
status.append(self.run_integration_suite('netapi', 'NetAPI'))
if self.options.returners:
status.append(self.run_integration_suite('returners', 'Returners'))
if self.options.renderers:
status.append(self.run_integration_suite('renderers', 'Renderers'))
return status