未验证 提交 44a6efeb 编写于 作者: A Amador Pahim

Merge branch 'ldoktor-testtmpdir'

...@@ -47,6 +47,11 @@ else: ...@@ -47,6 +47,11 @@ else:
import unittest import unittest
#: Environment variable used to store the location of a tmpfile which is
#: preserved across all tests execution (usually in one job)
COMMON_TMPDIR_NAME = 'AVOCADO_TESTS_COMMON_TMPDIR'
class NameNotTestNameError(Exception): class NameNotTestNameError(Exception):
""" """
...@@ -286,7 +291,7 @@ class Test(unittest.TestCase): ...@@ -286,7 +291,7 @@ class Test(unittest.TestCase):
Returns the path of the temporary directory that will stay the Returns the path of the temporary directory that will stay the
same for all tests in a given Job. same for all tests in a given Job.
""" """
env_var = 'AVOCADO_TESTS_COMMON_TMPDIR' env_var = COMMON_TMPDIR_NAME
path = os.environ.get(env_var) path = os.environ.get(env_var)
if path is None: if path is None:
msg = 'Environment Variable %s is not set.' % env_var msg = 'Environment Variable %s is not set.' % env_var
......
...@@ -20,6 +20,7 @@ import shutil ...@@ -20,6 +20,7 @@ import shutil
import tempfile import tempfile
from avocado.core.plugin_interfaces import JobPre, JobPost from avocado.core.plugin_interfaces import JobPre, JobPost
from avocado.core import test
class TestsTmpDir(JobPre, JobPost): class TestsTmpDir(JobPre, JobPost):
...@@ -28,7 +29,7 @@ class TestsTmpDir(JobPre, JobPost): ...@@ -28,7 +29,7 @@ class TestsTmpDir(JobPre, JobPost):
description = 'Creates a temporary directory for tests consumption' description = 'Creates a temporary directory for tests consumption'
def __init__(self): def __init__(self):
self._varname = 'AVOCADO_TESTS_COMMON_TMPDIR' self._varname = test.COMMON_TMPDIR_NAME
self._dirname = None self._dirname = None
def pre(self, job): def pre(self, job):
......
...@@ -11,6 +11,7 @@ else: ...@@ -11,6 +11,7 @@ else:
import unittest import unittest
from avocado.core import exit_codes from avocado.core import exit_codes
from avocado.core import test
from avocado.utils import process from avocado.utils import process
from avocado.utils import script from avocado.utils import script
...@@ -23,23 +24,21 @@ import tempfile ...@@ -23,23 +24,21 @@ import tempfile
from avocado import Test from avocado import Test
class MyTest(Test): class MyTest(Test):
def test1(self): def test1(self):
file = os.path.join(self.teststmpdir, tempfile.mkstemp(dir=self.teststmpdir)
next(tempfile._get_candidate_names()))
open(file, "w+").close()
if len(os.listdir(self.teststmpdir)) != 2: if len(os.listdir(self.teststmpdir)) != 2:
self.fail() self.fail()
""" """
SIMPLE_SCRIPT = """#!/bin/bash SIMPLE_SCRIPT = """#!/bin/bash
mktemp ${AVOCADO_TESTS_COMMON_TMPDIR}/XXXXXX mktemp ${{{0}}}/XXXXXX
if [ $(ls ${AVOCADO_TESTS_COMMON_TMPDIR} | wc -l) == 1 ] if [ $(ls ${{{0}}} | wc -l) == 1 ]
then then
exit 0 exit 0
else else
exit 1 exit 1
fi fi
""" """.format(test.COMMON_TMPDIR_NAME)
class TestsTmpDirTests(unittest.TestCase): class TestsTmpDirTests(unittest.TestCase):
...@@ -55,20 +54,45 @@ class TestsTmpDirTests(unittest.TestCase): ...@@ -55,20 +54,45 @@ class TestsTmpDirTests(unittest.TestCase):
INSTRUMENTED_SCRIPT) INSTRUMENTED_SCRIPT)
self.instrumented_test.save() self.instrumented_test.save()
def run_and_check(self, cmd_line, expected_rc): def run_and_check(self, cmd_line, expected_rc, env=None):
os.chdir(basedir) os.chdir(basedir)
result = process.run(cmd_line, ignore_status=True) result = process.run(cmd_line, ignore_status=True, env=env)
self.assertEqual(result.exit_status, expected_rc, self.assertEqual(result.exit_status, expected_rc,
"Command %s did not return rc " "Command %s did not return rc "
"%d:\n%s" % (cmd_line, expected_rc, result)) "%d:\n%s" % (cmd_line, expected_rc, result))
return result return result
@unittest.skipIf(test.COMMON_TMPDIR_NAME in os.environ,
"%s already set in os.environ"
% test.COMMON_TMPDIR_NAME)
def test_tests_tmp_dir(self): def test_tests_tmp_dir(self):
"""
Tests whether automatically created teststmpdir is shared across
all tests.
"""
cmd_line = ("./scripts/avocado run --sysinfo=off " cmd_line = ("./scripts/avocado run --sysinfo=off "
"--job-results-dir %s %s %s" % "--job-results-dir %s %s %s" %
(self.tmpdir, self.simple_test, self.instrumented_test)) (self.tmpdir, self.simple_test, self.instrumented_test))
self.run_and_check(cmd_line, exit_codes.AVOCADO_ALL_OK) self.run_and_check(cmd_line, exit_codes.AVOCADO_ALL_OK)
def test_manualy_created(self):
"""
Tests whether manually set teststmpdir is used and not deleted by
avocado
"""
shared_tmp = tempfile.mkdtemp(dir=self.tmpdir)
cmd = ("./scripts/avocado run --sysinfo=off "
"--job-results-dir %s %%s" % self.tmpdir)
self.run_and_check(cmd % self.simple_test, exit_codes.AVOCADO_ALL_OK,
{test.COMMON_TMPDIR_NAME: shared_tmp})
self.run_and_check(cmd % self.instrumented_test,
exit_codes.AVOCADO_ALL_OK,
{test.COMMON_TMPDIR_NAME: shared_tmp})
content = os.listdir(shared_tmp)
self.assertEqual(len(content), 2, "The number of tests in manually "
"set teststmpdir is not 2 (%s):\n%s"
% (len(content), content))
def tearDown(self): def tearDown(self):
shutil.rmtree(self.tmpdir) shutil.rmtree(self.tmpdir)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册