Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing return of incorrect subprocess #3562

Merged
merged 6 commits into from
Jan 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 52 additions & 47 deletions kratos/python_scripts/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
from __future__ import print_function, absolute_import, division

import imp
import os
import re
import getopt
import sys
import subprocess
import getopt
import threading
import subprocess

from KratosMultiphysics import Tester
from KratosMultiphysics import KratosLoader
from KratosMultiphysics.KratosUnittest import SupressConsoleOutput, SupressConsoleError, SupressAllConsole
import KratosMultiphysics as KtsMp
import KratosMultiphysics.KratosUnittest as KtsUt


def Usage():
Expand Down Expand Up @@ -42,7 +40,7 @@ def GetModulePath(module):

'''

return imp.find_module(module)[1]
return os.path.dirname(KtsMp.__file__)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer general I'd say => GetModulePath should now be named GetKratosMultiphysicsPath



def GetAvailableApplication():
Expand Down Expand Up @@ -76,23 +74,21 @@ def __init__(self):
self.exitCode = 0

def RunTestSuitInTime(self, application, applicationPath, path, level, verbose, command, timeout):
if(timeout > -1):
t = threading.Thread(
target=self.RunTestSuit,
args=(application, applicationPath, path, level, verbose, command)
)
t = threading.Thread(
target=self.RunTestSuit,
args=(application, applicationPath, path, level, verbose, command)
)

t.start()
t.join(timeout)
t.start()
t.join(timeout)

if t.isAlive():
self.process.terminate()
t.join()
print('\nABORT: Tests for {} took to long. Process Killed.'.format(application), file=sys.stderr)
else:
print('\nTests for {} finished in time ({}s).'.format(application, timeout))
else:
self.RunTestSuit(application, applicationPath, path, level, verbose, command)
if t.isAlive():
self.process.terminate()
t.join()
print('\n[Error]: Tests for {} took to long. Process Killed.'.format(application), file=sys.stderr)
if self.exitCode != 0:
# If thread terminated with an error, propagate and terminate
sys.exit(self.exitCode)

def RunTestSuit(self, application, applicationPath, path, level, verbose, command):
''' Calls the script that will run the tests.
Expand Down Expand Up @@ -153,24 +149,33 @@ def RunTestSuit(self, application, applicationPath, path, level, verbose, comman
file=sys.stderr)

if os.path.isfile(script):
self.process = subprocess.Popen([
command,
script,
'-l'+level,
'-v'+str(verbose)
], stdout=subprocess.PIPE)

# Used instead of wait to "soft-block" the process and prevent deadlocks
# and capture the first exit code different from OK
process_stdout, process_stderr = self.process.communicate()
if process_stdout:
print(process_stdout.decode('ascii'), file=sys.stdout)
if process_stderr:
print(process_stderr.decode('ascii'), file=sys.stderr)

# Running out of time in the tests will send the error code -15. We may want to skip
# that one in a future. Right now will throw everything different from 0.
self.exitCode = int(self.process.returncode != 0)
try:
self.process = subprocess.Popen([
command,
script,
'-l'+level,
'-v'+str(verbose)
], stdout=subprocess.PIPE)
except OSError:
# Command does not exist
print('[Error]: Unable to execute {}'.format(command), file=sys.stderr)
self.exitCode = 1
except ValueError:
# Command does exist, but the arguments are invalid (It sohuld never enter here. Just to be safe)
print('[Error]: Invalid arguments when calling {} {} {} {}'.format(command, script, '-l'+level, '-v'+str(verbose)), file=sys.stderr)
self.exitCode = 1
else:
# Used instead of wait to "soft-block" the process and prevent deadlocks
# and capture the first exit code different from OK
process_stdout, process_stderr = self.process.communicate()
if process_stdout:
print(process_stdout.decode('ascii'), file=sys.stdout)
if process_stderr:
print(process_stderr.decode('ascii'), file=sys.stderr)

# Running out of time in the tests will send the error code -15. We may want to skip
# that one in a future. Right now will throw everything different from 0.
self.exitCode = int(self.process.returncode != 0)
else:
if verbose > 0:
print(
Expand All @@ -196,8 +201,8 @@ def RunCppTests(self, applications):
__import__("KratosMultiphysics." + application)

try:
Tester.SetVerbosity(Tester.Verbosity.PROGRESS)
self.exitCode = Tester.RunAllTestCases()
KtsMp.Tester.SetVerbosity(KtsMp.Tester.Verbosity.PROGRESS)
self.exitCode = KtsMp.Tester.RunAllTestCases()
except Exception as e:
print('[Warning]:', e, file=sys.stderr)
self.exitCode = 1
Expand Down Expand Up @@ -283,7 +288,7 @@ def main():
assert False, 'unhandled option'

# Set timeout of the different levels
signalTime = int(-1)
signalTime = None
if level == 'small':
signalTime = int(60)
elif level == 'nightly':
Expand All @@ -295,7 +300,7 @@ def main():
# KratosCore must always be runned
print('Running tests for KratosCore', file=sys.stderr)

with SupressConsoleOutput():
with KtsUt.SupressConsoleOutput():
commander.RunTestSuitInTime(
'KratosCore',
'kratos',
Expand All @@ -313,11 +318,11 @@ def main():
print('Running tests for {}'.format(application), file=sys.stderr)
sys.stderr.flush()

with SupressConsoleOutput():
with KtsUt.SupressConsoleOutput():
commander.RunTestSuitInTime(
application,
application,
KratosLoader.kratos_applications+'/',
KtsMp.KratosLoader.kratos_applications+'/',
level,
verbosity,
cmd,
Expand All @@ -328,7 +333,7 @@ def main():

# Run the cpp tests (does the same as run_cpp_tests.py)
print('Running cpp tests', file=sys.stderr)
with SupressConsoleOutput():
with KtsUt.SupressConsoleOutput():
commander.RunCppTests(applications)

exit_code = max(exit_code, commander.exitCode)
Expand Down