diff --git a/avocado/core/test.py b/avocado/core/test.py index 6500063125a1048dd44cfb2baa3bb22e85344807..759a804f1a38cf2c7a75760b20583859c662b1fe 100644 --- a/avocado/core/test.py +++ b/avocado/core/test.py @@ -47,6 +47,11 @@ else: 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): """ @@ -286,7 +291,7 @@ class Test(unittest.TestCase): Returns the path of the temporary directory that will stay the 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) if path is None: msg = 'Environment Variable %s is not set.' % env_var diff --git a/avocado/plugins/teststmpdir.py b/avocado/plugins/teststmpdir.py index 18a7da2790f09a97def84d430f8d31647161ab27..7626e40dd68ce4158b52acc6eaf1417294238a29 100644 --- a/avocado/plugins/teststmpdir.py +++ b/avocado/plugins/teststmpdir.py @@ -20,6 +20,7 @@ import shutil import tempfile from avocado.core.plugin_interfaces import JobPre, JobPost +from avocado.core import test class TestsTmpDir(JobPre, JobPost): @@ -28,7 +29,7 @@ class TestsTmpDir(JobPre, JobPost): description = 'Creates a temporary directory for tests consumption' def __init__(self): - self._varname = 'AVOCADO_TESTS_COMMON_TMPDIR' + self._varname = test.COMMON_TMPDIR_NAME self._dirname = None def pre(self, job): diff --git a/selftests/functional/test_teststmpdir.py b/selftests/functional/test_teststmpdir.py index 31c93febe07efd39c564cba8bf14763b0dd51c70..9f5bc555aabb2d07933f062202a3d13df0b82151 100644 --- a/selftests/functional/test_teststmpdir.py +++ b/selftests/functional/test_teststmpdir.py @@ -11,6 +11,7 @@ else: import unittest from avocado.core import exit_codes +from avocado.core import test from avocado.utils import process from avocado.utils import script @@ -23,23 +24,21 @@ import tempfile from avocado import Test class MyTest(Test): def test1(self): - file = os.path.join(self.teststmpdir, - next(tempfile._get_candidate_names())) - open(file, "w+").close() + tempfile.mkstemp(dir=self.teststmpdir) if len(os.listdir(self.teststmpdir)) != 2: self.fail() """ SIMPLE_SCRIPT = """#!/bin/bash -mktemp ${AVOCADO_TESTS_COMMON_TMPDIR}/XXXXXX -if [ $(ls ${AVOCADO_TESTS_COMMON_TMPDIR} | wc -l) == 1 ] +mktemp ${{{0}}}/XXXXXX +if [ $(ls ${{{0}}} | wc -l) == 1 ] then exit 0 else exit 1 fi -""" +""".format(test.COMMON_TMPDIR_NAME) class TestsTmpDirTests(unittest.TestCase): @@ -55,20 +54,45 @@ class TestsTmpDirTests(unittest.TestCase): INSTRUMENTED_SCRIPT) 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) - 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, "Command %s did not return rc " "%d:\n%s" % (cmd_line, expected_rc, 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): + """ + Tests whether automatically created teststmpdir is shared across + all tests. + """ cmd_line = ("./scripts/avocado run --sysinfo=off " "--job-results-dir %s %s %s" % (self.tmpdir, self.simple_test, self.instrumented_test)) 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): shutil.rmtree(self.tmpdir)