1 # Copyright (c) 2012 OPEN CASCADE SAS
3 # The content of this file is subject to the Open CASCADE Technology Public
4 # License Version 6.5 (the "License"). You may not use the content of this file
5 # except in compliance with the License. Please obtain a copy of the License
6 # at http://www.opencascade.org and read it completely before using this file.
8 # The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 # main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 # The Original Code and all software distributed under the License is
12 # distributed on an "AS IS" basis, without warranty of any kind, and the
13 # Initial Developer hereby disclaims all such warranties, including without
14 # limitation, any warranties of merchantability, fitness for a particular
15 # purpose or non-infringement. Please see the License for the specific terms
16 # and conditions governing the rights and limitations under the License.
18 ############################################################################
19 # This file defines scripts for execution of OCCT tests.
20 # It should be loaded automatically when DRAW is started, and provides
21 # three top-level commands: 'test', 'testgrid', and 'testdiff'.
22 # See OCCT Tests User Guide for description of the test system.
24 # Note: procedures with names starting with underscore are for internal use
25 # inside the test system.
26 ############################################################################
28 # Default verbose level for command _run_test
31 # regexp for parsing test case results in summary log
32 set _test_case_regexp {^CASE\s+([\w.-]+)\s+([\w.-]+)\s+([\w.-]+)\s*:\s*([\w]+)(.*)}
34 # Basic command to run indicated test case in DRAW
35 help test {Run specified test case
36 Use: test group grid casename [echo=0]
37 - If echo is set to 0 (default), log is stored in memory and only summary
38 is output (the log can be obtained with command \'dlog get\')
39 - If echo is set to 1, all commands and results are echoed immediately,
40 thus log is not saved and summary is not produced}
41 proc test {group grid casename {echo 0}} {
42 # get test case paths (will raise error if input is invalid)
43 _get_test $group $grid $casename dir gridname casefile
46 uplevel _run_test $dir $group $gridname $casefile $echo
50 _check_log $dir $group $gridname $casename [dlog get]
56 # Basic command to run indicated test case in DRAW
57 help testgrid {Run all tests, or specified group, or one grid
58 Use: testgrid logdir [group [grid]] [options...]
59 Log directory should be empty (or non-existing)
61 -parallel N: run in parallel mode with up to N processes (default 0)
62 -refresh N: save summary logs every N seconds (default 60, minimal 1, 0 to disable)
63 -overwrite: force writing logs in existing non-empty directory
64 -xml filename: write XML report for Jenkins (in JUnit-like format)
66 proc testgrid {logdir args} {
67 global env tcl_platform _tests_verbose
69 ######################################################
71 ######################################################
73 # check that environment variable defining paths to test scripts is defined
74 if { ! [info exists env(CSF_TestScriptsPath)] ||
75 [llength $env(CSF_TestScriptsPath)] <= 0 } {
76 error "Error: Environment variable CSF_TestScriptsPath is not defined"
84 for {set narg 0} {$narg < [llength $args]} {incr narg} {
85 set arg [lindex $args $narg]
88 if { $arg == "-parallel" } {
90 if { $narg < [llength $args] } {
91 set parallel [expr [lindex $args $narg]]
99 if { $arg == "-refresh" } {
101 if { $narg < [llength $args] } {
102 set refresh [expr [lindex $args $narg]]
109 # allow overwrite logs
110 if { $arg == "-overwrite" } {
116 if { $arg == "-xml" } {
118 if { $narg < [llength $args] } {
119 set xmlfile [lindex $args $narg]
121 if { $xmlfile == "" } {
122 set xmlfile TESTS-summary.xml
128 if { [regexp {^-} $arg] } {
129 error "Error: unsupported option \"$arg\""
132 # treat arguments not recognized as options as group and grid names
133 if { ! [info exists groupname] } {
135 } elseif { ! [info exists gridname] } {
138 error "Error: cannot interpret argument $narg ($arg): both group and grid names are already defined by previous args!"
142 # check that target log directory is empty or does not exist
143 set logdir [file normalize [string trim $logdir]]
144 if { $logdir == "" } {
145 # if specified logdir is empty string, generate unique name like "results_2010-12-31T23:59:59"
146 set logdir "results_[clock format [clock seconds] -format {%Y-%m-%dT%H%M}]"
147 set logdir [file normalize $logdir]
149 if { [file isdirectory $logdir] && ! $overwrite && ! [catch {glob -directory $logdir *}] } {
150 error "Error: Specified log directory \"$logdir\" is not empty; please clean it before running tests"
152 if { [catch {file mkdir $logdir}] || ! [file writable $logdir] } {
153 error "Error: Cannot create directory \"$logdir\", or it is not writable"
156 ######################################################
157 # prepare list of tests to be performed
158 ######################################################
160 # list of tests, each defined by a list of:
161 # test scripts directory
162 # group (subfolder) name
163 # grid (subfolder) name
165 # path to test case file
168 # iterate by all script paths
169 foreach dir [_split_path $env(CSF_TestScriptsPath)] {
170 # protection against empty paths
171 set dir [string trim $dir]
172 if { $dir == "" } { continue }
174 if { $_tests_verbose > 0 } { _log_and_puts log "Examining tests directory $dir" }
176 # check that directory exists
177 if { ! [file isdirectory $dir] } {
178 _log_and_puts log "Warning: directory $dir listed in CSF_TestScriptsPath does not exist, skipped"
182 # if test group is specified, check that directory with given name exists in this dir
183 # if not, continue to the next test dir
184 if { [info exists groupname] && $groupname != "" } {
185 if { [file isdirectory $dir/$groupname] } {
186 set groups $groupname
191 # else search all directories in the current dir
192 if [catch {glob -directory $dir -tail -types d *} groups] { continue }
196 if { $_tests_verbose > 0 } { _log_and_puts log "Groups to be executed: $groups" }
197 foreach group [lsort -dictionary $groups] {
198 if { $_tests_verbose > 0 } { _log_and_puts log "Examining group directory $group" }
200 # file grids.list must exist: it defines sequence of grids in the group
201 if { ! [file exists $dir/$group/grids.list] } {
202 _log_and_puts log "Warning: directory $dir/$group does not contain file grids.list, skipped"
206 # read grids.list file and make a list of grids to be executed
208 set fd [open $dir/$group/grids.list]
210 while { [gets $fd line] >= 0 } {
213 # skip comments and empty lines
214 if { [regexp "\[ \t\]*\#.*" $line] } { continue }
215 if { [string trim $line] == "" } { continue }
217 # get grid id and name
218 if { ! [regexp "^\(\[0-9\]+\)\[ \t\]*\(\[A-Za-z0-9_.-\]+\)\$" $line res gridid grid] } {
219 _log_and_puts log "Warning: cannot recognize line $nline in file $dir/$group/grids.list as \"gridid gridname\"; ignored"
223 # if specific grid is requested, check that it is present; otherwise make complete list
224 if { ! [info exists gridname] || $gridname == "" || $gridname == $gridid || $gridname == $grid } {
225 lappend gridlist $grid
230 # iterate by all grids
231 foreach grid $gridlist {
233 # check if this grid is aliased to another one
234 set griddir $dir/$group/$grid
235 if { [file exists $griddir/cases.list] } {
236 set fd [open $griddir/cases.list]
237 if { [gets $fd line] >= 0 } {
238 set griddir [file normalize $dir/$group/$grid/[string trim $line]]
243 # check if grid directory actually exists
244 if { ! [file isdirectory $griddir] } {
245 _log_and_puts log "Error: tests directory for grid $grid ($griddir) is missing; skipped"
249 # create directory for logging test results
250 if { $logdir != "" } { file mkdir $logdir/$group/$grid }
252 # iterate by all tests in the grid directory
253 if { [catch {glob -directory $griddir -type f *} testfiles] } { continue }
254 foreach casefile [lsort -dictionary $testfiles] {
255 # filter out begin and end files
256 set casename [file tail $casefile]
257 if { $casename == "begin" || $casename == "end" } { continue }
259 lappend tests_list [list $dir $group $grid $casename $casefile]
264 if { [llength $tests_list] < 1 } {
265 error "Error: no tests are found, check you input arguments and variable CSF_TestScriptsPath!"
268 ######################################################
270 ######################################################
272 # log command arguments and environment
273 set log "Command: testgrid $args\nHost: [info hostname]\nStarted on: [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S}]\n"
274 set log "$log\nEnvironment:\n"
275 foreach envar [array names env] {
276 set log "$log$envar=\"$env($envar)\"\n"
280 set refresh_timer [clock seconds]
281 uplevel dchrono _timer reset
282 uplevel dchrono _timer start
284 # if parallel execution is requested, allocate thread pool
285 if { $parallel > 0 } {
286 if { ! [info exists tcl_platform(threaded)] || [catch {package require Thread}] } {
287 _log_and_puts log "Warning: Tcl package Thread is not available, running in sequential mode"
290 set worker [tpool::create -minworkers $parallel -maxworkers $parallel]
291 # suspend the pool until all jobs are posted, to prevent blocking of the process
292 # of starting / processing jobs by running threads
293 tpool::suspend $worker
294 if { $_tests_verbose > 0 } { _log_and_puts log "Executing tests in (up to) $parallel threads" }
299 foreach test_def $tests_list {
300 set dir [lindex $test_def 0]
301 set group [lindex $test_def 1]
302 set grid [lindex $test_def 2]
303 set casename [lindex $test_def 3]
304 set casefile [lindex $test_def 4]
306 # command to set tests for generation of image in results directory
308 if { $logdir != "" } { set imgdir_cmd "set imagedir $logdir/$group/$grid" }
310 # prepare command file for running test case in separate instance of DRAW
311 set fd_cmd [open $logdir/$group/$grid/${casename}.tcl w]
312 puts $fd_cmd "$imgdir_cmd"
313 puts $fd_cmd "set test_image $casename"
314 puts $fd_cmd "_run_test $dir $group $grid $casefile 1"
316 # use dlog command to obtain complete output of the test when it is absent (i.e. since OCCT 6.6.0)
317 # note: this is not needed if echo is set to 1 in call to _run_test above
318 if { ! [catch {dlog get}] } {
319 puts $fd_cmd "puts \[dlog get\]"
321 # else try to use old-style QA_ variables to get more output...
324 set env(QA_print_command) 1
327 # final 'exit' is needed when running on Linux under VirtualGl
331 # commant to run DRAW with a command file;
332 # note that empty string is passed as standard input to avoid possible
333 # hang-ups due to waiting for stdin of the launching process
334 set command "exec <<{} DRAWEXE -f $logdir/$group/$grid/${casename}.tcl"
336 # alternative method to run without temporary file; disabled as it needs too many backslashes
338 # set command "exec <<\"\" DRAWEXE -c $imgdir_cmd\\\; set test_image $casename\\\; \
339 # _run_test $dir $group $grid $casefile\\\; \
340 # puts \\\[dlog get\\\]\\\; exit"
343 # run test case, either in parallel or sequentially
344 if { $parallel > 0 } {
346 set job [tpool::post -nowait $worker "catch \"$command\" output; return \$output"]
347 set job_def($job) [list $logdir $dir $group $grid $casename]
349 # sequential execution
350 catch {eval $command} output
351 _log_test_case $output $logdir $dir $group $grid $casename log
353 # update summary log with requested period
354 if { $logdir != "" && $refresh > 0 && [expr [clock seconds] - $refresh_timer > $refresh] } {
355 # update and dump summary
356 _log_summarize $logdir $log
357 set refresh_timer [clock seconds]
362 # get results of started threads
363 if { $parallel > 0 } {
364 tpool::resume $worker
365 while { [llength [array names job_def]] > 0 } {
366 foreach job [tpool::wait $worker [array names job_def]] {
367 eval _log_test_case \[tpool::get $worker $job\] $job_def($job) log
371 # update summary log with requested period
372 if { $logdir != "" && $refresh > 0 && [clock seconds] > $refresh_timer + $refresh } {
373 _log_summarize $logdir $log
374 set refresh_timer [clock seconds]
377 # release thread pool
378 tpool::release $worker
381 uplevel dchrono _timer stop
382 set time [lindex [split [uplevel dchrono _timer show] "\n"] 0]
384 ######################################################
385 # output summary logs and exit
386 ######################################################
388 _log_summarize $logdir $log $time
389 if { $logdir != "" } {
390 puts "Detailed logs are saved in $logdir"
392 if { $logdir != "" && $xmlfile != "" } {
393 # XML output file is assumed relative to log dir unless it is absolute
394 if { [ file pathtype $xmlfile] == "relative" } {
395 set xmlfile [file normalize $logdir/$xmlfile]
397 _log_xml_summary $logdir $xmlfile $log 0
398 puts "XML summary is saved to $xmlfile"
404 # Procedure to compare results of two runs of test cases
405 help testdiff {Compare results of two executions of tests (CPU times, ...)
406 Use: testdiff dir1 dir2 [options...]
407 Where dir1 and dir2 are directories containing logs of two test runs.
409 -save filename: save resulting log in specified file
410 -subdir name: compare only specified subdirectory (can be nested)
411 -status {same|ok|all}: filter cases for comparing by their status:
412 same - only cases with same status are compared (default)
413 ok - only cases with OK status in both logs are compared
414 all - results are compared regardless of status
416 1 - output only differences
417 2 - output list of logs and directories present in one of dirs only
418 3 - (default) output progress messages
420 proc testdiff {dir1 dir2 args} {
421 if { "$dir1" == "$dir2" } {
422 error "Input directories are the same"
425 ######################################################
427 ######################################################
434 for {set narg 0} {$narg < [llength $args]} {incr narg} {
435 set arg [lindex $args $narg]
438 if { $arg == "-save" } {
440 if { $narg < [llength $args] } {
441 set logfile [lindex $args $narg]
443 error "Error: Option -save must be followed by log file name"
448 # subdirectory to compare
449 if { $arg == "-subdir" } {
451 if { $narg < [llength $args] } {
452 set basename [lindex $args $narg]
454 error "Error: Option -subdir must be followed by subdirectory path"
460 if { $arg == "-status" } {
462 if { $narg < [llength $args] } {
463 set status [lindex $args $narg]
464 } else { set status "" }
465 if { "$status" != "same" && "$status" != "all" && "$status" != "ok" } {
466 error "Error: Option -status must be followed by one of \"same\", \"all\", or \"ok\""
472 if { $arg == "-verbose" } {
474 if { $narg < [llength $args] } {
475 set verbose [expr [lindex $args $narg]]
480 # if { [regexp {^-} $arg] } {
481 error "Error: unsupported option \"$arg\""
485 # run diff procedure (recursive)
486 _test_diff $dir1 $dir2 $basename $status $verbose log
488 # save result to log file
489 if { "$logfile" != "" } {
490 _log_save $logfile $log
496 # Internal procedure to find test case indicated by group, grid, and test case names;
498 # - dir: path to the base directory of the tests group
499 # - gridname: actual name of the grid
500 # - casefile: path to the test case script
501 # if no such test is found, raises error with appropriate message
502 proc _get_test {group grid casename _dir _gridname _casefile} {
504 upvar $_gridname gridname
505 upvar $_casefile casefile
509 # check that environment variable defining paths to test scripts is defined
510 if { ! [info exists env(CSF_TestScriptsPath)] ||
511 [llength $env(CSF_TestScriptsPath)] <= 0 } {
512 error "Error: Environment variable CSF_TestScriptsPath is not defined"
515 # iterate by all script paths
516 foreach dir [_split_path $env(CSF_TestScriptsPath)] {
517 # protection against empty paths
518 set dir [string trim $dir]
519 if { $dir == "" } { continue }
521 # check that directory exists
522 if { ! [file isdirectory $dir] } {
523 puts "Warning: directory $dir listed in CSF_TestScriptsPath does not exist, skipped"
527 # check if test group with given name exists in this dir
528 # if not, continue to the next test dir
529 if { ! [file isdirectory $dir/$group] } { continue }
531 # check that grid with given name (possibly alias) exists; stop otherwise
533 if { ! [file isdirectory $dir/$group/$gridname] } {
534 # check if grid is named by alias rather than by actual name
535 if { [file exists $dir/$group/grids.list] } {
536 set fd [open $dir/$group/grids.list]
537 while { [gets $fd line] >= 0 } {
538 if { [regexp "\[ \t\]*\#.*" $line] } { continue }
539 if { [regexp "^$grid\[ \t\]*\(\[A-Za-z0-9_.-\]+\)\$" $line res gridname] } {
546 if { ! [file isdirectory $dir/$group/$gridname] } { continue }
548 # get actual file name of the script; stop if it cannot be found
549 set casefile $dir/$group/$gridname/$casename
550 if { ! [file exists $casefile] } {
551 # check if this grid is aliased to another one
552 if { [file exists $dir/$group/$gridname/cases.list] } {
553 set fd [open $dir/$group/$gridname/cases.list]
554 if { [gets $fd line] >= 0 } {
555 set casefile [file normalize $dir/$group/$gridname/[string trim $line]/$casename]
560 if { [file exists $casefile] } {
566 # coming here means specified test is not found; report error
567 error [join [list "Error: test case $group / $grid / $casename is not found in paths listed in variable" \
568 "CSF_TestScriptsPath (current value is \"$env(CSF_TestScriptsPath)\")"] "\n"]
571 # Internal procedure to run test case indicated by base directory,
572 # grid and grid names, and test case file path.
573 # The log can be obtained by command "dlog get".
574 proc _run_test {scriptsdir group gridname casefile echo} {
578 uplevel dchrono _timer reset
579 uplevel dchrono _timer start
581 # enable commands logging; switch to old-style mode if dlog command is not present
583 if { [catch {dlog reset}] } {
590 rename puts puts-saved
592 global _tests_verbose
594 # log only output to stdout and stderr, not to file!
595 if {[llength $args] > 1} {
596 set optarg [lindex $args end-1]
597 if { $optarg == "stdout" || $optarg == "stderr" || $optarg == "-newline" } {
598 dlog add [lindex $args end]
601 dlog add [lindex $args end]
604 # reproduce original puts
605 if { $_tests_verbose } {
606 eval puts-saved $args
613 uplevel set casename [file tail $casefile]
614 uplevel set groupname $group
615 uplevel set gridname $gridname
617 if { [file exists $scriptsdir/$group/begin] } {
618 puts "Executing $scriptsdir/$group/begin..."; flush stdout
619 uplevel source $scriptsdir/$group/begin
621 if { [file exists $scriptsdir/$group/$gridname/begin] } {
622 puts "Executing $scriptsdir/$group/$gridname/begin..."; flush stdout
623 uplevel source $scriptsdir/$group/$gridname/begin
626 puts "Executing $casefile..."; flush stdout
627 uplevel source $casefile
629 if { [file exists $scriptsdir/$group/$gridname/end] } {
630 puts "Executing $scriptsdir/$group/$gridname/end..."; flush stdout
631 uplevel source $scriptsdir/$group/$gridname/end
633 if { [file exists $scriptsdir/$group/end] } {
634 puts "Executing $scriptsdir/$group/end..."; flush stdout
635 uplevel source $scriptsdir/$group/end
638 puts "Tcl Exception: $res"
642 if { $dlog_exists } {
647 rename puts-saved puts
652 # stop cpulimit killer if armed by the test
656 uplevel dchrono _timer stop
657 set time [uplevel dchrono _timer show]
658 if [regexp -nocase {CPU user time:[ \t]*([0-9.e-]+)} $time res cpu] {
659 if { $dlog_exists && ! $echo } {
660 dlog add "TOTAL CPU TIME: $cpu sec"
662 puts "TOTAL CPU TIME: $cpu sec"
667 # Internal procedure to check log of test execution and decide if it passed or failed
668 proc _check_log {dir group gridname casename log {_summary {}} {_html_log {}}} {
670 if { $_summary != "" } { upvar $_summary summary }
671 if { $_html_log != "" } { upvar $_html_log html_log }
677 # load definition of 'bad words' indicating test failure
678 # note that rules are loaded in the order of decreasing priority (grid - group - common),
679 # thus grid rules will override group ones
681 foreach rulesfile [list $dir/$group/$gridname/parse.rules $dir/$group/parse.rules $dir/parse.rules] {
682 if [catch {set fd [open $rulesfile r]}] { continue }
683 while { [gets $fd line] >= 0 } {
684 # skip comments and empty lines
685 if { [regexp "\[ \t\]*\#.*" $line] } { continue }
686 if { [string trim $line] == "" } { continue }
688 if { ! [regexp {^([^/]*)/([^/]*)/(.*)$} $line res status rexp comment] } {
689 puts "Warning: cannot recognize parsing rule \"$line\" in file $rulesfile"
692 set status [string trim $status]
693 if { $comment != "" } { set status "$status ([string trim $comment])" }
694 set rexp [regsub -all {\\b} $rexp {\\y}] ;# convert regexp from Perl to Tcl style
695 lappend badwords [list $status $rexp]
699 if { [llength $badwords] <= 0 } {
700 puts "Warning: no definition of error indicators found (check files parse.rules)"
703 # analyse log line-by-line
706 foreach line [split $log "\n"] {
707 # check if line defines specific treatment of some messages
708 if [regexp -nocase {^[ \t]*TODO ([^:]*):(.*)$} $line res platforms pattern] {
709 if { ! [regexp -nocase {\mAll\M} $platforms] &&
710 ! [regexp -nocase "\\m$env(os_type)\\M" $platforms] } {
711 set html_log "$html_log\n$line"
712 continue ;# TODO statement is for another platform
715 # record TODOs that mark unstable cases
716 if { [regexp {[\?]} $platforms] } {
717 set todos_unstable([llength $todos]) 1
720 lappend todos [regsub -all {\\b} [string trim $pattern] {\\y}] ;# convert regexp from Perl to Tcl style
721 set html_log "$html_log\n[_html_highlight BAD $line]"
725 # check for presence of messages indicating test result
727 foreach bw $badwords {
728 if { [regexp [lindex $bw 1] $line] } {
729 # check if this is known bad case
731 for {set i 0} {$i < [llength $todos]} {incr i} {
732 if { [regexp [lindex $todos $i] $line] } {
735 set html_log "$html_log\n[_html_highlight BAD $line]"
740 # if it is not in todo, define status
742 set stat [lindex $bw 0 0]
743 set html_log "$html_log\n[_html_highlight $stat $line]"
744 if { $status == "" && $stat != "OK" && ! [regexp -nocase {^IGNOR} $stat] } {
745 set status [lindex $bw 0]
753 set html_log "$html_log\n$line"
757 # check for presence of TEST COMPLETED statement
758 if { $status == "" && ! [regexp {TEST COMPLETED} $log] } {
759 # check whether absence of TEST COMPLETED is known problem
760 set i [lsearch $todos "TEST INCOMPLETE"]
764 set status "FAILED (no final message is found)"
768 # check declared bad cases and diagnose possible improvement
769 # (bad case declared but not detected).
770 # Note that absence of the problem marked by TODO with question mark
771 # (unstable) is not reported as improvement.
772 if { $status == "" } {
773 for {set i 0} {$i < [llength $todos]} {incr i} {
774 if { ! [info exists todos_unstable($i)] &&
775 (! [info exists todo_count($i)] || $todo_count($i) <= 0) } {
776 set status "IMPROVEMENT (expected problem TODO no. [expr $i + 1] is not detected)"
782 # report test as known bad if at least one of expected problems is found
783 if { $status == "" && [llength [array names todo_count]] > 0 } {
784 set status "BAD (known problem)"
788 if { $status == "" } {set status "OK" }
791 set status "FAILED ($res)"
795 _log_and_puts summary "CASE $group $gridname $casename: $status"
796 set html_log "[_html_highlight [lindex $status 0] $summary]\n$html_log"
799 # Auxiliary procedure putting message to both cout and log variable (list)
800 proc _log_and_puts {logvar message} {
801 if { $logvar != "" } {
803 if [info exists log] {
804 set log "$log$message\n"
812 # Auxiliary procedure to log result on single test case
813 proc _log_test_case {output logdir dir group grid casename logvar} {
816 # check result and make HTML log
817 _check_log $dir $group $grid $casename $output summary html_log
818 set log "$log$summary"
821 if { $logdir != "" } {
822 _log_html $logdir/$group/$grid/$casename.html $html_log "Test $group $grid $casename"
823 _log_save $logdir/$group/$grid/$casename.log "$output\n$summary" "Test $group $grid $casename"
827 # Auxiliary procedure to save log to file
828 proc _log_save {file log {title {}}} {
829 # create missing directories as needed
830 catch {file mkdir [file dirname $file]}
833 if [catch {set fd [open $file w]} res] {
834 error "Error saving log file $file: $res"
844 # Auxiliary procedure to save log to file
845 proc _log_html {file log {title {}}} {
846 # create missing directories as needed
847 catch {file mkdir [file dirname $file]}
850 if [catch {set fd [open $file w]} res] {
851 error "Error saving log file $file: $res"
855 puts $fd "<html><head><title>$title</title><head><body><h1>$title</h1>"
857 # add images if present
858 set imgbasename [file rootname [file tail $file]]
859 foreach img [lsort [glob -nocomplain -directory [file dirname $file] -tails ${imgbasename}*.gif ${imgbasename}*.png ${imgbasename}*.jpg]] {
860 puts $fd "<p><img src=\"$img\"/><p>"
863 # print body, then end and close
866 puts $fd "</pre></body></html>"
872 # Auxiliary method to make text with HTML highlighting according to status
873 proc _html_color {status} {
874 # choose a color for the cell according to result
875 if { $status == "OK" } {
877 } elseif { [regexp -nocase {^FAIL} $status] } {
879 } elseif { [regexp -nocase {^BAD} $status] } {
881 } elseif { [regexp -nocase {^IMP} $status] } {
883 } elseif { [regexp -nocase {^SKIP} $status] } {
885 } elseif { [regexp -nocase {^IGNOR} $status] } {
888 puts "Warning: no color defined for status $status, using red as if FAILED"
893 # Format text line in HTML to be colored according to the status
894 proc _html_highlight {status line} {
895 return "<table><tr><td bgcolor=\"[_html_color $status]\">$line</td></tr></table>"
898 # Internal procedure to generate HTML page presenting log of the tests
899 # execution in tabular form, with links to reports on individual cases
900 proc _log_html_summary {logdir log totals regressions improvements total_time} {
901 global _test_case_regexp
903 # create missing directories as needed
904 catch {file mkdir $logdir}
906 # try to open a file and start HTML
907 if [catch {set fd [open $logdir/summary.html w]} res] {
908 error "Error creating log file: $res"
911 # write HRML header, including command to refresh log if still in progress
912 puts $fd "<html><head>"
913 puts $fd "<title>Tests summary</title>"
914 if { $total_time == "" } {
915 puts $fd "<meta http-equiv=\"refresh\" content=\"10\">"
917 puts $fd "<meta http-equiv=\"pragma\" content=\"NO-CACHE\">"
918 puts $fd "</head><body>"
921 set legend(OK) "Test passed OK"
922 set legend(FAILED) "Test failed (regression)"
923 set legend(BAD) "Known problem"
924 set legend(IMPROVEMENT) "Possible improvement (expected problem not detected)"
925 set legend(SKIPPED) "Test skipped due to lack of data file"
926 puts $fd "<h1>Summary</h1><table>"
927 foreach nbstat $totals {
928 set status [lindex $nbstat 1]
929 if { [info exists legend($status)] } {
930 set comment $legend($status)
932 set comment "User-defined status"
934 puts $fd "<tr><td align=\"right\">[lindex $nbstat 0]</td><td bgcolor=\"[_html_color $status]\">$status</td><td>$comment</td></tr>"
938 # time stamp and elapsed time info
939 if { $total_time != "" } {
940 puts $fd "<p>Generated on [clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S}] on [info hostname] <p> $total_time"
942 puts $fd "<p>NOTE: This is intermediate summary; the tests are still running! This page will refresh automatically until tests are finished."
945 # print regressions and improvements
946 foreach featured [list $regressions $improvements] {
947 if { [llength $featured] <= 1 } { continue }
948 set status [string trim [lindex $featured 0] { :}]
949 puts $fd "<h2>$status</h2>"
952 foreach test [lrange $featured 1 end] {
953 if { ! [regexp {^(.*)\s+([\w.]+)$} $test res gg name] } {
955 set name "Error building short list; check details"
957 if { $gg != $groupgrid } {
958 if { $groupgrid != "" } { puts $fd "</tr>" }
960 puts $fd "<tr><td>$gg</td>"
962 puts $fd "<td bgcolor=\"[_html_color $status]\"><a href=\"[regsub -all { } $gg /]/${name}.html\">$name</a></td>"
964 if { $groupgrid != "" } { puts $fd "</tr>" }
969 puts $fd "<h1>Details</h1>"
971 # process log line-by-line
974 foreach line [lsort -dictionary [split $log "\n"]] {
975 # check that the line is case report in the form "CASE group grid name: result (explanation)"
976 if { ! [regexp $_test_case_regexp $line res grp grd casename result message] } {
981 if { $grp != $group } {
982 if { $letter != "" } { puts $fd "</tr></table>" }
986 puts $fd "<h2>Group $group</h2>"
990 if { $grd != $grid } {
991 if { $letter != "" } { puts $fd "</tr></table>" }
994 puts $fd "<h3>Grid $grid</h3>"
997 # check if test case name is <letter><digit>;
998 # if not, set alnum to period "." to recognize non-standard test name
999 if { ! [regexp {([A-Za-z]+)([0-9]+)} $casename res alnum number] } {
1003 # start new row when letter changes or for non-standard names
1004 if { $alnum != $letter || $alnum == "." } {
1005 if { $letter != "" } {
1006 puts $fd "</tr><tr>"
1008 puts $fd "<table><tr>"
1013 puts $fd "<td bgcolor=\"[_html_color $result]\"><a href=\"$group/$grid/${casename}.html\">$casename</a></td>"
1015 puts $fd "</tr></table>"
1017 # add remaining lines of log as plain text
1018 puts $fd "<h2>Plain text messages</h2>\n<pre>"
1019 foreach line [split $log "\n"] {
1020 if { ! [regexp $_test_case_regexp $line] } {
1026 # close file and exit
1032 # Procedure to dump summary logs of tests
1033 proc _log_summarize {logdir log {total_time {}}} {
1035 # sort log records alphabetically to have the same behavior on Linux and Windows
1036 # (also needed if tests are run in parallel)
1037 set loglist [lsort -dictionary [split $log "\n"]]
1039 # classify test cases by status
1040 foreach line $loglist {
1041 if { [regexp {^CASE ([^:]*): ([[:alnum:]]+).*$} $line res caseid status] } {
1042 lappend stat($status) $caseid
1046 set improvements {Improvements:}
1047 set regressions {Failed:}
1048 if { [info exists stat] } {
1049 foreach status [lsort [array names stat]] {
1050 lappend totals [list [llength $stat($status)] $status]
1052 # separately count improvements (status starting with IMP) and regressions (all except IMP, OK, BAD, and SKIP)
1053 if { [regexp -nocase {^IMP} $status] } {
1054 eval lappend improvements $stat($status)
1055 } elseif { $status != "OK" && ! [regexp -nocase {^BAD} $status] && ! [regexp -nocase {^SKIP} $status] } {
1056 eval lappend regressions $stat($status)
1061 # if time is specified, add totals
1062 if { $total_time != "" } {
1063 if { [llength $improvements] > 1 } {
1064 _log_and_puts log [join $improvements "\n "]
1066 if { [llength $regressions] > 1 } {
1067 _log_and_puts log [join $regressions "\n "]
1069 if { [llength $improvements] == 1 && [llength $regressions] == 1 } {
1070 _log_and_puts log "No regressions"
1072 _log_and_puts log "Total cases: [join $totals {, }]"
1073 _log_and_puts log $total_time
1077 if { $logdir != "" } {
1078 _log_html_summary $logdir $log $totals $regressions $improvements $total_time
1079 _log_save $logdir/tests.log $log "Tests summary"
1085 # Internal procedure to generate XML log in JUnit style, for further
1086 # consumption by Jenkins or similar systems.
1088 # The output is intended to conform to XML schema supported by Jenkins found at
1089 # https://svn.jenkins-ci.org/trunk/hudson/dtkit/dtkit-format/dtkit-junit-model/src/main/resources/com/thalesgroup/dtkit/junit/model/xsd/junit-4.xsd
1091 # The mapping of the fields is inspired by annotated schema of Apache Ant JUnit XML format found at
1092 # http://windyroad.org/dl/Open%20Source/JUnit.xsd
1093 proc _log_xml_summary {logdir filename log include_cout} {
1094 global _test_case_regexp
1096 catch {file mkdir [file dirname $filename]}
1098 # try to open a file and start XML
1099 if [catch {set fd [open $filename w]} res] {
1100 error "Error creating XML summary file $filename: $res"
1102 puts $fd "<?xml version='1.0' encoding='utf-8'?>"
1103 puts $fd "<testsuites>"
1105 # prototype for command to generate test suite tag
1106 set time_and_host "timestamp=\"[clock format [clock seconds] -format {%Y-%m-%dT%H:%M:%S}]\" hostname=\"[info hostname]\""
1107 set cmd_testsuite {puts $fd "<testsuite name=\"$group $grid\" tests=\"$nbtests\" failures=\"$nbfail\" errors=\"$nberr\" time=\"$time\" skipped=\"$nbskip\" $time_and_host>\n$testcases\n</testsuite>\n"}
1109 # sort log and process it line-by-line
1111 foreach line [lsort -dictionary [split $log "\n"]] {
1112 # check that the line is case report in the form "CASE group grid name: result (explanation)"
1113 if { ! [regexp $_test_case_regexp $line res grp grd casename result message] } {
1116 set message [string trim $message " \t\r\n()"]
1118 # start new testsuite for each grid
1119 if { $grp != $group || $grd != $grid } {
1121 # write previous test suite
1122 if [info exists testcases] { eval $cmd_testsuite }
1137 # parse test log and get its CPU time
1140 if { [catch {set fdlog [open $logdir/$group/$grid/${casename}.log r]} ret] } {
1141 puts "Error: cannot open $logdir/$group/$grid/${casename}.log: $ret"
1143 while { [gets $fdlog logline] >= 0 } {
1144 if { $include_cout } {
1145 set testout "$testout$logline\n"
1147 if [regexp -nocase {TOTAL CPU TIME:\s*([\d.]+)\s*sec} $logline res cpu] {
1148 set add_cpu " time=\"$cpu\""
1149 set time [expr $time + $cpu]
1154 if { ! $include_cout } {
1155 set testout "$line\n"
1158 # record test case with its output and status
1159 # Mapping is: SKIPPED, BAD, and OK to OK, all other to failure
1160 set testcases "$testcases\n <testcase name=\"$casename\"$add_cpu status=\"$result\">\n"
1161 set testcases "$testcases\n <system-out>\n$testout </system-out>"
1162 if { $result != "OK" } {
1163 if { [regexp -nocase {^SKIP} $result] } {
1165 set testcases "$testcases\n <error name=\"$result\" message=\"$message\"/>"
1166 } elseif { [regexp -nocase {^BAD} $result] } {
1168 set testcases "$testcases\n <skipped>$message</skipped>"
1171 set testcases "$testcases\n <failure name=\"$result\" message=\"$message\"/>"
1174 set testcases "$testcases\n </testcase>"
1177 # write last test suite
1178 if [info exists testcases] { eval $cmd_testsuite }
1181 puts $fd "</testsuites>"
1186 # define custom platform name
1187 proc _tests_platform_def {} {
1188 global env tcl_platform
1190 if [info exists env(os_type)] { return }
1192 set env(os_type) $tcl_platform(platform)
1194 # use detailed mapping for various versions of Lunix
1195 # (note that mapping is rather non-uniform, for historical reasons)
1196 if { $env(os_type) == "unix" && ! [catch {exec cat /etc/issue} issue] } {
1197 if { [regexp {Mandriva[ \tA-Za-z]+([0-9]+)} $issue res num] } {
1198 set env(os_type) Mandriva$num
1199 } elseif { [regexp {Red Hat[ \tA-Za-z]+([0-9]+)} $issue res num] } {
1200 set env(os_type) RedHat$num
1201 } elseif { [regexp {Debian[ \tA-Za-z/]+([0-9]+)[.]([0-9]+)} $issue res num subnum] } {
1202 set env(os_type) Debian$num$subnum
1203 } elseif { [regexp {CentOS[ \tA-Za-z]+([0-9]+)[.]([0-9]+)} $issue res num subnum] } {
1204 set env(os_type) CentOS$num$subnum
1205 } elseif { [regexp {Scientific[ \tA-Za-z]+([0-9]+)[.]([0-9]+)} $issue res num subnum] } {
1206 set env(os_type) SL$num$subnum
1207 } elseif { [regexp {Fedora Core[ \tA-Za-z]+([0-9]+)} $issue res num] } {
1208 set env(os_type) FedoraCore$num
1210 if { [exec uname -m] == "x86_64" } {
1211 set env(os_type) "$env(os_type)-64"
1217 # Auxiliary procedure to split path specification (usually defined by
1218 # environment variable) into list of directories or files
1219 proc _split_path {pathspec} {
1222 # first replace all \ (which might occur on Windows) by /
1223 regsub -all "\\\\" $pathspec "/" pathspec
1225 # split path by platform-specific separator
1226 return [split $pathspec [_path_separator]]
1229 # Auxiliary procedure to define platform-specific separator for directories in
1230 # path specification
1231 proc _path_separator {} {
1234 # split path by platform-specific separator
1235 if { $tcl_platform(platform) == "windows" } {
1242 # Procedure to locate data file for test given its name.
1243 # The search is performed assuming that the function is called
1244 # from the test case script; the search order is:
1245 # - subdirectories in environment variable CSF_TestDataPath
1246 # If file is not found, raises Tcl error.
1247 proc locate_data_file {filename} {
1248 global env groupname gridname casename
1250 set scriptfile [info script]
1251 if { $scriptfile == "" } {
1252 error "Error: This procedure (locate_data_file) is for use only in test scripts!"
1255 # check sub-directories in paths indicated by CSF_TestDataPath
1256 if { [info exists env(CSF_TestDataPath)] } {
1257 foreach dir [_split_path $env(CSF_TestDataPath)] {
1258 while {[llength $dir] != 0} {
1259 set dir [lassign $dir name]
1260 lappend dir {*}[glob -nocomplain -directory $name -type d *]
1261 if { [file exists $name/$filename] } {
1262 return [file normalize $name/$filename]
1269 if { [file exists [uplevel datadir]/$filename] } {
1270 return [uplevel datadir]/$filename
1274 error [join [list "Error: file $filename could not be found neither in script" \
1275 "directories nor in paths indicated by CSF_TestDataPath environment variable"] "\n"]
1278 # Procedure to make a diff and common of two lists
1279 proc _list_diff {list1 list2 _in1 _in2 _common} {
1282 upvar $_common common
1287 foreach item $list1 {
1288 if { [lsearch -exact $list2 $item] >= 0 } {
1289 lappend common $item
1294 foreach item $list2 {
1295 if { [lsearch -exact $common $item] < 0 } {
1302 # procedure to load a file to Tcl string
1303 proc _read_file {filename} {
1304 set fd [open $filename r]
1305 set result [read -nonewline $fd]
1310 # Procedure to compare results of two runs of test cases
1311 proc _test_diff {dir1 dir2 basename status verbose _logvar {_statvar ""}} {
1314 # prepare variable (array) for collecting statistics
1315 if { "$_statvar" != "" } {
1316 upvar $_statvar stat
1323 # first check subdirectories
1324 set path1 [file join $dir1 $basename]
1325 set path2 [file join $dir2 $basename]
1326 set list1 [glob -directory $path1 -types d -tails -nocomplain *]
1327 set list2 [glob -directory $path2 -types d -tails -nocomplain *]
1328 if { [llength $list1] >0 || [llength $list2] > 0 } {
1329 _list_diff $list1 $list2 in1 in2 common
1330 if { "$verbose" > 1 } {
1331 if { [llength $in1] > 0 } { _log_and_puts log "Only in $path1: $in1" }
1332 if { [llength $in2] > 0 } { _log_and_puts log "Only in $path2: $in2" }
1334 foreach subdir $common {
1335 if { "$verbose" > 2 } {
1336 _log_and_puts log "Checking [file join $basename $subdir]"
1338 _test_diff $dir1 $dir2 [file join $basename $subdir] $status $verbose log stat
1341 # check log files (only if directory has no subdirs)
1342 set list1 [glob -directory $path1 -types f -tails -nocomplain *.log]
1343 set list2 [glob -directory $path2 -types f -tails -nocomplain *.log]
1344 _list_diff $list1 $list2 in1 in2 common
1345 if { "$verbose" > 1 } {
1346 if { [llength $in1] > 0 } { _log_and_puts log "Only in $path1: $in1" }
1347 if { [llength $in2] > 0 } { _log_and_puts log "Only in $path2: $in2" }
1349 foreach logfile $common {
1351 set log1 [_read_file [file join $dir1 $basename $logfile]]
1352 set log2 [_read_file [file join $dir2 $basename $logfile]]
1354 # check execution statuses
1355 set status1 UNDEFINED
1356 set status2 UNDEFINED
1357 if { ! [regexp {CASE [^:]*:\s*([\w]+)} $log1 res1 status1] ||
1358 ! [regexp {CASE [^:]*:\s*([\w]+)} $log2 res2 status2] ||
1359 "$status1" != "$status2" } {
1360 _log_and_puts log "STATUS [split $basename /] [file rootname $logfile]: $status1 / $status2"
1362 # if test statuses are different, further comparison makes
1363 # no sense unless explicitly requested
1364 if { "$status" != "all" } {
1368 if { "$status" == "ok" && "$status1" != "OK" } {
1375 if { [regexp {TOTAL CPU TIME:\s*([\d.]+)} $log1 res1 cpu1] &&
1376 [regexp {TOTAL CPU TIME:\s*([\d.]+)} $log2 res1 cpu2] } {
1377 set stat(cpu1) [expr $stat(cpu1) + $cpu1]
1378 set stat(cpu2) [expr $stat(cpu2) + $cpu2]
1380 # compare CPU times with 10% precision (but not less 0.5 sec)
1381 if { [expr abs ($cpu1 - $cpu2) > 0.5 + 0.05 * abs ($cpu1 + $cpu2)] } {
1382 _log_and_puts log "CPU [split $basename /] [file rootname $logfile]: $cpu1 / $cpu2"
1388 if { "$_statvar" == "" } {
1389 _log_and_puts log "Total CPU difference: $stat(cpu1) / $stat(cpu2)"