Salome HOME
Merge branch 'agr/connect'
[modules/kernel.git] / bin / appliskel / salome_tester / salome_test_driver.py
1 # Copyright (C) 2015  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_helper.py <timeout_delay> <test_file.py> [test file 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 def processResultSpecialParavis(res, out, err):
42   # :TRICKY: Special case of returncode=127
43   # When using paraview in SALOME environment, the following error
44   # systematically appears when exiting paraview (it's also true when using
45   # PARAVIS and exiting SALOME):
46   # Inconsistency detected by ld.so: dl-close.c: 738: _dl_close: Assertion `map->l_init_called' failed!
47   # For PARAVIS tests purpose, paraview functionalities are accessed in each
48   # test; these tests are run in the above subprocess call.
49   # The assertion error implies a subprocess return code of 127, and the test
50   # status is considered as "failed".
51   # The tricky part here is to discard such return codes, waiting for a fix
52   # maybe in paraview...
53   if res == 127 and err.startswith("Inconsistency detected by ld.so: dl-close.c"):
54       print "    ** THE FOLLOWING MESSAGE IS DISCARDED WHEN ANALYZING TEST SUCCESSFULNESS **"
55       print err,
56       print "    ** end of message **"
57       res = 0
58   elif err:
59       print "    ** Detected error **"
60       print "Error code: ", res
61       print err,
62       print "    ** end of message **"
63       pass
64
65   if out:
66       print out
67   return res
68 #
69
70 # Display output and errors
71 def processResult(res, out, err):
72   if out:
73     print out
74     pass
75   if err:
76     print err
77   print "Status code: ", res
78   return res
79 #
80
81 # Timeout management
82 class TimeoutException(Exception):
83   """Execption raised when test timeout is reached."""
84 #
85 def timeoutHandler(signum, frame):
86   raise TimeoutException()
87 #
88
89 if __name__ == "__main__":
90   timeout_delay = sys.argv[1]
91   args = sys.argv[2:]
92
93   # Add explicit call to python executable if a Python script is passed as
94   # first argument
95   if not args:
96     print "Invalid arguments for salome_test_helper.py. No command defined."
97     exit(1)
98   _, ext = os.path.splitext(args[0])
99   if ext == ".py":
100     test_and_args = [sys.executable] + args
101   else:
102     test_and_args = args
103
104   # Ensure OMNIORB_USER_PATH is set
105   from salomeContextUtils import setOmniOrbUserPath
106   setOmniOrbUserPath()
107
108   # Set timeout handler
109   print "Test timeout explicitely set to: %s seconds"%timeout_delay
110   signal.alarm(abs(int(timeout_delay)-10))
111   signal.signal(signal.SIGALRM, timeoutHandler)
112
113   # Run test in a new SALOME instance
114   from salome_instance import SalomeInstance
115   res = 1
116   try:
117     salome_instance = SalomeInstance.start(shutdown_servers=True)
118     port = salome_instance.get_port()
119     res, out, err = runTest(test_and_args)
120     #res = processResult(res, out, err)
121     res = processResultSpecialParavis(res, out, err)
122   except TimeoutException:
123     print "FAILED : timeout(%s) is reached"%timeout_delay
124   except:
125     import traceback
126     traceback.print_exc()
127     pass
128
129   salome_instance.stop()
130   print "Exit test with status code:", res
131   exit(res)
132 #