提交 2303ef2c 编写于 作者: L Lucas Meneghel Rodrigues

Merge pull request #819 from lmr/ldoktor-logdir

avocado.core.data_dir: Make logdir creation safer [v3]
......@@ -213,13 +213,29 @@ def create_job_logs_dir(logdir=None, unique_id=None):
start_time = time.strftime('%Y-%m-%dT%H.%M')
if logdir is None:
logdir = get_logs_dir()
if not os.path.exists(logdir):
utils_path.init_dir(logdir)
# Stand alone tests handling
if unique_id is None:
unique_id = job_id.create_unique_job_id()
debugbase = 'job-%s-%s' % (start_time, unique_id[:7])
debugdir = utils_path.init_dir(logdir, debugbase)
return debugdir
debugdir = os.path.join(logdir, 'job-%s-%s' % (start_time, unique_id[:7]))
for i in xrange(7, len(unique_id)):
try:
os.mkdir(debugdir)
except OSError:
debugdir += unique_id[i]
continue
return debugdir
debugdir += "."
for i in xrange(1000):
try:
os.mkdir(debugdir + str(i))
except OSError:
continue
return debugdir + str(i)
raise IOError("Unable to create unique logdir in 1000 iterations: %s"
% (debugdir))
class _TmpDirTracker(Borg):
......
......@@ -52,6 +52,27 @@ class DataDirTest(unittest.TestCase):
reload(data_dir)
del data_dir
def testUniqueLogDir(self):
"""
Tests that multiple queries for a logdir at the same time provides
unique results.
"""
from avocado.core import data_dir
flexmock(data_dir.time).should_receive('strftime').and_return("date")
logdir = os.path.join(self.mapping['base_dir'], "foor", "bar", "baz")
path_prefix = os.path.join(logdir, "job-date-")
uid = "1234567890"*4
for i in xrange(7, 40):
path = data_dir.create_job_logs_dir(logdir, uid)
self.assertEqual(path, path_prefix + uid[:i])
self.assertTrue(os.path.exists(path))
path = data_dir.create_job_logs_dir(logdir, uid)
self.assertEqual(path, path_prefix + uid + ".0")
self.assertTrue(os.path.exists(path))
path = data_dir.create_job_logs_dir(logdir, uid)
self.assertEqual(path, path_prefix + uid + ".1")
self.assertTrue(os.path.exists(path))
def tearDown(self):
os.unlink(self.config_file.name)
shutil.rmtree(self.mapping['base_dir'])
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册