Salome HOME
Merge changes from 'master' branch.
[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 # Run test
30 def runTest(command):
31   print("Running:", " ".join(command))
32   p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
33   out, err = p.communicate()
34   res = p.returncode
35   # About res value:
36   # A negative value -N indicates that the child was terminated by signal N (Unix only).
37   # On Unix, the value 11 generally corresponds to a segmentation fault.
38   return res, out, err
39 #
40
41 # Display output and errors
42 def processResult(res, out, err):
43   if out:
44     print(out)
45     pass
46   if err:
47     print("    ** Detected error **")
48     print("Error code: ", res)
49     print(err, end=' ')
50     print("    ** end of message **")
51     pass
52   return res
53 #
54
55 # Timeout management
56 class TimeoutException(Exception):
57   """Exception raised when test timeout is reached."""
58 #
59 def timeoutHandler(signum, frame):
60   raise TimeoutException()
61 #
62
63 if __name__ == "__main__":
64   timeout_delay = sys.argv[1]
65   args = sys.argv[2:]
66
67   # Add explicit call to python executable if a Python script is passed as
68   # first argument
69   if not args:
70     print("Invalid arguments for salome_test_driver.py. No command defined.")
71     sys.exit(1)
72   _, ext = os.path.splitext(args[0])
73   if ext == ".py":
74     test_and_args = [sys.executable] + args
75   else:
76     test_and_args = args
77
78   # Ensure OMNIORB_USER_PATH is set
79   from salomeContextUtils import setOmniOrbUserPath
80   setOmniOrbUserPath()
81
82   # Set timeout handler
83   print("Test timeout explicitly set to: %s seconds"%timeout_delay)
84   timeout_sec = abs(int(timeout_delay)-10)
85   if sys.platform == 'win32':
86     from threading import Timer
87     timer = Timer(timeout_sec, timeoutHandler)
88     timer.start()
89   else:
90     signal.alarm(timeout_sec)
91     signal.signal(signal.SIGALRM, timeoutHandler)
92
93   # Run test in a new SALOME instance
94   from salome_instance import SalomeInstance
95   res = 1
96   try:
97     salome_instance = SalomeInstance.start(shutdown_servers=True)
98     port = salome_instance.get_port()
99     res, out, err = runTest(test_and_args)
100     res = processResult(res, out, err)
101   except TimeoutException:
102     print("FAILED : timeout(%s) is reached"%timeout_delay)
103   except:
104     import traceback
105     traceback.print_exc()
106     pass
107   try:
108     salome_instance.stop()
109     os.kill(pid, signal.SIGTERM)
110   except:
111     pass
112   if sys.platform == 'win32':
113     timer.cancel()
114   print("Exit test with status code:", res)
115   sys.exit(res)
116 #