Fix unit.returners.test_local_cache for Windows

time.time is only precise to 3 decimal places in Windows. This adds a
sleep for .25 seconds to allow the test to pass
Also fixes a lint error
This commit is contained in:
twangboy 2018-03-21 16:00:52 +00:00
parent 83ed40c06a
commit 8d93156604
No known key found for this signature in database
GPG key ID: 93FF3BDEB278C9EB
2 changed files with 12 additions and 1 deletions

View file

@ -420,7 +420,7 @@ def clean_old_jobs():
shutil.rmtree(t_path)
elif os.path.isfile(jid_file):
jid_ctime = os.stat(jid_file).st_ctime
hours_difference = (time.time()- jid_ctime) / 3600.0
hours_difference = (time.time() - jid_ctime) / 3600.0
if hours_difference > __opts__['keep_jobs'] and os.path.exists(t_path):
# Remove the entire t_path from the original JID dir
shutil.rmtree(t_path)

View file

@ -10,6 +10,7 @@ Unit tests for the Default Job Cache (local_cache).
from __future__ import absolute_import
import os
import shutil
import time
import logging
import tempfile
@ -75,6 +76,11 @@ class LocalCacheCleanOldJobsTestCase(TestCase, LoaderModuleMockMixin):
# Call clean_old_jobs function, patching the keep_jobs value with a
# very small value to force the call to clean the job.
with patch.dict(local_cache.__opts__, {'keep_jobs': 0.00000001}):
# Sleep on Windows because time.time is only precise to 3 decimal
# points, and therefore subtracting the jid_ctime from time.time
# will result in a negative number
if salt.utils.is_windows():
time.sleep(0.25)
local_cache.clean_old_jobs()
# Assert that the JID dir was removed
@ -138,6 +144,11 @@ class LocalCacheCleanOldJobsTestCase(TestCase, LoaderModuleMockMixin):
# Call clean_old_jobs function, patching the keep_jobs value with a
# very small value to force the call to clean the job.
with patch.dict(local_cache.__opts__, {'keep_jobs': 0.00000001}):
# Sleep on Windows because time.time is only precise to 3 decimal
# points, and therefore subtracting the jid_ctime from time.time
# will result in a negative number
if salt.utils.is_windows():
time.sleep(0.25)
local_cache.clean_old_jobs()
# Assert that the JID dir was removed