Salome HOME
Pour résoudre un conflit entre les branches master et Windows.
[tools/sat.git] / src / test_module.py
1 #!/usr/bin/env python
2 #-*- coding:utf-8 -*-
3 #  Copyright (C) 2010-2013  CEA/DEN
4 #
5 #  This library is free software; you can redistribute it and/or
6 #  modify it under the terms of the GNU Lesser General Public
7 #  License as published by the Free Software Foundation; either
8 #  version 2.1 of the License.
9 #
10 #  This library is distributed in the hope that it will be useful,
11 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 #  Lesser General Public License for more details.
14 #
15 #  You should have received a copy of the GNU Lesser General Public
16 #  License along with this library; if not, write to the Free Software
17 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18
19 # Python 2/3 compatibility for execfile function
20 try:
21     execfile
22 except:
23     def execfile(somefile, global_vars, local_vars):
24         with open(somefile) as f:
25             code = compile(f.read(), somefile, 'exec')
26             exec(code, global_vars, local_vars)
27
28
29 import os
30 import sys
31 import datetime
32 import shutil
33 import string
34 import imp
35 import subprocess
36
37 from . import fork
38 import src
39
40 # directories not considered as test grids
41 C_IGNORE_GRIDS = ['.git', '.svn', 'RESSOURCES']
42
43 # Get directory to be used for the temporary files.
44 #
45 def getTmpDirDEFAULT():
46     if src.architecture.is_windows():
47         directory = os.getenv("TEMP")
48     else:
49         # for Linux: use /tmp/logs/{user} folder
50         directory = os.path.join( '/tmp', 'logs', os.getenv("USER", "unknown"))
51     return directory
52
53 class Test:
54     def __init__(self,
55                  config,
56                  logger,
57                  tmp_working_dir,
58                  testbase="",
59                  grids=None,
60                  sessions=None,
61                  launcher="",
62                  show_desktop=True):
63         self.grids = grids
64         self.config = config
65         self.logger = logger
66         self.tmp_working_dir = tmp_working_dir
67         self.sessions = sessions
68         self.launcher = launcher
69         self.show_desktop = show_desktop
70
71         res = self.prepare_testbase(testbase)
72         self.test_base_found = True
73         if res == 1:
74             # Fail
75             self.test_base_found = False
76         
77         self.settings = {}
78         self.known_errors = None
79
80         # create section for results
81         self.config.TESTS = src.pyconf.Sequence(self.config)
82
83         self.nb_run = 0
84         self.nb_succeed = 0
85         self.nb_timeout = 0
86         self.nb_not_run = 0
87         self.nb_acknoledge = 0
88
89     def _copy_dir(self, source, target):
90         if self.config.VARS.python >= "2.6":
91             shutil.copytree(source, target,
92                             symlinks=True,
93                             ignore=shutil.ignore_patterns('.git*','.svn*'))
94         else:
95             shutil.copytree(source, target,
96                             symlinks=True)
97
98     def prepare_testbase_from_dir(self, testbase_name, testbase_dir):
99         self.logger.write(_("get test base from dir: %s\n") % \
100                           src.printcolors.printcLabel(testbase_dir), 3)
101         if not os.access(testbase_dir, os.X_OK):
102             raise src.SatException(_("testbase %(name)s (%(dir)s) does not "
103                                      "exist ...\n") % { 'name': testbase_name,
104                                                        'dir': testbase_dir })
105
106         self._copy_dir(testbase_dir,
107                        os.path.join(self.tmp_working_dir, 'BASES', testbase_name))
108
109     def prepare_testbase_from_git(self,
110                                   testbase_name,
111                                   testbase_base,
112                                   testbase_tag):
113         self.logger.write(
114             _("get test base '%(testbase)s' with '%(tag)s' tag from git\n") % {
115                         "testbase" : src.printcolors.printcLabel(testbase_name),
116                         "tag" : src.printcolors.printcLabel(testbase_tag)},
117                           3)
118         try:
119             def set_signal(): # pragma: no cover
120                 """see http://bugs.python.org/issue1652"""
121                 import signal
122                 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
123
124             cmd = "git clone --depth 1 %(base)s %(dir)s"
125             cmd += " && cd %(dir)s"
126             if testbase_tag=='master':
127                 cmd += " && git fetch origin %(branch)s"
128             else:
129                 cmd += " && git fetch origin %(branch)s:%(branch)s"
130             cmd += " && git checkout %(branch)s"
131             cmd = cmd % { 'branch': testbase_tag,
132                          'base': testbase_base,
133                          'dir': testbase_name }
134
135             self.logger.write("> %s\n" % cmd, 5)
136             if src.architecture.is_windows():
137                 # preexec_fn not supported on windows platform
138                 res = subprocess.call(cmd,
139                                 cwd=os.path.join(self.tmp_working_dir, 'BASES'),
140                                 shell=True,
141                                 stdout=self.logger.logTxtFile,
142                                 stderr=subprocess.PIPE)
143             else:
144                 res = subprocess.call(cmd,
145                                 cwd=os.path.join(self.tmp_working_dir, 'BASES'),
146                                 shell=True,
147                                 preexec_fn=set_signal,
148                                 stdout=self.logger.logTxtFile,
149                                 stderr=subprocess.PIPE)
150             if res != 0:
151                 raise src.SatException(_("Error: unable to get test base "
152                                          "'%(name)s' from git '%(repo)s'.") % \
153                                        { 'name': testbase_name,
154                                         'repo': testbase_base })
155
156         except OSError:
157             self.logger.error(_("git is not installed. exiting...\n"))
158             sys.exit(0)
159
160     def prepare_testbase_from_svn(self, user, testbase_name, testbase_base):
161         self.logger.write(_("get test base '%s' from svn\n") % \
162                           src.printcolors.printcLabel(testbase_name), 3)
163         try:
164             def set_signal(): # pragma: no cover
165                 """see http://bugs.python.org/issue1652"""
166                 import signal
167                 signal.signal(signal.SIGPIPE, signal.SIG_DFL)
168
169             cmd = "svn checkout --username %(user)s %(base)s %(dir)s"
170             cmd = cmd % { 'user': user,
171                          'base': testbase_base,
172                          'dir': testbase_name }
173             
174             # Get the application environment
175             self.logger.write(_("Set the application environment\n"), 5)
176             env_appli = src.environment.SalomeEnviron(self.config,
177                                       src.environment.Environ(dict(os.environ)))
178             env_appli.set_application_env(self.logger)
179             
180             self.logger.write("> %s\n" % cmd, 5)
181             if src.architecture.is_windows():
182                 # preexec_fn not supported on windows platform
183                 res = subprocess.call(cmd,
184                                 cwd=os.path.join(self.tmp_working_dir, 'BASES'),
185                                 shell=True,
186                                 stdout=self.logger.logTxtFile,
187                                 stderr=subprocess.PIPE)
188             else:
189                 res = subprocess.call(cmd,
190                                 cwd=os.path.join(self.tmp_working_dir, 'BASES'),
191                                 shell=True,
192                                 preexec_fn=set_signal,
193                                 stdout=self.logger.logTxtFile,
194                                 stderr=subprocess.PIPE,
195                                 env=env_appli.environ.environ,)
196
197             if res != 0:
198                 raise src.SatException(_("Error: unable to get test base '%(nam"
199                                          "e)s' from svn '%(repo)s'.") % \
200                                        { 'name': testbase_name,
201                                         'repo': testbase_base })
202
203         except OSError:
204             self.logger.error(_("svn is not installed. exiting...\n"))
205             sys.exit(0)
206
207     ##
208     # Configure tests base.
209     def prepare_testbase(self, test_base_name):
210         src.printcolors.print_value(self.logger,
211                                     _("Test base"),
212                                     test_base_name,
213                                     3)
214         self.logger.write("\n", 3, False)
215
216         # search for the test base
217         test_base_info = None
218         for project_name in self.config.PROJECTS.projects:
219             project_info = self.config.PROJECTS.projects[project_name]
220             if "test_bases" not in project_info:
221                 continue
222             for t_b_info in project_info.test_bases:
223                 if t_b_info.name == test_base_name:
224                     test_base_info = t_b_info
225         
226         if not test_base_info:
227             if os.path.exists(test_base_name):
228                 self.prepare_testbase_from_dir("DIR", test_base_name)
229                 self.currentTestBase = "DIR"
230                 return 0
231         
232         if not test_base_info:
233             message = (_("########## ERROR: test base '%s' not found\n") % 
234                        test_base_name)
235             self.logger.write("%s\n" % src.printcolors.printcError(message))
236             return 1
237
238         if test_base_info.get_sources == "dir":
239             self.prepare_testbase_from_dir(test_base_name,
240                                            test_base_info.info.dir)
241         elif test_base_info.get_sources == "git":
242             self.prepare_testbase_from_git(test_base_name,
243                                        test_base_info.info.base,
244                                        self.config.APPLICATION.test_base.tag)
245         elif test_base_info.get_sources == "svn":
246             svn_user = src.get_cfg_param(test_base_info.info,
247                                          "svn_user",
248                                          self.config.USER.svn_user)
249             self.prepare_testbase_from_svn(svn_user,
250                                        test_base_name,
251                                        test_base_info.info.base)
252         else:
253             raise src.SatException(_("unknown source type '%(type)s' for test b"
254                                      "ase '%(base)s' ...\n") % {
255                                         'type': test_base_info.get_sources,
256                                         'base': test_base_name })
257
258         self.currentTestBase = test_base_name
259
260     ##
261     # Searches if the script is declared in known errors pyconf.
262     # Update the status if needed.
263     def search_known_errors(self, status, test_grid, test_session, test):
264         test_path = os.path.join(test_grid, test_session, test)
265         if not src.config_has_application(self.config):
266             return status, []
267
268         if self.known_errors is None:
269             return status, []
270
271         platform = self.config.VARS.arch
272         application = self.config.VARS.application
273         error = self.known_errors.get_error(test_path, application, platform)
274         if error is None:
275             return status, []
276         
277         if status == src.OK_STATUS:
278             if not error.fixed:
279                 # the error is fixed
280                 self.known_errors.fix_error(error)
281                 #import testerror
282                 #testerror.write_test_failures(
283                 #                        self.config.TOOLS.testerror.file_path,
284                 #                        self.known_errors.errors)
285             return status, [ error.date,
286                             error.expected,
287                             error.comment,
288                             error.fixed ]
289
290         if error.fixed:
291             self.known_errors.unfix_error(error)
292             #import testerror
293             #testerror.write_test_failures(self.config.TOOLS.testerror.file_path,
294             #                              self.known_errors.errors)
295
296         delta = self.known_errors.get_expecting_days(error)
297         kfres = [ error.date, error.expected, error.comment, error.fixed ]
298         if delta < 0:
299             return src.KO_STATUS, kfres
300         return src.KNOWNFAILURE_STATUS, kfres
301
302     ##
303     # Read the *.result.py files.
304     def read_results(self, listTest, has_timed_out):
305         results = {}
306         for test in listTest:
307             resfile = os.path.join(self.currentDir,
308                                    self.currentgrid,
309                                    self.currentsession,
310                                    test[:-3] + ".result.py")
311
312             # check if <test>.result.py file exists
313             if not os.path.exists(resfile):
314                 results[test] = ["?", -1, "", []]
315             else:
316                 gdic, ldic = {}, {}
317                 execfile(resfile, gdic, ldic)
318
319                 status = src.TIMEOUT_STATUS
320                 if not has_timed_out:
321                     status = src.KO_STATUS
322
323                 if ldic.has_key('status'):
324                     status = ldic['status']
325
326                 expected = []
327                 if status == src.KO_STATUS or status == src.OK_STATUS:
328                     status, expected = self.search_known_errors(status,
329                                                             self.currentgrid,
330                                                             self.currentsession,
331                                                             test)
332
333                 callback = ""
334                 if ldic.has_key('callback'):
335                     callback = ldic['callback']
336                 elif status == src.KO_STATUS:
337                     callback = "CRASH"
338
339                 exec_time = -1
340                 if ldic.has_key('time'):
341                     try:
342                         exec_time = float(ldic['time'])
343                     except:
344                         pass
345
346                 results[test] = [status, exec_time, callback, expected]
347             
348             # check if <test>.py file exists
349             testfile = os.path.join(self.currentDir,
350                                    self.currentgrid,
351                                    self.currentsession,
352                                    test)
353             
354             if not os.path.exists(testfile):
355                 results[test].append('')
356             else:
357                 text = open(testfile, "r").read()
358                 results[test].append(text)
359
360             # check if <test>.out.py file exists
361             outfile = os.path.join(self.currentDir,
362                                    self.currentgrid,
363                                    self.currentsession,
364                                    test[:-3] + ".out.py")
365             
366             if not os.path.exists(outfile):
367                 results[test].append('')
368             else:
369                 text = open(outfile, "r").read()
370                 results[test].append(text)
371
372         return results
373
374     ##
375     # Generates the script to be run by Salome.
376     # This python script includes init and close statements and a loop
377     # calling all the scripts of a single directory.
378     def generate_script(self, listTest, script_path, ignoreList):
379         # open template file
380         template_file = open(os.path.join(self.config.VARS.srcDir,
381                                           "test",
382                                           "scriptTemplate.py"), 'r')
383         template = string.Template(template_file.read())
384         
385         # create substitution dictionary
386         d = dict()
387         d['resourcesWay'] = os.path.join(self.currentDir, 'RESSOURCES')
388         d['tmpDir'] = os.path.join(self.tmp_working_dir, 'WORK')
389         d['toolsWay'] = os.path.join(self.config.VARS.srcDir, "test")
390         d['sessionDir'] = os.path.join(self.currentDir,
391                                     self.currentgrid,
392                                     self.currentsession)
393         d['resultFile'] = os.path.join(self.tmp_working_dir,
394                                        'WORK',
395                                        'exec_result')
396         d['listTest'] = listTest
397         d['sessionName'] = self.currentsession
398         d['ignore'] = ignoreList
399
400         # create script with template
401         script = open(script_path, 'w')
402         script.write(template.safe_substitute(d))
403         script.close()
404
405     # Find the getTmpDir function that gives access to *pidict file directory.
406     # (the *pidict file exists when SALOME is launched) 
407     def get_tmp_dir(self):
408         # Rare case where there is no KERNEL in grid list 
409         # (for example MED_STANDALONE)
410         if ('APPLICATION' in self.config 
411                 and 'KERNEL' not in self.config.APPLICATION.products 
412                 and 'KERNEL_ROOT_DIR' not in os.environ):
413             return getTmpDirDEFAULT
414         
415         # Case where "sat test" is launched in an existing SALOME environment
416         if 'KERNEL_ROOT_DIR' in os.environ:
417             root_dir =  os.environ['KERNEL_ROOT_DIR']
418         
419         if ('APPLICATION' in self.config 
420                 and 'KERNEL' in self.config.APPLICATION.products):
421             root_dir = src.product.get_product_config(self.config,
422                                                       "KERNEL").install_dir
423
424         # Case where there the appli option is called (with path to launcher)
425         if len(self.launcher) > 0:
426             # There are two cases : The old application (runAppli) 
427             # and the new one
428             launcherName = os.path.basename(self.launcher)
429             launcherDir = os.path.dirname(self.launcher)
430             if launcherName == 'runAppli':
431                 # Old application
432                 cmd = "for i in " + launcherDir + "/env.d/*.sh; do source ${i};"
433                 " done ; echo $KERNEL_ROOT_DIR"
434             else:
435                 # New application
436                 cmd = ("echo -e 'import os\nprint os.environ[\"KERNEL_" + 
437                        "ROOT_DIR\"]' > tmpscript.py; %s shell" + 
438                        " tmpscript.py") % self.launcher
439             root_dir = subprocess.Popen(cmd,
440                             stdout=subprocess.PIPE,
441                             shell=True,
442                             executable='/bin/bash').communicate()[0].split()[-1]
443         
444         # import grid salome_utils from KERNEL that gives 
445         # the right getTmpDir function
446         
447         (file_, pathname, description) = imp.find_module("salome_utils",
448                                                          [os.path.join(root_dir,
449                                                                     'bin',
450                                                                     'salome')])
451         try:
452             grid = imp.load_module("salome_utils",
453                                      file_,
454                                      pathname,
455                                      description)
456             return grid.getLogDir
457         except:
458             grid = imp.load_module("salome_utils",
459                                      file_,
460                                      pathname,
461                                      description)
462             return grid.getTmpDir
463         finally:
464             if file_:
465                 file_.close()
466
467
468     def get_test_timeout(self, test_name, default_value):
469         if ("timeout" in self.settings and 
470                 test_name in self.settings["timeout"]):
471             return self.settings["timeout"][test_name]
472
473         return default_value
474
475     def generate_launching_commands(self):
476         # Case where "sat test" is launched in an existing SALOME environment
477         if 'KERNEL_ROOT_DIR' in os.environ:
478             binSalome = "runSalome"
479             binPython = "python"
480             killSalome = "killSalome.py"
481         
482         # Rare case where there is no KERNEL in grid list 
483         # (for example MED_STANDALONE)
484         if ('APPLICATION' in self.config and 
485                 'KERNEL' not in self.config.APPLICATION.products):
486             binSalome = "runSalome"
487             binPython = "python" 
488             killSalome = "killSalome.py"   
489             src.environment.load_environment(self.config, False, self.logger)         
490             return binSalome, binPython, killSalome
491         
492         # Case where there the appli option is called (with path to launcher)
493         if len(self.launcher) > 0:
494             # There are two cases : The old application (runAppli) 
495             # and the new one
496             launcherName = os.path.basename(self.launcher)
497             launcherDir = os.path.dirname(self.launcher)
498             if launcherName == 'runAppli':
499                 # Old application
500                 binSalome = self.launcher
501                 binPython = ("for i in " +
502                              launcherDir +
503                              "/env.d/*.sh; do source ${i}; done ; python")
504                 killSalome = ("for i in " +
505                         launcherDir +
506                         "/env.d/*.sh; do source ${i}; done ; killSalome.py'")
507                 return binSalome, binPython, killSalome
508             else:
509                 # New application
510                 binSalome = self.launcher
511                 binPython = self.launcher + ' shell'
512                 killSalome = self.launcher + ' killall'
513                 return binSalome, binPython, killSalome
514
515         # SALOME version detection and APPLI repository detection
516         VersionSalome = src.get_salome_version(self.config)
517         appdir = 'APPLI'
518         if "APPLI" in self.config and "application_name" in self.config.APPLI:
519             appdir = self.config.APPLI.application_name
520         
521         # Case where SALOME has NOT the launcher that uses the SalomeContext API
522         if VersionSalome < 730:
523             binSalome = os.path.join(self.config.APPLICATION.workdir,
524                                      appdir,
525                                      "runAppli")
526             binPython = "python"
527             killSalome = "killSalome.py"
528             src.environment.load_environment(self.config, False, self.logger)           
529             return binSalome, binPython, killSalome
530         
531         # Case where SALOME has the launcher that uses the SalomeContext API
532         if VersionSalome >= 730:            
533             if 'profile' not in self.config.APPLICATION:
534                 # Before revision of application concept
535                 launcher_name = self.config.APPLI.launch_alias_name
536                 binSalome = os.path.join(self.config.APPLICATION.workdir,
537                                          appdir,
538                                          launcher_name)
539             else:
540                 # After revision of application concept
541                 launcher_name = self.config.APPLICATION.profile.launcher_name
542                 binSalome = os.path.join(self.config.APPLICATION.workdir,
543                                          launcher_name)
544             
545             if src.architecture.is_windows():
546                 binSalome += '.bat'
547
548             binPython = binSalome + ' shell'
549             killSalome = binSalome + ' killall'
550             return binSalome, binPython, killSalome
551                 
552         return binSalome, binPython, killSalome
553         
554
555     ##
556     # Runs tests of a session (using a single instance of Salome).
557     def run_tests(self, listTest, ignoreList):
558         out_path = os.path.join(self.currentDir,
559                                 self.currentgrid,
560                                 self.currentsession)
561         sessionname = "%s/%s" % (self.currentgrid, self.currentsession)
562         time_out = self.get_test_timeout(sessionname,
563                                          self.config.SITE.test.timeout)
564
565         time_out_salome = src.get_cfg_param(self.config.SITE.test,
566                                             "timeout_app",
567                                             self.config.SITE.test.timeout)
568
569         # generate wrapper script
570         script_path = os.path.join(out_path, 'wrapperScript.py')
571         self.generate_script(listTest, script_path, ignoreList)
572
573         tmpDir = self.get_tmp_dir()
574
575         binSalome, binPython, killSalome = self.generate_launching_commands()
576         if self.settings.has_key("run_with_grids") \
577            and self.settings["run_with_grids"].has_key(sessionname):
578             binSalome = (binSalome +
579                          " -m %s" % self.settings["run_with_grids"][sessionname])
580
581         logWay = os.path.join(self.tmp_working_dir, "WORK", "log_cxx")
582
583         status = False
584         elapsed = -1
585         if self.currentsession.startswith("NOGUI_"):
586             # runSalome -t (bash)
587             status, elapsed = fork.batch(binSalome, self.logger,
588                                         os.path.join(self.tmp_working_dir,
589                                                      "WORK"),
590                                         [ "-t",
591                                          "--shutdown-server=1",
592                                          script_path ],
593                                         delai=time_out,
594                                         log=logWay)
595
596         elif self.currentsession.startswith("PY_"):
597             # python script.py
598             status, elapsed = fork.batch(binPython, self.logger,
599                                           os.path.join(self.tmp_working_dir,
600                                                        "WORK"),
601                                           [script_path],
602                                           delai=time_out, log=logWay)
603
604         else:
605             opt = "-z 0"
606             if self.show_desktop: opt = "--show-desktop=0"
607             status, elapsed = fork.batch_salome(binSalome,
608                                                  self.logger,
609                                                  os.path.join(
610                                                         self.tmp_working_dir,
611                                                         "WORK"),
612                                                  [ opt,
613                                                   "--shutdown-server=1",
614                                                   script_path ],
615                                                  getTmpDir=tmpDir,
616                                                  fin=killSalome,
617                                                  delai=time_out,
618                                                  log=logWay,
619                                                  delaiapp=time_out_salome)
620
621         self.logger.write("status = %s, elapsed = %s\n" % (status, elapsed),
622                           5)
623
624         # create the test result to add in the config object
625         test_info = src.pyconf.Mapping(self.config)
626         test_info.testbase = self.currentTestBase
627         test_info.grid = self.currentgrid
628         test_info.session = self.currentsession
629         test_info.script = src.pyconf.Sequence(self.config)
630
631         script_results = self.read_results(listTest, elapsed == time_out)
632         for sr in sorted(script_results.keys()):
633             self.nb_run += 1
634
635             # create script result
636             script_info = src.pyconf.Mapping(self.config)
637             script_info.name = sr
638             script_info.res = script_results[sr][0]
639             script_info.time = script_results[sr][1]
640             if script_info.res == src.TIMEOUT_STATUS:
641                 script_info.time = time_out
642             if script_info.time < 1e-3: script_info.time = 0
643
644             callback = script_results[sr][2]
645             if script_info.res != src.OK_STATUS and len(callback) > 0:
646                 script_info.callback = callback
647
648             kfres = script_results[sr][3]
649             if len(kfres) > 0:
650                 script_info.known_error = src.pyconf.Mapping(self.config)
651                 script_info.known_error.date = kfres[0]
652                 script_info.known_error.expected = kfres[1]
653                 script_info.known_error.comment = kfres[2]
654                 script_info.known_error.fixed = kfres[3]
655             
656             script_info.content = script_results[sr][4]
657             script_info.out = script_results[sr][5]
658             
659             # add it to the list of results
660             test_info.script.append(script_info, '')
661
662             # display the results
663             if script_info.time > 0:
664                 exectime = "(%7.3f s)" % script_info.time
665             else:
666                 exectime = ""
667
668             sp = "." * (35 - len(script_info.name))
669             self.logger.write(self.write_test_margin(3), 3)
670             self.logger.write("script %s %s %s %s\n" % (
671                                 src.printcolors.printcLabel(script_info.name),
672                                 sp,
673                                 src.printcolors.printc(script_info.res),
674                                 exectime), 3, False)
675             if script_info and len(callback) > 0:
676                 self.logger.write("Exception in %s\n%s\n" % \
677                     (script_info.name,
678                      src.printcolors.printcWarning(callback)), 2, False)
679
680             if script_info.res == src.OK_STATUS:
681                 self.nb_succeed += 1
682             elif script_info.res == src.KNOWNFAILURE_STATUS:
683                 self.nb_acknoledge += 1
684             elif script_info.res == src.TIMEOUT_STATUS:
685                 self.nb_timeout += 1
686             elif script_info.res == src.NA_STATUS:
687                 self.nb_run -= 1
688             elif script_info.res == "?":
689                 self.nb_not_run += 1
690
691         self.config.TESTS.append(test_info, '')
692
693     ##
694     # Runs all tests of a session.
695     def run_session_tests(self):
696        
697         self.logger.write(self.write_test_margin(2), 3)
698         self.logger.write("Session = %s\n" % src.printcolors.printcLabel(
699                                                     self.currentsession), 3, False)
700
701         # prepare list of tests to run
702         tests = os.listdir(os.path.join(self.currentDir,
703                                         self.currentgrid,
704                                         self.currentsession))
705         tests = filter(lambda l: l.endswith(".py"), tests)
706         tests = sorted(tests, key=str.lower)
707
708         # build list of known failures
709         cat = "%s/%s/" % (self.currentgrid, self.currentsession)
710         ignoreDict = {}
711         for k in self.ignore_tests.keys():
712             if k.startswith(cat):
713                 ignoreDict[k[len(cat):]] = self.ignore_tests[k]
714
715         self.run_tests(tests, ignoreDict)
716
717     ##
718     # Runs all tests of a grid.
719     def run_grid_tests(self):
720         self.logger.write(self.write_test_margin(1), 3)
721         self.logger.write("grid = %s\n" % src.printcolors.printcLabel(
722                                                 self.currentgrid), 3, False)
723
724         grid_path = os.path.join(self.currentDir, self.currentgrid)
725
726         sessions = []
727         if self.sessions is not None:
728             sessions = self.sessions # user choice
729         else:
730             # use all scripts in grid
731             sessions = filter(lambda l: l not in C_IGNORE_GRIDS,
732                            os.listdir(grid_path))
733             sessions = filter(lambda l: os.path.isdir(os.path.join(grid_path,
734                                                                 l)), sessions)
735
736         sessions = sorted(sessions, key=str.lower)
737         for session_ in sessions:
738             if not os.path.exists(os.path.join(grid_path, session_)):
739                 self.logger.write(self.write_test_margin(2), 3)
740                 self.logger.write(src.printcolors.printcWarning("Session %s not"
741                                         " found" % session_) + "\n", 3, False)
742             else:
743                 self.currentsession = session_
744                 self.run_session_tests()
745
746     ##
747     # Runs test testbase.
748     def run_testbase_tests(self):
749         res_dir = os.path.join(self.currentDir, "RESSOURCES")
750         os.environ['PYTHONPATH'] =  (res_dir + 
751                                      os.pathsep + 
752                                      os.environ['PYTHONPATH'])
753         os.environ['TT_BASE_RESSOURCES'] = res_dir
754         src.printcolors.print_value(self.logger,
755                                     "TT_BASE_RESSOURCES",
756                                     res_dir,
757                                     4)
758         self.logger.write("\n", 4, False)
759
760         self.logger.write(self.write_test_margin(0), 3)
761         testbase_label = "Test base = %s\n" % src.printcolors.printcLabel(
762                                                         self.currentTestBase)
763         self.logger.write(testbase_label, 3, False)
764         self.logger.write("-" * len(src.printcolors.cleancolor(testbase_label)),
765                           3)
766         self.logger.write("\n", 3, False)
767
768         # load settings
769         settings_file = os.path.join(res_dir, "test_settings.py")
770         if os.path.exists(settings_file):
771             gdic, ldic = {}, {}
772             execfile(settings_file, gdic, ldic)
773             self.logger.write(_("Load test settings\n"), 3)
774             self.settings = ldic['settings_dic']
775             self.ignore_tests = ldic['known_failures_list']
776             if isinstance(self.ignore_tests, list):
777                 self.ignore_tests = {}
778                 self.logger.write(src.printcolors.printcWarning("known_failur"
779                   "es_list must be a dictionary (not a list)") + "\n", 1, False)
780         else:
781             self.ignore_tests = {}
782             self.settings.clear()
783
784         # read known failures pyconf
785         if "testerror" in self.config.SITE:
786             #import testerror
787             #self.known_errors = testerror.read_test_failures(
788             #                            self.config.TOOLS.testerror.file_path,
789             #                            do_error=False)
790             pass
791         else:
792             self.known_errors = None
793
794         if self.grids is not None:
795             grids = self.grids # given by user
796         else:
797             # select all the grids (i.e. directories) in the directory
798             grids = filter(lambda l: l not in C_IGNORE_GRIDS,
799                              os.listdir(self.currentDir))
800             grids = filter(lambda l: os.path.isdir(
801                                         os.path.join(self.currentDir, l)),
802                              grids)
803
804         grids = sorted(grids, key=str.lower)
805         for grid in grids:
806             if not os.path.exists(os.path.join(self.currentDir, grid)):
807                 self.logger.write(self.write_test_margin(1), 3)
808                 self.logger.write(src.printcolors.printcWarning(
809                             "grid %s does not exist\n" % grid), 3, False)
810             else:
811                 self.currentgrid = grid
812                 self.run_grid_tests()
813
814     def run_script(self, script_name):
815         if ('APPLICATION' in self.config and 
816                 script_name in self.config.APPLICATION):
817             script = self.config.APPLICATION[script_name]
818             if len(script) == 0:
819                 return
820
821             self.logger.write("\n", 2, False)
822             if not os.path.exists(script):
823                 self.logger.write(src.printcolors.printcWarning("WARNING: scrip"
824                                         "t not found: %s" % script) + "\n", 2)
825             else:
826                 self.logger.write(src.printcolors.printcHeader("----------- sta"
827                                             "rt %s" % script_name) + "\n", 2)
828                 self.logger.write("Run script: %s\n" % script, 2)
829                 subprocess.Popen(script, shell=True).wait()
830                 self.logger.write(src.printcolors.printcHeader("----------- end"
831                                                 " %s" % script_name) + "\n", 2)
832
833     def run_all_tests(self):
834         initTime = datetime.datetime.now()
835
836         self.run_script('test_setup')
837         self.logger.write("\n", 2, False)
838
839         self.logger.write(src.printcolors.printcHeader(
840                                             _("=== STARTING TESTS")) + "\n", 2)
841         self.logger.write("\n", 2, False)
842         self.currentDir = os.path.join(self.tmp_working_dir,
843                                        'BASES',
844                                        self.currentTestBase)
845         self.run_testbase_tests()
846
847         # calculate total execution time
848         totalTime = datetime.datetime.now() - initTime
849         totalTime -= datetime.timedelta(microseconds=totalTime.microseconds)
850         self.logger.write("\n", 2, False)
851         self.logger.write(src.printcolors.printcHeader(_("=== END TESTS")), 2)
852         self.logger.write(" %s\n" % src.printcolors.printcInfo(str(totalTime)),
853                           2,
854                           False)
855
856         #
857         # Start the tests
858         #
859         self.run_script('test_cleanup')
860         self.logger.write("\n", 2, False)
861
862         # evaluate results
863         res_count = "%d / %d" % (self.nb_succeed,
864                                  self.nb_run - self.nb_acknoledge)
865
866         res_out = _("Tests Results: %(succeed)d / %(total)d\n") % \
867             { 'succeed': self.nb_succeed, 'total': self.nb_run }
868         if self.nb_succeed == self.nb_run:
869             res_out = src.printcolors.printcSuccess(res_out)
870         else:
871             res_out = src.printcolors.printcError(res_out)
872         self.logger.write(res_out, 1)
873
874         if self.nb_timeout > 0:
875             self.logger.write(_("%d tests TIMEOUT\n") % self.nb_timeout, 1)
876             res_count += " TO: %d" % self.nb_timeout
877         if self.nb_not_run > 0:
878             self.logger.write(_("%d tests not executed\n") % self.nb_not_run, 1)
879             res_count += " NR: %d" % self.nb_not_run
880
881         status = src.OK_STATUS
882         if self.nb_run - self.nb_succeed - self.nb_acknoledge > 0:
883             status = src.KO_STATUS
884         elif self.nb_acknoledge:
885             status = src.KNOWNFAILURE_STATUS
886         
887         self.logger.write(_("Status: %s\n" % status), 3)
888
889         return self.nb_run - self.nb_succeed - self.nb_acknoledge
890
891     ##
892     # Write margin to show test results.
893     def write_test_margin(self, tab):
894         if tab == 0:
895             return ""
896         return "|   " * (tab - 1) + "+ "
897