Salome HOME
Merge branch 'gdd_env_modules_in_config_appli' into gdd/merge_gdd_env_modules_in_conf...
[modules/kernel.git] / bin / appliskel / salome_tester / salome_test_driver.py
1 # Copyright (C) 2015-2017  CEA/DEN, EDF R&D, OPEN CASCADE
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 """
21 Usage: salome_test_driver.py <timeout_delay> <test command> [test command arguments]
22 """
23
24 import sys
25 import os
26 import subprocess
27 import signal
28
29 # Timeout management
30 class TimeoutException(Exception):
31   """Exception raised when test timeout is reached."""
32 #
33 def timeoutHandler(signum, frame):
34   raise TimeoutException()
35 #
36
37 if __name__ == "__main__":
38   timeout_delay = sys.argv[1]
39   args = sys.argv[2:]
40
41   # Add explicit call to python executable if a Python script is passed as
42   # first argument
43   if not args:
44     print "Invalid arguments for salome_test_driver.py. No command defined."
45     sys.exit(1)
46   _, ext = os.path.splitext(args[0])
47   if ext == ".py":
48     test_and_args = [sys.executable] + args
49   else:
50     test_and_args = args
51
52   # Ensure OMNIORB_USER_PATH is set
53   from salomeContextUtils import setOmniOrbUserPath
54   setOmniOrbUserPath()
55
56   # Set timeout handler
57   print "Test timeout explicitely set to: %s seconds"%timeout_delay
58   timeout_sec = abs(int(timeout_delay)-10)
59   if sys.platform == 'win32':
60     from threading import Timer
61     timer = Timer(timeout_sec, timeoutHandler)
62     timer.start()
63   else:
64     signal.alarm(timeout_sec)
65     signal.signal(signal.SIGALRM, timeoutHandler)
66
67   # Run test in a new SALOME instance
68   from salome_instance import SalomeInstance
69   res = 1
70   try:
71     salome_instance = SalomeInstance.start(shutdown_servers=True)
72     port = salome_instance.get_port()
73     # Run the test
74     print "Running:", " ".join(test_and_args)
75     p = subprocess.Popen(test_and_args)
76     pid = p.pid
77     p.communicate()
78     res = p.returncode
79     # About res value:
80     # A negative value -N indicates that the child was terminated by signal N (Unix only).
81     # On Unix, the value 11 generally corresponds to a segmentation fault.
82   except TimeoutException:
83     print "FAILED : timeout(%s) is reached"%timeout_delay
84   except:
85     import traceback
86     traceback.print_exc()
87     pass
88   try:
89     salome_instance.stop()
90     os.kill(pid, signal.SIGTERM)
91   except:
92     pass
93   if sys.platform == 'win32':
94     timer.cancel()
95   print "Exit test with status code:", res
96   sys.exit(res)
97 #