未验证 提交 7a5a7b69 编写于 作者: L Lukáš Doktor

Merging pull request 1675

* https://github.com/avocado-framework/avocado:
  plugins: tests temporary directory
......@@ -280,6 +280,19 @@ class Test(unittest.TestCase):
else:
return None
@property
def teststmpdir(self):
"""
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'
path = os.environ.get(env_var)
if path is None:
msg = 'Environment Variable %s is not set.' % env_var
raise EnvironmentError(msg)
return path
@data_structures.LazyProperty
def workdir(self):
basename = (os.path.basename(self.logdir).replace(':', '_')
......
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat, Inc. 2016
# Author: Amador Pahim <apahim@redhat.com>
"""
Tests temporary directory plugin
"""
import os
import shutil
import tempfile
from avocado.core.plugin_interfaces import JobPre, JobPost
class TestsTmpDir(JobPre, JobPost):
name = 'teststmpdir'
description = 'Creates a temporary directory for tests consumption'
def __init__(self):
self._varname = 'AVOCADO_TESTS_COMMON_TMPDIR'
self._dirname = None
def pre(self, job):
if os.environ.get(self._varname) is None:
self._dirname = tempfile.mkdtemp(prefix='avocado_')
os.environ[self._varname] = self._dirname
def post(self, job):
if (self._dirname is not None and
os.environ.get(self._varname) == self._dirname):
try:
shutil.rmtree(self._dirname)
del os.environ[self._varname]
except:
pass
def __del__(self):
# Make sure we will cleanup if something bad happens
self.post(None)
......@@ -1190,29 +1190,34 @@ tests can do and also they can modify the test parameters.
Here are the current variables that Avocado exports to the tests:
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| Environemnt Variable | Meaning | Example |
+=========================+=======================================+=====================================================================================================+
| AVOCADO_VERSION | Version of Avocado test runner | 0.12.0 |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_BASEDIR | Base directory of Avocado tests | $HOME/Downloads/avocado-source/avocado |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_DATADIR | Data directory for the test | $AVOCADO_TEST_BASEDIR/my_test.sh.data |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_WORKDIR | Work directory for the test | /var/tmp/avocado_Bjr_rd/my_test.sh |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_SRCDIR | Source directory for the test | /var/tmp/avocado_Bjr_rd/my-test.sh/src |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_LOGDIR | Log directory for the test | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1 |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_LOGFILE | Log file for the test | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1/debug.log |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_OUTPUTDIR | Output directory for the test | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1/data |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_SYSINFODIR | The system information directory | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1/sysinfo |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| * | All variables from --mux-yaml | TIMEOUT=60; IO_WORKERS=10; VM_BYTES=512M; ... |
+-------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| Environemnt Variable | Meaning | Example |
+=============================+=======================================+=====================================================================================================+
| AVOCADO_VERSION | Version of Avocado test runner | 0.12.0 |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_BASEDIR | Base directory of Avocado tests | $HOME/Downloads/avocado-source/avocado |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_DATADIR | Data directory for the test | $AVOCADO_TEST_BASEDIR/my_test.sh.data |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_WORKDIR | Work directory for the test | /var/tmp/avocado_Bjr_rd/my_test.sh |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_SRCDIR | Source directory for the test | /var/tmp/avocado_Bjr_rd/my-test.sh/src |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TESTS_COMMON_TMPDIR | Temporary directory created by the | /var/tmp/avocado_XhEdo/ |
| | `teststmpdir` plugin. The directory | |
| | is persistent throughout the tests | |
| | in the same Job | |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_LOGDIR | Log directory for the test | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1 |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_LOGFILE | Log file for the test | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1/debug.log |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_OUTPUTDIR | Output directory for the test | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1/data |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| AVOCADO_TEST_SYSINFODIR | The system information directory | $HOME/logs/job-results/job-2014-09-16T14.38-ac332e6/test-results/$HOME/my_test.sh.1/sysinfo |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
| * | All variables from --mux-yaml | TIMEOUT=60; IO_WORKERS=10; VM_BYTES=512M; ... |
+-----------------------------+---------------------------------------+-----------------------------------------------------------------------------------------------------+
Simple Tests BASH extensions
......
#!/usr/bin/env python
import os
import sys
import tempfile
import shutil
if sys.version_info[:2] == (2, 6):
import unittest2 as unittest
else:
import unittest
from avocado.core import exit_codes
from avocado.utils import process
from avocado.utils import script
basedir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..')
basedir = os.path.abspath(basedir)
INSTRUMENTED_SCRIPT = """import os
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()
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 ]
then
exit 0
else
exit 1
fi
"""
class TestsTmpDirTests(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
self.simple_test = script.TemporaryScript(
'test_simple.py',
SIMPLE_SCRIPT)
self.simple_test.save()
self.instrumented_test = script.TemporaryScript(
'test_instrumented.py',
INSTRUMENTED_SCRIPT)
self.instrumented_test.save()
def run_and_check(self, cmd_line, expected_rc):
os.chdir(basedir)
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(result.exit_status, expected_rc,
"Command %s did not return rc "
"%d:\n%s" % (cmd_line, expected_rc, result))
return result
def test_tests_tmp_dir(self):
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 tearDown(self):
shutil.rmtree(self.tmpdir)
if __name__ == '__main__':
unittest.main()
......@@ -160,6 +160,7 @@ if __name__ == '__main__':
],
'avocado.plugins.job.prepost': [
'jobscripts = avocado.plugins.jobscripts:JobScripts',
'teststmpdir = avocado.plugins.teststmpdir:TestsTmpDir',
],
'avocado.plugins.result': [
'xunit = avocado.plugins.xunit:XUnitResult',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册