-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Because the current implementation of avocado.main() creates a job and runs "sys.argv[0]" to implement standalone mode, it ends up running itself over and over. This simple proposed fix, prevents avocado.main() from running itself again if called from itself. Since they are on different processes, the mechanism chosen to do this is to set an environment variable, that will be seen by the next process. Also, by exiting from main() with an error code, the test first level test will fail. This will let the user know that the chosen approach (SIMPLE tests written in Python and calling main()) are not worthy of a PASS. This adresses issue avocado-framework#961. Signed-off-by: Cleber Rosa <crosa@redhat.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
else: | ||
import unittest | ||
|
||
from avocado.core import exit_codes | ||
from avocado.utils import script | ||
from avocado.utils import process | ||
|
||
|
@@ -82,7 +83,6 @@ def test(self): | |
main() | ||
""" | ||
|
||
|
||
NOT_A_TEST = """ | ||
def hello(): | ||
print('Hello World!') | ||
|
@@ -100,6 +100,40 @@ def hello(): | |
true | ||
""" | ||
|
||
AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES = """#!/usr/bin/env python | ||
# A simple test (executable bit set when saved to file) that looks like | ||
# an Avocado instrumented test, with base class on separate file | ||
from avocado import Test | ||
from avocado import main | ||
from test2 import * | ||
class BasicTestSuite(SuperTest): | ||
def test1(self): | ||
self.xxx() | ||
self.assertTrue(True) | ||
if __name__ == '__main__': | ||
main() | ||
""" | ||
|
||
AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES_LIB = """ | ||
#!/usr/bin/python | ||
from avocado import Test | ||
class SuperTest(Test): | ||
def xxx(self): | ||
print "ahoj" | ||
""" | ||
|
||
AVOCADO_TEST_SIMPLE_USING_MAIN = """#!/usr/bin/env python | ||
from avocado import main | ||
if __name__ == "__main__": | ||
main() | ||
""" | ||
|
||
|
||
class LoaderTestFunctional(unittest.TestCase): | ||
|
||
|
@@ -161,6 +195,41 @@ def test_load_not_a_test(self): | |
def test_load_not_a_test_not_exec(self): | ||
self._test('notatest.py', NOT_A_TEST, 'NOT_A_TEST') | ||
|
||
def test_runner_simple_python_like_multiple_files(self): | ||
mylib = script.TemporaryScript( | ||
'test2.py', | ||
AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES_LIB, | ||
'avocado_simpletest_functional', | ||
0644) | ||
mylib.save() | ||
mytest = script.Script( | ||
os.path.join(os.path.dirname(mylib.path), 'test.py'), | ||
AVOCADO_SIMPLE_PYTHON_LIKE_MULTIPLE_FILES) | ||
os.chdir(basedir) | ||
mytest.save() | ||
cmd_line = "./scripts/avocado list -V %s" % mytest | ||
result = process.run(cmd_line) | ||
self.assertIn('SIMPLE: 1', result.stdout) | ||
# job should be able to finish under 5 seconds. If this fails, it's | ||
# possible that we hit the "simple test fork bomb" bug | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
clebergnu
Author
Owner
|
||
cmd_line = ("./scripts/avocado run --sysinfo=off --job-results-dir %s " | ||
"--job-timeout=5s %s" % (self.tmpdir, mytest)) | ||
result = process.run(cmd_line, ignore_status=True) | ||
self.assertEquals(result.exit_status, exit_codes.AVOCADO_TESTS_FAIL) | ||
|
||
def test_simple_using_main(self): | ||
mytest = script.TemporaryScript("simple_using_main.py", | ||
AVOCADO_TEST_SIMPLE_USING_MAIN, | ||
'avocado_simpletest_functional') | ||
mytest.save() | ||
os.chdir(basedir) | ||
# job should be able to finish under 5 seconds. If this fails, it's | ||
# possible that we hit the "simple test fork bomb" bug | ||
cmd_line = ("./scripts/avocado run --sysinfo=off --job-results-dir %s " | ||
"--job-timeout=5s %s" % (self.tmpdir, mytest)) | ||
result = process.run(cmd_line, ignore_status=True) | ||
self.assertEquals(result.exit_status, exit_codes.AVOCADO_TESTS_FAIL) | ||
|
||
def tearDown(self): | ||
shutil.rmtree(self.tmpdir) | ||
|
||
|
1 comment
on commit bd19f35
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ldoktor right, the unittest passes because of the change in behavior on this v3 that looks for exit_codes.AVOCADO_TESTS_FAIL
. This has indeed to be fixed.
Now, if we noticed more broken things, say in process.run()
, it'd be wise to take one at a time.
One doubt here, if the simple test fork bomb bug happens, will avocado handle things correctly just by passing
--job-timeout
to it? Isn't it better to add a timeout to theprocess.run
call?