提交 c97b6434 编写于 作者: L Lukáš Doktor

selftests: Avoid creating results in our selftests

This PR adjusts our tests to avoid producing results in the default
results directory (usually caused by instantiating Job or Test without a
results dir).

One of the tests still produces test results as it is essential for the
test execution, but it goes through the produced tests and removes the
base directory.
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 eb35afbb
import os
import sys
import shutil
import tempfile
if sys.version_info[:2] == (2, 6):
import unittest2 as unittest
......@@ -16,7 +18,14 @@ basedir = os.path.abspath(basedir)
UNITTEST_GOOD = """from avocado import Test
from unittest import main
class AvocadoPassTest(Test):
class CustomBaselogDirInit(Test):
def __init__(self, *args, **kwargs):
super(CustomBaselogDirInit, self).__init__(base_logdir="%s", *args,
**kwargs)
class AvocadoPassTest(CustomBaselogDirInit):
def test(self):
self.assertTrue(True)
if __name__ == '__main__':
......@@ -25,7 +34,14 @@ if __name__ == '__main__':
UNITTEST_FAIL = """from avocado import Test
from unittest import main
class AvocadoFailTest(Test):
class CustomBaselogDirInit(Test):
def __init__(self, *args, **kwargs):
super(CustomBaselogDirInit, self).__init__(base_logdir="%s", *args,
**kwargs)
class AvocadoFailTest(CustomBaselogDirInit):
def test(self):
self.fail('This test is supposed to fail')
if __name__ == '__main__':
......@@ -34,7 +50,14 @@ if __name__ == '__main__':
UNITTEST_ERROR = """from avocado import Test
from unittest import main
class AvocadoErrorTest(Test):
class CustomBaselogDirInit(Test):
def __init__(self, *args, **kwargs):
super(CustomBaselogDirInit, self).__init__(base_logdir="%s", *args,
**kwargs)
class AvocadoErrorTest(CustomBaselogDirInit):
def test(self):
self.error('This test is supposed to error')
if __name__ == '__main__':
......@@ -45,6 +68,7 @@ if __name__ == '__main__':
class UnittestCompat(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="avocado_" + __name__)
self.original_pypath = os.environ.get('PYTHONPATH')
if self.original_pypath is not None:
os.environ['PYTHONPATH'] = '%s:%s' % (
......@@ -53,17 +77,17 @@ class UnittestCompat(unittest.TestCase):
os.environ['PYTHONPATH'] = '%s' % basedir
self.unittest_script_good = script.TemporaryScript(
'unittest_good.py',
UNITTEST_GOOD,
UNITTEST_GOOD % self.tmpdir,
'avocado_as_unittest_functional')
self.unittest_script_good.save()
self.unittest_script_fail = script.TemporaryScript(
'unittest_fail.py',
UNITTEST_FAIL,
UNITTEST_FAIL % self.tmpdir,
'avocado_as_unittest_functional')
self.unittest_script_fail.save()
self.unittest_script_error = script.TemporaryScript(
'unittest_error.py',
UNITTEST_ERROR,
UNITTEST_ERROR % self.tmpdir,
'avocado_as_unittest_functional')
self.unittest_script_error.save()
......@@ -89,6 +113,9 @@ class UnittestCompat(unittest.TestCase):
self.assertIn('This test is supposed to error', result.stderr)
self.assertIn('FAILED (errors=1)', result.stderr)
def tearDown(self):
shutil.rmtree(self.tmpdir)
if __name__ == '__main__':
unittest.main()
import argparse
import os
import shutil
import sys
import tempfile
if sys.version_info[:2] == (2, 6):
import unittest2 as unittest
else:
......@@ -15,6 +17,9 @@ from avocado.utils import path as utils_path
class JobTest(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="avocado_" + __name__)
@staticmethod
def _find_simple_test_candidates(candidates=['true', 'time', 'uptime']):
found = []
......@@ -26,29 +31,30 @@ class JobTest(unittest.TestCase):
return found
def test_job_empty_suite(self):
args = argparse.Namespace()
args = argparse.Namespace(logdir=self.tmpdir)
empty_job = job.Job(args)
self.assertIsNone(empty_job.test_suite)
def test_job_empty_has_id(self):
args = argparse.Namespace()
args = argparse.Namespace(logdir=self.tmpdir)
empty_job = job.Job(args)
self.assertIsNotNone(empty_job.unique_id)
def test_job_test_suite_not_created(self):
args = argparse.Namespace()
args = argparse.Namespace(logdir=self.tmpdir)
myjob = job.Job(args)
self.assertIsNone(myjob.test_suite)
def test_job_create_test_suite_empty(self):
args = argparse.Namespace()
args = argparse.Namespace(logdir=self.tmpdir)
myjob = job.Job(args)
self.assertRaises(exceptions.OptionValidationError,
myjob.create_test_suite)
def test_job_create_test_suite_simple(self):
simple_tests_found = self._find_simple_test_candidates()
args = argparse.Namespace(reference=simple_tests_found)
args = argparse.Namespace(reference=simple_tests_found,
logdir=self.tmpdir)
myjob = job.Job(args)
myjob.create_test_suite()
self.assertEqual(len(simple_tests_found), len(myjob.test_suite))
......@@ -64,7 +70,8 @@ class JobTest(unittest.TestCase):
self.test_suite = filtered_test_suite
super(JobFilterTime, self).pre_tests()
simple_tests_found = self._find_simple_test_candidates()
args = argparse.Namespace(reference=simple_tests_found)
args = argparse.Namespace(reference=simple_tests_found,
logdir=self.tmpdir)
myjob = JobFilterTime(args)
myjob.create_test_suite()
myjob.pre_tests()
......@@ -72,7 +79,8 @@ class JobTest(unittest.TestCase):
def test_job_run_tests(self):
simple_tests_found = self._find_simple_test_candidates(['true'])
args = argparse.Namespace(reference=simple_tests_found)
args = argparse.Namespace(reference=simple_tests_found,
logdir=self.tmpdir)
myjob = job.Job(args)
myjob.create_test_suite()
self.assertEqual(myjob.run_tests(),
......@@ -85,7 +93,8 @@ class JobTest(unittest.TestCase):
f.write(self.unique_id[::-1])
super(JobLogPost, self).post_tests()
simple_tests_found = self._find_simple_test_candidates()
args = argparse.Namespace(reference=simple_tests_found)
args = argparse.Namespace(reference=simple_tests_found,
logdir=self.tmpdir)
myjob = JobLogPost(args)
myjob.create_test_suite()
myjob.pre_tests()
......@@ -111,7 +120,8 @@ class JobTest(unittest.TestCase):
f.write(self.unique_id[::-1])
super(JobFilterLog, self).post_tests()
simple_tests_found = self._find_simple_test_candidates()
args = argparse.Namespace(reference=simple_tests_found)
args = argparse.Namespace(reference=simple_tests_found,
logdir=self.tmpdir)
myjob = JobFilterLog(args)
self.assertEqual(myjob.run(),
exit_codes.AVOCADO_ALL_OK)
......@@ -119,6 +129,9 @@ class JobTest(unittest.TestCase):
self.assertEqual(myjob.unique_id[::-1],
open(os.path.join(myjob.logdir, "reversed_id")).read())
def tearDown(self):
shutil.rmtree(self.tmpdir)
if __name__ == '__main__':
unittest.main()
......@@ -29,7 +29,8 @@ class JSONResultTest(unittest.TestCase):
self.tmpfile = tempfile.mkstemp()
self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
args = argparse.Namespace(json_output=self.tmpfile[1])
args = argparse.Namespace(json_output=self.tmpfile[1],
logdir=self.tmpdir)
self.job = job.Job(args)
self.test_result = Result(FakeJob(args))
self.test_result.filename = self.tmpfile[1]
......
......@@ -21,14 +21,12 @@ false
"""
class DummyTest(test.Test):
def test(self):
pass
class TestClassTestUnit(unittest.TestCase):
class DummyTest(test.Test):
def test(self):
pass
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix="avocado_" + __name__)
......@@ -39,8 +37,8 @@ class TestClassTestUnit(unittest.TestCase):
def testUglyName(self):
def run(name, path_name):
""" Initialize test and check the dirs were created """
tst = DummyTest("test", test.TestName(1, name),
base_logdir=self.tmpdir)
tst = self.DummyTest("test", test.TestName(1, name),
base_logdir=self.tmpdir)
self.assertEqual(os.path.basename(tst.logdir), path_name)
self.assertTrue(os.path.exists(tst.logdir))
self.assertEqual(os.path.dirname(os.path.dirname(tst.logdir)),
......@@ -64,7 +62,8 @@ class TestClassTestUnit(unittest.TestCase):
def testLongName(self):
def check(uid, name, variant, exp_logdir):
tst = DummyTest("test", test.TestName(uid, name, variant))
tst = self.DummyTest("test", test.TestName(uid, name, variant),
base_logdir=self.tmpdir)
self.assertEqual(os.path.basename(tst.logdir), exp_logdir)
return tst
......@@ -100,8 +99,8 @@ class TestClassTestUnit(unittest.TestCase):
def testAllDirsExistsNoHang(self):
flexmock(os.path)
os.path.should_receive('exists').and_return(True)
self.assertRaises(exceptions.TestSetupFail, DummyTest, "test",
test.TestName(1, "name"))
self.assertRaises(exceptions.TestSetupFail, self.DummyTest, "test",
test.TestName(1, "name"), base_logdir=self.tmpdir)
class TestClassTest(unittest.TestCase):
......@@ -245,7 +244,7 @@ class SkipTest(unittest.TestCase):
def tearDown(self):
for tst in self.tests:
try:
shutil.rmtree(os.path.dirname(tst.logdir))
shutil.rmtree(os.path.dirname(os.path.dirname(tst.logdir)))
except Exception:
pass
......
......@@ -35,7 +35,7 @@ class xUnitSucceedTest(unittest.TestCase):
self.tmpfile = tempfile.mkstemp()
self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__)
args = argparse.Namespace()
args = argparse.Namespace(logdir=self.tmpdir)
args.xunit_output = self.tmpfile[1]
self.job = job.Job(args)
self.test_result = Result(FakeJob(args))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册